blob: c24d41b6855d39c545dd6078652abee5c5ecbe51 [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"
Evan Cheng55d42002011-01-08 01:24:27 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/GlobalAlias.h"
38#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/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
Evan Cheng8688a582013-01-29 02:32:37 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
611 setOperationAction(ISD::FSIN , MVT::f32, Expand);
612 setOperationAction(ISD::FCOS , MVT::f32, Expand);
613 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614
Chris Lattnera54aa942006-01-29 06:26:08 +0000615 // Expand FP immediates into loads from the stack, except for the special
616 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000617 addLegalFPImmediate(APFloat(+0.0)); // xorpd
618 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000619 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 // Use SSE for f32, x87 for f64.
621 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000622 addRegisterClass(MVT::f32, &X86::FR32RegClass);
623 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000624
625 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000627
628 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000632
633 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
635 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000636
637 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000638 setOperationAction(ISD::FSIN , MVT::f32, Expand);
639 setOperationAction(ISD::FCOS , MVT::f32, Expand);
640 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641
Nate Begemane1795842008-02-14 08:57:00 +0000642 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 addLegalFPImmediate(APFloat(+0.0f)); // xorps
644 addLegalFPImmediate(APFloat(+0.0)); // FLD0
645 addLegalFPImmediate(APFloat(+1.0)); // FLD1
646 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
647 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
648
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000649 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000650 setOperationAction(ISD::FSIN , MVT::f64, Expand);
651 setOperationAction(ISD::FCOS , MVT::f64, Expand);
652 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000653 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000654 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000655 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000656 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000657 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
658 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000659
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
661 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
662 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
663 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000664
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000665 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000666 setOperationAction(ISD::FSIN , MVT::f64, Expand);
667 setOperationAction(ISD::FSIN , MVT::f32, Expand);
668 setOperationAction(ISD::FCOS , MVT::f64, Expand);
669 setOperationAction(ISD::FCOS , MVT::f32, Expand);
670 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
671 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000672 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000673 addLegalFPImmediate(APFloat(+0.0)); // FLD0
674 addLegalFPImmediate(APFloat(+1.0)); // FLD1
675 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
676 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000677 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
678 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
679 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
680 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000681 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000682
Cameron Zwarich33390842011-07-08 21:39:21 +0000683 // We don't support FMA.
684 setOperationAction(ISD::FMA, MVT::f64, Expand);
685 setOperationAction(ISD::FMA, MVT::f32, Expand);
686
Dale Johannesen59a58732007-08-05 18:49:15 +0000687 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000688 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000689 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
691 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000692 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000693 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000694 addLegalFPImmediate(TmpFlt); // FLD0
695 TmpFlt.changeSign();
696 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000697
698 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000699 APFloat TmpFlt2(+1.0);
700 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
701 &ignored);
702 addLegalFPImmediate(TmpFlt2); // FLD1
703 TmpFlt2.changeSign();
704 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
705 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000706
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000707 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000708 setOperationAction(ISD::FSIN , MVT::f80, Expand);
709 setOperationAction(ISD::FCOS , MVT::f80, Expand);
710 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000711 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000712
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000713 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
714 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
715 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
716 setOperationAction(ISD::FRINT, MVT::f80, Expand);
717 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000718 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000719 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000720
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000721 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
723 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
724 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000725
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FLOG, MVT::f80, Expand);
727 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
728 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
729 setOperationAction(ISD::FEXP, MVT::f80, Expand);
730 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000731
Mon P Wangf007a8b2008-11-06 05:31:54 +0000732 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000733 // (for widening) or expand (for scalarization). Then we will selectively
734 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000735 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
736 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000737 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000738 setOperationAction(ISD::ADD , VT, Expand);
739 setOperationAction(ISD::SUB , VT, Expand);
740 setOperationAction(ISD::FADD, VT, Expand);
741 setOperationAction(ISD::FNEG, VT, Expand);
742 setOperationAction(ISD::FSUB, VT, Expand);
743 setOperationAction(ISD::MUL , VT, Expand);
744 setOperationAction(ISD::FMUL, VT, Expand);
745 setOperationAction(ISD::SDIV, VT, Expand);
746 setOperationAction(ISD::UDIV, VT, Expand);
747 setOperationAction(ISD::FDIV, VT, Expand);
748 setOperationAction(ISD::SREM, VT, Expand);
749 setOperationAction(ISD::UREM, VT, Expand);
750 setOperationAction(ISD::LOAD, VT, Expand);
751 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
752 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
753 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
754 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
755 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
756 setOperationAction(ISD::FABS, VT, Expand);
757 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000758 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000759 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000760 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000761 setOperationAction(ISD::FREM, VT, Expand);
762 setOperationAction(ISD::FMA, VT, Expand);
763 setOperationAction(ISD::FPOWI, VT, Expand);
764 setOperationAction(ISD::FSQRT, VT, Expand);
765 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
766 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000767 setOperationAction(ISD::FCEIL, VT, Expand);
768 setOperationAction(ISD::FTRUNC, VT, Expand);
769 setOperationAction(ISD::FRINT, VT, Expand);
770 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000771 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
772 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
773 setOperationAction(ISD::SDIVREM, VT, Expand);
774 setOperationAction(ISD::UDIVREM, VT, Expand);
775 setOperationAction(ISD::FPOW, VT, Expand);
776 setOperationAction(ISD::CTPOP, VT, Expand);
777 setOperationAction(ISD::CTTZ, VT, Expand);
778 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
779 setOperationAction(ISD::CTLZ, VT, Expand);
780 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
781 setOperationAction(ISD::SHL, VT, Expand);
782 setOperationAction(ISD::SRA, VT, Expand);
783 setOperationAction(ISD::SRL, VT, Expand);
784 setOperationAction(ISD::ROTL, VT, Expand);
785 setOperationAction(ISD::ROTR, VT, Expand);
786 setOperationAction(ISD::BSWAP, VT, Expand);
787 setOperationAction(ISD::SETCC, VT, Expand);
788 setOperationAction(ISD::FLOG, VT, Expand);
789 setOperationAction(ISD::FLOG2, VT, Expand);
790 setOperationAction(ISD::FLOG10, VT, Expand);
791 setOperationAction(ISD::FEXP, VT, Expand);
792 setOperationAction(ISD::FEXP2, VT, Expand);
793 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
794 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
795 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
796 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
797 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
798 setOperationAction(ISD::TRUNCATE, VT, Expand);
799 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
800 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
801 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
802 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000803 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
804 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000805 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000806 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000807 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
808 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
809 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000810 }
811
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
813 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000814 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000815 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000816 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000817 }
818
Dale Johannesen0488fb62010-09-30 23:57:10 +0000819 // MMX-sized vectors (other than x86mmx) are expected to be expanded
820 // into smaller operations.
821 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
822 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
823 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
824 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
825 setOperationAction(ISD::AND, MVT::v8i8, Expand);
826 setOperationAction(ISD::AND, MVT::v4i16, Expand);
827 setOperationAction(ISD::AND, MVT::v2i32, Expand);
828 setOperationAction(ISD::AND, MVT::v1i64, Expand);
829 setOperationAction(ISD::OR, MVT::v8i8, Expand);
830 setOperationAction(ISD::OR, MVT::v4i16, Expand);
831 setOperationAction(ISD::OR, MVT::v2i32, Expand);
832 setOperationAction(ISD::OR, MVT::v1i64, Expand);
833 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
834 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
835 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
836 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
840 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
842 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
843 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
844 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
845 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000846 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
847 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
848 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
849 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000850
Craig Topper1accb7e2012-01-10 06:54:16 +0000851 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000852 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000853
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
855 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
856 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
857 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
858 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
859 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000860 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
862 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
863 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
864 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
865 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000866 }
867
Craig Topper1accb7e2012-01-10 06:54:16 +0000868 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000869 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000870
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000871 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
872 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000873 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
874 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
875 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
876 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
879 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
880 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
881 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000882 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
884 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
885 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
886 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
887 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
888 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
889 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
890 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
891 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
892 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
893 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
894 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000895 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000896
Nadav Rotem354efd82011-09-18 14:57:03 +0000897 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000898 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
899 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
900 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
903 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
904 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
905 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
906 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000907
Evan Cheng2c3ae372006-04-12 21:21:57 +0000908 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000909 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000910 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000911 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000913 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000914 // Do not attempt to custom lower non-128-bit vectors
915 if (!VT.is128BitVector())
916 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000917 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
918 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
919 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000920 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000921
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
923 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
924 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
925 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
926 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
927 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000928
Nate Begemancdd1eec2008-02-12 22:51:28 +0000929 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
931 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000932 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000933
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000934 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000935 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000936 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000937
938 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000939 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000940 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000941
Craig Topper0d1f1762012-08-12 00:34:56 +0000942 setOperationAction(ISD::AND, VT, Promote);
943 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
944 setOperationAction(ISD::OR, VT, Promote);
945 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
946 setOperationAction(ISD::XOR, VT, Promote);
947 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
948 setOperationAction(ISD::LOAD, VT, Promote);
949 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
950 setOperationAction(ISD::SELECT, VT, Promote);
951 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000952 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000953
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000955
Evan Cheng2c3ae372006-04-12 21:21:57 +0000956 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
958 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
959 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
960 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000961
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
963 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000964
Michael Liaoa7554632012-10-23 17:36:08 +0000965 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
966 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000967 // As there is no 64-bit GPR available, we need build a special custom
968 // sequence to convert from v2i32 to v2f32.
969 if (!Subtarget->is64Bit())
970 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000971
Michael Liao9d796db2012-10-10 16:32:15 +0000972 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000973 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000974
Michael Liaob8150d82012-09-10 18:33:51 +0000975 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000976 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000977
Craig Topperd0a31172012-01-10 06:37:29 +0000978 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000979 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
980 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
981 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
982 setOperationAction(ISD::FRINT, MVT::f32, Legal);
983 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
984 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
985 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
986 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
987 setOperationAction(ISD::FRINT, MVT::f64, Legal);
988 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
989
Craig Topper12fb5c62012-09-08 17:42:27 +0000990 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000991 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
992 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
993 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
994 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000995 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000996 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
997 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
998 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
999 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001000
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001002 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001003
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001004 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1005 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1006 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1007 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1008 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001009
Nate Begeman14d12ca2008-02-11 04:19:36 +00001010 // i8 and i16 vectors are custom , because the source register and source
1011 // source memory operand types are not the same width. f32 vectors are
1012 // custom since the immediate controlling the insert encodes additional
1013 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1015 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1016 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1017 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1020 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1021 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1022 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001023
Pete Coopera77214a2011-11-14 19:38:42 +00001024 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001025 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001026 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001027 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1028 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001029 }
1030 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001031
Craig Topper1accb7e2012-01-10 06:54:16 +00001032 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001033 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001034 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001035
Nadav Rotem43012222011-05-11 08:12:09 +00001036 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001037 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001038
Nadav Rotem43012222011-05-11 08:12:09 +00001039 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001040 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001041
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001042 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001043 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1044 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1045
1046 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1047 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1048
1049 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1050 } else {
1051 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1052 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1053
1054 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1055 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1056
1057 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1058 }
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001059 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1060 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001061 }
1062
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001063 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001064 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1065 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1066 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1067 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1068 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1069 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001070
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001072 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1073 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001074
Owen Anderson825b72b2009-08-11 20:47:22 +00001075 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1076 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1077 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1078 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1079 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001080 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001081 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1082 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1083 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1084 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001086 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001087
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1089 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1090 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1091 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1092 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001093 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001094 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1095 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1096 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1097 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001099 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001100
Michael Liaobedcbd42012-10-16 18:14:11 +00001101 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001102 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001103
1104 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1105
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001106 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1107 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001108 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001109
Michael Liaoa7554632012-10-23 17:36:08 +00001110 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1111 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1112 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1113
Michael Liaob8150d82012-09-10 18:33:51 +00001114 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1115
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001116 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1117 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1118
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001119 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1120 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1121
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001122 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001123 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001124
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001125 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1126
Duncan Sands28b77e92011-09-06 19:07:46 +00001127 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1128 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1129 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1130 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001131
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001132 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1133 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1134 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1135
Craig Topperaaa643c2011-11-09 07:28:55 +00001136 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1137 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1138 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1139 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001140
Nadav Rotem0509db22012-12-28 05:45:24 +00001141 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1142 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1143 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1144 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1145 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1146 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001147
Craig Topperbf404372012-08-31 15:40:30 +00001148 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001149 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1150 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1151 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1152 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1153 setOperationAction(ISD::FMA, MVT::f32, Legal);
1154 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001155 }
Craig Topper880ef452012-08-11 22:34:26 +00001156
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001157 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001158 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1159 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1160 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1161 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001162
Craig Topperaaa643c2011-11-09 07:28:55 +00001163 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1164 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1165 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1166 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001167
Craig Topperaaa643c2011-11-09 07:28:55 +00001168 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1169 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1170 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001171 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001172
1173 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001174
1175 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1176 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1177
1178 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1179 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1180
1181 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001182
1183 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001184 } else {
1185 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1186 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1187 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1188 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1189
1190 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1191 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1192 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1193 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1194
1195 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1196 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1197 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1198 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001199
1200 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1201 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1202
1203 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1204 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1205
1206 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001207 }
Craig Topper13894fa2011-08-24 06:14:18 +00001208
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001209 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001210 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1211 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001212 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001213
1214 // Extract subvector is special because the value type
1215 // (result) is 128-bit but the source is 256-bit wide.
1216 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001217 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001218
1219 // Do not attempt to custom lower other non-256-bit vectors
1220 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001221 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001222
Craig Topper0d1f1762012-08-12 00:34:56 +00001223 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1224 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1225 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1226 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1227 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1228 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1229 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001230 }
1231
David Greene54d8eba2011-01-27 22:38:56 +00001232 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001233 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001234 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001235
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001236 // Do not attempt to promote non-256-bit vectors
1237 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001238 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001239
Craig Topper0d1f1762012-08-12 00:34:56 +00001240 setOperationAction(ISD::AND, VT, Promote);
1241 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1242 setOperationAction(ISD::OR, VT, Promote);
1243 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1244 setOperationAction(ISD::XOR, VT, Promote);
1245 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1246 setOperationAction(ISD::LOAD, VT, Promote);
1247 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1248 setOperationAction(ISD::SELECT, VT, Promote);
1249 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001250 }
David Greene9b9838d2009-06-29 16:47:10 +00001251 }
1252
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001253 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1254 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001255 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1256 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001257 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1258 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001259 }
1260
Evan Cheng6be2c582006-04-05 23:38:46 +00001261 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001263 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001264
Eli Friedman962f5492010-06-02 19:35:46 +00001265 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1266 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001267 //
Eli Friedman962f5492010-06-02 19:35:46 +00001268 // FIXME: We really should do custom legalization for addition and
1269 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1270 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001271 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1272 // Add/Sub/Mul with overflow operations are custom lowered.
1273 MVT VT = IntVTs[i];
1274 setOperationAction(ISD::SADDO, VT, Custom);
1275 setOperationAction(ISD::UADDO, VT, Custom);
1276 setOperationAction(ISD::SSUBO, VT, Custom);
1277 setOperationAction(ISD::USUBO, VT, Custom);
1278 setOperationAction(ISD::SMULO, VT, Custom);
1279 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001280 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001281
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001282 // There are no 8-bit 3-address imul/mul instructions
1283 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1284 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001285
Evan Chengd54f2d52009-03-31 19:38:51 +00001286 if (!Subtarget->is64Bit()) {
1287 // These libcalls are not available in 32-bit.
1288 setLibcallName(RTLIB::SHL_I128, 0);
1289 setLibcallName(RTLIB::SRL_I128, 0);
1290 setLibcallName(RTLIB::SRA_I128, 0);
1291 }
1292
Evan Cheng8688a582013-01-29 02:32:37 +00001293 // Combine sin / cos into one node or libcall if possible.
1294 if (Subtarget->hasSinCos()) {
1295 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1296 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001297 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001298 // For MacOSX, we don't want to the normal expansion of a libcall to
1299 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1300 // traffic.
1301 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1302 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1303 }
1304 }
1305
Evan Cheng206ee9d2006-07-07 08:33:52 +00001306 // We have target-specific dag combine patterns for the following nodes:
1307 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001308 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001309 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001310 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001311 setTargetDAGCombine(ISD::SHL);
1312 setTargetDAGCombine(ISD::SRA);
1313 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001314 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001315 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001316 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001317 setTargetDAGCombine(ISD::FADD);
1318 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001319 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001320 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001321 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001322 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001323 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001324 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001325 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001326 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001327 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001328 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001329 if (Subtarget->is64Bit())
1330 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001331 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001332
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001333 computeRegisterProperties();
1334
Evan Cheng05219282011-01-06 06:52:41 +00001335 // On Darwin, -Os means optimize for size without hurting performance,
1336 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001337 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001338 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001339 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001340 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1341 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1342 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001343 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001344 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001345
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001346 // Predictable cmov don't hurt on atom because it's in-order.
1347 predictableSelectIsExpensive = !Subtarget->isAtom();
1348
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001349 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001350}
1351
Duncan Sands28b77e92011-09-06 19:07:46 +00001352EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1353 if (!VT.isVector()) return MVT::i8;
1354 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001355}
1356
Evan Cheng29286502008-01-23 23:17:41 +00001357/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1358/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001359static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001360 if (MaxAlign == 16)
1361 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001362 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001363 if (VTy->getBitWidth() == 128)
1364 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001365 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001366 unsigned EltAlign = 0;
1367 getMaxByValAlign(ATy->getElementType(), EltAlign);
1368 if (EltAlign > MaxAlign)
1369 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001370 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001371 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1372 unsigned EltAlign = 0;
1373 getMaxByValAlign(STy->getElementType(i), EltAlign);
1374 if (EltAlign > MaxAlign)
1375 MaxAlign = EltAlign;
1376 if (MaxAlign == 16)
1377 break;
1378 }
1379 }
Evan Cheng29286502008-01-23 23:17:41 +00001380}
1381
1382/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1383/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001384/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1385/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001386unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001387 if (Subtarget->is64Bit()) {
1388 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001389 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001390 if (TyAlign > 8)
1391 return TyAlign;
1392 return 8;
1393 }
1394
Evan Cheng29286502008-01-23 23:17:41 +00001395 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001396 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001397 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001398 return Align;
1399}
Chris Lattner2b02a442007-02-25 08:29:00 +00001400
Evan Chengf0df0312008-05-15 08:39:06 +00001401/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001402/// and store operations as a result of memset, memcpy, and memmove
1403/// lowering. If DstAlign is zero that means it's safe to destination
1404/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1405/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001406/// probably because the source does not need to be loaded. If 'IsMemset' is
1407/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1408/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1409/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001410/// It returns EVT::Other if the type should be determined using generic
1411/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001412EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001413X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1414 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001415 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001416 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001417 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001418 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001419 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001420 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1421 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001422 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001423 (Subtarget->isUnalignedMemAccessFast() ||
1424 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001425 (SrcAlign == 0 || SrcAlign >= 16)))) {
1426 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001427 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001428 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001429 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001430 return MVT::v8f32;
1431 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001432 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001433 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001434 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001435 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001436 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001437 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001438 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001439 // Do not use f64 to lower memcpy if source is string constant. It's
1440 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001441 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001442 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001443 }
Evan Chengf0df0312008-05-15 08:39:06 +00001444 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 return MVT::i64;
1446 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001447}
1448
Evan Cheng7d342672012-12-12 01:32:07 +00001449bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001450 if (VT == MVT::f32)
1451 return X86ScalarSSEf32;
1452 else if (VT == MVT::f64)
1453 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001454 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001455}
1456
Evan Cheng376642e2012-12-10 23:21:26 +00001457bool
1458X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1459 if (Fast)
1460 *Fast = Subtarget->isUnalignedMemAccessFast();
1461 return true;
1462}
1463
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001464/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1465/// current function. The returned value is a member of the
1466/// MachineJumpTableInfo::JTEntryKind enum.
1467unsigned X86TargetLowering::getJumpTableEncoding() const {
1468 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1469 // symbol.
1470 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1471 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001472 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001473
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001474 // Otherwise, use the normal jump table encoding heuristics.
1475 return TargetLowering::getJumpTableEncoding();
1476}
1477
Chris Lattnerc64daab2010-01-26 05:02:42 +00001478const MCExpr *
1479X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1480 const MachineBasicBlock *MBB,
1481 unsigned uid,MCContext &Ctx) const{
1482 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1483 Subtarget->isPICStyleGOT());
1484 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1485 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001486 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1487 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001488}
1489
Evan Chengcc415862007-11-09 01:32:10 +00001490/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1491/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001492SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001493 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001494 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001495 // This doesn't have DebugLoc associated with it, but is not really the
1496 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001497 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001498 return Table;
1499}
1500
Chris Lattner589c6f62010-01-26 06:28:43 +00001501/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1502/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1503/// MCExpr.
1504const MCExpr *X86TargetLowering::
1505getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1506 MCContext &Ctx) const {
1507 // X86-64 uses RIP relative addressing based on the jump table label.
1508 if (Subtarget->isPICStyleRIPRel())
1509 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1510
1511 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001512 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001513}
1514
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001515// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001516std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001517X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001518 const TargetRegisterClass *RRC = 0;
1519 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001520 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001521 default:
1522 return TargetLowering::findRepresentativeClass(VT);
1523 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001524 RRC = Subtarget->is64Bit() ?
1525 (const TargetRegisterClass*)&X86::GR64RegClass :
1526 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001527 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001528 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001529 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001530 break;
1531 case MVT::f32: case MVT::f64:
1532 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1533 case MVT::v4f32: case MVT::v2f64:
1534 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1535 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001536 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001537 break;
1538 }
1539 return std::make_pair(RRC, Cost);
1540}
1541
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001542bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1543 unsigned &Offset) const {
1544 if (!Subtarget->isTargetLinux())
1545 return false;
1546
1547 if (Subtarget->is64Bit()) {
1548 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1549 Offset = 0x28;
1550 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1551 AddressSpace = 256;
1552 else
1553 AddressSpace = 257;
1554 } else {
1555 // %gs:0x14 on i386
1556 Offset = 0x14;
1557 AddressSpace = 256;
1558 }
1559 return true;
1560}
1561
Chris Lattner2b02a442007-02-25 08:29:00 +00001562//===----------------------------------------------------------------------===//
1563// Return Value Calling Convention Implementation
1564//===----------------------------------------------------------------------===//
1565
Chris Lattner59ed56b2007-02-28 04:55:35 +00001566#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001567
Michael J. Spencerec38de22010-10-10 22:04:20 +00001568bool
Eric Christopher471e4222011-06-08 23:55:35 +00001569X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001570 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001571 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001572 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001573 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001574 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001575 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001576 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001577}
1578
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579SDValue
1580X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001581 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001583 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001584 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001585 MachineFunction &MF = DAG.getMachineFunction();
1586 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Chris Lattner9774c912007-02-27 05:28:59 +00001588 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001589 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 RVLocs, *DAG.getContext());
1591 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Dan Gohman475871a2008-07-27 21:46:04 +00001593 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001595 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1596 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001597 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1598 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001599
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001600 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001601 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1602 CCValAssign &VA = RVLocs[i];
1603 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001604 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001605 EVT ValVT = ValToCopy.getValueType();
1606
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001607 // Promote values to the appropriate types
1608 if (VA.getLocInfo() == CCValAssign::SExt)
1609 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1610 else if (VA.getLocInfo() == CCValAssign::ZExt)
1611 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1612 else if (VA.getLocInfo() == CCValAssign::AExt)
1613 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1614 else if (VA.getLocInfo() == CCValAssign::BCvt)
1615 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1616
Dale Johannesenc4510512010-09-24 19:05:48 +00001617 // If this is x86-64, and we disabled SSE, we can't return FP values,
1618 // or SSE or MMX vectors.
1619 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1620 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001621 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001622 report_fatal_error("SSE register return with SSE disabled");
1623 }
1624 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1625 // llvm-gcc has never done it right and no one has noticed, so this
1626 // should be OK for now.
1627 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001628 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001629 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001630
Chris Lattner447ff682008-03-11 03:23:40 +00001631 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1632 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001633 if (VA.getLocReg() == X86::ST0 ||
1634 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001635 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1636 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001637 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001638 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001639 RetOps.push_back(ValToCopy);
1640 // Don't emit a copytoreg.
1641 continue;
1642 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001643
Evan Cheng242b38b2009-02-23 09:03:22 +00001644 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1645 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001646 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001647 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001648 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001649 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001650 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1651 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001652 // If we don't have SSE2 available, convert to v4f32 so the generated
1653 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001654 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001655 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001656 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001657 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001658 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001659
Dale Johannesendd64c412009-02-04 00:33:20 +00001660 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001661 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001662 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001663 }
Dan Gohman61a92132008-04-21 23:59:07 +00001664
Eli Benderskya5597f02013-01-25 22:07:43 +00001665 // The x86-64 ABIs require that for returning structs by value we copy
1666 // the sret argument into %rax/%eax (depending on ABI) for the return.
1667 // We saved the argument into a virtual register in the entry block,
1668 // so now we copy the value out and into %rax/%eax.
Dan Gohman61a92132008-04-21 23:59:07 +00001669 if (Subtarget->is64Bit() &&
1670 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1671 MachineFunction &MF = DAG.getMachineFunction();
1672 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1673 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001674 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001675 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001676 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001677
Eli Benderskya5597f02013-01-25 22:07:43 +00001678 unsigned RetValReg = Subtarget->isTarget64BitILP32() ? X86::EAX : X86::RAX;
1679 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001680 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001681
Eli Benderskya5597f02013-01-25 22:07:43 +00001682 // RAX/EAX now acts like a return value.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001683 RetOps.push_back(DAG.getRegister(RetValReg, MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001684 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001685
Chris Lattner447ff682008-03-11 03:23:40 +00001686 RetOps[0] = Chain; // Update chain.
1687
1688 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001689 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001690 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001691
1692 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001694}
1695
Evan Chengbf010eb2012-04-10 01:51:00 +00001696bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001697 if (N->getNumValues() != 1)
1698 return false;
1699 if (!N->hasNUsesOfValue(1, 0))
1700 return false;
1701
Evan Chengbf010eb2012-04-10 01:51:00 +00001702 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001703 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001704 if (Copy->getOpcode() == ISD::CopyToReg) {
1705 // If the copy has a glue operand, we conservatively assume it isn't safe to
1706 // perform a tail call.
1707 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1708 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001709 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001710 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001711 return false;
1712
Evan Cheng1bf891a2010-12-01 22:59:46 +00001713 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001714 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001715 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001716 if (UI->getOpcode() != X86ISD::RET_FLAG)
1717 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001718 HasRet = true;
1719 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001720
Evan Chengbf010eb2012-04-10 01:51:00 +00001721 if (!HasRet)
1722 return false;
1723
1724 Chain = TCChain;
1725 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001726}
1727
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001728MVT
1729X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001730 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001731 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001732 // TODO: Is this also valid on 32-bit?
1733 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001734 ReturnMVT = MVT::i8;
1735 else
1736 ReturnMVT = MVT::i32;
1737
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001738 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001739 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001740}
1741
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742/// LowerCallResult - Lower the result values of a call into the
1743/// appropriate copies out of appropriate physical registers.
1744///
1745SDValue
1746X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001747 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 const SmallVectorImpl<ISD::InputArg> &Ins,
1749 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001750 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001751
Chris Lattnere32bbf62007-02-28 07:09:55 +00001752 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001753 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001754 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001755 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001756 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001758
Chris Lattner3085e152007-02-25 08:59:22 +00001759 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001760 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001761 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001762 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Torok Edwin3f142c32009-02-01 18:15:56 +00001764 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001766 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001767 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001768 }
1769
Evan Cheng79fb3b42009-02-20 20:43:02 +00001770 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001771
1772 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001773 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001774 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001775 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001776 // instead.
1777 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1778 // If we prefer to use the value in xmm registers, copy it out as f80 and
1779 // use a truncate to move it from fp stack reg to xmm reg.
1780 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001781 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001782 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1783 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001784 Val = Chain.getValue(0);
1785
1786 // Round the f80 to the right size, which also moves it to the appropriate
1787 // xmm register.
1788 if (CopyVT != VA.getValVT())
1789 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1790 // This truncation won't change the value.
1791 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001792 } else {
1793 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1794 CopyVT, InFlag).getValue(1);
1795 Val = Chain.getValue(0);
1796 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001797 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001799 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001800
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001802}
1803
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001804//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001805// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001806//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001807// StdCall calling convention seems to be standard for many Windows' API
1808// routines and around. It differs from C calling convention just a little:
1809// callee should clean up the stack, not caller. Symbols should be also
1810// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001811// For info on fast calling convention see Fast Calling Convention (tail call)
1812// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001813
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001815/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001816enum StructReturnType {
1817 NotStructReturn,
1818 RegStructReturn,
1819 StackStructReturn
1820};
1821static StructReturnType
1822callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001823 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001824 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001825
Rafael Espindola1cee7102012-07-25 13:41:10 +00001826 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1827 if (!Flags.isSRet())
1828 return NotStructReturn;
1829 if (Flags.isInReg())
1830 return RegStructReturn;
1831 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001832}
1833
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001834/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001835/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001836static StructReturnType
1837argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001838 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001839 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001840
Rafael Espindola1cee7102012-07-25 13:41:10 +00001841 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1842 if (!Flags.isSRet())
1843 return NotStructReturn;
1844 if (Flags.isInReg())
1845 return RegStructReturn;
1846 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001847}
1848
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001849/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1850/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001851/// the specific parameter attribute. The copy will be passed as a byval
1852/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001853static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001854CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001855 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1856 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001857 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001858
Dale Johannesendd64c412009-02-04 00:33:20 +00001859 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001860 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001861 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001862}
1863
Chris Lattner29689432010-03-11 00:22:57 +00001864/// IsTailCallConvention - Return true if the calling convention is one that
1865/// supports tail call optimization.
1866static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001867 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1868 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001869}
1870
Evan Cheng485fafc2011-03-21 01:19:09 +00001871bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001872 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001873 return false;
1874
1875 CallSite CS(CI);
1876 CallingConv::ID CalleeCC = CS.getCallingConv();
1877 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1878 return false;
1879
1880 return true;
1881}
1882
Evan Cheng0c439eb2010-01-27 00:07:07 +00001883/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1884/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001885static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1886 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001887 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001888}
1889
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890SDValue
1891X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001892 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001893 const SmallVectorImpl<ISD::InputArg> &Ins,
1894 DebugLoc dl, SelectionDAG &DAG,
1895 const CCValAssign &VA,
1896 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001897 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001898 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001900 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1901 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001902 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001903 EVT ValVT;
1904
1905 // If value is passed by pointer we have address passed instead of the value
1906 // itself.
1907 if (VA.getLocInfo() == CCValAssign::Indirect)
1908 ValVT = VA.getLocVT();
1909 else
1910 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001911
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001912 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001913 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001914 // In case of tail call optimization mark all arguments mutable. Since they
1915 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001916 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001917 unsigned Bytes = Flags.getByValSize();
1918 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1919 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001920 return DAG.getFrameIndex(FI, getPointerTy());
1921 } else {
1922 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001923 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001924 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1925 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001926 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001927 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001928 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001929}
1930
Dan Gohman475871a2008-07-27 21:46:04 +00001931SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001932X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001933 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934 bool isVarArg,
1935 const SmallVectorImpl<ISD::InputArg> &Ins,
1936 DebugLoc dl,
1937 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001938 SmallVectorImpl<SDValue> &InVals)
1939 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001940 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001941 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001942
Gordon Henriksen86737662008-01-05 16:56:59 +00001943 const Function* Fn = MF.getFunction();
1944 if (Fn->hasExternalLinkage() &&
1945 Subtarget->isTargetCygMing() &&
1946 Fn->getName() == "main")
1947 FuncInfo->setForceFramePointer(true);
1948
Evan Cheng1bc78042006-04-26 01:20:17 +00001949 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001950 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001951 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001952 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001953
Chris Lattner29689432010-03-11 00:22:57 +00001954 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001955 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001956
Chris Lattner638402b2007-02-28 07:00:42 +00001957 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001958 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001959 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001960 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001961
1962 // Allocate shadow area for Win64
1963 if (IsWin64) {
1964 CCInfo.AllocateStack(32, 8);
1965 }
1966
Duncan Sands45907662010-10-31 13:21:44 +00001967 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001968
Chris Lattnerf39f7712007-02-28 05:46:49 +00001969 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001970 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001971 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1972 CCValAssign &VA = ArgLocs[i];
1973 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1974 // places.
1975 assert(VA.getValNo() != LastVal &&
1976 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001977 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001978 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001979
Chris Lattnerf39f7712007-02-28 05:46:49 +00001980 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001981 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001982 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001984 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001986 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001988 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001990 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001991 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001992 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001993 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001994 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001995 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001996 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001997 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001998 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001999
Devang Patel68e6bee2011-02-21 23:21:26 +00002000 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Chris Lattnerf39f7712007-02-28 05:46:49 +00002003 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2004 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2005 // right size.
2006 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002007 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002008 DAG.getValueType(VA.getValVT()));
2009 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002010 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002011 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002012 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002013 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002014
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002015 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002016 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002017 if (RegVT.isVector())
2018 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2019 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002020 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002021 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002022 } else {
2023 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002024 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002025 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002026
2027 // If value is passed via pointer - do a load.
2028 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002029 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002030 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002031
Dan Gohman98ca4f22009-08-05 01:29:28 +00002032 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002033 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002034
Eli Benderskya5597f02013-01-25 22:07:43 +00002035 // The x86-64 ABIs require that for returning structs by value we copy
2036 // the sret argument into %rax/%eax (depending on ABI) for the return.
2037 // Save the argument into a virtual register so that we can access it
2038 // from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002039 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002040 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2041 unsigned Reg = FuncInfo->getSRetReturnReg();
2042 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002043 MVT PtrTy = getPointerTy();
2044 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002045 FuncInfo->setSRetReturnReg(Reg);
2046 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002049 }
2050
Chris Lattnerf39f7712007-02-28 05:46:49 +00002051 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002052 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002053 if (FuncIsMadeTailCallSafe(CallConv,
2054 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002055 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002056
Evan Cheng1bc78042006-04-26 01:20:17 +00002057 // If the function takes variable number of arguments, make a frame index for
2058 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002059 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002060 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2061 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002062 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002063 }
2064 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002065 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2066
2067 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002068 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002069 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002070 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002071 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002072 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2073 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002074 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002075 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2076 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2077 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002078 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002079 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002080
2081 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002082 // The XMM registers which might contain var arg parameters are shadowed
2083 // in their paired GPR. So we only need to save the GPR to their home
2084 // slots.
2085 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002086 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002087 } else {
2088 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2089 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002090
Chad Rosier30450e82011-12-22 22:35:21 +00002091 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2092 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002093 }
2094 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2095 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002096
Bill Wendling831737d2012-12-30 10:32:01 +00002097 bool NoImplicitFloatOps = Fn->getAttributes().
2098 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002099 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002100 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002101 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2102 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002103 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002104 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002105 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002106 // Kernel mode asks for SSE to be disabled, so don't push them
2107 // on the stack.
2108 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002109
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002110 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002111 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002112 // Get to the caller-allocated home save location. Add 8 to account
2113 // for the return address.
2114 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002115 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002116 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002117 // Fixup to set vararg frame on shadow area (4 x i64).
2118 if (NumIntRegs < 4)
2119 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002120 } else {
2121 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002122 // registers, then we must store them to their spots on the stack so
2123 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002124 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2125 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2126 FuncInfo->setRegSaveFrameIndex(
2127 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002128 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002129 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002130
Gordon Henriksen86737662008-01-05 16:56:59 +00002131 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002132 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002133 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2134 getPointerTy());
2135 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002136 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002137 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2138 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002139 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002140 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002141 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002143 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002144 MachinePointerInfo::getFixedStack(
2145 FuncInfo->getRegSaveFrameIndex(), Offset),
2146 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002147 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002148 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002150
Dan Gohmanface41a2009-08-16 21:24:25 +00002151 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2152 // Now store the XMM (fp + vector) parameter registers.
2153 SmallVector<SDValue, 11> SaveXMMOps;
2154 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002155
Craig Topperc9099502012-04-20 06:31:50 +00002156 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002157 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2158 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002159
Dan Gohman1e93df62010-04-17 14:41:14 +00002160 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2161 FuncInfo->getRegSaveFrameIndex()));
2162 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2163 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002164
Dan Gohmanface41a2009-08-16 21:24:25 +00002165 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002166 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002167 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002168 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2169 SaveXMMOps.push_back(Val);
2170 }
2171 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2172 MVT::Other,
2173 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002174 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002175
2176 if (!MemOps.empty())
2177 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2178 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002179 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002181
Gordon Henriksen86737662008-01-05 16:56:59 +00002182 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002183 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2184 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002185 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002186 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002187 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002188 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002189 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002190 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002191 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002192 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002193
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002195 // RegSaveFrameIndex is X86-64 only.
2196 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002197 if (CallConv == CallingConv::X86_FastCall ||
2198 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002199 // fastcc functions can't have varargs.
2200 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002201 }
Evan Cheng25caf632006-05-23 21:06:34 +00002202
Rafael Espindola76927d752011-08-30 19:39:58 +00002203 FuncInfo->setArgumentStackSize(StackSize);
2204
Dan Gohman98ca4f22009-08-05 01:29:28 +00002205 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002206}
2207
Dan Gohman475871a2008-07-27 21:46:04 +00002208SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2210 SDValue StackPtr, SDValue Arg,
2211 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002212 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002213 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002214 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002215 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002216 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002217 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002218 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002219
2220 return DAG.getStore(Chain, dl, Arg, PtrOff,
2221 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002222 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002223}
2224
Bill Wendling64e87322009-01-16 19:25:27 +00002225/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002226/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002227SDValue
2228X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002229 SDValue &OutRetAddr, SDValue Chain,
2230 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002231 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002232 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002233 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002234 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002235
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002236 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002237 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002238 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002239 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240}
2241
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002242/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002243/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002244static SDValue
2245EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002246 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2247 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002248 // Store the return address to the appropriate stack slot.
2249 if (!FPDiff) return Chain;
2250 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002251 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002252 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002253 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002254 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002255 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002256 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002257 return Chain;
2258}
2259
Dan Gohman98ca4f22009-08-05 01:29:28 +00002260SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002261X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002262 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002263 SelectionDAG &DAG = CLI.DAG;
2264 DebugLoc &dl = CLI.DL;
2265 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2266 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2267 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2268 SDValue Chain = CLI.Chain;
2269 SDValue Callee = CLI.Callee;
2270 CallingConv::ID CallConv = CLI.CallConv;
2271 bool &isTailCall = CLI.IsTailCall;
2272 bool isVarArg = CLI.IsVarArg;
2273
Dan Gohman98ca4f22009-08-05 01:29:28 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002276 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002277 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002278 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002279 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002280
Nick Lewycky22de16d2012-01-19 00:34:10 +00002281 if (MF.getTarget().Options.DisableTailCalls)
2282 isTailCall = false;
2283
Evan Cheng5f941932010-02-05 02:21:12 +00002284 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002285 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002286 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002287 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002288 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002289 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002290
2291 // Sibcalls are automatically detected tailcalls which do not require
2292 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002293 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002294 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002295
2296 if (isTailCall)
2297 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002298 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002299
Chris Lattner29689432010-03-11 00:22:57 +00002300 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002301 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002302
Chris Lattner638402b2007-02-28 07:00:42 +00002303 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002304 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002305 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002307
2308 // Allocate shadow area for Win64
2309 if (IsWin64) {
2310 CCInfo.AllocateStack(32, 8);
2311 }
2312
Duncan Sands45907662010-10-31 13:21:44 +00002313 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Chris Lattner423c5f42007-02-28 05:31:48 +00002315 // Get a count of how many bytes are to be pushed on the stack.
2316 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002317 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002318 // This is a sibcall. The memory operands are available in caller's
2319 // own caller's stack.
2320 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002321 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2322 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002323 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002324
Gordon Henriksen86737662008-01-05 16:56:59 +00002325 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002326 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002327 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002328 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2329 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2330
Gordon Henriksen86737662008-01-05 16:56:59 +00002331 FPDiff = NumBytesCallerPushed - NumBytes;
2332
2333 // Set the delta of movement of the returnaddr stackslot.
2334 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002335 if (FPDiff < X86Info->getTCReturnAddrDelta())
2336 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002337 }
2338
Evan Chengf22f9b32010-02-06 03:28:46 +00002339 if (!IsSibcall)
2340 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002341
Dan Gohman475871a2008-07-27 21:46:04 +00002342 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002343 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002344 if (isTailCall && FPDiff)
2345 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2346 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002347
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2349 SmallVector<SDValue, 8> MemOpChains;
2350 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002351
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002352 // Walk the register/memloc assignments, inserting copies/loads. In the case
2353 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002354 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2355 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002357 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002358 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002359 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002360
Chris Lattner423c5f42007-02-28 05:31:48 +00002361 // Promote the value if needed.
2362 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002363 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002364 case CCValAssign::Full: break;
2365 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002366 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002367 break;
2368 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002369 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002370 break;
2371 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002372 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002373 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002374 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002375 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2376 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002377 } else
2378 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2379 break;
2380 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002381 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002382 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002383 case CCValAssign::Indirect: {
2384 // Store the argument.
2385 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002386 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002387 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002388 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002389 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002390 Arg = SpillSlot;
2391 break;
2392 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002394
Chris Lattner423c5f42007-02-28 05:31:48 +00002395 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002396 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2397 if (isVarArg && IsWin64) {
2398 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2399 // shadow reg if callee is a varargs function.
2400 unsigned ShadowReg = 0;
2401 switch (VA.getLocReg()) {
2402 case X86::XMM0: ShadowReg = X86::RCX; break;
2403 case X86::XMM1: ShadowReg = X86::RDX; break;
2404 case X86::XMM2: ShadowReg = X86::R8; break;
2405 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002406 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002407 if (ShadowReg)
2408 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002409 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002410 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002411 assert(VA.isMemLoc());
2412 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002413 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2414 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002415 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2416 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002417 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Evan Cheng32fe1032006-05-25 00:59:30 +00002420 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002421 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002422 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002423
Chris Lattner88e1fd52009-07-09 04:24:46 +00002424 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002425 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2426 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002428 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2429 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002430 } else {
2431 // If we are tail calling and generating PIC/GOT style code load the
2432 // address of the callee into ECX. The value in ecx is used as target of
2433 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2434 // for tail calls on PIC/GOT architectures. Normally we would just put the
2435 // address of GOT into ebx and then call target@PLT. But for tail calls
2436 // ebx would be restored (since ebx is callee saved) before jumping to the
2437 // target@PLT.
2438
2439 // Note: The actual moving to ECX is done further down.
2440 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2441 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2442 !G->getGlobal()->hasProtectedVisibility())
2443 Callee = LowerGlobalAddress(Callee, DAG);
2444 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002445 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002446 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002447 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002448
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002449 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002450 // From AMD64 ABI document:
2451 // For calls that may call functions that use varargs or stdargs
2452 // (prototype-less calls or calls to functions containing ellipsis (...) in
2453 // the declaration) %al is used as hidden argument to specify the number
2454 // of SSE registers used. The contents of %al do not need to match exactly
2455 // the number of registers, but must be an ubound on the number of SSE
2456 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002457
Gordon Henriksen86737662008-01-05 16:56:59 +00002458 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002459 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002460 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2461 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2462 };
2463 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002464 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002465 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002466
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002467 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2468 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002469 }
2470
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002471 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 if (isTailCall) {
2473 // Force all the incoming stack arguments to be loaded from the stack
2474 // before any new outgoing arguments are stored to the stack, because the
2475 // outgoing stack slots may alias the incoming argument stack slots, and
2476 // the alias isn't otherwise explicit. This is slightly more conservative
2477 // than necessary, because it means that each store effectively depends
2478 // on every argument instead of just those arguments it would clobber.
2479 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2480
Dan Gohman475871a2008-07-27 21:46:04 +00002481 SmallVector<SDValue, 8> MemOpChains2;
2482 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002483 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002484 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002485 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2486 CCValAssign &VA = ArgLocs[i];
2487 if (VA.isRegLoc())
2488 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002489 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002490 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002492 // Create frame index.
2493 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002494 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002495 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002496 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002497
Duncan Sands276dcbd2008-03-21 09:14:45 +00002498 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002499 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002500 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002501 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002502 StackPtr = DAG.getCopyFromReg(Chain, dl,
2503 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002504 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002505 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002506
Dan Gohman98ca4f22009-08-05 01:29:28 +00002507 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2508 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002509 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002510 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002511 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002512 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002514 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002515 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002516 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002517 }
2518 }
2519
2520 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002521 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002522 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002523
2524 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002525 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2526 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002527 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002528 }
2529
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002530 // Build a sequence of copy-to-reg nodes chained together with token chain
2531 // and flag operands which copy the outgoing args into registers.
2532 SDValue InFlag;
2533 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2534 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2535 RegsToPass[i].second, InFlag);
2536 InFlag = Chain.getValue(1);
2537 }
2538
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002539 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2540 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2541 // In the 64-bit large code model, we have to make all calls
2542 // through a register, since the call instruction's 32-bit
2543 // pc-relative offset may not be large enough to hold the whole
2544 // address.
2545 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002546 // If the callee is a GlobalAddress node (quite common, every direct call
2547 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2548 // it.
2549
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002550 // We should use extra load for direct calls to dllimported functions in
2551 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002552 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002553 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002554 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002555 bool ExtraLoad = false;
2556 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002557
Chris Lattner48a7d022009-07-09 05:02:21 +00002558 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2559 // external symbols most go through the PLT in PIC mode. If the symbol
2560 // has hidden or protected visibility, or if it is static or local, then
2561 // we don't need to use the PLT - we can directly call it.
2562 if (Subtarget->isTargetELF() &&
2563 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002564 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002565 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002566 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002567 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002568 (!Subtarget->getTargetTriple().isMacOSX() ||
2569 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002570 // PC-relative references to external symbols should go through $stub,
2571 // unless we're building with the leopard linker or later, which
2572 // automatically synthesizes these stubs.
2573 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002574 } else if (Subtarget->isPICStyleRIPRel() &&
2575 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002576 cast<Function>(GV)->getAttributes().
2577 hasAttribute(AttributeSet::FunctionIndex,
2578 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002579 // If the function is marked as non-lazy, generate an indirect call
2580 // which loads from the GOT directly. This avoids runtime overhead
2581 // at the cost of eager binding (and one extra byte of encoding).
2582 OpFlags = X86II::MO_GOTPCREL;
2583 WrapperKind = X86ISD::WrapperRIP;
2584 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002585 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002586
Devang Patel0d881da2010-07-06 22:08:15 +00002587 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002588 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002589
2590 // Add a wrapper if needed.
2591 if (WrapperKind != ISD::DELETED_NODE)
2592 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2593 // Add extra indirection if needed.
2594 if (ExtraLoad)
2595 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2596 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002597 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002598 }
Bill Wendling056292f2008-09-16 21:48:12 +00002599 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002600 unsigned char OpFlags = 0;
2601
Evan Cheng1bf891a2010-12-01 22:59:46 +00002602 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2603 // external symbols should go through the PLT.
2604 if (Subtarget->isTargetELF() &&
2605 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2606 OpFlags = X86II::MO_PLT;
2607 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002608 (!Subtarget->getTargetTriple().isMacOSX() ||
2609 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002610 // PC-relative references to external symbols should go through $stub,
2611 // unless we're building with the leopard linker or later, which
2612 // automatically synthesizes these stubs.
2613 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002614 }
Eric Christopherfd179292009-08-27 18:07:15 +00002615
Chris Lattner48a7d022009-07-09 05:02:21 +00002616 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2617 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002618 }
2619
Chris Lattnerd96d0722007-02-25 06:40:16 +00002620 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002621 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002622 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002623
Evan Chengf22f9b32010-02-06 03:28:46 +00002624 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002625 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2626 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002627 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002629
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002630 Ops.push_back(Chain);
2631 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002632
Dan Gohman98ca4f22009-08-05 01:29:28 +00002633 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002635
Gordon Henriksen86737662008-01-05 16:56:59 +00002636 // Add argument registers to the end of the list so that they are known live
2637 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002638 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2639 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2640 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002641
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002642 // Add a register mask operand representing the call-preserved registers.
2643 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2644 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2645 assert(Mask && "Missing call preserved mask for calling convention");
2646 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002647
Gabor Greifba36cb52008-08-28 21:40:38 +00002648 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002649 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002650
Dan Gohman98ca4f22009-08-05 01:29:28 +00002651 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002652 // We used to do:
2653 //// If this is the first return lowered for this function, add the regs
2654 //// to the liveout set for the function.
2655 // This isn't right, although it's probably harmless on x86; liveouts
2656 // should be computed from returns not tail calls. Consider a void
2657 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 return DAG.getNode(X86ISD::TC_RETURN, dl,
2659 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002660 }
2661
Dale Johannesenace16102009-02-03 19:33:06 +00002662 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002663 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002664
Chris Lattner2d297092006-05-23 18:50:38 +00002665 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002666 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002667 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2668 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002669 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002670 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002671 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002672 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002673 // pops the hidden struct pointer, so we have to push it back.
2674 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002675 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002676 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002677 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002678 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002679
Gordon Henriksenae636f82008-01-03 16:47:34 +00002680 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002681 if (!IsSibcall) {
2682 Chain = DAG.getCALLSEQ_END(Chain,
2683 DAG.getIntPtrConstant(NumBytes, true),
2684 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2685 true),
2686 InFlag);
2687 InFlag = Chain.getValue(1);
2688 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002689
Chris Lattner3085e152007-02-25 08:59:22 +00002690 // Handle result values, copying them out of physregs into vregs that we
2691 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002692 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2693 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002694}
2695
Evan Cheng25ab6902006-09-08 06:48:29 +00002696//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002697// Fast Calling Convention (tail call) implementation
2698//===----------------------------------------------------------------------===//
2699
2700// Like std call, callee cleans arguments, convention except that ECX is
2701// reserved for storing the tail called function address. Only 2 registers are
2702// free for argument passing (inreg). Tail call optimization is performed
2703// provided:
2704// * tailcallopt is enabled
2705// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002706// On X86_64 architecture with GOT-style position independent code only local
2707// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002708// To keep the stack aligned according to platform abi the function
2709// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2710// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002711// If a tail called function callee has more arguments than the caller the
2712// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002713// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002714// original REtADDR, but before the saved framepointer or the spilled registers
2715// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2716// stack layout:
2717// arg1
2718// arg2
2719// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002720// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002721// move area ]
2722// (possible EBP)
2723// ESI
2724// EDI
2725// local1 ..
2726
2727/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2728/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002729unsigned
2730X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2731 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002732 MachineFunction &MF = DAG.getMachineFunction();
2733 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002734 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002735 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002736 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002737 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002738 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002739 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2740 // Number smaller than 12 so just add the difference.
2741 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2742 } else {
2743 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002744 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002745 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002746 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002747 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002748}
2749
Evan Cheng5f941932010-02-05 02:21:12 +00002750/// MatchingStackOffset - Return true if the given stack call argument is
2751/// already available in the same position (relatively) of the caller's
2752/// incoming argument stack.
2753static
2754bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2755 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2756 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002757 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2758 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002759 if (Arg.getOpcode() == ISD::CopyFromReg) {
2760 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002761 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002762 return false;
2763 MachineInstr *Def = MRI->getVRegDef(VR);
2764 if (!Def)
2765 return false;
2766 if (!Flags.isByVal()) {
2767 if (!TII->isLoadFromStackSlot(Def, FI))
2768 return false;
2769 } else {
2770 unsigned Opcode = Def->getOpcode();
2771 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2772 Def->getOperand(1).isFI()) {
2773 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002774 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002775 } else
2776 return false;
2777 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002778 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2779 if (Flags.isByVal())
2780 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002781 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002782 // define @foo(%struct.X* %A) {
2783 // tail call @bar(%struct.X* byval %A)
2784 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002785 return false;
2786 SDValue Ptr = Ld->getBasePtr();
2787 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2788 if (!FINode)
2789 return false;
2790 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002791 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002792 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002793 FI = FINode->getIndex();
2794 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002795 } else
2796 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002797
Evan Cheng4cae1332010-03-05 08:38:04 +00002798 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002799 if (!MFI->isFixedObjectIndex(FI))
2800 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002801 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002802}
2803
Dan Gohman98ca4f22009-08-05 01:29:28 +00002804/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2805/// for tail call optimization. Targets which want to do tail call
2806/// optimization should implement this function.
2807bool
2808X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002809 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002810 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002811 bool isCalleeStructRet,
2812 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002813 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002814 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002815 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002816 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002817 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002818 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002819 CalleeCC != CallingConv::C)
2820 return false;
2821
Evan Cheng7096ae42010-01-29 06:45:59 +00002822 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002823 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002824 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002825
2826 // If the function return type is x86_fp80 and the callee return type is not,
2827 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2828 // perform a tailcall optimization here.
2829 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2830 return false;
2831
Evan Cheng13617962010-04-30 01:12:32 +00002832 CallingConv::ID CallerCC = CallerF->getCallingConv();
2833 bool CCMatch = CallerCC == CalleeCC;
2834
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002835 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002836 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002837 return true;
2838 return false;
2839 }
2840
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002841 // Look for obvious safe cases to perform tail call optimization that do not
2842 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002843
Evan Cheng2c12cb42010-03-26 16:26:03 +00002844 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2845 // emit a special epilogue.
2846 if (RegInfo->needsStackRealignment(MF))
2847 return false;
2848
Evan Chenga375d472010-03-15 18:54:48 +00002849 // Also avoid sibcall optimization if either caller or callee uses struct
2850 // return semantics.
2851 if (isCalleeStructRet || isCallerStructRet)
2852 return false;
2853
Chad Rosier2416da32011-06-24 21:15:36 +00002854 // An stdcall caller is expected to clean up its arguments; the callee
2855 // isn't going to do that.
2856 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2857 return false;
2858
Chad Rosier871f6642011-05-18 19:59:50 +00002859 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002860 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002861 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002862
2863 // Optimizing for varargs on Win64 is unlikely to be safe without
2864 // additional testing.
2865 if (Subtarget->isTargetWin64())
2866 return false;
2867
Chad Rosier871f6642011-05-18 19:59:50 +00002868 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002869 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002870 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002871
Chad Rosier871f6642011-05-18 19:59:50 +00002872 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2873 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2874 if (!ArgLocs[i].isRegLoc())
2875 return false;
2876 }
2877
Chad Rosier30450e82011-12-22 22:35:21 +00002878 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2879 // stack. Therefore, if it's not used by the call it is not safe to optimize
2880 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002881 bool Unused = false;
2882 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2883 if (!Ins[i].Used) {
2884 Unused = true;
2885 break;
2886 }
2887 }
2888 if (Unused) {
2889 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002890 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002891 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002892 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002893 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002894 CCValAssign &VA = RVLocs[i];
2895 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2896 return false;
2897 }
2898 }
2899
Evan Cheng13617962010-04-30 01:12:32 +00002900 // If the calling conventions do not match, then we'd better make sure the
2901 // results are returned in the same way as what the caller expects.
2902 if (!CCMatch) {
2903 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002904 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002905 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002906 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2907
2908 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002909 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002910 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002911 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2912
2913 if (RVLocs1.size() != RVLocs2.size())
2914 return false;
2915 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2916 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2917 return false;
2918 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2919 return false;
2920 if (RVLocs1[i].isRegLoc()) {
2921 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2922 return false;
2923 } else {
2924 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2925 return false;
2926 }
2927 }
2928 }
2929
Evan Chenga6bff982010-01-30 01:22:00 +00002930 // If the callee takes no arguments then go on to check the results of the
2931 // call.
2932 if (!Outs.empty()) {
2933 // Check if stack adjustment is needed. For now, do not do this if any
2934 // argument is passed on the stack.
2935 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002936 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002937 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002938
2939 // Allocate shadow area for Win64
2940 if (Subtarget->isTargetWin64()) {
2941 CCInfo.AllocateStack(32, 8);
2942 }
2943
Duncan Sands45907662010-10-31 13:21:44 +00002944 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002945 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002946 MachineFunction &MF = DAG.getMachineFunction();
2947 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2948 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002949
2950 // Check if the arguments are already laid out in the right way as
2951 // the caller's fixed stack objects.
2952 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002953 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2954 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002955 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002956 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2957 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002958 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002959 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002960 if (VA.getLocInfo() == CCValAssign::Indirect)
2961 return false;
2962 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002963 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2964 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002965 return false;
2966 }
2967 }
2968 }
Evan Cheng9c044672010-05-29 01:35:22 +00002969
2970 // If the tailcall address may be in a register, then make sure it's
2971 // possible to register allocate for it. In 32-bit, the call address can
2972 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002973 // callee-saved registers are restored. These happen to be the same
2974 // registers used to pass 'inreg' arguments so watch out for those.
2975 if (!Subtarget->is64Bit() &&
2976 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002977 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002978 unsigned NumInRegs = 0;
2979 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2980 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002981 if (!VA.isRegLoc())
2982 continue;
2983 unsigned Reg = VA.getLocReg();
2984 switch (Reg) {
2985 default: break;
2986 case X86::EAX: case X86::EDX: case X86::ECX:
2987 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002988 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002989 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002990 }
2991 }
2992 }
Evan Chenga6bff982010-01-30 01:22:00 +00002993 }
Evan Chengb1712452010-01-27 06:25:16 +00002994
Evan Cheng86809cc2010-02-03 03:28:02 +00002995 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002996}
2997
Dan Gohman3df24e62008-09-03 23:12:08 +00002998FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002999X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3000 const TargetLibraryInfo *libInfo) const {
3001 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003002}
3003
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003004//===----------------------------------------------------------------------===//
3005// Other Lowering Hooks
3006//===----------------------------------------------------------------------===//
3007
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003008static bool MayFoldLoad(SDValue Op) {
3009 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3010}
3011
3012static bool MayFoldIntoStore(SDValue Op) {
3013 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3014}
3015
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003016static bool isTargetShuffle(unsigned Opcode) {
3017 switch(Opcode) {
3018 default: return false;
3019 case X86ISD::PSHUFD:
3020 case X86ISD::PSHUFHW:
3021 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003022 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003023 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003024 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003025 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003026 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003027 case X86ISD::MOVLPS:
3028 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003029 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003030 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003031 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003032 case X86ISD::MOVSS:
3033 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003034 case X86ISD::UNPCKL:
3035 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003036 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003037 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003038 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003039 return true;
3040 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003041}
3042
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003043static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003044 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003045 switch(Opc) {
3046 default: llvm_unreachable("Unknown x86 shuffle node");
3047 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003048 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003049 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003050 return DAG.getNode(Opc, dl, VT, V1);
3051 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003052}
3053
3054static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003055 SDValue V1, unsigned TargetMask,
3056 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003057 switch(Opc) {
3058 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003059 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003060 case X86ISD::PSHUFHW:
3061 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003062 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003063 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003064 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3065 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003066}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003067
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003068static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003069 SDValue V1, SDValue V2, unsigned TargetMask,
3070 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003071 switch(Opc) {
3072 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003073 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003074 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003075 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003076 return DAG.getNode(Opc, dl, VT, V1, V2,
3077 DAG.getConstant(TargetMask, MVT::i8));
3078 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003079}
3080
3081static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3082 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3083 switch(Opc) {
3084 default: llvm_unreachable("Unknown x86 shuffle node");
3085 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003086 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003087 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003088 case X86ISD::MOVLPS:
3089 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003090 case X86ISD::MOVSS:
3091 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003092 case X86ISD::UNPCKL:
3093 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003094 return DAG.getNode(Opc, dl, VT, V1, V2);
3095 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003096}
3097
Dan Gohmand858e902010-04-17 15:26:15 +00003098SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003099 MachineFunction &MF = DAG.getMachineFunction();
3100 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3101 int ReturnAddrIndex = FuncInfo->getRAIndex();
3102
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003103 if (ReturnAddrIndex == 0) {
3104 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003105 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003106 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003107 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003108 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003109 }
3110
Evan Cheng25ab6902006-09-08 06:48:29 +00003111 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003112}
3113
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003114bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3115 bool hasSymbolicDisplacement) {
3116 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003117 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003118 return false;
3119
3120 // If we don't have a symbolic displacement - we don't have any extra
3121 // restrictions.
3122 if (!hasSymbolicDisplacement)
3123 return true;
3124
3125 // FIXME: Some tweaks might be needed for medium code model.
3126 if (M != CodeModel::Small && M != CodeModel::Kernel)
3127 return false;
3128
3129 // For small code model we assume that latest object is 16MB before end of 31
3130 // bits boundary. We may also accept pretty large negative constants knowing
3131 // that all objects are in the positive half of address space.
3132 if (M == CodeModel::Small && Offset < 16*1024*1024)
3133 return true;
3134
3135 // For kernel code model we know that all object resist in the negative half
3136 // of 32bits address space. We may not accept negative offsets, since they may
3137 // be just off and we may accept pretty large positive ones.
3138 if (M == CodeModel::Kernel && Offset > 0)
3139 return true;
3140
3141 return false;
3142}
3143
Evan Chengef41ff62011-06-23 17:54:54 +00003144/// isCalleePop - Determines whether the callee is required to pop its
3145/// own arguments. Callee pop is necessary to support tail calls.
3146bool X86::isCalleePop(CallingConv::ID CallingConv,
3147 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3148 if (IsVarArg)
3149 return false;
3150
3151 switch (CallingConv) {
3152 default:
3153 return false;
3154 case CallingConv::X86_StdCall:
3155 return !is64Bit;
3156 case CallingConv::X86_FastCall:
3157 return !is64Bit;
3158 case CallingConv::X86_ThisCall:
3159 return !is64Bit;
3160 case CallingConv::Fast:
3161 return TailCallOpt;
3162 case CallingConv::GHC:
3163 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003164 case CallingConv::HiPE:
3165 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003166 }
3167}
3168
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003169/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3170/// specific condition code, returning the condition code and the LHS/RHS of the
3171/// comparison to make.
3172static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3173 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003174 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003175 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3176 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3177 // X > -1 -> X == 0, jump !sign.
3178 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003179 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003180 }
3181 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003182 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003183 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003184 }
3185 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003186 // X < 1 -> X <= 0
3187 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003188 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003189 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003190 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003191
Evan Chengd9558e02006-01-06 00:43:03 +00003192 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003193 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003194 case ISD::SETEQ: return X86::COND_E;
3195 case ISD::SETGT: return X86::COND_G;
3196 case ISD::SETGE: return X86::COND_GE;
3197 case ISD::SETLT: return X86::COND_L;
3198 case ISD::SETLE: return X86::COND_LE;
3199 case ISD::SETNE: return X86::COND_NE;
3200 case ISD::SETULT: return X86::COND_B;
3201 case ISD::SETUGT: return X86::COND_A;
3202 case ISD::SETULE: return X86::COND_BE;
3203 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003204 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003205 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003206
Chris Lattner4c78e022008-12-23 23:42:27 +00003207 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003208
Chris Lattner4c78e022008-12-23 23:42:27 +00003209 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003210 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3211 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003212 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3213 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003214 }
3215
Chris Lattner4c78e022008-12-23 23:42:27 +00003216 switch (SetCCOpcode) {
3217 default: break;
3218 case ISD::SETOLT:
3219 case ISD::SETOLE:
3220 case ISD::SETUGT:
3221 case ISD::SETUGE:
3222 std::swap(LHS, RHS);
3223 break;
3224 }
3225
3226 // On a floating point condition, the flags are set as follows:
3227 // ZF PF CF op
3228 // 0 | 0 | 0 | X > Y
3229 // 0 | 0 | 1 | X < Y
3230 // 1 | 0 | 0 | X == Y
3231 // 1 | 1 | 1 | unordered
3232 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003233 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003234 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003235 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003236 case ISD::SETOLT: // flipped
3237 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003238 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003239 case ISD::SETOLE: // flipped
3240 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003241 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003242 case ISD::SETUGT: // flipped
3243 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003244 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003245 case ISD::SETUGE: // flipped
3246 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003247 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003248 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003249 case ISD::SETNE: return X86::COND_NE;
3250 case ISD::SETUO: return X86::COND_P;
3251 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003252 case ISD::SETOEQ:
3253 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003254 }
Evan Chengd9558e02006-01-06 00:43:03 +00003255}
3256
Evan Cheng4a460802006-01-11 00:33:36 +00003257/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3258/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003259/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003260static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003261 switch (X86CC) {
3262 default:
3263 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003264 case X86::COND_B:
3265 case X86::COND_BE:
3266 case X86::COND_E:
3267 case X86::COND_P:
3268 case X86::COND_A:
3269 case X86::COND_AE:
3270 case X86::COND_NE:
3271 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003272 return true;
3273 }
3274}
3275
Evan Chengeb2f9692009-10-27 19:56:55 +00003276/// isFPImmLegal - Returns true if the target can instruction select the
3277/// specified FP immediate natively. If false, the legalizer will
3278/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003279bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003280 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3281 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3282 return true;
3283 }
3284 return false;
3285}
3286
Nate Begeman9008ca62009-04-27 18:41:29 +00003287/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3288/// the specified range (L, H].
3289static bool isUndefOrInRange(int Val, int Low, int Hi) {
3290 return (Val < 0) || (Val >= Low && Val < Hi);
3291}
3292
3293/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3294/// specified value.
3295static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003296 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003297}
3298
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003299/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003300/// from position Pos and ending in Pos+Size, falls within the specified
3301/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003302static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003303 unsigned Pos, unsigned Size, int Low) {
3304 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003305 if (!isUndefOrEqual(Mask[i], Low))
3306 return false;
3307 return true;
3308}
3309
Nate Begeman9008ca62009-04-27 18:41:29 +00003310/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3311/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3312/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003313static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003314 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003317 return (Mask[0] < 2 && Mask[1] < 2);
3318 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003319}
3320
Nate Begeman9008ca62009-04-27 18:41:29 +00003321/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3322/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003323static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3324 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003325 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003326
Nate Begeman9008ca62009-04-27 18:41:29 +00003327 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003328 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3329 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003330
Evan Cheng506d3df2006-03-29 23:07:14 +00003331 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003332 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003333 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003334 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003335
Craig Toppera9a568a2012-05-02 08:03:44 +00003336 if (VT == MVT::v16i16) {
3337 // Lower quadword copied in order or undef.
3338 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3339 return false;
3340
3341 // Upper quadword shuffled.
3342 for (unsigned i = 12; i != 16; ++i)
3343 if (!isUndefOrInRange(Mask[i], 12, 16))
3344 return false;
3345 }
3346
Evan Cheng506d3df2006-03-29 23:07:14 +00003347 return true;
3348}
3349
Nate Begeman9008ca62009-04-27 18:41:29 +00003350/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3351/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003352static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3353 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003354 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003355
Rafael Espindola15684b22009-04-24 12:40:33 +00003356 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003357 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3358 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003359
Rafael Espindola15684b22009-04-24 12:40:33 +00003360 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003361 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003362 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003363 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Craig Toppera9a568a2012-05-02 08:03:44 +00003365 if (VT == MVT::v16i16) {
3366 // Upper quadword copied in order.
3367 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3368 return false;
3369
3370 // Lower quadword shuffled.
3371 for (unsigned i = 8; i != 12; ++i)
3372 if (!isUndefOrInRange(Mask[i], 8, 12))
3373 return false;
3374 }
3375
Rafael Espindola15684b22009-04-24 12:40:33 +00003376 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003377}
3378
Nate Begemana09008b2009-10-19 02:17:23 +00003379/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3380/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003381static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3382 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003383 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3384 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003385 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003386
Craig Topper0e2037b2012-01-20 05:53:00 +00003387 unsigned NumElts = VT.getVectorNumElements();
3388 unsigned NumLanes = VT.getSizeInBits()/128;
3389 unsigned NumLaneElts = NumElts/NumLanes;
3390
3391 // Do not handle 64-bit element shuffles with palignr.
3392 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003393 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003394
Craig Topper0e2037b2012-01-20 05:53:00 +00003395 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3396 unsigned i;
3397 for (i = 0; i != NumLaneElts; ++i) {
3398 if (Mask[i+l] >= 0)
3399 break;
3400 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003401
Craig Topper0e2037b2012-01-20 05:53:00 +00003402 // Lane is all undef, go to next lane
3403 if (i == NumLaneElts)
3404 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003405
Craig Topper0e2037b2012-01-20 05:53:00 +00003406 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003407
Craig Topper0e2037b2012-01-20 05:53:00 +00003408 // Make sure its in this lane in one of the sources
3409 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3410 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003411 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003412
3413 // If not lane 0, then we must match lane 0
3414 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3415 return false;
3416
3417 // Correct second source to be contiguous with first source
3418 if (Start >= (int)NumElts)
3419 Start -= NumElts - NumLaneElts;
3420
3421 // Make sure we're shifting in the right direction.
3422 if (Start <= (int)(i+l))
3423 return false;
3424
3425 Start -= i;
3426
3427 // Check the rest of the elements to see if they are consecutive.
3428 for (++i; i != NumLaneElts; ++i) {
3429 int Idx = Mask[i+l];
3430
3431 // Make sure its in this lane
3432 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3433 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3434 return false;
3435
3436 // If not lane 0, then we must match lane 0
3437 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3438 return false;
3439
3440 if (Idx >= (int)NumElts)
3441 Idx -= NumElts - NumLaneElts;
3442
3443 if (!isUndefOrEqual(Idx, Start+i))
3444 return false;
3445
3446 }
Nate Begemana09008b2009-10-19 02:17:23 +00003447 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003448
Nate Begemana09008b2009-10-19 02:17:23 +00003449 return true;
3450}
3451
Craig Topper1a7700a2012-01-19 08:19:12 +00003452/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3453/// the two vector operands have swapped position.
3454static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3455 unsigned NumElems) {
3456 for (unsigned i = 0; i != NumElems; ++i) {
3457 int idx = Mask[i];
3458 if (idx < 0)
3459 continue;
3460 else if (idx < (int)NumElems)
3461 Mask[i] = idx + NumElems;
3462 else
3463 Mask[i] = idx - NumElems;
3464 }
3465}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003466
Craig Topper1a7700a2012-01-19 08:19:12 +00003467/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3468/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3469/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3470/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003471static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003472 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003473 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003474 return false;
3475
Craig Topper1a7700a2012-01-19 08:19:12 +00003476 unsigned NumElems = VT.getVectorNumElements();
3477 unsigned NumLanes = VT.getSizeInBits()/128;
3478 unsigned NumLaneElems = NumElems/NumLanes;
3479
3480 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003481 return false;
3482
3483 // VSHUFPSY divides the resulting vector into 4 chunks.
3484 // The sources are also splitted into 4 chunks, and each destination
3485 // chunk must come from a different source chunk.
3486 //
3487 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3488 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3489 //
3490 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3491 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3492 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003493 // VSHUFPDY divides the resulting vector into 4 chunks.
3494 // The sources are also splitted into 4 chunks, and each destination
3495 // chunk must come from a different source chunk.
3496 //
3497 // SRC1 => X3 X2 X1 X0
3498 // SRC2 => Y3 Y2 Y1 Y0
3499 //
3500 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3501 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003502 unsigned HalfLaneElems = NumLaneElems/2;
3503 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3504 for (unsigned i = 0; i != NumLaneElems; ++i) {
3505 int Idx = Mask[i+l];
3506 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3507 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3508 return false;
3509 // For VSHUFPSY, the mask of the second half must be the same as the
3510 // first but with the appropriate offsets. This works in the same way as
3511 // VPERMILPS works with masks.
3512 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3513 continue;
3514 if (!isUndefOrEqual(Idx, Mask[i]+l))
3515 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003516 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003517 }
3518
3519 return true;
3520}
3521
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003522/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3523/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003524static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003525 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003526 return false;
3527
Craig Topper7a9a28b2012-08-12 02:23:29 +00003528 unsigned NumElems = VT.getVectorNumElements();
3529
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003530 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003531 return false;
3532
Evan Cheng2064a2b2006-03-28 06:50:32 +00003533 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003534 return isUndefOrEqual(Mask[0], 6) &&
3535 isUndefOrEqual(Mask[1], 7) &&
3536 isUndefOrEqual(Mask[2], 2) &&
3537 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003538}
3539
Nate Begeman0b10b912009-11-07 23:17:15 +00003540/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3541/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3542/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003543static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003544 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003545 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003546
Craig Topper7a9a28b2012-08-12 02:23:29 +00003547 unsigned NumElems = VT.getVectorNumElements();
3548
Nate Begeman0b10b912009-11-07 23:17:15 +00003549 if (NumElems != 4)
3550 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003551
Craig Topperdd637ae2012-02-19 05:41:45 +00003552 return isUndefOrEqual(Mask[0], 2) &&
3553 isUndefOrEqual(Mask[1], 3) &&
3554 isUndefOrEqual(Mask[2], 2) &&
3555 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003556}
3557
Evan Cheng5ced1d82006-04-06 23:23:56 +00003558/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3559/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003560static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003561 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003562 return false;
3563
Craig Topperdd637ae2012-02-19 05:41:45 +00003564 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003565
Evan Cheng5ced1d82006-04-06 23:23:56 +00003566 if (NumElems != 2 && NumElems != 4)
3567 return false;
3568
Chad Rosier238ae312012-04-30 17:47:15 +00003569 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003570 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003571 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003572
Chad Rosier238ae312012-04-30 17:47:15 +00003573 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003574 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003575 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003576
3577 return true;
3578}
3579
Nate Begeman0b10b912009-11-07 23:17:15 +00003580/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3581/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003582static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003583 if (!VT.is128BitVector())
3584 return false;
3585
Craig Topperdd637ae2012-02-19 05:41:45 +00003586 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003587
Craig Topper7a9a28b2012-08-12 02:23:29 +00003588 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003589 return false;
3590
Chad Rosier238ae312012-04-30 17:47:15 +00003591 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003592 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003593 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003594
Chad Rosier238ae312012-04-30 17:47:15 +00003595 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3596 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003597 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003598
3599 return true;
3600}
3601
Elena Demikhovsky15963732012-06-26 08:04:10 +00003602//
3603// Some special combinations that can be optimized.
3604//
3605static
3606SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3607 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003608 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003609 DebugLoc dl = SVOp->getDebugLoc();
3610
3611 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3612 return SDValue();
3613
3614 ArrayRef<int> Mask = SVOp->getMask();
3615
3616 // These are the special masks that may be optimized.
3617 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3618 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3619 bool MatchEvenMask = true;
3620 bool MatchOddMask = true;
3621 for (int i=0; i<8; ++i) {
3622 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3623 MatchEvenMask = false;
3624 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3625 MatchOddMask = false;
3626 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003627
Elena Demikhovsky32510202012-09-04 12:49:02 +00003628 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003629 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003630
Elena Demikhovsky15963732012-06-26 08:04:10 +00003631 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3632
Elena Demikhovsky32510202012-09-04 12:49:02 +00003633 SDValue Op0 = SVOp->getOperand(0);
3634 SDValue Op1 = SVOp->getOperand(1);
3635
3636 if (MatchEvenMask) {
3637 // Shift the second operand right to 32 bits.
3638 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3639 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3640 } else {
3641 // Shift the first operand left to 32 bits.
3642 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3643 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3644 }
3645 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3646 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003647}
3648
Evan Cheng0038e592006-03-28 00:39:58 +00003649/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3650/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003651static bool isUNPCKLMask(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 Topper5a529e42013-01-18 06:44:29 +00003658 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003659 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +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;
David Greenea20244d2011-03-02 17:23:43 +00003666
Craig Topper94438ba2011-12-16 08:06:31 +00003667 for (unsigned l = 0; l != NumLanes; ++l) {
3668 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3669 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003670 i += 2, ++j) {
3671 int BitI = Mask[i];
3672 int BitI1 = Mask[i+1];
3673 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003674 return false;
David Greenea20244d2011-03-02 17:23:43 +00003675 if (V2IsSplat) {
3676 if (!isUndefOrEqual(BitI1, NumElts))
3677 return false;
3678 } else {
3679 if (!isUndefOrEqual(BitI1, j + NumElts))
3680 return false;
3681 }
Evan Cheng39623da2006-04-20 08:58:49 +00003682 }
Evan Cheng0038e592006-03-28 00:39:58 +00003683 }
David Greenea20244d2011-03-02 17:23:43 +00003684
Evan Cheng0038e592006-03-28 00:39:58 +00003685 return true;
3686}
3687
Evan Cheng4fcb9222006-03-28 02:43:26 +00003688/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3689/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003690static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003691 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003692 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003693
3694 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3695 "Unsupported vector type for unpckh");
3696
Craig Topper5a529e42013-01-18 06:44:29 +00003697 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003698 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003699 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003700
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003701 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3702 // independently on 128-bit lanes.
3703 unsigned NumLanes = VT.getSizeInBits()/128;
3704 unsigned NumLaneElts = NumElts/NumLanes;
3705
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003706 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003707 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3708 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003709 int BitI = Mask[i];
3710 int BitI1 = Mask[i+1];
3711 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003712 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003713 if (V2IsSplat) {
3714 if (isUndefOrEqual(BitI1, NumElts))
3715 return false;
3716 } else {
3717 if (!isUndefOrEqual(BitI1, j+NumElts))
3718 return false;
3719 }
Evan Cheng39623da2006-04-20 08:58:49 +00003720 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003721 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003722 return true;
3723}
3724
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003725/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3726/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3727/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003728static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003729 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003730 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003731
3732 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3733 "Unsupported vector type for unpckh");
3734
Craig Topper5a529e42013-01-18 06:44:29 +00003735 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003736 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003737 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003738
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003739 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3740 // FIXME: Need a better way to get rid of this, there's no latency difference
3741 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3742 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003743 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003744 return false;
3745
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003746 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3747 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003748 unsigned NumLanes = VT.getSizeInBits()/128;
3749 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003750
Craig Topper94438ba2011-12-16 08:06:31 +00003751 for (unsigned l = 0; l != NumLanes; ++l) {
3752 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3753 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003754 i += 2, ++j) {
3755 int BitI = Mask[i];
3756 int BitI1 = Mask[i+1];
3757
3758 if (!isUndefOrEqual(BitI, j))
3759 return false;
3760 if (!isUndefOrEqual(BitI1, j))
3761 return false;
3762 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003763 }
David Greenea20244d2011-03-02 17:23:43 +00003764
Rafael Espindola15684b22009-04-24 12:40:33 +00003765 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003766}
3767
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003768/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3769/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3770/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003771static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003772 unsigned NumElts = VT.getVectorNumElements();
3773
3774 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3775 "Unsupported vector type for unpckh");
3776
Craig Topper5a529e42013-01-18 06:44:29 +00003777 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003778 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003779 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003780
Craig Topper94438ba2011-12-16 08:06:31 +00003781 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3782 // independently on 128-bit lanes.
3783 unsigned NumLanes = VT.getSizeInBits()/128;
3784 unsigned NumLaneElts = NumElts/NumLanes;
3785
3786 for (unsigned l = 0; l != NumLanes; ++l) {
3787 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3788 i != (l+1)*NumLaneElts; i += 2, ++j) {
3789 int BitI = Mask[i];
3790 int BitI1 = Mask[i+1];
3791 if (!isUndefOrEqual(BitI, j))
3792 return false;
3793 if (!isUndefOrEqual(BitI1, j))
3794 return false;
3795 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003796 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003797 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003798}
3799
Evan Cheng017dcc62006-04-21 01:05:10 +00003800/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3801/// specifies a shuffle of elements that is suitable for input to MOVSS,
3802/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003803static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003804 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003805 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003806 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003807 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003808
Craig Topperc612d792012-01-02 09:17:37 +00003809 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003810
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003812 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003813
Craig Topperc612d792012-01-02 09:17:37 +00003814 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003815 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003816 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003817
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003818 return true;
3819}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003820
Craig Topper70b883b2011-11-28 10:14:51 +00003821/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003822/// as permutations between 128-bit chunks or halves. As an example: this
3823/// shuffle bellow:
3824/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3825/// The first half comes from the second half of V1 and the second half from the
3826/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003827static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3828 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003829 return false;
3830
3831 // The shuffle result is divided into half A and half B. In total the two
3832 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3833 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003834 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003835 bool MatchA = false, MatchB = false;
3836
3837 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003838 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003839 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3840 MatchA = true;
3841 break;
3842 }
3843 }
3844
3845 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003846 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003847 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3848 MatchB = true;
3849 break;
3850 }
3851 }
3852
3853 return MatchA && MatchB;
3854}
3855
Craig Topper70b883b2011-11-28 10:14:51 +00003856/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3857/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003858static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003859 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003860
Craig Topperc612d792012-01-02 09:17:37 +00003861 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003862
Craig Topperc612d792012-01-02 09:17:37 +00003863 unsigned FstHalf = 0, SndHalf = 0;
3864 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003865 if (SVOp->getMaskElt(i) > 0) {
3866 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3867 break;
3868 }
3869 }
Craig Topperc612d792012-01-02 09:17:37 +00003870 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003871 if (SVOp->getMaskElt(i) > 0) {
3872 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3873 break;
3874 }
3875 }
3876
3877 return (FstHalf | (SndHalf << 4));
3878}
3879
Craig Topper70b883b2011-11-28 10:14:51 +00003880/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003881/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3882/// Note that VPERMIL mask matching is different depending whether theunderlying
3883/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3884/// to the same elements of the low, but to the higher half of the source.
3885/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003886/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003887static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3888 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003889 return false;
3890
Craig Topperc612d792012-01-02 09:17:37 +00003891 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003892 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003893 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003894 return false;
3895
Craig Topperc612d792012-01-02 09:17:37 +00003896 unsigned NumLanes = VT.getSizeInBits()/128;
3897 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003898 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003899 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003900 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003901 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003902 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003903 continue;
3904 // VPERMILPS handling
3905 if (Mask[i] < 0)
3906 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003907 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003908 return false;
3909 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003910 }
3911
3912 return true;
3913}
3914
Craig Topper5aaffa82012-02-19 02:53:47 +00003915/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003916/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003917/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003918static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003920 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003921 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003922
3923 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003924 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003925 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003926
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003928 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003929
Craig Topperc612d792012-01-02 09:17:37 +00003930 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3932 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3933 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003934 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003935
Evan Cheng39623da2006-04-20 08:58:49 +00003936 return true;
3937}
3938
Evan Chengd9539472006-04-14 21:59:03 +00003939/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3940/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003941/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003942static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003943 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003944 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003945 return false;
3946
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003947 unsigned NumElems = VT.getVectorNumElements();
3948
Craig Topper5a529e42013-01-18 06:44:29 +00003949 if ((VT.is128BitVector() && NumElems != 4) ||
3950 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003951 return false;
3952
3953 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003954 for (unsigned i = 0; i != NumElems; i += 2)
3955 if (!isUndefOrEqual(Mask[i], i+1) ||
3956 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003957 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003958
3959 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003960}
3961
3962/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3963/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003964/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003965static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003966 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003967 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003968 return false;
3969
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003970 unsigned NumElems = VT.getVectorNumElements();
3971
Craig Topper5a529e42013-01-18 06:44:29 +00003972 if ((VT.is128BitVector() && NumElems != 4) ||
3973 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003974 return false;
3975
3976 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003977 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003978 if (!isUndefOrEqual(Mask[i], i) ||
3979 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003980 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003981
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003982 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003983}
3984
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003985/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3986/// specifies a shuffle of elements that is suitable for input to 256-bit
3987/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003988static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3989 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003990 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003991
Craig Topper7a9a28b2012-08-12 02:23:29 +00003992 unsigned NumElts = VT.getVectorNumElements();
3993 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003994 return false;
3995
Craig Topperc612d792012-01-02 09:17:37 +00003996 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003997 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003998 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003999 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004000 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004001 return false;
4002 return true;
4003}
4004
Evan Cheng0b457f02008-09-25 20:50:48 +00004005/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004006/// specifies a shuffle of elements that is suitable for input to 128-bit
4007/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004008static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004009 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004010 return false;
4011
Craig Topperc612d792012-01-02 09:17:37 +00004012 unsigned e = VT.getVectorNumElements() / 2;
4013 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004014 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004015 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004016 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004017 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004018 return false;
4019 return true;
4020}
4021
David Greenec38a03e2011-02-03 15:50:00 +00004022/// isVEXTRACTF128Index - Return true if the specified
4023/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4024/// suitable for input to VEXTRACTF128.
4025bool X86::isVEXTRACTF128Index(SDNode *N) {
4026 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4027 return false;
4028
4029 // The index should be aligned on a 128-bit boundary.
4030 uint64_t Index =
4031 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4032
Craig Topper5141d972013-01-18 08:41:28 +00004033 MVT VT = N->getValueType(0).getSimpleVT();
4034 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004035 bool Result = (Index * ElSize) % 128 == 0;
4036
4037 return Result;
4038}
4039
David Greeneccacdc12011-02-04 16:08:29 +00004040/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4041/// operand specifies a subvector insert that is suitable for input to
4042/// VINSERTF128.
4043bool X86::isVINSERTF128Index(SDNode *N) {
4044 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4045 return false;
4046
4047 // The index should be aligned on a 128-bit boundary.
4048 uint64_t Index =
4049 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4050
Craig Topper5141d972013-01-18 08:41:28 +00004051 MVT VT = N->getValueType(0).getSimpleVT();
4052 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004053 bool Result = (Index * ElSize) % 128 == 0;
4054
4055 return Result;
4056}
4057
Evan Cheng63d33002006-03-22 08:01:21 +00004058/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004059/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004060/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004061static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004062 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004063
Craig Topper1a7700a2012-01-19 08:19:12 +00004064 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4065 "Unsupported vector type for PSHUF/SHUFP");
4066
4067 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4068 // independently on 128-bit lanes.
4069 unsigned NumElts = VT.getVectorNumElements();
4070 unsigned NumLanes = VT.getSizeInBits()/128;
4071 unsigned NumLaneElts = NumElts/NumLanes;
4072
4073 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4074 "Only supports 2 or 4 elements per lane");
4075
4076 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004077 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004078 for (unsigned i = 0; i != NumElts; ++i) {
4079 int Elt = N->getMaskElt(i);
4080 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004081 Elt &= NumLaneElts - 1;
4082 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004083 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004084 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004085
Evan Cheng63d33002006-03-22 08:01:21 +00004086 return Mask;
4087}
4088
Evan Cheng506d3df2006-03-29 23:07:14 +00004089/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004090/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004091static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004092 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004093
4094 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4095 "Unsupported vector type for PSHUFHW");
4096
4097 unsigned NumElts = VT.getVectorNumElements();
4098
Evan Cheng506d3df2006-03-29 23:07:14 +00004099 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004100 for (unsigned l = 0; l != NumElts; l += 8) {
4101 // 8 nodes per lane, but we only care about the last 4.
4102 for (unsigned i = 0; i < 4; ++i) {
4103 int Elt = N->getMaskElt(l+i+4);
4104 if (Elt < 0) continue;
4105 Elt &= 0x3; // only 2-bits.
4106 Mask |= Elt << (i * 2);
4107 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004108 }
Craig Topper6b28d352012-05-03 07:12:59 +00004109
Evan Cheng506d3df2006-03-29 23:07:14 +00004110 return Mask;
4111}
4112
4113/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004114/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004115static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004116 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004117
4118 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4119 "Unsupported vector type for PSHUFHW");
4120
4121 unsigned NumElts = VT.getVectorNumElements();
4122
Evan Cheng506d3df2006-03-29 23:07:14 +00004123 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004124 for (unsigned l = 0; l != NumElts; l += 8) {
4125 // 8 nodes per lane, but we only care about the first 4.
4126 for (unsigned i = 0; i < 4; ++i) {
4127 int Elt = N->getMaskElt(l+i);
4128 if (Elt < 0) continue;
4129 Elt &= 0x3; // only 2-bits
4130 Mask |= Elt << (i * 2);
4131 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004132 }
Craig Topper6b28d352012-05-03 07:12:59 +00004133
Evan Cheng506d3df2006-03-29 23:07:14 +00004134 return Mask;
4135}
4136
Nate Begemana09008b2009-10-19 02:17:23 +00004137/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4138/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004139static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004140 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004141 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004142
Craig Topper0e2037b2012-01-20 05:53:00 +00004143 unsigned NumElts = VT.getVectorNumElements();
4144 unsigned NumLanes = VT.getSizeInBits()/128;
4145 unsigned NumLaneElts = NumElts/NumLanes;
4146
4147 int Val = 0;
4148 unsigned i;
4149 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004150 Val = SVOp->getMaskElt(i);
4151 if (Val >= 0)
4152 break;
4153 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004154 if (Val >= (int)NumElts)
4155 Val -= NumElts - NumLaneElts;
4156
Eli Friedman63f8dde2011-07-25 21:36:45 +00004157 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004158 return (Val - i) * EltSize;
4159}
4160
David Greenec38a03e2011-02-03 15:50:00 +00004161/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4162/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4163/// instructions.
4164unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4165 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4166 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4167
4168 uint64_t Index =
4169 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4170
Craig Toppercfcab212013-01-19 08:27:45 +00004171 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4172 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004173
4174 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004175 return Index / NumElemsPerChunk;
4176}
4177
David Greeneccacdc12011-02-04 16:08:29 +00004178/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4179/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4180/// instructions.
4181unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4182 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4183 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4184
4185 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004186 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004187
Craig Toppercfcab212013-01-19 08:27:45 +00004188 MVT VecVT = N->getValueType(0).getSimpleVT();
4189 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004190
4191 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004192 return Index / NumElemsPerChunk;
4193}
4194
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004195/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4196/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4197/// Handles 256-bit.
4198static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004199 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004200
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004201 unsigned NumElts = VT.getVectorNumElements();
4202
Craig Topper095c5282012-04-15 23:48:57 +00004203 assert((VT.is256BitVector() && NumElts == 4) &&
4204 "Unsupported vector type for VPERMQ/VPERMPD");
4205
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004206 unsigned Mask = 0;
4207 for (unsigned i = 0; i != NumElts; ++i) {
4208 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004209 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004210 continue;
4211 Mask |= Elt << (i*2);
4212 }
4213
4214 return Mask;
4215}
Evan Cheng37b73872009-07-30 08:33:02 +00004216/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4217/// constant +0.0.
4218bool X86::isZeroNode(SDValue Elt) {
4219 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004220 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004221 (isa<ConstantFPSDNode>(Elt) &&
4222 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4223}
4224
Nate Begeman9008ca62009-04-27 18:41:29 +00004225/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4226/// their permute mask.
4227static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4228 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004229 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004230 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004231 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004232
Nate Begeman5a5ca152009-04-29 05:20:52 +00004233 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004234 int Idx = SVOp->getMaskElt(i);
4235 if (Idx >= 0) {
4236 if (Idx < (int)NumElems)
4237 Idx += NumElems;
4238 else
4239 Idx -= NumElems;
4240 }
4241 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004242 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4244 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004245}
4246
Evan Cheng533a0aa2006-04-19 20:35:22 +00004247/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4248/// match movhlps. The lower half elements should come from upper half of
4249/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004250/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004251static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004252 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004253 return false;
4254 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004255 return false;
4256 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004257 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004258 return false;
4259 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004260 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004261 return false;
4262 return true;
4263}
4264
Evan Cheng5ced1d82006-04-06 23:23:56 +00004265/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004266/// is promoted to a vector. It also returns the LoadSDNode by reference if
4267/// required.
4268static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004269 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4270 return false;
4271 N = N->getOperand(0).getNode();
4272 if (!ISD::isNON_EXTLoad(N))
4273 return false;
4274 if (LD)
4275 *LD = cast<LoadSDNode>(N);
4276 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004277}
4278
Dan Gohman65fd6562011-11-03 21:49:52 +00004279// Test whether the given value is a vector value which will be legalized
4280// into a load.
4281static bool WillBeConstantPoolLoad(SDNode *N) {
4282 if (N->getOpcode() != ISD::BUILD_VECTOR)
4283 return false;
4284
4285 // Check for any non-constant elements.
4286 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4287 switch (N->getOperand(i).getNode()->getOpcode()) {
4288 case ISD::UNDEF:
4289 case ISD::ConstantFP:
4290 case ISD::Constant:
4291 break;
4292 default:
4293 return false;
4294 }
4295
4296 // Vectors of all-zeros and all-ones are materialized with special
4297 // instructions rather than being loaded.
4298 return !ISD::isBuildVectorAllZeros(N) &&
4299 !ISD::isBuildVectorAllOnes(N);
4300}
4301
Evan Cheng533a0aa2006-04-19 20:35:22 +00004302/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4303/// match movlp{s|d}. The lower half elements should come from lower half of
4304/// V1 (and in order), and the upper half elements should come from the upper
4305/// half of V2 (and in order). And since V1 will become the source of the
4306/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004307static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004308 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004309 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004310 return false;
4311
Evan Cheng466685d2006-10-09 20:57:25 +00004312 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004313 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004314 // Is V2 is a vector load, don't do this transformation. We will try to use
4315 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004316 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004317 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004318
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004319 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004320
Evan Cheng533a0aa2006-04-19 20:35:22 +00004321 if (NumElems != 2 && NumElems != 4)
4322 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004323 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004324 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004325 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004326 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004327 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004328 return false;
4329 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004330}
4331
Evan Cheng39623da2006-04-20 08:58:49 +00004332/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4333/// all the same.
4334static bool isSplatVector(SDNode *N) {
4335 if (N->getOpcode() != ISD::BUILD_VECTOR)
4336 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004337
Dan Gohman475871a2008-07-27 21:46:04 +00004338 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004339 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4340 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004341 return false;
4342 return true;
4343}
4344
Evan Cheng213d2cf2007-05-17 18:45:50 +00004345/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004346/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004347/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004348static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004349 SDValue V1 = N->getOperand(0);
4350 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004351 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4352 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004354 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004356 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4357 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004358 if (Opc != ISD::BUILD_VECTOR ||
4359 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 return false;
4361 } else if (Idx >= 0) {
4362 unsigned Opc = V1.getOpcode();
4363 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4364 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004365 if (Opc != ISD::BUILD_VECTOR ||
4366 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004367 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004368 }
4369 }
4370 return true;
4371}
4372
4373/// getZeroVector - Returns a vector of specified type with all zero elements.
4374///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004375static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004376 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004377 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004378
Dale Johannesen0488fb62010-09-30 23:57:10 +00004379 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004380 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004381 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004382 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004383 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004384 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4385 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4386 } else { // SSE1
4387 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4388 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4389 }
Craig Topper5a529e42013-01-18 06:44:29 +00004390 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004391 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004392 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4393 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4394 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4395 } else {
4396 // 256-bit logic and arithmetic instructions in AVX are all
4397 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4398 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4399 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4400 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4401 }
Craig Topper9d352402012-04-23 07:24:41 +00004402 } else
4403 llvm_unreachable("Unexpected vector type");
4404
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004405 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004406}
4407
Chris Lattner8a594482007-11-25 00:24:49 +00004408/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004409/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4410/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4411/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004412static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004413 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004414 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004415
Owen Anderson825b72b2009-08-11 20:47:22 +00004416 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004417 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004418 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004419 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004420 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4421 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4422 } else { // AVX
4423 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004424 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004425 }
Craig Topper5a529e42013-01-18 06:44:29 +00004426 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004427 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004428 } else
4429 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004430
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004431 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004432}
4433
Evan Cheng39623da2006-04-20 08:58:49 +00004434/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4435/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004436static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004437 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004438 if (Mask[i] > (int)NumElems) {
4439 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004440 }
Evan Cheng39623da2006-04-20 08:58:49 +00004441 }
Evan Cheng39623da2006-04-20 08:58:49 +00004442}
4443
Evan Cheng017dcc62006-04-21 01:05:10 +00004444/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4445/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004446static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 SDValue V2) {
4448 unsigned NumElems = VT.getVectorNumElements();
4449 SmallVector<int, 8> Mask;
4450 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004451 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004452 Mask.push_back(i);
4453 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004454}
4455
Nate Begeman9008ca62009-04-27 18:41:29 +00004456/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004457static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 SDValue V2) {
4459 unsigned NumElems = VT.getVectorNumElements();
4460 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004461 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 Mask.push_back(i);
4463 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004464 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004466}
4467
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004469static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 SDValue V2) {
4471 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004472 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004473 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004474 Mask.push_back(i + Half);
4475 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004476 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004478}
4479
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004480// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004481// a generic shuffle instruction because the target has no such instructions.
4482// Generate shuffles which repeat i16 and i8 several times until they can be
4483// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004484static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004485 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004487 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004488
Nate Begeman9008ca62009-04-27 18:41:29 +00004489 while (NumElems > 4) {
4490 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004491 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004494 EltNo -= NumElems/2;
4495 }
4496 NumElems >>= 1;
4497 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004498 return V;
4499}
Eric Christopherfd179292009-08-27 18:07:15 +00004500
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4502static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4503 EVT VT = V.getValueType();
4504 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004505
Craig Topper5a529e42013-01-18 06:44:29 +00004506 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004507 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004508 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004509 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4510 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004511 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004512 // To use VPERMILPS to splat scalars, the second half of indicies must
4513 // refer to the higher part, which is a duplication of the lower one,
4514 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004515 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4516 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004517
4518 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4519 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4520 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004521 } else
4522 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004523
4524 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4525}
4526
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004527/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004528static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4529 EVT SrcVT = SV->getValueType(0);
4530 SDValue V1 = SV->getOperand(0);
4531 DebugLoc dl = SV->getDebugLoc();
4532
4533 int EltNo = SV->getSplatIndex();
4534 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004535 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004536
Craig Topper5a529e42013-01-18 06:44:29 +00004537 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4538 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004539
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004540 // Extract the 128-bit part containing the splat element and update
4541 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004542 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004543 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4544 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004545 EltNo -= NumElems/2;
4546 }
4547
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004548 // All i16 and i8 vector types can't be used directly by a generic shuffle
4549 // instruction because the target has no such instruction. Generate shuffles
4550 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004551 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004552 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004553 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004554 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004555
4556 // Recreate the 256-bit vector and place the same 128-bit vector
4557 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004558 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004559 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004560 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004561 }
4562
4563 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004564}
4565
Evan Chengba05f722006-04-21 23:03:30 +00004566/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004567/// vector of zero or undef vector. This produces a shuffle where the low
4568/// element of V2 is swizzled into the zero/undef vector, landing at element
4569/// 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 +00004570static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004571 bool IsZero,
4572 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004573 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004574 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004575 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004576 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004577 unsigned NumElems = VT.getVectorNumElements();
4578 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004579 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004580 // If this is the insertion idx, put the low elt of V2 here.
4581 MaskVec.push_back(i == Idx ? NumElems : i);
4582 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004583}
4584
Craig Toppera1ffc682012-03-20 06:42:26 +00004585/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4586/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004587/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004588static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004589 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004590 unsigned NumElems = VT.getVectorNumElements();
4591 SDValue ImmN;
4592
Craig Topper89f4e662012-03-20 07:17:59 +00004593 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004594 switch(N->getOpcode()) {
4595 case X86ISD::SHUFP:
4596 ImmN = N->getOperand(N->getNumOperands()-1);
4597 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4598 break;
4599 case X86ISD::UNPCKH:
4600 DecodeUNPCKHMask(VT, Mask);
4601 break;
4602 case X86ISD::UNPCKL:
4603 DecodeUNPCKLMask(VT, Mask);
4604 break;
4605 case X86ISD::MOVHLPS:
4606 DecodeMOVHLPSMask(NumElems, Mask);
4607 break;
4608 case X86ISD::MOVLHPS:
4609 DecodeMOVLHPSMask(NumElems, Mask);
4610 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004611 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004612 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004613 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004614 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004615 case X86ISD::PSHUFD:
4616 case X86ISD::VPERMILP:
4617 ImmN = N->getOperand(N->getNumOperands()-1);
4618 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004619 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004620 break;
4621 case X86ISD::PSHUFHW:
4622 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004623 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004624 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004625 break;
4626 case X86ISD::PSHUFLW:
4627 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004628 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004629 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004630 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004631 case X86ISD::VPERMI:
4632 ImmN = N->getOperand(N->getNumOperands()-1);
4633 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4634 IsUnary = true;
4635 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004636 case X86ISD::MOVSS:
4637 case X86ISD::MOVSD: {
4638 // The index 0 always comes from the first element of the second source,
4639 // this is why MOVSS and MOVSD are used in the first place. The other
4640 // elements come from the other positions of the first source vector
4641 Mask.push_back(NumElems);
4642 for (unsigned i = 1; i != NumElems; ++i) {
4643 Mask.push_back(i);
4644 }
4645 break;
4646 }
4647 case X86ISD::VPERM2X128:
4648 ImmN = N->getOperand(N->getNumOperands()-1);
4649 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004650 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004651 break;
4652 case X86ISD::MOVDDUP:
4653 case X86ISD::MOVLHPD:
4654 case X86ISD::MOVLPD:
4655 case X86ISD::MOVLPS:
4656 case X86ISD::MOVSHDUP:
4657 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004658 // Not yet implemented
4659 return false;
4660 default: llvm_unreachable("unknown target shuffle node");
4661 }
4662
4663 return true;
4664}
4665
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004666/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4667/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004668static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004669 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004670 if (Depth == 6)
4671 return SDValue(); // Limit search depth.
4672
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004673 SDValue V = SDValue(N, 0);
4674 EVT VT = V.getValueType();
4675 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004676
4677 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4678 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004679 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004680
Craig Topper3d092db2012-03-21 02:14:01 +00004681 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004682 return DAG.getUNDEF(VT.getVectorElementType());
4683
Craig Topperd156dc12012-02-06 07:17:51 +00004684 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004685 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4686 : SV->getOperand(1);
4687 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004688 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689
4690 // Recurse into target specific vector shuffles to find scalars.
4691 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004692 MVT ShufVT = V.getValueType().getSimpleVT();
4693 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004694 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004695 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004696
Craig Topperd978c542012-05-06 19:46:21 +00004697 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004698 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004699
Craig Topper3d092db2012-03-21 02:14:01 +00004700 int Elt = ShuffleMask[Index];
4701 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004702 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004703
Craig Topper3d092db2012-03-21 02:14:01 +00004704 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004705 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004706 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004707 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708 }
4709
4710 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004711 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004712 V = V.getOperand(0);
4713 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004714 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004715
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004716 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004717 return SDValue();
4718 }
4719
4720 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4721 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004722 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004723
4724 if (V.getOpcode() == ISD::BUILD_VECTOR)
4725 return V.getOperand(Index);
4726
4727 return SDValue();
4728}
4729
4730/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4731/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004732/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004733static
Craig Topper3d092db2012-03-21 02:14:01 +00004734unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004735 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004736 unsigned i;
4737 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004738 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004739 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004740 if (!(Elt.getNode() &&
4741 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4742 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743 }
4744
4745 return i;
4746}
4747
Craig Topper3d092db2012-03-21 02:14:01 +00004748/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4749/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004750/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4751static
Craig Topper3d092db2012-03-21 02:14:01 +00004752bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4753 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4754 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004755 bool SeenV1 = false;
4756 bool SeenV2 = false;
4757
Craig Topper3d092db2012-03-21 02:14:01 +00004758 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004759 int Idx = SVOp->getMaskElt(i);
4760 // Ignore undef indicies
4761 if (Idx < 0)
4762 continue;
4763
Craig Topper3d092db2012-03-21 02:14:01 +00004764 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004765 SeenV1 = true;
4766 else
4767 SeenV2 = true;
4768
4769 // Only accept consecutive elements from the same vector
4770 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4771 return false;
4772 }
4773
4774 OpNum = SeenV1 ? 0 : 1;
4775 return true;
4776}
4777
4778/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4779/// logical left shift of a vector.
4780static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4781 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4782 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4783 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4784 false /* check zeros from right */, DAG);
4785 unsigned OpSrc;
4786
4787 if (!NumZeros)
4788 return false;
4789
4790 // Considering the elements in the mask that are not consecutive zeros,
4791 // check if they consecutively come from only one of the source vectors.
4792 //
4793 // V1 = {X, A, B, C} 0
4794 // \ \ \ /
4795 // vector_shuffle V1, V2 <1, 2, 3, X>
4796 //
4797 if (!isShuffleMaskConsecutive(SVOp,
4798 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004799 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004800 NumZeros, // Where to start looking in the src vector
4801 NumElems, // Number of elements in vector
4802 OpSrc)) // Which source operand ?
4803 return false;
4804
4805 isLeft = false;
4806 ShAmt = NumZeros;
4807 ShVal = SVOp->getOperand(OpSrc);
4808 return true;
4809}
4810
4811/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4812/// logical left shift of a vector.
4813static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4814 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4815 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4816 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4817 true /* check zeros from left */, DAG);
4818 unsigned OpSrc;
4819
4820 if (!NumZeros)
4821 return false;
4822
4823 // Considering the elements in the mask that are not consecutive zeros,
4824 // check if they consecutively come from only one of the source vectors.
4825 //
4826 // 0 { A, B, X, X } = V2
4827 // / \ / /
4828 // vector_shuffle V1, V2 <X, X, 4, 5>
4829 //
4830 if (!isShuffleMaskConsecutive(SVOp,
4831 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004832 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004833 0, // Where to start looking in the src vector
4834 NumElems, // Number of elements in vector
4835 OpSrc)) // Which source operand ?
4836 return false;
4837
4838 isLeft = true;
4839 ShAmt = NumZeros;
4840 ShVal = SVOp->getOperand(OpSrc);
4841 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004842}
4843
4844/// isVectorShift - Returns true if the shuffle can be implemented as a
4845/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004846static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004847 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004848 // Although the logic below support any bitwidth size, there are no
4849 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004850 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004851 return false;
4852
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004853 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4854 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4855 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004856
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004857 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004858}
4859
Evan Chengc78d3b42006-04-24 18:01:45 +00004860/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4861///
Dan Gohman475871a2008-07-27 21:46:04 +00004862static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004863 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004864 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004865 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004866 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004867 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004868 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004869
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004870 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004871 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004872 bool First = true;
4873 for (unsigned i = 0; i < 16; ++i) {
4874 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4875 if (ThisIsNonZero && First) {
4876 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004877 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004878 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004879 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 First = false;
4881 }
4882
4883 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004884 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004885 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4886 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004887 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004888 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004889 }
4890 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4892 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4893 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004894 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004896 } else
4897 ThisElt = LastElt;
4898
Gabor Greifba36cb52008-08-28 21:40:38 +00004899 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004901 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 }
4903 }
4904
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004905 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004906}
4907
Bill Wendlinga348c562007-03-22 18:42:45 +00004908/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004909///
Dan Gohman475871a2008-07-27 21:46:04 +00004910static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004911 unsigned NumNonZero, unsigned NumZero,
4912 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004913 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004914 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004915 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004916 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004917
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004918 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004919 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004920 bool First = true;
4921 for (unsigned i = 0; i < 8; ++i) {
4922 bool isNonZero = (NonZeros & (1 << i)) != 0;
4923 if (isNonZero) {
4924 if (First) {
4925 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004926 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004927 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004928 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004929 First = false;
4930 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004931 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004932 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004933 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004934 }
4935 }
4936
4937 return V;
4938}
4939
Evan Chengf26ffe92008-05-29 08:22:04 +00004940/// getVShift - Return a vector logical shift node.
4941///
Owen Andersone50ed302009-08-10 22:56:29 +00004942static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 unsigned NumBits, SelectionDAG &DAG,
4944 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004945 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004946 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004947 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004948 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4949 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004950 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004951 DAG.getConstant(NumBits,
4952 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004953}
4954
Dan Gohman475871a2008-07-27 21:46:04 +00004955SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004956X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004957 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004958
Evan Chengc3630942009-12-09 21:00:30 +00004959 // Check if the scalar load can be widened into a vector load. And if
4960 // the address is "base + cst" see if the cst can be "absorbed" into
4961 // the shuffle mask.
4962 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4963 SDValue Ptr = LD->getBasePtr();
4964 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4965 return SDValue();
4966 EVT PVT = LD->getValueType(0);
4967 if (PVT != MVT::i32 && PVT != MVT::f32)
4968 return SDValue();
4969
4970 int FI = -1;
4971 int64_t Offset = 0;
4972 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4973 FI = FINode->getIndex();
4974 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004975 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004976 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4977 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4978 Offset = Ptr.getConstantOperandVal(1);
4979 Ptr = Ptr.getOperand(0);
4980 } else {
4981 return SDValue();
4982 }
4983
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004984 // FIXME: 256-bit vector instructions don't require a strict alignment,
4985 // improve this code to support it better.
4986 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004987 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004988 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004989 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004990 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004991 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004992 // Can't change the alignment. FIXME: It's possible to compute
4993 // the exact stack offset and reference FI + adjust offset instead.
4994 // If someone *really* cares about this. That's the way to implement it.
4995 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004996 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004997 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004998 }
4999 }
5000
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005001 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005002 // Ptr + (Offset & ~15).
5003 if (Offset < 0)
5004 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005005 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005006 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005007 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005008 if (StartOffset)
5009 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5010 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5011
5012 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005013 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005014
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005015 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5016 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005017 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005018 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005019
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005020 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005021 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005022 Mask.push_back(EltNo);
5023
Craig Toppercc3000632012-01-30 07:50:31 +00005024 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005025 }
5026
5027 return SDValue();
5028}
5029
Michael J. Spencerec38de22010-10-10 22:04:20 +00005030/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5031/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005032/// load which has the same value as a build_vector whose operands are 'elts'.
5033///
5034/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005035///
Nate Begeman1449f292010-03-24 22:19:06 +00005036/// FIXME: we'd also like to handle the case where the last elements are zero
5037/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5038/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005039static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005040 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005041 EVT EltVT = VT.getVectorElementType();
5042 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005043
Nate Begemanfdea31a2010-03-24 20:49:50 +00005044 LoadSDNode *LDBase = NULL;
5045 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005046
Nate Begeman1449f292010-03-24 22:19:06 +00005047 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005048 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005049 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005050 for (unsigned i = 0; i < NumElems; ++i) {
5051 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005052
Nate Begemanfdea31a2010-03-24 20:49:50 +00005053 if (!Elt.getNode() ||
5054 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5055 return SDValue();
5056 if (!LDBase) {
5057 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5058 return SDValue();
5059 LDBase = cast<LoadSDNode>(Elt.getNode());
5060 LastLoadedElt = i;
5061 continue;
5062 }
5063 if (Elt.getOpcode() == ISD::UNDEF)
5064 continue;
5065
5066 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5067 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5068 return SDValue();
5069 LastLoadedElt = i;
5070 }
Nate Begeman1449f292010-03-24 22:19:06 +00005071
5072 // If we have found an entire vector of loads and undefs, then return a large
5073 // load of the entire vector width starting at the base pointer. If we found
5074 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005075 if (LastLoadedElt == NumElems - 1) {
5076 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005077 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005078 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005079 LDBase->isVolatile(), LDBase->isNonTemporal(),
5080 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005081 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005082 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005083 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005084 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005085 }
5086 if (NumElems == 4 && LastLoadedElt == 1 &&
5087 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005088 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5089 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005090 SDValue ResNode =
5091 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5092 LDBase->getPointerInfo(),
5093 LDBase->getAlignment(),
5094 false/*isVolatile*/, true/*ReadMem*/,
5095 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005096
5097 // Make sure the newly-created LOAD is in the same position as LDBase in
5098 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5099 // update uses of LDBase's output chain to use the TokenFactor.
5100 if (LDBase->hasAnyUseOfValue(1)) {
5101 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5102 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5103 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5104 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5105 SDValue(ResNode.getNode(), 1));
5106 }
5107
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005108 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005109 }
5110 return SDValue();
5111}
5112
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5114/// to generate a splat value for the following cases:
5115/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005116/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005117/// a scalar load, or a constant.
5118/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005119/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005120SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005121X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005122 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005123 return SDValue();
5124
Craig Topper45e1c752013-01-20 00:38:18 +00005125 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005126 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005127
Craig Topper5da8a802012-05-04 05:49:51 +00005128 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5129 "Unsupported vector type for broadcast.");
5130
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005131 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005132 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005133
Nadav Rotem9d68b062012-04-08 12:54:54 +00005134 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005135 default:
5136 // Unknown pattern found.
5137 return SDValue();
5138
5139 case ISD::BUILD_VECTOR: {
5140 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005141 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005142 return SDValue();
5143
Nadav Rotem9d68b062012-04-08 12:54:54 +00005144 Ld = Op.getOperand(0);
5145 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5146 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005147
5148 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005149 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005150 // Constants may have multiple users.
5151 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005152 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005153 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 }
5155
5156 case ISD::VECTOR_SHUFFLE: {
5157 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5158
5159 // Shuffles must have a splat mask where the first element is
5160 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005161 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005162 return SDValue();
5163
5164 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005165 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005166 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5167
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005168 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005169 return SDValue();
5170
5171 // Use the register form of the broadcast instruction available on AVX2.
5172 if (VT.is256BitVector())
5173 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5174 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5175 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005176
5177 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005178 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005179 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005180
5181 // The scalar_to_vector node and the suspected
5182 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005183 // Constants may have multiple users.
5184 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005185 return SDValue();
5186 break;
5187 }
5188 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005189
Craig Topper7a9a28b2012-08-12 02:23:29 +00005190 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005191
5192 // Handle the broadcasting a single constant scalar from the constant pool
5193 // into a vector. On Sandybridge it is still better to load a constant vector
5194 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005195 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005196 EVT CVT = Ld.getValueType();
5197 assert(!CVT.isVector() && "Must not broadcast a vector type");
5198 unsigned ScalarSize = CVT.getSizeInBits();
5199
Craig Topper5da8a802012-05-04 05:49:51 +00005200 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005201 const Constant *C = 0;
5202 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5203 C = CI->getConstantIntValue();
5204 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5205 C = CF->getConstantFPValue();
5206
5207 assert(C && "Invalid constant type");
5208
Nadav Rotem154819d2012-04-09 07:45:58 +00005209 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005210 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005211 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005212 MachinePointerInfo::getConstantPool(),
5213 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005214
Nadav Rotem9d68b062012-04-08 12:54:54 +00005215 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5216 }
5217 }
5218
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005219 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005220 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5221
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005222 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005223 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005224 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5225 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5226
5227 // The scalar source must be a normal load.
5228 if (!IsLoad)
5229 return SDValue();
5230
Craig Topper5da8a802012-05-04 05:49:51 +00005231 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005232 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005233
Craig Toppera9376332012-01-10 08:23:59 +00005234 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005235 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005236 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005237 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005238 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005239 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005240
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005241 // Unsupported broadcast.
5242 return SDValue();
5243}
5244
Evan Chengc3630942009-12-09 21:00:30 +00005245SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005246X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5247 EVT VT = Op.getValueType();
5248
5249 // Skip if insert_vec_elt is not supported.
5250 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5251 return SDValue();
5252
5253 DebugLoc DL = Op.getDebugLoc();
5254 unsigned NumElems = Op.getNumOperands();
5255
5256 SDValue VecIn1;
5257 SDValue VecIn2;
5258 SmallVector<unsigned, 4> InsertIndices;
5259 SmallVector<int, 8> Mask(NumElems, -1);
5260
5261 for (unsigned i = 0; i != NumElems; ++i) {
5262 unsigned Opc = Op.getOperand(i).getOpcode();
5263
5264 if (Opc == ISD::UNDEF)
5265 continue;
5266
5267 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5268 // Quit if more than 1 elements need inserting.
5269 if (InsertIndices.size() > 1)
5270 return SDValue();
5271
5272 InsertIndices.push_back(i);
5273 continue;
5274 }
5275
5276 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5277 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5278
5279 // Quit if extracted from vector of different type.
5280 if (ExtractedFromVec.getValueType() != VT)
5281 return SDValue();
5282
5283 // Quit if non-constant index.
5284 if (!isa<ConstantSDNode>(ExtIdx))
5285 return SDValue();
5286
5287 if (VecIn1.getNode() == 0)
5288 VecIn1 = ExtractedFromVec;
5289 else if (VecIn1 != ExtractedFromVec) {
5290 if (VecIn2.getNode() == 0)
5291 VecIn2 = ExtractedFromVec;
5292 else if (VecIn2 != ExtractedFromVec)
5293 // Quit if more than 2 vectors to shuffle
5294 return SDValue();
5295 }
5296
5297 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5298
5299 if (ExtractedFromVec == VecIn1)
5300 Mask[i] = Idx;
5301 else if (ExtractedFromVec == VecIn2)
5302 Mask[i] = Idx + NumElems;
5303 }
5304
5305 if (VecIn1.getNode() == 0)
5306 return SDValue();
5307
5308 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5309 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5310 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5311 unsigned Idx = InsertIndices[i];
5312 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5313 DAG.getIntPtrConstant(Idx));
5314 }
5315
5316 return NV;
5317}
5318
5319SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005320X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005321 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005322
Craig Topper45e1c752013-01-20 00:38:18 +00005323 MVT VT = Op.getValueType().getSimpleVT();
5324 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005325 unsigned NumElems = Op.getNumOperands();
5326
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005327 // Vectors containing all zeros can be matched by pxor and xorps later
5328 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5329 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5330 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005331 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005332 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005333
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005334 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005335 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005336
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005337 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005338 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5339 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005340 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005341 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005342 return Op;
5343
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005344 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005345 }
5346
Nadav Rotem154819d2012-04-09 07:45:58 +00005347 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005348 if (Broadcast.getNode())
5349 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005350
Owen Andersone50ed302009-08-10 22:56:29 +00005351 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005352
Evan Cheng0db9fe62006-04-25 20:13:52 +00005353 unsigned NumZero = 0;
5354 unsigned NumNonZero = 0;
5355 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005356 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005357 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005359 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005360 if (Elt.getOpcode() == ISD::UNDEF)
5361 continue;
5362 Values.insert(Elt);
5363 if (Elt.getOpcode() != ISD::Constant &&
5364 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005365 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005366 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005367 NumZero++;
5368 else {
5369 NonZeros |= (1 << i);
5370 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005371 }
5372 }
5373
Chris Lattner97a2a562010-08-26 05:24:29 +00005374 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5375 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005376 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005377
Chris Lattner67f453a2008-03-09 05:42:06 +00005378 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005379 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005380 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005381 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005382
Chris Lattner62098042008-03-09 01:05:04 +00005383 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5384 // the value are obviously zero, truncate the value to i32 and do the
5385 // insertion that way. Only do this if the value is non-constant or if the
5386 // value is a constant being inserted into element 0. It is cheaper to do
5387 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005388 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005389 (!IsAllConstants || Idx == 0)) {
5390 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005391 // Handle SSE only.
5392 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5393 EVT VecVT = MVT::v4i32;
5394 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005395
Chris Lattner62098042008-03-09 01:05:04 +00005396 // Truncate the value (which may itself be a constant) to i32, and
5397 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005398 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005399 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005400 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005401
Chris Lattner62098042008-03-09 01:05:04 +00005402 // Now we have our 32-bit value zero extended in the low element of
5403 // a vector. If Idx != 0, swizzle it into place.
5404 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005405 SmallVector<int, 4> Mask;
5406 Mask.push_back(Idx);
5407 for (unsigned i = 1; i != VecElts; ++i)
5408 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005409 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005410 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005411 }
Craig Topper07a27622012-01-22 03:07:48 +00005412 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005413 }
5414 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005415
Chris Lattner19f79692008-03-08 22:59:52 +00005416 // If we have a constant or non-constant insertion into the low element of
5417 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5418 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005419 // depending on what the source datatype is.
5420 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005421 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005422 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005423
5424 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005425 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005426 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005427 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005428 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5429 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005430 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005431 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005432 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5433 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005434 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005435 }
5436
5437 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005438 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005439 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005440 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005441 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005442 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005443 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005444 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005445 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005446 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005447 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005448 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005449 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005450
5451 // Is it a vector logical left shift?
5452 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005453 X86::isZeroNode(Op.getOperand(0)) &&
5454 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005455 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005456 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005457 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005458 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005459 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005460 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005461
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005462 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005463 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464
Chris Lattner19f79692008-03-08 22:59:52 +00005465 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5466 // is a non-constant being inserted into an element other than the low one,
5467 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5468 // movd/movss) to move this into the low element, then shuffle it into
5469 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005471 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005472
Evan Cheng0db9fe62006-04-25 20:13:52 +00005473 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005474 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005475 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005476 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005477 MaskVec.push_back(i == Idx ? 0 : 1);
5478 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 }
5480 }
5481
Chris Lattner67f453a2008-03-09 05:42:06 +00005482 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005483 if (Values.size() == 1) {
5484 if (EVTBits == 32) {
5485 // Instead of a shuffle like this:
5486 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5487 // Check if it's possible to issue this instead.
5488 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5489 unsigned Idx = CountTrailingZeros_32(NonZeros);
5490 SDValue Item = Op.getOperand(Idx);
5491 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5492 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5493 }
Dan Gohman475871a2008-07-27 21:46:04 +00005494 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005495 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005496
Dan Gohmana3941172007-07-24 22:55:08 +00005497 // A vector full of immediates; various special cases are already
5498 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005499 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005500 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005501
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005502 // For AVX-length vectors, build the individual 128-bit pieces and use
5503 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005504 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005505 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005506 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005507 V.push_back(Op.getOperand(i));
5508
5509 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5510
5511 // Build both the lower and upper subvector.
5512 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5513 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5514 NumElems/2);
5515
5516 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005517 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005518 }
5519
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005520 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005521 if (EVTBits == 64) {
5522 if (NumNonZero == 1) {
5523 // One half is zero or undef.
5524 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005525 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005526 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005527 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005528 }
Dan Gohman475871a2008-07-27 21:46:04 +00005529 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005530 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005531
5532 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005533 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005535 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005536 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005537 }
5538
Bill Wendling826f36f2007-03-28 00:57:11 +00005539 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005540 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005541 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005542 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543 }
5544
5545 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005546 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005547 if (NumElems == 4 && NumZero > 0) {
5548 for (unsigned i = 0; i < 4; ++i) {
5549 bool isZero = !(NonZeros & (1 << i));
5550 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005551 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005552 else
Dale Johannesenace16102009-02-03 19:33:06 +00005553 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005554 }
5555
5556 for (unsigned i = 0; i < 2; ++i) {
5557 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5558 default: break;
5559 case 0:
5560 V[i] = V[i*2]; // Must be a zero vector.
5561 break;
5562 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005563 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005564 break;
5565 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005566 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005567 break;
5568 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005569 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005570 break;
5571 }
5572 }
5573
Benjamin Kramer9c683542012-01-30 15:16:21 +00005574 bool Reverse1 = (NonZeros & 0x3) == 2;
5575 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5576 int MaskVec[] = {
5577 Reverse1 ? 1 : 0,
5578 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005579 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5580 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005581 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005582 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005583 }
5584
Craig Topper7a9a28b2012-08-12 02:23:29 +00005585 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005586 // Check for a build vector of consecutive loads.
5587 for (unsigned i = 0; i < NumElems; ++i)
5588 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005589
Nate Begemanfdea31a2010-03-24 20:49:50 +00005590 // Check for elements which are consecutive loads.
5591 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5592 if (LD.getNode())
5593 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005594
Michael Liaofacace82012-10-19 17:15:18 +00005595 // Check for a build vector from mostly shuffle plus few inserting.
5596 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5597 if (Sh.getNode())
5598 return Sh;
5599
Michael J. Spencerec38de22010-10-10 22:04:20 +00005600 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005601 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005602 SDValue Result;
5603 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5604 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5605 else
5606 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005607
Chris Lattner24faf612010-08-28 17:59:08 +00005608 for (unsigned i = 1; i < NumElems; ++i) {
5609 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5610 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005611 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005612 }
5613 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005614 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005615
Chris Lattner6e80e442010-08-28 17:15:43 +00005616 // Otherwise, expand into a number of unpckl*, start by extending each of
5617 // our (non-undef) elements to the full vector width with the element in the
5618 // bottom slot of the vector (which generates no code for SSE).
5619 for (unsigned i = 0; i < NumElems; ++i) {
5620 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5621 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5622 else
5623 V[i] = DAG.getUNDEF(VT);
5624 }
5625
5626 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5628 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5629 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005630 unsigned EltStride = NumElems >> 1;
5631 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005632 for (unsigned i = 0; i < EltStride; ++i) {
5633 // If V[i+EltStride] is undef and this is the first round of mixing,
5634 // then it is safe to just drop this shuffle: V[i] is already in the
5635 // right place, the one element (since it's the first round) being
5636 // inserted as undef can be dropped. This isn't safe for successive
5637 // rounds because they will permute elements within both vectors.
5638 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5639 EltStride == NumElems/2)
5640 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005641
Chris Lattner6e80e442010-08-28 17:15:43 +00005642 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005643 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005644 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005645 }
5646 return V[0];
5647 }
Dan Gohman475871a2008-07-27 21:46:04 +00005648 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005649}
5650
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005651// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5652// to create 256-bit vectors from two other 128-bit ones.
5653static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5654 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005655 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005656
Craig Topper7a9a28b2012-08-12 02:23:29 +00005657 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005658
5659 SDValue V1 = Op.getOperand(0);
5660 SDValue V2 = Op.getOperand(1);
5661 unsigned NumElems = ResVT.getVectorNumElements();
5662
Craig Topper4c7972d2012-04-22 18:15:59 +00005663 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005664}
5665
Craig Topper55b24052012-09-11 06:15:32 +00005666static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005667 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005668
5669 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5670 // from two other 128-bit ones.
5671 return LowerAVXCONCAT_VECTORS(Op, DAG);
5672}
5673
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005674// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005675static SDValue
5676LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5677 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005678 SDValue V1 = SVOp->getOperand(0);
5679 SDValue V2 = SVOp->getOperand(1);
5680 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005681 MVT VT = SVOp->getValueType(0).getSimpleVT();
5682 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005683 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005684
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005685 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5686 return SDValue();
5687 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005688 return SDValue();
5689
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005690 // Check the mask for BLEND and build the value.
5691 unsigned MaskValue = 0;
5692 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005693 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005694 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005695
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005696 // Blend for v16i16 should be symetric for the both lanes.
5697 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005698
Craig Topper9b33ef72013-01-21 06:57:59 +00005699 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005700 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005701 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005702
Craig Topper04f74a12013-01-21 07:25:16 +00005703 if ((EltIdx < 0 || EltIdx == (int)i) &&
5704 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005705 continue;
5706
Craig Topper9b33ef72013-01-21 06:57:59 +00005707 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005708 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005709 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5710 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005711 else
Craig Topper1842ba02012-04-23 06:38:28 +00005712 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005713 }
5714
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005715 // Convert i32 vectors to floating point if it is not AVX2.
5716 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005717 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005718 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005719 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5720 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005721 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5722 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5723 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005724
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005725 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5726 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005727 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005728}
5729
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730// v8i16 shuffles - Prefer shuffles in the following order:
5731// 1. [all] pshuflw, pshufhw, optional move
5732// 2. [ssse3] 1 x pshufb
5733// 3. [ssse3] 2 x pshufb + 1 x por
5734// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005735static SDValue
5736LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5737 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005738 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005739 SDValue V1 = SVOp->getOperand(0);
5740 SDValue V2 = SVOp->getOperand(1);
5741 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005743
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 // Determine if more than 1 of the words in each of the low and high quadwords
5745 // of the result come from the same quadword of one of the two inputs. Undef
5746 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005747 unsigned LoQuad[] = { 0, 0, 0, 0 };
5748 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005749 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005751 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005752 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 MaskVals.push_back(EltIdx);
5754 if (EltIdx < 0) {
5755 ++Quad[0];
5756 ++Quad[1];
5757 ++Quad[2];
5758 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005759 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005760 }
5761 ++Quad[EltIdx / 4];
5762 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005763 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005764
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005766 unsigned MaxQuad = 1;
5767 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 if (LoQuad[i] > MaxQuad) {
5769 BestLoQuad = i;
5770 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005771 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005772 }
5773
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005775 MaxQuad = 1;
5776 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 if (HiQuad[i] > MaxQuad) {
5778 BestHiQuad = i;
5779 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005780 }
5781 }
5782
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005784 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 // single pshufb instruction is necessary. If There are more than 2 input
5786 // quads, disable the next transformation since it does not help SSSE3.
5787 bool V1Used = InputQuads[0] || InputQuads[1];
5788 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005789 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005791 BestLoQuad = InputQuads[0] ? 0 : 1;
5792 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 }
5794 if (InputQuads.count() > 2) {
5795 BestLoQuad = -1;
5796 BestHiQuad = -1;
5797 }
5798 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005799
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5801 // the shuffle mask. If a quad is scored as -1, that means that it contains
5802 // words from all 4 input quadwords.
5803 SDValue NewV;
5804 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005805 int MaskV[] = {
5806 BestLoQuad < 0 ? 0 : BestLoQuad,
5807 BestHiQuad < 0 ? 1 : BestHiQuad
5808 };
Eric Christopherfd179292009-08-27 18:07:15 +00005809 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005810 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5811 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5812 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005813
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5815 // source words for the shuffle, to aid later transformations.
5816 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005817 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005818 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005820 if (idx != (int)i)
5821 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005823 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 AllWordsInNewV = false;
5825 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005826 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005827
Nate Begemanb9a47b82009-02-23 08:49:38 +00005828 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5829 if (AllWordsInNewV) {
5830 for (int i = 0; i != 8; ++i) {
5831 int idx = MaskVals[i];
5832 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005833 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005834 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 if ((idx != i) && idx < 4)
5836 pshufhw = false;
5837 if ((idx != i) && idx > 3)
5838 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 V1 = NewV;
5841 V2Used = false;
5842 BestLoQuad = 0;
5843 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005844 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005845
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5847 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005848 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005849 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5850 unsigned TargetMask = 0;
5851 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005853 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5854 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5855 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005856 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005857 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005858 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005859 }
Eric Christopherfd179292009-08-27 18:07:15 +00005860
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005861 // Promote splats to a larger type which usually leads to more efficient code.
5862 // FIXME: Is this true if pshufb is available?
5863 if (SVOp->isSplat())
5864 return PromoteSplat(SVOp, DAG);
5865
Nate Begemanb9a47b82009-02-23 08:49:38 +00005866 // If we have SSSE3, and all words of the result are from 1 input vector,
5867 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5868 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005869 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005871
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005873 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 // mask, and elements that come from V1 in the V2 mask, so that the two
5875 // results can be OR'd together.
5876 bool TwoInputs = V1Used && V2Used;
5877 for (unsigned i = 0; i != 8; ++i) {
5878 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005879 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5880 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005881 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005882 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005884 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005885 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005886 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005889 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005890
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 // Calculate the shuffle mask for the second input, shuffle it, and
5892 // OR it with the first shuffled input.
5893 pshufbMask.clear();
5894 for (unsigned i = 0; i != 8; ++i) {
5895 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005896 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5897 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5898 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5899 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005900 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005901 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005902 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005903 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 MVT::v16i8, &pshufbMask[0], 16));
5905 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005906 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 }
5908
5909 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5910 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005911 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005913 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005914 for (int i = 0; i != 4; ++i) {
5915 int idx = MaskVals[i];
5916 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 InOrder.set(i);
5918 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005919 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 }
5922 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005923 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005924 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005925
Craig Topperdd637ae2012-02-19 05:41:45 +00005926 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5927 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005928 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005929 NewV.getOperand(0),
5930 getShufflePSHUFLWImmediate(SVOp), DAG);
5931 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 }
Eric Christopherfd179292009-08-27 18:07:15 +00005933
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5935 // and update MaskVals with the new element order.
5936 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005937 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 for (unsigned i = 4; i != 8; ++i) {
5939 int idx = MaskVals[i];
5940 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005941 InOrder.set(i);
5942 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005943 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 }
5946 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005947 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005948 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005949
Craig Topperdd637ae2012-02-19 05:41:45 +00005950 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5951 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005952 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005953 NewV.getOperand(0),
5954 getShufflePSHUFHWImmediate(SVOp), DAG);
5955 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 }
Eric Christopherfd179292009-08-27 18:07:15 +00005957
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 // In case BestHi & BestLo were both -1, which means each quadword has a word
5959 // from each of the four input quadwords, calculate the InOrder bitvector now
5960 // before falling through to the insert/extract cleanup.
5961 if (BestLoQuad == -1 && BestHiQuad == -1) {
5962 NewV = V1;
5963 for (int i = 0; i != 8; ++i)
5964 if (MaskVals[i] < 0 || MaskVals[i] == i)
5965 InOrder.set(i);
5966 }
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 // The other elements are put in the right place using pextrw and pinsrw.
5969 for (unsigned i = 0; i != 8; ++i) {
5970 if (InOrder[i])
5971 continue;
5972 int EltIdx = MaskVals[i];
5973 if (EltIdx < 0)
5974 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005975 SDValue ExtOp = (EltIdx < 8) ?
5976 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5977 DAG.getIntPtrConstant(EltIdx)) :
5978 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005981 DAG.getIntPtrConstant(i));
5982 }
5983 return NewV;
5984}
5985
5986// v16i8 shuffles - Prefer shuffles in the following order:
5987// 1. [ssse3] 1 x pshufb
5988// 2. [ssse3] 2 x pshufb + 1 x por
5989// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5990static
Nate Begeman9008ca62009-04-27 18:41:29 +00005991SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005992 SelectionDAG &DAG,
5993 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005994 SDValue V1 = SVOp->getOperand(0);
5995 SDValue V2 = SVOp->getOperand(1);
5996 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005997 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005998
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005999 // Promote splats to a larger type which usually leads to more efficient code.
6000 // FIXME: Is this true if pshufb is available?
6001 if (SVOp->isSplat())
6002 return PromoteSplat(SVOp, DAG);
6003
Nate Begemanb9a47b82009-02-23 08:49:38 +00006004 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006005 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006006 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006007
Nate Begemanb9a47b82009-02-23 08:49:38 +00006008 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006009 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006011
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006013 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 //
6015 // Otherwise, we have elements from both input vectors, and must zero out
6016 // elements that come from V2 in the first mask, and V1 in the second mask
6017 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 for (unsigned i = 0; i != 16; ++i) {
6019 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006020 if (EltIdx < 0 || EltIdx >= 16)
6021 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006023 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006024 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006025 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006026 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006027
6028 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6029 // the 2nd operand if it's undefined or zero.
6030 if (V2.getOpcode() == ISD::UNDEF ||
6031 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006032 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006033
Nate Begemanb9a47b82009-02-23 08:49:38 +00006034 // Calculate the shuffle mask for the second input, shuffle it, and
6035 // OR it with the first shuffled input.
6036 pshufbMask.clear();
6037 for (unsigned i = 0; i != 16; ++i) {
6038 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006039 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006040 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006041 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006042 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006043 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 MVT::v16i8, &pshufbMask[0], 16));
6045 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006046 }
Eric Christopherfd179292009-08-27 18:07:15 +00006047
Nate Begemanb9a47b82009-02-23 08:49:38 +00006048 // No SSSE3 - Calculate in place words and then fix all out of place words
6049 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6050 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006051 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6052 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006053 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006054 for (int i = 0; i != 8; ++i) {
6055 int Elt0 = MaskVals[i*2];
6056 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006057
Nate Begemanb9a47b82009-02-23 08:49:38 +00006058 // This word of the result is all undef, skip it.
6059 if (Elt0 < 0 && Elt1 < 0)
6060 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006061
Nate Begemanb9a47b82009-02-23 08:49:38 +00006062 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006063 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006064 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006065
Nate Begemanb9a47b82009-02-23 08:49:38 +00006066 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6067 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6068 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006069
6070 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6071 // using a single extract together, load it and store it.
6072 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006073 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006074 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006075 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006076 DAG.getIntPtrConstant(i));
6077 continue;
6078 }
6079
Nate Begemanb9a47b82009-02-23 08:49:38 +00006080 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006081 // source byte is not also odd, shift the extracted word left 8 bits
6082 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006083 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006084 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006085 DAG.getIntPtrConstant(Elt1 / 2));
6086 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006088 DAG.getConstant(8,
6089 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006090 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006091 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6092 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006093 }
6094 // If Elt0 is defined, extract it from the appropriate source. If the
6095 // source byte is not also even, shift the extracted word right 8 bits. If
6096 // Elt1 was also defined, OR the extracted values together before
6097 // inserting them in the result.
6098 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006099 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006100 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6101 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006102 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006103 DAG.getConstant(8,
6104 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006105 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006106 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6107 DAG.getConstant(0x00FF, MVT::i16));
6108 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006109 : InsElt0;
6110 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006111 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006112 DAG.getIntPtrConstant(i));
6113 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006114 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006115}
6116
Elena Demikhovsky41789462012-09-06 12:42:01 +00006117// v32i8 shuffles - Translate to VPSHUFB if possible.
6118static
6119SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006120 const X86Subtarget *Subtarget,
6121 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006122 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006123 SDValue V1 = SVOp->getOperand(0);
6124 SDValue V2 = SVOp->getOperand(1);
6125 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006126 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006127
6128 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006129 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6130 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006131
Michael Liao471b9172012-10-03 23:43:52 +00006132 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006133 // (1) one of input vector is undefined or zeroinitializer.
6134 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6135 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006136 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006137 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006138 return SDValue();
6139
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006140 if (V1IsAllZero && !V2IsAllZero) {
6141 CommuteVectorShuffleMask(MaskVals, 32);
6142 V1 = V2;
6143 }
6144 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006145 for (unsigned i = 0; i != 32; i++) {
6146 int EltIdx = MaskVals[i];
6147 if (EltIdx < 0 || EltIdx >= 32)
6148 EltIdx = 0x80;
6149 else {
6150 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6151 // Cross lane is not allowed.
6152 return SDValue();
6153 EltIdx &= 0xf;
6154 }
6155 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6156 }
6157 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6158 DAG.getNode(ISD::BUILD_VECTOR, dl,
6159 MVT::v32i8, &pshufbMask[0], 32));
6160}
6161
Evan Cheng7a831ce2007-12-15 03:00:47 +00006162/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006163/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006164/// done when every pair / quad of shuffle mask elements point to elements in
6165/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006166/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006167static
Nate Begeman9008ca62009-04-27 18:41:29 +00006168SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006169 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006170 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006171 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006172 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006173 MVT NewVT;
6174 unsigned Scale;
6175 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006176 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006177 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6178 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6179 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6180 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6181 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6182 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006183 }
6184
Nate Begeman9008ca62009-04-27 18:41:29 +00006185 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006186 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006187 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006188 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006189 int EltIdx = SVOp->getMaskElt(i+j);
6190 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006191 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006192 if (StartIdx < 0)
6193 StartIdx = (EltIdx / Scale);
6194 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006195 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006196 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006197 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006198 }
6199
Craig Topper11ac1f82012-05-04 04:08:44 +00006200 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6201 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006202 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006203}
6204
Evan Chengd880b972008-05-09 21:53:03 +00006205/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006206///
Craig Topperf84b7502013-01-20 00:50:58 +00006207static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006208 SDValue SrcOp, SelectionDAG &DAG,
6209 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006210 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006211 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006212 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006213 LD = dyn_cast<LoadSDNode>(SrcOp);
6214 if (!LD) {
6215 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6216 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006217 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006218 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006219 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006220 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006221 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006222 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006223 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006224 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006225 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6226 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6227 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006228 SrcOp.getOperand(0)
6229 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006230 }
6231 }
6232 }
6233
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006234 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006235 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006236 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006237 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006238}
6239
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006240/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6241/// which could not be matched by any known target speficic shuffle
6242static SDValue
6243LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006244
6245 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6246 if (NewOp.getNode())
6247 return NewOp;
6248
Craig Topper657a99c2013-01-19 23:36:09 +00006249 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006250
Craig Topper8f35c132012-01-20 09:29:03 +00006251 unsigned NumElems = VT.getVectorNumElements();
6252 unsigned NumLaneElems = NumElems / 2;
6253
Craig Topper8f35c132012-01-20 09:29:03 +00006254 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006255 MVT EltVT = VT.getVectorElementType();
6256 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006257 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006258
Craig Topper9a2b6e12012-04-06 07:45:23 +00006259 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006260 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006261 // Build a shuffle mask for the output, discovering on the fly which
6262 // input vectors to use as shuffle operands (recorded in InputUsed).
6263 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006264 // out with UseBuildVector set.
6265 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006266 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006267 unsigned LaneStart = l * NumLaneElems;
6268 for (unsigned i = 0; i != NumLaneElems; ++i) {
6269 // The mask element. This indexes into the input.
6270 int Idx = SVOp->getMaskElt(i+LaneStart);
6271 if (Idx < 0) {
6272 // the mask element does not index into any input vector.
6273 Mask.push_back(-1);
6274 continue;
6275 }
Craig Topper8f35c132012-01-20 09:29:03 +00006276
Craig Topper9a2b6e12012-04-06 07:45:23 +00006277 // The input vector this mask element indexes into.
6278 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006279
Craig Topper9a2b6e12012-04-06 07:45:23 +00006280 // Turn the index into an offset from the start of the input vector.
6281 Idx -= Input * NumLaneElems;
6282
6283 // Find or create a shuffle vector operand to hold this input.
6284 unsigned OpNo;
6285 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6286 if (InputUsed[OpNo] == Input)
6287 // This input vector is already an operand.
6288 break;
6289 if (InputUsed[OpNo] < 0) {
6290 // Create a new operand for this input vector.
6291 InputUsed[OpNo] = Input;
6292 break;
6293 }
6294 }
6295
6296 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006297 // More than two input vectors used! Give up on trying to create a
6298 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6299 UseBuildVector = true;
6300 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006301 }
6302
6303 // Add the mask index for the new shuffle vector.
6304 Mask.push_back(Idx + OpNo * NumLaneElems);
6305 }
6306
Craig Topper8ae97ba2012-05-21 06:40:16 +00006307 if (UseBuildVector) {
6308 SmallVector<SDValue, 16> SVOps;
6309 for (unsigned i = 0; i != NumLaneElems; ++i) {
6310 // The mask element. This indexes into the input.
6311 int Idx = SVOp->getMaskElt(i+LaneStart);
6312 if (Idx < 0) {
6313 SVOps.push_back(DAG.getUNDEF(EltVT));
6314 continue;
6315 }
6316
6317 // The input vector this mask element indexes into.
6318 int Input = Idx / NumElems;
6319
6320 // Turn the index into an offset from the start of the input vector.
6321 Idx -= Input * NumElems;
6322
6323 // Extract the vector element by hand.
6324 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6325 SVOp->getOperand(Input),
6326 DAG.getIntPtrConstant(Idx)));
6327 }
6328
6329 // Construct the output using a BUILD_VECTOR.
6330 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6331 SVOps.size());
6332 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006333 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006334 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006335 } else {
6336 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006337 (InputUsed[0] % 2) * NumLaneElems,
6338 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006339 // If only one input was used, use an undefined vector for the other.
6340 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6341 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006342 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006343 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006344 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006345 }
6346
6347 Mask.clear();
6348 }
Craig Topper8f35c132012-01-20 09:29:03 +00006349
6350 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006351 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006352}
6353
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006354/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6355/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006356static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006357LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006358 SDValue V1 = SVOp->getOperand(0);
6359 SDValue V2 = SVOp->getOperand(1);
6360 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006361 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006362
Craig Topper7a9a28b2012-08-12 02:23:29 +00006363 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006364
Benjamin Kramer9c683542012-01-30 15:16:21 +00006365 std::pair<int, int> Locs[4];
6366 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006367 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006368
Evan Chengace3c172008-07-22 21:13:36 +00006369 unsigned NumHi = 0;
6370 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006371 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006372 int Idx = PermMask[i];
6373 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006374 Locs[i] = std::make_pair(-1, -1);
6375 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006376 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6377 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006378 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006379 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006380 NumLo++;
6381 } else {
6382 Locs[i] = std::make_pair(1, NumHi);
6383 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006385 NumHi++;
6386 }
6387 }
6388 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006389
Evan Chengace3c172008-07-22 21:13:36 +00006390 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006391 // If no more than two elements come from either vector. This can be
6392 // implemented with two shuffles. First shuffle gather the elements.
6393 // The second shuffle, which takes the first shuffle as both of its
6394 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006395 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006396
Benjamin Kramer9c683542012-01-30 15:16:21 +00006397 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006398
Benjamin Kramer9c683542012-01-30 15:16:21 +00006399 for (unsigned i = 0; i != 4; ++i)
6400 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006401 unsigned Idx = (i < 2) ? 0 : 4;
6402 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006403 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006404 }
Evan Chengace3c172008-07-22 21:13:36 +00006405
Nate Begeman9008ca62009-04-27 18:41:29 +00006406 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006407 }
6408
6409 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006410 // Otherwise, we must have three elements from one vector, call it X, and
6411 // one element from the other, call it Y. First, use a shufps to build an
6412 // intermediate vector with the one element from Y and the element from X
6413 // that will be in the same half in the final destination (the indexes don't
6414 // matter). Then, use a shufps to build the final vector, taking the half
6415 // containing the element from Y from the intermediate, and the other half
6416 // from X.
6417 if (NumHi == 3) {
6418 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006419 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006420 std::swap(V1, V2);
6421 }
6422
6423 // Find the element from V2.
6424 unsigned HiIndex;
6425 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006426 int Val = PermMask[HiIndex];
6427 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006428 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006429 if (Val >= 4)
6430 break;
6431 }
6432
Nate Begeman9008ca62009-04-27 18:41:29 +00006433 Mask1[0] = PermMask[HiIndex];
6434 Mask1[1] = -1;
6435 Mask1[2] = PermMask[HiIndex^1];
6436 Mask1[3] = -1;
6437 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006438
6439 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006440 Mask1[0] = PermMask[0];
6441 Mask1[1] = PermMask[1];
6442 Mask1[2] = HiIndex & 1 ? 6 : 4;
6443 Mask1[3] = HiIndex & 1 ? 4 : 6;
6444 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006445 }
Craig Topper69947b92012-04-23 06:57:04 +00006446
6447 Mask1[0] = HiIndex & 1 ? 2 : 0;
6448 Mask1[1] = HiIndex & 1 ? 0 : 2;
6449 Mask1[2] = PermMask[2];
6450 Mask1[3] = PermMask[3];
6451 if (Mask1[2] >= 0)
6452 Mask1[2] += 4;
6453 if (Mask1[3] >= 0)
6454 Mask1[3] += 4;
6455 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006456 }
6457
6458 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006459 int LoMask[] = { -1, -1, -1, -1 };
6460 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006461
Benjamin Kramer9c683542012-01-30 15:16:21 +00006462 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006463 unsigned MaskIdx = 0;
6464 unsigned LoIdx = 0;
6465 unsigned HiIdx = 2;
6466 for (unsigned i = 0; i != 4; ++i) {
6467 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006468 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006469 MaskIdx = 1;
6470 LoIdx = 0;
6471 HiIdx = 2;
6472 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006473 int Idx = PermMask[i];
6474 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006475 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006476 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006477 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006478 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006479 LoIdx++;
6480 } else {
6481 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006482 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006483 HiIdx++;
6484 }
6485 }
6486
Nate Begeman9008ca62009-04-27 18:41:29 +00006487 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6488 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006489 int MaskOps[] = { -1, -1, -1, -1 };
6490 for (unsigned i = 0; i != 4; ++i)
6491 if (Locs[i].first != -1)
6492 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006493 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006494}
6495
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006496static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006497 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006498 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006499
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006500 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6501 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006502 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6503 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6504 // BUILD_VECTOR (load), undef
6505 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006506
6507 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006508}
6509
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006510static
Evan Cheng835580f2010-10-07 20:50:20 +00006511SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6512 EVT VT = Op.getValueType();
6513
6514 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006515 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6516 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006517 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6518 V1, DAG));
6519}
6520
6521static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006522SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006523 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006524 SDValue V1 = Op.getOperand(0);
6525 SDValue V2 = Op.getOperand(1);
6526 EVT VT = Op.getValueType();
6527
6528 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6529
Craig Topper1accb7e2012-01-10 06:54:16 +00006530 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006531 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6532
Evan Cheng0899f5c2011-08-31 02:05:24 +00006533 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6534 return DAG.getNode(ISD::BITCAST, dl, VT,
6535 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6536 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6537 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006538}
6539
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006540static
6541SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6542 SDValue V1 = Op.getOperand(0);
6543 SDValue V2 = Op.getOperand(1);
6544 EVT VT = Op.getValueType();
6545
6546 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6547 "unsupported shuffle type");
6548
6549 if (V2.getOpcode() == ISD::UNDEF)
6550 V2 = V1;
6551
6552 // v4i32 or v4f32
6553 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6554}
6555
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006556static
Craig Topper1accb7e2012-01-10 06:54:16 +00006557SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006558 SDValue V1 = Op.getOperand(0);
6559 SDValue V2 = Op.getOperand(1);
6560 EVT VT = Op.getValueType();
6561 unsigned NumElems = VT.getVectorNumElements();
6562
6563 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6564 // operand of these instructions is only memory, so check if there's a
6565 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6566 // same masks.
6567 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006568
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006569 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006570 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006571 CanFoldLoad = true;
6572
6573 // When V1 is a load, it can be folded later into a store in isel, example:
6574 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6575 // turns into:
6576 // (MOVLPSmr addr:$src1, VR128:$src2)
6577 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006578 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006579 CanFoldLoad = true;
6580
Dan Gohman65fd6562011-11-03 21:49:52 +00006581 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006582 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006583 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006584 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6585
6586 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006587 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006588 if (SVOp->getMaskElt(1) != -1)
6589 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006590 }
6591
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592 // movl and movlp will both match v2i64, but v2i64 is never matched by
6593 // movl earlier because we make it strict to avoid messing with the movlp load
6594 // folding logic (see the code above getMOVLP call). Match it here then,
6595 // this is horrible, but will stay like this until we move all shuffle
6596 // matching to x86 specific nodes. Note that for the 1st condition all
6597 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006598 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006599 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6600 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006601 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006602 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006603 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006604 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006605
6606 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6607
6608 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006609 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006610 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006611}
6612
Michael Liaod9d09602012-10-23 17:34:00 +00006613// Reduce a vector shuffle to zext.
6614SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006615X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006616 // PMOVZX is only available from SSE41.
6617 if (!Subtarget->hasSSE41())
6618 return SDValue();
6619
6620 EVT VT = Op.getValueType();
6621
6622 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006623 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006624 return SDValue();
6625
6626 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6627 DebugLoc DL = Op.getDebugLoc();
6628 SDValue V1 = Op.getOperand(0);
6629 SDValue V2 = Op.getOperand(1);
6630 unsigned NumElems = VT.getVectorNumElements();
6631
6632 // Extending is an unary operation and the element type of the source vector
6633 // won't be equal to or larger than i64.
6634 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6635 VT.getVectorElementType() == MVT::i64)
6636 return SDValue();
6637
6638 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6639 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006640 while ((1U << Shift) < NumElems) {
6641 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006642 break;
6643 Shift += 1;
6644 // The maximal ratio is 8, i.e. from i8 to i64.
6645 if (Shift > 3)
6646 return SDValue();
6647 }
6648
6649 // Check the shuffle mask.
6650 unsigned Mask = (1U << Shift) - 1;
6651 for (unsigned i = 0; i != NumElems; ++i) {
6652 int EltIdx = SVOp->getMaskElt(i);
6653 if ((i & Mask) != 0 && EltIdx != -1)
6654 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006655 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006656 return SDValue();
6657 }
6658
6659 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6660 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6661 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6662
6663 if (!isTypeLegal(NVT))
6664 return SDValue();
6665
6666 // Simplify the operand as it's prepared to be fed into shuffle.
6667 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6668 if (V1.getOpcode() == ISD::BITCAST &&
6669 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6670 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6671 V1.getOperand(0)
6672 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6673 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6674 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006675 ConstantSDNode *CIdx =
6676 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006677 // If it's foldable, i.e. normal load with single use, we will let code
6678 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006679 if (CIdx && CIdx->getZExtValue() == 0 &&
6680 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006681 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6682 }
6683
6684 return DAG.getNode(ISD::BITCAST, DL, VT,
6685 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6686}
6687
Nadav Rotem154819d2012-04-09 07:45:58 +00006688SDValue
6689X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006690 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006691 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006692 DebugLoc dl = Op.getDebugLoc();
6693 SDValue V1 = Op.getOperand(0);
6694 SDValue V2 = Op.getOperand(1);
6695
6696 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006697 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006698
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006699 // Handle splat operations
6700 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006701 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006702 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006703 if (Broadcast.getNode())
6704 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006705 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006706
Michael Liaod9d09602012-10-23 17:34:00 +00006707 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006708 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006709 if (NewOp.getNode())
6710 return NewOp;
6711
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006712 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6713 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006714 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6715 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006716 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006717 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006718 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006719 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006720 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006721 // FIXME: Figure out a cleaner way to do this.
6722 // Try to make use of movq to zero out the top part.
6723 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006724 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006725 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006726 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006727 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6728 NewVT, true, false))
6729 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006730 DAG, Subtarget, dl);
6731 }
6732 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006733 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006734 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006735 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006736 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6737 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6738 DAG, Subtarget, dl);
6739 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006740 }
6741 }
6742 return SDValue();
6743}
6744
Dan Gohman475871a2008-07-27 21:46:04 +00006745SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006746X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006747 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue V1 = Op.getOperand(0);
6749 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006750 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006751 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006752 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006753 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006754 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006755 bool V1IsSplat = false;
6756 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006757 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006758 bool HasFp256 = Subtarget->hasFp256();
6759 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006760 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006761 bool OptForSize = MF.getFunction()->getAttributes().
6762 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763
Craig Topper3426a3e2011-11-14 06:46:21 +00006764 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006765
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006766 if (V1IsUndef && V2IsUndef)
6767 return DAG.getUNDEF(VT);
6768
6769 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006770
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006771 // Vector shuffle lowering takes 3 steps:
6772 //
6773 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6774 // narrowing and commutation of operands should be handled.
6775 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6776 // shuffle nodes.
6777 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6778 // so the shuffle can be broken into other shuffles and the legalizer can
6779 // try the lowering again.
6780 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006781 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006782 // be matched during isel, all of them must be converted to a target specific
6783 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006784
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006785 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6786 // narrowing and commutation of operands should be handled. The actual code
6787 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006788 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006789 if (NewOp.getNode())
6790 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006791
Craig Topper5aaffa82012-02-19 02:53:47 +00006792 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6793
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006794 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6795 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006796 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006797 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006798 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006799 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006800
Craig Topperdd637ae2012-02-19 05:41:45 +00006801 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006802 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006803 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006804
Craig Topperdd637ae2012-02-19 05:41:45 +00006805 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006806 return getMOVHighToLow(Op, dl, DAG);
6807
6808 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006809 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006810 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006811 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006812
Craig Topper5aaffa82012-02-19 02:53:47 +00006813 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006814 // The actual implementation will match the mask in the if above and then
6815 // during isel it can match several different instructions, not only pshufd
6816 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006817 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6818 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006819
Craig Topper5aaffa82012-02-19 02:53:47 +00006820 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006821
Craig Topper1accb7e2012-01-10 06:54:16 +00006822 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006823 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6824
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006825 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6826 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6827 DAG);
6828
Craig Topperb3982da2011-12-31 23:50:21 +00006829 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006830 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006831 }
Eric Christopherfd179292009-08-27 18:07:15 +00006832
Evan Chengf26ffe92008-05-29 08:22:04 +00006833 // Check if this can be converted into a logical shift.
6834 bool isLeft = false;
6835 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006836 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006837 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006838 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006839 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006840 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006841 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006842 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006843 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006844 }
Eric Christopherfd179292009-08-27 18:07:15 +00006845
Craig Topper5aaffa82012-02-19 02:53:47 +00006846 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006847 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006848 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006849 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006850 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006851 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6852
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006853 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006854 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6855 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006856 }
Eric Christopherfd179292009-08-27 18:07:15 +00006857
Nate Begeman9008ca62009-04-27 18:41:29 +00006858 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006859 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006860 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006861
Craig Topperdd637ae2012-02-19 05:41:45 +00006862 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006863 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006864
Craig Topperdd637ae2012-02-19 05:41:45 +00006865 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006866 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006867
Craig Topperdd637ae2012-02-19 05:41:45 +00006868 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006869 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006870
Craig Topperdd637ae2012-02-19 05:41:45 +00006871 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006872 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006873
Craig Topperdd637ae2012-02-19 05:41:45 +00006874 if (ShouldXformToMOVHLPS(M, VT) ||
6875 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006876 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006877
Evan Chengf26ffe92008-05-29 08:22:04 +00006878 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006879 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006880 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006881 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006882 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006883 }
Eric Christopherfd179292009-08-27 18:07:15 +00006884
Evan Cheng9eca5e82006-10-25 21:49:50 +00006885 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006886 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6887 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006888 V1IsSplat = isSplatVector(V1.getNode());
6889 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006890
Chris Lattner8a594482007-11-25 00:24:49 +00006891 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006892 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6893 CommuteVectorShuffleMask(M, NumElems);
6894 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006895 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006896 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006897 }
6898
Craig Topperbeabc6c2011-12-05 06:56:46 +00006899 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006900 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006901 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006902 return V1;
6903 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6904 // the instruction selector will not match, so get a canonical MOVL with
6905 // swapped operands to undo the commute.
6906 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006907 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006909 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006910 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006911
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006912 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006913 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006914
Evan Cheng9bbbb982006-10-25 20:48:19 +00006915 if (V2IsSplat) {
6916 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006917 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006918 // new vector_shuffle with the corrected mask.p
6919 SmallVector<int, 8> NewMask(M.begin(), M.end());
6920 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006921 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006922 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006923 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006924 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006925 }
6926
Evan Cheng9eca5e82006-10-25 21:49:50 +00006927 if (Commuted) {
6928 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006929 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006930 CommuteVectorShuffleMask(M, NumElems);
6931 std::swap(V1, V2);
6932 std::swap(V1IsSplat, V2IsSplat);
6933 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006934
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006935 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006936 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006937
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006938 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006939 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006940 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006941
Nate Begeman9008ca62009-04-27 18:41:29 +00006942 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006943 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006944 return CommuteVectorShuffle(SVOp, DAG);
6945
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006946 // The checks below are all present in isShuffleMaskLegal, but they are
6947 // inlined here right now to enable us to directly emit target specific
6948 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006949
Craig Topper0e2037b2012-01-20 05:53:00 +00006950 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006951 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006952 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006953 DAG);
6954
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006955 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6956 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006957 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006958 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006959 }
6960
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006961 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006962 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006963 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006964 DAG);
6965
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006966 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006967 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006968 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006969 DAG);
6970
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006971 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006972 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006973 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006974
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006975 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006976 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006977 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006978 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006979
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006980 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006981 // Generate target specific nodes for 128 or 256-bit shuffles only
6982 // supported in the AVX instruction set.
6983 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006984
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006985 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006986 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006987 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6988
Craig Topper70b883b2011-11-28 10:14:51 +00006989 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006990 if (isVPERMILPMask(M, VT, HasFp256)) {
6991 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006992 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006993 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006994 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006995 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006996 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006997
Craig Topper70b883b2011-11-28 10:14:51 +00006998 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006999 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007000 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007001 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007002
Craig Topper1842ba02012-04-23 06:38:28 +00007003 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007004 if (BlendOp.getNode())
7005 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007006
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007007 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007008 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007009 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007010 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007011 }
Craig Topper92040742012-04-16 06:43:40 +00007012 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7013 &permclMask[0], 8);
7014 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007015 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007016 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007017 }
Craig Topper095c5282012-04-15 23:48:57 +00007018
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007019 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007020 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007021 getShuffleCLImmediate(SVOp), DAG);
7022
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007023 //===--------------------------------------------------------------------===//
7024 // Since no target specific shuffle was selected for this generic one,
7025 // lower it into other known shuffles. FIXME: this isn't true yet, but
7026 // this is the plan.
7027 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007028
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007029 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7030 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007031 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007032 if (NewOp.getNode())
7033 return NewOp;
7034 }
7035
7036 if (VT == MVT::v16i8) {
7037 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7038 if (NewOp.getNode())
7039 return NewOp;
7040 }
7041
Elena Demikhovsky41789462012-09-06 12:42:01 +00007042 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007043 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007044 if (NewOp.getNode())
7045 return NewOp;
7046 }
7047
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007048 // Handle all 128-bit wide vectors with 4 elements, and match them with
7049 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007050 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007051 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7052
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007053 // Handle general 256-bit shuffles
7054 if (VT.is256BitVector())
7055 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7056
Dan Gohman475871a2008-07-27 21:46:04 +00007057 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007058}
7059
Craig Topperf84b7502013-01-20 00:50:58 +00007060static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007061 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007062 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007063
Craig Topper45e1c752013-01-20 00:38:18 +00007064 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007065 return SDValue();
7066
Duncan Sands83ec4b62008-06-06 12:08:01 +00007067 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007069 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007070 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007071 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007072 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007073 }
7074
7075 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007076 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7077 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7078 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7080 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007081 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007083 Op.getOperand(0)),
7084 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007086 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007087 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007088 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007089 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007090 }
7091
7092 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007093 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7094 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007095 // result has a single use which is a store or a bitcast to i32. And in
7096 // the case of a store, it's not worth it if the index is a constant 0,
7097 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007098 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007099 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007100 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007101 if ((User->getOpcode() != ISD::STORE ||
7102 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7103 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007104 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007105 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007106 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007107 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007108 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007109 Op.getOperand(0)),
7110 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007111 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007112 }
7113
7114 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007115 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007116 if (isa<ConstantSDNode>(Op.getOperand(1)))
7117 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007118 }
Dan Gohman475871a2008-07-27 21:46:04 +00007119 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007120}
7121
Dan Gohman475871a2008-07-27 21:46:04 +00007122SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007123X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7124 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007125 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007126 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007127
David Greene74a579d2011-02-10 16:57:36 +00007128 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007129 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007130
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007131 // If this is a 256-bit vector result, first extract the 128-bit vector and
7132 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007133 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007134 DebugLoc dl = Op.getNode()->getDebugLoc();
7135 unsigned NumElems = VecVT.getVectorNumElements();
7136 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007137 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7138
7139 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007140 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007141
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007142 if (IdxVal >= NumElems/2)
7143 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007144 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007145 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007146 }
7147
Craig Topper7a9a28b2012-08-12 02:23:29 +00007148 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007149
Craig Topperd0a31172012-01-10 06:37:29 +00007150 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007151 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007152 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007153 return Res;
7154 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007155
Craig Topper45e1c752013-01-20 00:38:18 +00007156 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007157 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007158 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007159 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007160 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007161 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007162 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7164 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007165 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007166 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007167 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007168 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007169 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007170 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007171 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007172 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007173 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007174 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007175 }
7176
7177 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007178 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179 if (Idx == 0)
7180 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007181
Evan Cheng0db9fe62006-04-25 20:13:52 +00007182 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007183 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007184 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007185 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007186 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007187 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007188 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007189 }
7190
7191 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007192 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7193 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7194 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007195 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007196 if (Idx == 0)
7197 return Op;
7198
7199 // UNPCKHPD the element to the lowest double word, then movsd.
7200 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7201 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007202 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007203 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007204 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007205 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007206 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007207 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007208 }
7209
Dan Gohman475871a2008-07-27 21:46:04 +00007210 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007211}
7212
Craig Topperf84b7502013-01-20 00:50:58 +00007213static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007214 MVT VT = Op.getValueType().getSimpleVT();
7215 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007216 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007217
Dan Gohman475871a2008-07-27 21:46:04 +00007218 SDValue N0 = Op.getOperand(0);
7219 SDValue N1 = Op.getOperand(1);
7220 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007221
Craig Topper7a9a28b2012-08-12 02:23:29 +00007222 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007223 return SDValue();
7224
Dan Gohman8a55ce42009-09-23 21:02:20 +00007225 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007226 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007227 unsigned Opc;
7228 if (VT == MVT::v8i16)
7229 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007230 else if (VT == MVT::v16i8)
7231 Opc = X86ISD::PINSRB;
7232 else
7233 Opc = X86ISD::PINSRB;
7234
Nate Begeman14d12ca2008-02-11 04:19:36 +00007235 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7236 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007237 if (N1.getValueType() != MVT::i32)
7238 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7239 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007240 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007241 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007242 }
7243
7244 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007245 // Bits [7:6] of the constant are the source select. This will always be
7246 // zero here. The DAG Combiner may combine an extract_elt index into these
7247 // bits. For example (insert (extract, 3), 2) could be matched by putting
7248 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007249 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007250 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007251 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007252 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007253 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007254 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007255 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007256 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007257 }
7258
7259 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007260 // PINSR* works with constant index.
7261 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007262 }
Dan Gohman475871a2008-07-27 21:46:04 +00007263 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007264}
7265
Dan Gohman475871a2008-07-27 21:46:04 +00007266SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007267X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007268 MVT VT = Op.getValueType().getSimpleVT();
7269 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007270
David Greene6b381262011-02-09 15:32:06 +00007271 DebugLoc dl = Op.getDebugLoc();
7272 SDValue N0 = Op.getOperand(0);
7273 SDValue N1 = Op.getOperand(1);
7274 SDValue N2 = Op.getOperand(2);
7275
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007276 // If this is a 256-bit vector result, first extract the 128-bit vector,
7277 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007278 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007279 if (!isa<ConstantSDNode>(N2))
7280 return SDValue();
7281
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007282 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007283 unsigned NumElems = VT.getVectorNumElements();
7284 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007285 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007286
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007287 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007288 bool Upper = IdxVal >= NumElems/2;
7289 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7290 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007291
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007292 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007293 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007294 }
7295
Craig Topperd0a31172012-01-10 06:37:29 +00007296 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007297 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7298
Dan Gohman8a55ce42009-09-23 21:02:20 +00007299 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007300 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007301
Dan Gohman8a55ce42009-09-23 21:02:20 +00007302 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007303 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7304 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007305 if (N1.getValueType() != MVT::i32)
7306 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7307 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007308 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007309 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007310 }
Dan Gohman475871a2008-07-27 21:46:04 +00007311 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007312}
7313
Craig Topper55b24052012-09-11 06:15:32 +00007314static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007315 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007316 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007317 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007318
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007319 // If this is a 256-bit vector result, first insert into a 128-bit
7320 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007321 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007322 // Insert into a 128-bit vector.
7323 EVT VT128 = EVT::getVectorVT(*Context,
7324 OpVT.getVectorElementType(),
7325 OpVT.getVectorNumElements() / 2);
7326
7327 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7328
7329 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007330 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007331 }
7332
Craig Topperd77d2fe2012-04-29 20:22:05 +00007333 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007334 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007335 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007336
Owen Anderson825b72b2009-08-11 20:47:22 +00007337 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007338 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007339 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007340 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007341}
7342
David Greene91585092011-01-26 15:38:49 +00007343// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7344// a simple subregister reference or explicit instructions to grab
7345// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007346static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7347 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007348 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007349 DebugLoc dl = Op.getNode()->getDebugLoc();
7350 SDValue Vec = Op.getNode()->getOperand(0);
7351 SDValue Idx = Op.getNode()->getOperand(1);
7352
Craig Topper7a9a28b2012-08-12 02:23:29 +00007353 if (Op.getNode()->getValueType(0).is128BitVector() &&
7354 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007355 isa<ConstantSDNode>(Idx)) {
7356 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7357 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007358 }
David Greene91585092011-01-26 15:38:49 +00007359 }
7360 return SDValue();
7361}
7362
David Greenecfe33c42011-01-26 19:13:22 +00007363// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7364// simple superregister reference or explicit instructions to insert
7365// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007366static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7367 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007368 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007369 DebugLoc dl = Op.getNode()->getDebugLoc();
7370 SDValue Vec = Op.getNode()->getOperand(0);
7371 SDValue SubVec = Op.getNode()->getOperand(1);
7372 SDValue Idx = Op.getNode()->getOperand(2);
7373
Craig Topper7a9a28b2012-08-12 02:23:29 +00007374 if (Op.getNode()->getValueType(0).is256BitVector() &&
7375 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007376 isa<ConstantSDNode>(Idx)) {
7377 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7378 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007379 }
7380 }
7381 return SDValue();
7382}
7383
Bill Wendling056292f2008-09-16 21:48:12 +00007384// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7385// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7386// one of the above mentioned nodes. It has to be wrapped because otherwise
7387// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7388// be used to form addressing mode. These wrapped nodes will be selected
7389// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007390SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007391X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007392 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007393
Chris Lattner41621a22009-06-26 19:22:52 +00007394 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7395 // global base reg.
7396 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007397 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007398 CodeModel::Model M = getTargetMachine().getCodeModel();
7399
Chris Lattner4f066492009-07-11 20:29:19 +00007400 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007401 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007402 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007403 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007404 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007405 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007406 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007407
Evan Cheng1606e8e2009-03-13 07:51:59 +00007408 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007409 CP->getAlignment(),
7410 CP->getOffset(), OpFlag);
7411 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007412 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007413 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007414 if (OpFlag) {
7415 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007416 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007417 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007418 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007419 }
7420
7421 return Result;
7422}
7423
Dan Gohmand858e902010-04-17 15:26:15 +00007424SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007425 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007426
Chris Lattner18c59872009-06-27 04:16:01 +00007427 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7428 // global base reg.
7429 unsigned char OpFlag = 0;
7430 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007431 CodeModel::Model M = getTargetMachine().getCodeModel();
7432
Chris Lattner4f066492009-07-11 20:29:19 +00007433 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007434 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007435 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007436 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007437 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007438 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007439 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007440
Chris Lattner18c59872009-06-27 04:16:01 +00007441 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7442 OpFlag);
7443 DebugLoc DL = JT->getDebugLoc();
7444 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007445
Chris Lattner18c59872009-06-27 04:16:01 +00007446 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007447 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007448 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7449 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007450 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007451 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007452
Chris Lattner18c59872009-06-27 04:16:01 +00007453 return Result;
7454}
7455
7456SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007457X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007458 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007459
Chris Lattner18c59872009-06-27 04:16:01 +00007460 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7461 // global base reg.
7462 unsigned char OpFlag = 0;
7463 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007464 CodeModel::Model M = getTargetMachine().getCodeModel();
7465
Chris Lattner4f066492009-07-11 20:29:19 +00007466 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007467 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7468 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7469 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007470 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007471 } else if (Subtarget->isPICStyleGOT()) {
7472 OpFlag = X86II::MO_GOT;
7473 } else if (Subtarget->isPICStyleStubPIC()) {
7474 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7475 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7476 OpFlag = X86II::MO_DARWIN_NONLAZY;
7477 }
Eric Christopherfd179292009-08-27 18:07:15 +00007478
Chris Lattner18c59872009-06-27 04:16:01 +00007479 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007480
Chris Lattner18c59872009-06-27 04:16:01 +00007481 DebugLoc DL = Op.getDebugLoc();
7482 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007483
Chris Lattner18c59872009-06-27 04:16:01 +00007484 // With PIC, the address is actually $g + Offset.
7485 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007486 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007487 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7488 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007489 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007490 Result);
7491 }
Eric Christopherfd179292009-08-27 18:07:15 +00007492
Eli Friedman586272d2011-08-11 01:48:05 +00007493 // For symbols that require a load from a stub to get the address, emit the
7494 // load.
7495 if (isGlobalStubReference(OpFlag))
7496 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007497 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007498
Chris Lattner18c59872009-06-27 04:16:01 +00007499 return Result;
7500}
7501
Dan Gohman475871a2008-07-27 21:46:04 +00007502SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007503X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007504 // Create the TargetBlockAddressAddress node.
7505 unsigned char OpFlags =
7506 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007507 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007508 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007509 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007510 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007511 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7512 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007513
Dan Gohmanf705adb2009-10-30 01:28:02 +00007514 if (Subtarget->isPICStyleRIPRel() &&
7515 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007516 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7517 else
7518 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007519
Dan Gohman29cbade2009-11-20 23:18:13 +00007520 // With PIC, the address is actually $g + Offset.
7521 if (isGlobalRelativeToPICBase(OpFlags)) {
7522 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7523 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7524 Result);
7525 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007526
7527 return Result;
7528}
7529
7530SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007531X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007532 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007533 // Create the TargetGlobalAddress node, folding in the constant
7534 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007535 unsigned char OpFlags =
7536 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007537 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007538 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007539 if (OpFlags == X86II::MO_NO_FLAG &&
7540 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007541 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007542 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007543 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007544 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007545 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007546 }
Eric Christopherfd179292009-08-27 18:07:15 +00007547
Chris Lattner4f066492009-07-11 20:29:19 +00007548 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007549 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007550 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7551 else
7552 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007553
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007554 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007555 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007556 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7557 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007558 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007559 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007560
Chris Lattner36c25012009-07-10 07:34:39 +00007561 // For globals that require a load from a stub to get the address, emit the
7562 // load.
7563 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007564 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007565 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007566
Dan Gohman6520e202008-10-18 02:06:02 +00007567 // If there was a non-zero offset that we didn't fold, create an explicit
7568 // addition for it.
7569 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007570 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007571 DAG.getConstant(Offset, getPointerTy()));
7572
Evan Cheng0db9fe62006-04-25 20:13:52 +00007573 return Result;
7574}
7575
Evan Chengda43bcf2008-09-24 00:05:32 +00007576SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007577X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007578 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007579 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007580 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007581}
7582
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007583static SDValue
7584GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007585 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007586 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007587 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007588 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007589 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007590 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007591 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007592 GA->getOffset(),
7593 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007594
7595 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7596 : X86ISD::TLSADDR;
7597
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007598 if (InFlag) {
7599 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007600 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007601 } else {
7602 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007603 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007604 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007605
7606 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007607 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007608
Rafael Espindola15f1b662009-04-24 12:59:40 +00007609 SDValue Flag = Chain.getValue(1);
7610 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007611}
7612
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007613// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007614static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007615LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007616 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007617 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007618 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7619 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007620 DAG.getNode(X86ISD::GlobalBaseReg,
7621 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007622 InFlag = Chain.getValue(1);
7623
Chris Lattnerb903bed2009-06-26 21:20:29 +00007624 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007625}
7626
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007627// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007628static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007629LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007630 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007631 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7632 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007633}
7634
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007635static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7636 SelectionDAG &DAG,
7637 const EVT PtrVT,
7638 bool is64Bit) {
7639 DebugLoc dl = GA->getDebugLoc();
7640
7641 // Get the start address of the TLS block for this module.
7642 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7643 .getInfo<X86MachineFunctionInfo>();
7644 MFI->incNumLocalDynamicTLSAccesses();
7645
7646 SDValue Base;
7647 if (is64Bit) {
7648 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7649 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7650 } else {
7651 SDValue InFlag;
7652 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7653 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7654 InFlag = Chain.getValue(1);
7655 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7656 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7657 }
7658
7659 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7660 // of Base.
7661
7662 // Build x@dtpoff.
7663 unsigned char OperandFlags = X86II::MO_DTPOFF;
7664 unsigned WrapperKind = X86ISD::Wrapper;
7665 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7666 GA->getValueType(0),
7667 GA->getOffset(), OperandFlags);
7668 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7669
7670 // Add x@dtpoff with the base.
7671 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7672}
7673
Hans Wennborg228756c2012-05-11 10:11:01 +00007674// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007675static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007676 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007677 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007678 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007679
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007680 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7681 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7682 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007683
Michael J. Spencerec38de22010-10-10 22:04:20 +00007684 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007685 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007686 MachinePointerInfo(Ptr),
7687 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007688
Chris Lattnerb903bed2009-06-26 21:20:29 +00007689 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007690 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7691 // initialexec.
7692 unsigned WrapperKind = X86ISD::Wrapper;
7693 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007694 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007695 } else if (model == TLSModel::InitialExec) {
7696 if (is64Bit) {
7697 OperandFlags = X86II::MO_GOTTPOFF;
7698 WrapperKind = X86ISD::WrapperRIP;
7699 } else {
7700 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7701 }
Chris Lattner18c59872009-06-27 04:16:01 +00007702 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007703 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007704 }
Eric Christopherfd179292009-08-27 18:07:15 +00007705
Hans Wennborg228756c2012-05-11 10:11:01 +00007706 // emit "addl x@ntpoff,%eax" (local exec)
7707 // or "addl x@indntpoff,%eax" (initial exec)
7708 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007709 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007710 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007711 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007712 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007713
Hans Wennborg228756c2012-05-11 10:11:01 +00007714 if (model == TLSModel::InitialExec) {
7715 if (isPIC && !is64Bit) {
7716 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7717 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7718 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007719 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007720
7721 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7722 MachinePointerInfo::getGOT(), false, false, false,
7723 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007724 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007725
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007726 // The address of the thread local variable is the add of the thread
7727 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007728 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007729}
7730
Dan Gohman475871a2008-07-27 21:46:04 +00007731SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007732X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007733
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007734 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007735 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007736
Eric Christopher30ef0e52010-06-03 04:07:48 +00007737 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007738 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007739
Eric Christopher30ef0e52010-06-03 04:07:48 +00007740 switch (model) {
7741 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007742 if (Subtarget->is64Bit())
7743 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7744 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007745 case TLSModel::LocalDynamic:
7746 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7747 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007748 case TLSModel::InitialExec:
7749 case TLSModel::LocalExec:
7750 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007751 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007752 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007753 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007754 llvm_unreachable("Unknown TLS model.");
7755 }
7756
7757 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007758 // Darwin only has one model of TLS. Lower to that.
7759 unsigned char OpFlag = 0;
7760 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7761 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007762
Eric Christopher30ef0e52010-06-03 04:07:48 +00007763 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7764 // global base reg.
7765 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7766 !Subtarget->is64Bit();
7767 if (PIC32)
7768 OpFlag = X86II::MO_TLVP_PIC_BASE;
7769 else
7770 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007771 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007772 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007773 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007774 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007775 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007776
Eric Christopher30ef0e52010-06-03 04:07:48 +00007777 // With PIC32, the address is actually $g + Offset.
7778 if (PIC32)
7779 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7780 DAG.getNode(X86ISD::GlobalBaseReg,
7781 DebugLoc(), getPointerTy()),
7782 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007783
Eric Christopher30ef0e52010-06-03 04:07:48 +00007784 // Lowering the machine isd will make sure everything is in the right
7785 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007786 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007787 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007788 SDValue Args[] = { Chain, Offset };
7789 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007790
Eric Christopher30ef0e52010-06-03 04:07:48 +00007791 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7792 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7793 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007794
Eric Christopher30ef0e52010-06-03 04:07:48 +00007795 // And our return value (tls address) is in the standard call return value
7796 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007797 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007798 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7799 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007800 }
7801
7802 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007803 // Just use the implicit TLS architecture
7804 // Need to generate someting similar to:
7805 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7806 // ; from TEB
7807 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7808 // mov rcx, qword [rdx+rcx*8]
7809 // mov eax, .tls$:tlsvar
7810 // [rax+rcx] contains the address
7811 // Windows 64bit: gs:0x58
7812 // Windows 32bit: fs:__tls_array
7813
7814 // If GV is an alias then use the aliasee for determining
7815 // thread-localness.
7816 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7817 GV = GA->resolveAliasedGlobal(false);
7818 DebugLoc dl = GA->getDebugLoc();
7819 SDValue Chain = DAG.getEntryNode();
7820
7821 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7822 // %gs:0x58 (64-bit).
7823 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7824 ? Type::getInt8PtrTy(*DAG.getContext(),
7825 256)
7826 : Type::getInt32PtrTy(*DAG.getContext(),
7827 257));
7828
7829 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7830 Subtarget->is64Bit()
7831 ? DAG.getIntPtrConstant(0x58)
7832 : DAG.getExternalSymbol("_tls_array",
7833 getPointerTy()),
7834 MachinePointerInfo(Ptr),
7835 false, false, false, 0);
7836
7837 // Load the _tls_index variable
7838 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7839 if (Subtarget->is64Bit())
7840 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7841 IDX, MachinePointerInfo(), MVT::i32,
7842 false, false, 0);
7843 else
7844 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7845 false, false, false, 0);
7846
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007847 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007848 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007849 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7850
7851 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7852 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7853 false, false, false, 0);
7854
7855 // Get the offset of start of .tls section
7856 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7857 GA->getValueType(0),
7858 GA->getOffset(), X86II::MO_SECREL);
7859 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7860
7861 // The address of the thread local variable is the add of the thread
7862 // pointer with the offset of the variable.
7863 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007864 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007865
David Blaikie4d6ccb52012-01-20 21:51:11 +00007866 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007867}
7868
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(
Tim Northover0a29cb02013-01-22 09:46:31 +00008035 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8036 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008037 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008038 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8039 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008040 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008041 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008042
Bill Wendling397ae212012-01-05 02:13:20 +00008043 // Load the 64-bit value into an XMM register.
8044 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8045 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008046 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008047 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008048 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008049 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8050 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8051 CLod0);
8052
Owen Anderson825b72b2009-08-11 20:47:22 +00008053 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008054 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008055 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008056 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008057 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008058 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008059
Craig Topperd0a31172012-01-10 06:37:29 +00008060 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008061 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8062 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8063 } else {
8064 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8065 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8066 S2F, 0x4E, DAG);
8067 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8068 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8069 Sub);
8070 }
8071
8072 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008073 DAG.getIntPtrConstant(0));
8074}
8075
Bill Wendling8b8a6362009-01-17 03:56:04 +00008076// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008077SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8078 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008079 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008080 // FP constant to bias correct the final result.
8081 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008082 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008083
8084 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008085 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008086 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008087
Eli Friedmanf3704762011-08-29 21:15:46 +00008088 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008089 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008090
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008092 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008093 DAG.getIntPtrConstant(0));
8094
8095 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008096 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008097 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008098 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008099 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008100 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008101 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008102 MVT::v2f64, Bias)));
8103 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008104 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008105 DAG.getIntPtrConstant(0));
8106
8107 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008109
8110 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008111 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008112
Craig Topper69947b92012-04-23 06:57:04 +00008113 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008114 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008115 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008116 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008117 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008118
8119 // Handle final rounding.
8120 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008121}
8122
Michael Liaoa7554632012-10-23 17:36:08 +00008123SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8124 SelectionDAG &DAG) const {
8125 SDValue N0 = Op.getOperand(0);
8126 EVT SVT = N0.getValueType();
8127 DebugLoc dl = Op.getDebugLoc();
8128
8129 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8130 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8131 "Custom UINT_TO_FP is not supported!");
8132
Craig Topperb99bafe2013-01-21 06:21:54 +00008133 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8134 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008135 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8136 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8137}
8138
Dan Gohmand858e902010-04-17 15:26:15 +00008139SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8140 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008141 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008142 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008143
Michael Liaoa7554632012-10-23 17:36:08 +00008144 if (Op.getValueType().isVector())
8145 return lowerUINT_TO_FP_vec(Op, DAG);
8146
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008147 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008148 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8149 // the optimization here.
8150 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008151 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008152
Owen Andersone50ed302009-08-10 22:56:29 +00008153 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008154 EVT DstVT = Op.getValueType();
8155 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008156 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008157 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008158 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008159 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008160 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008161
8162 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008163 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008164 if (SrcVT == MVT::i32) {
8165 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8166 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8167 getPointerTy(), StackSlot, WordOff);
8168 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008169 StackSlot, MachinePointerInfo(),
8170 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008171 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008172 OffsetSlot, MachinePointerInfo(),
8173 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008174 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8175 return Fild;
8176 }
8177
8178 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8179 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008180 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008181 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008182 // For i64 source, we need to add the appropriate power of 2 if the input
8183 // was negative. This is the same as the optimization in
8184 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8185 // we must be careful to do the computation in x87 extended precision, not
8186 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008187 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8188 MachineMemOperand *MMO =
8189 DAG.getMachineFunction()
8190 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8191 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008192
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008193 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8194 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008195 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8196 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008197
8198 APInt FF(32, 0x5F800000ULL);
8199
8200 // Check whether the sign bit is set.
8201 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8202 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8203 ISD::SETLT);
8204
8205 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8206 SDValue FudgePtr = DAG.getConstantPool(
8207 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8208 getPointerTy());
8209
8210 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8211 SDValue Zero = DAG.getIntPtrConstant(0);
8212 SDValue Four = DAG.getIntPtrConstant(4);
8213 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8214 Zero, Four);
8215 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8216
8217 // Load the value out, extending it from f32 to f80.
8218 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008219 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008220 FudgePtr, MachinePointerInfo::getConstantPool(),
8221 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008222 // Extend everything to 80 bits to force it to be done on x87.
8223 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8224 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008225}
8226
Craig Topperb99bafe2013-01-21 06:21:54 +00008227std::pair<SDValue,SDValue>
8228X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8229 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008230 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008231
Owen Andersone50ed302009-08-10 22:56:29 +00008232 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008233
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008234 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008235 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8236 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008237 }
8238
Owen Anderson825b72b2009-08-11 20:47:22 +00008239 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8240 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008241 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008242
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008243 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008244 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008245 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008246 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008247 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008248 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008249 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008250 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008251
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008252 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8253 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008254 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008255 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008256 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008257 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008258
Evan Cheng0db9fe62006-04-25 20:13:52 +00008259 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008260 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8261 Opc = X86ISD::WIN_FTOL;
8262 else
8263 switch (DstTy.getSimpleVT().SimpleTy) {
8264 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8265 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8266 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8267 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8268 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008269
Dan Gohman475871a2008-07-27 21:46:04 +00008270 SDValue Chain = DAG.getEntryNode();
8271 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008272 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008273 // FIXME This causes a redundant load/store if the SSE-class value is already
8274 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008275 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008277 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008278 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008279 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008281 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008282 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008283 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008284
Chris Lattner492a43e2010-09-22 01:28:21 +00008285 MachineMemOperand *MMO =
8286 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8287 MachineMemOperand::MOLoad, MemSize, MemSize);
8288 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8289 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008290 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008291 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008292 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8293 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008294
Chris Lattner07290932010-09-22 01:05:16 +00008295 MachineMemOperand *MMO =
8296 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8297 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008298
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008299 if (Opc != X86ISD::WIN_FTOL) {
8300 // Build the FP_TO_INT*_IN_MEM
8301 SDValue Ops[] = { Chain, Value, StackSlot };
8302 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8303 Ops, 3, DstTy, MMO);
8304 return std::make_pair(FIST, StackSlot);
8305 } else {
8306 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8307 DAG.getVTList(MVT::Other, MVT::Glue),
8308 Chain, Value);
8309 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8310 MVT::i32, ftol.getValue(1));
8311 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8312 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008313 SDValue Ops[] = { eax, edx };
8314 SDValue pair = IsReplace
8315 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8316 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008317 return std::make_pair(pair, SDValue());
8318 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008319}
8320
Nadav Rotem0509db22012-12-28 05:45:24 +00008321static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8322 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008323 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008324 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008325 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008326 DebugLoc dl = Op->getDebugLoc();
8327
8328 // Optimize vectors in AVX mode:
8329 //
8330 // v8i16 -> v8i32
8331 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8332 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8333 // Concat upper and lower parts.
8334 //
8335 // v4i32 -> v4i64
8336 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8337 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8338 // Concat upper and lower parts.
8339 //
8340
8341 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8342 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8343 return SDValue();
8344
8345 if (Subtarget->hasInt256())
8346 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8347
8348 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8349 SDValue Undef = DAG.getUNDEF(InVT);
8350 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8351 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8352 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8353
Craig Toppera080daf2013-01-20 21:50:27 +00008354 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008355 VT.getVectorNumElements()/2);
8356
8357 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8358 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8359
8360 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8361}
8362
8363SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8364 SelectionDAG &DAG) const {
8365 if (Subtarget->hasFp256()) {
8366 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8367 if (Res.getNode())
8368 return Res;
8369 }
8370
8371 return SDValue();
8372}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008373SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8374 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008375 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008376 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008377 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008378 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008379
Nadav Rotem0509db22012-12-28 05:45:24 +00008380 if (Subtarget->hasFp256()) {
8381 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8382 if (Res.getNode())
8383 return Res;
8384 }
8385
Michael Liaoa7554632012-10-23 17:36:08 +00008386 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8387 VT.getVectorNumElements() != SVT.getVectorNumElements())
8388 return SDValue();
8389
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008390 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008391
8392 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008393 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008394 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8395
8396 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8397 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8398 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008399 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8400 DAG.getUNDEF(MVT::v8i16),
8401 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008402
8403 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8404}
8405
Craig Topperd713c0f2013-01-20 21:34:37 +00008406SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008407 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008408 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008409 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008410 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008411
Nadav Rotem3c22a442012-12-27 07:45:10 +00008412 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8413 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8414 if (Subtarget->hasInt256()) {
8415 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8416 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8417 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8418 ShufMask);
8419 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8420 DAG.getIntPtrConstant(0));
8421 }
8422
8423 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8424 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8425 DAG.getIntPtrConstant(0));
8426 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8427 DAG.getIntPtrConstant(2));
8428
8429 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8430 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8431
8432 // The PSHUFD mask:
8433 static const int ShufMask1[] = {0, 2, 0, 0};
8434 SDValue Undef = DAG.getUNDEF(VT);
8435 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8436 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8437
8438 // The MOVLHPS mask:
8439 static const int ShufMask2[] = {0, 1, 4, 5};
8440 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8441 }
8442
8443 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8444 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8445 if (Subtarget->hasInt256()) {
8446 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8447
8448 SmallVector<SDValue,32> pshufbMask;
8449 for (unsigned i = 0; i < 2; ++i) {
8450 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8451 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8452 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8453 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8454 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8455 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8456 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8457 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8458 for (unsigned j = 0; j < 8; ++j)
8459 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8460 }
8461 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8462 &pshufbMask[0], 32);
8463 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8464 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8465
8466 static const int ShufMask[] = {0, 2, -1, -1};
8467 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8468 &ShufMask[0]);
8469 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8470 DAG.getIntPtrConstant(0));
8471 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8472 }
8473
8474 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8475 DAG.getIntPtrConstant(0));
8476
8477 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8478 DAG.getIntPtrConstant(4));
8479
8480 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8481 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8482
8483 // The PSHUFB mask:
8484 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8485 -1, -1, -1, -1, -1, -1, -1, -1};
8486
8487 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8488 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8489 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8490
8491 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8492 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8493
8494 // The MOVLHPS Mask:
8495 static const int ShufMask2[] = {0, 1, 4, 5};
8496 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8497 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8498 }
8499
8500 // Handle truncation of V256 to V128 using shuffles.
8501 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008502 return SDValue();
8503
Nadav Rotem3c22a442012-12-27 07:45:10 +00008504 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8505 "Invalid op");
8506 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008507
8508 unsigned NumElems = VT.getVectorNumElements();
8509 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8510 NumElems * 2);
8511
Michael Liaobedcbd42012-10-16 18:14:11 +00008512 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8513 // Prepare truncation shuffle mask
8514 for (unsigned i = 0; i != NumElems; ++i)
8515 MaskVec[i] = i * 2;
8516 SDValue V = DAG.getVectorShuffle(NVT, DL,
8517 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8518 DAG.getUNDEF(NVT), &MaskVec[0]);
8519 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8520 DAG.getIntPtrConstant(0));
8521}
8522
Dan Gohmand858e902010-04-17 15:26:15 +00008523SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8524 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008525 MVT VT = Op.getValueType().getSimpleVT();
8526 if (VT.isVector()) {
8527 if (VT == MVT::v8i16)
8528 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008529 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8530 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008531 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008532 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008533
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008534 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8535 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008536 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008537 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8538 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008539
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008540 if (StackSlot.getNode())
8541 // Load the result.
8542 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8543 FIST, StackSlot, MachinePointerInfo(),
8544 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008545
8546 // The node is the result.
8547 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008548}
8549
Dan Gohmand858e902010-04-17 15:26:15 +00008550SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8551 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008552 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8553 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008554 SDValue FIST = Vals.first, StackSlot = Vals.second;
8555 assert(FIST.getNode() && "Unexpected failure");
8556
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008557 if (StackSlot.getNode())
8558 // Load the result.
8559 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8560 FIST, StackSlot, MachinePointerInfo(),
8561 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008562
8563 // The node is the result.
8564 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008565}
8566
Craig Topperb84b4232013-01-21 06:13:28 +00008567static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008568 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008569 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008570 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008571 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008572
8573 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8574
8575 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8576 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8577 In, DAG.getUNDEF(SVT)));
8578}
8579
Craig Topper43620672012-09-08 07:31:51 +00008580SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008581 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008582 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008583 MVT VT = Op.getValueType().getSimpleVT();
8584 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008585 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8586 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008587 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008588 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008589 }
Craig Topper43620672012-09-08 07:31:51 +00008590 Constant *C;
8591 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008592 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8593 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008594 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008595 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8596 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008597 C = ConstantVector::getSplat(NumElts, C);
8598 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8599 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008600 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008601 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008602 false, false, false, Alignment);
8603 if (VT.isVector()) {
8604 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8605 return DAG.getNode(ISD::BITCAST, dl, VT,
8606 DAG.getNode(ISD::AND, dl, ANDVT,
8607 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8608 Op.getOperand(0)),
8609 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8610 }
Dale Johannesenace16102009-02-03 19:33:06 +00008611 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008612}
8613
Dan Gohmand858e902010-04-17 15:26:15 +00008614SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008615 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008616 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008617 MVT VT = Op.getValueType().getSimpleVT();
8618 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008619 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8620 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008621 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008622 NumElts = VT.getVectorNumElements();
8623 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008624 Constant *C;
8625 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008626 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8627 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008628 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008629 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8630 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008631 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008632 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8633 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008634 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008635 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008636 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008637 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008638 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008639 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008640 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008641 DAG.getNode(ISD::BITCAST, dl, XORVT,
8642 Op.getOperand(0)),
8643 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008644 }
Craig Topper69947b92012-04-23 06:57:04 +00008645
8646 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008647}
8648
Dan Gohmand858e902010-04-17 15:26:15 +00008649SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008650 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008651 SDValue Op0 = Op.getOperand(0);
8652 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008653 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008654 MVT VT = Op.getValueType().getSimpleVT();
8655 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008656
8657 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008658 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008659 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008660 SrcVT = VT;
8661 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008662 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008663 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008664 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008665 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008666 }
8667
8668 // At this point the operands and the result should have the same
8669 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008670
Evan Cheng68c47cb2007-01-05 07:55:56 +00008671 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008672 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008674 const fltSemantics &Sem = APFloat::IEEEdouble;
8675 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8676 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008677 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008678 const fltSemantics &Sem = APFloat::IEEEsingle;
8679 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8680 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8681 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8682 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008683 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008684 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008685 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008686 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008687 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008688 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008689 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008690
8691 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008692 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008693 // Op0 is MVT::f32, Op1 is MVT::f64.
8694 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8695 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8696 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008697 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008698 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008699 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008700 }
8701
Evan Cheng73d6cf12007-01-05 21:37:56 +00008702 // Clear first operand sign bit.
8703 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008705 const fltSemantics &Sem = APFloat::IEEEdouble;
8706 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8707 APInt(64, ~(1ULL << 63)))));
8708 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008709 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008710 const fltSemantics &Sem = APFloat::IEEEsingle;
8711 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8712 APInt(32, ~(1U << 31)))));
8713 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8714 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8715 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008716 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008717 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008718 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008719 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008720 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008721 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008722 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008723
8724 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008725 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008726}
8727
Craig Topper55b24052012-09-11 06:15:32 +00008728static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008729 SDValue N0 = Op.getOperand(0);
8730 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008731 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008732
8733 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8734 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8735 DAG.getConstant(1, VT));
8736 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8737}
8738
Michael Liaof966e4e2012-09-13 20:24:54 +00008739// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8740//
Craig Topperb99bafe2013-01-21 06:21:54 +00008741SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8742 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008743 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8744
8745 if (!Subtarget->hasSSE41())
8746 return SDValue();
8747
8748 if (!Op->hasOneUse())
8749 return SDValue();
8750
8751 SDNode *N = Op.getNode();
8752 DebugLoc DL = N->getDebugLoc();
8753
8754 SmallVector<SDValue, 8> Opnds;
8755 DenseMap<SDValue, unsigned> VecInMap;
8756 EVT VT = MVT::Other;
8757
8758 // Recognize a special case where a vector is casted into wide integer to
8759 // test all 0s.
8760 Opnds.push_back(N->getOperand(0));
8761 Opnds.push_back(N->getOperand(1));
8762
8763 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8764 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8765 // BFS traverse all OR'd operands.
8766 if (I->getOpcode() == ISD::OR) {
8767 Opnds.push_back(I->getOperand(0));
8768 Opnds.push_back(I->getOperand(1));
8769 // Re-evaluate the number of nodes to be traversed.
8770 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8771 continue;
8772 }
8773
8774 // Quit if a non-EXTRACT_VECTOR_ELT
8775 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8776 return SDValue();
8777
8778 // Quit if without a constant index.
8779 SDValue Idx = I->getOperand(1);
8780 if (!isa<ConstantSDNode>(Idx))
8781 return SDValue();
8782
8783 SDValue ExtractedFromVec = I->getOperand(0);
8784 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8785 if (M == VecInMap.end()) {
8786 VT = ExtractedFromVec.getValueType();
8787 // Quit if not 128/256-bit vector.
8788 if (!VT.is128BitVector() && !VT.is256BitVector())
8789 return SDValue();
8790 // Quit if not the same type.
8791 if (VecInMap.begin() != VecInMap.end() &&
8792 VT != VecInMap.begin()->first.getValueType())
8793 return SDValue();
8794 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8795 }
8796 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8797 }
8798
8799 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008800 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008801
8802 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8803 SmallVector<SDValue, 8> VecIns;
8804
8805 for (DenseMap<SDValue, unsigned>::const_iterator
8806 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8807 // Quit if not all elements are used.
8808 if (I->second != FullMask)
8809 return SDValue();
8810 VecIns.push_back(I->first);
8811 }
8812
8813 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8814
8815 // Cast all vectors into TestVT for PTEST.
8816 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8817 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8818
8819 // If more than one full vectors are evaluated, OR them first before PTEST.
8820 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8821 // Each iteration will OR 2 nodes and append the result until there is only
8822 // 1 node left, i.e. the final OR'd value of all vectors.
8823 SDValue LHS = VecIns[Slot];
8824 SDValue RHS = VecIns[Slot + 1];
8825 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8826 }
8827
8828 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8829 VecIns.back(), VecIns.back());
8830}
8831
Dan Gohman076aee32009-03-04 19:44:21 +00008832/// Emit nodes that will be selected as "test Op0,Op0", or something
8833/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008834SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008835 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008836 DebugLoc dl = Op.getDebugLoc();
8837
Dan Gohman31125812009-03-07 01:58:32 +00008838 // CF and OF aren't always set the way we want. Determine which
8839 // of these we need.
8840 bool NeedCF = false;
8841 bool NeedOF = false;
8842 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008843 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008844 case X86::COND_A: case X86::COND_AE:
8845 case X86::COND_B: case X86::COND_BE:
8846 NeedCF = true;
8847 break;
8848 case X86::COND_G: case X86::COND_GE:
8849 case X86::COND_L: case X86::COND_LE:
8850 case X86::COND_O: case X86::COND_NO:
8851 NeedOF = true;
8852 break;
Dan Gohman31125812009-03-07 01:58:32 +00008853 }
8854
Dan Gohman076aee32009-03-04 19:44:21 +00008855 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008856 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8857 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008858 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8859 // Emit a CMP with 0, which is the TEST pattern.
8860 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8861 DAG.getConstant(0, Op.getValueType()));
8862
8863 unsigned Opcode = 0;
8864 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008865
8866 // Truncate operations may prevent the merge of the SETCC instruction
8867 // and the arithmetic intruction before it. Attempt to truncate the operands
8868 // of the arithmetic instruction and use a reduced bit-width instruction.
8869 bool NeedTruncation = false;
8870 SDValue ArithOp = Op;
8871 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8872 SDValue Arith = Op->getOperand(0);
8873 // Both the trunc and the arithmetic op need to have one user each.
8874 if (Arith->hasOneUse())
8875 switch (Arith.getOpcode()) {
8876 default: break;
8877 case ISD::ADD:
8878 case ISD::SUB:
8879 case ISD::AND:
8880 case ISD::OR:
8881 case ISD::XOR: {
8882 NeedTruncation = true;
8883 ArithOp = Arith;
8884 }
8885 }
8886 }
8887
8888 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8889 // which may be the result of a CAST. We use the variable 'Op', which is the
8890 // non-casted variable when we check for possible users.
8891 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008892 case ISD::ADD:
8893 // Due to an isel shortcoming, be conservative if this add is likely to be
8894 // selected as part of a load-modify-store instruction. When the root node
8895 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8896 // uses of other nodes in the match, such as the ADD in this case. This
8897 // leads to the ADD being left around and reselected, with the result being
8898 // two adds in the output. Alas, even if none our users are stores, that
8899 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8900 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8901 // climbing the DAG back to the root, and it doesn't seem to be worth the
8902 // effort.
8903 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008904 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8905 if (UI->getOpcode() != ISD::CopyToReg &&
8906 UI->getOpcode() != ISD::SETCC &&
8907 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008908 goto default_case;
8909
8910 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008911 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008912 // An add of one will be selected as an INC.
8913 if (C->getAPIntValue() == 1) {
8914 Opcode = X86ISD::INC;
8915 NumOperands = 1;
8916 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008917 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008918
8919 // An add of negative one (subtract of one) will be selected as a DEC.
8920 if (C->getAPIntValue().isAllOnesValue()) {
8921 Opcode = X86ISD::DEC;
8922 NumOperands = 1;
8923 break;
8924 }
Dan Gohman076aee32009-03-04 19:44:21 +00008925 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008926
8927 // Otherwise use a regular EFLAGS-setting add.
8928 Opcode = X86ISD::ADD;
8929 NumOperands = 2;
8930 break;
8931 case ISD::AND: {
8932 // If the primary and result isn't used, don't bother using X86ISD::AND,
8933 // because a TEST instruction will be better.
8934 bool NonFlagUse = false;
8935 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8936 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8937 SDNode *User = *UI;
8938 unsigned UOpNo = UI.getOperandNo();
8939 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8940 // Look pass truncate.
8941 UOpNo = User->use_begin().getOperandNo();
8942 User = *User->use_begin();
8943 }
8944
8945 if (User->getOpcode() != ISD::BRCOND &&
8946 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008947 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008948 NonFlagUse = true;
8949 break;
8950 }
Dan Gohman076aee32009-03-04 19:44:21 +00008951 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008952
8953 if (!NonFlagUse)
8954 break;
8955 }
8956 // FALL THROUGH
8957 case ISD::SUB:
8958 case ISD::OR:
8959 case ISD::XOR:
8960 // Due to the ISEL shortcoming noted above, be conservative if this op is
8961 // likely to be selected as part of a load-modify-store instruction.
8962 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8963 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8964 if (UI->getOpcode() == ISD::STORE)
8965 goto default_case;
8966
8967 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008968 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008969 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008970 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008971 case ISD::XOR: Opcode = X86ISD::XOR; break;
8972 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008973 case ISD::OR: {
8974 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8975 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8976 if (EFLAGS.getNode())
8977 return EFLAGS;
8978 }
8979 Opcode = X86ISD::OR;
8980 break;
8981 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008982 }
8983
8984 NumOperands = 2;
8985 break;
8986 case X86ISD::ADD:
8987 case X86ISD::SUB:
8988 case X86ISD::INC:
8989 case X86ISD::DEC:
8990 case X86ISD::OR:
8991 case X86ISD::XOR:
8992 case X86ISD::AND:
8993 return SDValue(Op.getNode(), 1);
8994 default:
8995 default_case:
8996 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008997 }
8998
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008999 // If we found that truncation is beneficial, perform the truncation and
9000 // update 'Op'.
9001 if (NeedTruncation) {
9002 EVT VT = Op.getValueType();
9003 SDValue WideVal = Op->getOperand(0);
9004 EVT WideVT = WideVal.getValueType();
9005 unsigned ConvertedOp = 0;
9006 // Use a target machine opcode to prevent further DAGCombine
9007 // optimizations that may separate the arithmetic operations
9008 // from the setcc node.
9009 switch (WideVal.getOpcode()) {
9010 default: break;
9011 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9012 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9013 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9014 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9015 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9016 }
9017
9018 if (ConvertedOp) {
9019 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9020 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9021 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9022 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9023 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9024 }
9025 }
9026 }
9027
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009028 if (Opcode == 0)
9029 // Emit a CMP with 0, which is the TEST pattern.
9030 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9031 DAG.getConstant(0, Op.getValueType()));
9032
9033 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9034 SmallVector<SDValue, 4> Ops;
9035 for (unsigned i = 0; i != NumOperands; ++i)
9036 Ops.push_back(Op.getOperand(i));
9037
9038 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9039 DAG.ReplaceAllUsesWith(Op, New);
9040 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009041}
9042
9043/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9044/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009045SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009046 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009047 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9048 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009049 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009050
9051 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009052 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9053 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9054 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9055 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9056 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9057 Op0, Op1);
9058 return SDValue(Sub.getNode(), 1);
9059 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009060 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009061}
9062
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009063/// Convert a comparison if required by the subtarget.
9064SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9065 SelectionDAG &DAG) const {
9066 // If the subtarget does not support the FUCOMI instruction, floating-point
9067 // comparisons have to be converted.
9068 if (Subtarget->hasCMov() ||
9069 Cmp.getOpcode() != X86ISD::CMP ||
9070 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9071 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9072 return Cmp;
9073
9074 // The instruction selector will select an FUCOM instruction instead of
9075 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9076 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9077 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9078 DebugLoc dl = Cmp.getDebugLoc();
9079 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9080 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9081 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9082 DAG.getConstant(8, MVT::i8));
9083 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9084 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9085}
9086
Evan Cheng4e544802012-12-05 00:10:38 +00009087static bool isAllOnes(SDValue V) {
9088 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9089 return C && C->isAllOnesValue();
9090}
9091
Evan Chengd40d03e2010-01-06 19:38:29 +00009092/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9093/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009094SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9095 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009096 SDValue Op0 = And.getOperand(0);
9097 SDValue Op1 = And.getOperand(1);
9098 if (Op0.getOpcode() == ISD::TRUNCATE)
9099 Op0 = Op0.getOperand(0);
9100 if (Op1.getOpcode() == ISD::TRUNCATE)
9101 Op1 = Op1.getOperand(0);
9102
Evan Chengd40d03e2010-01-06 19:38:29 +00009103 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009104 if (Op1.getOpcode() == ISD::SHL)
9105 std::swap(Op0, Op1);
9106 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009107 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9108 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009109 // If we looked past a truncate, check that it's only truncating away
9110 // known zeros.
9111 unsigned BitWidth = Op0.getValueSizeInBits();
9112 unsigned AndBitWidth = And.getValueSizeInBits();
9113 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009114 APInt Zeros, Ones;
9115 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009116 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9117 return SDValue();
9118 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009119 LHS = Op1;
9120 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009121 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009122 } else if (Op1.getOpcode() == ISD::Constant) {
9123 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009124 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009125 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009126
9127 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009128 LHS = AndLHS.getOperand(0);
9129 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009130 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009131
9132 // Use BT if the immediate can't be encoded in a TEST instruction.
9133 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9134 LHS = AndLHS;
9135 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9136 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009137 }
Evan Cheng0488db92007-09-25 01:57:46 +00009138
Evan Chengd40d03e2010-01-06 19:38:29 +00009139 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009140 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9141 // the condition code later.
9142 bool Invert = false;
9143 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9144 Invert = true;
9145 LHS = LHS.getOperand(0);
9146 }
9147
Evan Chenge5b51ac2010-04-17 06:13:15 +00009148 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009149 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009150 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009151 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009152 // Also promote i16 to i32 for performance / code size reason.
9153 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009154 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009155 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009156
Evan Chengd40d03e2010-01-06 19:38:29 +00009157 // If the operand types disagree, extend the shift amount to match. Since
9158 // BT ignores high bits (like shifts) we can use anyextend.
9159 if (LHS.getValueType() != RHS.getValueType())
9160 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009161
Evan Chengd40d03e2010-01-06 19:38:29 +00009162 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009163 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9164 // Flip the condition if the LHS was a not instruction
9165 if (Invert)
9166 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009167 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9168 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009169 }
9170
Evan Cheng54de3ea2010-01-05 06:52:31 +00009171 return SDValue();
9172}
9173
Craig Topper89af15e2011-09-18 08:03:58 +00009174// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009175// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009176static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009177 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009178
Craig Topper7a9a28b2012-08-12 02:23:29 +00009179 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009180 "Unsupported value type for operation");
9181
Craig Topper66ddd152012-04-27 22:54:43 +00009182 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009183 DebugLoc dl = Op.getDebugLoc();
9184 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009185
9186 // Extract the LHS vectors
9187 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009188 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9189 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009190
9191 // Extract the RHS vectors
9192 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009193 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9194 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009195
9196 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009197 MVT EltVT = VT.getVectorElementType();
9198 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009199 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9200 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9201 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9202}
9203
Craig Topper26827f32013-01-20 09:02:22 +00009204static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9205 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009206 SDValue Cond;
9207 SDValue Op0 = Op.getOperand(0);
9208 SDValue Op1 = Op.getOperand(1);
9209 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009210 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009211 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009212 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009213 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009214
9215 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009216#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009217 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009218 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9219#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009220
Craig Topper523908d2012-08-13 02:34:03 +00009221 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009222 bool Swap = false;
9223
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009224 // SSE Condition code mapping:
9225 // 0 - EQ
9226 // 1 - LT
9227 // 2 - LE
9228 // 3 - UNORD
9229 // 4 - NEQ
9230 // 5 - NLT
9231 // 6 - NLE
9232 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009233 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009234 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009235 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009236 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009237 case ISD::SETOGT:
9238 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009239 case ISD::SETLT:
9240 case ISD::SETOLT: SSECC = 1; break;
9241 case ISD::SETOGE:
9242 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009243 case ISD::SETLE:
9244 case ISD::SETOLE: SSECC = 2; break;
9245 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009246 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009247 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009248 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009249 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009250 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009251 case ISD::SETUGT: SSECC = 6; break;
9252 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009253 case ISD::SETUEQ:
9254 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009255 }
9256 if (Swap)
9257 std::swap(Op0, Op1);
9258
Nate Begemanfb8ead02008-07-25 19:05:58 +00009259 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009260 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009261 unsigned CC0, CC1;
9262 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009263 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009264 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9265 } else {
9266 assert(SetCCOpcode == ISD::SETONE);
9267 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009268 }
Craig Topper523908d2012-08-13 02:34:03 +00009269
9270 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9271 DAG.getConstant(CC0, MVT::i8));
9272 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9273 DAG.getConstant(CC1, MVT::i8));
9274 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009275 }
9276 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009277 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9278 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009279 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009280
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009281 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009282 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009283 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009284
Nate Begeman30a0de92008-07-17 16:51:19 +00009285 // We are handling one of the integer comparisons here. Since SSE only has
9286 // GT and EQ comparisons for integer, swapping operands and multiple
9287 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009288 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009289 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009290
Nate Begeman30a0de92008-07-17 16:51:19 +00009291 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009292 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009293 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009294 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009295 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009296 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009297 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009298 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009299 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009300 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009301 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009302 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009303 }
9304 if (Swap)
9305 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009306
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009307 // Check that the operation in question is available (most are plain SSE2,
9308 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009309 if (VT == MVT::v2i64) {
9310 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9311 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009312 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9313 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009314 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009315 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9316
9317 // First cast everything to the right type,
9318 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9319 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9320
9321 // Do the compare.
9322 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9323
9324 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009325 const int Mask[] = { 1, 0, 3, 2 };
9326 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9327 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009328
9329 if (Invert)
9330 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9331
9332 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9333 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009334 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009335
Nate Begeman30a0de92008-07-17 16:51:19 +00009336 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9337 // bits of the inputs before performing those operations.
9338 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009339 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009340 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9341 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009342 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009343 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9344 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009345 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9346 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009347 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009348
Dale Johannesenace16102009-02-03 19:33:06 +00009349 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009350
9351 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009352 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009353 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009354
Nate Begeman30a0de92008-07-17 16:51:19 +00009355 return Result;
9356}
Evan Cheng0488db92007-09-25 01:57:46 +00009357
Craig Topper26827f32013-01-20 09:02:22 +00009358SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9359
9360 MVT VT = Op.getValueType().getSimpleVT();
9361
9362 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9363
9364 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9365 SDValue Op0 = Op.getOperand(0);
9366 SDValue Op1 = Op.getOperand(1);
9367 DebugLoc dl = Op.getDebugLoc();
9368 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9369
9370 // Optimize to BT if possible.
9371 // Lower (X & (1 << N)) == 0 to BT(X, N).
9372 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9373 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9374 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9375 Op1.getOpcode() == ISD::Constant &&
9376 cast<ConstantSDNode>(Op1)->isNullValue() &&
9377 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9378 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9379 if (NewSetCC.getNode())
9380 return NewSetCC;
9381 }
9382
9383 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9384 // these.
9385 if (Op1.getOpcode() == ISD::Constant &&
9386 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9387 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9388 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9389
9390 // If the input is a setcc, then reuse the input setcc or use a new one with
9391 // the inverted condition.
9392 if (Op0.getOpcode() == X86ISD::SETCC) {
9393 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9394 bool Invert = (CC == ISD::SETNE) ^
9395 cast<ConstantSDNode>(Op1)->isNullValue();
9396 if (!Invert) return Op0;
9397
9398 CCode = X86::GetOppositeBranchCondition(CCode);
9399 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9400 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9401 }
9402 }
9403
9404 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9405 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9406 if (X86CC == X86::COND_INVALID)
9407 return SDValue();
9408
9409 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9410 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9411 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9412 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9413}
9414
Evan Cheng370e5342008-12-03 08:38:43 +00009415// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009416static bool isX86LogicalCmp(SDValue Op) {
9417 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009418 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9419 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009420 return true;
9421 if (Op.getResNo() == 1 &&
9422 (Opc == X86ISD::ADD ||
9423 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009424 Opc == X86ISD::ADC ||
9425 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009426 Opc == X86ISD::SMUL ||
9427 Opc == X86ISD::UMUL ||
9428 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009429 Opc == X86ISD::DEC ||
9430 Opc == X86ISD::OR ||
9431 Opc == X86ISD::XOR ||
9432 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009433 return true;
9434
Chris Lattner9637d5b2010-12-05 07:49:54 +00009435 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9436 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009437
Dan Gohman076aee32009-03-04 19:44:21 +00009438 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009439}
9440
Chris Lattnera2b56002010-12-05 01:23:24 +00009441static bool isZero(SDValue V) {
9442 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9443 return C && C->isNullValue();
9444}
9445
Evan Chengb64dd5f2012-08-07 22:21:00 +00009446static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9447 if (V.getOpcode() != ISD::TRUNCATE)
9448 return false;
9449
9450 SDValue VOp0 = V.getOperand(0);
9451 unsigned InBits = VOp0.getValueSizeInBits();
9452 unsigned Bits = V.getValueSizeInBits();
9453 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9454}
9455
Dan Gohmand858e902010-04-17 15:26:15 +00009456SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009457 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009458 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009459 SDValue Op1 = Op.getOperand(1);
9460 SDValue Op2 = Op.getOperand(2);
9461 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009462 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009463
Dan Gohman1a492952009-10-20 16:22:37 +00009464 if (Cond.getOpcode() == ISD::SETCC) {
9465 SDValue NewCond = LowerSETCC(Cond, DAG);
9466 if (NewCond.getNode())
9467 Cond = NewCond;
9468 }
Evan Cheng734503b2006-09-11 02:19:56 +00009469
Chris Lattnera2b56002010-12-05 01:23:24 +00009470 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009471 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009472 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009473 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009474 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009475 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9476 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009477 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009478
Chris Lattnera2b56002010-12-05 01:23:24 +00009479 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009480
9481 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009482 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9483 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009484
9485 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009486 // Apply further optimizations for special cases
9487 // (select (x != 0), -1, 0) -> neg & sbb
9488 // (select (x == 0), 0, -1) -> neg & sbb
9489 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009490 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009491 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9492 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009493 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9494 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009495 CmpOp0);
9496 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9497 DAG.getConstant(X86::COND_B, MVT::i8),
9498 SDValue(Neg.getNode(), 1));
9499 return Res;
9500 }
9501
Chris Lattnera2b56002010-12-05 01:23:24 +00009502 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9503 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009504 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009505
Chris Lattner96908b12010-12-05 02:00:51 +00009506 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009507 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9508 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009509
Chris Lattner96908b12010-12-05 02:00:51 +00009510 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9511 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009512
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009513 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009514 if (N2C == 0 || !N2C->isNullValue())
9515 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9516 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009517 }
9518 }
9519
Chris Lattnera2b56002010-12-05 01:23:24 +00009520 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009521 if (Cond.getOpcode() == ISD::AND &&
9522 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9523 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009524 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009525 Cond = Cond.getOperand(0);
9526 }
9527
Evan Cheng3f41d662007-10-08 22:16:29 +00009528 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9529 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009530 unsigned CondOpcode = Cond.getOpcode();
9531 if (CondOpcode == X86ISD::SETCC ||
9532 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009533 CC = Cond.getOperand(0);
9534
Dan Gohman475871a2008-07-27 21:46:04 +00009535 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009536 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009537 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009538
Evan Cheng3f41d662007-10-08 22:16:29 +00009539 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009540 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009541 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009542 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009543
Chris Lattnerd1980a52009-03-12 06:52:53 +00009544 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9545 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009546 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009547 addTest = false;
9548 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009549 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9550 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9551 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9552 Cond.getOperand(0).getValueType() != MVT::i8)) {
9553 SDValue LHS = Cond.getOperand(0);
9554 SDValue RHS = Cond.getOperand(1);
9555 unsigned X86Opcode;
9556 unsigned X86Cond;
9557 SDVTList VTs;
9558 switch (CondOpcode) {
9559 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9560 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9561 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9562 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9563 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9564 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9565 default: llvm_unreachable("unexpected overflowing operator");
9566 }
9567 if (CondOpcode == ISD::UMULO)
9568 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9569 MVT::i32);
9570 else
9571 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9572
9573 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9574
9575 if (CondOpcode == ISD::UMULO)
9576 Cond = X86Op.getValue(2);
9577 else
9578 Cond = X86Op.getValue(1);
9579
9580 CC = DAG.getConstant(X86Cond, MVT::i8);
9581 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009582 }
9583
9584 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009585 // Look pass the truncate if the high bits are known zero.
9586 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9587 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009588
9589 // We know the result of AND is compared against zero. Try to match
9590 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009591 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009592 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009593 if (NewSetCC.getNode()) {
9594 CC = NewSetCC.getOperand(0);
9595 Cond = NewSetCC.getOperand(1);
9596 addTest = false;
9597 }
9598 }
9599 }
9600
9601 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009602 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009603 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009604 }
9605
Benjamin Kramere915ff32010-12-22 23:09:28 +00009606 // a < b ? -1 : 0 -> RES = ~setcc_carry
9607 // a < b ? 0 : -1 -> RES = setcc_carry
9608 // a >= b ? -1 : 0 -> RES = setcc_carry
9609 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009610 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009611 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009612 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9613
9614 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9615 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9616 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9617 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9618 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9619 return DAG.getNOT(DL, Res, Res.getValueType());
9620 return Res;
9621 }
9622 }
9623
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009624 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9625 // widen the cmov and push the truncate through. This avoids introducing a new
9626 // branch during isel and doesn't add any extensions.
9627 if (Op.getValueType() == MVT::i8 &&
9628 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9629 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9630 if (T1.getValueType() == T2.getValueType() &&
9631 // Blacklist CopyFromReg to avoid partial register stalls.
9632 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9633 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009634 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009635 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9636 }
9637 }
9638
Evan Cheng0488db92007-09-25 01:57:46 +00009639 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9640 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009641 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009642 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009643 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009644}
9645
Nadav Rotem1a330af2012-12-27 22:47:16 +00009646SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9647 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009648 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009649 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009650 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009651 DebugLoc dl = Op->getDebugLoc();
9652
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009653 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9654 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9655 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009656
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009657 if (Subtarget->hasInt256())
9658 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009659
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009660 // Optimize vectors in AVX mode
9661 // Sign extend v8i16 to v8i32 and
9662 // v4i32 to v4i64
9663 //
9664 // Divide input vector into two parts
9665 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9666 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9667 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009668
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009669 unsigned NumElems = InVT.getVectorNumElements();
9670 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009671
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009672 SmallVector<int,8> ShufMask1(NumElems, -1);
9673 for (unsigned i = 0; i != NumElems/2; ++i)
9674 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009675
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009676 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009677
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009678 SmallVector<int,8> ShufMask2(NumElems, -1);
9679 for (unsigned i = 0; i != NumElems/2; ++i)
9680 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009681
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009682 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009683
Craig Toppera080daf2013-01-20 21:50:27 +00009684 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009685 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009686
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009687 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9688 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009689
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009690 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009691}
9692
Evan Cheng370e5342008-12-03 08:38:43 +00009693// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9694// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9695// from the AND / OR.
9696static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9697 Opc = Op.getOpcode();
9698 if (Opc != ISD::OR && Opc != ISD::AND)
9699 return false;
9700 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9701 Op.getOperand(0).hasOneUse() &&
9702 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9703 Op.getOperand(1).hasOneUse());
9704}
9705
Evan Cheng961d6d42009-02-02 08:19:07 +00009706// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9707// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009708static bool isXor1OfSetCC(SDValue Op) {
9709 if (Op.getOpcode() != ISD::XOR)
9710 return false;
9711 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9712 if (N1C && N1C->getAPIntValue() == 1) {
9713 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9714 Op.getOperand(0).hasOneUse();
9715 }
9716 return false;
9717}
9718
Dan Gohmand858e902010-04-17 15:26:15 +00009719SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009720 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009721 SDValue Chain = Op.getOperand(0);
9722 SDValue Cond = Op.getOperand(1);
9723 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009724 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009725 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009726 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009727
Dan Gohman1a492952009-10-20 16:22:37 +00009728 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009729 // Check for setcc([su]{add,sub,mul}o == 0).
9730 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9731 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9732 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9733 Cond.getOperand(0).getResNo() == 1 &&
9734 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9735 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9736 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9737 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9738 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9739 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9740 Inverted = true;
9741 Cond = Cond.getOperand(0);
9742 } else {
9743 SDValue NewCond = LowerSETCC(Cond, DAG);
9744 if (NewCond.getNode())
9745 Cond = NewCond;
9746 }
Dan Gohman1a492952009-10-20 16:22:37 +00009747 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009748#if 0
9749 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009750 else if (Cond.getOpcode() == X86ISD::ADD ||
9751 Cond.getOpcode() == X86ISD::SUB ||
9752 Cond.getOpcode() == X86ISD::SMUL ||
9753 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009754 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009755#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009756
Evan Chengad9c0a32009-12-15 00:53:42 +00009757 // Look pass (and (setcc_carry (cmp ...)), 1).
9758 if (Cond.getOpcode() == ISD::AND &&
9759 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9760 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009761 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009762 Cond = Cond.getOperand(0);
9763 }
9764
Evan Cheng3f41d662007-10-08 22:16:29 +00009765 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9766 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009767 unsigned CondOpcode = Cond.getOpcode();
9768 if (CondOpcode == X86ISD::SETCC ||
9769 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009770 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009771
Dan Gohman475871a2008-07-27 21:46:04 +00009772 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009773 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009774 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009775 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009776 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009777 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009778 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009779 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009780 default: break;
9781 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009782 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009783 // These can only come from an arithmetic instruction with overflow,
9784 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009785 Cond = Cond.getNode()->getOperand(1);
9786 addTest = false;
9787 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009788 }
Evan Cheng0488db92007-09-25 01:57:46 +00009789 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009790 }
9791 CondOpcode = Cond.getOpcode();
9792 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9793 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9794 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9795 Cond.getOperand(0).getValueType() != MVT::i8)) {
9796 SDValue LHS = Cond.getOperand(0);
9797 SDValue RHS = Cond.getOperand(1);
9798 unsigned X86Opcode;
9799 unsigned X86Cond;
9800 SDVTList VTs;
9801 switch (CondOpcode) {
9802 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9803 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9804 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9805 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9806 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9807 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9808 default: llvm_unreachable("unexpected overflowing operator");
9809 }
9810 if (Inverted)
9811 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9812 if (CondOpcode == ISD::UMULO)
9813 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9814 MVT::i32);
9815 else
9816 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9817
9818 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9819
9820 if (CondOpcode == ISD::UMULO)
9821 Cond = X86Op.getValue(2);
9822 else
9823 Cond = X86Op.getValue(1);
9824
9825 CC = DAG.getConstant(X86Cond, MVT::i8);
9826 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009827 } else {
9828 unsigned CondOpc;
9829 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9830 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009831 if (CondOpc == ISD::OR) {
9832 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9833 // two branches instead of an explicit OR instruction with a
9834 // separate test.
9835 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009836 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009837 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009838 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009839 Chain, Dest, CC, Cmp);
9840 CC = Cond.getOperand(1).getOperand(0);
9841 Cond = Cmp;
9842 addTest = false;
9843 }
9844 } else { // ISD::AND
9845 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9846 // two branches instead of an explicit AND instruction with a
9847 // separate test. However, we only do this if this block doesn't
9848 // have a fall-through edge, because this requires an explicit
9849 // jmp when the condition is false.
9850 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009851 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009852 Op.getNode()->hasOneUse()) {
9853 X86::CondCode CCode =
9854 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9855 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009856 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009857 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009858 // Look for an unconditional branch following this conditional branch.
9859 // We need this because we need to reverse the successors in order
9860 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009861 if (User->getOpcode() == ISD::BR) {
9862 SDValue FalseBB = User->getOperand(1);
9863 SDNode *NewBR =
9864 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009865 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009866 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009867 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009868
Dale Johannesene4d209d2009-02-03 20:21:25 +00009869 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009870 Chain, Dest, CC, Cmp);
9871 X86::CondCode CCode =
9872 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9873 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009874 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009875 Cond = Cmp;
9876 addTest = false;
9877 }
9878 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009879 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009880 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9881 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9882 // It should be transformed during dag combiner except when the condition
9883 // is set by a arithmetics with overflow node.
9884 X86::CondCode CCode =
9885 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9886 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009887 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009888 Cond = Cond.getOperand(0).getOperand(1);
9889 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009890 } else if (Cond.getOpcode() == ISD::SETCC &&
9891 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9892 // For FCMP_OEQ, we can emit
9893 // two branches instead of an explicit AND instruction with a
9894 // separate test. However, we only do this if this block doesn't
9895 // have a fall-through edge, because this requires an explicit
9896 // jmp when the condition is false.
9897 if (Op.getNode()->hasOneUse()) {
9898 SDNode *User = *Op.getNode()->use_begin();
9899 // Look for an unconditional branch following this conditional branch.
9900 // We need this because we need to reverse the successors in order
9901 // to implement FCMP_OEQ.
9902 if (User->getOpcode() == ISD::BR) {
9903 SDValue FalseBB = User->getOperand(1);
9904 SDNode *NewBR =
9905 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9906 assert(NewBR == User);
9907 (void)NewBR;
9908 Dest = FalseBB;
9909
9910 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9911 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009912 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009913 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9914 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9915 Chain, Dest, CC, Cmp);
9916 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9917 Cond = Cmp;
9918 addTest = false;
9919 }
9920 }
9921 } else if (Cond.getOpcode() == ISD::SETCC &&
9922 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9923 // For FCMP_UNE, we can emit
9924 // two branches instead of an explicit AND instruction with a
9925 // separate test. However, we only do this if this block doesn't
9926 // have a fall-through edge, because this requires an explicit
9927 // jmp when the condition is false.
9928 if (Op.getNode()->hasOneUse()) {
9929 SDNode *User = *Op.getNode()->use_begin();
9930 // Look for an unconditional branch following this conditional branch.
9931 // We need this because we need to reverse the successors in order
9932 // to implement FCMP_UNE.
9933 if (User->getOpcode() == ISD::BR) {
9934 SDValue FalseBB = User->getOperand(1);
9935 SDNode *NewBR =
9936 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9937 assert(NewBR == User);
9938 (void)NewBR;
9939
9940 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9941 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009942 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009943 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9944 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9945 Chain, Dest, CC, Cmp);
9946 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9947 Cond = Cmp;
9948 addTest = false;
9949 Dest = FalseBB;
9950 }
9951 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009952 }
Evan Cheng0488db92007-09-25 01:57:46 +00009953 }
9954
9955 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009956 // Look pass the truncate if the high bits are known zero.
9957 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9958 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009959
9960 // We know the result of AND is compared against zero. Try to match
9961 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009962 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009963 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9964 if (NewSetCC.getNode()) {
9965 CC = NewSetCC.getOperand(0);
9966 Cond = NewSetCC.getOperand(1);
9967 addTest = false;
9968 }
9969 }
9970 }
9971
9972 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009973 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009974 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009975 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009976 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009977 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009978 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009979}
9980
Anton Korobeynikove060b532007-04-17 19:34:00 +00009981// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9982// Calls to _alloca is needed to probe the stack when allocating more than 4k
9983// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9984// that the guard pages used by the OS virtual memory manager are allocated in
9985// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009986SDValue
9987X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009988 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009989 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009990 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009991 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009992 "are being used");
9993 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009994 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009995
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009996 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009997 SDValue Chain = Op.getOperand(0);
9998 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009999 // FIXME: Ensure alignment here
10000
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010001 bool Is64Bit = Subtarget->is64Bit();
10002 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010003
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010004 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010005 MachineFunction &MF = DAG.getMachineFunction();
10006 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010007
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010008 if (Is64Bit) {
10009 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010010 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010011 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010012
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010013 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010014 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010015 if (I->hasNestAttr())
10016 report_fatal_error("Cannot use segmented stacks with functions that "
10017 "have nested arguments.");
10018 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010019
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010020 const TargetRegisterClass *AddrRegClass =
10021 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10022 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10023 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10024 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10025 DAG.getRegister(Vreg, SPTy));
10026 SDValue Ops1[2] = { Value, Chain };
10027 return DAG.getMergeValues(Ops1, 2, dl);
10028 } else {
10029 SDValue Flag;
10030 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010031
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010032 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10033 Flag = Chain.getValue(1);
10034 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010035
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010036 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10037 Flag = Chain.getValue(1);
10038
Michael Liaoc5c970e2012-10-31 04:14:09 +000010039 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10040 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010041
10042 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10043 return DAG.getMergeValues(Ops1, 2, dl);
10044 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010045}
10046
Dan Gohmand858e902010-04-17 15:26:15 +000010047SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010048 MachineFunction &MF = DAG.getMachineFunction();
10049 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10050
Dan Gohman69de1932008-02-06 22:27:42 +000010051 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010052 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010053
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010054 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010055 // vastart just stores the address of the VarArgsFrameIndex slot into the
10056 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010057 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10058 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010059 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10060 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010061 }
10062
10063 // __va_list_tag:
10064 // gp_offset (0 - 6 * 8)
10065 // fp_offset (48 - 48 + 8 * 16)
10066 // overflow_arg_area (point to parameters coming in memory).
10067 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010068 SmallVector<SDValue, 8> MemOps;
10069 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010070 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010071 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010072 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10073 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010074 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010075 MemOps.push_back(Store);
10076
10077 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010078 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010079 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010080 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010081 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10082 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010083 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010084 MemOps.push_back(Store);
10085
10086 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010087 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010088 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010089 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10090 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010091 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10092 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010093 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010094 MemOps.push_back(Store);
10095
10096 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010097 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010098 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010099 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10100 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010101 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10102 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010103 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010104 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010105 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010106}
10107
Dan Gohmand858e902010-04-17 15:26:15 +000010108SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010109 assert(Subtarget->is64Bit() &&
10110 "LowerVAARG only handles 64-bit va_arg!");
10111 assert((Subtarget->isTargetLinux() ||
10112 Subtarget->isTargetDarwin()) &&
10113 "Unhandled target in LowerVAARG");
10114 assert(Op.getNode()->getNumOperands() == 4);
10115 SDValue Chain = Op.getOperand(0);
10116 SDValue SrcPtr = Op.getOperand(1);
10117 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10118 unsigned Align = Op.getConstantOperandVal(3);
10119 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010120
Dan Gohman320afb82010-10-12 18:00:49 +000010121 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010122 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010123 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010124 uint8_t ArgMode;
10125
10126 // Decide which area this value should be read from.
10127 // TODO: Implement the AMD64 ABI in its entirety. This simple
10128 // selection mechanism works only for the basic types.
10129 if (ArgVT == MVT::f80) {
10130 llvm_unreachable("va_arg for f80 not yet implemented");
10131 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10132 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10133 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10134 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10135 } else {
10136 llvm_unreachable("Unhandled argument type in LowerVAARG");
10137 }
10138
10139 if (ArgMode == 2) {
10140 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010141 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010142 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010143 .getFunction()->getAttributes()
10144 .hasAttribute(AttributeSet::FunctionIndex,
10145 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010146 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010147 }
10148
10149 // Insert VAARG_64 node into the DAG
10150 // VAARG_64 returns two values: Variable Argument Address, Chain
10151 SmallVector<SDValue, 11> InstOps;
10152 InstOps.push_back(Chain);
10153 InstOps.push_back(SrcPtr);
10154 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10155 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10156 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10157 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10158 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10159 VTs, &InstOps[0], InstOps.size(),
10160 MVT::i64,
10161 MachinePointerInfo(SV),
10162 /*Align=*/0,
10163 /*Volatile=*/false,
10164 /*ReadMem=*/true,
10165 /*WriteMem=*/true);
10166 Chain = VAARG.getValue(1);
10167
10168 // Load the next argument and return it
10169 return DAG.getLoad(ArgVT, dl,
10170 Chain,
10171 VAARG,
10172 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010173 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010174}
10175
Craig Topper55b24052012-09-11 06:15:32 +000010176static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10177 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010178 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010179 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010180 SDValue Chain = Op.getOperand(0);
10181 SDValue DstPtr = Op.getOperand(1);
10182 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010183 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10184 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010185 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010186
Chris Lattnere72f2022010-09-21 05:40:29 +000010187 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010188 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010189 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010190 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010191}
10192
Craig Topper80e46362012-01-23 06:16:53 +000010193// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10194// may or may not be a constant. Takes immediate version of shift as input.
10195static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10196 SDValue SrcOp, SDValue ShAmt,
10197 SelectionDAG &DAG) {
10198 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10199
10200 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010201 // Constant may be a TargetConstant. Use a regular constant.
10202 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010203 switch (Opc) {
10204 default: llvm_unreachable("Unknown target vector shift node");
10205 case X86ISD::VSHLI:
10206 case X86ISD::VSRLI:
10207 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010208 return DAG.getNode(Opc, dl, VT, SrcOp,
10209 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010210 }
10211 }
10212
10213 // Change opcode to non-immediate version
10214 switch (Opc) {
10215 default: llvm_unreachable("Unknown target vector shift node");
10216 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10217 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10218 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10219 }
10220
10221 // Need to build a vector containing shift amount
10222 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10223 SDValue ShOps[4];
10224 ShOps[0] = ShAmt;
10225 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010226 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010227 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010228
10229 // The return type has to be a 128-bit type with the same element
10230 // type as the input type.
10231 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10232 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10233
10234 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010235 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10236}
10237
Craig Topper55b24052012-09-11 06:15:32 +000010238static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010239 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010240 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010241 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010242 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010243 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010244 case Intrinsic::x86_sse_comieq_ss:
10245 case Intrinsic::x86_sse_comilt_ss:
10246 case Intrinsic::x86_sse_comile_ss:
10247 case Intrinsic::x86_sse_comigt_ss:
10248 case Intrinsic::x86_sse_comige_ss:
10249 case Intrinsic::x86_sse_comineq_ss:
10250 case Intrinsic::x86_sse_ucomieq_ss:
10251 case Intrinsic::x86_sse_ucomilt_ss:
10252 case Intrinsic::x86_sse_ucomile_ss:
10253 case Intrinsic::x86_sse_ucomigt_ss:
10254 case Intrinsic::x86_sse_ucomige_ss:
10255 case Intrinsic::x86_sse_ucomineq_ss:
10256 case Intrinsic::x86_sse2_comieq_sd:
10257 case Intrinsic::x86_sse2_comilt_sd:
10258 case Intrinsic::x86_sse2_comile_sd:
10259 case Intrinsic::x86_sse2_comigt_sd:
10260 case Intrinsic::x86_sse2_comige_sd:
10261 case Intrinsic::x86_sse2_comineq_sd:
10262 case Intrinsic::x86_sse2_ucomieq_sd:
10263 case Intrinsic::x86_sse2_ucomilt_sd:
10264 case Intrinsic::x86_sse2_ucomile_sd:
10265 case Intrinsic::x86_sse2_ucomigt_sd:
10266 case Intrinsic::x86_sse2_ucomige_sd:
10267 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010268 unsigned Opc;
10269 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010270 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010271 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010272 case Intrinsic::x86_sse_comieq_ss:
10273 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010274 Opc = X86ISD::COMI;
10275 CC = ISD::SETEQ;
10276 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010277 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010278 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010279 Opc = X86ISD::COMI;
10280 CC = ISD::SETLT;
10281 break;
10282 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010283 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010284 Opc = X86ISD::COMI;
10285 CC = ISD::SETLE;
10286 break;
10287 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010288 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010289 Opc = X86ISD::COMI;
10290 CC = ISD::SETGT;
10291 break;
10292 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010293 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010294 Opc = X86ISD::COMI;
10295 CC = ISD::SETGE;
10296 break;
10297 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010298 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010299 Opc = X86ISD::COMI;
10300 CC = ISD::SETNE;
10301 break;
10302 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010303 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010304 Opc = X86ISD::UCOMI;
10305 CC = ISD::SETEQ;
10306 break;
10307 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010308 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010309 Opc = X86ISD::UCOMI;
10310 CC = ISD::SETLT;
10311 break;
10312 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010313 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010314 Opc = X86ISD::UCOMI;
10315 CC = ISD::SETLE;
10316 break;
10317 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010318 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010319 Opc = X86ISD::UCOMI;
10320 CC = ISD::SETGT;
10321 break;
10322 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010323 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010324 Opc = X86ISD::UCOMI;
10325 CC = ISD::SETGE;
10326 break;
10327 case Intrinsic::x86_sse_ucomineq_ss:
10328 case Intrinsic::x86_sse2_ucomineq_sd:
10329 Opc = X86ISD::UCOMI;
10330 CC = ISD::SETNE;
10331 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010332 }
Evan Cheng734503b2006-09-11 02:19:56 +000010333
Dan Gohman475871a2008-07-27 21:46:04 +000010334 SDValue LHS = Op.getOperand(1);
10335 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010336 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010337 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010338 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10339 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10340 DAG.getConstant(X86CC, MVT::i8), Cond);
10341 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010342 }
Craig Topper6d688152012-08-14 07:43:25 +000010343
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010344 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010345 case Intrinsic::x86_sse2_pmulu_dq:
10346 case Intrinsic::x86_avx2_pmulu_dq:
10347 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10348 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010349
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010350 // SSE2/AVX2 sub with unsigned saturation intrinsics
10351 case Intrinsic::x86_sse2_psubus_b:
10352 case Intrinsic::x86_sse2_psubus_w:
10353 case Intrinsic::x86_avx2_psubus_b:
10354 case Intrinsic::x86_avx2_psubus_w:
10355 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10356 Op.getOperand(1), Op.getOperand(2));
10357
Craig Topper6d688152012-08-14 07:43:25 +000010358 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010359 case Intrinsic::x86_sse3_hadd_ps:
10360 case Intrinsic::x86_sse3_hadd_pd:
10361 case Intrinsic::x86_avx_hadd_ps_256:
10362 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010363 case Intrinsic::x86_sse3_hsub_ps:
10364 case Intrinsic::x86_sse3_hsub_pd:
10365 case Intrinsic::x86_avx_hsub_ps_256:
10366 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010367 case Intrinsic::x86_ssse3_phadd_w_128:
10368 case Intrinsic::x86_ssse3_phadd_d_128:
10369 case Intrinsic::x86_avx2_phadd_w:
10370 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010371 case Intrinsic::x86_ssse3_phsub_w_128:
10372 case Intrinsic::x86_ssse3_phsub_d_128:
10373 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010374 case Intrinsic::x86_avx2_phsub_d: {
10375 unsigned Opcode;
10376 switch (IntNo) {
10377 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10378 case Intrinsic::x86_sse3_hadd_ps:
10379 case Intrinsic::x86_sse3_hadd_pd:
10380 case Intrinsic::x86_avx_hadd_ps_256:
10381 case Intrinsic::x86_avx_hadd_pd_256:
10382 Opcode = X86ISD::FHADD;
10383 break;
10384 case Intrinsic::x86_sse3_hsub_ps:
10385 case Intrinsic::x86_sse3_hsub_pd:
10386 case Intrinsic::x86_avx_hsub_ps_256:
10387 case Intrinsic::x86_avx_hsub_pd_256:
10388 Opcode = X86ISD::FHSUB;
10389 break;
10390 case Intrinsic::x86_ssse3_phadd_w_128:
10391 case Intrinsic::x86_ssse3_phadd_d_128:
10392 case Intrinsic::x86_avx2_phadd_w:
10393 case Intrinsic::x86_avx2_phadd_d:
10394 Opcode = X86ISD::HADD;
10395 break;
10396 case Intrinsic::x86_ssse3_phsub_w_128:
10397 case Intrinsic::x86_ssse3_phsub_d_128:
10398 case Intrinsic::x86_avx2_phsub_w:
10399 case Intrinsic::x86_avx2_phsub_d:
10400 Opcode = X86ISD::HSUB;
10401 break;
10402 }
10403 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010404 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010405 }
10406
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010407 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10408 case Intrinsic::x86_sse2_pmaxu_b:
10409 case Intrinsic::x86_sse41_pmaxuw:
10410 case Intrinsic::x86_sse41_pmaxud:
10411 case Intrinsic::x86_avx2_pmaxu_b:
10412 case Intrinsic::x86_avx2_pmaxu_w:
10413 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010414 case Intrinsic::x86_sse2_pminu_b:
10415 case Intrinsic::x86_sse41_pminuw:
10416 case Intrinsic::x86_sse41_pminud:
10417 case Intrinsic::x86_avx2_pminu_b:
10418 case Intrinsic::x86_avx2_pminu_w:
10419 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010420 case Intrinsic::x86_sse41_pmaxsb:
10421 case Intrinsic::x86_sse2_pmaxs_w:
10422 case Intrinsic::x86_sse41_pmaxsd:
10423 case Intrinsic::x86_avx2_pmaxs_b:
10424 case Intrinsic::x86_avx2_pmaxs_w:
10425 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010426 case Intrinsic::x86_sse41_pminsb:
10427 case Intrinsic::x86_sse2_pmins_w:
10428 case Intrinsic::x86_sse41_pminsd:
10429 case Intrinsic::x86_avx2_pmins_b:
10430 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010431 case Intrinsic::x86_avx2_pmins_d: {
10432 unsigned Opcode;
10433 switch (IntNo) {
10434 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10435 case Intrinsic::x86_sse2_pmaxu_b:
10436 case Intrinsic::x86_sse41_pmaxuw:
10437 case Intrinsic::x86_sse41_pmaxud:
10438 case Intrinsic::x86_avx2_pmaxu_b:
10439 case Intrinsic::x86_avx2_pmaxu_w:
10440 case Intrinsic::x86_avx2_pmaxu_d:
10441 Opcode = X86ISD::UMAX;
10442 break;
10443 case Intrinsic::x86_sse2_pminu_b:
10444 case Intrinsic::x86_sse41_pminuw:
10445 case Intrinsic::x86_sse41_pminud:
10446 case Intrinsic::x86_avx2_pminu_b:
10447 case Intrinsic::x86_avx2_pminu_w:
10448 case Intrinsic::x86_avx2_pminu_d:
10449 Opcode = X86ISD::UMIN;
10450 break;
10451 case Intrinsic::x86_sse41_pmaxsb:
10452 case Intrinsic::x86_sse2_pmaxs_w:
10453 case Intrinsic::x86_sse41_pmaxsd:
10454 case Intrinsic::x86_avx2_pmaxs_b:
10455 case Intrinsic::x86_avx2_pmaxs_w:
10456 case Intrinsic::x86_avx2_pmaxs_d:
10457 Opcode = X86ISD::SMAX;
10458 break;
10459 case Intrinsic::x86_sse41_pminsb:
10460 case Intrinsic::x86_sse2_pmins_w:
10461 case Intrinsic::x86_sse41_pminsd:
10462 case Intrinsic::x86_avx2_pmins_b:
10463 case Intrinsic::x86_avx2_pmins_w:
10464 case Intrinsic::x86_avx2_pmins_d:
10465 Opcode = X86ISD::SMIN;
10466 break;
10467 }
10468 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010469 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010470 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010471
Craig Topper6d183e42012-12-29 16:44:25 +000010472 // SSE/SSE2/AVX floating point max/min intrinsics.
10473 case Intrinsic::x86_sse_max_ps:
10474 case Intrinsic::x86_sse2_max_pd:
10475 case Intrinsic::x86_avx_max_ps_256:
10476 case Intrinsic::x86_avx_max_pd_256:
10477 case Intrinsic::x86_sse_min_ps:
10478 case Intrinsic::x86_sse2_min_pd:
10479 case Intrinsic::x86_avx_min_ps_256:
10480 case Intrinsic::x86_avx_min_pd_256: {
10481 unsigned Opcode;
10482 switch (IntNo) {
10483 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10484 case Intrinsic::x86_sse_max_ps:
10485 case Intrinsic::x86_sse2_max_pd:
10486 case Intrinsic::x86_avx_max_ps_256:
10487 case Intrinsic::x86_avx_max_pd_256:
10488 Opcode = X86ISD::FMAX;
10489 break;
10490 case Intrinsic::x86_sse_min_ps:
10491 case Intrinsic::x86_sse2_min_pd:
10492 case Intrinsic::x86_avx_min_ps_256:
10493 case Intrinsic::x86_avx_min_pd_256:
10494 Opcode = X86ISD::FMIN;
10495 break;
10496 }
10497 return DAG.getNode(Opcode, dl, Op.getValueType(),
10498 Op.getOperand(1), Op.getOperand(2));
10499 }
10500
Craig Topper6d688152012-08-14 07:43:25 +000010501 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010502 case Intrinsic::x86_avx2_psllv_d:
10503 case Intrinsic::x86_avx2_psllv_q:
10504 case Intrinsic::x86_avx2_psllv_d_256:
10505 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010506 case Intrinsic::x86_avx2_psrlv_d:
10507 case Intrinsic::x86_avx2_psrlv_q:
10508 case Intrinsic::x86_avx2_psrlv_d_256:
10509 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010510 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010511 case Intrinsic::x86_avx2_psrav_d_256: {
10512 unsigned Opcode;
10513 switch (IntNo) {
10514 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10515 case Intrinsic::x86_avx2_psllv_d:
10516 case Intrinsic::x86_avx2_psllv_q:
10517 case Intrinsic::x86_avx2_psllv_d_256:
10518 case Intrinsic::x86_avx2_psllv_q_256:
10519 Opcode = ISD::SHL;
10520 break;
10521 case Intrinsic::x86_avx2_psrlv_d:
10522 case Intrinsic::x86_avx2_psrlv_q:
10523 case Intrinsic::x86_avx2_psrlv_d_256:
10524 case Intrinsic::x86_avx2_psrlv_q_256:
10525 Opcode = ISD::SRL;
10526 break;
10527 case Intrinsic::x86_avx2_psrav_d:
10528 case Intrinsic::x86_avx2_psrav_d_256:
10529 Opcode = ISD::SRA;
10530 break;
10531 }
10532 return DAG.getNode(Opcode, dl, Op.getValueType(),
10533 Op.getOperand(1), Op.getOperand(2));
10534 }
10535
Craig Topper969ba282012-01-25 06:43:11 +000010536 case Intrinsic::x86_ssse3_pshuf_b_128:
10537 case Intrinsic::x86_avx2_pshuf_b:
10538 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10539 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010540
Craig Topper969ba282012-01-25 06:43:11 +000010541 case Intrinsic::x86_ssse3_psign_b_128:
10542 case Intrinsic::x86_ssse3_psign_w_128:
10543 case Intrinsic::x86_ssse3_psign_d_128:
10544 case Intrinsic::x86_avx2_psign_b:
10545 case Intrinsic::x86_avx2_psign_w:
10546 case Intrinsic::x86_avx2_psign_d:
10547 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10548 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010549
Craig Toppere566cd02012-01-26 07:18:03 +000010550 case Intrinsic::x86_sse41_insertps:
10551 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10552 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010553
Craig Toppere566cd02012-01-26 07:18:03 +000010554 case Intrinsic::x86_avx_vperm2f128_ps_256:
10555 case Intrinsic::x86_avx_vperm2f128_pd_256:
10556 case Intrinsic::x86_avx_vperm2f128_si_256:
10557 case Intrinsic::x86_avx2_vperm2i128:
10558 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10559 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010560
Craig Topperffa6c402012-04-16 07:13:00 +000010561 case Intrinsic::x86_avx2_permd:
10562 case Intrinsic::x86_avx2_permps:
10563 // Operands intentionally swapped. Mask is last operand to intrinsic,
10564 // but second operand for node/intruction.
10565 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10566 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010567
Craig Topper22d8f0d2012-12-29 18:18:20 +000010568 case Intrinsic::x86_sse_sqrt_ps:
10569 case Intrinsic::x86_sse2_sqrt_pd:
10570 case Intrinsic::x86_avx_sqrt_ps_256:
10571 case Intrinsic::x86_avx_sqrt_pd_256:
10572 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10573
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010574 // ptest and testp intrinsics. The intrinsic these come from are designed to
10575 // return an integer value, not just an instruction so lower it to the ptest
10576 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010577 case Intrinsic::x86_sse41_ptestz:
10578 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010579 case Intrinsic::x86_sse41_ptestnzc:
10580 case Intrinsic::x86_avx_ptestz_256:
10581 case Intrinsic::x86_avx_ptestc_256:
10582 case Intrinsic::x86_avx_ptestnzc_256:
10583 case Intrinsic::x86_avx_vtestz_ps:
10584 case Intrinsic::x86_avx_vtestc_ps:
10585 case Intrinsic::x86_avx_vtestnzc_ps:
10586 case Intrinsic::x86_avx_vtestz_pd:
10587 case Intrinsic::x86_avx_vtestc_pd:
10588 case Intrinsic::x86_avx_vtestnzc_pd:
10589 case Intrinsic::x86_avx_vtestz_ps_256:
10590 case Intrinsic::x86_avx_vtestc_ps_256:
10591 case Intrinsic::x86_avx_vtestnzc_ps_256:
10592 case Intrinsic::x86_avx_vtestz_pd_256:
10593 case Intrinsic::x86_avx_vtestc_pd_256:
10594 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10595 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010596 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010597 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010598 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010599 case Intrinsic::x86_avx_vtestz_ps:
10600 case Intrinsic::x86_avx_vtestz_pd:
10601 case Intrinsic::x86_avx_vtestz_ps_256:
10602 case Intrinsic::x86_avx_vtestz_pd_256:
10603 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010604 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010605 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010606 // ZF = 1
10607 X86CC = X86::COND_E;
10608 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010609 case Intrinsic::x86_avx_vtestc_ps:
10610 case Intrinsic::x86_avx_vtestc_pd:
10611 case Intrinsic::x86_avx_vtestc_ps_256:
10612 case Intrinsic::x86_avx_vtestc_pd_256:
10613 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010614 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010615 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010616 // CF = 1
10617 X86CC = X86::COND_B;
10618 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010619 case Intrinsic::x86_avx_vtestnzc_ps:
10620 case Intrinsic::x86_avx_vtestnzc_pd:
10621 case Intrinsic::x86_avx_vtestnzc_ps_256:
10622 case Intrinsic::x86_avx_vtestnzc_pd_256:
10623 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010624 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010625 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010626 // ZF and CF = 0
10627 X86CC = X86::COND_A;
10628 break;
10629 }
Eric Christopherfd179292009-08-27 18:07:15 +000010630
Eric Christopher71c67532009-07-29 00:28:05 +000010631 SDValue LHS = Op.getOperand(1);
10632 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010633 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10634 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010635 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10636 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10637 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010638 }
Evan Cheng5759f972008-05-04 09:15:50 +000010639
Craig Topper80e46362012-01-23 06:16:53 +000010640 // SSE/AVX shift intrinsics
10641 case Intrinsic::x86_sse2_psll_w:
10642 case Intrinsic::x86_sse2_psll_d:
10643 case Intrinsic::x86_sse2_psll_q:
10644 case Intrinsic::x86_avx2_psll_w:
10645 case Intrinsic::x86_avx2_psll_d:
10646 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010647 case Intrinsic::x86_sse2_psrl_w:
10648 case Intrinsic::x86_sse2_psrl_d:
10649 case Intrinsic::x86_sse2_psrl_q:
10650 case Intrinsic::x86_avx2_psrl_w:
10651 case Intrinsic::x86_avx2_psrl_d:
10652 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010653 case Intrinsic::x86_sse2_psra_w:
10654 case Intrinsic::x86_sse2_psra_d:
10655 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010656 case Intrinsic::x86_avx2_psra_d: {
10657 unsigned Opcode;
10658 switch (IntNo) {
10659 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10660 case Intrinsic::x86_sse2_psll_w:
10661 case Intrinsic::x86_sse2_psll_d:
10662 case Intrinsic::x86_sse2_psll_q:
10663 case Intrinsic::x86_avx2_psll_w:
10664 case Intrinsic::x86_avx2_psll_d:
10665 case Intrinsic::x86_avx2_psll_q:
10666 Opcode = X86ISD::VSHL;
10667 break;
10668 case Intrinsic::x86_sse2_psrl_w:
10669 case Intrinsic::x86_sse2_psrl_d:
10670 case Intrinsic::x86_sse2_psrl_q:
10671 case Intrinsic::x86_avx2_psrl_w:
10672 case Intrinsic::x86_avx2_psrl_d:
10673 case Intrinsic::x86_avx2_psrl_q:
10674 Opcode = X86ISD::VSRL;
10675 break;
10676 case Intrinsic::x86_sse2_psra_w:
10677 case Intrinsic::x86_sse2_psra_d:
10678 case Intrinsic::x86_avx2_psra_w:
10679 case Intrinsic::x86_avx2_psra_d:
10680 Opcode = X86ISD::VSRA;
10681 break;
10682 }
10683 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010684 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010685 }
10686
10687 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010688 case Intrinsic::x86_sse2_pslli_w:
10689 case Intrinsic::x86_sse2_pslli_d:
10690 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010691 case Intrinsic::x86_avx2_pslli_w:
10692 case Intrinsic::x86_avx2_pslli_d:
10693 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010694 case Intrinsic::x86_sse2_psrli_w:
10695 case Intrinsic::x86_sse2_psrli_d:
10696 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010697 case Intrinsic::x86_avx2_psrli_w:
10698 case Intrinsic::x86_avx2_psrli_d:
10699 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010700 case Intrinsic::x86_sse2_psrai_w:
10701 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010702 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010703 case Intrinsic::x86_avx2_psrai_d: {
10704 unsigned Opcode;
10705 switch (IntNo) {
10706 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10707 case Intrinsic::x86_sse2_pslli_w:
10708 case Intrinsic::x86_sse2_pslli_d:
10709 case Intrinsic::x86_sse2_pslli_q:
10710 case Intrinsic::x86_avx2_pslli_w:
10711 case Intrinsic::x86_avx2_pslli_d:
10712 case Intrinsic::x86_avx2_pslli_q:
10713 Opcode = X86ISD::VSHLI;
10714 break;
10715 case Intrinsic::x86_sse2_psrli_w:
10716 case Intrinsic::x86_sse2_psrli_d:
10717 case Intrinsic::x86_sse2_psrli_q:
10718 case Intrinsic::x86_avx2_psrli_w:
10719 case Intrinsic::x86_avx2_psrli_d:
10720 case Intrinsic::x86_avx2_psrli_q:
10721 Opcode = X86ISD::VSRLI;
10722 break;
10723 case Intrinsic::x86_sse2_psrai_w:
10724 case Intrinsic::x86_sse2_psrai_d:
10725 case Intrinsic::x86_avx2_psrai_w:
10726 case Intrinsic::x86_avx2_psrai_d:
10727 Opcode = X86ISD::VSRAI;
10728 break;
10729 }
10730 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010731 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010732 }
10733
Craig Topper4feb6472012-08-06 06:22:36 +000010734 case Intrinsic::x86_sse42_pcmpistria128:
10735 case Intrinsic::x86_sse42_pcmpestria128:
10736 case Intrinsic::x86_sse42_pcmpistric128:
10737 case Intrinsic::x86_sse42_pcmpestric128:
10738 case Intrinsic::x86_sse42_pcmpistrio128:
10739 case Intrinsic::x86_sse42_pcmpestrio128:
10740 case Intrinsic::x86_sse42_pcmpistris128:
10741 case Intrinsic::x86_sse42_pcmpestris128:
10742 case Intrinsic::x86_sse42_pcmpistriz128:
10743 case Intrinsic::x86_sse42_pcmpestriz128: {
10744 unsigned Opcode;
10745 unsigned X86CC;
10746 switch (IntNo) {
10747 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10748 case Intrinsic::x86_sse42_pcmpistria128:
10749 Opcode = X86ISD::PCMPISTRI;
10750 X86CC = X86::COND_A;
10751 break;
10752 case Intrinsic::x86_sse42_pcmpestria128:
10753 Opcode = X86ISD::PCMPESTRI;
10754 X86CC = X86::COND_A;
10755 break;
10756 case Intrinsic::x86_sse42_pcmpistric128:
10757 Opcode = X86ISD::PCMPISTRI;
10758 X86CC = X86::COND_B;
10759 break;
10760 case Intrinsic::x86_sse42_pcmpestric128:
10761 Opcode = X86ISD::PCMPESTRI;
10762 X86CC = X86::COND_B;
10763 break;
10764 case Intrinsic::x86_sse42_pcmpistrio128:
10765 Opcode = X86ISD::PCMPISTRI;
10766 X86CC = X86::COND_O;
10767 break;
10768 case Intrinsic::x86_sse42_pcmpestrio128:
10769 Opcode = X86ISD::PCMPESTRI;
10770 X86CC = X86::COND_O;
10771 break;
10772 case Intrinsic::x86_sse42_pcmpistris128:
10773 Opcode = X86ISD::PCMPISTRI;
10774 X86CC = X86::COND_S;
10775 break;
10776 case Intrinsic::x86_sse42_pcmpestris128:
10777 Opcode = X86ISD::PCMPESTRI;
10778 X86CC = X86::COND_S;
10779 break;
10780 case Intrinsic::x86_sse42_pcmpistriz128:
10781 Opcode = X86ISD::PCMPISTRI;
10782 X86CC = X86::COND_E;
10783 break;
10784 case Intrinsic::x86_sse42_pcmpestriz128:
10785 Opcode = X86ISD::PCMPESTRI;
10786 X86CC = X86::COND_E;
10787 break;
10788 }
10789 SmallVector<SDValue, 5> NewOps;
10790 NewOps.append(Op->op_begin()+1, Op->op_end());
10791 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10792 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10793 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10794 DAG.getConstant(X86CC, MVT::i8),
10795 SDValue(PCMP.getNode(), 1));
10796 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10797 }
Craig Topper6d688152012-08-14 07:43:25 +000010798
Craig Topper4feb6472012-08-06 06:22:36 +000010799 case Intrinsic::x86_sse42_pcmpistri128:
10800 case Intrinsic::x86_sse42_pcmpestri128: {
10801 unsigned Opcode;
10802 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10803 Opcode = X86ISD::PCMPISTRI;
10804 else
10805 Opcode = X86ISD::PCMPESTRI;
10806
10807 SmallVector<SDValue, 5> NewOps;
10808 NewOps.append(Op->op_begin()+1, Op->op_end());
10809 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10810 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10811 }
Craig Topper0e292372012-08-24 04:03:22 +000010812 case Intrinsic::x86_fma_vfmadd_ps:
10813 case Intrinsic::x86_fma_vfmadd_pd:
10814 case Intrinsic::x86_fma_vfmsub_ps:
10815 case Intrinsic::x86_fma_vfmsub_pd:
10816 case Intrinsic::x86_fma_vfnmadd_ps:
10817 case Intrinsic::x86_fma_vfnmadd_pd:
10818 case Intrinsic::x86_fma_vfnmsub_ps:
10819 case Intrinsic::x86_fma_vfnmsub_pd:
10820 case Intrinsic::x86_fma_vfmaddsub_ps:
10821 case Intrinsic::x86_fma_vfmaddsub_pd:
10822 case Intrinsic::x86_fma_vfmsubadd_ps:
10823 case Intrinsic::x86_fma_vfmsubadd_pd:
10824 case Intrinsic::x86_fma_vfmadd_ps_256:
10825 case Intrinsic::x86_fma_vfmadd_pd_256:
10826 case Intrinsic::x86_fma_vfmsub_ps_256:
10827 case Intrinsic::x86_fma_vfmsub_pd_256:
10828 case Intrinsic::x86_fma_vfnmadd_ps_256:
10829 case Intrinsic::x86_fma_vfnmadd_pd_256:
10830 case Intrinsic::x86_fma_vfnmsub_ps_256:
10831 case Intrinsic::x86_fma_vfnmsub_pd_256:
10832 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10833 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10834 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10835 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010836 unsigned Opc;
10837 switch (IntNo) {
10838 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10839 case Intrinsic::x86_fma_vfmadd_ps:
10840 case Intrinsic::x86_fma_vfmadd_pd:
10841 case Intrinsic::x86_fma_vfmadd_ps_256:
10842 case Intrinsic::x86_fma_vfmadd_pd_256:
10843 Opc = X86ISD::FMADD;
10844 break;
10845 case Intrinsic::x86_fma_vfmsub_ps:
10846 case Intrinsic::x86_fma_vfmsub_pd:
10847 case Intrinsic::x86_fma_vfmsub_ps_256:
10848 case Intrinsic::x86_fma_vfmsub_pd_256:
10849 Opc = X86ISD::FMSUB;
10850 break;
10851 case Intrinsic::x86_fma_vfnmadd_ps:
10852 case Intrinsic::x86_fma_vfnmadd_pd:
10853 case Intrinsic::x86_fma_vfnmadd_ps_256:
10854 case Intrinsic::x86_fma_vfnmadd_pd_256:
10855 Opc = X86ISD::FNMADD;
10856 break;
10857 case Intrinsic::x86_fma_vfnmsub_ps:
10858 case Intrinsic::x86_fma_vfnmsub_pd:
10859 case Intrinsic::x86_fma_vfnmsub_ps_256:
10860 case Intrinsic::x86_fma_vfnmsub_pd_256:
10861 Opc = X86ISD::FNMSUB;
10862 break;
10863 case Intrinsic::x86_fma_vfmaddsub_ps:
10864 case Intrinsic::x86_fma_vfmaddsub_pd:
10865 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10866 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10867 Opc = X86ISD::FMADDSUB;
10868 break;
10869 case Intrinsic::x86_fma_vfmsubadd_ps:
10870 case Intrinsic::x86_fma_vfmsubadd_pd:
10871 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10872 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10873 Opc = X86ISD::FMSUBADD;
10874 break;
10875 }
10876
10877 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10878 Op.getOperand(2), Op.getOperand(3));
10879 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010880 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010881}
Evan Cheng72261582005-12-20 06:22:03 +000010882
Craig Topper55b24052012-09-11 06:15:32 +000010883static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010884 DebugLoc dl = Op.getDebugLoc();
10885 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10886 switch (IntNo) {
10887 default: return SDValue(); // Don't custom lower most intrinsics.
10888
10889 // RDRAND intrinsics.
10890 case Intrinsic::x86_rdrand_16:
10891 case Intrinsic::x86_rdrand_32:
10892 case Intrinsic::x86_rdrand_64: {
10893 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010894 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10895 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010896
10897 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10898 // return the value from Rand, which is always 0, casted to i32.
10899 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10900 DAG.getConstant(1, Op->getValueType(1)),
10901 DAG.getConstant(X86::COND_B, MVT::i32),
10902 SDValue(Result.getNode(), 1) };
10903 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10904 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10905 Ops, 4);
10906
10907 // Return { result, isValid, chain }.
10908 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010909 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010910 }
10911 }
10912}
10913
Dan Gohmand858e902010-04-17 15:26:15 +000010914SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10915 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010916 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10917 MFI->setReturnAddressIsTaken(true);
10918
Bill Wendling64e87322009-01-16 19:25:27 +000010919 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010920 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010921 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010922
10923 if (Depth > 0) {
10924 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10925 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010926 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10927 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10928 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010929 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010930 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010931 }
10932
10933 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010934 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010935 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010936 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010937}
10938
Dan Gohmand858e902010-04-17 15:26:15 +000010939SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010940 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10941 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010942
Owen Andersone50ed302009-08-10 22:56:29 +000010943 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010944 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010945 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10946 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010947 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010948 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010949 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10950 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010951 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010952 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010953}
10954
Dan Gohman475871a2008-07-27 21:46:04 +000010955SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010956 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010957 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010958}
10959
Dan Gohmand858e902010-04-17 15:26:15 +000010960SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010961 SDValue Chain = Op.getOperand(0);
10962 SDValue Offset = Op.getOperand(1);
10963 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010964 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010965
Dan Gohmand8816272010-08-11 18:14:00 +000010966 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10967 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10968 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010969 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010970
Dan Gohmand8816272010-08-11 18:14:00 +000010971 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010972 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010973 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010974 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10975 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010976 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010977
Dale Johannesene4d209d2009-02-03 20:21:25 +000010978 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010979 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010980 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010981}
10982
Michael Liao6c0e04c2012-10-15 22:39:43 +000010983SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10984 SelectionDAG &DAG) const {
10985 DebugLoc DL = Op.getDebugLoc();
10986 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10987 DAG.getVTList(MVT::i32, MVT::Other),
10988 Op.getOperand(0), Op.getOperand(1));
10989}
10990
10991SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10992 SelectionDAG &DAG) const {
10993 DebugLoc DL = Op.getDebugLoc();
10994 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10995 Op.getOperand(0), Op.getOperand(1));
10996}
10997
Craig Topper55b24052012-09-11 06:15:32 +000010998static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010999 return Op.getOperand(0);
11000}
11001
11002SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11003 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011004 SDValue Root = Op.getOperand(0);
11005 SDValue Trmp = Op.getOperand(1); // trampoline
11006 SDValue FPtr = Op.getOperand(2); // nested function
11007 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011008 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011009
Dan Gohman69de1932008-02-06 22:27:42 +000011010 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011011 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011012
11013 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011014 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011015
11016 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011017 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11018 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011019
Michael Liao7abf67a2012-10-04 19:50:43 +000011020 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11021 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011022
11023 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11024
11025 // Load the pointer to the nested function into R11.
11026 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011027 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011029 Addr, MachinePointerInfo(TrmpAddr),
11030 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011031
Owen Anderson825b72b2009-08-11 20:47:22 +000011032 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11033 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011034 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11035 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011036 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011037
11038 // Load the 'nest' parameter value into R10.
11039 // R10 is specified in X86CallingConv.td
11040 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011041 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11042 DAG.getConstant(10, MVT::i64));
11043 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011044 Addr, MachinePointerInfo(TrmpAddr, 10),
11045 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011046
Owen Anderson825b72b2009-08-11 20:47:22 +000011047 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11048 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011049 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11050 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011051 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011052
11053 // Jump to the nested function.
11054 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011055 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11056 DAG.getConstant(20, MVT::i64));
11057 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011058 Addr, MachinePointerInfo(TrmpAddr, 20),
11059 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011060
11061 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011062 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11063 DAG.getConstant(22, MVT::i64));
11064 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011065 MachinePointerInfo(TrmpAddr, 22),
11066 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011067
Duncan Sands4a544a72011-09-06 13:37:06 +000011068 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011069 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011070 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011071 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011072 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011073 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011074
11075 switch (CC) {
11076 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011077 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011078 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011079 case CallingConv::X86_StdCall: {
11080 // Pass 'nest' parameter in ECX.
11081 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011082 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011083
11084 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011085 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011086 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011087
Chris Lattner58d74912008-03-12 17:45:29 +000011088 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011089 unsigned InRegCount = 0;
11090 unsigned Idx = 1;
11091
11092 for (FunctionType::param_iterator I = FTy->param_begin(),
11093 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011094 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011095 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011096 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011097
11098 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011099 report_fatal_error("Nest register in use - reduce number of inreg"
11100 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011101 }
11102 }
11103 break;
11104 }
11105 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011106 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011107 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011108 // Pass 'nest' parameter in EAX.
11109 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011110 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011111 break;
11112 }
11113
Dan Gohman475871a2008-07-27 21:46:04 +000011114 SDValue OutChains[4];
11115 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011116
Owen Anderson825b72b2009-08-11 20:47:22 +000011117 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11118 DAG.getConstant(10, MVT::i32));
11119 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011120
Chris Lattnera62fe662010-02-05 19:20:30 +000011121 // This is storing the opcode for MOV32ri.
11122 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011123 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011124 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011125 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011126 Trmp, MachinePointerInfo(TrmpAddr),
11127 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011128
Owen Anderson825b72b2009-08-11 20:47:22 +000011129 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11130 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011131 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11132 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011133 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011134
Chris Lattnera62fe662010-02-05 19:20:30 +000011135 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011136 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11137 DAG.getConstant(5, MVT::i32));
11138 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011139 MachinePointerInfo(TrmpAddr, 5),
11140 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011141
Owen Anderson825b72b2009-08-11 20:47:22 +000011142 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11143 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011144 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11145 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011146 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011147
Duncan Sands4a544a72011-09-06 13:37:06 +000011148 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011149 }
11150}
11151
Dan Gohmand858e902010-04-17 15:26:15 +000011152SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11153 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011154 /*
11155 The rounding mode is in bits 11:10 of FPSR, and has the following
11156 settings:
11157 00 Round to nearest
11158 01 Round to -inf
11159 10 Round to +inf
11160 11 Round to 0
11161
11162 FLT_ROUNDS, on the other hand, expects the following:
11163 -1 Undefined
11164 0 Round to 0
11165 1 Round to nearest
11166 2 Round to +inf
11167 3 Round to -inf
11168
11169 To perform the conversion, we do:
11170 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11171 */
11172
11173 MachineFunction &MF = DAG.getMachineFunction();
11174 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011175 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011176 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011177 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011178 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011179
11180 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011181 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011182 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011183
Chris Lattner2156b792010-09-22 01:11:26 +000011184 MachineMemOperand *MMO =
11185 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11186 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011187
Chris Lattner2156b792010-09-22 01:11:26 +000011188 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11189 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11190 DAG.getVTList(MVT::Other),
11191 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011192
11193 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011194 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011195 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011196
11197 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011198 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011199 DAG.getNode(ISD::SRL, DL, MVT::i16,
11200 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011201 CWD, DAG.getConstant(0x800, MVT::i16)),
11202 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011203 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011204 DAG.getNode(ISD::SRL, DL, MVT::i16,
11205 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011206 CWD, DAG.getConstant(0x400, MVT::i16)),
11207 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011208
Dan Gohman475871a2008-07-27 21:46:04 +000011209 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011210 DAG.getNode(ISD::AND, DL, MVT::i16,
11211 DAG.getNode(ISD::ADD, DL, MVT::i16,
11212 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011213 DAG.getConstant(1, MVT::i16)),
11214 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011215
Duncan Sands83ec4b62008-06-06 12:08:01 +000011216 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011217 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011218}
11219
Craig Topper55b24052012-09-11 06:15:32 +000011220static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011221 EVT VT = Op.getValueType();
11222 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011223 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011224 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011225
11226 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011227 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011228 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011229 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011230 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011231 }
Evan Cheng18efe262007-12-14 02:13:44 +000011232
Evan Cheng152804e2007-12-14 08:30:15 +000011233 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011234 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011235 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011236
11237 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011238 SDValue Ops[] = {
11239 Op,
11240 DAG.getConstant(NumBits+NumBits-1, OpVT),
11241 DAG.getConstant(X86::COND_E, MVT::i8),
11242 Op.getValue(1)
11243 };
11244 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011245
11246 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011247 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011248
Owen Anderson825b72b2009-08-11 20:47:22 +000011249 if (VT == MVT::i8)
11250 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011251 return Op;
11252}
11253
Craig Topper55b24052012-09-11 06:15:32 +000011254static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011255 EVT VT = Op.getValueType();
11256 EVT OpVT = VT;
11257 unsigned NumBits = VT.getSizeInBits();
11258 DebugLoc dl = Op.getDebugLoc();
11259
11260 Op = Op.getOperand(0);
11261 if (VT == MVT::i8) {
11262 // Zero extend to i32 since there is not an i8 bsr.
11263 OpVT = MVT::i32;
11264 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11265 }
11266
11267 // Issue a bsr (scan bits in reverse).
11268 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11269 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11270
11271 // And xor with NumBits-1.
11272 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11273
11274 if (VT == MVT::i8)
11275 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11276 return Op;
11277}
11278
Craig Topper55b24052012-09-11 06:15:32 +000011279static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011280 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011281 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011282 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011283 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011284
11285 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011286 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011287 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011288
11289 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011290 SDValue Ops[] = {
11291 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011292 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011293 DAG.getConstant(X86::COND_E, MVT::i8),
11294 Op.getValue(1)
11295 };
Chandler Carruth77821022011-12-24 12:12:34 +000011296 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011297}
11298
Craig Topper13894fa2011-08-24 06:14:18 +000011299// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11300// ones, and then concatenate the result back.
11301static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011302 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011303
Craig Topper7a9a28b2012-08-12 02:23:29 +000011304 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011305 "Unsupported value type for operation");
11306
Craig Topper66ddd152012-04-27 22:54:43 +000011307 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011308 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011309
11310 // Extract the LHS vectors
11311 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011312 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11313 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011314
11315 // Extract the RHS vectors
11316 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011317 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11318 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011319
11320 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11321 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11322
11323 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11324 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11325 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11326}
11327
Craig Topper55b24052012-09-11 06:15:32 +000011328static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011329 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011330 Op.getValueType().isInteger() &&
11331 "Only handle AVX 256-bit vector integer operation");
11332 return Lower256IntArith(Op, DAG);
11333}
11334
Craig Topper55b24052012-09-11 06:15:32 +000011335static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011336 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011337 Op.getValueType().isInteger() &&
11338 "Only handle AVX 256-bit vector integer operation");
11339 return Lower256IntArith(Op, DAG);
11340}
11341
Craig Topper55b24052012-09-11 06:15:32 +000011342static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11343 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011344 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011345 EVT VT = Op.getValueType();
11346
11347 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011348 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011349 return Lower256IntArith(Op, DAG);
11350
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011351 SDValue A = Op.getOperand(0);
11352 SDValue B = Op.getOperand(1);
11353
11354 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11355 if (VT == MVT::v4i32) {
11356 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11357 "Should not custom lower when pmuldq is available!");
11358
11359 // Extract the odd parts.
11360 const int UnpackMask[] = { 1, -1, 3, -1 };
11361 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11362 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11363
11364 // Multiply the even parts.
11365 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11366 // Now multiply odd parts.
11367 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11368
11369 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11370 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11371
11372 // Merge the two vectors back together with a shuffle. This expands into 2
11373 // shuffles.
11374 const int ShufMask[] = { 0, 4, 2, 6 };
11375 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11376 }
11377
Craig Topper5b209e82012-02-05 03:14:49 +000011378 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11379 "Only know how to lower V2I64/V4I64 multiply");
11380
Craig Topper5b209e82012-02-05 03:14:49 +000011381 // Ahi = psrlqi(a, 32);
11382 // Bhi = psrlqi(b, 32);
11383 //
11384 // AloBlo = pmuludq(a, b);
11385 // AloBhi = pmuludq(a, Bhi);
11386 // AhiBlo = pmuludq(Ahi, b);
11387
11388 // AloBhi = psllqi(AloBhi, 32);
11389 // AhiBlo = psllqi(AhiBlo, 32);
11390 // return AloBlo + AloBhi + AhiBlo;
11391
Craig Topper5b209e82012-02-05 03:14:49 +000011392 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011393
Craig Topper5b209e82012-02-05 03:14:49 +000011394 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11395 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011396
Craig Topper5b209e82012-02-05 03:14:49 +000011397 // Bit cast to 32-bit vectors for MULUDQ
11398 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11399 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11400 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11401 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11402 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011403
Craig Topper5b209e82012-02-05 03:14:49 +000011404 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11405 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11406 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011407
Craig Topper5b209e82012-02-05 03:14:49 +000011408 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11409 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011410
Dale Johannesene4d209d2009-02-03 20:21:25 +000011411 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011412 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011413}
11414
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011415SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11416 EVT VT = Op.getValueType();
11417 EVT EltTy = VT.getVectorElementType();
11418 unsigned NumElts = VT.getVectorNumElements();
11419 SDValue N0 = Op.getOperand(0);
11420 DebugLoc dl = Op.getDebugLoc();
11421
11422 // Lower sdiv X, pow2-const.
11423 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11424 if (!C)
11425 return SDValue();
11426
11427 APInt SplatValue, SplatUndef;
11428 unsigned MinSplatBits;
11429 bool HasAnyUndefs;
11430 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11431 return SDValue();
11432
11433 if ((SplatValue != 0) &&
11434 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11435 unsigned lg2 = SplatValue.countTrailingZeros();
11436 // Splat the sign bit.
11437 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11438 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11439 // Add (N0 < 0) ? abs2 - 1 : 0;
11440 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11441 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11442 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11443 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11444 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11445
11446 // If we're dividing by a positive value, we're done. Otherwise, we must
11447 // negate the result.
11448 if (SplatValue.isNonNegative())
11449 return SRA;
11450
11451 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11452 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11453 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11454 }
11455 return SDValue();
11456}
11457
Nadav Rotem43012222011-05-11 08:12:09 +000011458SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11459
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011460 EVT VT = Op.getValueType();
11461 DebugLoc dl = Op.getDebugLoc();
11462 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011463 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011464
Craig Topper1accb7e2012-01-10 06:54:16 +000011465 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011466 return SDValue();
11467
Nadav Rotem43012222011-05-11 08:12:09 +000011468 // Optimize shl/srl/sra with constant shift amount.
11469 if (isSplatVector(Amt.getNode())) {
11470 SDValue SclrAmt = Amt->getOperand(0);
11471 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11472 uint64_t ShiftAmt = C->getZExtValue();
11473
Craig Toppered2e13d2012-01-22 19:15:14 +000011474 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011475 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011476 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11477 if (Op.getOpcode() == ISD::SHL)
11478 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11479 DAG.getConstant(ShiftAmt, MVT::i32));
11480 if (Op.getOpcode() == ISD::SRL)
11481 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11482 DAG.getConstant(ShiftAmt, MVT::i32));
11483 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11484 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11485 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011486 }
11487
Craig Toppered2e13d2012-01-22 19:15:14 +000011488 if (VT == MVT::v16i8) {
11489 if (Op.getOpcode() == ISD::SHL) {
11490 // Make a large shift.
11491 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11492 DAG.getConstant(ShiftAmt, MVT::i32));
11493 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11494 // Zero out the rightmost bits.
11495 SmallVector<SDValue, 16> V(16,
11496 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11497 MVT::i8));
11498 return DAG.getNode(ISD::AND, dl, VT, SHL,
11499 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011500 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011501 if (Op.getOpcode() == ISD::SRL) {
11502 // Make a large shift.
11503 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11504 DAG.getConstant(ShiftAmt, MVT::i32));
11505 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11506 // Zero out the leftmost bits.
11507 SmallVector<SDValue, 16> V(16,
11508 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11509 MVT::i8));
11510 return DAG.getNode(ISD::AND, dl, VT, SRL,
11511 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11512 }
11513 if (Op.getOpcode() == ISD::SRA) {
11514 if (ShiftAmt == 7) {
11515 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011516 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011517 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011518 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011519
Craig Toppered2e13d2012-01-22 19:15:14 +000011520 // R s>> a === ((R u>> a) ^ m) - m
11521 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11522 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11523 MVT::i8));
11524 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11525 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11526 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11527 return Res;
11528 }
Craig Topper731dfd02012-04-23 03:42:40 +000011529 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011530 }
Craig Topper46154eb2011-11-11 07:39:23 +000011531
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011532 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011533 if (Op.getOpcode() == ISD::SHL) {
11534 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011535 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11536 DAG.getConstant(ShiftAmt, MVT::i32));
11537 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011538 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011539 SmallVector<SDValue, 32> V(32,
11540 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11541 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011542 return DAG.getNode(ISD::AND, dl, VT, SHL,
11543 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011544 }
Craig Topper0d86d462011-11-20 00:12:05 +000011545 if (Op.getOpcode() == ISD::SRL) {
11546 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011547 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11548 DAG.getConstant(ShiftAmt, MVT::i32));
11549 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011550 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011551 SmallVector<SDValue, 32> V(32,
11552 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11553 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011554 return DAG.getNode(ISD::AND, dl, VT, SRL,
11555 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11556 }
11557 if (Op.getOpcode() == ISD::SRA) {
11558 if (ShiftAmt == 7) {
11559 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011560 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011561 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011562 }
11563
11564 // R s>> a === ((R u>> a) ^ m) - m
11565 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11566 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11567 MVT::i8));
11568 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11569 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11570 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11571 return Res;
11572 }
Craig Topper731dfd02012-04-23 03:42:40 +000011573 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011574 }
Nadav Rotem43012222011-05-11 08:12:09 +000011575 }
11576 }
11577
11578 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011579 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011580 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011581
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011582 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011583 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011584 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11585 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11586 }
Nadav Rotem43012222011-05-11 08:12:09 +000011587 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011588 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011589
Nate Begeman51409212010-07-28 00:21:48 +000011590 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011591 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011592 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011593
Lang Hames8b99c1e2011-12-17 01:08:46 +000011594 // Turn 'a' into a mask suitable for VSELECT
11595 SDValue VSelM = DAG.getConstant(0x80, VT);
11596 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011597 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011598
Lang Hames8b99c1e2011-12-17 01:08:46 +000011599 SDValue CM1 = DAG.getConstant(0x0f, VT);
11600 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011601
Lang Hames8b99c1e2011-12-17 01:08:46 +000011602 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11603 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011604 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11605 DAG.getConstant(4, MVT::i32), DAG);
11606 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011607 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11608
Nate Begeman51409212010-07-28 00:21:48 +000011609 // a += a
11610 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011611 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011612 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011613
Lang Hames8b99c1e2011-12-17 01:08:46 +000011614 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11615 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011616 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11617 DAG.getConstant(2, MVT::i32), DAG);
11618 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011619 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11620
Nate Begeman51409212010-07-28 00:21:48 +000011621 // a += a
11622 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011623 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011624 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011625
Lang Hames8b99c1e2011-12-17 01:08:46 +000011626 // return VSELECT(r, r+r, a);
11627 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011628 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011629 return R;
11630 }
Craig Topper46154eb2011-11-11 07:39:23 +000011631
11632 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011633 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011634 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011635 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11636 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11637
11638 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011639 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11640 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011641
11642 // Recreate the shift amount vectors
11643 SDValue Amt1, Amt2;
11644 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11645 // Constant shift amount
11646 SmallVector<SDValue, 4> Amt1Csts;
11647 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011648 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011649 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011650 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011651 Amt2Csts.push_back(Amt->getOperand(i));
11652
11653 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11654 &Amt1Csts[0], NumElems/2);
11655 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11656 &Amt2Csts[0], NumElems/2);
11657 } else {
11658 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011659 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11660 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011661 }
11662
11663 // Issue new vector shifts for the smaller types
11664 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11665 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11666
11667 // Concatenate the result back
11668 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11669 }
11670
Nate Begeman51409212010-07-28 00:21:48 +000011671 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011672}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011673
Craig Topper55b24052012-09-11 06:15:32 +000011674static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011675 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11676 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011677 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11678 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011679 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011680 SDValue LHS = N->getOperand(0);
11681 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011682 unsigned BaseOp = 0;
11683 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011684 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011685 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011686 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011687 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011688 // A subtract of one will be selected as a INC. Note that INC doesn't
11689 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011690 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11691 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011692 BaseOp = X86ISD::INC;
11693 Cond = X86::COND_O;
11694 break;
11695 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011696 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011697 Cond = X86::COND_O;
11698 break;
11699 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011700 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011701 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011702 break;
11703 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011704 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11705 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011706 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11707 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011708 BaseOp = X86ISD::DEC;
11709 Cond = X86::COND_O;
11710 break;
11711 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011712 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011713 Cond = X86::COND_O;
11714 break;
11715 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011716 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011717 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011718 break;
11719 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011720 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011721 Cond = X86::COND_O;
11722 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011723 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11724 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11725 MVT::i32);
11726 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011727
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011728 SDValue SetCC =
11729 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11730 DAG.getConstant(X86::COND_O, MVT::i32),
11731 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011732
Dan Gohman6e5fda22011-07-22 18:45:15 +000011733 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011734 }
Bill Wendling74c37652008-12-09 22:08:41 +000011735 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011736
Bill Wendling61edeb52008-12-02 01:06:39 +000011737 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011738 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011739 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011740
Bill Wendling61edeb52008-12-02 01:06:39 +000011741 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011742 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11743 DAG.getConstant(Cond, MVT::i32),
11744 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011745
Dan Gohman6e5fda22011-07-22 18:45:15 +000011746 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011747}
11748
Chad Rosier30450e82011-12-22 22:35:21 +000011749SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11750 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011751 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011752 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11753 EVT VT = Op.getValueType();
11754
Craig Toppered2e13d2012-01-22 19:15:14 +000011755 if (!Subtarget->hasSSE2() || !VT.isVector())
11756 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011757
Craig Toppered2e13d2012-01-22 19:15:14 +000011758 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11759 ExtraVT.getScalarType().getSizeInBits();
11760 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11761
11762 switch (VT.getSimpleVT().SimpleTy) {
11763 default: return SDValue();
11764 case MVT::v8i32:
11765 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011766 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011767 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011768 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011769 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011770 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011771
Craig Toppered2e13d2012-01-22 19:15:14 +000011772 // Extract the LHS vectors
11773 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011774 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11775 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011776
Craig Toppered2e13d2012-01-22 19:15:14 +000011777 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11778 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011779
Craig Toppered2e13d2012-01-22 19:15:14 +000011780 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011781 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011782 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11783 ExtraNumElems/2);
11784 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011785
Craig Toppered2e13d2012-01-22 19:15:14 +000011786 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11787 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011788
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011789 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011790 }
11791 // fall through
11792 case MVT::v4i32:
11793 case MVT::v8i16: {
11794 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11795 Op.getOperand(0), ShAmt, DAG);
11796 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011797 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011798 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011799}
11800
Craig Topper55b24052012-09-11 06:15:32 +000011801static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11802 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011803 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011804
Eric Christopher77ed1352011-07-08 00:04:56 +000011805 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11806 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011807 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011808 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011809 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011810 SDValue Ops[] = {
11811 DAG.getRegister(X86::ESP, MVT::i32), // Base
11812 DAG.getTargetConstant(1, MVT::i8), // Scale
11813 DAG.getRegister(0, MVT::i32), // Index
11814 DAG.getTargetConstant(0, MVT::i32), // Disp
11815 DAG.getRegister(0, MVT::i32), // Segment.
11816 Zero,
11817 Chain
11818 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011819 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011820 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11821 array_lengthof(Ops));
11822 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011823 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011824
Eric Christopher9a9d2752010-07-22 02:48:34 +000011825 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011826 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011827 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011828
Chris Lattner132929a2010-08-14 17:26:09 +000011829 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11830 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11831 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11832 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011833
Chris Lattner132929a2010-08-14 17:26:09 +000011834 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11835 if (!Op1 && !Op2 && !Op3 && Op4)
11836 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011837
Chris Lattner132929a2010-08-14 17:26:09 +000011838 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11839 if (Op1 && !Op2 && !Op3 && !Op4)
11840 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011841
11842 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011843 // (MFENCE)>;
11844 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011845}
11846
Craig Topper55b24052012-09-11 06:15:32 +000011847static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11848 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011849 DebugLoc dl = Op.getDebugLoc();
11850 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11851 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11852 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11853 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11854
11855 // The only fence that needs an instruction is a sequentially-consistent
11856 // cross-thread fence.
11857 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11858 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11859 // no-sse2). There isn't any reason to disable it if the target processor
11860 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011861 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011862 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11863
11864 SDValue Chain = Op.getOperand(0);
11865 SDValue Zero = DAG.getConstant(0, MVT::i32);
11866 SDValue Ops[] = {
11867 DAG.getRegister(X86::ESP, MVT::i32), // Base
11868 DAG.getTargetConstant(1, MVT::i8), // Scale
11869 DAG.getRegister(0, MVT::i32), // Index
11870 DAG.getTargetConstant(0, MVT::i32), // Disp
11871 DAG.getRegister(0, MVT::i32), // Segment.
11872 Zero,
11873 Chain
11874 };
11875 SDNode *Res =
11876 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11877 array_lengthof(Ops));
11878 return SDValue(Res, 0);
11879 }
11880
11881 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11882 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11883}
11884
Craig Topper55b24052012-09-11 06:15:32 +000011885static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11886 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011887 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011888 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011889 unsigned Reg = 0;
11890 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011891 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011892 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011893 case MVT::i8: Reg = X86::AL; size = 1; break;
11894 case MVT::i16: Reg = X86::AX; size = 2; break;
11895 case MVT::i32: Reg = X86::EAX; size = 4; break;
11896 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011897 assert(Subtarget->is64Bit() && "Node not type legal!");
11898 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011899 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011900 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011901 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011902 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011903 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011904 Op.getOperand(1),
11905 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011906 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011907 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011908 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011909 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11910 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11911 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011912 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011913 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011914 return cpOut;
11915}
11916
Craig Topper55b24052012-09-11 06:15:32 +000011917static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11918 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011919 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011920 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011921 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011922 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011923 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011924 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11925 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011926 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011927 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11928 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011929 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011930 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011931 rdx.getValue(1)
11932 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011933 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011934}
11935
Craig Topper55b24052012-09-11 06:15:32 +000011936SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011937 EVT SrcVT = Op.getOperand(0).getValueType();
11938 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011939 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011940 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011941 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011942 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011943 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011944 // i64 <=> MMX conversions are Legal.
11945 if (SrcVT==MVT::i64 && DstVT.isVector())
11946 return Op;
11947 if (DstVT==MVT::i64 && SrcVT.isVector())
11948 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011949 // MMX <=> MMX conversions are Legal.
11950 if (SrcVT.isVector() && DstVT.isVector())
11951 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011952 // All other conversions need to be expanded.
11953 return SDValue();
11954}
Chris Lattner5b856542010-12-20 00:59:46 +000011955
Craig Topper55b24052012-09-11 06:15:32 +000011956static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011957 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011958 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011959 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011960 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011961 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011962 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011963 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011964 Node->getOperand(0),
11965 Node->getOperand(1), negOp,
11966 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011967 cast<AtomicSDNode>(Node)->getAlignment(),
11968 cast<AtomicSDNode>(Node)->getOrdering(),
11969 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011970}
11971
Eli Friedman327236c2011-08-24 20:50:09 +000011972static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11973 SDNode *Node = Op.getNode();
11974 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011975 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011976
11977 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011978 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11979 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11980 // (The only way to get a 16-byte store is cmpxchg16b)
11981 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11982 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11983 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011984 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11985 cast<AtomicSDNode>(Node)->getMemoryVT(),
11986 Node->getOperand(0),
11987 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011988 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011989 cast<AtomicSDNode>(Node)->getOrdering(),
11990 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011991 return Swap.getValue(1);
11992 }
11993 // Other atomic stores have a simple pattern.
11994 return Op;
11995}
11996
Chris Lattner5b856542010-12-20 00:59:46 +000011997static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11998 EVT VT = Op.getNode()->getValueType(0);
11999
12000 // Let legalize expand this if it isn't a legal type yet.
12001 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12002 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012003
Chris Lattner5b856542010-12-20 00:59:46 +000012004 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012005
Chris Lattner5b856542010-12-20 00:59:46 +000012006 unsigned Opc;
12007 bool ExtraOp = false;
12008 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012009 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012010 case ISD::ADDC: Opc = X86ISD::ADD; break;
12011 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12012 case ISD::SUBC: Opc = X86ISD::SUB; break;
12013 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12014 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012015
Chris Lattner5b856542010-12-20 00:59:46 +000012016 if (!ExtraOp)
12017 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12018 Op.getOperand(1));
12019 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12020 Op.getOperand(1), Op.getOperand(2));
12021}
12022
Evan Cheng8688a582013-01-29 02:32:37 +000012023SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012024 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012025
Evan Cheng8688a582013-01-29 02:32:37 +000012026 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12027 // which returns the values in two XMM registers.
12028 DebugLoc dl = Op.getDebugLoc();
12029 SDValue Arg = Op.getOperand(0);
12030 EVT ArgVT = Arg.getValueType();
12031 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012032
Evan Cheng8688a582013-01-29 02:32:37 +000012033 ArgListTy Args;
12034 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012035
Evan Cheng8688a582013-01-29 02:32:37 +000012036 Entry.Node = Arg;
12037 Entry.Ty = ArgTy;
12038 Entry.isSExt = false;
12039 Entry.isZExt = false;
12040 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012041
12042 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12043 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12044 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012045 const char *LibcallName = (ArgVT == MVT::f64)
12046 ? "__sincos_stret" : "__sincosf_stret";
12047 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012048
Evan Cheng8688a582013-01-29 02:32:37 +000012049 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12050 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012051 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12052 false, false, false, false, 0,
12053 CallingConv::C, /*isTaillCall=*/false,
12054 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12055 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012056 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012057 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012058}
12059
Evan Cheng0db9fe62006-04-25 20:13:52 +000012060/// LowerOperation - Provide custom lowering hooks for some operations.
12061///
Dan Gohmand858e902010-04-17 15:26:15 +000012062SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012063 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012064 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012065 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012066 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12067 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12068 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012069 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012070 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012071 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012072 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012073 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12074 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12075 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012076 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12077 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012078 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12079 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12080 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012081 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012082 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012083 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012084 case ISD::SHL_PARTS:
12085 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012086 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012087 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012088 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012089 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012090 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12091 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12092 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012093 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012094 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012095 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012096 case ISD::FABS: return LowerFABS(Op, DAG);
12097 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012098 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012099 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012100 case ISD::SETCC: return LowerSETCC(Op, DAG);
12101 case ISD::SELECT: return LowerSELECT(Op, DAG);
12102 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012103 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012104 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012105 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012106 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012107 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012108 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012109 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12110 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012111 case ISD::FRAME_TO_ARGS_OFFSET:
12112 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012113 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012114 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012115 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12116 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012117 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12118 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012119 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012120 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012121 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012122 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012123 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012124 case ISD::SRA:
12125 case ISD::SRL:
12126 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012127 case ISD::SADDO:
12128 case ISD::UADDO:
12129 case ISD::SSUBO:
12130 case ISD::USUBO:
12131 case ISD::SMULO:
12132 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012133 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012134 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012135 case ISD::ADDC:
12136 case ISD::ADDE:
12137 case ISD::SUBC:
12138 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012139 case ISD::ADD: return LowerADD(Op, DAG);
12140 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012141 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012142 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012143 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012144}
12145
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012146static void ReplaceATOMIC_LOAD(SDNode *Node,
12147 SmallVectorImpl<SDValue> &Results,
12148 SelectionDAG &DAG) {
12149 DebugLoc dl = Node->getDebugLoc();
12150 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12151
12152 // Convert wide load -> cmpxchg8b/cmpxchg16b
12153 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12154 // (The only way to get a 16-byte load is cmpxchg16b)
12155 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012156 SDValue Zero = DAG.getConstant(0, VT);
12157 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012158 Node->getOperand(0),
12159 Node->getOperand(1), Zero, Zero,
12160 cast<AtomicSDNode>(Node)->getMemOperand(),
12161 cast<AtomicSDNode>(Node)->getOrdering(),
12162 cast<AtomicSDNode>(Node)->getSynchScope());
12163 Results.push_back(Swap.getValue(0));
12164 Results.push_back(Swap.getValue(1));
12165}
12166
Craig Topperc0878702012-08-17 06:55:11 +000012167static void
Duncan Sands1607f052008-12-01 11:39:25 +000012168ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012169 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012170 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012171 assert (Node->getValueType(0) == MVT::i64 &&
12172 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012173
12174 SDValue Chain = Node->getOperand(0);
12175 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012176 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012177 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012178 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012179 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012180 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012181 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012182 SDValue Result =
12183 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12184 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012185 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012186 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012187 Results.push_back(Result.getValue(2));
12188}
12189
Duncan Sands126d9072008-07-04 11:47:58 +000012190/// ReplaceNodeResults - Replace a node with an illegal result type
12191/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012192void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12193 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012194 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012195 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012196 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012197 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012198 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012199 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012200 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012201 case ISD::ADDC:
12202 case ISD::ADDE:
12203 case ISD::SUBC:
12204 case ISD::SUBE:
12205 // We don't want to expand or promote these.
12206 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012207 case ISD::FP_TO_SINT:
12208 case ISD::FP_TO_UINT: {
12209 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12210
12211 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12212 return;
12213
Eli Friedman948e95a2009-05-23 09:59:16 +000012214 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012215 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012216 SDValue FIST = Vals.first, StackSlot = Vals.second;
12217 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012218 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012219 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012220 if (StackSlot.getNode() != 0)
12221 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12222 MachinePointerInfo(),
12223 false, false, false, 0));
12224 else
12225 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012226 }
12227 return;
12228 }
Michael Liao991b6a22012-10-24 04:09:32 +000012229 case ISD::UINT_TO_FP: {
12230 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12231 N->getValueType(0) != MVT::v2f32)
12232 return;
12233 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12234 N->getOperand(0));
12235 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12236 MVT::f64);
12237 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12238 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12239 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12240 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12241 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12242 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12243 return;
12244 }
Michael Liao44c2d612012-10-10 16:53:28 +000012245 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012246 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12247 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012248 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12249 Results.push_back(V);
12250 return;
12251 }
Duncan Sands1607f052008-12-01 11:39:25 +000012252 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012253 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012254 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012255 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012256 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012257 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012258 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012259 eax.getValue(2));
12260 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12261 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012262 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012263 Results.push_back(edx.getValue(1));
12264 return;
12265 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012266 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012267 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012268 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012269 bool Regs64bit = T == MVT::i128;
12270 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012271 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012272 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12273 DAG.getConstant(0, HalfT));
12274 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12275 DAG.getConstant(1, HalfT));
12276 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12277 Regs64bit ? X86::RAX : X86::EAX,
12278 cpInL, SDValue());
12279 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12280 Regs64bit ? X86::RDX : X86::EDX,
12281 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012282 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012283 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12284 DAG.getConstant(0, HalfT));
12285 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12286 DAG.getConstant(1, HalfT));
12287 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12288 Regs64bit ? X86::RBX : X86::EBX,
12289 swapInL, cpInH.getValue(1));
12290 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012291 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012292 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012293 SDValue Ops[] = { swapInH.getValue(0),
12294 N->getOperand(1),
12295 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012296 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012297 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012298 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12299 X86ISD::LCMPXCHG8_DAG;
12300 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012301 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012302 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12303 Regs64bit ? X86::RAX : X86::EAX,
12304 HalfT, Result.getValue(1));
12305 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12306 Regs64bit ? X86::RDX : X86::EDX,
12307 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012308 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012309 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012310 Results.push_back(cpOutH.getValue(1));
12311 return;
12312 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012313 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012314 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012315 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012316 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012317 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012318 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012319 case ISD::ATOMIC_LOAD_MAX:
12320 case ISD::ATOMIC_LOAD_MIN:
12321 case ISD::ATOMIC_LOAD_UMAX:
12322 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012323 case ISD::ATOMIC_SWAP: {
12324 unsigned Opc;
12325 switch (N->getOpcode()) {
12326 default: llvm_unreachable("Unexpected opcode");
12327 case ISD::ATOMIC_LOAD_ADD:
12328 Opc = X86ISD::ATOMADD64_DAG;
12329 break;
12330 case ISD::ATOMIC_LOAD_AND:
12331 Opc = X86ISD::ATOMAND64_DAG;
12332 break;
12333 case ISD::ATOMIC_LOAD_NAND:
12334 Opc = X86ISD::ATOMNAND64_DAG;
12335 break;
12336 case ISD::ATOMIC_LOAD_OR:
12337 Opc = X86ISD::ATOMOR64_DAG;
12338 break;
12339 case ISD::ATOMIC_LOAD_SUB:
12340 Opc = X86ISD::ATOMSUB64_DAG;
12341 break;
12342 case ISD::ATOMIC_LOAD_XOR:
12343 Opc = X86ISD::ATOMXOR64_DAG;
12344 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012345 case ISD::ATOMIC_LOAD_MAX:
12346 Opc = X86ISD::ATOMMAX64_DAG;
12347 break;
12348 case ISD::ATOMIC_LOAD_MIN:
12349 Opc = X86ISD::ATOMMIN64_DAG;
12350 break;
12351 case ISD::ATOMIC_LOAD_UMAX:
12352 Opc = X86ISD::ATOMUMAX64_DAG;
12353 break;
12354 case ISD::ATOMIC_LOAD_UMIN:
12355 Opc = X86ISD::ATOMUMIN64_DAG;
12356 break;
Craig Topperc0878702012-08-17 06:55:11 +000012357 case ISD::ATOMIC_SWAP:
12358 Opc = X86ISD::ATOMSWAP64_DAG;
12359 break;
12360 }
12361 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012362 return;
Craig Topperc0878702012-08-17 06:55:11 +000012363 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012364 case ISD::ATOMIC_LOAD:
12365 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012366 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012367}
12368
Evan Cheng72261582005-12-20 06:22:03 +000012369const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12370 switch (Opcode) {
12371 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012372 case X86ISD::BSF: return "X86ISD::BSF";
12373 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012374 case X86ISD::SHLD: return "X86ISD::SHLD";
12375 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012376 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012377 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012378 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012379 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012380 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012381 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012382 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12383 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12384 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012385 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012386 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012387 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012388 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012389 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012390 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012391 case X86ISD::COMI: return "X86ISD::COMI";
12392 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012393 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012394 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012395 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12396 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012397 case X86ISD::CMOV: return "X86ISD::CMOV";
12398 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012399 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012400 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12401 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012402 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012403 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012404 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012405 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012406 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012407 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12408 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012409 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012410 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012411 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012412 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012413 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012414 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012415 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012416 case X86ISD::HADD: return "X86ISD::HADD";
12417 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012418 case X86ISD::FHADD: return "X86ISD::FHADD";
12419 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012420 case X86ISD::UMAX: return "X86ISD::UMAX";
12421 case X86ISD::UMIN: return "X86ISD::UMIN";
12422 case X86ISD::SMAX: return "X86ISD::SMAX";
12423 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012424 case X86ISD::FMAX: return "X86ISD::FMAX";
12425 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012426 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12427 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012428 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12429 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012430 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012431 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012432 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012433 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12434 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012435 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012436 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012437 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012438 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012439 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12440 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012441 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12442 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12443 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12444 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12445 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12446 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012447 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012448 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012449 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012450 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12451 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012452 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012453 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012454 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12455 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012456 case X86ISD::VSHL: return "X86ISD::VSHL";
12457 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012458 case X86ISD::VSRA: return "X86ISD::VSRA";
12459 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12460 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12461 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012462 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012463 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12464 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012465 case X86ISD::ADD: return "X86ISD::ADD";
12466 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012467 case X86ISD::ADC: return "X86ISD::ADC";
12468 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012469 case X86ISD::SMUL: return "X86ISD::SMUL";
12470 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012471 case X86ISD::INC: return "X86ISD::INC";
12472 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012473 case X86ISD::OR: return "X86ISD::OR";
12474 case X86ISD::XOR: return "X86ISD::XOR";
12475 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012476 case X86ISD::BLSI: return "X86ISD::BLSI";
12477 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12478 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012479 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012480 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012481 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012482 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012483 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12484 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012485 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012486 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012487 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012488 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012489 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012490 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12491 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012492 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12493 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12494 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012495 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12496 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012497 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12498 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012499 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012500 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012501 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012502 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12503 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012504 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012505 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012506 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012507 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012508 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012509 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012510 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012511 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012512 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012513 case X86ISD::FMADD: return "X86ISD::FMADD";
12514 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12515 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12516 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12517 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12518 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012519 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12520 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012521 }
12522}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012523
Chris Lattnerc9addb72007-03-30 23:15:24 +000012524// isLegalAddressingMode - Return true if the addressing mode represented
12525// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012526bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012527 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012528 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012529 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012530 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012531
Chris Lattnerc9addb72007-03-30 23:15:24 +000012532 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012533 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012534 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012535
Chris Lattnerc9addb72007-03-30 23:15:24 +000012536 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012537 unsigned GVFlags =
12538 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012539
Chris Lattnerdfed4132009-07-10 07:38:24 +000012540 // If a reference to this global requires an extra load, we can't fold it.
12541 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012542 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012543
Chris Lattnerdfed4132009-07-10 07:38:24 +000012544 // If BaseGV requires a register for the PIC base, we cannot also have a
12545 // BaseReg specified.
12546 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012547 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012548
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012549 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012550 if ((M != CodeModel::Small || R != Reloc::Static) &&
12551 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012552 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012553 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012554
Chris Lattnerc9addb72007-03-30 23:15:24 +000012555 switch (AM.Scale) {
12556 case 0:
12557 case 1:
12558 case 2:
12559 case 4:
12560 case 8:
12561 // These scales always work.
12562 break;
12563 case 3:
12564 case 5:
12565 case 9:
12566 // These scales are formed with basereg+scalereg. Only accept if there is
12567 // no basereg yet.
12568 if (AM.HasBaseReg)
12569 return false;
12570 break;
12571 default: // Other stuff never works.
12572 return false;
12573 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012574
Chris Lattnerc9addb72007-03-30 23:15:24 +000012575 return true;
12576}
12577
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012578bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012579 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012580 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012581 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12582 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012583 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012584}
12585
Evan Cheng70e10d32012-07-17 06:53:39 +000012586bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012587 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012588}
12589
12590bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012591 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012592 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012593}
12594
Owen Andersone50ed302009-08-10 22:56:29 +000012595bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012596 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012597 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012598 unsigned NumBits1 = VT1.getSizeInBits();
12599 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012600 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012601}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012602
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012603bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012604 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012605 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012606}
12607
Owen Andersone50ed302009-08-10 22:56:29 +000012608bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012609 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012610 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012611}
12612
Evan Cheng2766a472012-12-06 19:13:27 +000012613bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12614 EVT VT1 = Val.getValueType();
12615 if (isZExtFree(VT1, VT2))
12616 return true;
12617
12618 if (Val.getOpcode() != ISD::LOAD)
12619 return false;
12620
12621 if (!VT1.isSimple() || !VT1.isInteger() ||
12622 !VT2.isSimple() || !VT2.isInteger())
12623 return false;
12624
12625 switch (VT1.getSimpleVT().SimpleTy) {
12626 default: break;
12627 case MVT::i8:
12628 case MVT::i16:
12629 case MVT::i32:
12630 // X86 has 8, 16, and 32-bit zero-extending loads.
12631 return true;
12632 }
12633
12634 return false;
12635}
12636
Owen Andersone50ed302009-08-10 22:56:29 +000012637bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012638 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012639 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012640}
12641
Evan Cheng60c07e12006-07-05 22:17:51 +000012642/// isShuffleMaskLegal - Targets can use this to indicate that they only
12643/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12644/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12645/// are assumed to be legal.
12646bool
Eric Christopherfd179292009-08-27 18:07:15 +000012647X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012648 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012649 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012650 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012651 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012652
Nate Begemana09008b2009-10-19 02:17:23 +000012653 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012654 return (VT.getVectorNumElements() == 2 ||
12655 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12656 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012657 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012658 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012659 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12660 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012661 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012662 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12663 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12664 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12665 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012666}
12667
Dan Gohman7d8143f2008-04-09 20:09:42 +000012668bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012669X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012670 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012671 unsigned NumElts = VT.getVectorNumElements();
12672 // FIXME: This collection of masks seems suspect.
12673 if (NumElts == 2)
12674 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012675 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012676 return (isMOVLMask(Mask, VT) ||
12677 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012678 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12679 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012680 }
12681 return false;
12682}
12683
12684//===----------------------------------------------------------------------===//
12685// X86 Scheduler Hooks
12686//===----------------------------------------------------------------------===//
12687
Michael Liaobe02a902012-11-08 07:28:54 +000012688/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012689static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12690 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012691 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012692
12693 const BasicBlock *BB = MBB->getBasicBlock();
12694 MachineFunction::iterator I = MBB;
12695 ++I;
12696
12697 // For the v = xbegin(), we generate
12698 //
12699 // thisMBB:
12700 // xbegin sinkMBB
12701 //
12702 // mainMBB:
12703 // eax = -1
12704 //
12705 // sinkMBB:
12706 // v = eax
12707
12708 MachineBasicBlock *thisMBB = MBB;
12709 MachineFunction *MF = MBB->getParent();
12710 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12711 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12712 MF->insert(I, mainMBB);
12713 MF->insert(I, sinkMBB);
12714
12715 // Transfer the remainder of BB and its successor edges to sinkMBB.
12716 sinkMBB->splice(sinkMBB->begin(), MBB,
12717 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12718 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12719
12720 // thisMBB:
12721 // xbegin sinkMBB
12722 // # fallthrough to mainMBB
12723 // # abortion to sinkMBB
12724 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12725 thisMBB->addSuccessor(mainMBB);
12726 thisMBB->addSuccessor(sinkMBB);
12727
12728 // mainMBB:
12729 // EAX = -1
12730 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12731 mainMBB->addSuccessor(sinkMBB);
12732
12733 // sinkMBB:
12734 // EAX is live into the sinkMBB
12735 sinkMBB->addLiveIn(X86::EAX);
12736 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12737 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12738 .addReg(X86::EAX);
12739
12740 MI->eraseFromParent();
12741 return sinkMBB;
12742}
12743
Michael Liaob118a072012-09-20 03:06:15 +000012744// Get CMPXCHG opcode for the specified data type.
12745static unsigned getCmpXChgOpcode(EVT VT) {
12746 switch (VT.getSimpleVT().SimpleTy) {
12747 case MVT::i8: return X86::LCMPXCHG8;
12748 case MVT::i16: return X86::LCMPXCHG16;
12749 case MVT::i32: return X86::LCMPXCHG32;
12750 case MVT::i64: return X86::LCMPXCHG64;
12751 default:
12752 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012753 }
Michael Liaob118a072012-09-20 03:06:15 +000012754 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012755}
12756
Michael Liaob118a072012-09-20 03:06:15 +000012757// Get LOAD opcode for the specified data type.
12758static unsigned getLoadOpcode(EVT VT) {
12759 switch (VT.getSimpleVT().SimpleTy) {
12760 case MVT::i8: return X86::MOV8rm;
12761 case MVT::i16: return X86::MOV16rm;
12762 case MVT::i32: return X86::MOV32rm;
12763 case MVT::i64: return X86::MOV64rm;
12764 default:
12765 break;
12766 }
12767 llvm_unreachable("Invalid operand size!");
12768}
12769
12770// Get opcode of the non-atomic one from the specified atomic instruction.
12771static unsigned getNonAtomicOpcode(unsigned Opc) {
12772 switch (Opc) {
12773 case X86::ATOMAND8: return X86::AND8rr;
12774 case X86::ATOMAND16: return X86::AND16rr;
12775 case X86::ATOMAND32: return X86::AND32rr;
12776 case X86::ATOMAND64: return X86::AND64rr;
12777 case X86::ATOMOR8: return X86::OR8rr;
12778 case X86::ATOMOR16: return X86::OR16rr;
12779 case X86::ATOMOR32: return X86::OR32rr;
12780 case X86::ATOMOR64: return X86::OR64rr;
12781 case X86::ATOMXOR8: return X86::XOR8rr;
12782 case X86::ATOMXOR16: return X86::XOR16rr;
12783 case X86::ATOMXOR32: return X86::XOR32rr;
12784 case X86::ATOMXOR64: return X86::XOR64rr;
12785 }
12786 llvm_unreachable("Unhandled atomic-load-op opcode!");
12787}
12788
12789// Get opcode of the non-atomic one from the specified atomic instruction with
12790// extra opcode.
12791static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12792 unsigned &ExtraOpc) {
12793 switch (Opc) {
12794 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12795 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12796 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12797 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012798 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012799 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12800 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12801 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012802 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012803 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12804 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12805 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012806 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012807 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12808 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12809 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012810 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012811 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12812 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12813 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12814 }
12815 llvm_unreachable("Unhandled atomic-load-op opcode!");
12816}
12817
12818// Get opcode of the non-atomic one from the specified atomic instruction for
12819// 64-bit data type on 32-bit target.
12820static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12821 switch (Opc) {
12822 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12823 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12824 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12825 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12826 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12827 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012828 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12829 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12830 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12831 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012832 }
12833 llvm_unreachable("Unhandled atomic-load-op opcode!");
12834}
12835
12836// Get opcode of the non-atomic one from the specified atomic instruction for
12837// 64-bit data type on 32-bit target with extra opcode.
12838static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12839 unsigned &HiOpc,
12840 unsigned &ExtraOpc) {
12841 switch (Opc) {
12842 case X86::ATOMNAND6432:
12843 ExtraOpc = X86::NOT32r;
12844 HiOpc = X86::AND32rr;
12845 return X86::AND32rr;
12846 }
12847 llvm_unreachable("Unhandled atomic-load-op opcode!");
12848}
12849
12850// Get pseudo CMOV opcode from the specified data type.
12851static unsigned getPseudoCMOVOpc(EVT VT) {
12852 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012853 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012854 case MVT::i16: return X86::CMOV_GR16;
12855 case MVT::i32: return X86::CMOV_GR32;
12856 default:
12857 break;
12858 }
12859 llvm_unreachable("Unknown CMOV opcode!");
12860}
12861
12862// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12863// They will be translated into a spin-loop or compare-exchange loop from
12864//
12865// ...
12866// dst = atomic-fetch-op MI.addr, MI.val
12867// ...
12868//
12869// to
12870//
12871// ...
12872// EAX = LOAD MI.addr
12873// loop:
12874// t1 = OP MI.val, EAX
12875// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12876// JNE loop
12877// sink:
12878// dst = EAX
12879// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012880MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012881X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12882 MachineBasicBlock *MBB) const {
12883 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12884 DebugLoc DL = MI->getDebugLoc();
12885
12886 MachineFunction *MF = MBB->getParent();
12887 MachineRegisterInfo &MRI = MF->getRegInfo();
12888
12889 const BasicBlock *BB = MBB->getBasicBlock();
12890 MachineFunction::iterator I = MBB;
12891 ++I;
12892
Michael Liao13d08bf2013-01-22 21:47:38 +000012893 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000012894 "Unexpected number of operands");
12895
12896 assert(MI->hasOneMemOperand() &&
12897 "Expected atomic-load-op to have one memoperand");
12898
12899 // Memory Reference
12900 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12901 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12902
12903 unsigned DstReg, SrcReg;
12904 unsigned MemOpndSlot;
12905
12906 unsigned CurOp = 0;
12907
12908 DstReg = MI->getOperand(CurOp++).getReg();
12909 MemOpndSlot = CurOp;
12910 CurOp += X86::AddrNumOperands;
12911 SrcReg = MI->getOperand(CurOp++).getReg();
12912
12913 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012914 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012915 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12916
12917 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12918 unsigned LOADOpc = getLoadOpcode(VT);
12919
12920 // For the atomic load-arith operator, we generate
12921 //
12922 // thisMBB:
12923 // EAX = LOAD [MI.addr]
12924 // mainMBB:
12925 // t1 = OP MI.val, EAX
12926 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12927 // JNE mainMBB
12928 // sinkMBB:
12929
12930 MachineBasicBlock *thisMBB = MBB;
12931 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12932 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12933 MF->insert(I, mainMBB);
12934 MF->insert(I, sinkMBB);
12935
12936 MachineInstrBuilder MIB;
12937
12938 // Transfer the remainder of BB and its successor edges to sinkMBB.
12939 sinkMBB->splice(sinkMBB->begin(), MBB,
12940 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12941 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12942
12943 // thisMBB:
12944 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12945 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12946 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12947 MIB.setMemRefs(MMOBegin, MMOEnd);
12948
12949 thisMBB->addSuccessor(mainMBB);
12950
12951 // mainMBB:
12952 MachineBasicBlock *origMainMBB = mainMBB;
12953 mainMBB->addLiveIn(AccPhyReg);
12954
12955 // Copy AccPhyReg as it is used more than once.
12956 unsigned AccReg = MRI.createVirtualRegister(RC);
12957 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12958 .addReg(AccPhyReg);
12959
12960 unsigned t1 = MRI.createVirtualRegister(RC);
12961 unsigned Opc = MI->getOpcode();
12962 switch (Opc) {
12963 default:
12964 llvm_unreachable("Unhandled atomic-load-op opcode!");
12965 case X86::ATOMAND8:
12966 case X86::ATOMAND16:
12967 case X86::ATOMAND32:
12968 case X86::ATOMAND64:
12969 case X86::ATOMOR8:
12970 case X86::ATOMOR16:
12971 case X86::ATOMOR32:
12972 case X86::ATOMOR64:
12973 case X86::ATOMXOR8:
12974 case X86::ATOMXOR16:
12975 case X86::ATOMXOR32:
12976 case X86::ATOMXOR64: {
12977 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12978 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12979 .addReg(AccReg);
12980 break;
12981 }
12982 case X86::ATOMNAND8:
12983 case X86::ATOMNAND16:
12984 case X86::ATOMNAND32:
12985 case X86::ATOMNAND64: {
12986 unsigned t2 = MRI.createVirtualRegister(RC);
12987 unsigned NOTOpc;
12988 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12989 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12990 .addReg(AccReg);
12991 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12992 break;
12993 }
Michael Liao08382492012-09-21 03:00:17 +000012994 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012995 case X86::ATOMMAX16:
12996 case X86::ATOMMAX32:
12997 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012998 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012999 case X86::ATOMMIN16:
13000 case X86::ATOMMIN32:
13001 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013002 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013003 case X86::ATOMUMAX16:
13004 case X86::ATOMUMAX32:
13005 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013006 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013007 case X86::ATOMUMIN16:
13008 case X86::ATOMUMIN32:
13009 case X86::ATOMUMIN64: {
13010 unsigned CMPOpc;
13011 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13012
13013 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13014 .addReg(SrcReg)
13015 .addReg(AccReg);
13016
13017 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013018 if (VT != MVT::i8) {
13019 // Native support
13020 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
13021 .addReg(SrcReg)
13022 .addReg(AccReg);
13023 } else {
13024 // Promote i8 to i32 to use CMOV32
13025 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
13026 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13027 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
13028 unsigned t2 = MRI.createVirtualRegister(RC32);
13029
13030 unsigned Undef = MRI.createVirtualRegister(RC32);
13031 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13032
13033 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13034 .addReg(Undef)
13035 .addReg(SrcReg)
13036 .addImm(X86::sub_8bit);
13037 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13038 .addReg(Undef)
13039 .addReg(AccReg)
13040 .addImm(X86::sub_8bit);
13041
13042 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
13043 .addReg(SrcReg32)
13044 .addReg(AccReg32);
13045
13046 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
13047 .addReg(t2, 0, X86::sub_8bit);
13048 }
Michael Liaob118a072012-09-20 03:06:15 +000013049 } else {
13050 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013051 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013052 "Invalid atomic-load-op transformation!");
13053 unsigned SelOpc = getPseudoCMOVOpc(VT);
13054 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13055 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
13056 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
13057 .addReg(SrcReg).addReg(AccReg)
13058 .addImm(CC);
13059 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13060 }
13061 break;
13062 }
13063 }
13064
13065 // Copy AccPhyReg back from virtual register.
13066 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
13067 .addReg(AccReg);
13068
13069 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13070 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13071 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13072 MIB.addReg(t1);
13073 MIB.setMemRefs(MMOBegin, MMOEnd);
13074
13075 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13076
13077 mainMBB->addSuccessor(origMainMBB);
13078 mainMBB->addSuccessor(sinkMBB);
13079
13080 // sinkMBB:
13081 sinkMBB->addLiveIn(AccPhyReg);
13082
13083 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13084 TII->get(TargetOpcode::COPY), DstReg)
13085 .addReg(AccPhyReg);
13086
13087 MI->eraseFromParent();
13088 return sinkMBB;
13089}
13090
13091// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13092// instructions. They will be translated into a spin-loop or compare-exchange
13093// loop from
13094//
13095// ...
13096// dst = atomic-fetch-op MI.addr, MI.val
13097// ...
13098//
13099// to
13100//
13101// ...
13102// EAX = LOAD [MI.addr + 0]
13103// EDX = LOAD [MI.addr + 4]
13104// loop:
13105// EBX = OP MI.val.lo, EAX
13106// ECX = OP MI.val.hi, EDX
13107// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13108// JNE loop
13109// sink:
13110// dst = EDX:EAX
13111// ...
13112MachineBasicBlock *
13113X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13114 MachineBasicBlock *MBB) const {
13115 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13116 DebugLoc DL = MI->getDebugLoc();
13117
13118 MachineFunction *MF = MBB->getParent();
13119 MachineRegisterInfo &MRI = MF->getRegInfo();
13120
13121 const BasicBlock *BB = MBB->getBasicBlock();
13122 MachineFunction::iterator I = MBB;
13123 ++I;
13124
Michael Liao13d08bf2013-01-22 21:47:38 +000013125 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013126 "Unexpected number of operands");
13127
13128 assert(MI->hasOneMemOperand() &&
13129 "Expected atomic-load-op32 to have one memoperand");
13130
13131 // Memory Reference
13132 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13133 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13134
13135 unsigned DstLoReg, DstHiReg;
13136 unsigned SrcLoReg, SrcHiReg;
13137 unsigned MemOpndSlot;
13138
13139 unsigned CurOp = 0;
13140
13141 DstLoReg = MI->getOperand(CurOp++).getReg();
13142 DstHiReg = MI->getOperand(CurOp++).getReg();
13143 MemOpndSlot = CurOp;
13144 CurOp += X86::AddrNumOperands;
13145 SrcLoReg = MI->getOperand(CurOp++).getReg();
13146 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013147
Craig Topperc9099502012-04-20 06:31:50 +000013148 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013149 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013150
Michael Liaob118a072012-09-20 03:06:15 +000013151 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13152 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013153
Michael Liaob118a072012-09-20 03:06:15 +000013154 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013155 //
Michael Liaob118a072012-09-20 03:06:15 +000013156 // thisMBB:
13157 // EAX = LOAD [MI.addr + 0]
13158 // EDX = LOAD [MI.addr + 4]
13159 // mainMBB:
13160 // EBX = OP MI.vallo, EAX
13161 // ECX = OP MI.valhi, EDX
13162 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13163 // JNE mainMBB
13164 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000013165
Mon P Wang63307c32008-05-05 19:05:59 +000013166 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013167 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13168 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13169 MF->insert(I, mainMBB);
13170 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013171
Michael Liaob118a072012-09-20 03:06:15 +000013172 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013173
Michael Liaob118a072012-09-20 03:06:15 +000013174 // Transfer the remainder of BB and its successor edges to sinkMBB.
13175 sinkMBB->splice(sinkMBB->begin(), MBB,
13176 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13177 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013178
Michael Liaob118a072012-09-20 03:06:15 +000013179 // thisMBB:
13180 // Lo
13181 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
13182 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13183 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13184 MIB.setMemRefs(MMOBegin, MMOEnd);
13185 // Hi
13186 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
13187 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000013188 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000013189 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000013190 else
Michael Liaob118a072012-09-20 03:06:15 +000013191 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13192 }
13193 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000013194
Michael Liaob118a072012-09-20 03:06:15 +000013195 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013196
Michael Liaob118a072012-09-20 03:06:15 +000013197 // mainMBB:
13198 MachineBasicBlock *origMainMBB = mainMBB;
13199 mainMBB->addLiveIn(X86::EAX);
13200 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013201
Michael Liaob118a072012-09-20 03:06:15 +000013202 // Copy EDX:EAX as they are used more than once.
13203 unsigned LoReg = MRI.createVirtualRegister(RC);
13204 unsigned HiReg = MRI.createVirtualRegister(RC);
13205 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
13206 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000013207
Michael Liaob118a072012-09-20 03:06:15 +000013208 unsigned t1L = MRI.createVirtualRegister(RC);
13209 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000013210
Michael Liaob118a072012-09-20 03:06:15 +000013211 unsigned Opc = MI->getOpcode();
13212 switch (Opc) {
13213 default:
13214 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13215 case X86::ATOMAND6432:
13216 case X86::ATOMOR6432:
13217 case X86::ATOMXOR6432:
13218 case X86::ATOMADD6432:
13219 case X86::ATOMSUB6432: {
13220 unsigned HiOpc;
13221 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000013222 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
13223 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013224 break;
13225 }
13226 case X86::ATOMNAND6432: {
13227 unsigned HiOpc, NOTOpc;
13228 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
13229 unsigned t2L = MRI.createVirtualRegister(RC);
13230 unsigned t2H = MRI.createVirtualRegister(RC);
13231 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
13232 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
13233 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
13234 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
13235 break;
13236 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013237 case X86::ATOMMAX6432:
13238 case X86::ATOMMIN6432:
13239 case X86::ATOMUMAX6432:
13240 case X86::ATOMUMIN6432: {
13241 unsigned HiOpc;
13242 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13243 unsigned cL = MRI.createVirtualRegister(RC8);
13244 unsigned cH = MRI.createVirtualRegister(RC8);
13245 unsigned cL32 = MRI.createVirtualRegister(RC);
13246 unsigned cH32 = MRI.createVirtualRegister(RC);
13247 unsigned cc = MRI.createVirtualRegister(RC);
13248 // cl := cmp src_lo, lo
13249 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13250 .addReg(SrcLoReg).addReg(LoReg);
13251 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13252 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13253 // ch := cmp src_hi, hi
13254 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13255 .addReg(SrcHiReg).addReg(HiReg);
13256 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13257 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13258 // cc := if (src_hi == hi) ? cl : ch;
13259 if (Subtarget->hasCMov()) {
13260 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13261 .addReg(cH32).addReg(cL32);
13262 } else {
13263 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13264 .addReg(cH32).addReg(cL32)
13265 .addImm(X86::COND_E);
13266 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13267 }
13268 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13269 if (Subtarget->hasCMov()) {
13270 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
13271 .addReg(SrcLoReg).addReg(LoReg);
13272 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
13273 .addReg(SrcHiReg).addReg(HiReg);
13274 } else {
13275 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
13276 .addReg(SrcLoReg).addReg(LoReg)
13277 .addImm(X86::COND_NE);
13278 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13279 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
13280 .addReg(SrcHiReg).addReg(HiReg)
13281 .addImm(X86::COND_NE);
13282 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13283 }
13284 break;
13285 }
Michael Liaob118a072012-09-20 03:06:15 +000013286 case X86::ATOMSWAP6432: {
13287 unsigned HiOpc;
13288 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13289 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13290 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13291 break;
13292 }
13293 }
Mon P Wang63307c32008-05-05 19:05:59 +000013294
Michael Liaob118a072012-09-20 03:06:15 +000013295 // Copy EDX:EAX back from HiReg:LoReg
13296 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13297 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13298 // Copy ECX:EBX from t1H:t1L
13299 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13300 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013301
Michael Liaob118a072012-09-20 03:06:15 +000013302 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13303 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13304 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13305 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013306
Michael Liaob118a072012-09-20 03:06:15 +000013307 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013308
Michael Liaob118a072012-09-20 03:06:15 +000013309 mainMBB->addSuccessor(origMainMBB);
13310 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013311
Michael Liaob118a072012-09-20 03:06:15 +000013312 // sinkMBB:
13313 sinkMBB->addLiveIn(X86::EAX);
13314 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013315
Michael Liaob118a072012-09-20 03:06:15 +000013316 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13317 TII->get(TargetOpcode::COPY), DstLoReg)
13318 .addReg(X86::EAX);
13319 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13320 TII->get(TargetOpcode::COPY), DstHiReg)
13321 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013322
Michael Liaob118a072012-09-20 03:06:15 +000013323 MI->eraseFromParent();
13324 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013325}
13326
Eric Christopherf83a5de2009-08-27 18:08:16 +000013327// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013328// or XMM0_V32I8 in AVX all of this code can be replaced with that
13329// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013330static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13331 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013332 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013333 switch (MI->getOpcode()) {
13334 default: llvm_unreachable("illegal opcode!");
13335 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13336 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13337 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13338 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13339 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13340 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13341 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13342 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013343 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013344
Craig Topper8aae8dd2012-11-10 08:57:41 +000013345 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013346 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013347
Craig Topper52ea2452012-11-10 09:25:36 +000013348 unsigned NumArgs = MI->getNumOperands();
13349 for (unsigned i = 1; i < NumArgs; ++i) {
13350 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013351 if (!(Op.isReg() && Op.isImplicit()))
13352 MIB.addOperand(Op);
13353 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013354 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013355 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13356
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013357 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013358 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013359 .addReg(X86::XMM0);
13360
Dan Gohman14152b42010-07-06 20:24:04 +000013361 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013362 return BB;
13363}
13364
Craig Topper9c7ae012012-11-10 01:23:36 +000013365// FIXME: Custom handling because TableGen doesn't support multiple implicit
13366// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013367static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13368 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013369 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013370 switch (MI->getOpcode()) {
13371 default: llvm_unreachable("illegal opcode!");
13372 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13373 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13374 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13375 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13376 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13377 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13378 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13379 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013380 }
13381
Craig Topper8aae8dd2012-11-10 08:57:41 +000013382 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013383 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013384
Craig Topper52ea2452012-11-10 09:25:36 +000013385 unsigned NumArgs = MI->getNumOperands(); // remove the results
13386 for (unsigned i = 1; i < NumArgs; ++i) {
13387 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013388 if (!(Op.isReg() && Op.isImplicit()))
13389 MIB.addOperand(Op);
13390 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013391 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013392 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13393
13394 BuildMI(*BB, MI, dl,
13395 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13396 .addReg(X86::ECX);
13397
13398 MI->eraseFromParent();
13399 return BB;
13400}
13401
Craig Topper2da36912012-11-11 22:45:02 +000013402static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13403 const TargetInstrInfo *TII,
13404 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013405 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013406
Eric Christopher228232b2010-11-30 07:20:12 +000013407 // Address into RAX/EAX, other two args into ECX, EDX.
13408 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13409 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13410 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13411 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013412 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013413
Eric Christopher228232b2010-11-30 07:20:12 +000013414 unsigned ValOps = X86::AddrNumOperands;
13415 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13416 .addReg(MI->getOperand(ValOps).getReg());
13417 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13418 .addReg(MI->getOperand(ValOps+1).getReg());
13419
13420 // The instruction doesn't actually take any operands though.
13421 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013422
Eric Christopher228232b2010-11-30 07:20:12 +000013423 MI->eraseFromParent(); // The pseudo is gone now.
13424 return BB;
13425}
13426
13427MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013428X86TargetLowering::EmitVAARG64WithCustomInserter(
13429 MachineInstr *MI,
13430 MachineBasicBlock *MBB) const {
13431 // Emit va_arg instruction on X86-64.
13432
13433 // Operands to this pseudo-instruction:
13434 // 0 ) Output : destination address (reg)
13435 // 1-5) Input : va_list address (addr, i64mem)
13436 // 6 ) ArgSize : Size (in bytes) of vararg type
13437 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13438 // 8 ) Align : Alignment of type
13439 // 9 ) EFLAGS (implicit-def)
13440
13441 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13442 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13443
13444 unsigned DestReg = MI->getOperand(0).getReg();
13445 MachineOperand &Base = MI->getOperand(1);
13446 MachineOperand &Scale = MI->getOperand(2);
13447 MachineOperand &Index = MI->getOperand(3);
13448 MachineOperand &Disp = MI->getOperand(4);
13449 MachineOperand &Segment = MI->getOperand(5);
13450 unsigned ArgSize = MI->getOperand(6).getImm();
13451 unsigned ArgMode = MI->getOperand(7).getImm();
13452 unsigned Align = MI->getOperand(8).getImm();
13453
13454 // Memory Reference
13455 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13456 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13457 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13458
13459 // Machine Information
13460 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13461 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13462 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13463 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13464 DebugLoc DL = MI->getDebugLoc();
13465
13466 // struct va_list {
13467 // i32 gp_offset
13468 // i32 fp_offset
13469 // i64 overflow_area (address)
13470 // i64 reg_save_area (address)
13471 // }
13472 // sizeof(va_list) = 24
13473 // alignment(va_list) = 8
13474
13475 unsigned TotalNumIntRegs = 6;
13476 unsigned TotalNumXMMRegs = 8;
13477 bool UseGPOffset = (ArgMode == 1);
13478 bool UseFPOffset = (ArgMode == 2);
13479 unsigned MaxOffset = TotalNumIntRegs * 8 +
13480 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13481
13482 /* Align ArgSize to a multiple of 8 */
13483 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13484 bool NeedsAlign = (Align > 8);
13485
13486 MachineBasicBlock *thisMBB = MBB;
13487 MachineBasicBlock *overflowMBB;
13488 MachineBasicBlock *offsetMBB;
13489 MachineBasicBlock *endMBB;
13490
13491 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13492 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13493 unsigned OffsetReg = 0;
13494
13495 if (!UseGPOffset && !UseFPOffset) {
13496 // If we only pull from the overflow region, we don't create a branch.
13497 // We don't need to alter control flow.
13498 OffsetDestReg = 0; // unused
13499 OverflowDestReg = DestReg;
13500
13501 offsetMBB = NULL;
13502 overflowMBB = thisMBB;
13503 endMBB = thisMBB;
13504 } else {
13505 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13506 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13507 // If not, pull from overflow_area. (branch to overflowMBB)
13508 //
13509 // thisMBB
13510 // | .
13511 // | .
13512 // offsetMBB overflowMBB
13513 // | .
13514 // | .
13515 // endMBB
13516
13517 // Registers for the PHI in endMBB
13518 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13519 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13520
13521 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13522 MachineFunction *MF = MBB->getParent();
13523 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13524 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13525 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13526
13527 MachineFunction::iterator MBBIter = MBB;
13528 ++MBBIter;
13529
13530 // Insert the new basic blocks
13531 MF->insert(MBBIter, offsetMBB);
13532 MF->insert(MBBIter, overflowMBB);
13533 MF->insert(MBBIter, endMBB);
13534
13535 // Transfer the remainder of MBB and its successor edges to endMBB.
13536 endMBB->splice(endMBB->begin(), thisMBB,
13537 llvm::next(MachineBasicBlock::iterator(MI)),
13538 thisMBB->end());
13539 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13540
13541 // Make offsetMBB and overflowMBB successors of thisMBB
13542 thisMBB->addSuccessor(offsetMBB);
13543 thisMBB->addSuccessor(overflowMBB);
13544
13545 // endMBB is a successor of both offsetMBB and overflowMBB
13546 offsetMBB->addSuccessor(endMBB);
13547 overflowMBB->addSuccessor(endMBB);
13548
13549 // Load the offset value into a register
13550 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13551 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13552 .addOperand(Base)
13553 .addOperand(Scale)
13554 .addOperand(Index)
13555 .addDisp(Disp, UseFPOffset ? 4 : 0)
13556 .addOperand(Segment)
13557 .setMemRefs(MMOBegin, MMOEnd);
13558
13559 // Check if there is enough room left to pull this argument.
13560 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13561 .addReg(OffsetReg)
13562 .addImm(MaxOffset + 8 - ArgSizeA8);
13563
13564 // Branch to "overflowMBB" if offset >= max
13565 // Fall through to "offsetMBB" otherwise
13566 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13567 .addMBB(overflowMBB);
13568 }
13569
13570 // In offsetMBB, emit code to use the reg_save_area.
13571 if (offsetMBB) {
13572 assert(OffsetReg != 0);
13573
13574 // Read the reg_save_area address.
13575 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13576 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13577 .addOperand(Base)
13578 .addOperand(Scale)
13579 .addOperand(Index)
13580 .addDisp(Disp, 16)
13581 .addOperand(Segment)
13582 .setMemRefs(MMOBegin, MMOEnd);
13583
13584 // Zero-extend the offset
13585 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13586 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13587 .addImm(0)
13588 .addReg(OffsetReg)
13589 .addImm(X86::sub_32bit);
13590
13591 // Add the offset to the reg_save_area to get the final address.
13592 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13593 .addReg(OffsetReg64)
13594 .addReg(RegSaveReg);
13595
13596 // Compute the offset for the next argument
13597 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13598 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13599 .addReg(OffsetReg)
13600 .addImm(UseFPOffset ? 16 : 8);
13601
13602 // Store it back into the va_list.
13603 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13604 .addOperand(Base)
13605 .addOperand(Scale)
13606 .addOperand(Index)
13607 .addDisp(Disp, UseFPOffset ? 4 : 0)
13608 .addOperand(Segment)
13609 .addReg(NextOffsetReg)
13610 .setMemRefs(MMOBegin, MMOEnd);
13611
13612 // Jump to endMBB
13613 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13614 .addMBB(endMBB);
13615 }
13616
13617 //
13618 // Emit code to use overflow area
13619 //
13620
13621 // Load the overflow_area address into a register.
13622 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13623 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13624 .addOperand(Base)
13625 .addOperand(Scale)
13626 .addOperand(Index)
13627 .addDisp(Disp, 8)
13628 .addOperand(Segment)
13629 .setMemRefs(MMOBegin, MMOEnd);
13630
13631 // If we need to align it, do so. Otherwise, just copy the address
13632 // to OverflowDestReg.
13633 if (NeedsAlign) {
13634 // Align the overflow address
13635 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13636 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13637
13638 // aligned_addr = (addr + (align-1)) & ~(align-1)
13639 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13640 .addReg(OverflowAddrReg)
13641 .addImm(Align-1);
13642
13643 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13644 .addReg(TmpReg)
13645 .addImm(~(uint64_t)(Align-1));
13646 } else {
13647 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13648 .addReg(OverflowAddrReg);
13649 }
13650
13651 // Compute the next overflow address after this argument.
13652 // (the overflow address should be kept 8-byte aligned)
13653 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13654 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13655 .addReg(OverflowDestReg)
13656 .addImm(ArgSizeA8);
13657
13658 // Store the new overflow address.
13659 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13660 .addOperand(Base)
13661 .addOperand(Scale)
13662 .addOperand(Index)
13663 .addDisp(Disp, 8)
13664 .addOperand(Segment)
13665 .addReg(NextAddrReg)
13666 .setMemRefs(MMOBegin, MMOEnd);
13667
13668 // If we branched, emit the PHI to the front of endMBB.
13669 if (offsetMBB) {
13670 BuildMI(*endMBB, endMBB->begin(), DL,
13671 TII->get(X86::PHI), DestReg)
13672 .addReg(OffsetDestReg).addMBB(offsetMBB)
13673 .addReg(OverflowDestReg).addMBB(overflowMBB);
13674 }
13675
13676 // Erase the pseudo instruction
13677 MI->eraseFromParent();
13678
13679 return endMBB;
13680}
13681
13682MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013683X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13684 MachineInstr *MI,
13685 MachineBasicBlock *MBB) const {
13686 // Emit code to save XMM registers to the stack. The ABI says that the
13687 // number of registers to save is given in %al, so it's theoretically
13688 // possible to do an indirect jump trick to avoid saving all of them,
13689 // however this code takes a simpler approach and just executes all
13690 // of the stores if %al is non-zero. It's less code, and it's probably
13691 // easier on the hardware branch predictor, and stores aren't all that
13692 // expensive anyway.
13693
13694 // Create the new basic blocks. One block contains all the XMM stores,
13695 // and one block is the final destination regardless of whether any
13696 // stores were performed.
13697 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13698 MachineFunction *F = MBB->getParent();
13699 MachineFunction::iterator MBBIter = MBB;
13700 ++MBBIter;
13701 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13702 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13703 F->insert(MBBIter, XMMSaveMBB);
13704 F->insert(MBBIter, EndMBB);
13705
Dan Gohman14152b42010-07-06 20:24:04 +000013706 // Transfer the remainder of MBB and its successor edges to EndMBB.
13707 EndMBB->splice(EndMBB->begin(), MBB,
13708 llvm::next(MachineBasicBlock::iterator(MI)),
13709 MBB->end());
13710 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13711
Dan Gohmand6708ea2009-08-15 01:38:56 +000013712 // The original block will now fall through to the XMM save block.
13713 MBB->addSuccessor(XMMSaveMBB);
13714 // The XMMSaveMBB will fall through to the end block.
13715 XMMSaveMBB->addSuccessor(EndMBB);
13716
13717 // Now add the instructions.
13718 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13719 DebugLoc DL = MI->getDebugLoc();
13720
13721 unsigned CountReg = MI->getOperand(0).getReg();
13722 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13723 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13724
13725 if (!Subtarget->isTargetWin64()) {
13726 // If %al is 0, branch around the XMM save block.
13727 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013728 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013729 MBB->addSuccessor(EndMBB);
13730 }
13731
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013732 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013733 // In the XMM save block, save all the XMM argument registers.
13734 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13735 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013736 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013737 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013738 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013739 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013740 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013741 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013742 .addFrameIndex(RegSaveFrameIndex)
13743 .addImm(/*Scale=*/1)
13744 .addReg(/*IndexReg=*/0)
13745 .addImm(/*Disp=*/Offset)
13746 .addReg(/*Segment=*/0)
13747 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013748 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013749 }
13750
Dan Gohman14152b42010-07-06 20:24:04 +000013751 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013752
13753 return EndMBB;
13754}
Mon P Wang63307c32008-05-05 19:05:59 +000013755
Lang Hames6e3f7e42012-02-03 01:13:49 +000013756// The EFLAGS operand of SelectItr might be missing a kill marker
13757// because there were multiple uses of EFLAGS, and ISel didn't know
13758// which to mark. Figure out whether SelectItr should have had a
13759// kill marker, and set it if it should. Returns the correct kill
13760// marker value.
13761static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13762 MachineBasicBlock* BB,
13763 const TargetRegisterInfo* TRI) {
13764 // Scan forward through BB for a use/def of EFLAGS.
13765 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13766 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013767 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013768 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013769 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013770 if (mi.definesRegister(X86::EFLAGS))
13771 break; // Should have kill-flag - update below.
13772 }
13773
13774 // If we hit the end of the block, check whether EFLAGS is live into a
13775 // successor.
13776 if (miI == BB->end()) {
13777 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13778 sEnd = BB->succ_end();
13779 sItr != sEnd; ++sItr) {
13780 MachineBasicBlock* succ = *sItr;
13781 if (succ->isLiveIn(X86::EFLAGS))
13782 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013783 }
13784 }
13785
Lang Hames6e3f7e42012-02-03 01:13:49 +000013786 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13787 // out. SelectMI should have a kill flag on EFLAGS.
13788 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013789 return true;
13790}
13791
Evan Cheng60c07e12006-07-05 22:17:51 +000013792MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013793X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013794 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013795 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13796 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013797
Chris Lattner52600972009-09-02 05:57:00 +000013798 // To "insert" a SELECT_CC instruction, we actually have to insert the
13799 // diamond control-flow pattern. The incoming instruction knows the
13800 // destination vreg to set, the condition code register to branch on, the
13801 // true/false values to select between, and a branch opcode to use.
13802 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13803 MachineFunction::iterator It = BB;
13804 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013805
Chris Lattner52600972009-09-02 05:57:00 +000013806 // thisMBB:
13807 // ...
13808 // TrueVal = ...
13809 // cmpTY ccX, r1, r2
13810 // bCC copy1MBB
13811 // fallthrough --> copy0MBB
13812 MachineBasicBlock *thisMBB = BB;
13813 MachineFunction *F = BB->getParent();
13814 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13815 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013816 F->insert(It, copy0MBB);
13817 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013818
Bill Wendling730c07e2010-06-25 20:48:10 +000013819 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13820 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013821 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13822 if (!MI->killsRegister(X86::EFLAGS) &&
13823 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13824 copy0MBB->addLiveIn(X86::EFLAGS);
13825 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013826 }
13827
Dan Gohman14152b42010-07-06 20:24:04 +000013828 // Transfer the remainder of BB and its successor edges to sinkMBB.
13829 sinkMBB->splice(sinkMBB->begin(), BB,
13830 llvm::next(MachineBasicBlock::iterator(MI)),
13831 BB->end());
13832 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13833
13834 // Add the true and fallthrough blocks as its successors.
13835 BB->addSuccessor(copy0MBB);
13836 BB->addSuccessor(sinkMBB);
13837
13838 // Create the conditional branch instruction.
13839 unsigned Opc =
13840 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13841 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13842
Chris Lattner52600972009-09-02 05:57:00 +000013843 // copy0MBB:
13844 // %FalseValue = ...
13845 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013846 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013847
Chris Lattner52600972009-09-02 05:57:00 +000013848 // sinkMBB:
13849 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13850 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013851 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13852 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013853 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13854 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13855
Dan Gohman14152b42010-07-06 20:24:04 +000013856 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013857 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013858}
13859
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013860MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013861X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13862 bool Is64Bit) const {
13863 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13864 DebugLoc DL = MI->getDebugLoc();
13865 MachineFunction *MF = BB->getParent();
13866 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13867
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013868 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013869
13870 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13871 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13872
13873 // BB:
13874 // ... [Till the alloca]
13875 // If stacklet is not large enough, jump to mallocMBB
13876 //
13877 // bumpMBB:
13878 // Allocate by subtracting from RSP
13879 // Jump to continueMBB
13880 //
13881 // mallocMBB:
13882 // Allocate by call to runtime
13883 //
13884 // continueMBB:
13885 // ...
13886 // [rest of original BB]
13887 //
13888
13889 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13890 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13891 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13892
13893 MachineRegisterInfo &MRI = MF->getRegInfo();
13894 const TargetRegisterClass *AddrRegClass =
13895 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13896
13897 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13898 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13899 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013900 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013901 sizeVReg = MI->getOperand(1).getReg(),
13902 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13903
13904 MachineFunction::iterator MBBIter = BB;
13905 ++MBBIter;
13906
13907 MF->insert(MBBIter, bumpMBB);
13908 MF->insert(MBBIter, mallocMBB);
13909 MF->insert(MBBIter, continueMBB);
13910
13911 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13912 (MachineBasicBlock::iterator(MI)), BB->end());
13913 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13914
13915 // Add code to the main basic block to check if the stack limit has been hit,
13916 // and if so, jump to mallocMBB otherwise to bumpMBB.
13917 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013918 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013919 .addReg(tmpSPVReg).addReg(sizeVReg);
13920 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013921 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013922 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013923 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13924
13925 // bumpMBB simply decreases the stack pointer, since we know the current
13926 // stacklet has enough space.
13927 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013928 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013929 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013930 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013931 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13932
13933 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013934 const uint32_t *RegMask =
13935 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013936 if (Is64Bit) {
13937 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13938 .addReg(sizeVReg);
13939 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013940 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013941 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013942 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013943 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013944 } else {
13945 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13946 .addImm(12);
13947 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13948 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013949 .addExternalSymbol("__morestack_allocate_stack_space")
13950 .addRegMask(RegMask)
13951 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013952 }
13953
13954 if (!Is64Bit)
13955 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13956 .addImm(16);
13957
13958 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13959 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13960 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13961
13962 // Set up the CFG correctly.
13963 BB->addSuccessor(bumpMBB);
13964 BB->addSuccessor(mallocMBB);
13965 mallocMBB->addSuccessor(continueMBB);
13966 bumpMBB->addSuccessor(continueMBB);
13967
13968 // Take care of the PHI nodes.
13969 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13970 MI->getOperand(0).getReg())
13971 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13972 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13973
13974 // Delete the original pseudo instruction.
13975 MI->eraseFromParent();
13976
13977 // And we're done.
13978 return continueMBB;
13979}
13980
13981MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013982X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013983 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013984 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13985 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013986
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013987 assert(!Subtarget->isTargetEnvMacho());
13988
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013989 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13990 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013991
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013992 if (Subtarget->isTargetWin64()) {
13993 if (Subtarget->isTargetCygMing()) {
13994 // ___chkstk(Mingw64):
13995 // Clobbers R10, R11, RAX and EFLAGS.
13996 // Updates RSP.
13997 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13998 .addExternalSymbol("___chkstk")
13999 .addReg(X86::RAX, RegState::Implicit)
14000 .addReg(X86::RSP, RegState::Implicit)
14001 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14002 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14003 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14004 } else {
14005 // __chkstk(MSVCRT): does not update stack pointer.
14006 // Clobbers R10, R11 and EFLAGS.
14007 // FIXME: RAX(allocated size) might be reused and not killed.
14008 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14009 .addExternalSymbol("__chkstk")
14010 .addReg(X86::RAX, RegState::Implicit)
14011 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14012 // RAX has the offset to subtracted from RSP.
14013 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14014 .addReg(X86::RSP)
14015 .addReg(X86::RAX);
14016 }
14017 } else {
14018 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014019 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14020
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014021 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14022 .addExternalSymbol(StackProbeSymbol)
14023 .addReg(X86::EAX, RegState::Implicit)
14024 .addReg(X86::ESP, RegState::Implicit)
14025 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14026 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14027 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14028 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014029
Dan Gohman14152b42010-07-06 20:24:04 +000014030 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014031 return BB;
14032}
Chris Lattner52600972009-09-02 05:57:00 +000014033
14034MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014035X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14036 MachineBasicBlock *BB) const {
14037 // This is pretty easy. We're taking the value that we received from
14038 // our load from the relocation, sticking it in either RDI (x86-64)
14039 // or EAX and doing an indirect call. The return value will then
14040 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014041 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014042 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014043 DebugLoc DL = MI->getDebugLoc();
14044 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014045
14046 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014047 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014048
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014049 // Get a register mask for the lowered call.
14050 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14051 // proper register mask.
14052 const uint32_t *RegMask =
14053 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014054 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014055 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14056 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014057 .addReg(X86::RIP)
14058 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014059 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014060 MI->getOperand(3).getTargetFlags())
14061 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014062 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014063 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014064 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014065 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014066 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14067 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014068 .addReg(0)
14069 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014070 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014071 MI->getOperand(3).getTargetFlags())
14072 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014073 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014074 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014075 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014076 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014077 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14078 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014079 .addReg(TII->getGlobalBaseReg(F))
14080 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014081 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014082 MI->getOperand(3).getTargetFlags())
14083 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014084 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014085 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014086 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014087 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014088
Dan Gohman14152b42010-07-06 20:24:04 +000014089 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014090 return BB;
14091}
14092
14093MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014094X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14095 MachineBasicBlock *MBB) const {
14096 DebugLoc DL = MI->getDebugLoc();
14097 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14098
14099 MachineFunction *MF = MBB->getParent();
14100 MachineRegisterInfo &MRI = MF->getRegInfo();
14101
14102 const BasicBlock *BB = MBB->getBasicBlock();
14103 MachineFunction::iterator I = MBB;
14104 ++I;
14105
14106 // Memory Reference
14107 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14108 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14109
14110 unsigned DstReg;
14111 unsigned MemOpndSlot = 0;
14112
14113 unsigned CurOp = 0;
14114
14115 DstReg = MI->getOperand(CurOp++).getReg();
14116 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14117 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14118 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14119 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14120
14121 MemOpndSlot = CurOp;
14122
14123 MVT PVT = getPointerTy();
14124 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14125 "Invalid Pointer Size!");
14126
14127 // For v = setjmp(buf), we generate
14128 //
14129 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014130 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014131 // SjLjSetup restoreMBB
14132 //
14133 // mainMBB:
14134 // v_main = 0
14135 //
14136 // sinkMBB:
14137 // v = phi(main, restore)
14138 //
14139 // restoreMBB:
14140 // v_restore = 1
14141
14142 MachineBasicBlock *thisMBB = MBB;
14143 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14144 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14145 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14146 MF->insert(I, mainMBB);
14147 MF->insert(I, sinkMBB);
14148 MF->push_back(restoreMBB);
14149
14150 MachineInstrBuilder MIB;
14151
14152 // Transfer the remainder of BB and its successor edges to sinkMBB.
14153 sinkMBB->splice(sinkMBB->begin(), MBB,
14154 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14155 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14156
14157 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014158 unsigned PtrStoreOpc = 0;
14159 unsigned LabelReg = 0;
14160 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14161 Reloc::Model RM = getTargetMachine().getRelocationModel();
14162 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14163 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014164
Michael Liao281ae5a2012-10-17 02:22:27 +000014165 // Prepare IP either in reg or imm.
14166 if (!UseImmLabel) {
14167 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14168 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14169 LabelReg = MRI.createVirtualRegister(PtrRC);
14170 if (Subtarget->is64Bit()) {
14171 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14172 .addReg(X86::RIP)
14173 .addImm(0)
14174 .addReg(0)
14175 .addMBB(restoreMBB)
14176 .addReg(0);
14177 } else {
14178 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14179 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14180 .addReg(XII->getGlobalBaseReg(MF))
14181 .addImm(0)
14182 .addReg(0)
14183 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14184 .addReg(0);
14185 }
14186 } else
14187 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014188 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014189 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014190 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14191 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014192 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014193 else
14194 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14195 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014196 if (!UseImmLabel)
14197 MIB.addReg(LabelReg);
14198 else
14199 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014200 MIB.setMemRefs(MMOBegin, MMOEnd);
14201 // Setup
14202 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14203 .addMBB(restoreMBB);
14204 MIB.addRegMask(RegInfo->getNoPreservedMask());
14205 thisMBB->addSuccessor(mainMBB);
14206 thisMBB->addSuccessor(restoreMBB);
14207
14208 // mainMBB:
14209 // EAX = 0
14210 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14211 mainMBB->addSuccessor(sinkMBB);
14212
14213 // sinkMBB:
14214 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14215 TII->get(X86::PHI), DstReg)
14216 .addReg(mainDstReg).addMBB(mainMBB)
14217 .addReg(restoreDstReg).addMBB(restoreMBB);
14218
14219 // restoreMBB:
14220 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14221 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14222 restoreMBB->addSuccessor(sinkMBB);
14223
14224 MI->eraseFromParent();
14225 return sinkMBB;
14226}
14227
14228MachineBasicBlock *
14229X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14230 MachineBasicBlock *MBB) const {
14231 DebugLoc DL = MI->getDebugLoc();
14232 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14233
14234 MachineFunction *MF = MBB->getParent();
14235 MachineRegisterInfo &MRI = MF->getRegInfo();
14236
14237 // Memory Reference
14238 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14239 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14240
14241 MVT PVT = getPointerTy();
14242 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14243 "Invalid Pointer Size!");
14244
14245 const TargetRegisterClass *RC =
14246 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14247 unsigned Tmp = MRI.createVirtualRegister(RC);
14248 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14249 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14250 unsigned SP = RegInfo->getStackRegister();
14251
14252 MachineInstrBuilder MIB;
14253
Michael Liao281ae5a2012-10-17 02:22:27 +000014254 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14255 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014256
14257 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14258 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14259
14260 // Reload FP
14261 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14262 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14263 MIB.addOperand(MI->getOperand(i));
14264 MIB.setMemRefs(MMOBegin, MMOEnd);
14265 // Reload IP
14266 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14267 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14268 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014269 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014270 else
14271 MIB.addOperand(MI->getOperand(i));
14272 }
14273 MIB.setMemRefs(MMOBegin, MMOEnd);
14274 // Reload SP
14275 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14276 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14277 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014278 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014279 else
14280 MIB.addOperand(MI->getOperand(i));
14281 }
14282 MIB.setMemRefs(MMOBegin, MMOEnd);
14283 // Jump
14284 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14285
14286 MI->eraseFromParent();
14287 return MBB;
14288}
14289
14290MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014291X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014292 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014293 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014294 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014295 case X86::TAILJMPd64:
14296 case X86::TAILJMPr64:
14297 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014298 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014299 case X86::TCRETURNdi64:
14300 case X86::TCRETURNri64:
14301 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014302 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014303 case X86::WIN_ALLOCA:
14304 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014305 case X86::SEG_ALLOCA_32:
14306 return EmitLoweredSegAlloca(MI, BB, false);
14307 case X86::SEG_ALLOCA_64:
14308 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014309 case X86::TLSCall_32:
14310 case X86::TLSCall_64:
14311 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014312 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014313 case X86::CMOV_FR32:
14314 case X86::CMOV_FR64:
14315 case X86::CMOV_V4F32:
14316 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014317 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014318 case X86::CMOV_V8F32:
14319 case X86::CMOV_V4F64:
14320 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014321 case X86::CMOV_GR16:
14322 case X86::CMOV_GR32:
14323 case X86::CMOV_RFP32:
14324 case X86::CMOV_RFP64:
14325 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014326 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014327
Dale Johannesen849f2142007-07-03 00:53:03 +000014328 case X86::FP32_TO_INT16_IN_MEM:
14329 case X86::FP32_TO_INT32_IN_MEM:
14330 case X86::FP32_TO_INT64_IN_MEM:
14331 case X86::FP64_TO_INT16_IN_MEM:
14332 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014333 case X86::FP64_TO_INT64_IN_MEM:
14334 case X86::FP80_TO_INT16_IN_MEM:
14335 case X86::FP80_TO_INT32_IN_MEM:
14336 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014337 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14338 DebugLoc DL = MI->getDebugLoc();
14339
Evan Cheng60c07e12006-07-05 22:17:51 +000014340 // Change the floating point control register to use "round towards zero"
14341 // mode when truncating to an integer value.
14342 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014343 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014344 addFrameReference(BuildMI(*BB, MI, DL,
14345 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014346
14347 // Load the old value of the high byte of the control word...
14348 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014349 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014350 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014351 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014352
14353 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014354 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014355 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014356
14357 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014358 addFrameReference(BuildMI(*BB, MI, DL,
14359 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014360
14361 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014362 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014363 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014364
14365 // Get the X86 opcode to use.
14366 unsigned Opc;
14367 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014368 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014369 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14370 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14371 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14372 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14373 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14374 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014375 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14376 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14377 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014378 }
14379
14380 X86AddressMode AM;
14381 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014382 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014383 AM.BaseType = X86AddressMode::RegBase;
14384 AM.Base.Reg = Op.getReg();
14385 } else {
14386 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014387 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014388 }
14389 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014390 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014391 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014392 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014393 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014394 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014395 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014396 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014397 AM.GV = Op.getGlobal();
14398 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014399 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014400 }
Dan Gohman14152b42010-07-06 20:24:04 +000014401 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014402 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014403
14404 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014405 addFrameReference(BuildMI(*BB, MI, DL,
14406 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014407
Dan Gohman14152b42010-07-06 20:24:04 +000014408 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014409 return BB;
14410 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014411 // String/text processing lowering.
14412 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014413 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014414 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014415 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014416 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014417 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014418 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014419 case X86::VPCMPESTRM128MEM:
14420 assert(Subtarget->hasSSE42() &&
14421 "Target must have SSE4.2 or AVX features enabled");
14422 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014423
14424 // String/text processing lowering.
14425 case X86::PCMPISTRIREG:
14426 case X86::VPCMPISTRIREG:
14427 case X86::PCMPISTRIMEM:
14428 case X86::VPCMPISTRIMEM:
14429 case X86::PCMPESTRIREG:
14430 case X86::VPCMPESTRIREG:
14431 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014432 case X86::VPCMPESTRIMEM:
14433 assert(Subtarget->hasSSE42() &&
14434 "Target must have SSE4.2 or AVX features enabled");
14435 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014436
Craig Topper8aae8dd2012-11-10 08:57:41 +000014437 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014438 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014439 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014440
Michael Liaobe02a902012-11-08 07:28:54 +000014441 // xbegin
14442 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014443 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014444
Craig Topper8aae8dd2012-11-10 08:57:41 +000014445 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014446 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014447 case X86::ATOMAND16:
14448 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014449 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014450 // Fall through
14451 case X86::ATOMOR8:
14452 case X86::ATOMOR16:
14453 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014454 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014455 // Fall through
14456 case X86::ATOMXOR16:
14457 case X86::ATOMXOR8:
14458 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014459 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014460 // Fall through
14461 case X86::ATOMNAND8:
14462 case X86::ATOMNAND16:
14463 case X86::ATOMNAND32:
14464 case X86::ATOMNAND64:
14465 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014466 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014467 case X86::ATOMMAX16:
14468 case X86::ATOMMAX32:
14469 case X86::ATOMMAX64:
14470 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014471 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014472 case X86::ATOMMIN16:
14473 case X86::ATOMMIN32:
14474 case X86::ATOMMIN64:
14475 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014476 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014477 case X86::ATOMUMAX16:
14478 case X86::ATOMUMAX32:
14479 case X86::ATOMUMAX64:
14480 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014481 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014482 case X86::ATOMUMIN16:
14483 case X86::ATOMUMIN32:
14484 case X86::ATOMUMIN64:
14485 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014486
14487 // This group does 64-bit operations on a 32-bit host.
14488 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014489 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014490 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014491 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014492 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014493 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014494 case X86::ATOMMAX6432:
14495 case X86::ATOMMIN6432:
14496 case X86::ATOMUMAX6432:
14497 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014498 case X86::ATOMSWAP6432:
14499 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014500
Dan Gohmand6708ea2009-08-15 01:38:56 +000014501 case X86::VASTART_SAVE_XMM_REGS:
14502 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014503
14504 case X86::VAARG_64:
14505 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014506
14507 case X86::EH_SjLj_SetJmp32:
14508 case X86::EH_SjLj_SetJmp64:
14509 return emitEHSjLjSetJmp(MI, BB);
14510
14511 case X86::EH_SjLj_LongJmp32:
14512 case X86::EH_SjLj_LongJmp64:
14513 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014514 }
14515}
14516
14517//===----------------------------------------------------------------------===//
14518// X86 Optimization Hooks
14519//===----------------------------------------------------------------------===//
14520
Dan Gohman475871a2008-07-27 21:46:04 +000014521void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014522 APInt &KnownZero,
14523 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014524 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014525 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014526 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014527 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014528 assert((Opc >= ISD::BUILTIN_OP_END ||
14529 Opc == ISD::INTRINSIC_WO_CHAIN ||
14530 Opc == ISD::INTRINSIC_W_CHAIN ||
14531 Opc == ISD::INTRINSIC_VOID) &&
14532 "Should use MaskedValueIsZero if you don't know whether Op"
14533 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014534
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014535 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014536 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014537 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014538 case X86ISD::ADD:
14539 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014540 case X86ISD::ADC:
14541 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014542 case X86ISD::SMUL:
14543 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014544 case X86ISD::INC:
14545 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014546 case X86ISD::OR:
14547 case X86ISD::XOR:
14548 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014549 // These nodes' second result is a boolean.
14550 if (Op.getResNo() == 0)
14551 break;
14552 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014553 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014554 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014555 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014556 case ISD::INTRINSIC_WO_CHAIN: {
14557 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14558 unsigned NumLoBits = 0;
14559 switch (IntId) {
14560 default: break;
14561 case Intrinsic::x86_sse_movmsk_ps:
14562 case Intrinsic::x86_avx_movmsk_ps_256:
14563 case Intrinsic::x86_sse2_movmsk_pd:
14564 case Intrinsic::x86_avx_movmsk_pd_256:
14565 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014566 case Intrinsic::x86_sse2_pmovmskb_128:
14567 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014568 // High bits of movmskp{s|d}, pmovmskb are known zero.
14569 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014570 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014571 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14572 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14573 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14574 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14575 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14576 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014577 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014578 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014579 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014580 break;
14581 }
14582 }
14583 break;
14584 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014585 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014586}
Chris Lattner259e97c2006-01-31 19:43:35 +000014587
Owen Andersonbc146b02010-09-21 20:42:50 +000014588unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14589 unsigned Depth) const {
14590 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14591 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14592 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014593
Owen Andersonbc146b02010-09-21 20:42:50 +000014594 // Fallback case.
14595 return 1;
14596}
14597
Evan Cheng206ee9d2006-07-07 08:33:52 +000014598/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014599/// node is a GlobalAddress + offset.
14600bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014601 const GlobalValue* &GA,
14602 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014603 if (N->getOpcode() == X86ISD::Wrapper) {
14604 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014605 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014606 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014607 return true;
14608 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014609 }
Evan Chengad4196b2008-05-12 19:56:52 +000014610 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014611}
14612
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014613/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14614/// same as extracting the high 128-bit part of 256-bit vector and then
14615/// inserting the result into the low part of a new 256-bit vector
14616static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14617 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014618 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014619
14620 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014621 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014622 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14623 SVOp->getMaskElt(j) >= 0)
14624 return false;
14625
14626 return true;
14627}
14628
14629/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14630/// same as extracting the low 128-bit part of 256-bit vector and then
14631/// inserting the result into the high part of a new 256-bit vector
14632static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14633 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014634 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014635
14636 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014637 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014638 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14639 SVOp->getMaskElt(j) >= 0)
14640 return false;
14641
14642 return true;
14643}
14644
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014645/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14646static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014647 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014648 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014649 DebugLoc dl = N->getDebugLoc();
14650 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14651 SDValue V1 = SVOp->getOperand(0);
14652 SDValue V2 = SVOp->getOperand(1);
14653 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014654 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014655
14656 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14657 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14658 //
14659 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014660 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014661 // V UNDEF BUILD_VECTOR UNDEF
14662 // \ / \ /
14663 // CONCAT_VECTOR CONCAT_VECTOR
14664 // \ /
14665 // \ /
14666 // RESULT: V + zero extended
14667 //
14668 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14669 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14670 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14671 return SDValue();
14672
14673 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14674 return SDValue();
14675
14676 // To match the shuffle mask, the first half of the mask should
14677 // be exactly the first vector, and all the rest a splat with the
14678 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014679 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014680 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14681 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14682 return SDValue();
14683
Chad Rosier3d1161e2012-01-03 21:05:52 +000014684 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14685 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014686 if (Ld->hasNUsesOfValue(1, 0)) {
14687 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14688 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14689 SDValue ResNode =
14690 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14691 Ld->getMemoryVT(),
14692 Ld->getPointerInfo(),
14693 Ld->getAlignment(),
14694 false/*isVolatile*/, true/*ReadMem*/,
14695 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014696
14697 // Make sure the newly-created LOAD is in the same position as Ld in
14698 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14699 // and update uses of Ld's output chain to use the TokenFactor.
14700 if (Ld->hasAnyUseOfValue(1)) {
14701 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14702 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14703 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14704 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14705 SDValue(ResNode.getNode(), 1));
14706 }
14707
Chad Rosier42726832012-05-07 18:47:44 +000014708 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14709 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014710 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014711
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014712 // Emit a zeroed vector and insert the desired subvector on its
14713 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014714 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014715 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014716 return DCI.CombineTo(N, InsV);
14717 }
14718
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014719 //===--------------------------------------------------------------------===//
14720 // Combine some shuffles into subvector extracts and inserts:
14721 //
14722
14723 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14724 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014725 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14726 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014727 return DCI.CombineTo(N, InsV);
14728 }
14729
14730 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14731 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014732 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14733 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014734 return DCI.CombineTo(N, InsV);
14735 }
14736
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014737 return SDValue();
14738}
14739
14740/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014741static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014742 TargetLowering::DAGCombinerInfo &DCI,
14743 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014744 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014745 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014746
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014747 // Don't create instructions with illegal types after legalize types has run.
14748 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14749 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14750 return SDValue();
14751
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014752 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014753 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014754 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014755 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014756
14757 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014758 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014759 return SDValue();
14760
14761 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14762 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14763 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014764 SmallVector<SDValue, 16> Elts;
14765 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014766 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014767
Nate Begemanfdea31a2010-03-24 20:49:50 +000014768 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014769}
Evan Chengd880b972008-05-09 21:53:03 +000014770
Nadav Roteme12bf182013-01-04 17:35:21 +000014771/// PerformTruncateCombine - Converts truncate operation to
14772/// a sequence of vector shuffle operations.
14773/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014774static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14775 TargetLowering::DAGCombinerInfo &DCI,
14776 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014777 return SDValue();
14778}
14779
Craig Topper89f4e662012-03-20 07:17:59 +000014780/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14781/// specific shuffle of a load can be folded into a single element load.
14782/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14783/// shuffles have been customed lowered so we need to handle those here.
14784static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14785 TargetLowering::DAGCombinerInfo &DCI) {
14786 if (DCI.isBeforeLegalizeOps())
14787 return SDValue();
14788
14789 SDValue InVec = N->getOperand(0);
14790 SDValue EltNo = N->getOperand(1);
14791
14792 if (!isa<ConstantSDNode>(EltNo))
14793 return SDValue();
14794
14795 EVT VT = InVec.getValueType();
14796
14797 bool HasShuffleIntoBitcast = false;
14798 if (InVec.getOpcode() == ISD::BITCAST) {
14799 // Don't duplicate a load with other uses.
14800 if (!InVec.hasOneUse())
14801 return SDValue();
14802 EVT BCVT = InVec.getOperand(0).getValueType();
14803 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14804 return SDValue();
14805 InVec = InVec.getOperand(0);
14806 HasShuffleIntoBitcast = true;
14807 }
14808
14809 if (!isTargetShuffle(InVec.getOpcode()))
14810 return SDValue();
14811
14812 // Don't duplicate a load with other uses.
14813 if (!InVec.hasOneUse())
14814 return SDValue();
14815
14816 SmallVector<int, 16> ShuffleMask;
14817 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014818 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14819 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014820 return SDValue();
14821
14822 // Select the input vector, guarding against out of range extract vector.
14823 unsigned NumElems = VT.getVectorNumElements();
14824 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14825 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14826 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14827 : InVec.getOperand(1);
14828
14829 // If inputs to shuffle are the same for both ops, then allow 2 uses
14830 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14831
14832 if (LdNode.getOpcode() == ISD::BITCAST) {
14833 // Don't duplicate a load with other uses.
14834 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14835 return SDValue();
14836
14837 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14838 LdNode = LdNode.getOperand(0);
14839 }
14840
14841 if (!ISD::isNormalLoad(LdNode.getNode()))
14842 return SDValue();
14843
14844 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14845
14846 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14847 return SDValue();
14848
14849 if (HasShuffleIntoBitcast) {
14850 // If there's a bitcast before the shuffle, check if the load type and
14851 // alignment is valid.
14852 unsigned Align = LN0->getAlignment();
14853 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014854 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014855 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14856
14857 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14858 return SDValue();
14859 }
14860
14861 // All checks match so transform back to vector_shuffle so that DAG combiner
14862 // can finish the job
14863 DebugLoc dl = N->getDebugLoc();
14864
14865 // Create shuffle node taking into account the case that its a unary shuffle
14866 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14867 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14868 InVec.getOperand(0), Shuffle,
14869 &ShuffleMask[0]);
14870 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14871 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14872 EltNo);
14873}
14874
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014875/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14876/// generation and convert it from being a bunch of shuffles and extracts
14877/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014878static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014879 TargetLowering::DAGCombinerInfo &DCI) {
14880 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14881 if (NewOp.getNode())
14882 return NewOp;
14883
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014884 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014885 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14886 // from mmx to v2i32 has a single usage.
14887 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14888 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14889 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14890 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14891 N->getValueType(0),
14892 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014893
14894 // Only operate on vectors of 4 elements, where the alternative shuffling
14895 // gets to be more expensive.
14896 if (InputVector.getValueType() != MVT::v4i32)
14897 return SDValue();
14898
14899 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14900 // single use which is a sign-extend or zero-extend, and all elements are
14901 // used.
14902 SmallVector<SDNode *, 4> Uses;
14903 unsigned ExtractedElements = 0;
14904 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14905 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14906 if (UI.getUse().getResNo() != InputVector.getResNo())
14907 return SDValue();
14908
14909 SDNode *Extract = *UI;
14910 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14911 return SDValue();
14912
14913 if (Extract->getValueType(0) != MVT::i32)
14914 return SDValue();
14915 if (!Extract->hasOneUse())
14916 return SDValue();
14917 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14918 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14919 return SDValue();
14920 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14921 return SDValue();
14922
14923 // Record which element was extracted.
14924 ExtractedElements |=
14925 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14926
14927 Uses.push_back(Extract);
14928 }
14929
14930 // If not all the elements were used, this may not be worthwhile.
14931 if (ExtractedElements != 15)
14932 return SDValue();
14933
14934 // Ok, we've now decided to do the transformation.
14935 DebugLoc dl = InputVector.getDebugLoc();
14936
14937 // Store the value to a temporary stack slot.
14938 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014939 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14940 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014941
14942 // Replace each use (extract) with a load of the appropriate element.
14943 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14944 UE = Uses.end(); UI != UE; ++UI) {
14945 SDNode *Extract = *UI;
14946
Nadav Rotem86694292011-05-17 08:31:57 +000014947 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014948 SDValue Idx = Extract->getOperand(1);
14949 unsigned EltSize =
14950 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14951 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014952 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014953 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14954
Nadav Rotem86694292011-05-17 08:31:57 +000014955 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014956 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014957
14958 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014959 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014960 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014961 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014962
14963 // Replace the exact with the load.
14964 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14965 }
14966
14967 // The replacement was made in place; don't return anything.
14968 return SDValue();
14969}
14970
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014971/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14972static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14973 SDValue RHS, SelectionDAG &DAG,
14974 const X86Subtarget *Subtarget) {
14975 if (!VT.isVector())
14976 return 0;
14977
14978 switch (VT.getSimpleVT().SimpleTy) {
14979 default: return 0;
14980 case MVT::v32i8:
14981 case MVT::v16i16:
14982 case MVT::v8i32:
14983 if (!Subtarget->hasAVX2())
14984 return 0;
14985 case MVT::v16i8:
14986 case MVT::v8i16:
14987 case MVT::v4i32:
14988 if (!Subtarget->hasSSE2())
14989 return 0;
14990 }
14991
14992 // SSE2 has only a small subset of the operations.
14993 bool hasUnsigned = Subtarget->hasSSE41() ||
14994 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14995 bool hasSigned = Subtarget->hasSSE41() ||
14996 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14997
14998 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14999
15000 // Check for x CC y ? x : y.
15001 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15002 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15003 switch (CC) {
15004 default: break;
15005 case ISD::SETULT:
15006 case ISD::SETULE:
15007 return hasUnsigned ? X86ISD::UMIN : 0;
15008 case ISD::SETUGT:
15009 case ISD::SETUGE:
15010 return hasUnsigned ? X86ISD::UMAX : 0;
15011 case ISD::SETLT:
15012 case ISD::SETLE:
15013 return hasSigned ? X86ISD::SMIN : 0;
15014 case ISD::SETGT:
15015 case ISD::SETGE:
15016 return hasSigned ? X86ISD::SMAX : 0;
15017 }
15018 // Check for x CC y ? y : x -- a min/max with reversed arms.
15019 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15020 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15021 switch (CC) {
15022 default: break;
15023 case ISD::SETULT:
15024 case ISD::SETULE:
15025 return hasUnsigned ? X86ISD::UMAX : 0;
15026 case ISD::SETUGT:
15027 case ISD::SETUGE:
15028 return hasUnsigned ? X86ISD::UMIN : 0;
15029 case ISD::SETLT:
15030 case ISD::SETLE:
15031 return hasSigned ? X86ISD::SMAX : 0;
15032 case ISD::SETGT:
15033 case ISD::SETGE:
15034 return hasSigned ? X86ISD::SMIN : 0;
15035 }
15036 }
15037
15038 return 0;
15039}
15040
Duncan Sands6bcd2192011-09-17 16:49:39 +000015041/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15042/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015043static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015044 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015045 const X86Subtarget *Subtarget) {
15046 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015047 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015048 // Get the LHS/RHS of the select.
15049 SDValue LHS = N->getOperand(1);
15050 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015051 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015052
Dan Gohman670e5392009-09-21 18:03:22 +000015053 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015054 // instructions match the semantics of the common C idiom x<y?x:y but not
15055 // x<=y?x:y, because of how they handle negative zero (which can be
15056 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015057 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15058 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015059 (Subtarget->hasSSE2() ||
15060 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015061 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015062
Chris Lattner47b4ce82009-03-11 05:48:52 +000015063 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015064 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015065 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15066 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015067 switch (CC) {
15068 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015069 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015070 // Converting this to a min would handle NaNs incorrectly, and swapping
15071 // the operands would cause it to handle comparisons between positive
15072 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015073 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015074 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015075 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15076 break;
15077 std::swap(LHS, RHS);
15078 }
Dan Gohman670e5392009-09-21 18:03:22 +000015079 Opcode = X86ISD::FMIN;
15080 break;
15081 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015082 // Converting this to a min would handle comparisons between positive
15083 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015084 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015085 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15086 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015087 Opcode = X86ISD::FMIN;
15088 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015089 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015090 // Converting this to a min would handle both negative zeros and NaNs
15091 // incorrectly, but we can swap the operands to fix both.
15092 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015093 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015094 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015095 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015096 Opcode = X86ISD::FMIN;
15097 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015098
Dan Gohman670e5392009-09-21 18:03:22 +000015099 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015100 // Converting this to a max would handle comparisons between positive
15101 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015102 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015103 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015104 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015105 Opcode = X86ISD::FMAX;
15106 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015107 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015108 // Converting this to a max would handle NaNs incorrectly, and swapping
15109 // the operands would cause it to handle comparisons between positive
15110 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015111 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015112 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015113 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15114 break;
15115 std::swap(LHS, RHS);
15116 }
Dan Gohman670e5392009-09-21 18:03:22 +000015117 Opcode = X86ISD::FMAX;
15118 break;
15119 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015120 // Converting this to a max would handle both negative zeros and NaNs
15121 // incorrectly, but we can swap the operands to fix both.
15122 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015123 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015124 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015125 case ISD::SETGE:
15126 Opcode = X86ISD::FMAX;
15127 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015128 }
Dan Gohman670e5392009-09-21 18:03:22 +000015129 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015130 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15131 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015132 switch (CC) {
15133 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015134 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015135 // Converting this to a min would handle comparisons between positive
15136 // and negative zero incorrectly, and swapping the operands would
15137 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015138 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015139 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015140 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015141 break;
15142 std::swap(LHS, RHS);
15143 }
Dan Gohman670e5392009-09-21 18:03:22 +000015144 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015145 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015146 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015147 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015148 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015149 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15150 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015151 Opcode = X86ISD::FMIN;
15152 break;
15153 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015154 // Converting this to a min would handle both negative zeros and NaNs
15155 // incorrectly, but we can swap the operands to fix both.
15156 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015157 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015158 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015159 case ISD::SETGE:
15160 Opcode = X86ISD::FMIN;
15161 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015162
Dan Gohman670e5392009-09-21 18:03:22 +000015163 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015164 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015165 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015166 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015167 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015168 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015169 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015170 // Converting this to a max would handle comparisons between positive
15171 // and negative zero incorrectly, and swapping the operands would
15172 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015173 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015174 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015175 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015176 break;
15177 std::swap(LHS, RHS);
15178 }
Dan Gohman670e5392009-09-21 18:03:22 +000015179 Opcode = X86ISD::FMAX;
15180 break;
15181 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015182 // Converting this to a max would handle both negative zeros and NaNs
15183 // incorrectly, but we can swap the operands to fix both.
15184 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015185 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015186 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015187 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015188 Opcode = X86ISD::FMAX;
15189 break;
15190 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015191 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015192
Chris Lattner47b4ce82009-03-11 05:48:52 +000015193 if (Opcode)
15194 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015195 }
Eric Christopherfd179292009-08-27 18:07:15 +000015196
Chris Lattnerd1980a52009-03-12 06:52:53 +000015197 // If this is a select between two integer constants, try to do some
15198 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015199 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15200 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015201 // Don't do this for crazy integer types.
15202 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15203 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015204 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015205 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015206
Chris Lattnercee56e72009-03-13 05:53:31 +000015207 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015208 // Efficiently invertible.
15209 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15210 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15211 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15212 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015213 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015214 }
Eric Christopherfd179292009-08-27 18:07:15 +000015215
Chris Lattnerd1980a52009-03-12 06:52:53 +000015216 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015217 if (FalseC->getAPIntValue() == 0 &&
15218 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015219 if (NeedsCondInvert) // Invert the condition if needed.
15220 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15221 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015222
Chris Lattnerd1980a52009-03-12 06:52:53 +000015223 // Zero extend the condition if needed.
15224 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015225
Chris Lattnercee56e72009-03-13 05:53:31 +000015226 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015227 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015228 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015229 }
Eric Christopherfd179292009-08-27 18:07:15 +000015230
Chris Lattner97a29a52009-03-13 05:22:11 +000015231 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015232 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015233 if (NeedsCondInvert) // Invert the condition if needed.
15234 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15235 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015236
Chris Lattner97a29a52009-03-13 05:22:11 +000015237 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015238 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15239 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015240 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015241 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015242 }
Eric Christopherfd179292009-08-27 18:07:15 +000015243
Chris Lattnercee56e72009-03-13 05:53:31 +000015244 // Optimize cases that will turn into an LEA instruction. This requires
15245 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015246 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015247 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015248 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015249
Chris Lattnercee56e72009-03-13 05:53:31 +000015250 bool isFastMultiplier = false;
15251 if (Diff < 10) {
15252 switch ((unsigned char)Diff) {
15253 default: break;
15254 case 1: // result = add base, cond
15255 case 2: // result = lea base( , cond*2)
15256 case 3: // result = lea base(cond, cond*2)
15257 case 4: // result = lea base( , cond*4)
15258 case 5: // result = lea base(cond, cond*4)
15259 case 8: // result = lea base( , cond*8)
15260 case 9: // result = lea base(cond, cond*8)
15261 isFastMultiplier = true;
15262 break;
15263 }
15264 }
Eric Christopherfd179292009-08-27 18:07:15 +000015265
Chris Lattnercee56e72009-03-13 05:53:31 +000015266 if (isFastMultiplier) {
15267 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15268 if (NeedsCondInvert) // Invert the condition if needed.
15269 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15270 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015271
Chris Lattnercee56e72009-03-13 05:53:31 +000015272 // Zero extend the condition if needed.
15273 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15274 Cond);
15275 // Scale the condition by the difference.
15276 if (Diff != 1)
15277 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15278 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015279
Chris Lattnercee56e72009-03-13 05:53:31 +000015280 // Add the base if non-zero.
15281 if (FalseC->getAPIntValue() != 0)
15282 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15283 SDValue(FalseC, 0));
15284 return Cond;
15285 }
Eric Christopherfd179292009-08-27 18:07:15 +000015286 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015287 }
15288 }
Eric Christopherfd179292009-08-27 18:07:15 +000015289
Evan Cheng56f582d2012-01-04 01:41:39 +000015290 // Canonicalize max and min:
15291 // (x > y) ? x : y -> (x >= y) ? x : y
15292 // (x < y) ? x : y -> (x <= y) ? x : y
15293 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15294 // the need for an extra compare
15295 // against zero. e.g.
15296 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15297 // subl %esi, %edi
15298 // testl %edi, %edi
15299 // movl $0, %eax
15300 // cmovgl %edi, %eax
15301 // =>
15302 // xorl %eax, %eax
15303 // subl %esi, $edi
15304 // cmovsl %eax, %edi
15305 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15306 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15307 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15308 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15309 switch (CC) {
15310 default: break;
15311 case ISD::SETLT:
15312 case ISD::SETGT: {
15313 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15314 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15315 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15316 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15317 }
15318 }
15319 }
15320
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015321 // Match VSELECTs into subs with unsigned saturation.
15322 if (!DCI.isBeforeLegalize() &&
15323 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15324 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15325 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15326 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15327 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15328
15329 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15330 // left side invert the predicate to simplify logic below.
15331 SDValue Other;
15332 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15333 Other = RHS;
15334 CC = ISD::getSetCCInverse(CC, true);
15335 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15336 Other = LHS;
15337 }
15338
15339 if (Other.getNode() && Other->getNumOperands() == 2 &&
15340 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15341 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15342 SDValue CondRHS = Cond->getOperand(1);
15343
15344 // Look for a general sub with unsigned saturation first.
15345 // x >= y ? x-y : 0 --> subus x, y
15346 // x > y ? x-y : 0 --> subus x, y
15347 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15348 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15349 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15350
15351 // If the RHS is a constant we have to reverse the const canonicalization.
15352 // x > C-1 ? x+-C : 0 --> subus x, C
15353 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15354 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15355 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015356 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015357 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015358 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015359 }
15360
15361 // Another special case: If C was a sign bit, the sub has been
15362 // canonicalized into a xor.
15363 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15364 // it's safe to decanonicalize the xor?
15365 // x s< 0 ? x^C : 0 --> subus x, C
15366 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15367 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15368 isSplatVector(OpRHS.getNode())) {
15369 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15370 if (A.isSignBit())
15371 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15372 }
15373 }
15374 }
15375
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015376 // Try to match a min/max vector operation.
15377 if (!DCI.isBeforeLegalize() &&
15378 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15379 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15380 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15381
Nadav Rotemcc616562012-01-15 19:27:55 +000015382 // If we know that this node is legal then we know that it is going to be
15383 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15384 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15385 // to simplify previous instructions.
15386 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15387 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015388 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015389 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015390
15391 // Don't optimize vector selects that map to mask-registers.
15392 if (BitWidth == 1)
15393 return SDValue();
15394
Nadav Rotemcc616562012-01-15 19:27:55 +000015395 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15396 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15397
15398 APInt KnownZero, KnownOne;
15399 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15400 DCI.isBeforeLegalizeOps());
15401 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15402 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15403 DCI.CommitTargetLoweringOpt(TLO);
15404 }
15405
Dan Gohman475871a2008-07-27 21:46:04 +000015406 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015407}
15408
Michael Liao2a33cec2012-08-10 19:58:13 +000015409// Check whether a boolean test is testing a boolean value generated by
15410// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15411// code.
15412//
15413// Simplify the following patterns:
15414// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15415// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15416// to (Op EFLAGS Cond)
15417//
15418// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15419// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15420// to (Op EFLAGS !Cond)
15421//
15422// where Op could be BRCOND or CMOV.
15423//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015424static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015425 // Quit if not CMP and SUB with its value result used.
15426 if (Cmp.getOpcode() != X86ISD::CMP &&
15427 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15428 return SDValue();
15429
15430 // Quit if not used as a boolean value.
15431 if (CC != X86::COND_E && CC != X86::COND_NE)
15432 return SDValue();
15433
15434 // Check CMP operands. One of them should be 0 or 1 and the other should be
15435 // an SetCC or extended from it.
15436 SDValue Op1 = Cmp.getOperand(0);
15437 SDValue Op2 = Cmp.getOperand(1);
15438
15439 SDValue SetCC;
15440 const ConstantSDNode* C = 0;
15441 bool needOppositeCond = (CC == X86::COND_E);
15442
15443 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15444 SetCC = Op2;
15445 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15446 SetCC = Op1;
15447 else // Quit if all operands are not constants.
15448 return SDValue();
15449
15450 if (C->getZExtValue() == 1)
15451 needOppositeCond = !needOppositeCond;
15452 else if (C->getZExtValue() != 0)
15453 // Quit if the constant is neither 0 or 1.
15454 return SDValue();
15455
15456 // Skip 'zext' node.
15457 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15458 SetCC = SetCC.getOperand(0);
15459
Michael Liao7fdc66b2012-09-10 16:36:16 +000015460 switch (SetCC.getOpcode()) {
15461 case X86ISD::SETCC:
15462 // Set the condition code or opposite one if necessary.
15463 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15464 if (needOppositeCond)
15465 CC = X86::GetOppositeBranchCondition(CC);
15466 return SetCC.getOperand(1);
15467 case X86ISD::CMOV: {
15468 // Check whether false/true value has canonical one, i.e. 0 or 1.
15469 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15470 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15471 // Quit if true value is not a constant.
15472 if (!TVal)
15473 return SDValue();
15474 // Quit if false value is not a constant.
15475 if (!FVal) {
15476 // A special case for rdrand, where 0 is set if false cond is found.
15477 SDValue Op = SetCC.getOperand(0);
15478 if (Op.getOpcode() != X86ISD::RDRAND)
15479 return SDValue();
15480 }
15481 // Quit if false value is not the constant 0 or 1.
15482 bool FValIsFalse = true;
15483 if (FVal && FVal->getZExtValue() != 0) {
15484 if (FVal->getZExtValue() != 1)
15485 return SDValue();
15486 // If FVal is 1, opposite cond is needed.
15487 needOppositeCond = !needOppositeCond;
15488 FValIsFalse = false;
15489 }
15490 // Quit if TVal is not the constant opposite of FVal.
15491 if (FValIsFalse && TVal->getZExtValue() != 1)
15492 return SDValue();
15493 if (!FValIsFalse && TVal->getZExtValue() != 0)
15494 return SDValue();
15495 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15496 if (needOppositeCond)
15497 CC = X86::GetOppositeBranchCondition(CC);
15498 return SetCC.getOperand(3);
15499 }
15500 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015501
Michael Liao7fdc66b2012-09-10 16:36:16 +000015502 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015503}
15504
Chris Lattnerd1980a52009-03-12 06:52:53 +000015505/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15506static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015507 TargetLowering::DAGCombinerInfo &DCI,
15508 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015509 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015510
Chris Lattnerd1980a52009-03-12 06:52:53 +000015511 // If the flag operand isn't dead, don't touch this CMOV.
15512 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15513 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015514
Evan Chengb5a55d92011-05-24 01:48:22 +000015515 SDValue FalseOp = N->getOperand(0);
15516 SDValue TrueOp = N->getOperand(1);
15517 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15518 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015519
Evan Chengb5a55d92011-05-24 01:48:22 +000015520 if (CC == X86::COND_E || CC == X86::COND_NE) {
15521 switch (Cond.getOpcode()) {
15522 default: break;
15523 case X86ISD::BSR:
15524 case X86ISD::BSF:
15525 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15526 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15527 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15528 }
15529 }
15530
Michael Liao2a33cec2012-08-10 19:58:13 +000015531 SDValue Flags;
15532
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015533 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015534 if (Flags.getNode() &&
15535 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015536 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015537 SDValue Ops[] = { FalseOp, TrueOp,
15538 DAG.getConstant(CC, MVT::i8), Flags };
15539 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15540 Ops, array_lengthof(Ops));
15541 }
15542
Chris Lattnerd1980a52009-03-12 06:52:53 +000015543 // If this is a select between two integer constants, try to do some
15544 // optimizations. Note that the operands are ordered the opposite of SELECT
15545 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015546 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15547 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015548 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15549 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015550 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15551 CC = X86::GetOppositeBranchCondition(CC);
15552 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015553 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015554 }
Eric Christopherfd179292009-08-27 18:07:15 +000015555
Chris Lattnerd1980a52009-03-12 06:52:53 +000015556 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015557 // This is efficient for any integer data type (including i8/i16) and
15558 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015559 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015560 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15561 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015562
Chris Lattnerd1980a52009-03-12 06:52:53 +000015563 // Zero extend the condition if needed.
15564 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015565
Chris Lattnerd1980a52009-03-12 06:52:53 +000015566 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15567 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015568 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015569 if (N->getNumValues() == 2) // Dead flag value?
15570 return DCI.CombineTo(N, Cond, SDValue());
15571 return Cond;
15572 }
Eric Christopherfd179292009-08-27 18:07:15 +000015573
Chris Lattnercee56e72009-03-13 05:53:31 +000015574 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15575 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015576 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015577 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15578 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015579
Chris Lattner97a29a52009-03-13 05:22:11 +000015580 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015581 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15582 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015583 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15584 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015585
Chris Lattner97a29a52009-03-13 05:22:11 +000015586 if (N->getNumValues() == 2) // Dead flag value?
15587 return DCI.CombineTo(N, Cond, SDValue());
15588 return Cond;
15589 }
Eric Christopherfd179292009-08-27 18:07:15 +000015590
Chris Lattnercee56e72009-03-13 05:53:31 +000015591 // Optimize cases that will turn into an LEA instruction. This requires
15592 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015593 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015594 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015595 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015596
Chris Lattnercee56e72009-03-13 05:53:31 +000015597 bool isFastMultiplier = false;
15598 if (Diff < 10) {
15599 switch ((unsigned char)Diff) {
15600 default: break;
15601 case 1: // result = add base, cond
15602 case 2: // result = lea base( , cond*2)
15603 case 3: // result = lea base(cond, cond*2)
15604 case 4: // result = lea base( , cond*4)
15605 case 5: // result = lea base(cond, cond*4)
15606 case 8: // result = lea base( , cond*8)
15607 case 9: // result = lea base(cond, cond*8)
15608 isFastMultiplier = true;
15609 break;
15610 }
15611 }
Eric Christopherfd179292009-08-27 18:07:15 +000015612
Chris Lattnercee56e72009-03-13 05:53:31 +000015613 if (isFastMultiplier) {
15614 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015615 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15616 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015617 // Zero extend the condition if needed.
15618 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15619 Cond);
15620 // Scale the condition by the difference.
15621 if (Diff != 1)
15622 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15623 DAG.getConstant(Diff, Cond.getValueType()));
15624
15625 // Add the base if non-zero.
15626 if (FalseC->getAPIntValue() != 0)
15627 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15628 SDValue(FalseC, 0));
15629 if (N->getNumValues() == 2) // Dead flag value?
15630 return DCI.CombineTo(N, Cond, SDValue());
15631 return Cond;
15632 }
Eric Christopherfd179292009-08-27 18:07:15 +000015633 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015634 }
15635 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015636
15637 // Handle these cases:
15638 // (select (x != c), e, c) -> select (x != c), e, x),
15639 // (select (x == c), c, e) -> select (x == c), x, e)
15640 // where the c is an integer constant, and the "select" is the combination
15641 // of CMOV and CMP.
15642 //
15643 // The rationale for this change is that the conditional-move from a constant
15644 // needs two instructions, however, conditional-move from a register needs
15645 // only one instruction.
15646 //
15647 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15648 // some instruction-combining opportunities. This opt needs to be
15649 // postponed as late as possible.
15650 //
15651 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15652 // the DCI.xxxx conditions are provided to postpone the optimization as
15653 // late as possible.
15654
15655 ConstantSDNode *CmpAgainst = 0;
15656 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15657 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15658 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15659
15660 if (CC == X86::COND_NE &&
15661 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15662 CC = X86::GetOppositeBranchCondition(CC);
15663 std::swap(TrueOp, FalseOp);
15664 }
15665
15666 if (CC == X86::COND_E &&
15667 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15668 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15669 DAG.getConstant(CC, MVT::i8), Cond };
15670 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15671 array_lengthof(Ops));
15672 }
15673 }
15674 }
15675
Chris Lattnerd1980a52009-03-12 06:52:53 +000015676 return SDValue();
15677}
15678
Evan Cheng0b0cd912009-03-28 05:57:29 +000015679/// PerformMulCombine - Optimize a single multiply with constant into two
15680/// in order to implement it with two cheaper instructions, e.g.
15681/// LEA + SHL, LEA + LEA.
15682static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15683 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015684 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15685 return SDValue();
15686
Owen Andersone50ed302009-08-10 22:56:29 +000015687 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015688 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015689 return SDValue();
15690
15691 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15692 if (!C)
15693 return SDValue();
15694 uint64_t MulAmt = C->getZExtValue();
15695 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15696 return SDValue();
15697
15698 uint64_t MulAmt1 = 0;
15699 uint64_t MulAmt2 = 0;
15700 if ((MulAmt % 9) == 0) {
15701 MulAmt1 = 9;
15702 MulAmt2 = MulAmt / 9;
15703 } else if ((MulAmt % 5) == 0) {
15704 MulAmt1 = 5;
15705 MulAmt2 = MulAmt / 5;
15706 } else if ((MulAmt % 3) == 0) {
15707 MulAmt1 = 3;
15708 MulAmt2 = MulAmt / 3;
15709 }
15710 if (MulAmt2 &&
15711 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15712 DebugLoc DL = N->getDebugLoc();
15713
15714 if (isPowerOf2_64(MulAmt2) &&
15715 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15716 // If second multiplifer is pow2, issue it first. We want the multiply by
15717 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15718 // is an add.
15719 std::swap(MulAmt1, MulAmt2);
15720
15721 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015722 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015723 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015724 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015725 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015726 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015727 DAG.getConstant(MulAmt1, VT));
15728
Eric Christopherfd179292009-08-27 18:07:15 +000015729 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015730 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015731 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015732 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015733 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015734 DAG.getConstant(MulAmt2, VT));
15735
15736 // Do not add new nodes to DAG combiner worklist.
15737 DCI.CombineTo(N, NewMul, false);
15738 }
15739 return SDValue();
15740}
15741
Evan Chengad9c0a32009-12-15 00:53:42 +000015742static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15743 SDValue N0 = N->getOperand(0);
15744 SDValue N1 = N->getOperand(1);
15745 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15746 EVT VT = N0.getValueType();
15747
15748 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15749 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015750 if (VT.isInteger() && !VT.isVector() &&
15751 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015752 N0.getOperand(1).getOpcode() == ISD::Constant) {
15753 SDValue N00 = N0.getOperand(0);
15754 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15755 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15756 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15757 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15758 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15759 APInt ShAmt = N1C->getAPIntValue();
15760 Mask = Mask.shl(ShAmt);
15761 if (Mask != 0)
15762 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15763 N00, DAG.getConstant(Mask, VT));
15764 }
15765 }
15766
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015767 // Hardware support for vector shifts is sparse which makes us scalarize the
15768 // vector operations in many cases. Also, on sandybridge ADD is faster than
15769 // shl.
15770 // (shl V, 1) -> add V,V
15771 if (isSplatVector(N1.getNode())) {
15772 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15773 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15774 // We shift all of the values by one. In many cases we do not have
15775 // hardware support for this operation. This is better expressed as an ADD
15776 // of two values.
15777 if (N1C && (1 == N1C->getZExtValue())) {
15778 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15779 }
15780 }
15781
Evan Chengad9c0a32009-12-15 00:53:42 +000015782 return SDValue();
15783}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015784
Nate Begeman740ab032009-01-26 00:52:55 +000015785/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15786/// when possible.
15787static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015788 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015789 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015790 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015791 if (N->getOpcode() == ISD::SHL) {
15792 SDValue V = PerformSHLCombine(N, DAG);
15793 if (V.getNode()) return V;
15794 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015795
Nate Begeman740ab032009-01-26 00:52:55 +000015796 // On X86 with SSE2 support, we can transform this to a vector shift if
15797 // all elements are shifted by the same amount. We can't do this in legalize
15798 // because the a constant vector is typically transformed to a constant pool
15799 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015800 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015801 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015802
Craig Topper7be5dfd2011-11-12 09:58:49 +000015803 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015804 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015805 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015806 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015807
Mon P Wang3becd092009-01-28 08:12:05 +000015808 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015809 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015810 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015811 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015812 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15813 unsigned NumElts = VT.getVectorNumElements();
15814 unsigned i = 0;
15815 for (; i != NumElts; ++i) {
15816 SDValue Arg = ShAmtOp.getOperand(i);
15817 if (Arg.getOpcode() == ISD::UNDEF) continue;
15818 BaseShAmt = Arg;
15819 break;
15820 }
Craig Topper37c26772012-01-17 04:44:50 +000015821 // Handle the case where the build_vector is all undef
15822 // FIXME: Should DAG allow this?
15823 if (i == NumElts)
15824 return SDValue();
15825
Mon P Wang3becd092009-01-28 08:12:05 +000015826 for (; i != NumElts; ++i) {
15827 SDValue Arg = ShAmtOp.getOperand(i);
15828 if (Arg.getOpcode() == ISD::UNDEF) continue;
15829 if (Arg != BaseShAmt) {
15830 return SDValue();
15831 }
15832 }
15833 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015834 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015835 SDValue InVec = ShAmtOp.getOperand(0);
15836 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15837 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15838 unsigned i = 0;
15839 for (; i != NumElts; ++i) {
15840 SDValue Arg = InVec.getOperand(i);
15841 if (Arg.getOpcode() == ISD::UNDEF) continue;
15842 BaseShAmt = Arg;
15843 break;
15844 }
15845 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15846 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015847 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015848 if (C->getZExtValue() == SplatIdx)
15849 BaseShAmt = InVec.getOperand(1);
15850 }
15851 }
Mon P Wang845b1892012-02-01 22:15:20 +000015852 if (BaseShAmt.getNode() == 0) {
15853 // Don't create instructions with illegal types after legalize
15854 // types has run.
15855 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15856 !DCI.isBeforeLegalize())
15857 return SDValue();
15858
Mon P Wangefa42202009-09-03 19:56:25 +000015859 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15860 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015861 }
Mon P Wang3becd092009-01-28 08:12:05 +000015862 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015863 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015864
Mon P Wangefa42202009-09-03 19:56:25 +000015865 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015866 if (EltVT.bitsGT(MVT::i32))
15867 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15868 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015869 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015870
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015871 // The shift amount is identical so we can do a vector shift.
15872 SDValue ValOp = N->getOperand(0);
15873 switch (N->getOpcode()) {
15874 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015875 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015876 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015877 switch (VT.getSimpleVT().SimpleTy) {
15878 default: return SDValue();
15879 case MVT::v2i64:
15880 case MVT::v4i32:
15881 case MVT::v8i16:
15882 case MVT::v4i64:
15883 case MVT::v8i32:
15884 case MVT::v16i16:
15885 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15886 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015887 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015888 switch (VT.getSimpleVT().SimpleTy) {
15889 default: return SDValue();
15890 case MVT::v4i32:
15891 case MVT::v8i16:
15892 case MVT::v8i32:
15893 case MVT::v16i16:
15894 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15895 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015896 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015897 switch (VT.getSimpleVT().SimpleTy) {
15898 default: return SDValue();
15899 case MVT::v2i64:
15900 case MVT::v4i32:
15901 case MVT::v8i16:
15902 case MVT::v4i64:
15903 case MVT::v8i32:
15904 case MVT::v16i16:
15905 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15906 }
Nate Begeman740ab032009-01-26 00:52:55 +000015907 }
Nate Begeman740ab032009-01-26 00:52:55 +000015908}
15909
Stuart Hastings865f0932011-06-03 23:53:54 +000015910// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15911// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15912// and friends. Likewise for OR -> CMPNEQSS.
15913static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15914 TargetLowering::DAGCombinerInfo &DCI,
15915 const X86Subtarget *Subtarget) {
15916 unsigned opcode;
15917
15918 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15919 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015920 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015921 SDValue N0 = N->getOperand(0);
15922 SDValue N1 = N->getOperand(1);
15923 SDValue CMP0 = N0->getOperand(1);
15924 SDValue CMP1 = N1->getOperand(1);
15925 DebugLoc DL = N->getDebugLoc();
15926
15927 // The SETCCs should both refer to the same CMP.
15928 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15929 return SDValue();
15930
15931 SDValue CMP00 = CMP0->getOperand(0);
15932 SDValue CMP01 = CMP0->getOperand(1);
15933 EVT VT = CMP00.getValueType();
15934
15935 if (VT == MVT::f32 || VT == MVT::f64) {
15936 bool ExpectingFlags = false;
15937 // Check for any users that want flags:
15938 for (SDNode::use_iterator UI = N->use_begin(),
15939 UE = N->use_end();
15940 !ExpectingFlags && UI != UE; ++UI)
15941 switch (UI->getOpcode()) {
15942 default:
15943 case ISD::BR_CC:
15944 case ISD::BRCOND:
15945 case ISD::SELECT:
15946 ExpectingFlags = true;
15947 break;
15948 case ISD::CopyToReg:
15949 case ISD::SIGN_EXTEND:
15950 case ISD::ZERO_EXTEND:
15951 case ISD::ANY_EXTEND:
15952 break;
15953 }
15954
15955 if (!ExpectingFlags) {
15956 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15957 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15958
15959 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15960 X86::CondCode tmp = cc0;
15961 cc0 = cc1;
15962 cc1 = tmp;
15963 }
15964
15965 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15966 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15967 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15968 X86ISD::NodeType NTOperator = is64BitFP ?
15969 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15970 // FIXME: need symbolic constants for these magic numbers.
15971 // See X86ATTInstPrinter.cpp:printSSECC().
15972 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15973 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15974 DAG.getConstant(x86cc, MVT::i8));
15975 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15976 OnesOrZeroesF);
15977 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15978 DAG.getConstant(1, MVT::i32));
15979 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15980 return OneBitOfTruth;
15981 }
15982 }
15983 }
15984 }
15985 return SDValue();
15986}
15987
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015988/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15989/// so it can be folded inside ANDNP.
15990static bool CanFoldXORWithAllOnes(const SDNode *N) {
15991 EVT VT = N->getValueType(0);
15992
15993 // Match direct AllOnes for 128 and 256-bit vectors
15994 if (ISD::isBuildVectorAllOnes(N))
15995 return true;
15996
15997 // Look through a bit convert.
15998 if (N->getOpcode() == ISD::BITCAST)
15999 N = N->getOperand(0).getNode();
16000
16001 // Sometimes the operand may come from a insert_subvector building a 256-bit
16002 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016003 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016004 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16005 SDValue V1 = N->getOperand(0);
16006 SDValue V2 = N->getOperand(1);
16007
16008 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16009 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16010 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16011 ISD::isBuildVectorAllOnes(V2.getNode()))
16012 return true;
16013 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016014
16015 return false;
16016}
16017
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016018// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16019// register. In most cases we actually compare or select YMM-sized registers
16020// and mixing the two types creates horrible code. This method optimizes
16021// some of the transition sequences.
16022static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16023 TargetLowering::DAGCombinerInfo &DCI,
16024 const X86Subtarget *Subtarget) {
16025 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016026 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016027 return SDValue();
16028
16029 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16030 N->getOpcode() == ISD::ZERO_EXTEND ||
16031 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16032
16033 SDValue Narrow = N->getOperand(0);
16034 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016035 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016036 return SDValue();
16037
16038 if (Narrow->getOpcode() != ISD::XOR &&
16039 Narrow->getOpcode() != ISD::AND &&
16040 Narrow->getOpcode() != ISD::OR)
16041 return SDValue();
16042
16043 SDValue N0 = Narrow->getOperand(0);
16044 SDValue N1 = Narrow->getOperand(1);
16045 DebugLoc DL = Narrow->getDebugLoc();
16046
16047 // The Left side has to be a trunc.
16048 if (N0.getOpcode() != ISD::TRUNCATE)
16049 return SDValue();
16050
16051 // The type of the truncated inputs.
16052 EVT WideVT = N0->getOperand(0)->getValueType(0);
16053 if (WideVT != VT)
16054 return SDValue();
16055
16056 // The right side has to be a 'trunc' or a constant vector.
16057 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16058 bool RHSConst = (isSplatVector(N1.getNode()) &&
16059 isa<ConstantSDNode>(N1->getOperand(0)));
16060 if (!RHSTrunc && !RHSConst)
16061 return SDValue();
16062
16063 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16064
16065 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16066 return SDValue();
16067
16068 // Set N0 and N1 to hold the inputs to the new wide operation.
16069 N0 = N0->getOperand(0);
16070 if (RHSConst) {
16071 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16072 N1->getOperand(0));
16073 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16074 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16075 } else if (RHSTrunc) {
16076 N1 = N1->getOperand(0);
16077 }
16078
16079 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016080 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016081 unsigned Opcode = N->getOpcode();
16082 switch (Opcode) {
16083 case ISD::ANY_EXTEND:
16084 return Op;
16085 case ISD::ZERO_EXTEND: {
16086 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16087 APInt Mask = APInt::getAllOnesValue(InBits);
16088 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16089 return DAG.getNode(ISD::AND, DL, VT,
16090 Op, DAG.getConstant(Mask, VT));
16091 }
16092 case ISD::SIGN_EXTEND:
16093 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16094 Op, DAG.getValueType(NarrowVT));
16095 default:
16096 llvm_unreachable("Unexpected opcode");
16097 }
16098}
16099
Nate Begemanb65c1752010-12-17 22:55:37 +000016100static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16101 TargetLowering::DAGCombinerInfo &DCI,
16102 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016103 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016104 if (DCI.isBeforeLegalizeOps())
16105 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016106
Stuart Hastings865f0932011-06-03 23:53:54 +000016107 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16108 if (R.getNode())
16109 return R;
16110
Craig Topperb926afc2012-12-17 05:12:30 +000016111 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016112 // BLSI is X & (-X)
16113 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016114 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16115 SDValue N0 = N->getOperand(0);
16116 SDValue N1 = N->getOperand(1);
16117 DebugLoc DL = N->getDebugLoc();
16118
Craig Topperb4c94572011-10-21 06:55:01 +000016119 // Check LHS for neg
16120 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16121 isZero(N0.getOperand(0)))
16122 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16123
16124 // Check RHS for neg
16125 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16126 isZero(N1.getOperand(0)))
16127 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16128
16129 // Check LHS for X-1
16130 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16131 isAllOnes(N0.getOperand(1)))
16132 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16133
16134 // Check RHS for X-1
16135 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16136 isAllOnes(N1.getOperand(1)))
16137 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16138
Craig Topper54a11172011-10-14 07:06:56 +000016139 return SDValue();
16140 }
16141
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016142 // Want to form ANDNP nodes:
16143 // 1) In the hopes of then easily combining them with OR and AND nodes
16144 // to form PBLEND/PSIGN.
16145 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016146 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016147 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016148
Nate Begemanb65c1752010-12-17 22:55:37 +000016149 SDValue N0 = N->getOperand(0);
16150 SDValue N1 = N->getOperand(1);
16151 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016152
Nate Begemanb65c1752010-12-17 22:55:37 +000016153 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016154 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016155 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16156 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016157 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016158
16159 // Check RHS for vnot
16160 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016161 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16162 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016163 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016164
Nate Begemanb65c1752010-12-17 22:55:37 +000016165 return SDValue();
16166}
16167
Evan Cheng760d1942010-01-04 21:22:48 +000016168static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016169 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016170 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016171 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016172 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016173 return SDValue();
16174
Stuart Hastings865f0932011-06-03 23:53:54 +000016175 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16176 if (R.getNode())
16177 return R;
16178
Evan Cheng760d1942010-01-04 21:22:48 +000016179 SDValue N0 = N->getOperand(0);
16180 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016181
Nate Begemanb65c1752010-12-17 22:55:37 +000016182 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016183 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016184 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016185 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016186 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016187
Craig Topper1666cb62011-11-19 07:07:26 +000016188 // Canonicalize pandn to RHS
16189 if (N0.getOpcode() == X86ISD::ANDNP)
16190 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016191 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016192 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16193 SDValue Mask = N1.getOperand(0);
16194 SDValue X = N1.getOperand(1);
16195 SDValue Y;
16196 if (N0.getOperand(0) == Mask)
16197 Y = N0.getOperand(1);
16198 if (N0.getOperand(1) == Mask)
16199 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016200
Craig Topper1666cb62011-11-19 07:07:26 +000016201 // Check to see if the mask appeared in both the AND and ANDNP and
16202 if (!Y.getNode())
16203 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016204
Craig Topper1666cb62011-11-19 07:07:26 +000016205 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016206 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016207 if (Mask.getOpcode() == ISD::BITCAST)
16208 Mask = Mask.getOperand(0);
16209 if (X.getOpcode() == ISD::BITCAST)
16210 X = X.getOperand(0);
16211 if (Y.getOpcode() == ISD::BITCAST)
16212 Y = Y.getOperand(0);
16213
Craig Topper1666cb62011-11-19 07:07:26 +000016214 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016215
Craig Toppered2e13d2012-01-22 19:15:14 +000016216 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016217 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16218 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016219 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016220 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016221
16222 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016223 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016224 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16225 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16226 if ((SraAmt + 1) != EltBits)
16227 return SDValue();
16228
16229 DebugLoc DL = N->getDebugLoc();
16230
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016231 // We are going to replace the AND, OR, NAND with either BLEND
16232 // or PSIGN, which only look at the MSB. The VSRAI instruction
16233 // does not affect the highest bit, so we can get rid of it.
16234 Mask = Mask.getOperand(0);
16235
Craig Topper1666cb62011-11-19 07:07:26 +000016236 // Now we know we at least have a plendvb with the mask val. See if
16237 // we can form a psignb/w/d.
16238 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016239 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16240 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016241 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16242 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16243 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016244 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000016245 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016246 }
16247 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016248 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016249 return SDValue();
16250
16251 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16252
16253 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16254 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16255 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016256 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016257 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016258 }
16259 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016260
Craig Topper1666cb62011-11-19 07:07:26 +000016261 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16262 return SDValue();
16263
Nate Begemanb65c1752010-12-17 22:55:37 +000016264 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016265 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16266 std::swap(N0, N1);
16267 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16268 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016269 if (!N0.hasOneUse() || !N1.hasOneUse())
16270 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016271
16272 SDValue ShAmt0 = N0.getOperand(1);
16273 if (ShAmt0.getValueType() != MVT::i8)
16274 return SDValue();
16275 SDValue ShAmt1 = N1.getOperand(1);
16276 if (ShAmt1.getValueType() != MVT::i8)
16277 return SDValue();
16278 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16279 ShAmt0 = ShAmt0.getOperand(0);
16280 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16281 ShAmt1 = ShAmt1.getOperand(0);
16282
16283 DebugLoc DL = N->getDebugLoc();
16284 unsigned Opc = X86ISD::SHLD;
16285 SDValue Op0 = N0.getOperand(0);
16286 SDValue Op1 = N1.getOperand(0);
16287 if (ShAmt0.getOpcode() == ISD::SUB) {
16288 Opc = X86ISD::SHRD;
16289 std::swap(Op0, Op1);
16290 std::swap(ShAmt0, ShAmt1);
16291 }
16292
Evan Cheng8b1190a2010-04-28 01:18:01 +000016293 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016294 if (ShAmt1.getOpcode() == ISD::SUB) {
16295 SDValue Sum = ShAmt1.getOperand(0);
16296 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016297 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16298 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16299 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16300 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016301 return DAG.getNode(Opc, DL, VT,
16302 Op0, Op1,
16303 DAG.getNode(ISD::TRUNCATE, DL,
16304 MVT::i8, ShAmt0));
16305 }
16306 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16307 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16308 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016309 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016310 return DAG.getNode(Opc, DL, VT,
16311 N0.getOperand(0), N1.getOperand(0),
16312 DAG.getNode(ISD::TRUNCATE, DL,
16313 MVT::i8, ShAmt0));
16314 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016315
Evan Cheng760d1942010-01-04 21:22:48 +000016316 return SDValue();
16317}
16318
Manman Ren92363622012-06-07 22:39:10 +000016319// Generate NEG and CMOV for integer abs.
16320static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16321 EVT VT = N->getValueType(0);
16322
16323 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16324 // 8-bit integer abs to NEG and CMOV.
16325 if (VT.isInteger() && VT.getSizeInBits() == 8)
16326 return SDValue();
16327
16328 SDValue N0 = N->getOperand(0);
16329 SDValue N1 = N->getOperand(1);
16330 DebugLoc DL = N->getDebugLoc();
16331
16332 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16333 // and change it to SUB and CMOV.
16334 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16335 N0.getOpcode() == ISD::ADD &&
16336 N0.getOperand(1) == N1 &&
16337 N1.getOpcode() == ISD::SRA &&
16338 N1.getOperand(0) == N0.getOperand(0))
16339 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16340 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16341 // Generate SUB & CMOV.
16342 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16343 DAG.getConstant(0, VT), N0.getOperand(0));
16344
16345 SDValue Ops[] = { N0.getOperand(0), Neg,
16346 DAG.getConstant(X86::COND_GE, MVT::i8),
16347 SDValue(Neg.getNode(), 1) };
16348 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16349 Ops, array_lengthof(Ops));
16350 }
16351 return SDValue();
16352}
16353
Craig Topper3738ccd2011-12-27 06:27:23 +000016354// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016355static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16356 TargetLowering::DAGCombinerInfo &DCI,
16357 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016358 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016359 if (DCI.isBeforeLegalizeOps())
16360 return SDValue();
16361
Manman Ren45d53b82012-06-08 18:58:26 +000016362 if (Subtarget->hasCMov()) {
16363 SDValue RV = performIntegerAbsCombine(N, DAG);
16364 if (RV.getNode())
16365 return RV;
16366 }
Manman Ren92363622012-06-07 22:39:10 +000016367
16368 // Try forming BMI if it is available.
16369 if (!Subtarget->hasBMI())
16370 return SDValue();
16371
Craig Topperb4c94572011-10-21 06:55:01 +000016372 if (VT != MVT::i32 && VT != MVT::i64)
16373 return SDValue();
16374
Craig Topper3738ccd2011-12-27 06:27:23 +000016375 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16376
Craig Topperb4c94572011-10-21 06:55:01 +000016377 // Create BLSMSK instructions by finding X ^ (X-1)
16378 SDValue N0 = N->getOperand(0);
16379 SDValue N1 = N->getOperand(1);
16380 DebugLoc DL = N->getDebugLoc();
16381
16382 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16383 isAllOnes(N0.getOperand(1)))
16384 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16385
16386 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16387 isAllOnes(N1.getOperand(1)))
16388 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16389
16390 return SDValue();
16391}
16392
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016393/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16394static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016395 TargetLowering::DAGCombinerInfo &DCI,
16396 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016397 LoadSDNode *Ld = cast<LoadSDNode>(N);
16398 EVT RegVT = Ld->getValueType(0);
16399 EVT MemVT = Ld->getMemoryVT();
16400 DebugLoc dl = Ld->getDebugLoc();
16401 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016402 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016403
16404 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016405 unsigned Alignment = Ld->getAlignment();
Nadav Rotemba958652013-01-19 08:38:41 +000016406 bool IsAligned = Alignment == 0 || Alignment == MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016407
16408 // On Sandybridge unaligned 256bit loads are inefficient.
16409 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016410 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016411 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016412 if (NumElems < 2)
16413 return SDValue();
16414
Nadav Rotem48177ac2013-01-18 23:10:30 +000016415 SDValue Ptr = Ld->getBasePtr();
16416 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16417
16418 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16419 NumElems/2);
16420 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16421 Ld->getPointerInfo(), Ld->isVolatile(),
16422 Ld->isNonTemporal(), Ld->isInvariant(),
16423 Alignment);
16424 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16425 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16426 Ld->getPointerInfo(), Ld->isVolatile(),
16427 Ld->isNonTemporal(), Ld->isInvariant(),
Nadav Rotemba958652013-01-19 08:38:41 +000016428 std::max(Alignment/2U, 1U));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016429 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16430 Load1.getValue(1),
16431 Load2.getValue(1));
16432
16433 SDValue NewVec = DAG.getUNDEF(RegVT);
16434 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16435 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16436 return DCI.CombineTo(N, NewVec, TF, true);
16437 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016438
Nadav Rotemca6f2962011-09-18 19:00:23 +000016439 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016440 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16441 // expansion is still better than scalar code.
16442 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16443 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016444 // TODO: It is possible to support ZExt by zeroing the undef values
16445 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016446 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16447 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016448 assert(MemVT != RegVT && "Cannot extend to the same type");
16449 assert(MemVT.isVector() && "Must load a vector from memory");
16450
16451 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016452 unsigned MemSz = MemVT.getSizeInBits();
16453 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016454
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016455 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16456 return SDValue();
16457
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016458 // All sizes must be a power of two.
16459 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16460 return SDValue();
16461
16462 // Attempt to load the original value using scalar loads.
16463 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016464 MVT SclrLoadTy = MVT::i8;
16465 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16466 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16467 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016468 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016469 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016470 }
16471 }
16472
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016473 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16474 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16475 (64 <= MemSz))
16476 SclrLoadTy = MVT::f64;
16477
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016478 // Calculate the number of scalar loads that we need to perform
16479 // in order to load our vector from memory.
16480 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016481 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16482 return SDValue();
16483
16484 unsigned loadRegZize = RegSz;
16485 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16486 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016487
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016488 // Represent our vector as a sequence of elements which are the
16489 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016490 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016491 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016492
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016493 // Represent the data using the same element type that is stored in
16494 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016495 EVT WideVecVT =
16496 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016497 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016498
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016499 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16500 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016501
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016502 // We can't shuffle using an illegal type.
16503 if (!TLI.isTypeLegal(WideVecVT))
16504 return SDValue();
16505
16506 SmallVector<SDValue, 8> Chains;
16507 SDValue Ptr = Ld->getBasePtr();
16508 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16509 TLI.getPointerTy());
16510 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16511
16512 for (unsigned i = 0; i < NumLoads; ++i) {
16513 // Perform a single load.
16514 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16515 Ptr, Ld->getPointerInfo(),
16516 Ld->isVolatile(), Ld->isNonTemporal(),
16517 Ld->isInvariant(), Ld->getAlignment());
16518 Chains.push_back(ScalarLoad.getValue(1));
16519 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16520 // another round of DAGCombining.
16521 if (i == 0)
16522 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16523 else
16524 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16525 ScalarLoad, DAG.getIntPtrConstant(i));
16526
16527 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16528 }
16529
16530 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16531 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016532
16533 // Bitcast the loaded value to a vector of the original element type, in
16534 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016535 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016536 unsigned SizeRatio = RegSz/MemSz;
16537
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016538 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016539 // If we have SSE4.1 we can directly emit a VSEXT node.
16540 if (Subtarget->hasSSE41()) {
16541 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16542 return DCI.CombineTo(N, Sext, TF, true);
16543 }
16544
16545 // Otherwise we'll shuffle the small elements in the high bits of the
16546 // larger type and perform an arithmetic shift. If the shift is not legal
16547 // it's better to scalarize.
16548 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16549 return SDValue();
16550
16551 // Redistribute the loaded elements into the different locations.
16552 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16553 for (unsigned i = 0; i != NumElems; ++i)
16554 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16555
16556 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16557 DAG.getUNDEF(WideVecVT),
16558 &ShuffleVec[0]);
16559
16560 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16561
16562 // Build the arithmetic shift.
16563 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16564 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016565 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16566 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016567
16568 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016569 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016570
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016571 // Redistribute the loaded elements into the different locations.
16572 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016573 for (unsigned i = 0; i != NumElems; ++i)
16574 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016575
16576 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016577 DAG.getUNDEF(WideVecVT),
16578 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016579
16580 // Bitcast to the requested type.
16581 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16582 // Replace the original load with the new sequence
16583 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016584 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016585 }
16586
16587 return SDValue();
16588}
16589
Chris Lattner149a4e52008-02-22 02:09:43 +000016590/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016591static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016592 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016593 StoreSDNode *St = cast<StoreSDNode>(N);
16594 EVT VT = St->getValue().getValueType();
16595 EVT StVT = St->getMemoryVT();
16596 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016597 SDValue StoredVal = St->getOperand(1);
16598 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotemba958652013-01-19 08:38:41 +000016599 unsigned Alignment = St->getAlignment();
16600 bool IsAligned = Alignment == 0 || Alignment == VT.getSizeInBits()/8;
Nadav Rotem5e742a32011-08-11 16:41:21 +000016601
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016602 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016603 // On Sandy Bridge, 256-bit memory operations are executed by two
16604 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16605 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016606 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016607 StVT == VT && !IsAligned) {
16608 unsigned NumElems = VT.getVectorNumElements();
16609 if (NumElems < 2)
16610 return SDValue();
16611
16612 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16613 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016614
16615 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16616 SDValue Ptr0 = St->getBasePtr();
16617 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16618
16619 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16620 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016621 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016622 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16623 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016624 St->isNonTemporal(),
16625 std::max(Alignment/2U, 1U));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016626 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16627 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016628
16629 // Optimize trunc store (of multiple scalars) to shuffle and store.
16630 // First, pack all of the elements in one place. Next, store to memory
16631 // in fewer chunks.
16632 if (St->isTruncatingStore() && VT.isVector()) {
16633 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16634 unsigned NumElems = VT.getVectorNumElements();
16635 assert(StVT != VT && "Cannot truncate to the same type");
16636 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16637 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16638
16639 // From, To sizes and ElemCount must be pow of two
16640 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016641 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016642 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016643 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016644
Nadav Rotem614061b2011-08-10 19:30:14 +000016645 unsigned SizeRatio = FromSz / ToSz;
16646
16647 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16648
16649 // Create a type on which we perform the shuffle
16650 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16651 StVT.getScalarType(), NumElems*SizeRatio);
16652
16653 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16654
16655 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16656 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016657 for (unsigned i = 0; i != NumElems; ++i)
16658 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016659
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016660 // Can't shuffle using an illegal type.
16661 if (!TLI.isTypeLegal(WideVecVT))
16662 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016663
16664 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016665 DAG.getUNDEF(WideVecVT),
16666 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016667 // At this point all of the data is stored at the bottom of the
16668 // register. We now need to save it to mem.
16669
16670 // Find the largest store unit
16671 MVT StoreType = MVT::i8;
16672 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16673 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16674 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016675 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016676 StoreType = Tp;
16677 }
16678
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016679 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16680 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16681 (64 <= NumElems * ToSz))
16682 StoreType = MVT::f64;
16683
Nadav Rotem614061b2011-08-10 19:30:14 +000016684 // Bitcast the original vector into a vector of store-size units
16685 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016686 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016687 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16688 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16689 SmallVector<SDValue, 8> Chains;
16690 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16691 TLI.getPointerTy());
16692 SDValue Ptr = St->getBasePtr();
16693
16694 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016695 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016696 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16697 StoreType, ShuffWide,
16698 DAG.getIntPtrConstant(i));
16699 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16700 St->getPointerInfo(), St->isVolatile(),
16701 St->isNonTemporal(), St->getAlignment());
16702 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16703 Chains.push_back(Ch);
16704 }
16705
16706 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16707 Chains.size());
16708 }
16709
Chris Lattner149a4e52008-02-22 02:09:43 +000016710 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16711 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016712 // A preferable solution to the general problem is to figure out the right
16713 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016714
16715 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016716 if (VT.getSizeInBits() != 64)
16717 return SDValue();
16718
Devang Patel578efa92009-06-05 21:57:13 +000016719 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016720 bool NoImplicitFloatOps = F->getAttributes().
16721 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016722 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016723 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016724 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016725 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016726 isa<LoadSDNode>(St->getValue()) &&
16727 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16728 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016729 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016730 LoadSDNode *Ld = 0;
16731 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016732 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016733 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016734 // Must be a store of a load. We currently handle two cases: the load
16735 // is a direct child, and it's under an intervening TokenFactor. It is
16736 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016737 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016738 Ld = cast<LoadSDNode>(St->getChain());
16739 else if (St->getValue().hasOneUse() &&
16740 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016741 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016742 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016743 TokenFactorIndex = i;
16744 Ld = cast<LoadSDNode>(St->getValue());
16745 } else
16746 Ops.push_back(ChainVal->getOperand(i));
16747 }
16748 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016749
Evan Cheng536e6672009-03-12 05:59:15 +000016750 if (!Ld || !ISD::isNormalLoad(Ld))
16751 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016752
Evan Cheng536e6672009-03-12 05:59:15 +000016753 // If this is not the MMX case, i.e. we are just turning i64 load/store
16754 // into f64 load/store, avoid the transformation if there are multiple
16755 // uses of the loaded value.
16756 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16757 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016758
Evan Cheng536e6672009-03-12 05:59:15 +000016759 DebugLoc LdDL = Ld->getDebugLoc();
16760 DebugLoc StDL = N->getDebugLoc();
16761 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16762 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16763 // pair instead.
16764 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016765 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016766 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16767 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016768 Ld->isNonTemporal(), Ld->isInvariant(),
16769 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016770 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016771 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016772 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016773 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016774 Ops.size());
16775 }
Evan Cheng536e6672009-03-12 05:59:15 +000016776 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016777 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016778 St->isVolatile(), St->isNonTemporal(),
16779 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016780 }
Evan Cheng536e6672009-03-12 05:59:15 +000016781
16782 // Otherwise, lower to two pairs of 32-bit loads / stores.
16783 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016784 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16785 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016786
Owen Anderson825b72b2009-08-11 20:47:22 +000016787 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016788 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016789 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016790 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016791 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016792 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016793 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016794 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016795 MinAlign(Ld->getAlignment(), 4));
16796
16797 SDValue NewChain = LoLd.getValue(1);
16798 if (TokenFactorIndex != -1) {
16799 Ops.push_back(LoLd);
16800 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016801 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016802 Ops.size());
16803 }
16804
16805 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016806 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16807 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016808
16809 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016810 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016811 St->isVolatile(), St->isNonTemporal(),
16812 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016813 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016814 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016815 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016816 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016817 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016818 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016819 }
Dan Gohman475871a2008-07-27 21:46:04 +000016820 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016821}
16822
Duncan Sands17470be2011-09-22 20:15:48 +000016823/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16824/// and return the operands for the horizontal operation in LHS and RHS. A
16825/// horizontal operation performs the binary operation on successive elements
16826/// of its first operand, then on successive elements of its second operand,
16827/// returning the resulting values in a vector. For example, if
16828/// A = < float a0, float a1, float a2, float a3 >
16829/// and
16830/// B = < float b0, float b1, float b2, float b3 >
16831/// then the result of doing a horizontal operation on A and B is
16832/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16833/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16834/// A horizontal-op B, for some already available A and B, and if so then LHS is
16835/// set to A, RHS to B, and the routine returns 'true'.
16836/// Note that the binary operation should have the property that if one of the
16837/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016838static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016839 // Look for the following pattern: if
16840 // A = < float a0, float a1, float a2, float a3 >
16841 // B = < float b0, float b1, float b2, float b3 >
16842 // and
16843 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16844 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16845 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16846 // which is A horizontal-op B.
16847
16848 // At least one of the operands should be a vector shuffle.
16849 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16850 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16851 return false;
16852
16853 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016854
16855 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16856 "Unsupported vector type for horizontal add/sub");
16857
16858 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16859 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016860 unsigned NumElts = VT.getVectorNumElements();
16861 unsigned NumLanes = VT.getSizeInBits()/128;
16862 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016863 assert((NumLaneElts % 2 == 0) &&
16864 "Vector type should have an even number of elements in each lane");
16865 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016866
16867 // View LHS in the form
16868 // LHS = VECTOR_SHUFFLE A, B, LMask
16869 // If LHS is not a shuffle then pretend it is the shuffle
16870 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16871 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16872 // type VT.
16873 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016874 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016875 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16876 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16877 A = LHS.getOperand(0);
16878 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16879 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016880 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16881 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016882 } else {
16883 if (LHS.getOpcode() != ISD::UNDEF)
16884 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016885 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016886 LMask[i] = i;
16887 }
16888
16889 // Likewise, view RHS in the form
16890 // RHS = VECTOR_SHUFFLE C, D, RMask
16891 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016892 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016893 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16894 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16895 C = RHS.getOperand(0);
16896 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16897 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016898 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16899 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016900 } else {
16901 if (RHS.getOpcode() != ISD::UNDEF)
16902 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016903 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016904 RMask[i] = i;
16905 }
16906
16907 // Check that the shuffles are both shuffling the same vectors.
16908 if (!(A == C && B == D) && !(A == D && B == C))
16909 return false;
16910
16911 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16912 if (!A.getNode() && !B.getNode())
16913 return false;
16914
16915 // If A and B occur in reverse order in RHS, then "swap" them (which means
16916 // rewriting the mask).
16917 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016918 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016919
16920 // At this point LHS and RHS are equivalent to
16921 // LHS = VECTOR_SHUFFLE A, B, LMask
16922 // RHS = VECTOR_SHUFFLE A, B, RMask
16923 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016924 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016925 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016926
Craig Topperf8363302011-12-02 08:18:41 +000016927 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016928 if (LIdx < 0 || RIdx < 0 ||
16929 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16930 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016931 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016932
Craig Topperf8363302011-12-02 08:18:41 +000016933 // Check that successive elements are being operated on. If not, this is
16934 // not a horizontal operation.
16935 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16936 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016937 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016938 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016939 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016940 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016941 }
16942
16943 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16944 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16945 return true;
16946}
16947
16948/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16949static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16950 const X86Subtarget *Subtarget) {
16951 EVT VT = N->getValueType(0);
16952 SDValue LHS = N->getOperand(0);
16953 SDValue RHS = N->getOperand(1);
16954
16955 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016956 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016957 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016958 isHorizontalBinOp(LHS, RHS, true))
16959 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16960 return SDValue();
16961}
16962
16963/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16964static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16965 const X86Subtarget *Subtarget) {
16966 EVT VT = N->getValueType(0);
16967 SDValue LHS = N->getOperand(0);
16968 SDValue RHS = N->getOperand(1);
16969
16970 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016971 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016972 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016973 isHorizontalBinOp(LHS, RHS, false))
16974 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16975 return SDValue();
16976}
16977
Chris Lattner6cf73262008-01-25 06:14:17 +000016978/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16979/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016980static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016981 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16982 // F[X]OR(0.0, x) -> x
16983 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016984 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16985 if (C->getValueAPF().isPosZero())
16986 return N->getOperand(1);
16987 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16988 if (C->getValueAPF().isPosZero())
16989 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016990 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016991}
16992
Nadav Rotemd60cb112012-08-19 13:06:16 +000016993/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16994/// X86ISD::FMAX nodes.
16995static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16996 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16997
16998 // Only perform optimizations if UnsafeMath is used.
16999 if (!DAG.getTarget().Options.UnsafeFPMath)
17000 return SDValue();
17001
17002 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017003 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017004 unsigned NewOp = 0;
17005 switch (N->getOpcode()) {
17006 default: llvm_unreachable("unknown opcode");
17007 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17008 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17009 }
17010
17011 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17012 N->getOperand(0), N->getOperand(1));
17013}
17014
Chris Lattneraf723b92008-01-25 05:46:26 +000017015/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017016static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017017 // FAND(0.0, x) -> 0.0
17018 // FAND(x, 0.0) -> 0.0
17019 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17020 if (C->getValueAPF().isPosZero())
17021 return N->getOperand(0);
17022 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17023 if (C->getValueAPF().isPosZero())
17024 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017025 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017026}
17027
Dan Gohmane5af2d32009-01-29 01:59:02 +000017028static SDValue PerformBTCombine(SDNode *N,
17029 SelectionDAG &DAG,
17030 TargetLowering::DAGCombinerInfo &DCI) {
17031 // BT ignores high bits in the bit index operand.
17032 SDValue Op1 = N->getOperand(1);
17033 if (Op1.hasOneUse()) {
17034 unsigned BitWidth = Op1.getValueSizeInBits();
17035 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17036 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017037 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17038 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017039 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017040 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17041 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17042 DCI.CommitTargetLoweringOpt(TLO);
17043 }
17044 return SDValue();
17045}
Chris Lattner83e6c992006-10-04 06:57:07 +000017046
Eli Friedman7a5e5552009-06-07 06:52:44 +000017047static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17048 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017049 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017050 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017051 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017052 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017053 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017054 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017055 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017056 }
17057 return SDValue();
17058}
17059
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017060static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17061 TargetLowering::DAGCombinerInfo &DCI,
17062 const X86Subtarget *Subtarget) {
17063 if (!DCI.isBeforeLegalizeOps())
17064 return SDValue();
17065
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017066 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017067 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017068
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017069 EVT VT = N->getValueType(0);
17070 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017071 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17072 if (R.getNode())
17073 return R;
17074 }
17075
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017076 return SDValue();
17077}
17078
Michael Liaof6c24ee2012-08-10 14:39:24 +000017079static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017080 const X86Subtarget* Subtarget) {
17081 DebugLoc dl = N->getDebugLoc();
17082 EVT VT = N->getValueType(0);
17083
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017084 // Let legalize expand this if it isn't a legal type yet.
17085 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17086 return SDValue();
17087
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017088 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017089 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17090 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017091 return SDValue();
17092
17093 SDValue A = N->getOperand(0);
17094 SDValue B = N->getOperand(1);
17095 SDValue C = N->getOperand(2);
17096
17097 bool NegA = (A.getOpcode() == ISD::FNEG);
17098 bool NegB = (B.getOpcode() == ISD::FNEG);
17099 bool NegC = (C.getOpcode() == ISD::FNEG);
17100
Michael Liaof6c24ee2012-08-10 14:39:24 +000017101 // Negative multiplication when NegA xor NegB
17102 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017103 if (NegA)
17104 A = A.getOperand(0);
17105 if (NegB)
17106 B = B.getOperand(0);
17107 if (NegC)
17108 C = C.getOperand(0);
17109
17110 unsigned Opcode;
17111 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017112 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017113 else
Craig Topperbf404372012-08-31 15:40:30 +000017114 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17115
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017116 return DAG.getNode(Opcode, dl, VT, A, B, C);
17117}
17118
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017119static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017120 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017121 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017122 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17123 // (and (i32 x86isd::setcc_carry), 1)
17124 // This eliminates the zext. This transformation is necessary because
17125 // ISD::SETCC is always legalized to i8.
17126 DebugLoc dl = N->getDebugLoc();
17127 SDValue N0 = N->getOperand(0);
17128 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017129
Evan Cheng2e489c42009-12-16 00:53:11 +000017130 if (N0.getOpcode() == ISD::AND &&
17131 N0.hasOneUse() &&
17132 N0.getOperand(0).hasOneUse()) {
17133 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017134 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17135 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17136 if (!C || C->getZExtValue() != 1)
17137 return SDValue();
17138 return DAG.getNode(ISD::AND, dl, VT,
17139 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17140 N00.getOperand(0), N00.getOperand(1)),
17141 DAG.getConstant(1, VT));
17142 }
17143 }
17144
Craig Topper5a529e42013-01-18 06:44:29 +000017145 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017146 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17147 if (R.getNode())
17148 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017149 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017150
Evan Cheng2e489c42009-12-16 00:53:11 +000017151 return SDValue();
17152}
17153
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017154// Optimize x == -y --> x+y == 0
17155// x != -y --> x+y != 0
17156static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17157 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17158 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017159 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017160
17161 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17162 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17163 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17164 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17165 LHS.getValueType(), RHS, LHS.getOperand(1));
17166 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17167 addV, DAG.getConstant(0, addV.getValueType()), CC);
17168 }
17169 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17170 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17171 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17172 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17173 RHS.getValueType(), LHS, RHS.getOperand(1));
17174 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17175 addV, DAG.getConstant(0, addV.getValueType()), CC);
17176 }
17177 return SDValue();
17178}
17179
Eric Christophere187e252013-01-31 00:50:48 +000017180// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17181// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017182// an all-ones bit which is more useful than 0/1 in some cases.
17183static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17184 return DAG.getNode(ISD::AND, DL, MVT::i8,
17185 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17186 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17187 DAG.getConstant(1, MVT::i8));
17188}
17189
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017190// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017191static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17192 TargetLowering::DAGCombinerInfo &DCI,
17193 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017194 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017195 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17196 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017197
Shuxin Yanga5526a92012-10-31 23:11:48 +000017198 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017199 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017200 // materializing "setb reg".
17201 //
17202 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17203 // cannot take an immediate as its first operand.
17204 //
Eric Christophere187e252013-01-31 00:50:48 +000017205 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017206 EFLAGS.getValueType().isInteger() &&
17207 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17208 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17209 EFLAGS.getNode()->getVTList(),
17210 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17211 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17212 return MaterializeSETB(DL, NewEFLAGS, DAG);
17213 }
17214 }
17215
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017216 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17217 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17218 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017219 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017220 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017221
Michael Liao2a33cec2012-08-10 19:58:13 +000017222 SDValue Flags;
17223
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017224 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17225 if (Flags.getNode()) {
17226 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17227 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17228 }
17229
Michael Liao2a33cec2012-08-10 19:58:13 +000017230 return SDValue();
17231}
17232
17233// Optimize branch condition evaluation.
17234//
17235static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17236 TargetLowering::DAGCombinerInfo &DCI,
17237 const X86Subtarget *Subtarget) {
17238 DebugLoc DL = N->getDebugLoc();
17239 SDValue Chain = N->getOperand(0);
17240 SDValue Dest = N->getOperand(1);
17241 SDValue EFLAGS = N->getOperand(3);
17242 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17243
17244 SDValue Flags;
17245
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017246 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17247 if (Flags.getNode()) {
17248 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17249 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17250 Flags);
17251 }
17252
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017253 return SDValue();
17254}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017255
Benjamin Kramer1396c402011-06-18 11:09:41 +000017256static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17257 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017258 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017259 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017260
17261 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017262 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017263 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017264 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017265 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17266 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17267 }
17268
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017269 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17270 // a 32-bit target where SSE doesn't support i64->FP operations.
17271 if (Op0.getOpcode() == ISD::LOAD) {
17272 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17273 EVT VT = Ld->getValueType(0);
17274 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17275 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17276 !XTLI->getSubtarget()->is64Bit() &&
17277 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017278 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17279 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017280 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17281 return FILDChain;
17282 }
17283 }
17284 return SDValue();
17285}
17286
Chris Lattner23a01992010-12-20 01:37:09 +000017287// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17288static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17289 X86TargetLowering::DAGCombinerInfo &DCI) {
17290 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17291 // the result is either zero or one (depending on the input carry bit).
17292 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17293 if (X86::isZeroNode(N->getOperand(0)) &&
17294 X86::isZeroNode(N->getOperand(1)) &&
17295 // We don't have a good way to replace an EFLAGS use, so only do this when
17296 // dead right now.
17297 SDValue(N, 1).use_empty()) {
17298 DebugLoc DL = N->getDebugLoc();
17299 EVT VT = N->getValueType(0);
17300 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17301 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17302 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17303 DAG.getConstant(X86::COND_B,MVT::i8),
17304 N->getOperand(2)),
17305 DAG.getConstant(1, VT));
17306 return DCI.CombineTo(N, Res1, CarryOut);
17307 }
17308
17309 return SDValue();
17310}
17311
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017312// fold (add Y, (sete X, 0)) -> adc 0, Y
17313// (add Y, (setne X, 0)) -> sbb -1, Y
17314// (sub (sete X, 0), Y) -> sbb 0, Y
17315// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017316static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017317 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017318
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017319 // Look through ZExts.
17320 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17321 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17322 return SDValue();
17323
17324 SDValue SetCC = Ext.getOperand(0);
17325 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17326 return SDValue();
17327
17328 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17329 if (CC != X86::COND_E && CC != X86::COND_NE)
17330 return SDValue();
17331
17332 SDValue Cmp = SetCC.getOperand(1);
17333 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017334 !X86::isZeroNode(Cmp.getOperand(1)) ||
17335 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017336 return SDValue();
17337
17338 SDValue CmpOp0 = Cmp.getOperand(0);
17339 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17340 DAG.getConstant(1, CmpOp0.getValueType()));
17341
17342 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17343 if (CC == X86::COND_NE)
17344 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17345 DL, OtherVal.getValueType(), OtherVal,
17346 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17347 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17348 DL, OtherVal.getValueType(), OtherVal,
17349 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17350}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017351
Craig Topper54f952a2011-11-19 09:02:40 +000017352/// PerformADDCombine - Do target-specific dag combines on integer adds.
17353static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17354 const X86Subtarget *Subtarget) {
17355 EVT VT = N->getValueType(0);
17356 SDValue Op0 = N->getOperand(0);
17357 SDValue Op1 = N->getOperand(1);
17358
17359 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017360 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017361 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017362 isHorizontalBinOp(Op0, Op1, true))
17363 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17364
17365 return OptimizeConditionalInDecrement(N, DAG);
17366}
17367
17368static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17369 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017370 SDValue Op0 = N->getOperand(0);
17371 SDValue Op1 = N->getOperand(1);
17372
17373 // X86 can't encode an immediate LHS of a sub. See if we can push the
17374 // negation into a preceding instruction.
17375 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017376 // If the RHS of the sub is a XOR with one use and a constant, invert the
17377 // immediate. Then add one to the LHS of the sub so we can turn
17378 // X-Y -> X+~Y+1, saving one register.
17379 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17380 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017381 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017382 EVT VT = Op0.getValueType();
17383 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17384 Op1.getOperand(0),
17385 DAG.getConstant(~XorC, VT));
17386 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017387 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017388 }
17389 }
17390
Craig Topper54f952a2011-11-19 09:02:40 +000017391 // Try to synthesize horizontal adds from adds of shuffles.
17392 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017393 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017394 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017395 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017396 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17397
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017398 return OptimizeConditionalInDecrement(N, DAG);
17399}
17400
Michael Liaod9d09602012-10-23 17:34:00 +000017401/// performVZEXTCombine - Performs build vector combines
17402static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17403 TargetLowering::DAGCombinerInfo &DCI,
17404 const X86Subtarget *Subtarget) {
17405 // (vzext (bitcast (vzext (x)) -> (vzext x)
17406 SDValue In = N->getOperand(0);
17407 while (In.getOpcode() == ISD::BITCAST)
17408 In = In.getOperand(0);
17409
17410 if (In.getOpcode() != X86ISD::VZEXT)
17411 return SDValue();
17412
17413 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17414}
17415
Dan Gohman475871a2008-07-27 21:46:04 +000017416SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017417 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017418 SelectionDAG &DAG = DCI.DAG;
17419 switch (N->getOpcode()) {
17420 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017421 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017422 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017423 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017424 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017425 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017426 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17427 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017428 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017429 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017430 case ISD::SHL:
17431 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017432 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017433 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017434 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017435 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017436 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017437 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017438 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017439 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17440 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017441 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017442 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017443 case X86ISD::FMIN:
17444 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017445 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017446 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017447 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017448 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017449 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017450 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017451 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017452 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017453 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017454 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017455 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017456 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017457 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017458 case X86ISD::UNPCKH:
17459 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017460 case X86ISD::MOVHLPS:
17461 case X86ISD::MOVLHPS:
17462 case X86ISD::PSHUFD:
17463 case X86ISD::PSHUFHW:
17464 case X86ISD::PSHUFLW:
17465 case X86ISD::MOVSS:
17466 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017467 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017468 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017469 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017470 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017471 }
17472
Dan Gohman475871a2008-07-27 21:46:04 +000017473 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017474}
17475
Evan Chenge5b51ac2010-04-17 06:13:15 +000017476/// isTypeDesirableForOp - Return true if the target has native support for
17477/// the specified value type and it is 'desirable' to use the type for the
17478/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17479/// instruction encodings are longer and some i16 instructions are slow.
17480bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17481 if (!isTypeLegal(VT))
17482 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017483 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017484 return true;
17485
17486 switch (Opc) {
17487 default:
17488 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017489 case ISD::LOAD:
17490 case ISD::SIGN_EXTEND:
17491 case ISD::ZERO_EXTEND:
17492 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017493 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017494 case ISD::SRL:
17495 case ISD::SUB:
17496 case ISD::ADD:
17497 case ISD::MUL:
17498 case ISD::AND:
17499 case ISD::OR:
17500 case ISD::XOR:
17501 return false;
17502 }
17503}
17504
17505/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017506/// beneficial for dag combiner to promote the specified node. If true, it
17507/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017508bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017509 EVT VT = Op.getValueType();
17510 if (VT != MVT::i16)
17511 return false;
17512
Evan Cheng4c26e932010-04-19 19:29:22 +000017513 bool Promote = false;
17514 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017515 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017516 default: break;
17517 case ISD::LOAD: {
17518 LoadSDNode *LD = cast<LoadSDNode>(Op);
17519 // If the non-extending load has a single use and it's not live out, then it
17520 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017521 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17522 Op.hasOneUse()*/) {
17523 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17524 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17525 // The only case where we'd want to promote LOAD (rather then it being
17526 // promoted as an operand is when it's only use is liveout.
17527 if (UI->getOpcode() != ISD::CopyToReg)
17528 return false;
17529 }
17530 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017531 Promote = true;
17532 break;
17533 }
17534 case ISD::SIGN_EXTEND:
17535 case ISD::ZERO_EXTEND:
17536 case ISD::ANY_EXTEND:
17537 Promote = true;
17538 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017539 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017540 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017541 SDValue N0 = Op.getOperand(0);
17542 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017543 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017544 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017545 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017546 break;
17547 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017548 case ISD::ADD:
17549 case ISD::MUL:
17550 case ISD::AND:
17551 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017552 case ISD::XOR:
17553 Commute = true;
17554 // fallthrough
17555 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017556 SDValue N0 = Op.getOperand(0);
17557 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017558 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017559 return false;
17560 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017561 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017562 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017563 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017564 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017565 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017566 }
17567 }
17568
17569 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017570 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017571}
17572
Evan Cheng60c07e12006-07-05 22:17:51 +000017573//===----------------------------------------------------------------------===//
17574// X86 Inline Assembly Support
17575//===----------------------------------------------------------------------===//
17576
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017577namespace {
17578 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017579 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017580 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017581
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017582 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017583 StringRef piece(*args[i]);
17584 if (!s.startswith(piece)) // Check if the piece matches.
17585 return false;
17586
17587 s = s.substr(piece.size());
17588 StringRef::size_type pos = s.find_first_not_of(" \t");
17589 if (pos == 0) // We matched a prefix.
17590 return false;
17591
17592 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017593 }
17594
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017595 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017596 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017597 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017598}
17599
Chris Lattnerb8105652009-07-20 17:51:36 +000017600bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17601 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017602
17603 std::string AsmStr = IA->getAsmString();
17604
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017605 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17606 if (!Ty || Ty->getBitWidth() % 16 != 0)
17607 return false;
17608
Chris Lattnerb8105652009-07-20 17:51:36 +000017609 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017610 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017611 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017612
17613 switch (AsmPieces.size()) {
17614 default: return false;
17615 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017616 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017617 // we will turn this bswap into something that will be lowered to logical
17618 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17619 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017620 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017621 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17622 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17623 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17624 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17625 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17626 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017627 // No need to check constraints, nothing other than the equivalent of
17628 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017629 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017630 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017631
Chris Lattnerb8105652009-07-20 17:51:36 +000017632 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017633 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017634 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017635 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17636 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017637 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017638 const std::string &ConstraintsStr = IA->getConstraintString();
17639 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017640 std::sort(AsmPieces.begin(), AsmPieces.end());
17641 if (AsmPieces.size() == 4 &&
17642 AsmPieces[0] == "~{cc}" &&
17643 AsmPieces[1] == "~{dirflag}" &&
17644 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017645 AsmPieces[3] == "~{fpsr}")
17646 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017647 }
17648 break;
17649 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017650 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017651 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017652 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17653 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17654 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017655 AsmPieces.clear();
17656 const std::string &ConstraintsStr = IA->getConstraintString();
17657 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17658 std::sort(AsmPieces.begin(), AsmPieces.end());
17659 if (AsmPieces.size() == 4 &&
17660 AsmPieces[0] == "~{cc}" &&
17661 AsmPieces[1] == "~{dirflag}" &&
17662 AsmPieces[2] == "~{flags}" &&
17663 AsmPieces[3] == "~{fpsr}")
17664 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017665 }
Evan Cheng55d42002011-01-08 01:24:27 +000017666
17667 if (CI->getType()->isIntegerTy(64)) {
17668 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17669 if (Constraints.size() >= 2 &&
17670 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17671 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17672 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017673 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17674 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17675 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017676 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017677 }
17678 }
17679 break;
17680 }
17681 return false;
17682}
17683
Chris Lattnerf4dff842006-07-11 02:54:03 +000017684/// getConstraintType - Given a constraint letter, return the type of
17685/// constraint it is for this target.
17686X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017687X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17688 if (Constraint.size() == 1) {
17689 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017690 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017691 case 'q':
17692 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017693 case 'f':
17694 case 't':
17695 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017696 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017697 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017698 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017699 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017700 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017701 case 'a':
17702 case 'b':
17703 case 'c':
17704 case 'd':
17705 case 'S':
17706 case 'D':
17707 case 'A':
17708 return C_Register;
17709 case 'I':
17710 case 'J':
17711 case 'K':
17712 case 'L':
17713 case 'M':
17714 case 'N':
17715 case 'G':
17716 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017717 case 'e':
17718 case 'Z':
17719 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017720 default:
17721 break;
17722 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017723 }
Chris Lattner4234f572007-03-25 02:14:49 +000017724 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017725}
17726
John Thompson44ab89e2010-10-29 17:29:13 +000017727/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017728/// This object must already have been set up with the operand type
17729/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017730TargetLowering::ConstraintWeight
17731 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017732 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017733 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017734 Value *CallOperandVal = info.CallOperandVal;
17735 // If we don't have a value, we can't do a match,
17736 // but allow it at the lowest weight.
17737 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017738 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017739 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017740 // Look at the constraint type.
17741 switch (*constraint) {
17742 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017743 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17744 case 'R':
17745 case 'q':
17746 case 'Q':
17747 case 'a':
17748 case 'b':
17749 case 'c':
17750 case 'd':
17751 case 'S':
17752 case 'D':
17753 case 'A':
17754 if (CallOperandVal->getType()->isIntegerTy())
17755 weight = CW_SpecificReg;
17756 break;
17757 case 'f':
17758 case 't':
17759 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017760 if (type->isFloatingPointTy())
17761 weight = CW_SpecificReg;
17762 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017763 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017764 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17765 weight = CW_SpecificReg;
17766 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017767 case 'x':
17768 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017769 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017770 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017771 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017772 break;
17773 case 'I':
17774 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17775 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017776 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017777 }
17778 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017779 case 'J':
17780 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17781 if (C->getZExtValue() <= 63)
17782 weight = CW_Constant;
17783 }
17784 break;
17785 case 'K':
17786 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17787 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17788 weight = CW_Constant;
17789 }
17790 break;
17791 case 'L':
17792 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17793 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17794 weight = CW_Constant;
17795 }
17796 break;
17797 case 'M':
17798 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17799 if (C->getZExtValue() <= 3)
17800 weight = CW_Constant;
17801 }
17802 break;
17803 case 'N':
17804 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17805 if (C->getZExtValue() <= 0xff)
17806 weight = CW_Constant;
17807 }
17808 break;
17809 case 'G':
17810 case 'C':
17811 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17812 weight = CW_Constant;
17813 }
17814 break;
17815 case 'e':
17816 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17817 if ((C->getSExtValue() >= -0x80000000LL) &&
17818 (C->getSExtValue() <= 0x7fffffffLL))
17819 weight = CW_Constant;
17820 }
17821 break;
17822 case 'Z':
17823 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17824 if (C->getZExtValue() <= 0xffffffff)
17825 weight = CW_Constant;
17826 }
17827 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017828 }
17829 return weight;
17830}
17831
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017832/// LowerXConstraint - try to replace an X constraint, which matches anything,
17833/// with another that has more specific requirements based on the type of the
17834/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017835const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017836LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017837 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17838 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017839 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017840 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017841 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017842 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017843 return "x";
17844 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017845
Chris Lattner5e764232008-04-26 23:02:14 +000017846 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017847}
17848
Chris Lattner48884cd2007-08-25 00:47:38 +000017849/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17850/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017851void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017852 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017853 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017854 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017855 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017856
Eric Christopher100c8332011-06-02 23:16:42 +000017857 // Only support length 1 constraints for now.
17858 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017859
Eric Christopher100c8332011-06-02 23:16:42 +000017860 char ConstraintLetter = Constraint[0];
17861 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017862 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017863 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017864 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017865 if (C->getZExtValue() <= 31) {
17866 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017867 break;
17868 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017869 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017870 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017871 case 'J':
17872 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017873 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017874 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17875 break;
17876 }
17877 }
17878 return;
17879 case 'K':
17880 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017881 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017882 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17883 break;
17884 }
17885 }
17886 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017887 case 'N':
17888 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017889 if (C->getZExtValue() <= 255) {
17890 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017891 break;
17892 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017893 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017894 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017895 case 'e': {
17896 // 32-bit signed value
17897 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017898 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17899 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017900 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017901 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017902 break;
17903 }
17904 // FIXME gcc accepts some relocatable values here too, but only in certain
17905 // memory models; it's complicated.
17906 }
17907 return;
17908 }
17909 case 'Z': {
17910 // 32-bit unsigned value
17911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017912 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17913 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017914 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17915 break;
17916 }
17917 }
17918 // FIXME gcc accepts some relocatable values here too, but only in certain
17919 // memory models; it's complicated.
17920 return;
17921 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017922 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017923 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017924 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017925 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017926 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017927 break;
17928 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017929
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017930 // In any sort of PIC mode addresses need to be computed at runtime by
17931 // adding in a register or some sort of table lookup. These can't
17932 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017933 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017934 return;
17935
Chris Lattnerdc43a882007-05-03 16:52:29 +000017936 // If we are in non-pic codegen mode, we allow the address of a global (with
17937 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017938 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017939 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017940
Chris Lattner49921962009-05-08 18:23:14 +000017941 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17942 while (1) {
17943 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17944 Offset += GA->getOffset();
17945 break;
17946 } else if (Op.getOpcode() == ISD::ADD) {
17947 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17948 Offset += C->getZExtValue();
17949 Op = Op.getOperand(0);
17950 continue;
17951 }
17952 } else if (Op.getOpcode() == ISD::SUB) {
17953 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17954 Offset += -C->getZExtValue();
17955 Op = Op.getOperand(0);
17956 continue;
17957 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017958 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017959
Chris Lattner49921962009-05-08 18:23:14 +000017960 // Otherwise, this isn't something we can handle, reject it.
17961 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017962 }
Eric Christopherfd179292009-08-27 18:07:15 +000017963
Dan Gohman46510a72010-04-15 01:51:59 +000017964 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017965 // If we require an extra load to get this address, as in PIC mode, we
17966 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017967 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17968 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017969 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017970
Devang Patel0d881da2010-07-06 22:08:15 +000017971 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17972 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017973 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017974 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017975 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017976
Gabor Greifba36cb52008-08-28 21:40:38 +000017977 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017978 Ops.push_back(Result);
17979 return;
17980 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017981 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017982}
17983
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017984std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017985X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017986 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017987 // First, see if this is a constraint that directly corresponds to an LLVM
17988 // register class.
17989 if (Constraint.size() == 1) {
17990 // GCC Constraint Letters
17991 switch (Constraint[0]) {
17992 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017993 // TODO: Slight differences here in allocation order and leaving
17994 // RIP in the class. Do they matter any more here than they do
17995 // in the normal allocation?
17996 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17997 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017998 if (VT == MVT::i32 || VT == MVT::f32)
17999 return std::make_pair(0U, &X86::GR32RegClass);
18000 if (VT == MVT::i16)
18001 return std::make_pair(0U, &X86::GR16RegClass);
18002 if (VT == MVT::i8 || VT == MVT::i1)
18003 return std::make_pair(0U, &X86::GR8RegClass);
18004 if (VT == MVT::i64 || VT == MVT::f64)
18005 return std::make_pair(0U, &X86::GR64RegClass);
18006 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018007 }
18008 // 32-bit fallthrough
18009 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018010 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018011 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18012 if (VT == MVT::i16)
18013 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18014 if (VT == MVT::i8 || VT == MVT::i1)
18015 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18016 if (VT == MVT::i64)
18017 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018018 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018019 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018020 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018021 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018022 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018023 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018024 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018025 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018026 return std::make_pair(0U, &X86::GR32RegClass);
18027 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018028 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018029 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018030 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018031 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018032 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018033 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018034 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18035 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018036 case 'f': // FP Stack registers.
18037 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18038 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018039 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018040 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018041 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018042 return std::make_pair(0U, &X86::RFP64RegClass);
18043 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018044 case 'y': // MMX_REGS if MMX allowed.
18045 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018046 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018047 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018048 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018049 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018050 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018051 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018052
Owen Anderson825b72b2009-08-11 20:47:22 +000018053 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018054 default: break;
18055 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018056 case MVT::f32:
18057 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018058 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018059 case MVT::f64:
18060 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018061 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018062 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018063 case MVT::v16i8:
18064 case MVT::v8i16:
18065 case MVT::v4i32:
18066 case MVT::v2i64:
18067 case MVT::v4f32:
18068 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018069 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018070 // AVX types.
18071 case MVT::v32i8:
18072 case MVT::v16i16:
18073 case MVT::v8i32:
18074 case MVT::v4i64:
18075 case MVT::v8f32:
18076 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018077 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018078 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018079 break;
18080 }
18081 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018082
Chris Lattnerf76d1802006-07-31 23:26:50 +000018083 // Use the default implementation in TargetLowering to convert the register
18084 // constraint into a member of a register class.
18085 std::pair<unsigned, const TargetRegisterClass*> Res;
18086 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018087
18088 // Not found as a standard register?
18089 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018090 // Map st(0) -> st(7) -> ST0
18091 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18092 tolower(Constraint[1]) == 's' &&
18093 tolower(Constraint[2]) == 't' &&
18094 Constraint[3] == '(' &&
18095 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18096 Constraint[5] == ')' &&
18097 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018098
Chris Lattner56d77c72009-09-13 22:41:48 +000018099 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018100 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018101 return Res;
18102 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018103
Chris Lattner56d77c72009-09-13 22:41:48 +000018104 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018105 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018106 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018107 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018108 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018109 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018110
18111 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018112 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018113 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018114 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018115 return Res;
18116 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018117
Dale Johannesen330169f2008-11-13 21:52:36 +000018118 // 'A' means EAX + EDX.
18119 if (Constraint == "A") {
18120 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018121 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018122 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018123 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018124 return Res;
18125 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018126
Chris Lattnerf76d1802006-07-31 23:26:50 +000018127 // Otherwise, check to see if this is a register class of the wrong value
18128 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18129 // turn into {ax},{dx}.
18130 if (Res.second->hasType(VT))
18131 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018132
Chris Lattnerf76d1802006-07-31 23:26:50 +000018133 // All of the single-register GCC register classes map their values onto
18134 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18135 // really want an 8-bit or 32-bit register, map to the appropriate register
18136 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018137 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000018138 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018139 unsigned DestReg = 0;
18140 switch (Res.first) {
18141 default: break;
18142 case X86::AX: DestReg = X86::AL; break;
18143 case X86::DX: DestReg = X86::DL; break;
18144 case X86::CX: DestReg = X86::CL; break;
18145 case X86::BX: DestReg = X86::BL; break;
18146 }
18147 if (DestReg) {
18148 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018149 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018150 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018151 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018152 unsigned DestReg = 0;
18153 switch (Res.first) {
18154 default: break;
18155 case X86::AX: DestReg = X86::EAX; break;
18156 case X86::DX: DestReg = X86::EDX; break;
18157 case X86::CX: DestReg = X86::ECX; break;
18158 case X86::BX: DestReg = X86::EBX; break;
18159 case X86::SI: DestReg = X86::ESI; break;
18160 case X86::DI: DestReg = X86::EDI; break;
18161 case X86::BP: DestReg = X86::EBP; break;
18162 case X86::SP: DestReg = X86::ESP; break;
18163 }
18164 if (DestReg) {
18165 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018166 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018167 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018168 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018169 unsigned DestReg = 0;
18170 switch (Res.first) {
18171 default: break;
18172 case X86::AX: DestReg = X86::RAX; break;
18173 case X86::DX: DestReg = X86::RDX; break;
18174 case X86::CX: DestReg = X86::RCX; break;
18175 case X86::BX: DestReg = X86::RBX; break;
18176 case X86::SI: DestReg = X86::RSI; break;
18177 case X86::DI: DestReg = X86::RDI; break;
18178 case X86::BP: DestReg = X86::RBP; break;
18179 case X86::SP: DestReg = X86::RSP; break;
18180 }
18181 if (DestReg) {
18182 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018183 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018184 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018185 }
Craig Topperc9099502012-04-20 06:31:50 +000018186 } else if (Res.second == &X86::FR32RegClass ||
18187 Res.second == &X86::FR64RegClass ||
18188 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018189 // Handle references to XMM physical registers that got mapped into the
18190 // wrong class. This can happen with constraints like {xmm0} where the
18191 // target independent register mapper will just pick the first match it can
18192 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018193
18194 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018195 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018196 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018197 Res.second = &X86::FR64RegClass;
18198 else if (X86::VR128RegClass.hasType(VT))
18199 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018200 else if (X86::VR256RegClass.hasType(VT))
18201 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018202 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018203
Chris Lattnerf76d1802006-07-31 23:26:50 +000018204 return Res;
18205}