blob: 5dbe633d6e84b442f3787b74e9b9702846b36e65 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.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();
69 assert(VT.getSizeInBits() == 256 && "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 Topperb14940a2012-04-22 20:55:18 +000088 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
89 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();
108 assert(VT.getSizeInBits() == 128 && "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 Topperb14940a2012-04-22 20:55:18 +0000121 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
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();
Evan Cheng25ab6902006-09-08 06:48:29 +0000161 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000162
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000163 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000164 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000167 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000170 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000171 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Eric Christopherde5e1012011-03-11 01:05:58 +0000174 // For 64-bit since we have so many registers use the ILP scheduler, for
175 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000176 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000177 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000179 else if (Subtarget->is64Bit())
180 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000181 else
182 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000184
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000185 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000186 // Setup Windows compiler runtime calls.
187 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000188 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000189 setLibcallName(RTLIB::SREM_I64, "_allrem");
190 setLibcallName(RTLIB::UREM_I64, "_aullrem");
191 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000192 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000193 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000194 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
195 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
196 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000197
198 // The _ftol2 runtime function has an unusual calling conv, which
199 // is modeled by a special pseudo-instruction.
200 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
201 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
202 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
203 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000204 }
205
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000206 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000207 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000208 setUseUnderscoreSetJmp(false);
209 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000210 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 // MS runtime is weird: it exports _setjmp, but longjmp!
212 setUseUnderscoreSetJmp(true);
213 setUseUnderscoreLongJmp(false);
214 } else {
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(true);
217 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000218
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000220 addRegisterClass(MVT::i8, &X86::GR8RegClass);
221 addRegisterClass(MVT::i16, &X86::GR16RegClass);
222 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000227
Scott Michelfdc40a02009-02-17 22:15:04 +0000228 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000230 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000232 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
234 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000235
236 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
238 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
239 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
240 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000243
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
245 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
247 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
248 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000249
Evan Cheng25ab6902006-09-08 06:48:29 +0000250 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000253 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000254 // We have an algorithm for SSE2->double, and we turn this into a
255 // 64-bit FILD followed by conditional FADD for other targets.
256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000257 // We have an algorithm for SSE2, and we turn this into a 64-bit
258 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000259 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000260 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000261
262 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
263 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
265 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000266
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000267 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000268 // SSE has no i16 to fp conversion, only i32
269 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000273 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000280 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000281
Dale Johannesen73328d12007-09-19 23:55:34 +0000282 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
283 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000286
Evan Cheng02568ff2006-01-30 22:13:22 +0000287 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
288 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
290 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000291
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000292 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000294 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000296 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000299 }
300
301 // Handle FP_TO_UINT by promoting the destination to a larger signed
302 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
304 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
305 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306
Evan Cheng25ab6902006-09-08 06:48:29 +0000307 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000310 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000311 // Since AVX is a superset of SSE3, only check for SSE here.
312 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 // Expand FP_TO_UINT into a select.
314 // FIXME: We would like to use a Custom expander here eventually to do
315 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000318 // With SSE3 we can use fisttpll to convert to a signed i64; without
319 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000322
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000323 if (isTargetFTOL()) {
324 // Use the _ftol2 runtime function, which has a pseudo-instruction
325 // to handle its weird calling convention.
326 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
327 }
328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000350 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Chandler Carruth77821022011-12-24 12:12:34 +0000381 // Promote the i8 variants and force them on up to i32 which has a shorter
382 // encoding.
383 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
384 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
385 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
386 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000387 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000388 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
390 if (Subtarget->is64Bit())
391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000392 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000393 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
394 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
395 if (Subtarget->is64Bit())
396 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
397 }
Craig Topper37f21672011-10-11 06:44:02 +0000398
399 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000400 // When promoting the i8 variants, force them to i32 for a shorter
401 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000402 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000403 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
404 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
405 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000406 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
408 if (Subtarget->is64Bit())
409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000410 } else {
411 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
413 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000414 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
417 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000418 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
420 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 }
422
Benjamin Kramer1292c222010-12-04 20:32:23 +0000423 if (Subtarget->hasPOPCNT()) {
424 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
425 } else {
426 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
427 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
428 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
429 if (Subtarget->is64Bit())
430 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
431 }
432
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
434 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000435
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000436 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000437 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000438 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000439 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000440 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
442 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
443 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
445 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000446 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
448 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
449 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000451 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000453 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000456
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000457 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
459 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
460 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
461 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000462 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
464 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000465 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000466 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
468 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
469 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
470 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000471 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000472 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000473 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
475 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
476 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000482
Craig Topper1accb7e2012-01-10 06:54:16 +0000483 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000485
Eric Christopher9a9d2752010-07-22 02:48:34 +0000486 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000487 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000488
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000489 // On X86 and X86-64, atomic operations are lowered to locked instructions.
490 // Locked instructions, in turn, have implicit fence semantics (all memory
491 // operations are flushed before issuing the locked instruction, and they
492 // are not buffered), so we can fold away the common pattern of
493 // fence-atomic-fence.
494 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000495
Mon P Wang63307c32008-05-05 19:05:59 +0000496 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000497 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000498 MVT VT = IntVTs[i];
499 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
500 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000501 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000503
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000504 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000505 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000513 }
514
Eli Friedman43f51ae2011-08-26 21:21:21 +0000515 if (Subtarget->hasCmpxchg16b()) {
516 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
517 }
518
Evan Cheng3c992d22006-03-07 02:02:57 +0000519 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000520 if (!Subtarget->isTargetDarwin() &&
521 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000522 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000524 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
527 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
528 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
529 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000530 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000531 setExceptionPointerRegister(X86::RAX);
532 setExceptionSelectorRegister(X86::RDX);
533 } else {
534 setExceptionPointerRegister(X86::EAX);
535 setExceptionSelectorRegister(X86::EDX);
536 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
538 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000539
Duncan Sands4a544a72011-09-06 13:37:06 +0000540 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
541 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000542
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000544
Nate Begemanacc398c2006-01-25 18:21:52 +0000545 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::VASTART , MVT::Other, Custom);
547 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000548 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::VAARG , MVT::Other, Custom);
550 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000551 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::VAARG , MVT::Other, Expand);
553 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000554 }
Evan Chengae642192007-03-02 23:16:35 +0000555
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
557 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000558
559 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
560 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
561 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000562 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000563 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
564 MVT::i64 : MVT::i32, Custom);
565 else
566 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
567 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000568
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000569 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000570 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000571 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000572 addRegisterClass(MVT::f32, &X86::FR32RegClass);
573 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Evan Cheng223547a2006-01-31 22:28:30 +0000575 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FABS , MVT::f64, Custom);
577 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000578
579 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FNEG , MVT::f64, Custom);
581 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000582
Evan Cheng68c47cb2007-01-05 07:55:56 +0000583 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
585 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000586
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000587 // Lower this to FGETSIGNx86 plus an AND.
588 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
589 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
590
Evan Chengd25e9e82006-02-02 00:28:23 +0000591 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FSIN , MVT::f64, Expand);
593 setOperationAction(ISD::FCOS , MVT::f64, Expand);
594 setOperationAction(ISD::FSIN , MVT::f32, Expand);
595 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000596
Chris Lattnera54aa942006-01-29 06:26:08 +0000597 // Expand FP immediates into loads from the stack, except for the special
598 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599 addLegalFPImmediate(APFloat(+0.0)); // xorpd
600 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000601 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 // Use SSE for f32, x87 for f64.
603 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000604 addRegisterClass(MVT::f32, &X86::FR32RegClass);
605 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606
607 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000609
610 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614
615 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
617 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
619 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FSIN , MVT::f32, Expand);
621 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
Nate Begemane1795842008-02-14 08:57:00 +0000623 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000624 addLegalFPImmediate(APFloat(+0.0f)); // xorps
625 addLegalFPImmediate(APFloat(+0.0)); // FLD0
626 addLegalFPImmediate(APFloat(+1.0)); // FLD1
627 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
628 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
629
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000630 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
632 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000633 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000634 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000635 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000637 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
638 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000639
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
641 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
642 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
643 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000644
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000645 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
647 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000648 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000649 addLegalFPImmediate(APFloat(+0.0)); // FLD0
650 addLegalFPImmediate(APFloat(+1.0)); // FLD1
651 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
652 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000653 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
654 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
655 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
656 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000657 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000658
Cameron Zwarich33390842011-07-08 21:39:21 +0000659 // We don't support FMA.
660 setOperationAction(ISD::FMA, MVT::f64, Expand);
661 setOperationAction(ISD::FMA, MVT::f32, Expand);
662
Dale Johannesen59a58732007-08-05 18:49:15 +0000663 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000664 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000665 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
667 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000669 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000670 addLegalFPImmediate(TmpFlt); // FLD0
671 TmpFlt.changeSign();
672 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000673
674 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000675 APFloat TmpFlt2(+1.0);
676 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
677 &ignored);
678 addLegalFPImmediate(TmpFlt2); // FLD1
679 TmpFlt2.changeSign();
680 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
681 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000682
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000683 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
685 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000687
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000688 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
689 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
690 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
691 setOperationAction(ISD::FRINT, MVT::f80, Expand);
692 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000693 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000694 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000695
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000696 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
698 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
699 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000700
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FLOG, MVT::f80, Expand);
702 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
703 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
704 setOperationAction(ISD::FEXP, MVT::f80, Expand);
705 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000706
Mon P Wangf007a8b2008-11-06 05:31:54 +0000707 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000708 // (for widening) or expand (for scalarization). Then we will selectively
709 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000710 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
711 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000728 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
729 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000734 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000745 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000747 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
752 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000754 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000764 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000765 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000769 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000770 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
771 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000772 setTruncStoreAction((MVT::SimpleValueType)VT,
773 (MVT::SimpleValueType)InnerVT, Expand);
774 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
775 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
776 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000777 }
778
Evan Chengc7ce29b2009-02-13 22:36:38 +0000779 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
780 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000781 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000782 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000783 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000784 }
785
Dale Johannesen0488fb62010-09-30 23:57:10 +0000786 // MMX-sized vectors (other than x86mmx) are expected to be expanded
787 // into smaller operations.
788 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
789 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
790 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
791 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
792 setOperationAction(ISD::AND, MVT::v8i8, Expand);
793 setOperationAction(ISD::AND, MVT::v4i16, Expand);
794 setOperationAction(ISD::AND, MVT::v2i32, Expand);
795 setOperationAction(ISD::AND, MVT::v1i64, Expand);
796 setOperationAction(ISD::OR, MVT::v8i8, Expand);
797 setOperationAction(ISD::OR, MVT::v4i16, Expand);
798 setOperationAction(ISD::OR, MVT::v2i32, Expand);
799 setOperationAction(ISD::OR, MVT::v1i64, Expand);
800 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
801 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
802 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
803 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
804 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
805 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
806 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
807 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
808 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
809 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
810 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
811 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
812 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000813 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
814 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
815 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
816 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000817
Craig Topper1accb7e2012-01-10 06:54:16 +0000818 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000819 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
822 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
823 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
824 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
825 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
826 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
827 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
828 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
829 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
830 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
831 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000832 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000833 }
834
Craig Topper1accb7e2012-01-10 06:54:16 +0000835 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000836 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000837
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000838 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
839 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000840 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
841 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
842 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
843 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
846 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
847 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
848 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
849 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
850 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
851 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
852 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
853 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
854 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
855 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
856 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
857 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
858 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
859 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
860 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000861
Nadav Rotem354efd82011-09-18 14:57:03 +0000862 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000863 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
864 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
865 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
868 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
869 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000872
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000873 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
874 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
875 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
876 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
877 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
878
Evan Cheng2c3ae372006-04-12 21:21:57 +0000879 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000880 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000882 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000884 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000885 // Do not attempt to custom lower non-128-bit vectors
886 if (!VT.is128BitVector())
887 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::BUILD_VECTOR,
889 VT.getSimpleVT().SimpleTy, Custom);
890 setOperationAction(ISD::VECTOR_SHUFFLE,
891 VT.getSimpleVT().SimpleTy, Custom);
892 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
893 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000894 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000895
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
897 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
898 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
899 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
900 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
901 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000902
Nate Begemancdd1eec2008-02-12 22:51:28 +0000903 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
905 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000906 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000907
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000908 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000909 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000911 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000912
913 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000914 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000915 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000916
Owen Andersond6662ad2009-08-10 20:46:15 +0000917 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000919 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000921 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000923 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000925 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000927 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000928
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000930
Evan Cheng2c3ae372006-04-12 21:21:57 +0000931 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000932 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
933 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
934 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
935 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000936
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
938 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000939 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000940
Craig Topperd0a31172012-01-10 06:37:29 +0000941 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000942 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
943 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
944 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
945 setOperationAction(ISD::FRINT, MVT::f32, Legal);
946 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
947 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
948 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
949 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
950 setOperationAction(ISD::FRINT, MVT::f64, Legal);
951 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
952
Nate Begeman14d12ca2008-02-11 04:19:36 +0000953 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000955
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000956 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
957 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
958 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
959 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
960 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000961
Nate Begeman14d12ca2008-02-11 04:19:36 +0000962 // i8 and i16 vectors are custom , because the source register and source
963 // source memory operand types are not the same width. f32 vectors are
964 // custom since the immediate controlling the insert encodes additional
965 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
967 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
968 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
969 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000970
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
972 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
973 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
974 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000975
Pete Coopera77214a2011-11-14 19:38:42 +0000976 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000977 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000978 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000979 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
980 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000981 }
982 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000983
Craig Topper1accb7e2012-01-10 06:54:16 +0000984 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000985 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000986 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000987
Nadav Rotem43012222011-05-11 08:12:09 +0000988 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000989 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000990
Nadav Rotem43012222011-05-11 08:12:09 +0000991 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000992 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000993
994 if (Subtarget->hasAVX2()) {
995 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
996 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
997
998 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
999 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1000
1001 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1002 } else {
1003 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1004 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1005
1006 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1007 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1008
1009 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1010 }
Nadav Rotem43012222011-05-11 08:12:09 +00001011 }
1012
Craig Topperd0a31172012-01-10 06:37:29 +00001013 if (Subtarget->hasSSE42())
Duncan Sands28b77e92011-09-06 19:07:46 +00001014 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +00001015
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001016 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001017 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1018 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1019 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1020 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1021 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1022 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001023
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1026 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001027
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1029 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1030 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1031 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1032 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1033 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001034
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1036 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1037 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1038 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1039 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1040 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001041
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001042 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1043 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001044 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001045
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001046 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1047 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1048
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001049 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1050 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1051
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001052 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001053 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001054
Duncan Sands28b77e92011-09-06 19:07:46 +00001055 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1056 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1057 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1058 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001059
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001060 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1061 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1062 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1063
Craig Topperaaa643c2011-11-09 07:28:55 +00001064 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1065 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1066 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1067 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001068
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001069 if (Subtarget->hasFMA()) {
1070 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1071 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1072 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1073 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1074 setOperationAction(ISD::FMA, MVT::f32, Custom);
1075 setOperationAction(ISD::FMA, MVT::f64, Custom);
1076 }
Craig Topper880ef452012-08-11 22:34:26 +00001077
Craig Topperaaa643c2011-11-09 07:28:55 +00001078 if (Subtarget->hasAVX2()) {
1079 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1080 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1081 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1082 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001083
Craig Topperaaa643c2011-11-09 07:28:55 +00001084 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1085 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1086 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1087 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001088
Craig Topperaaa643c2011-11-09 07:28:55 +00001089 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1090 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1091 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001092 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001093
1094 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001095
1096 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1097 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1098
1099 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1100 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1101
1102 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001103 } else {
1104 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1105 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1106 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1107 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1108
1109 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1110 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1111 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1112 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1113
1114 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1115 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1116 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1117 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001118
1119 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1120 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1121
1122 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1123 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1124
1125 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001126 }
Craig Topper13894fa2011-08-24 06:14:18 +00001127
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001128 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001129 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1130 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001131 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1132 EVT VT = SVT;
1133
1134 // Extract subvector is special because the value type
1135 // (result) is 128-bit but the source is 256-bit wide.
1136 if (VT.is128BitVector())
1137 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1138
1139 // Do not attempt to custom lower other non-256-bit vectors
1140 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001141 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001142
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001143 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1144 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1145 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1146 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001147 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001148 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
Craig Topper880ef452012-08-11 22:34:26 +00001149 setOperationAction(ISD::CONCAT_VECTORS, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001150 }
1151
David Greene54d8eba2011-01-27 22:38:56 +00001152 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001153 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001154 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1155 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001156
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001157 // Do not attempt to promote non-256-bit vectors
1158 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001159 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001160
1161 setOperationAction(ISD::AND, SVT, Promote);
1162 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1163 setOperationAction(ISD::OR, SVT, Promote);
1164 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1165 setOperationAction(ISD::XOR, SVT, Promote);
1166 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1167 setOperationAction(ISD::LOAD, SVT, Promote);
1168 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1169 setOperationAction(ISD::SELECT, SVT, Promote);
1170 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001171 }
David Greene9b9838d2009-06-29 16:47:10 +00001172 }
1173
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001174 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1175 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001176 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1177 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001178 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1179 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001180 }
1181
Evan Cheng6be2c582006-04-05 23:38:46 +00001182 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001183 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001184 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001185
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001186
Eli Friedman962f5492010-06-02 19:35:46 +00001187 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1188 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001189 //
Eli Friedman962f5492010-06-02 19:35:46 +00001190 // FIXME: We really should do custom legalization for addition and
1191 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1192 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001193 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1194 // Add/Sub/Mul with overflow operations are custom lowered.
1195 MVT VT = IntVTs[i];
1196 setOperationAction(ISD::SADDO, VT, Custom);
1197 setOperationAction(ISD::UADDO, VT, Custom);
1198 setOperationAction(ISD::SSUBO, VT, Custom);
1199 setOperationAction(ISD::USUBO, VT, Custom);
1200 setOperationAction(ISD::SMULO, VT, Custom);
1201 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001202 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001203
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001204 // There are no 8-bit 3-address imul/mul instructions
1205 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1206 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001207
Evan Chengd54f2d52009-03-31 19:38:51 +00001208 if (!Subtarget->is64Bit()) {
1209 // These libcalls are not available in 32-bit.
1210 setLibcallName(RTLIB::SHL_I128, 0);
1211 setLibcallName(RTLIB::SRL_I128, 0);
1212 setLibcallName(RTLIB::SRA_I128, 0);
1213 }
1214
Evan Cheng206ee9d2006-07-07 08:33:52 +00001215 // We have target-specific dag combine patterns for the following nodes:
1216 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001217 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001218 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001219 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001220 setTargetDAGCombine(ISD::SHL);
1221 setTargetDAGCombine(ISD::SRA);
1222 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001223 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001224 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001225 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001226 setTargetDAGCombine(ISD::FADD);
1227 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001228 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001229 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001230 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001231 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001232 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001233 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001234 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001235 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001236 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001237 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001238 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001239 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001240 if (Subtarget->is64Bit())
1241 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001242 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001243
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001244 computeRegisterProperties();
1245
Evan Cheng05219282011-01-06 06:52:41 +00001246 // On Darwin, -Os means optimize for size without hurting performance,
1247 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001248 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001249 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001250 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001251 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1252 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1253 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001254 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001255 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001256
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001257 // Predictable cmov don't hurt on atom because it's in-order.
1258 predictableSelectIsExpensive = !Subtarget->isAtom();
1259
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001260 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001261}
1262
Scott Michel5b8f82e2008-03-10 15:42:14 +00001263
Duncan Sands28b77e92011-09-06 19:07:46 +00001264EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1265 if (!VT.isVector()) return MVT::i8;
1266 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001267}
1268
1269
Evan Cheng29286502008-01-23 23:17:41 +00001270/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1271/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001272static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001273 if (MaxAlign == 16)
1274 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001275 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001276 if (VTy->getBitWidth() == 128)
1277 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001278 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001279 unsigned EltAlign = 0;
1280 getMaxByValAlign(ATy->getElementType(), EltAlign);
1281 if (EltAlign > MaxAlign)
1282 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001283 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001284 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1285 unsigned EltAlign = 0;
1286 getMaxByValAlign(STy->getElementType(i), EltAlign);
1287 if (EltAlign > MaxAlign)
1288 MaxAlign = EltAlign;
1289 if (MaxAlign == 16)
1290 break;
1291 }
1292 }
Evan Cheng29286502008-01-23 23:17:41 +00001293}
1294
1295/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1296/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001297/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1298/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001299unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001300 if (Subtarget->is64Bit()) {
1301 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001302 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001303 if (TyAlign > 8)
1304 return TyAlign;
1305 return 8;
1306 }
1307
Evan Cheng29286502008-01-23 23:17:41 +00001308 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001309 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001310 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001311 return Align;
1312}
Chris Lattner2b02a442007-02-25 08:29:00 +00001313
Evan Chengf0df0312008-05-15 08:39:06 +00001314/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001315/// and store operations as a result of memset, memcpy, and memmove
1316/// lowering. If DstAlign is zero that means it's safe to destination
1317/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1318/// means there isn't a need to check it against alignment requirement,
1319/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001320/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001321/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1322/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1323/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001324/// It returns EVT::Other if the type should be determined using generic
1325/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001326EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001327X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1328 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001329 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001330 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001331 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001332 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1333 // linux. This is because the stack realignment code can't handle certain
1334 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001335 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001336 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001337 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001338 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001339 (Subtarget->isUnalignedMemAccessFast() ||
1340 ((DstAlign == 0 || DstAlign >= 16) &&
1341 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001342 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001343 if (Subtarget->getStackAlignment() >= 32) {
1344 if (Subtarget->hasAVX2())
1345 return MVT::v8i32;
1346 if (Subtarget->hasAVX())
1347 return MVT::v8f32;
1348 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001349 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001350 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001351 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001352 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001353 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001354 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001355 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001356 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001357 // Do not use f64 to lower memcpy if source is string constant. It's
1358 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001359 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001360 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001361 }
Evan Chengf0df0312008-05-15 08:39:06 +00001362 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001363 return MVT::i64;
1364 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001365}
1366
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001367/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1368/// current function. The returned value is a member of the
1369/// MachineJumpTableInfo::JTEntryKind enum.
1370unsigned X86TargetLowering::getJumpTableEncoding() const {
1371 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1372 // symbol.
1373 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1374 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001375 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001376
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001377 // Otherwise, use the normal jump table encoding heuristics.
1378 return TargetLowering::getJumpTableEncoding();
1379}
1380
Chris Lattnerc64daab2010-01-26 05:02:42 +00001381const MCExpr *
1382X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1383 const MachineBasicBlock *MBB,
1384 unsigned uid,MCContext &Ctx) const{
1385 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1386 Subtarget->isPICStyleGOT());
1387 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1388 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001389 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1390 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001391}
1392
Evan Chengcc415862007-11-09 01:32:10 +00001393/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1394/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001395SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001396 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001397 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001398 // This doesn't have DebugLoc associated with it, but is not really the
1399 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001400 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001401 return Table;
1402}
1403
Chris Lattner589c6f62010-01-26 06:28:43 +00001404/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1405/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1406/// MCExpr.
1407const MCExpr *X86TargetLowering::
1408getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1409 MCContext &Ctx) const {
1410 // X86-64 uses RIP relative addressing based on the jump table label.
1411 if (Subtarget->isPICStyleRIPRel())
1412 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1413
1414 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001415 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001416}
1417
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001418// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001419std::pair<const TargetRegisterClass*, uint8_t>
1420X86TargetLowering::findRepresentativeClass(EVT VT) const{
1421 const TargetRegisterClass *RRC = 0;
1422 uint8_t Cost = 1;
1423 switch (VT.getSimpleVT().SimpleTy) {
1424 default:
1425 return TargetLowering::findRepresentativeClass(VT);
1426 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001427 RRC = Subtarget->is64Bit() ?
1428 (const TargetRegisterClass*)&X86::GR64RegClass :
1429 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001430 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001431 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001432 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001433 break;
1434 case MVT::f32: case MVT::f64:
1435 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1436 case MVT::v4f32: case MVT::v2f64:
1437 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1438 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001439 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001440 break;
1441 }
1442 return std::make_pair(RRC, Cost);
1443}
1444
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001445bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1446 unsigned &Offset) const {
1447 if (!Subtarget->isTargetLinux())
1448 return false;
1449
1450 if (Subtarget->is64Bit()) {
1451 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1452 Offset = 0x28;
1453 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1454 AddressSpace = 256;
1455 else
1456 AddressSpace = 257;
1457 } else {
1458 // %gs:0x14 on i386
1459 Offset = 0x14;
1460 AddressSpace = 256;
1461 }
1462 return true;
1463}
1464
1465
Chris Lattner2b02a442007-02-25 08:29:00 +00001466//===----------------------------------------------------------------------===//
1467// Return Value Calling Convention Implementation
1468//===----------------------------------------------------------------------===//
1469
Chris Lattner59ed56b2007-02-28 04:55:35 +00001470#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001471
Michael J. Spencerec38de22010-10-10 22:04:20 +00001472bool
Eric Christopher471e4222011-06-08 23:55:35 +00001473X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001474 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001475 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001476 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001477 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001478 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001479 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001480 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001481}
1482
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483SDValue
1484X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001485 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001486 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001487 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001488 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001489 MachineFunction &MF = DAG.getMachineFunction();
1490 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001491
Chris Lattner9774c912007-02-27 05:28:59 +00001492 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001493 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001494 RVLocs, *DAG.getContext());
1495 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001496
Evan Chengdcea1632010-02-04 02:40:39 +00001497 // Add the regs to the liveout set for the function.
1498 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1499 for (unsigned i = 0; i != RVLocs.size(); ++i)
1500 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1501 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001502
Dan Gohman475871a2008-07-27 21:46:04 +00001503 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001504
Dan Gohman475871a2008-07-27 21:46:04 +00001505 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001506 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1507 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001508 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1509 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001510
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001511 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001512 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1513 CCValAssign &VA = RVLocs[i];
1514 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001515 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001516 EVT ValVT = ValToCopy.getValueType();
1517
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001518 // Promote values to the appropriate types
1519 if (VA.getLocInfo() == CCValAssign::SExt)
1520 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1521 else if (VA.getLocInfo() == CCValAssign::ZExt)
1522 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1523 else if (VA.getLocInfo() == CCValAssign::AExt)
1524 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1525 else if (VA.getLocInfo() == CCValAssign::BCvt)
1526 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1527
Dale Johannesenc4510512010-09-24 19:05:48 +00001528 // If this is x86-64, and we disabled SSE, we can't return FP values,
1529 // or SSE or MMX vectors.
1530 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1531 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001532 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001533 report_fatal_error("SSE register return with SSE disabled");
1534 }
1535 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1536 // llvm-gcc has never done it right and no one has noticed, so this
1537 // should be OK for now.
1538 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001539 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001540 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001541
Chris Lattner447ff682008-03-11 03:23:40 +00001542 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1543 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001544 if (VA.getLocReg() == X86::ST0 ||
1545 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001546 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1547 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001548 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001549 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001550 RetOps.push_back(ValToCopy);
1551 // Don't emit a copytoreg.
1552 continue;
1553 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001554
Evan Cheng242b38b2009-02-23 09:03:22 +00001555 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1556 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001557 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001558 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001559 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001560 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001561 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1562 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001563 // If we don't have SSE2 available, convert to v4f32 so the generated
1564 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001565 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001567 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001568 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001569 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001570
Dale Johannesendd64c412009-02-04 00:33:20 +00001571 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001572 Flag = Chain.getValue(1);
1573 }
Dan Gohman61a92132008-04-21 23:59:07 +00001574
1575 // The x86-64 ABI for returning structs by value requires that we copy
1576 // the sret argument into %rax for the return. We saved the argument into
1577 // a virtual register in the entry block, so now we copy the value out
1578 // and into %rax.
1579 if (Subtarget->is64Bit() &&
1580 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1581 MachineFunction &MF = DAG.getMachineFunction();
1582 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1583 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001584 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001585 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001586 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001587
Dale Johannesendd64c412009-02-04 00:33:20 +00001588 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001589 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001590
1591 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001592 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001593 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001594
Chris Lattner447ff682008-03-11 03:23:40 +00001595 RetOps[0] = Chain; // Update chain.
1596
1597 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001598 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001599 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
1601 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001603}
1604
Evan Chengbf010eb2012-04-10 01:51:00 +00001605bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001606 if (N->getNumValues() != 1)
1607 return false;
1608 if (!N->hasNUsesOfValue(1, 0))
1609 return false;
1610
Evan Chengbf010eb2012-04-10 01:51:00 +00001611 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001612 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001613 if (Copy->getOpcode() == ISD::CopyToReg) {
1614 // If the copy has a glue operand, we conservatively assume it isn't safe to
1615 // perform a tail call.
1616 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1617 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001618 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001619 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001620 return false;
1621
Evan Cheng1bf891a2010-12-01 22:59:46 +00001622 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001623 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001624 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001625 if (UI->getOpcode() != X86ISD::RET_FLAG)
1626 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001627 HasRet = true;
1628 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001629
Evan Chengbf010eb2012-04-10 01:51:00 +00001630 if (!HasRet)
1631 return false;
1632
1633 Chain = TCChain;
1634 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001635}
1636
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001637EVT
1638X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001639 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001640 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001641 // TODO: Is this also valid on 32-bit?
1642 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001643 ReturnMVT = MVT::i8;
1644 else
1645 ReturnMVT = MVT::i32;
1646
1647 EVT MinVT = getRegisterType(Context, ReturnMVT);
1648 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001649}
1650
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651/// LowerCallResult - Lower the result values of a call into the
1652/// appropriate copies out of appropriate physical registers.
1653///
1654SDValue
1655X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001656 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657 const SmallVectorImpl<ISD::InputArg> &Ins,
1658 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001659 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001660
Chris Lattnere32bbf62007-02-28 07:09:55 +00001661 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001662 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001663 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001664 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001665 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001666 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
Chris Lattner3085e152007-02-25 08:59:22 +00001668 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001669 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001670 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001671 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001672
Torok Edwin3f142c32009-02-01 18:15:56 +00001673 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001675 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001676 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001677 }
1678
Evan Cheng79fb3b42009-02-20 20:43:02 +00001679 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001680
1681 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001682 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001683 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001684 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001685 // instead.
1686 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1687 // If we prefer to use the value in xmm registers, copy it out as f80 and
1688 // use a truncate to move it from fp stack reg to xmm reg.
1689 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001690 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001691 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1692 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001693 Val = Chain.getValue(0);
1694
1695 // Round the f80 to the right size, which also moves it to the appropriate
1696 // xmm register.
1697 if (CopyVT != VA.getValVT())
1698 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1699 // This truncation won't change the value.
1700 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001701 } else {
1702 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1703 CopyVT, InFlag).getValue(1);
1704 Val = Chain.getValue(0);
1705 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001706 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001707 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001708 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001709
Dan Gohman98ca4f22009-08-05 01:29:28 +00001710 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001711}
1712
1713
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001714//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001715// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001716//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001717// StdCall calling convention seems to be standard for many Windows' API
1718// routines and around. It differs from C calling convention just a little:
1719// callee should clean up the stack, not caller. Symbols should be also
1720// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001721// For info on fast calling convention see Fast Calling Convention (tail call)
1722// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001723
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001725/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001726enum StructReturnType {
1727 NotStructReturn,
1728 RegStructReturn,
1729 StackStructReturn
1730};
1731static StructReturnType
1732callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001733 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001734 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001735
Rafael Espindola1cee7102012-07-25 13:41:10 +00001736 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1737 if (!Flags.isSRet())
1738 return NotStructReturn;
1739 if (Flags.isInReg())
1740 return RegStructReturn;
1741 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001742}
1743
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001744/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001745/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001746static StructReturnType
1747argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001749 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001750
Rafael Espindola1cee7102012-07-25 13:41:10 +00001751 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1752 if (!Flags.isSRet())
1753 return NotStructReturn;
1754 if (Flags.isInReg())
1755 return RegStructReturn;
1756 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001757}
1758
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001759/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1760/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001761/// the specific parameter attribute. The copy will be passed as a byval
1762/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001763static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001764CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001765 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1766 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001767 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001768
Dale Johannesendd64c412009-02-04 00:33:20 +00001769 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001770 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001771 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001772}
1773
Chris Lattner29689432010-03-11 00:22:57 +00001774/// IsTailCallConvention - Return true if the calling convention is one that
1775/// supports tail call optimization.
1776static bool IsTailCallConvention(CallingConv::ID CC) {
1777 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1778}
1779
Evan Cheng485fafc2011-03-21 01:19:09 +00001780bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001781 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001782 return false;
1783
1784 CallSite CS(CI);
1785 CallingConv::ID CalleeCC = CS.getCallingConv();
1786 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1787 return false;
1788
1789 return true;
1790}
1791
Evan Cheng0c439eb2010-01-27 00:07:07 +00001792/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1793/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001794static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1795 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001796 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001797}
1798
Dan Gohman98ca4f22009-08-05 01:29:28 +00001799SDValue
1800X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001801 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 const SmallVectorImpl<ISD::InputArg> &Ins,
1803 DebugLoc dl, SelectionDAG &DAG,
1804 const CCValAssign &VA,
1805 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001806 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001807 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001808 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001809 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1810 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001811 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001812 EVT ValVT;
1813
1814 // If value is passed by pointer we have address passed instead of the value
1815 // itself.
1816 if (VA.getLocInfo() == CCValAssign::Indirect)
1817 ValVT = VA.getLocVT();
1818 else
1819 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001820
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001821 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001822 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001823 // In case of tail call optimization mark all arguments mutable. Since they
1824 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001825 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001826 unsigned Bytes = Flags.getByValSize();
1827 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1828 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001829 return DAG.getFrameIndex(FI, getPointerTy());
1830 } else {
1831 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001832 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001833 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1834 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001835 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001836 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001837 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001838}
1839
Dan Gohman475871a2008-07-27 21:46:04 +00001840SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001841X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001842 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843 bool isVarArg,
1844 const SmallVectorImpl<ISD::InputArg> &Ins,
1845 DebugLoc dl,
1846 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001847 SmallVectorImpl<SDValue> &InVals)
1848 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001849 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001851
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 const Function* Fn = MF.getFunction();
1853 if (Fn->hasExternalLinkage() &&
1854 Subtarget->isTargetCygMing() &&
1855 Fn->getName() == "main")
1856 FuncInfo->setForceFramePointer(true);
1857
Evan Cheng1bc78042006-04-26 01:20:17 +00001858 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001859 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001860 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001861 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001862
Chris Lattner29689432010-03-11 00:22:57 +00001863 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1864 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001865
Chris Lattner638402b2007-02-28 07:00:42 +00001866 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001867 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001868 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001869 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001870
1871 // Allocate shadow area for Win64
1872 if (IsWin64) {
1873 CCInfo.AllocateStack(32, 8);
1874 }
1875
Duncan Sands45907662010-10-31 13:21:44 +00001876 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001877
Chris Lattnerf39f7712007-02-28 05:46:49 +00001878 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001879 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001880 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1881 CCValAssign &VA = ArgLocs[i];
1882 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1883 // places.
1884 assert(VA.getValNo() != LastVal &&
1885 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001886 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001887 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001888
Chris Lattnerf39f7712007-02-28 05:46:49 +00001889 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001890 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001891 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001893 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001895 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001897 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001899 RC = &X86::FR64RegClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001900 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
Craig Topperc9099502012-04-20 06:31:50 +00001901 RC = &X86::VR256RegClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001902 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Craig Topperc9099502012-04-20 06:31:50 +00001903 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001904 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001905 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001906 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001907 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001908
Devang Patel68e6bee2011-02-21 23:21:26 +00001909 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Chris Lattnerf39f7712007-02-28 05:46:49 +00001912 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1913 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1914 // right size.
1915 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001916 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001917 DAG.getValueType(VA.getValVT()));
1918 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001919 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001920 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001921 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001922 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001924 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001925 // Handle MMX values passed in XMM regs.
1926 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001927 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1928 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 } else
1930 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001931 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001932 } else {
1933 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001935 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001936
1937 // If value is passed via pointer - do a load.
1938 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001939 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001940 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001941
Dan Gohman98ca4f22009-08-05 01:29:28 +00001942 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001943 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001944
Dan Gohman61a92132008-04-21 23:59:07 +00001945 // The x86-64 ABI for returning structs by value requires that we copy
1946 // the sret argument into %rax for the return. Save the argument into
1947 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001948 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001949 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1950 unsigned Reg = FuncInfo->getSRetReturnReg();
1951 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001953 FuncInfo->setSRetReturnReg(Reg);
1954 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001955 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001957 }
1958
Chris Lattnerf39f7712007-02-28 05:46:49 +00001959 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001960 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001961 if (FuncIsMadeTailCallSafe(CallConv,
1962 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001963 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001964
Evan Cheng1bc78042006-04-26 01:20:17 +00001965 // If the function takes variable number of arguments, make a frame index for
1966 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001968 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1969 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001970 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 }
1972 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001973 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1974
1975 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001976 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001977 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001978 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001979 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001980 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1981 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001982 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001983 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1984 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1985 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001986 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001987 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001988
1989 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001990 // The XMM registers which might contain var arg parameters are shadowed
1991 // in their paired GPR. So we only need to save the GPR to their home
1992 // slots.
1993 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001994 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001995 } else {
1996 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1997 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001998
Chad Rosier30450e82011-12-22 22:35:21 +00001999 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2000 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002001 }
2002 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2003 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002004
Devang Patel578efa92009-06-05 21:57:13 +00002005 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002006 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002007 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002008 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2009 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002010 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002011 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002012 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002013 // Kernel mode asks for SSE to be disabled, so don't push them
2014 // on the stack.
2015 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002016
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002017 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002018 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002019 // Get to the caller-allocated home save location. Add 8 to account
2020 // for the return address.
2021 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002022 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002023 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002024 // Fixup to set vararg frame on shadow area (4 x i64).
2025 if (NumIntRegs < 4)
2026 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002027 } else {
2028 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002029 // registers, then we must store them to their spots on the stack so
2030 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002031 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2032 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2033 FuncInfo->setRegSaveFrameIndex(
2034 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002035 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002036 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002037
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002039 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002040 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2041 getPointerTy());
2042 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002043 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002044 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2045 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002046 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002047 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002050 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002051 MachinePointerInfo::getFixedStack(
2052 FuncInfo->getRegSaveFrameIndex(), Offset),
2053 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002054 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002055 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002057
Dan Gohmanface41a2009-08-16 21:24:25 +00002058 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2059 // Now store the XMM (fp + vector) parameter registers.
2060 SmallVector<SDValue, 11> SaveXMMOps;
2061 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002062
Craig Topperc9099502012-04-20 06:31:50 +00002063 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002064 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2065 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002066
Dan Gohman1e93df62010-04-17 14:41:14 +00002067 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2068 FuncInfo->getRegSaveFrameIndex()));
2069 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2070 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002071
Dan Gohmanface41a2009-08-16 21:24:25 +00002072 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002073 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002074 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002075 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2076 SaveXMMOps.push_back(Val);
2077 }
2078 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2079 MVT::Other,
2080 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002081 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002082
2083 if (!MemOps.empty())
2084 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2085 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002086 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002090 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2091 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002092 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002093 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002094 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002095 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002096 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002097 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002098 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002099 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002100
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002102 // RegSaveFrameIndex is X86-64 only.
2103 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002104 if (CallConv == CallingConv::X86_FastCall ||
2105 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002106 // fastcc functions can't have varargs.
2107 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002108 }
Evan Cheng25caf632006-05-23 21:06:34 +00002109
Rafael Espindola76927d752011-08-30 19:39:58 +00002110 FuncInfo->setArgumentStackSize(StackSize);
2111
Dan Gohman98ca4f22009-08-05 01:29:28 +00002112 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002113}
2114
Dan Gohman475871a2008-07-27 21:46:04 +00002115SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2117 SDValue StackPtr, SDValue Arg,
2118 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002119 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002120 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002121 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002122 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002123 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002124 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002125 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002126
2127 return DAG.getStore(Chain, dl, Arg, PtrOff,
2128 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002129 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002130}
2131
Bill Wendling64e87322009-01-16 19:25:27 +00002132/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002133/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002134SDValue
2135X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002136 SDValue &OutRetAddr, SDValue Chain,
2137 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002138 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002139 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002140 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002141 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002142
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002144 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002145 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002146 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002147}
2148
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002149/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002150/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002151static SDValue
2152EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002153 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002154 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002155 // Store the return address to the appropriate stack slot.
2156 if (!FPDiff) return Chain;
2157 // Calculate the new stack slot for the return address.
2158 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002159 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002160 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002161 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002162 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002163 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002164 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002165 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002166 return Chain;
2167}
2168
Dan Gohman98ca4f22009-08-05 01:29:28 +00002169SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002170X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002171 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002172 SelectionDAG &DAG = CLI.DAG;
2173 DebugLoc &dl = CLI.DL;
2174 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2175 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2176 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2177 SDValue Chain = CLI.Chain;
2178 SDValue Callee = CLI.Callee;
2179 CallingConv::ID CallConv = CLI.CallConv;
2180 bool &isTailCall = CLI.IsTailCall;
2181 bool isVarArg = CLI.IsVarArg;
2182
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183 MachineFunction &MF = DAG.getMachineFunction();
2184 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002185 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002186 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002187 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002188 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189
Nick Lewycky22de16d2012-01-19 00:34:10 +00002190 if (MF.getTarget().Options.DisableTailCalls)
2191 isTailCall = false;
2192
Evan Cheng5f941932010-02-05 02:21:12 +00002193 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002194 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002195 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002196 isVarArg, SR != NotStructReturn,
2197 MF.getFunction()->hasStructRetAttr(),
2198 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002199
2200 // Sibcalls are automatically detected tailcalls which do not require
2201 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002202 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002203 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002204
2205 if (isTailCall)
2206 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002207 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002208
Chris Lattner29689432010-03-11 00:22:57 +00002209 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2210 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002211
Chris Lattner638402b2007-02-28 07:00:42 +00002212 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002213 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002214 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002216
2217 // Allocate shadow area for Win64
2218 if (IsWin64) {
2219 CCInfo.AllocateStack(32, 8);
2220 }
2221
Duncan Sands45907662010-10-31 13:21:44 +00002222 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002223
Chris Lattner423c5f42007-02-28 05:31:48 +00002224 // Get a count of how many bytes are to be pushed on the stack.
2225 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002226 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002227 // This is a sibcall. The memory operands are available in caller's
2228 // own caller's stack.
2229 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002230 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2231 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002232 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002233
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002235 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2239 FPDiff = NumBytesCallerPushed - NumBytes;
2240
2241 // Set the delta of movement of the returnaddr stackslot.
2242 // But only set if delta is greater than previous delta.
2243 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2244 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2245 }
2246
Evan Chengf22f9b32010-02-06 03:28:46 +00002247 if (!IsSibcall)
2248 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002249
Dan Gohman475871a2008-07-27 21:46:04 +00002250 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002251 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002252 if (isTailCall && FPDiff)
2253 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2254 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002255
Dan Gohman475871a2008-07-27 21:46:04 +00002256 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2257 SmallVector<SDValue, 8> MemOpChains;
2258 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002259
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002260 // Walk the register/memloc assignments, inserting copies/loads. In the case
2261 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002262 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2263 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002264 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002265 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002266 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002267 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002268
Chris Lattner423c5f42007-02-28 05:31:48 +00002269 // Promote the value if needed.
2270 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002271 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002272 case CCValAssign::Full: break;
2273 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002274 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002275 break;
2276 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002277 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002278 break;
2279 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002280 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2281 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002282 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2284 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002285 } else
2286 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2287 break;
2288 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002289 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002290 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002291 case CCValAssign::Indirect: {
2292 // Store the argument.
2293 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002294 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002295 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002296 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002297 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002298 Arg = SpillSlot;
2299 break;
2300 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002301 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002302
Chris Lattner423c5f42007-02-28 05:31:48 +00002303 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002304 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2305 if (isVarArg && IsWin64) {
2306 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2307 // shadow reg if callee is a varargs function.
2308 unsigned ShadowReg = 0;
2309 switch (VA.getLocReg()) {
2310 case X86::XMM0: ShadowReg = X86::RCX; break;
2311 case X86::XMM1: ShadowReg = X86::RDX; break;
2312 case X86::XMM2: ShadowReg = X86::R8; break;
2313 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002314 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002315 if (ShadowReg)
2316 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002317 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002318 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002319 assert(VA.isMemLoc());
2320 if (StackPtr.getNode() == 0)
2321 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2322 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2323 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002324 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002326
Evan Cheng32fe1032006-05-25 00:59:30 +00002327 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002329 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002330
Chris Lattner88e1fd52009-07-09 04:24:46 +00002331 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002332 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2333 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002334 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002335 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2336 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002337 } else {
2338 // If we are tail calling and generating PIC/GOT style code load the
2339 // address of the callee into ECX. The value in ecx is used as target of
2340 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2341 // for tail calls on PIC/GOT architectures. Normally we would just put the
2342 // address of GOT into ebx and then call target@PLT. But for tail calls
2343 // ebx would be restored (since ebx is callee saved) before jumping to the
2344 // target@PLT.
2345
2346 // Note: The actual moving to ECX is done further down.
2347 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2348 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2349 !G->getGlobal()->hasProtectedVisibility())
2350 Callee = LowerGlobalAddress(Callee, DAG);
2351 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002352 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002353 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002354 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002355
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002356 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002357 // From AMD64 ABI document:
2358 // For calls that may call functions that use varargs or stdargs
2359 // (prototype-less calls or calls to functions containing ellipsis (...) in
2360 // the declaration) %al is used as hidden argument to specify the number
2361 // of SSE registers used. The contents of %al do not need to match exactly
2362 // the number of registers, but must be an ubound on the number of SSE
2363 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002364
Gordon Henriksen86737662008-01-05 16:56:59 +00002365 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002366 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002367 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2368 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2369 };
2370 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002371 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002372 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002373
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002374 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2375 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002376 }
2377
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002378 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002379 if (isTailCall) {
2380 // Force all the incoming stack arguments to be loaded from the stack
2381 // before any new outgoing arguments are stored to the stack, because the
2382 // outgoing stack slots may alias the incoming argument stack slots, and
2383 // the alias isn't otherwise explicit. This is slightly more conservative
2384 // than necessary, because it means that each store effectively depends
2385 // on every argument instead of just those arguments it would clobber.
2386 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2387
Dan Gohman475871a2008-07-27 21:46:04 +00002388 SmallVector<SDValue, 8> MemOpChains2;
2389 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002390 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002391 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002392 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2393 CCValAssign &VA = ArgLocs[i];
2394 if (VA.isRegLoc())
2395 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002396 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002397 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002398 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002399 // Create frame index.
2400 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002401 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002402 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002403 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002404
Duncan Sands276dcbd2008-03-21 09:14:45 +00002405 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002406 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002407 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002408 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002409 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002410 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002411 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002412
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2414 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002415 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002416 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002417 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002418 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002420 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002421 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002422 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002423 }
2424 }
2425
2426 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002427 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002428 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002429
2430 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002431 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002432 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002433 }
2434
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002435 // Build a sequence of copy-to-reg nodes chained together with token chain
2436 // and flag operands which copy the outgoing args into registers.
2437 SDValue InFlag;
2438 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2439 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2440 RegsToPass[i].second, InFlag);
2441 InFlag = Chain.getValue(1);
2442 }
2443
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002444 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2445 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2446 // In the 64-bit large code model, we have to make all calls
2447 // through a register, since the call instruction's 32-bit
2448 // pc-relative offset may not be large enough to hold the whole
2449 // address.
2450 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002451 // If the callee is a GlobalAddress node (quite common, every direct call
2452 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2453 // it.
2454
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002455 // We should use extra load for direct calls to dllimported functions in
2456 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002457 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002458 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002459 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002460 bool ExtraLoad = false;
2461 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002462
Chris Lattner48a7d022009-07-09 05:02:21 +00002463 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2464 // external symbols most go through the PLT in PIC mode. If the symbol
2465 // has hidden or protected visibility, or if it is static or local, then
2466 // we don't need to use the PLT - we can directly call it.
2467 if (Subtarget->isTargetELF() &&
2468 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002469 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002470 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002471 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002472 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002473 (!Subtarget->getTargetTriple().isMacOSX() ||
2474 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002475 // PC-relative references to external symbols should go through $stub,
2476 // unless we're building with the leopard linker or later, which
2477 // automatically synthesizes these stubs.
2478 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002479 } else if (Subtarget->isPICStyleRIPRel() &&
2480 isa<Function>(GV) &&
2481 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2482 // If the function is marked as non-lazy, generate an indirect call
2483 // which loads from the GOT directly. This avoids runtime overhead
2484 // at the cost of eager binding (and one extra byte of encoding).
2485 OpFlags = X86II::MO_GOTPCREL;
2486 WrapperKind = X86ISD::WrapperRIP;
2487 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002488 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002489
Devang Patel0d881da2010-07-06 22:08:15 +00002490 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002491 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002492
2493 // Add a wrapper if needed.
2494 if (WrapperKind != ISD::DELETED_NODE)
2495 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2496 // Add extra indirection if needed.
2497 if (ExtraLoad)
2498 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2499 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002500 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002501 }
Bill Wendling056292f2008-09-16 21:48:12 +00002502 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002503 unsigned char OpFlags = 0;
2504
Evan Cheng1bf891a2010-12-01 22:59:46 +00002505 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2506 // external symbols should go through the PLT.
2507 if (Subtarget->isTargetELF() &&
2508 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2509 OpFlags = X86II::MO_PLT;
2510 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002511 (!Subtarget->getTargetTriple().isMacOSX() ||
2512 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002513 // PC-relative references to external symbols should go through $stub,
2514 // unless we're building with the leopard linker or later, which
2515 // automatically synthesizes these stubs.
2516 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002517 }
Eric Christopherfd179292009-08-27 18:07:15 +00002518
Chris Lattner48a7d022009-07-09 05:02:21 +00002519 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2520 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002521 }
2522
Chris Lattnerd96d0722007-02-25 06:40:16 +00002523 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002524 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002525 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002526
Evan Chengf22f9b32010-02-06 03:28:46 +00002527 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002528 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2529 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002530 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002532
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002533 Ops.push_back(Chain);
2534 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002535
Dan Gohman98ca4f22009-08-05 01:29:28 +00002536 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002537 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002538
Gordon Henriksen86737662008-01-05 16:56:59 +00002539 // Add argument registers to the end of the list so that they are known live
2540 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002541 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2542 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2543 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002544
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002545 // Add a register mask operand representing the call-preserved registers.
2546 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2547 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2548 assert(Mask && "Missing call preserved mask for calling convention");
2549 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002550
Gabor Greifba36cb52008-08-28 21:40:38 +00002551 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002552 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002553
Dan Gohman98ca4f22009-08-05 01:29:28 +00002554 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002555 // We used to do:
2556 //// If this is the first return lowered for this function, add the regs
2557 //// to the liveout set for the function.
2558 // This isn't right, although it's probably harmless on x86; liveouts
2559 // should be computed from returns not tail calls. Consider a void
2560 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002561 return DAG.getNode(X86ISD::TC_RETURN, dl,
2562 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002563 }
2564
Dale Johannesenace16102009-02-03 19:33:06 +00002565 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002566 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002567
Chris Lattner2d297092006-05-23 18:50:38 +00002568 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002569 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002570 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2571 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002572 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002573 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002574 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002575 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002576 // pops the hidden struct pointer, so we have to push it back.
2577 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002578 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002579 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002580 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002581 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002582
Gordon Henriksenae636f82008-01-03 16:47:34 +00002583 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002584 if (!IsSibcall) {
2585 Chain = DAG.getCALLSEQ_END(Chain,
2586 DAG.getIntPtrConstant(NumBytes, true),
2587 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2588 true),
2589 InFlag);
2590 InFlag = Chain.getValue(1);
2591 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002592
Chris Lattner3085e152007-02-25 08:59:22 +00002593 // Handle result values, copying them out of physregs into vregs that we
2594 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002595 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2596 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002597}
2598
Evan Cheng25ab6902006-09-08 06:48:29 +00002599
2600//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002601// Fast Calling Convention (tail call) implementation
2602//===----------------------------------------------------------------------===//
2603
2604// Like std call, callee cleans arguments, convention except that ECX is
2605// reserved for storing the tail called function address. Only 2 registers are
2606// free for argument passing (inreg). Tail call optimization is performed
2607// provided:
2608// * tailcallopt is enabled
2609// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002610// On X86_64 architecture with GOT-style position independent code only local
2611// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002612// To keep the stack aligned according to platform abi the function
2613// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2614// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002615// If a tail called function callee has more arguments than the caller the
2616// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002617// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002618// original REtADDR, but before the saved framepointer or the spilled registers
2619// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2620// stack layout:
2621// arg1
2622// arg2
2623// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002624// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002625// move area ]
2626// (possible EBP)
2627// ESI
2628// EDI
2629// local1 ..
2630
2631/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2632/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002633unsigned
2634X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2635 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002636 MachineFunction &MF = DAG.getMachineFunction();
2637 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002638 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002639 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002640 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002641 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002642 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002643 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2644 // Number smaller than 12 so just add the difference.
2645 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2646 } else {
2647 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002648 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002649 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002650 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002651 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002652}
2653
Evan Cheng5f941932010-02-05 02:21:12 +00002654/// MatchingStackOffset - Return true if the given stack call argument is
2655/// already available in the same position (relatively) of the caller's
2656/// incoming argument stack.
2657static
2658bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2659 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2660 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002661 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2662 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002663 if (Arg.getOpcode() == ISD::CopyFromReg) {
2664 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002665 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002666 return false;
2667 MachineInstr *Def = MRI->getVRegDef(VR);
2668 if (!Def)
2669 return false;
2670 if (!Flags.isByVal()) {
2671 if (!TII->isLoadFromStackSlot(Def, FI))
2672 return false;
2673 } else {
2674 unsigned Opcode = Def->getOpcode();
2675 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2676 Def->getOperand(1).isFI()) {
2677 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002678 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002679 } else
2680 return false;
2681 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002682 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2683 if (Flags.isByVal())
2684 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002685 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002686 // define @foo(%struct.X* %A) {
2687 // tail call @bar(%struct.X* byval %A)
2688 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002689 return false;
2690 SDValue Ptr = Ld->getBasePtr();
2691 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2692 if (!FINode)
2693 return false;
2694 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002695 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002696 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002697 FI = FINode->getIndex();
2698 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002699 } else
2700 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002701
Evan Cheng4cae1332010-03-05 08:38:04 +00002702 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002703 if (!MFI->isFixedObjectIndex(FI))
2704 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002705 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002706}
2707
Dan Gohman98ca4f22009-08-05 01:29:28 +00002708/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2709/// for tail call optimization. Targets which want to do tail call
2710/// optimization should implement this function.
2711bool
2712X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002713 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002714 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002715 bool isCalleeStructRet,
2716 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002717 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002718 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002719 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002720 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002721 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002722 CalleeCC != CallingConv::C)
2723 return false;
2724
Evan Cheng7096ae42010-01-29 06:45:59 +00002725 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002726 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002727 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002728 CallingConv::ID CallerCC = CallerF->getCallingConv();
2729 bool CCMatch = CallerCC == CalleeCC;
2730
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002731 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002732 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002733 return true;
2734 return false;
2735 }
2736
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002737 // Look for obvious safe cases to perform tail call optimization that do not
2738 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002739
Evan Cheng2c12cb42010-03-26 16:26:03 +00002740 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2741 // emit a special epilogue.
2742 if (RegInfo->needsStackRealignment(MF))
2743 return false;
2744
Evan Chenga375d472010-03-15 18:54:48 +00002745 // Also avoid sibcall optimization if either caller or callee uses struct
2746 // return semantics.
2747 if (isCalleeStructRet || isCallerStructRet)
2748 return false;
2749
Chad Rosier2416da32011-06-24 21:15:36 +00002750 // An stdcall caller is expected to clean up its arguments; the callee
2751 // isn't going to do that.
2752 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2753 return false;
2754
Chad Rosier871f6642011-05-18 19:59:50 +00002755 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002756 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002757 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002758
2759 // Optimizing for varargs on Win64 is unlikely to be safe without
2760 // additional testing.
2761 if (Subtarget->isTargetWin64())
2762 return false;
2763
Chad Rosier871f6642011-05-18 19:59:50 +00002764 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002765 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002766 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002767
Chad Rosier871f6642011-05-18 19:59:50 +00002768 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2769 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2770 if (!ArgLocs[i].isRegLoc())
2771 return false;
2772 }
2773
Chad Rosier30450e82011-12-22 22:35:21 +00002774 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2775 // stack. Therefore, if it's not used by the call it is not safe to optimize
2776 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002777 bool Unused = false;
2778 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2779 if (!Ins[i].Used) {
2780 Unused = true;
2781 break;
2782 }
2783 }
2784 if (Unused) {
2785 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002786 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002787 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002788 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002789 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002790 CCValAssign &VA = RVLocs[i];
2791 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2792 return false;
2793 }
2794 }
2795
Evan Cheng13617962010-04-30 01:12:32 +00002796 // If the calling conventions do not match, then we'd better make sure the
2797 // results are returned in the same way as what the caller expects.
2798 if (!CCMatch) {
2799 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002800 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002801 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002802 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2803
2804 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002805 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002806 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002807 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2808
2809 if (RVLocs1.size() != RVLocs2.size())
2810 return false;
2811 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2812 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2813 return false;
2814 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2815 return false;
2816 if (RVLocs1[i].isRegLoc()) {
2817 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2818 return false;
2819 } else {
2820 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2821 return false;
2822 }
2823 }
2824 }
2825
Evan Chenga6bff982010-01-30 01:22:00 +00002826 // If the callee takes no arguments then go on to check the results of the
2827 // call.
2828 if (!Outs.empty()) {
2829 // Check if stack adjustment is needed. For now, do not do this if any
2830 // argument is passed on the stack.
2831 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002832 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002833 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002834
2835 // Allocate shadow area for Win64
2836 if (Subtarget->isTargetWin64()) {
2837 CCInfo.AllocateStack(32, 8);
2838 }
2839
Duncan Sands45907662010-10-31 13:21:44 +00002840 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002841 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002842 MachineFunction &MF = DAG.getMachineFunction();
2843 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2844 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002845
2846 // Check if the arguments are already laid out in the right way as
2847 // the caller's fixed stack objects.
2848 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002849 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2850 const X86InstrInfo *TII =
2851 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002852 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2853 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002854 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002855 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002856 if (VA.getLocInfo() == CCValAssign::Indirect)
2857 return false;
2858 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002859 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2860 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002861 return false;
2862 }
2863 }
2864 }
Evan Cheng9c044672010-05-29 01:35:22 +00002865
2866 // If the tailcall address may be in a register, then make sure it's
2867 // possible to register allocate for it. In 32-bit, the call address can
2868 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002869 // callee-saved registers are restored. These happen to be the same
2870 // registers used to pass 'inreg' arguments so watch out for those.
2871 if (!Subtarget->is64Bit() &&
2872 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002873 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002874 unsigned NumInRegs = 0;
2875 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2876 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002877 if (!VA.isRegLoc())
2878 continue;
2879 unsigned Reg = VA.getLocReg();
2880 switch (Reg) {
2881 default: break;
2882 case X86::EAX: case X86::EDX: case X86::ECX:
2883 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002884 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002885 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002886 }
2887 }
2888 }
Evan Chenga6bff982010-01-30 01:22:00 +00002889 }
Evan Chengb1712452010-01-27 06:25:16 +00002890
Evan Cheng86809cc2010-02-03 03:28:02 +00002891 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002892}
2893
Dan Gohman3df24e62008-09-03 23:12:08 +00002894FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002895X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2896 const TargetLibraryInfo *libInfo) const {
2897 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002898}
2899
2900
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002901//===----------------------------------------------------------------------===//
2902// Other Lowering Hooks
2903//===----------------------------------------------------------------------===//
2904
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002905static bool MayFoldLoad(SDValue Op) {
2906 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2907}
2908
2909static bool MayFoldIntoStore(SDValue Op) {
2910 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2911}
2912
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002913static bool isTargetShuffle(unsigned Opcode) {
2914 switch(Opcode) {
2915 default: return false;
2916 case X86ISD::PSHUFD:
2917 case X86ISD::PSHUFHW:
2918 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002919 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002920 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002921 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002922 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002923 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002924 case X86ISD::MOVLPS:
2925 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002926 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002927 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002928 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002929 case X86ISD::MOVSS:
2930 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002931 case X86ISD::UNPCKL:
2932 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002933 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002934 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002935 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002936 return true;
2937 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002938}
2939
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002940static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002941 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002942 switch(Opc) {
2943 default: llvm_unreachable("Unknown x86 shuffle node");
2944 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002945 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002946 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002947 return DAG.getNode(Opc, dl, VT, V1);
2948 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002949}
2950
2951static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002952 SDValue V1, unsigned TargetMask,
2953 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002954 switch(Opc) {
2955 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002956 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002957 case X86ISD::PSHUFHW:
2958 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002959 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002960 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002961 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2962 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002963}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002964
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002965static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002966 SDValue V1, SDValue V2, unsigned TargetMask,
2967 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002968 switch(Opc) {
2969 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002970 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002971 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002972 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002973 return DAG.getNode(Opc, dl, VT, V1, V2,
2974 DAG.getConstant(TargetMask, MVT::i8));
2975 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002976}
2977
2978static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2979 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2980 switch(Opc) {
2981 default: llvm_unreachable("Unknown x86 shuffle node");
2982 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002983 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002984 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002985 case X86ISD::MOVLPS:
2986 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002987 case X86ISD::MOVSS:
2988 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002989 case X86ISD::UNPCKL:
2990 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002991 return DAG.getNode(Opc, dl, VT, V1, V2);
2992 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002993}
2994
Dan Gohmand858e902010-04-17 15:26:15 +00002995SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002996 MachineFunction &MF = DAG.getMachineFunction();
2997 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2998 int ReturnAddrIndex = FuncInfo->getRAIndex();
2999
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003000 if (ReturnAddrIndex == 0) {
3001 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00003002 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003003 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003004 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003005 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003006 }
3007
Evan Cheng25ab6902006-09-08 06:48:29 +00003008 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003009}
3010
3011
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003012bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3013 bool hasSymbolicDisplacement) {
3014 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003015 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003016 return false;
3017
3018 // If we don't have a symbolic displacement - we don't have any extra
3019 // restrictions.
3020 if (!hasSymbolicDisplacement)
3021 return true;
3022
3023 // FIXME: Some tweaks might be needed for medium code model.
3024 if (M != CodeModel::Small && M != CodeModel::Kernel)
3025 return false;
3026
3027 // For small code model we assume that latest object is 16MB before end of 31
3028 // bits boundary. We may also accept pretty large negative constants knowing
3029 // that all objects are in the positive half of address space.
3030 if (M == CodeModel::Small && Offset < 16*1024*1024)
3031 return true;
3032
3033 // For kernel code model we know that all object resist in the negative half
3034 // of 32bits address space. We may not accept negative offsets, since they may
3035 // be just off and we may accept pretty large positive ones.
3036 if (M == CodeModel::Kernel && Offset > 0)
3037 return true;
3038
3039 return false;
3040}
3041
Evan Chengef41ff62011-06-23 17:54:54 +00003042/// isCalleePop - Determines whether the callee is required to pop its
3043/// own arguments. Callee pop is necessary to support tail calls.
3044bool X86::isCalleePop(CallingConv::ID CallingConv,
3045 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3046 if (IsVarArg)
3047 return false;
3048
3049 switch (CallingConv) {
3050 default:
3051 return false;
3052 case CallingConv::X86_StdCall:
3053 return !is64Bit;
3054 case CallingConv::X86_FastCall:
3055 return !is64Bit;
3056 case CallingConv::X86_ThisCall:
3057 return !is64Bit;
3058 case CallingConv::Fast:
3059 return TailCallOpt;
3060 case CallingConv::GHC:
3061 return TailCallOpt;
3062 }
3063}
3064
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003065/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3066/// specific condition code, returning the condition code and the LHS/RHS of the
3067/// comparison to make.
3068static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3069 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003070 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003071 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3072 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3073 // X > -1 -> X == 0, jump !sign.
3074 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003075 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003076 }
3077 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003078 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003079 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003080 }
3081 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003082 // X < 1 -> X <= 0
3083 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003084 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003085 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003086 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003087
Evan Chengd9558e02006-01-06 00:43:03 +00003088 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003089 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003090 case ISD::SETEQ: return X86::COND_E;
3091 case ISD::SETGT: return X86::COND_G;
3092 case ISD::SETGE: return X86::COND_GE;
3093 case ISD::SETLT: return X86::COND_L;
3094 case ISD::SETLE: return X86::COND_LE;
3095 case ISD::SETNE: return X86::COND_NE;
3096 case ISD::SETULT: return X86::COND_B;
3097 case ISD::SETUGT: return X86::COND_A;
3098 case ISD::SETULE: return X86::COND_BE;
3099 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003100 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003102
Chris Lattner4c78e022008-12-23 23:42:27 +00003103 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003104
Chris Lattner4c78e022008-12-23 23:42:27 +00003105 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003106 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3107 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003108 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3109 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003110 }
3111
Chris Lattner4c78e022008-12-23 23:42:27 +00003112 switch (SetCCOpcode) {
3113 default: break;
3114 case ISD::SETOLT:
3115 case ISD::SETOLE:
3116 case ISD::SETUGT:
3117 case ISD::SETUGE:
3118 std::swap(LHS, RHS);
3119 break;
3120 }
3121
3122 // On a floating point condition, the flags are set as follows:
3123 // ZF PF CF op
3124 // 0 | 0 | 0 | X > Y
3125 // 0 | 0 | 1 | X < Y
3126 // 1 | 0 | 0 | X == Y
3127 // 1 | 1 | 1 | unordered
3128 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003129 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003130 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003131 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003132 case ISD::SETOLT: // flipped
3133 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003134 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003135 case ISD::SETOLE: // flipped
3136 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003137 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003138 case ISD::SETUGT: // flipped
3139 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003140 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003141 case ISD::SETUGE: // flipped
3142 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003143 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003144 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003145 case ISD::SETNE: return X86::COND_NE;
3146 case ISD::SETUO: return X86::COND_P;
3147 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003148 case ISD::SETOEQ:
3149 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003150 }
Evan Chengd9558e02006-01-06 00:43:03 +00003151}
3152
Evan Cheng4a460802006-01-11 00:33:36 +00003153/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3154/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003155/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003156static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003157 switch (X86CC) {
3158 default:
3159 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003160 case X86::COND_B:
3161 case X86::COND_BE:
3162 case X86::COND_E:
3163 case X86::COND_P:
3164 case X86::COND_A:
3165 case X86::COND_AE:
3166 case X86::COND_NE:
3167 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003168 return true;
3169 }
3170}
3171
Evan Chengeb2f9692009-10-27 19:56:55 +00003172/// isFPImmLegal - Returns true if the target can instruction select the
3173/// specified FP immediate natively. If false, the legalizer will
3174/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003175bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003176 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3177 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3178 return true;
3179 }
3180 return false;
3181}
3182
Nate Begeman9008ca62009-04-27 18:41:29 +00003183/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3184/// the specified range (L, H].
3185static bool isUndefOrInRange(int Val, int Low, int Hi) {
3186 return (Val < 0) || (Val >= Low && Val < Hi);
3187}
3188
3189/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3190/// specified value.
3191static bool isUndefOrEqual(int Val, int CmpVal) {
3192 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003193 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003195}
3196
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003197/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003198/// from position Pos and ending in Pos+Size, falls within the specified
3199/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003200static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003201 unsigned Pos, unsigned Size, int Low) {
3202 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003203 if (!isUndefOrEqual(Mask[i], Low))
3204 return false;
3205 return true;
3206}
3207
Nate Begeman9008ca62009-04-27 18:41:29 +00003208/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3209/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3210/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003211static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003212 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 return (Mask[0] < 2 && Mask[1] < 2);
3216 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003217}
3218
Nate Begeman9008ca62009-04-27 18:41:29 +00003219/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3220/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003221static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3222 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003223 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003224
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003226 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3227 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003228
Evan Cheng506d3df2006-03-29 23:07:14 +00003229 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003230 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003231 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003232 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003233
Craig Toppera9a568a2012-05-02 08:03:44 +00003234 if (VT == MVT::v16i16) {
3235 // Lower quadword copied in order or undef.
3236 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3237 return false;
3238
3239 // Upper quadword shuffled.
3240 for (unsigned i = 12; i != 16; ++i)
3241 if (!isUndefOrInRange(Mask[i], 12, 16))
3242 return false;
3243 }
3244
Evan Cheng506d3df2006-03-29 23:07:14 +00003245 return true;
3246}
3247
Nate Begeman9008ca62009-04-27 18:41:29 +00003248/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3249/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003250static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3251 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003252 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003253
Rafael Espindola15684b22009-04-24 12:40:33 +00003254 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003255 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3256 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003257
Rafael Espindola15684b22009-04-24 12:40:33 +00003258 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003259 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003260 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003261 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003262
Craig Toppera9a568a2012-05-02 08:03:44 +00003263 if (VT == MVT::v16i16) {
3264 // Upper quadword copied in order.
3265 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3266 return false;
3267
3268 // Lower quadword shuffled.
3269 for (unsigned i = 8; i != 12; ++i)
3270 if (!isUndefOrInRange(Mask[i], 8, 12))
3271 return false;
3272 }
3273
Rafael Espindola15684b22009-04-24 12:40:33 +00003274 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003275}
3276
Nate Begemana09008b2009-10-19 02:17:23 +00003277/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3278/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003279static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3280 const X86Subtarget *Subtarget) {
3281 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3282 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003283 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003284
Craig Topper0e2037b2012-01-20 05:53:00 +00003285 unsigned NumElts = VT.getVectorNumElements();
3286 unsigned NumLanes = VT.getSizeInBits()/128;
3287 unsigned NumLaneElts = NumElts/NumLanes;
3288
3289 // Do not handle 64-bit element shuffles with palignr.
3290 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003291 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003292
Craig Topper0e2037b2012-01-20 05:53:00 +00003293 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3294 unsigned i;
3295 for (i = 0; i != NumLaneElts; ++i) {
3296 if (Mask[i+l] >= 0)
3297 break;
3298 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003299
Craig Topper0e2037b2012-01-20 05:53:00 +00003300 // Lane is all undef, go to next lane
3301 if (i == NumLaneElts)
3302 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003303
Craig Topper0e2037b2012-01-20 05:53:00 +00003304 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003305
Craig Topper0e2037b2012-01-20 05:53:00 +00003306 // Make sure its in this lane in one of the sources
3307 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3308 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003309 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003310
3311 // If not lane 0, then we must match lane 0
3312 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3313 return false;
3314
3315 // Correct second source to be contiguous with first source
3316 if (Start >= (int)NumElts)
3317 Start -= NumElts - NumLaneElts;
3318
3319 // Make sure we're shifting in the right direction.
3320 if (Start <= (int)(i+l))
3321 return false;
3322
3323 Start -= i;
3324
3325 // Check the rest of the elements to see if they are consecutive.
3326 for (++i; i != NumLaneElts; ++i) {
3327 int Idx = Mask[i+l];
3328
3329 // Make sure its in this lane
3330 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3331 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3332 return false;
3333
3334 // If not lane 0, then we must match lane 0
3335 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3336 return false;
3337
3338 if (Idx >= (int)NumElts)
3339 Idx -= NumElts - NumLaneElts;
3340
3341 if (!isUndefOrEqual(Idx, Start+i))
3342 return false;
3343
3344 }
Nate Begemana09008b2009-10-19 02:17:23 +00003345 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003346
Nate Begemana09008b2009-10-19 02:17:23 +00003347 return true;
3348}
3349
Craig Topper1a7700a2012-01-19 08:19:12 +00003350/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3351/// the two vector operands have swapped position.
3352static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3353 unsigned NumElems) {
3354 for (unsigned i = 0; i != NumElems; ++i) {
3355 int idx = Mask[i];
3356 if (idx < 0)
3357 continue;
3358 else if (idx < (int)NumElems)
3359 Mask[i] = idx + NumElems;
3360 else
3361 Mask[i] = idx - NumElems;
3362 }
3363}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003364
Craig Topper1a7700a2012-01-19 08:19:12 +00003365/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3366/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3367/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3368/// reverse of what x86 shuffles want.
3369static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3370 bool Commuted = false) {
3371 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003372 return false;
3373
Craig Topper1a7700a2012-01-19 08:19:12 +00003374 unsigned NumElems = VT.getVectorNumElements();
3375 unsigned NumLanes = VT.getSizeInBits()/128;
3376 unsigned NumLaneElems = NumElems/NumLanes;
3377
3378 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003379 return false;
3380
3381 // VSHUFPSY divides the resulting vector into 4 chunks.
3382 // The sources are also splitted into 4 chunks, and each destination
3383 // chunk must come from a different source chunk.
3384 //
3385 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3386 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3387 //
3388 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3389 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3390 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003391 // VSHUFPDY divides the resulting vector into 4 chunks.
3392 // The sources are also splitted into 4 chunks, and each destination
3393 // chunk must come from a different source chunk.
3394 //
3395 // SRC1 => X3 X2 X1 X0
3396 // SRC2 => Y3 Y2 Y1 Y0
3397 //
3398 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3399 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003400 unsigned HalfLaneElems = NumLaneElems/2;
3401 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3402 for (unsigned i = 0; i != NumLaneElems; ++i) {
3403 int Idx = Mask[i+l];
3404 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3405 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3406 return false;
3407 // For VSHUFPSY, the mask of the second half must be the same as the
3408 // first but with the appropriate offsets. This works in the same way as
3409 // VPERMILPS works with masks.
3410 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3411 continue;
3412 if (!isUndefOrEqual(Idx, Mask[i]+l))
3413 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003414 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003415 }
3416
3417 return true;
3418}
3419
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003420/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3421/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003422static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003423 unsigned NumElems = VT.getVectorNumElements();
3424
3425 if (VT.getSizeInBits() != 128)
3426 return false;
3427
3428 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003429 return false;
3430
Evan Cheng2064a2b2006-03-28 06:50:32 +00003431 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003432 return isUndefOrEqual(Mask[0], 6) &&
3433 isUndefOrEqual(Mask[1], 7) &&
3434 isUndefOrEqual(Mask[2], 2) &&
3435 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003436}
3437
Nate Begeman0b10b912009-11-07 23:17:15 +00003438/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3439/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3440/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003441static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003442 unsigned NumElems = VT.getVectorNumElements();
3443
3444 if (VT.getSizeInBits() != 128)
3445 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003446
Nate Begeman0b10b912009-11-07 23:17:15 +00003447 if (NumElems != 4)
3448 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003449
Craig Topperdd637ae2012-02-19 05:41:45 +00003450 return isUndefOrEqual(Mask[0], 2) &&
3451 isUndefOrEqual(Mask[1], 3) &&
3452 isUndefOrEqual(Mask[2], 2) &&
3453 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003454}
3455
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3457/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003458static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003459 if (VT.getSizeInBits() != 128)
3460 return false;
3461
Craig Topperdd637ae2012-02-19 05:41:45 +00003462 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
Evan Cheng5ced1d82006-04-06 23:23:56 +00003464 if (NumElems != 2 && NumElems != 4)
3465 return false;
3466
Chad Rosier238ae312012-04-30 17:47:15 +00003467 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003468 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003469 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003470
Chad Rosier238ae312012-04-30 17:47:15 +00003471 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003472 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003473 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003474
3475 return true;
3476}
3477
Nate Begeman0b10b912009-11-07 23:17:15 +00003478/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3479/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003480static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
3481 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003482
David Greenea20244d2011-03-02 17:23:43 +00003483 if ((NumElems != 2 && NumElems != 4)
Craig Topperdd637ae2012-02-19 05:41:45 +00003484 || VT.getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003485 return false;
3486
Chad Rosier238ae312012-04-30 17:47:15 +00003487 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003488 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003489 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003490
Chad Rosier238ae312012-04-30 17:47:15 +00003491 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3492 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003493 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003494
3495 return true;
3496}
3497
Elena Demikhovsky15963732012-06-26 08:04:10 +00003498//
3499// Some special combinations that can be optimized.
3500//
3501static
3502SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3503 SelectionDAG &DAG) {
3504 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003505 DebugLoc dl = SVOp->getDebugLoc();
3506
3507 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3508 return SDValue();
3509
3510 ArrayRef<int> Mask = SVOp->getMask();
3511
3512 // These are the special masks that may be optimized.
3513 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3514 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3515 bool MatchEvenMask = true;
3516 bool MatchOddMask = true;
3517 for (int i=0; i<8; ++i) {
3518 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3519 MatchEvenMask = false;
3520 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3521 MatchOddMask = false;
3522 }
3523 static const int CompactionMaskEven[] = {0, 2, -1, -1, 4, 6, -1, -1};
3524 static const int CompactionMaskOdd [] = {1, 3, -1, -1, 5, 7, -1, -1};
3525
3526 const int *CompactionMask;
3527 if (MatchEvenMask)
3528 CompactionMask = CompactionMaskEven;
3529 else if (MatchOddMask)
3530 CompactionMask = CompactionMaskOdd;
3531 else
3532 return SDValue();
3533
3534 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3535
3536 SDValue Op0 = DAG.getVectorShuffle(VT, dl, SVOp->getOperand(0),
3537 UndefNode, CompactionMask);
3538 SDValue Op1 = DAG.getVectorShuffle(VT, dl, SVOp->getOperand(1),
3539 UndefNode, CompactionMask);
3540 static const int UnpackMask[] = {0, 8, 1, 9, 4, 12, 5, 13};
3541 return DAG.getVectorShuffle(VT, dl, Op0, Op1, UnpackMask);
3542}
3543
Evan Cheng0038e592006-03-28 00:39:58 +00003544/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3545/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003546static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003547 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003548 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003549
3550 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3551 "Unsupported vector type for unpckh");
3552
Craig Topper6347e862011-11-21 06:57:39 +00003553 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003554 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003555 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003556
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003557 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3558 // independently on 128-bit lanes.
3559 unsigned NumLanes = VT.getSizeInBits()/128;
3560 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003561
Craig Topper94438ba2011-12-16 08:06:31 +00003562 for (unsigned l = 0; l != NumLanes; ++l) {
3563 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3564 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003565 i += 2, ++j) {
3566 int BitI = Mask[i];
3567 int BitI1 = Mask[i+1];
3568 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003569 return false;
David Greenea20244d2011-03-02 17:23:43 +00003570 if (V2IsSplat) {
3571 if (!isUndefOrEqual(BitI1, NumElts))
3572 return false;
3573 } else {
3574 if (!isUndefOrEqual(BitI1, j + NumElts))
3575 return false;
3576 }
Evan Cheng39623da2006-04-20 08:58:49 +00003577 }
Evan Cheng0038e592006-03-28 00:39:58 +00003578 }
David Greenea20244d2011-03-02 17:23:43 +00003579
Evan Cheng0038e592006-03-28 00:39:58 +00003580 return true;
3581}
3582
Evan Cheng4fcb9222006-03-28 02:43:26 +00003583/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3584/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003585static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003586 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003587 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003588
3589 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3590 "Unsupported vector type for unpckh");
3591
Craig Topper6347e862011-11-21 06:57:39 +00003592 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003593 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003594 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003595
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003596 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3597 // independently on 128-bit lanes.
3598 unsigned NumLanes = VT.getSizeInBits()/128;
3599 unsigned NumLaneElts = NumElts/NumLanes;
3600
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003601 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003602 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3603 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003604 int BitI = Mask[i];
3605 int BitI1 = Mask[i+1];
3606 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003607 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003608 if (V2IsSplat) {
3609 if (isUndefOrEqual(BitI1, NumElts))
3610 return false;
3611 } else {
3612 if (!isUndefOrEqual(BitI1, j+NumElts))
3613 return false;
3614 }
Evan Cheng39623da2006-04-20 08:58:49 +00003615 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003616 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003617 return true;
3618}
3619
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003620/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3621/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3622/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003623static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003624 bool HasAVX2) {
3625 unsigned NumElts = VT.getVectorNumElements();
3626
3627 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3628 "Unsupported vector type for unpckh");
3629
3630 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3631 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003632 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003633
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003634 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3635 // FIXME: Need a better way to get rid of this, there's no latency difference
3636 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3637 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003638 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003639 return false;
3640
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003641 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3642 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003643 unsigned NumLanes = VT.getSizeInBits()/128;
3644 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003645
Craig Topper94438ba2011-12-16 08:06:31 +00003646 for (unsigned l = 0; l != NumLanes; ++l) {
3647 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3648 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003649 i += 2, ++j) {
3650 int BitI = Mask[i];
3651 int BitI1 = Mask[i+1];
3652
3653 if (!isUndefOrEqual(BitI, j))
3654 return false;
3655 if (!isUndefOrEqual(BitI1, j))
3656 return false;
3657 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003658 }
David Greenea20244d2011-03-02 17:23:43 +00003659
Rafael Espindola15684b22009-04-24 12:40:33 +00003660 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003661}
3662
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003663/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3664/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3665/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003666static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003667 unsigned NumElts = VT.getVectorNumElements();
3668
3669 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3670 "Unsupported vector type for unpckh");
3671
3672 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3673 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003674 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003675
Craig Topper94438ba2011-12-16 08:06:31 +00003676 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3677 // independently on 128-bit lanes.
3678 unsigned NumLanes = VT.getSizeInBits()/128;
3679 unsigned NumLaneElts = NumElts/NumLanes;
3680
3681 for (unsigned l = 0; l != NumLanes; ++l) {
3682 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3683 i != (l+1)*NumLaneElts; i += 2, ++j) {
3684 int BitI = Mask[i];
3685 int BitI1 = Mask[i+1];
3686 if (!isUndefOrEqual(BitI, j))
3687 return false;
3688 if (!isUndefOrEqual(BitI1, j))
3689 return false;
3690 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003691 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003692 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003693}
3694
Evan Cheng017dcc62006-04-21 01:05:10 +00003695/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3696/// specifies a shuffle of elements that is suitable for input to MOVSS,
3697/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003698static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003699 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003700 return false;
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003701 if (VT.getSizeInBits() == 256)
3702 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003703
Craig Topperc612d792012-01-02 09:17:37 +00003704 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003705
Nate Begeman9008ca62009-04-27 18:41:29 +00003706 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003707 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003708
Craig Topperc612d792012-01-02 09:17:37 +00003709 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003711 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003712
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003713 return true;
3714}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003715
Craig Topper70b883b2011-11-28 10:14:51 +00003716/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003717/// as permutations between 128-bit chunks or halves. As an example: this
3718/// shuffle bellow:
3719/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3720/// The first half comes from the second half of V1 and the second half from the
3721/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003722static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003723 if (!HasAVX || VT.getSizeInBits() != 256)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003724 return false;
3725
3726 // The shuffle result is divided into half A and half B. In total the two
3727 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3728 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003729 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003730 bool MatchA = false, MatchB = false;
3731
3732 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003733 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003734 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3735 MatchA = true;
3736 break;
3737 }
3738 }
3739
3740 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003741 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003742 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3743 MatchB = true;
3744 break;
3745 }
3746 }
3747
3748 return MatchA && MatchB;
3749}
3750
Craig Topper70b883b2011-11-28 10:14:51 +00003751/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3752/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003753static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003754 EVT VT = SVOp->getValueType(0);
3755
Craig Topperc612d792012-01-02 09:17:37 +00003756 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757
Craig Topperc612d792012-01-02 09:17:37 +00003758 unsigned FstHalf = 0, SndHalf = 0;
3759 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003760 if (SVOp->getMaskElt(i) > 0) {
3761 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3762 break;
3763 }
3764 }
Craig Topperc612d792012-01-02 09:17:37 +00003765 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003766 if (SVOp->getMaskElt(i) > 0) {
3767 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3768 break;
3769 }
3770 }
3771
3772 return (FstHalf | (SndHalf << 4));
3773}
3774
Craig Topper70b883b2011-11-28 10:14:51 +00003775/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003776/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3777/// Note that VPERMIL mask matching is different depending whether theunderlying
3778/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3779/// to the same elements of the low, but to the higher half of the source.
3780/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003781/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003782static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003783 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003784 return false;
3785
Craig Topperc612d792012-01-02 09:17:37 +00003786 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003787 // Only match 256-bit with 32/64-bit types
3788 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003789 return false;
3790
Craig Topperc612d792012-01-02 09:17:37 +00003791 unsigned NumLanes = VT.getSizeInBits()/128;
3792 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003793 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003794 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003795 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003796 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003797 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003798 continue;
3799 // VPERMILPS handling
3800 if (Mask[i] < 0)
3801 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003802 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003803 return false;
3804 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003805 }
3806
3807 return true;
3808}
3809
Craig Topper5aaffa82012-02-19 02:53:47 +00003810/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003811/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003812/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003813static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topperc612d792012-01-02 09:17:37 +00003815 unsigned NumOps = VT.getVectorNumElements();
Craig Topper97327dc2012-03-18 22:50:10 +00003816 if (VT.getSizeInBits() == 256)
3817 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00003818 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003819 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003820
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003822 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003823
Craig Topperc612d792012-01-02 09:17:37 +00003824 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003825 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3826 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3827 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003829
Evan Cheng39623da2006-04-20 08:58:49 +00003830 return true;
3831}
3832
Evan Chengd9539472006-04-14 21:59:03 +00003833/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3834/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003835/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003836static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003837 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003838 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003839 return false;
3840
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003841 unsigned NumElems = VT.getVectorNumElements();
3842
3843 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3844 (VT.getSizeInBits() == 256 && NumElems != 8))
3845 return false;
3846
3847 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003848 for (unsigned i = 0; i != NumElems; i += 2)
3849 if (!isUndefOrEqual(Mask[i], i+1) ||
3850 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003851 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003852
3853 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003854}
3855
3856/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3857/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003858/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003859static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003860 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003861 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003862 return false;
3863
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003864 unsigned NumElems = VT.getVectorNumElements();
3865
3866 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3867 (VT.getSizeInBits() == 256 && NumElems != 8))
3868 return false;
3869
3870 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003871 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003872 if (!isUndefOrEqual(Mask[i], i) ||
3873 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003875
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003876 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003877}
3878
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003879/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3880/// specifies a shuffle of elements that is suitable for input to 256-bit
3881/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003882static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topperc612d792012-01-02 09:17:37 +00003883 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003884
Craig Topperbeabc6c2011-12-05 06:56:46 +00003885 if (!HasAVX || VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003886 return false;
3887
Craig Topperc612d792012-01-02 09:17:37 +00003888 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003889 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003890 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003891 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003892 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003893 return false;
3894 return true;
3895}
3896
Evan Cheng0b457f02008-09-25 20:50:48 +00003897/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003898/// specifies a shuffle of elements that is suitable for input to 128-bit
3899/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003900static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003901 if (VT.getSizeInBits() != 128)
3902 return false;
3903
Craig Topperc612d792012-01-02 09:17:37 +00003904 unsigned e = VT.getVectorNumElements() / 2;
3905 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003906 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003907 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003908 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003909 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003910 return false;
3911 return true;
3912}
3913
David Greenec38a03e2011-02-03 15:50:00 +00003914/// isVEXTRACTF128Index - Return true if the specified
3915/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3916/// suitable for input to VEXTRACTF128.
3917bool X86::isVEXTRACTF128Index(SDNode *N) {
3918 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3919 return false;
3920
3921 // The index should be aligned on a 128-bit boundary.
3922 uint64_t Index =
3923 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3924
3925 unsigned VL = N->getValueType(0).getVectorNumElements();
3926 unsigned VBits = N->getValueType(0).getSizeInBits();
3927 unsigned ElSize = VBits / VL;
3928 bool Result = (Index * ElSize) % 128 == 0;
3929
3930 return Result;
3931}
3932
David Greeneccacdc12011-02-04 16:08:29 +00003933/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3934/// operand specifies a subvector insert that is suitable for input to
3935/// VINSERTF128.
3936bool X86::isVINSERTF128Index(SDNode *N) {
3937 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3938 return false;
3939
3940 // The index should be aligned on a 128-bit boundary.
3941 uint64_t Index =
3942 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3943
3944 unsigned VL = N->getValueType(0).getVectorNumElements();
3945 unsigned VBits = N->getValueType(0).getSizeInBits();
3946 unsigned ElSize = VBits / VL;
3947 bool Result = (Index * ElSize) % 128 == 0;
3948
3949 return Result;
3950}
3951
Evan Cheng63d33002006-03-22 08:01:21 +00003952/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003953/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003954/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003955static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003956 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003957
Craig Topper1a7700a2012-01-19 08:19:12 +00003958 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3959 "Unsupported vector type for PSHUF/SHUFP");
3960
3961 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3962 // independently on 128-bit lanes.
3963 unsigned NumElts = VT.getVectorNumElements();
3964 unsigned NumLanes = VT.getSizeInBits()/128;
3965 unsigned NumLaneElts = NumElts/NumLanes;
3966
3967 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3968 "Only supports 2 or 4 elements per lane");
3969
3970 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003971 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003972 for (unsigned i = 0; i != NumElts; ++i) {
3973 int Elt = N->getMaskElt(i);
3974 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003975 Elt &= NumLaneElts - 1;
3976 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003977 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003978 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003979
Evan Cheng63d33002006-03-22 08:01:21 +00003980 return Mask;
3981}
3982
Evan Cheng506d3df2006-03-29 23:07:14 +00003983/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003984/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003985static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003986 EVT VT = N->getValueType(0);
3987
3988 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3989 "Unsupported vector type for PSHUFHW");
3990
3991 unsigned NumElts = VT.getVectorNumElements();
3992
Evan Cheng506d3df2006-03-29 23:07:14 +00003993 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003994 for (unsigned l = 0; l != NumElts; l += 8) {
3995 // 8 nodes per lane, but we only care about the last 4.
3996 for (unsigned i = 0; i < 4; ++i) {
3997 int Elt = N->getMaskElt(l+i+4);
3998 if (Elt < 0) continue;
3999 Elt &= 0x3; // only 2-bits.
4000 Mask |= Elt << (i * 2);
4001 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004002 }
Craig Topper6b28d352012-05-03 07:12:59 +00004003
Evan Cheng506d3df2006-03-29 23:07:14 +00004004 return Mask;
4005}
4006
4007/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004008/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004009static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004010 EVT VT = N->getValueType(0);
4011
4012 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4013 "Unsupported vector type for PSHUFHW");
4014
4015 unsigned NumElts = VT.getVectorNumElements();
4016
Evan Cheng506d3df2006-03-29 23:07:14 +00004017 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004018 for (unsigned l = 0; l != NumElts; l += 8) {
4019 // 8 nodes per lane, but we only care about the first 4.
4020 for (unsigned i = 0; i < 4; ++i) {
4021 int Elt = N->getMaskElt(l+i);
4022 if (Elt < 0) continue;
4023 Elt &= 0x3; // only 2-bits
4024 Mask |= Elt << (i * 2);
4025 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004026 }
Craig Topper6b28d352012-05-03 07:12:59 +00004027
Evan Cheng506d3df2006-03-29 23:07:14 +00004028 return Mask;
4029}
4030
Nate Begemana09008b2009-10-19 02:17:23 +00004031/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4032/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004033static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4034 EVT VT = SVOp->getValueType(0);
4035 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004036
Craig Topper0e2037b2012-01-20 05:53:00 +00004037 unsigned NumElts = VT.getVectorNumElements();
4038 unsigned NumLanes = VT.getSizeInBits()/128;
4039 unsigned NumLaneElts = NumElts/NumLanes;
4040
4041 int Val = 0;
4042 unsigned i;
4043 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004044 Val = SVOp->getMaskElt(i);
4045 if (Val >= 0)
4046 break;
4047 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004048 if (Val >= (int)NumElts)
4049 Val -= NumElts - NumLaneElts;
4050
Eli Friedman63f8dde2011-07-25 21:36:45 +00004051 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004052 return (Val - i) * EltSize;
4053}
4054
David Greenec38a03e2011-02-03 15:50:00 +00004055/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4056/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4057/// instructions.
4058unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4059 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4060 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4061
4062 uint64_t Index =
4063 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4064
4065 EVT VecVT = N->getOperand(0).getValueType();
4066 EVT ElVT = VecVT.getVectorElementType();
4067
4068 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004069 return Index / NumElemsPerChunk;
4070}
4071
David Greeneccacdc12011-02-04 16:08:29 +00004072/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4073/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4074/// instructions.
4075unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4076 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4077 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4078
4079 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004080 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004081
4082 EVT VecVT = N->getValueType(0);
4083 EVT ElVT = VecVT.getVectorElementType();
4084
4085 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004086 return Index / NumElemsPerChunk;
4087}
4088
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004089/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4090/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4091/// Handles 256-bit.
4092static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4093 EVT VT = N->getValueType(0);
4094
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004095 unsigned NumElts = VT.getVectorNumElements();
4096
Craig Topper095c5282012-04-15 23:48:57 +00004097 assert((VT.is256BitVector() && NumElts == 4) &&
4098 "Unsupported vector type for VPERMQ/VPERMPD");
4099
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004100 unsigned Mask = 0;
4101 for (unsigned i = 0; i != NumElts; ++i) {
4102 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004103 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004104 continue;
4105 Mask |= Elt << (i*2);
4106 }
4107
4108 return Mask;
4109}
Evan Cheng37b73872009-07-30 08:33:02 +00004110/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4111/// constant +0.0.
4112bool X86::isZeroNode(SDValue Elt) {
4113 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004114 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004115 (isa<ConstantFPSDNode>(Elt) &&
4116 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4117}
4118
Nate Begeman9008ca62009-04-27 18:41:29 +00004119/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4120/// their permute mask.
4121static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4122 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004123 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004124 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004125 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004126
Nate Begeman5a5ca152009-04-29 05:20:52 +00004127 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004128 int Idx = SVOp->getMaskElt(i);
4129 if (Idx >= 0) {
4130 if (Idx < (int)NumElems)
4131 Idx += NumElems;
4132 else
4133 Idx -= NumElems;
4134 }
4135 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004136 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4138 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004139}
4140
Evan Cheng533a0aa2006-04-19 20:35:22 +00004141/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4142/// match movhlps. The lower half elements should come from upper half of
4143/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004144/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004145static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004146 if (VT.getSizeInBits() != 128)
4147 return false;
4148 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004149 return false;
4150 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004151 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004152 return false;
4153 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004154 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004155 return false;
4156 return true;
4157}
4158
Evan Cheng5ced1d82006-04-06 23:23:56 +00004159/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004160/// is promoted to a vector. It also returns the LoadSDNode by reference if
4161/// required.
4162static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004163 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4164 return false;
4165 N = N->getOperand(0).getNode();
4166 if (!ISD::isNON_EXTLoad(N))
4167 return false;
4168 if (LD)
4169 *LD = cast<LoadSDNode>(N);
4170 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004171}
4172
Dan Gohman65fd6562011-11-03 21:49:52 +00004173// Test whether the given value is a vector value which will be legalized
4174// into a load.
4175static bool WillBeConstantPoolLoad(SDNode *N) {
4176 if (N->getOpcode() != ISD::BUILD_VECTOR)
4177 return false;
4178
4179 // Check for any non-constant elements.
4180 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4181 switch (N->getOperand(i).getNode()->getOpcode()) {
4182 case ISD::UNDEF:
4183 case ISD::ConstantFP:
4184 case ISD::Constant:
4185 break;
4186 default:
4187 return false;
4188 }
4189
4190 // Vectors of all-zeros and all-ones are materialized with special
4191 // instructions rather than being loaded.
4192 return !ISD::isBuildVectorAllZeros(N) &&
4193 !ISD::isBuildVectorAllOnes(N);
4194}
4195
Evan Cheng533a0aa2006-04-19 20:35:22 +00004196/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4197/// match movlp{s|d}. The lower half elements should come from lower half of
4198/// V1 (and in order), and the upper half elements should come from the upper
4199/// half of V2 (and in order). And since V1 will become the source of the
4200/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004201static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004202 ArrayRef<int> Mask, EVT VT) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004203 if (VT.getSizeInBits() != 128)
4204 return false;
4205
Evan Cheng466685d2006-10-09 20:57:25 +00004206 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004207 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004208 // Is V2 is a vector load, don't do this transformation. We will try to use
4209 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004210 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004211 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004212
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004213 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004214
Evan Cheng533a0aa2006-04-19 20:35:22 +00004215 if (NumElems != 2 && NumElems != 4)
4216 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004217 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004218 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004219 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004220 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004221 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004222 return false;
4223 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004224}
4225
Evan Cheng39623da2006-04-20 08:58:49 +00004226/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4227/// all the same.
4228static bool isSplatVector(SDNode *N) {
4229 if (N->getOpcode() != ISD::BUILD_VECTOR)
4230 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004231
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004233 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4234 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004235 return false;
4236 return true;
4237}
4238
Evan Cheng213d2cf2007-05-17 18:45:50 +00004239/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004240/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004241/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004242static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004243 SDValue V1 = N->getOperand(0);
4244 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004245 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4246 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004247 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004248 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004250 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4251 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004252 if (Opc != ISD::BUILD_VECTOR ||
4253 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004254 return false;
4255 } else if (Idx >= 0) {
4256 unsigned Opc = V1.getOpcode();
4257 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4258 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004259 if (Opc != ISD::BUILD_VECTOR ||
4260 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004261 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004262 }
4263 }
4264 return true;
4265}
4266
4267/// getZeroVector - Returns a vector of specified type with all zero elements.
4268///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004269static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004270 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004271 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004272 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004273
Dale Johannesen0488fb62010-09-30 23:57:10 +00004274 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004275 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004276 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004277 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004278 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004279 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4280 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4281 } else { // SSE1
4282 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4283 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4284 }
Craig Topper9d352402012-04-23 07:24:41 +00004285 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004286 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004287 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4288 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4289 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4290 } else {
4291 // 256-bit logic and arithmetic instructions in AVX are all
4292 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4293 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4294 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4295 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4296 }
Craig Topper9d352402012-04-23 07:24:41 +00004297 } else
4298 llvm_unreachable("Unexpected vector type");
4299
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004300 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004301}
4302
Chris Lattner8a594482007-11-25 00:24:49 +00004303/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004304/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4305/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4306/// Then bitcast to their original type, ensuring they get CSE'd.
4307static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4308 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004310 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004311
Owen Anderson825b72b2009-08-11 20:47:22 +00004312 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004313 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004314 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004315 if (HasAVX2) { // AVX2
4316 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4317 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4318 } else { // AVX
4319 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004320 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004321 }
Craig Topper9d352402012-04-23 07:24:41 +00004322 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004323 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004324 } else
4325 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004326
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004327 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004328}
4329
Evan Cheng39623da2006-04-20 08:58:49 +00004330/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4331/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004332static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004333 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004334 if (Mask[i] > (int)NumElems) {
4335 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004336 }
Evan Cheng39623da2006-04-20 08:58:49 +00004337 }
Evan Cheng39623da2006-04-20 08:58:49 +00004338}
4339
Evan Cheng017dcc62006-04-21 01:05:10 +00004340/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4341/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004342static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 SDValue V2) {
4344 unsigned NumElems = VT.getVectorNumElements();
4345 SmallVector<int, 8> Mask;
4346 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004347 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 Mask.push_back(i);
4349 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004350}
4351
Nate Begeman9008ca62009-04-27 18:41:29 +00004352/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004353static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 SDValue V2) {
4355 unsigned NumElems = VT.getVectorNumElements();
4356 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004357 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 Mask.push_back(i);
4359 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004360 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004362}
4363
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004364/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004365static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 SDValue V2) {
4367 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004369 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 Mask.push_back(i + Half);
4371 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004372 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004373 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004374}
4375
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004376// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004377// a generic shuffle instruction because the target has no such instructions.
4378// Generate shuffles which repeat i16 and i8 several times until they can be
4379// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004380static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004381 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004383 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004384
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 while (NumElems > 4) {
4386 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004387 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 EltNo -= NumElems/2;
4391 }
4392 NumElems >>= 1;
4393 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004394 return V;
4395}
Eric Christopherfd179292009-08-27 18:07:15 +00004396
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4398static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4399 EVT VT = V.getValueType();
4400 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004401 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004402
Craig Topper9d352402012-04-23 07:24:41 +00004403 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004404 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004406 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4407 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004408 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004409 // To use VPERMILPS to splat scalars, the second half of indicies must
4410 // refer to the higher part, which is a duplication of the lower one,
4411 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004412 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4413 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004414
4415 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4416 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4417 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004418 } else
4419 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420
4421 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4422}
4423
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004424/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004425static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4426 EVT SrcVT = SV->getValueType(0);
4427 SDValue V1 = SV->getOperand(0);
4428 DebugLoc dl = SV->getDebugLoc();
4429
4430 int EltNo = SV->getSplatIndex();
4431 int NumElems = SrcVT.getVectorNumElements();
4432 unsigned Size = SrcVT.getSizeInBits();
4433
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004434 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4435 "Unknown how to promote splat for type");
4436
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004437 // Extract the 128-bit part containing the splat element and update
4438 // the splat element index when it refers to the higher register.
4439 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004440 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4441 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004442 EltNo -= NumElems/2;
4443 }
4444
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004445 // All i16 and i8 vector types can't be used directly by a generic shuffle
4446 // instruction because the target has no such instruction. Generate shuffles
4447 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004448 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004449 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004450 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004451 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452
4453 // Recreate the 256-bit vector and place the same 128-bit vector
4454 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004455 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004456 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004457 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004458 }
4459
4460 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004461}
4462
Evan Chengba05f722006-04-21 23:03:30 +00004463/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004464/// vector of zero or undef vector. This produces a shuffle where the low
4465/// element of V2 is swizzled into the zero/undef vector, landing at element
4466/// 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 +00004467static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004468 bool IsZero,
4469 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004470 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004471 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004472 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004473 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004474 unsigned NumElems = VT.getVectorNumElements();
4475 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004476 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 // If this is the insertion idx, put the low elt of V2 here.
4478 MaskVec.push_back(i == Idx ? NumElems : i);
4479 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004480}
4481
Craig Toppera1ffc682012-03-20 06:42:26 +00004482/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4483/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004484/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004485static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004486 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004487 unsigned NumElems = VT.getVectorNumElements();
4488 SDValue ImmN;
4489
Craig Topper89f4e662012-03-20 07:17:59 +00004490 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004491 switch(N->getOpcode()) {
4492 case X86ISD::SHUFP:
4493 ImmN = N->getOperand(N->getNumOperands()-1);
4494 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4495 break;
4496 case X86ISD::UNPCKH:
4497 DecodeUNPCKHMask(VT, Mask);
4498 break;
4499 case X86ISD::UNPCKL:
4500 DecodeUNPCKLMask(VT, Mask);
4501 break;
4502 case X86ISD::MOVHLPS:
4503 DecodeMOVHLPSMask(NumElems, Mask);
4504 break;
4505 case X86ISD::MOVLHPS:
4506 DecodeMOVLHPSMask(NumElems, Mask);
4507 break;
4508 case X86ISD::PSHUFD:
4509 case X86ISD::VPERMILP:
4510 ImmN = N->getOperand(N->getNumOperands()-1);
4511 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004512 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004513 break;
4514 case X86ISD::PSHUFHW:
4515 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004516 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004517 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004518 break;
4519 case X86ISD::PSHUFLW:
4520 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004521 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004522 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004523 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004524 case X86ISD::VPERMI:
4525 ImmN = N->getOperand(N->getNumOperands()-1);
4526 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4527 IsUnary = true;
4528 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004529 case X86ISD::MOVSS:
4530 case X86ISD::MOVSD: {
4531 // The index 0 always comes from the first element of the second source,
4532 // this is why MOVSS and MOVSD are used in the first place. The other
4533 // elements come from the other positions of the first source vector
4534 Mask.push_back(NumElems);
4535 for (unsigned i = 1; i != NumElems; ++i) {
4536 Mask.push_back(i);
4537 }
4538 break;
4539 }
4540 case X86ISD::VPERM2X128:
4541 ImmN = N->getOperand(N->getNumOperands()-1);
4542 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004543 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004544 break;
4545 case X86ISD::MOVDDUP:
4546 case X86ISD::MOVLHPD:
4547 case X86ISD::MOVLPD:
4548 case X86ISD::MOVLPS:
4549 case X86ISD::MOVSHDUP:
4550 case X86ISD::MOVSLDUP:
4551 case X86ISD::PALIGN:
4552 // Not yet implemented
4553 return false;
4554 default: llvm_unreachable("unknown target shuffle node");
4555 }
4556
4557 return true;
4558}
4559
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004560/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4561/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004562static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004563 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004564 if (Depth == 6)
4565 return SDValue(); // Limit search depth.
4566
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004567 SDValue V = SDValue(N, 0);
4568 EVT VT = V.getValueType();
4569 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004570
4571 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4572 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004573 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004574
Craig Topper3d092db2012-03-21 02:14:01 +00004575 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004576 return DAG.getUNDEF(VT.getVectorElementType());
4577
Craig Topperd156dc12012-02-06 07:17:51 +00004578 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004579 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4580 : SV->getOperand(1);
4581 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004582 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004583
4584 // Recurse into target specific vector shuffles to find scalars.
4585 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004586 MVT ShufVT = V.getValueType().getSimpleVT();
4587 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004588 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004589 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004590 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004591
Craig Topperd978c542012-05-06 19:46:21 +00004592 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004593 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004594
Craig Topper3d092db2012-03-21 02:14:01 +00004595 int Elt = ShuffleMask[Index];
4596 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004597 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004598
Craig Topper3d092db2012-03-21 02:14:01 +00004599 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004600 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004601 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004602 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004603 }
4604
4605 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004606 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004607 V = V.getOperand(0);
4608 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004609 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004610
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004611 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004612 return SDValue();
4613 }
4614
4615 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4616 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004617 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004618
4619 if (V.getOpcode() == ISD::BUILD_VECTOR)
4620 return V.getOperand(Index);
4621
4622 return SDValue();
4623}
4624
4625/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4626/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004627/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004628static
Craig Topper3d092db2012-03-21 02:14:01 +00004629unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004630 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004631 unsigned i;
4632 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004634 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004635 if (!(Elt.getNode() &&
4636 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4637 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004638 }
4639
4640 return i;
4641}
4642
Craig Topper3d092db2012-03-21 02:14:01 +00004643/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4644/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4646static
Craig Topper3d092db2012-03-21 02:14:01 +00004647bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4648 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4649 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650 bool SeenV1 = false;
4651 bool SeenV2 = false;
4652
Craig Topper3d092db2012-03-21 02:14:01 +00004653 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 int Idx = SVOp->getMaskElt(i);
4655 // Ignore undef indicies
4656 if (Idx < 0)
4657 continue;
4658
Craig Topper3d092db2012-03-21 02:14:01 +00004659 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 SeenV1 = true;
4661 else
4662 SeenV2 = true;
4663
4664 // Only accept consecutive elements from the same vector
4665 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4666 return false;
4667 }
4668
4669 OpNum = SeenV1 ? 0 : 1;
4670 return true;
4671}
4672
4673/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4674/// logical left shift of a vector.
4675static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4676 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4677 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4678 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4679 false /* check zeros from right */, DAG);
4680 unsigned OpSrc;
4681
4682 if (!NumZeros)
4683 return false;
4684
4685 // Considering the elements in the mask that are not consecutive zeros,
4686 // check if they consecutively come from only one of the source vectors.
4687 //
4688 // V1 = {X, A, B, C} 0
4689 // \ \ \ /
4690 // vector_shuffle V1, V2 <1, 2, 3, X>
4691 //
4692 if (!isShuffleMaskConsecutive(SVOp,
4693 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004694 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004695 NumZeros, // Where to start looking in the src vector
4696 NumElems, // Number of elements in vector
4697 OpSrc)) // Which source operand ?
4698 return false;
4699
4700 isLeft = false;
4701 ShAmt = NumZeros;
4702 ShVal = SVOp->getOperand(OpSrc);
4703 return true;
4704}
4705
4706/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4707/// logical left shift of a vector.
4708static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4709 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4710 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4711 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4712 true /* check zeros from left */, DAG);
4713 unsigned OpSrc;
4714
4715 if (!NumZeros)
4716 return false;
4717
4718 // Considering the elements in the mask that are not consecutive zeros,
4719 // check if they consecutively come from only one of the source vectors.
4720 //
4721 // 0 { A, B, X, X } = V2
4722 // / \ / /
4723 // vector_shuffle V1, V2 <X, X, 4, 5>
4724 //
4725 if (!isShuffleMaskConsecutive(SVOp,
4726 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004727 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004728 0, // Where to start looking in the src vector
4729 NumElems, // Number of elements in vector
4730 OpSrc)) // Which source operand ?
4731 return false;
4732
4733 isLeft = true;
4734 ShAmt = NumZeros;
4735 ShVal = SVOp->getOperand(OpSrc);
4736 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004737}
4738
4739/// isVectorShift - Returns true if the shuffle can be implemented as a
4740/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004741static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004742 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004743 // Although the logic below support any bitwidth size, there are no
4744 // shift instructions which handle more than 128-bit vectors.
4745 if (SVOp->getValueType(0).getSizeInBits() > 128)
4746 return false;
4747
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004748 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4749 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4750 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004751
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004752 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004753}
4754
Evan Chengc78d3b42006-04-24 18:01:45 +00004755/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4756///
Dan Gohman475871a2008-07-27 21:46:04 +00004757static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004758 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004759 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004760 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004761 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004762 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004763 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004764
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004765 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004766 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004767 bool First = true;
4768 for (unsigned i = 0; i < 16; ++i) {
4769 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4770 if (ThisIsNonZero && First) {
4771 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004772 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004773 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004775 First = false;
4776 }
4777
4778 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004779 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004780 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4781 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004782 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004783 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004784 }
4785 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004786 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4787 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4788 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004789 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004791 } else
4792 ThisElt = LastElt;
4793
Gabor Greifba36cb52008-08-28 21:40:38 +00004794 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004795 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004796 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004797 }
4798 }
4799
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004800 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004801}
4802
Bill Wendlinga348c562007-03-22 18:42:45 +00004803/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004804///
Dan Gohman475871a2008-07-27 21:46:04 +00004805static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004806 unsigned NumNonZero, unsigned NumZero,
4807 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004808 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004809 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004810 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004812
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004813 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004814 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 bool First = true;
4816 for (unsigned i = 0; i < 8; ++i) {
4817 bool isNonZero = (NonZeros & (1 << i)) != 0;
4818 if (isNonZero) {
4819 if (First) {
4820 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004821 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004822 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004824 First = false;
4825 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004826 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004828 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004829 }
4830 }
4831
4832 return V;
4833}
4834
Evan Chengf26ffe92008-05-29 08:22:04 +00004835/// getVShift - Return a vector logical shift node.
4836///
Owen Andersone50ed302009-08-10 22:56:29 +00004837static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004838 unsigned NumBits, SelectionDAG &DAG,
4839 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004840 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004841 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004842 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004843 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4844 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004845 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004846 DAG.getConstant(NumBits,
4847 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004848}
4849
Dan Gohman475871a2008-07-27 21:46:04 +00004850SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004851X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004852 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004853
Evan Chengc3630942009-12-09 21:00:30 +00004854 // Check if the scalar load can be widened into a vector load. And if
4855 // the address is "base + cst" see if the cst can be "absorbed" into
4856 // the shuffle mask.
4857 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4858 SDValue Ptr = LD->getBasePtr();
4859 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4860 return SDValue();
4861 EVT PVT = LD->getValueType(0);
4862 if (PVT != MVT::i32 && PVT != MVT::f32)
4863 return SDValue();
4864
4865 int FI = -1;
4866 int64_t Offset = 0;
4867 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4868 FI = FINode->getIndex();
4869 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004870 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004871 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4872 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4873 Offset = Ptr.getConstantOperandVal(1);
4874 Ptr = Ptr.getOperand(0);
4875 } else {
4876 return SDValue();
4877 }
4878
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004879 // FIXME: 256-bit vector instructions don't require a strict alignment,
4880 // improve this code to support it better.
4881 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004882 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004883 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004884 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004885 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004886 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004887 // Can't change the alignment. FIXME: It's possible to compute
4888 // the exact stack offset and reference FI + adjust offset instead.
4889 // If someone *really* cares about this. That's the way to implement it.
4890 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004891 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004892 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004893 }
4894 }
4895
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004896 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004897 // Ptr + (Offset & ~15).
4898 if (Offset < 0)
4899 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004900 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004901 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004902 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004903 if (StartOffset)
4904 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4905 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4906
4907 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004908 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004909
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004910 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4911 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004912 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004913 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004915 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004916 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004917 Mask.push_back(EltNo);
4918
Craig Toppercc3000632012-01-30 07:50:31 +00004919 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004920 }
4921
4922 return SDValue();
4923}
4924
Michael J. Spencerec38de22010-10-10 22:04:20 +00004925/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4926/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004927/// load which has the same value as a build_vector whose operands are 'elts'.
4928///
4929/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004930///
Nate Begeman1449f292010-03-24 22:19:06 +00004931/// FIXME: we'd also like to handle the case where the last elements are zero
4932/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4933/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004934static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004935 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004936 EVT EltVT = VT.getVectorElementType();
4937 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004938
Nate Begemanfdea31a2010-03-24 20:49:50 +00004939 LoadSDNode *LDBase = NULL;
4940 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004941
Nate Begeman1449f292010-03-24 22:19:06 +00004942 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004943 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004944 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004945 for (unsigned i = 0; i < NumElems; ++i) {
4946 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004947
Nate Begemanfdea31a2010-03-24 20:49:50 +00004948 if (!Elt.getNode() ||
4949 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4950 return SDValue();
4951 if (!LDBase) {
4952 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4953 return SDValue();
4954 LDBase = cast<LoadSDNode>(Elt.getNode());
4955 LastLoadedElt = i;
4956 continue;
4957 }
4958 if (Elt.getOpcode() == ISD::UNDEF)
4959 continue;
4960
4961 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4962 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4963 return SDValue();
4964 LastLoadedElt = i;
4965 }
Nate Begeman1449f292010-03-24 22:19:06 +00004966
4967 // If we have found an entire vector of loads and undefs, then return a large
4968 // load of the entire vector width starting at the base pointer. If we found
4969 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004970 if (LastLoadedElt == NumElems - 1) {
4971 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004972 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004973 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004974 LDBase->isVolatile(), LDBase->isNonTemporal(),
4975 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004976 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004977 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004978 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004979 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004980 }
4981 if (NumElems == 4 && LastLoadedElt == 1 &&
4982 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004983 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4984 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004985 SDValue ResNode =
4986 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4987 LDBase->getPointerInfo(),
4988 LDBase->getAlignment(),
4989 false/*isVolatile*/, true/*ReadMem*/,
4990 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004991 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004992 }
4993 return SDValue();
4994}
4995
Nadav Rotem9d68b062012-04-08 12:54:54 +00004996/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
4997/// to generate a splat value for the following cases:
4998/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00004999/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005000/// a scalar load, or a constant.
5001/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005002/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005003SDValue
5004X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005005 if (!Subtarget->hasAVX())
5006 return SDValue();
5007
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005008 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005009 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005010
Craig Topper5da8a802012-05-04 05:49:51 +00005011 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5012 "Unsupported vector type for broadcast.");
5013
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005014 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005015 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005016
Nadav Rotem9d68b062012-04-08 12:54:54 +00005017 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005018 default:
5019 // Unknown pattern found.
5020 return SDValue();
5021
5022 case ISD::BUILD_VECTOR: {
5023 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005024 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005025 return SDValue();
5026
Nadav Rotem9d68b062012-04-08 12:54:54 +00005027 Ld = Op.getOperand(0);
5028 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5029 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005030
5031 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005032 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005033 // Constants may have multiple users.
5034 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005035 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005036 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005037 }
5038
5039 case ISD::VECTOR_SHUFFLE: {
5040 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5041
5042 // Shuffles must have a splat mask where the first element is
5043 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005044 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005045 return SDValue();
5046
5047 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005048 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005049 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5050
5051 if (!Subtarget->hasAVX2())
5052 return SDValue();
5053
5054 // Use the register form of the broadcast instruction available on AVX2.
5055 if (VT.is256BitVector())
5056 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5057 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5058 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005059
5060 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005061 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005062 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005063
5064 // The scalar_to_vector node and the suspected
5065 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005066 // Constants may have multiple users.
5067 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005068 return SDValue();
5069 break;
5070 }
5071 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005072
Nadav Rotem9d68b062012-04-08 12:54:54 +00005073 bool Is256 = VT.getSizeInBits() == 256;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005074
5075 // Handle the broadcasting a single constant scalar from the constant pool
5076 // into a vector. On Sandybridge it is still better to load a constant vector
5077 // from the constant pool and not to broadcast it from a scalar.
5078 if (ConstSplatVal && Subtarget->hasAVX2()) {
5079 EVT CVT = Ld.getValueType();
5080 assert(!CVT.isVector() && "Must not broadcast a vector type");
5081 unsigned ScalarSize = CVT.getSizeInBits();
5082
Craig Topper5da8a802012-05-04 05:49:51 +00005083 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005084 const Constant *C = 0;
5085 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5086 C = CI->getConstantIntValue();
5087 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5088 C = CF->getConstantFPValue();
5089
5090 assert(C && "Invalid constant type");
5091
Nadav Rotem154819d2012-04-09 07:45:58 +00005092 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005093 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005094 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005095 MachinePointerInfo::getConstantPool(),
5096 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005097
Nadav Rotem9d68b062012-04-08 12:54:54 +00005098 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5099 }
5100 }
5101
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005102 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005103 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5104
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005105 // Handle AVX2 in-register broadcasts.
5106 if (!IsLoad && Subtarget->hasAVX2() &&
5107 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5108 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5109
5110 // The scalar source must be a normal load.
5111 if (!IsLoad)
5112 return SDValue();
5113
Craig Topper5da8a802012-05-04 05:49:51 +00005114 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005115 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005116
Craig Toppera9376332012-01-10 08:23:59 +00005117 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005118 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005119 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005120 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005121 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005122 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005123
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005124 // Unsupported broadcast.
5125 return SDValue();
5126}
5127
Evan Chengc3630942009-12-09 21:00:30 +00005128SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005129X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005130 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005131
David Greenef125a292011-02-08 19:04:41 +00005132 EVT VT = Op.getValueType();
5133 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005134 unsigned NumElems = Op.getNumOperands();
5135
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005136 // Vectors containing all zeros can be matched by pxor and xorps later
5137 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5138 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5139 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005140 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005141 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005143 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005144 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005145
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005146 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005147 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5148 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005149 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005150 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005151 return Op;
5152
Craig Topper07a27622012-01-22 03:07:48 +00005153 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005154 }
5155
Nadav Rotem154819d2012-04-09 07:45:58 +00005156 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005157 if (Broadcast.getNode())
5158 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005159
Owen Andersone50ed302009-08-10 22:56:29 +00005160 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161
Evan Cheng0db9fe62006-04-25 20:13:52 +00005162 unsigned NumZero = 0;
5163 unsigned NumNonZero = 0;
5164 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005165 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005166 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005167 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005168 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005169 if (Elt.getOpcode() == ISD::UNDEF)
5170 continue;
5171 Values.insert(Elt);
5172 if (Elt.getOpcode() != ISD::Constant &&
5173 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005174 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005175 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005176 NumZero++;
5177 else {
5178 NonZeros |= (1 << i);
5179 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005180 }
5181 }
5182
Chris Lattner97a2a562010-08-26 05:24:29 +00005183 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5184 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005185 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005186
Chris Lattner67f453a2008-03-09 05:42:06 +00005187 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005188 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005189 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005190 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005191
Chris Lattner62098042008-03-09 01:05:04 +00005192 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5193 // the value are obviously zero, truncate the value to i32 and do the
5194 // insertion that way. Only do this if the value is non-constant or if the
5195 // value is a constant being inserted into element 0. It is cheaper to do
5196 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005197 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005198 (!IsAllConstants || Idx == 0)) {
5199 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005200 // Handle SSE only.
5201 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5202 EVT VecVT = MVT::v4i32;
5203 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005204
Chris Lattner62098042008-03-09 01:05:04 +00005205 // Truncate the value (which may itself be a constant) to i32, and
5206 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005207 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005208 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005209 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005210
Chris Lattner62098042008-03-09 01:05:04 +00005211 // Now we have our 32-bit value zero extended in the low element of
5212 // a vector. If Idx != 0, swizzle it into place.
5213 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 SmallVector<int, 4> Mask;
5215 Mask.push_back(Idx);
5216 for (unsigned i = 1; i != VecElts; ++i)
5217 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005218 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005219 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005220 }
Craig Topper07a27622012-01-22 03:07:48 +00005221 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005222 }
5223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005224
Chris Lattner19f79692008-03-08 22:59:52 +00005225 // If we have a constant or non-constant insertion into the low element of
5226 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5227 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005228 // depending on what the source datatype is.
5229 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005230 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005231 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005232
5233 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005234 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005235 if (VT.getSizeInBits() == 256) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005236 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005237 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5238 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005239 }
Craig Topperd62c16e2011-12-29 03:20:51 +00005240 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005241 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5242 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005243 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005244 }
5245
5246 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005247 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005248 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper19ec2a92011-12-29 03:34:54 +00005249 if (VT.getSizeInBits() == 256) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005250 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005251 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005252 } else {
5253 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005254 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005255 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005256 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005257 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005258 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005259
5260 // Is it a vector logical left shift?
5261 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005262 X86::isZeroNode(Op.getOperand(0)) &&
5263 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005264 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005265 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005266 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005267 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005268 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005269 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005270
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005271 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005272 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273
Chris Lattner19f79692008-03-08 22:59:52 +00005274 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5275 // is a non-constant being inserted into an element other than the low one,
5276 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5277 // movd/movss) to move this into the low element, then shuffle it into
5278 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005280 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005281
Evan Cheng0db9fe62006-04-25 20:13:52 +00005282 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005283 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005285 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005286 MaskVec.push_back(i == Idx ? 0 : 1);
5287 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005288 }
5289 }
5290
Chris Lattner67f453a2008-03-09 05:42:06 +00005291 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005292 if (Values.size() == 1) {
5293 if (EVTBits == 32) {
5294 // Instead of a shuffle like this:
5295 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5296 // Check if it's possible to issue this instead.
5297 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5298 unsigned Idx = CountTrailingZeros_32(NonZeros);
5299 SDValue Item = Op.getOperand(Idx);
5300 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5301 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5302 }
Dan Gohman475871a2008-07-27 21:46:04 +00005303 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005304 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005305
Dan Gohmana3941172007-07-24 22:55:08 +00005306 // A vector full of immediates; various special cases are already
5307 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005308 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005309 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005310
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005311 // For AVX-length vectors, build the individual 128-bit pieces and use
5312 // shuffles to put them in place.
Craig Topperfa5b70e2012-02-03 06:32:21 +00005313 if (VT.getSizeInBits() == 256) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005314 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005315 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005316 V.push_back(Op.getOperand(i));
5317
5318 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5319
5320 // Build both the lower and upper subvector.
5321 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5322 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5323 NumElems/2);
5324
5325 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005326 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005327 }
5328
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005329 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005330 if (EVTBits == 64) {
5331 if (NumNonZero == 1) {
5332 // One half is zero or undef.
5333 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005334 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005335 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005336 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005337 }
Dan Gohman475871a2008-07-27 21:46:04 +00005338 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005339 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005340
5341 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005342 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005343 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005344 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005345 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346 }
5347
Bill Wendling826f36f2007-03-28 00:57:11 +00005348 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005350 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005351 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005352 }
5353
5354 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005355 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005356 if (NumElems == 4 && NumZero > 0) {
5357 for (unsigned i = 0; i < 4; ++i) {
5358 bool isZero = !(NonZeros & (1 << i));
5359 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005360 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361 else
Dale Johannesenace16102009-02-03 19:33:06 +00005362 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363 }
5364
5365 for (unsigned i = 0; i < 2; ++i) {
5366 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5367 default: break;
5368 case 0:
5369 V[i] = V[i*2]; // Must be a zero vector.
5370 break;
5371 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005372 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373 break;
5374 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005375 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376 break;
5377 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005378 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 break;
5380 }
5381 }
5382
Benjamin Kramer9c683542012-01-30 15:16:21 +00005383 bool Reverse1 = (NonZeros & 0x3) == 2;
5384 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5385 int MaskVec[] = {
5386 Reverse1 ? 1 : 0,
5387 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005388 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5389 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005390 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005391 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005392 }
5393
Nate Begemanfdea31a2010-03-24 20:49:50 +00005394 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5395 // Check for a build vector of consecutive loads.
5396 for (unsigned i = 0; i < NumElems; ++i)
5397 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005398
Nate Begemanfdea31a2010-03-24 20:49:50 +00005399 // Check for elements which are consecutive loads.
5400 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5401 if (LD.getNode())
5402 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005403
5404 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005405 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005406 SDValue Result;
5407 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5408 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5409 else
5410 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005411
Chris Lattner24faf612010-08-28 17:59:08 +00005412 for (unsigned i = 1; i < NumElems; ++i) {
5413 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5414 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005415 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005416 }
5417 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005418 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005419
Chris Lattner6e80e442010-08-28 17:15:43 +00005420 // Otherwise, expand into a number of unpckl*, start by extending each of
5421 // our (non-undef) elements to the full vector width with the element in the
5422 // bottom slot of the vector (which generates no code for SSE).
5423 for (unsigned i = 0; i < NumElems; ++i) {
5424 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5425 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5426 else
5427 V[i] = DAG.getUNDEF(VT);
5428 }
5429
5430 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005431 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5432 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5433 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005434 unsigned EltStride = NumElems >> 1;
5435 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005436 for (unsigned i = 0; i < EltStride; ++i) {
5437 // If V[i+EltStride] is undef and this is the first round of mixing,
5438 // then it is safe to just drop this shuffle: V[i] is already in the
5439 // right place, the one element (since it's the first round) being
5440 // inserted as undef can be dropped. This isn't safe for successive
5441 // rounds because they will permute elements within both vectors.
5442 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5443 EltStride == NumElems/2)
5444 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005445
Chris Lattner6e80e442010-08-28 17:15:43 +00005446 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005447 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005448 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005449 }
5450 return V[0];
5451 }
Dan Gohman475871a2008-07-27 21:46:04 +00005452 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005453}
5454
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005455// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5456// them in a MMX register. This is better than doing a stack convert.
5457static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005458 DebugLoc dl = Op.getDebugLoc();
5459 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005460
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005461 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5462 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5463 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005464 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005465 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5466 InVec = Op.getOperand(1);
5467 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5468 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005469 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005470 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5471 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5472 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005473 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005474 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5475 Mask[0] = 0; Mask[1] = 2;
5476 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5477 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005478 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005479}
5480
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005481// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5482// to create 256-bit vectors from two other 128-bit ones.
5483static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5484 DebugLoc dl = Op.getDebugLoc();
5485 EVT ResVT = Op.getValueType();
5486
5487 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5488
5489 SDValue V1 = Op.getOperand(0);
5490 SDValue V2 = Op.getOperand(1);
5491 unsigned NumElems = ResVT.getVectorNumElements();
5492
Craig Topper4c7972d2012-04-22 18:15:59 +00005493 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005494}
5495
5496SDValue
5497X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005498 EVT ResVT = Op.getValueType();
5499
5500 assert(Op.getNumOperands() == 2);
5501 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5502 "Unsupported CONCAT_VECTORS for value type");
5503
5504 // We support concatenate two MMX registers and place them in a MMX register.
5505 // This is better than doing a stack convert.
5506 if (ResVT.is128BitVector())
5507 return LowerMMXCONCAT_VECTORS(Op, DAG);
5508
5509 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5510 // from two other 128-bit ones.
5511 return LowerAVXCONCAT_VECTORS(Op, DAG);
5512}
5513
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005514// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005515static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005516 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005517 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005518 SDValue V1 = SVOp->getOperand(0);
5519 SDValue V2 = SVOp->getOperand(1);
5520 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005521 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005522 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005523
Nadav Roteme6113782012-04-11 06:40:27 +00005524 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005525 return SDValue();
5526
Craig Topper1842ba02012-04-23 06:38:28 +00005527 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005528 MVT OpTy;
5529
Craig Topper708e44f2012-04-23 07:36:33 +00005530 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005531 default: return SDValue();
5532 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005533 ISDNo = X86ISD::BLENDPW;
5534 OpTy = MVT::v8i16;
5535 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005536 case MVT::v4i32:
5537 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005538 ISDNo = X86ISD::BLENDPS;
5539 OpTy = MVT::v4f32;
5540 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005541 case MVT::v2i64:
5542 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005543 ISDNo = X86ISD::BLENDPD;
5544 OpTy = MVT::v2f64;
5545 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005546 case MVT::v8i32:
5547 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005548 if (!Subtarget->hasAVX())
5549 return SDValue();
5550 ISDNo = X86ISD::BLENDPS;
5551 OpTy = MVT::v8f32;
5552 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005553 case MVT::v4i64:
5554 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005555 if (!Subtarget->hasAVX())
5556 return SDValue();
5557 ISDNo = X86ISD::BLENDPD;
5558 OpTy = MVT::v4f64;
5559 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005560 }
5561 assert(ISDNo && "Invalid Op Number");
5562
5563 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005564
Craig Topper1842ba02012-04-23 06:38:28 +00005565 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005566 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005567 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005568 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005569 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005570 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005571 else
5572 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005573 }
5574
Nadav Roteme6113782012-04-11 06:40:27 +00005575 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5576 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5577 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5578 DAG.getConstant(MaskVals, MVT::i32));
5579 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005580}
5581
Nate Begemanb9a47b82009-02-23 08:49:38 +00005582// v8i16 shuffles - Prefer shuffles in the following order:
5583// 1. [all] pshuflw, pshufhw, optional move
5584// 2. [ssse3] 1 x pshufb
5585// 3. [ssse3] 2 x pshufb + 1 x por
5586// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005587SDValue
5588X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5589 SelectionDAG &DAG) const {
5590 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005591 SDValue V1 = SVOp->getOperand(0);
5592 SDValue V2 = SVOp->getOperand(1);
5593 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005594 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005595
Nate Begemanb9a47b82009-02-23 08:49:38 +00005596 // Determine if more than 1 of the words in each of the low and high quadwords
5597 // of the result come from the same quadword of one of the two inputs. Undef
5598 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005599 unsigned LoQuad[] = { 0, 0, 0, 0 };
5600 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005601 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005602 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005603 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005605 MaskVals.push_back(EltIdx);
5606 if (EltIdx < 0) {
5607 ++Quad[0];
5608 ++Quad[1];
5609 ++Quad[2];
5610 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005611 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005612 }
5613 ++Quad[EltIdx / 4];
5614 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005615 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005616
Nate Begemanb9a47b82009-02-23 08:49:38 +00005617 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005618 unsigned MaxQuad = 1;
5619 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005620 if (LoQuad[i] > MaxQuad) {
5621 BestLoQuad = i;
5622 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005623 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005624 }
5625
Nate Begemanb9a47b82009-02-23 08:49:38 +00005626 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005627 MaxQuad = 1;
5628 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005629 if (HiQuad[i] > MaxQuad) {
5630 BestHiQuad = i;
5631 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005632 }
5633 }
5634
Nate Begemanb9a47b82009-02-23 08:49:38 +00005635 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005636 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637 // single pshufb instruction is necessary. If There are more than 2 input
5638 // quads, disable the next transformation since it does not help SSSE3.
5639 bool V1Used = InputQuads[0] || InputQuads[1];
5640 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005641 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005642 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005643 BestLoQuad = InputQuads[0] ? 0 : 1;
5644 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005645 }
5646 if (InputQuads.count() > 2) {
5647 BestLoQuad = -1;
5648 BestHiQuad = -1;
5649 }
5650 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005651
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5653 // the shuffle mask. If a quad is scored as -1, that means that it contains
5654 // words from all 4 input quadwords.
5655 SDValue NewV;
5656 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005657 int MaskV[] = {
5658 BestLoQuad < 0 ? 0 : BestLoQuad,
5659 BestHiQuad < 0 ? 1 : BestHiQuad
5660 };
Eric Christopherfd179292009-08-27 18:07:15 +00005661 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005662 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5663 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5664 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005665
Nate Begemanb9a47b82009-02-23 08:49:38 +00005666 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5667 // source words for the shuffle, to aid later transformations.
5668 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005669 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005670 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005672 if (idx != (int)i)
5673 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005674 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005675 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 AllWordsInNewV = false;
5677 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005678 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005679
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5681 if (AllWordsInNewV) {
5682 for (int i = 0; i != 8; ++i) {
5683 int idx = MaskVals[i];
5684 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005685 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005686 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 if ((idx != i) && idx < 4)
5688 pshufhw = false;
5689 if ((idx != i) && idx > 3)
5690 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005691 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005692 V1 = NewV;
5693 V2Used = false;
5694 BestLoQuad = 0;
5695 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005696 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005697
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5699 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005700 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005701 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5702 unsigned TargetMask = 0;
5703 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005704 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005705 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5706 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5707 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005708 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005709 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005710 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005711 }
Eric Christopherfd179292009-08-27 18:07:15 +00005712
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 // If we have SSSE3, and all words of the result are from 1 input vector,
5714 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5715 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005716 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005718
Nate Begemanb9a47b82009-02-23 08:49:38 +00005719 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005720 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 // mask, and elements that come from V1 in the V2 mask, so that the two
5722 // results can be OR'd together.
5723 bool TwoInputs = V1Used && V2Used;
5724 for (unsigned i = 0; i != 8; ++i) {
5725 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005726 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5727 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5728 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5729 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005731 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005732 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005733 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005734 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005736 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005737
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 // Calculate the shuffle mask for the second input, shuffle it, and
5739 // OR it with the first shuffled input.
5740 pshufbMask.clear();
5741 for (unsigned i = 0; i != 8; ++i) {
5742 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005743 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5744 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5745 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5746 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005748 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005749 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005750 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005751 MVT::v16i8, &pshufbMask[0], 16));
5752 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005753 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 }
5755
5756 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5757 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005758 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005760 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 for (int i = 0; i != 4; ++i) {
5762 int idx = MaskVals[i];
5763 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005764 InOrder.set(i);
5765 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005766 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 }
5769 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005771 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005772
Craig Topperdd637ae2012-02-19 05:41:45 +00005773 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5774 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005775 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005776 NewV.getOperand(0),
5777 getShufflePSHUFLWImmediate(SVOp), DAG);
5778 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 }
Eric Christopherfd179292009-08-27 18:07:15 +00005780
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5782 // and update MaskVals with the new element order.
5783 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005784 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 for (unsigned i = 4; i != 8; ++i) {
5786 int idx = MaskVals[i];
5787 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005788 InOrder.set(i);
5789 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005790 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 }
5793 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005794 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005795 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005796
Craig Topperdd637ae2012-02-19 05:41:45 +00005797 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5798 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005799 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005800 NewV.getOperand(0),
5801 getShufflePSHUFHWImmediate(SVOp), DAG);
5802 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 }
Eric Christopherfd179292009-08-27 18:07:15 +00005804
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 // In case BestHi & BestLo were both -1, which means each quadword has a word
5806 // from each of the four input quadwords, calculate the InOrder bitvector now
5807 // before falling through to the insert/extract cleanup.
5808 if (BestLoQuad == -1 && BestHiQuad == -1) {
5809 NewV = V1;
5810 for (int i = 0; i != 8; ++i)
5811 if (MaskVals[i] < 0 || MaskVals[i] == i)
5812 InOrder.set(i);
5813 }
Eric Christopherfd179292009-08-27 18:07:15 +00005814
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 // The other elements are put in the right place using pextrw and pinsrw.
5816 for (unsigned i = 0; i != 8; ++i) {
5817 if (InOrder[i])
5818 continue;
5819 int EltIdx = MaskVals[i];
5820 if (EltIdx < 0)
5821 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005822 SDValue ExtOp = (EltIdx < 8) ?
5823 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5824 DAG.getIntPtrConstant(EltIdx)) :
5825 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005827 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005828 DAG.getIntPtrConstant(i));
5829 }
5830 return NewV;
5831}
5832
5833// v16i8 shuffles - Prefer shuffles in the following order:
5834// 1. [ssse3] 1 x pshufb
5835// 2. [ssse3] 2 x pshufb + 1 x por
5836// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5837static
Nate Begeman9008ca62009-04-27 18:41:29 +00005838SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005839 SelectionDAG &DAG,
5840 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 SDValue V1 = SVOp->getOperand(0);
5842 SDValue V2 = SVOp->getOperand(1);
5843 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005844 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005845
Craig Topperb82b5ab2012-05-18 06:42:06 +00005846 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
5847
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005849 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005851
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005853 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005854 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005855
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005857 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 //
5859 // Otherwise, we have elements from both input vectors, and must zero out
5860 // elements that come from V2 in the first mask, and V1 in the second mask
5861 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 for (unsigned i = 0; i != 16; ++i) {
5863 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005864 if (EltIdx < 0 || EltIdx >= 16)
5865 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005866 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005867 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005868 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005869 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 MVT::v16i8, &pshufbMask[0], 16));
Craig Topperb82b5ab2012-05-18 06:42:06 +00005871 if (V2IsUndef)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005873
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 // Calculate the shuffle mask for the second input, shuffle it, and
5875 // OR it with the first shuffled input.
5876 pshufbMask.clear();
5877 for (unsigned i = 0; i != 16; ++i) {
5878 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005879 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005880 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005883 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005884 MVT::v16i8, &pshufbMask[0], 16));
5885 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 }
Eric Christopherfd179292009-08-27 18:07:15 +00005887
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 // No SSSE3 - Calculate in place words and then fix all out of place words
5889 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5890 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005891 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5892 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005893 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 for (int i = 0; i != 8; ++i) {
5895 int Elt0 = MaskVals[i*2];
5896 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005897
Nate Begemanb9a47b82009-02-23 08:49:38 +00005898 // This word of the result is all undef, skip it.
5899 if (Elt0 < 0 && Elt1 < 0)
5900 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005901
Nate Begemanb9a47b82009-02-23 08:49:38 +00005902 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005903 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005905
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5907 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5908 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005909
5910 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5911 // using a single extract together, load it and store it.
5912 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005913 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005914 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005915 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005916 DAG.getIntPtrConstant(i));
5917 continue;
5918 }
5919
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005921 // source byte is not also odd, shift the extracted word left 8 bits
5922 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005923 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 DAG.getIntPtrConstant(Elt1 / 2));
5926 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005927 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005928 DAG.getConstant(8,
5929 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005930 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5932 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 }
5934 // If Elt0 is defined, extract it from the appropriate source. If the
5935 // source byte is not also even, shift the extracted word right 8 bits. If
5936 // Elt1 was also defined, OR the extracted values together before
5937 // inserting them in the result.
5938 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005940 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5941 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005943 DAG.getConstant(8,
5944 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005945 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5947 DAG.getConstant(0x00FF, MVT::i16));
5948 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 : InsElt0;
5950 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 DAG.getIntPtrConstant(i));
5953 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005954 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005955}
5956
Evan Cheng7a831ce2007-12-15 03:00:47 +00005957/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005958/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005959/// done when every pair / quad of shuffle mask elements point to elements in
5960/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005961/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005962static
Nate Begeman9008ca62009-04-27 18:41:29 +00005963SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005964 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00005965 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00005966 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00005967 MVT NewVT;
5968 unsigned Scale;
5969 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00005970 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00005971 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
5972 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
5973 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
5974 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
5975 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
5976 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005977 }
5978
Nate Begeman9008ca62009-04-27 18:41:29 +00005979 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00005980 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005981 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00005982 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005983 int EltIdx = SVOp->getMaskElt(i+j);
5984 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005985 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00005986 if (StartIdx < 0)
5987 StartIdx = (EltIdx / Scale);
5988 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00005989 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005990 }
Craig Topper11ac1f82012-05-04 04:08:44 +00005991 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005992 }
5993
Craig Topper11ac1f82012-05-04 04:08:44 +00005994 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
5995 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00005996 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005997}
5998
Evan Chengd880b972008-05-09 21:53:03 +00005999/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006000///
Owen Andersone50ed302009-08-10 22:56:29 +00006001static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006002 SDValue SrcOp, SelectionDAG &DAG,
6003 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006005 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006006 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006007 LD = dyn_cast<LoadSDNode>(SrcOp);
6008 if (!LD) {
6009 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6010 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006011 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006012 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006013 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006014 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006015 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006016 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006017 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006018 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006019 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6020 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6021 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006022 SrcOp.getOperand(0)
6023 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006024 }
6025 }
6026 }
6027
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006028 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006029 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006030 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006031 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006032}
6033
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006034/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6035/// which could not be matched by any known target speficic shuffle
6036static SDValue
6037LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006038
6039 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6040 if (NewOp.getNode())
6041 return NewOp;
6042
Craig Topper8f35c132012-01-20 09:29:03 +00006043 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006044
Craig Topper8f35c132012-01-20 09:29:03 +00006045 unsigned NumElems = VT.getVectorNumElements();
6046 unsigned NumLaneElems = NumElems / 2;
6047
Craig Topper8f35c132012-01-20 09:29:03 +00006048 DebugLoc dl = SVOp->getDebugLoc();
6049 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006050 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006051 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006052
Craig Topper9a2b6e12012-04-06 07:45:23 +00006053 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006054 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006055 // Build a shuffle mask for the output, discovering on the fly which
6056 // input vectors to use as shuffle operands (recorded in InputUsed).
6057 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006058 // out with UseBuildVector set.
6059 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006060 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006061 unsigned LaneStart = l * NumLaneElems;
6062 for (unsigned i = 0; i != NumLaneElems; ++i) {
6063 // The mask element. This indexes into the input.
6064 int Idx = SVOp->getMaskElt(i+LaneStart);
6065 if (Idx < 0) {
6066 // the mask element does not index into any input vector.
6067 Mask.push_back(-1);
6068 continue;
6069 }
Craig Topper8f35c132012-01-20 09:29:03 +00006070
Craig Topper9a2b6e12012-04-06 07:45:23 +00006071 // The input vector this mask element indexes into.
6072 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006073
Craig Topper9a2b6e12012-04-06 07:45:23 +00006074 // Turn the index into an offset from the start of the input vector.
6075 Idx -= Input * NumLaneElems;
6076
6077 // Find or create a shuffle vector operand to hold this input.
6078 unsigned OpNo;
6079 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6080 if (InputUsed[OpNo] == Input)
6081 // This input vector is already an operand.
6082 break;
6083 if (InputUsed[OpNo] < 0) {
6084 // Create a new operand for this input vector.
6085 InputUsed[OpNo] = Input;
6086 break;
6087 }
6088 }
6089
6090 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006091 // More than two input vectors used! Give up on trying to create a
6092 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6093 UseBuildVector = true;
6094 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006095 }
6096
6097 // Add the mask index for the new shuffle vector.
6098 Mask.push_back(Idx + OpNo * NumLaneElems);
6099 }
6100
Craig Topper8ae97ba2012-05-21 06:40:16 +00006101 if (UseBuildVector) {
6102 SmallVector<SDValue, 16> SVOps;
6103 for (unsigned i = 0; i != NumLaneElems; ++i) {
6104 // The mask element. This indexes into the input.
6105 int Idx = SVOp->getMaskElt(i+LaneStart);
6106 if (Idx < 0) {
6107 SVOps.push_back(DAG.getUNDEF(EltVT));
6108 continue;
6109 }
6110
6111 // The input vector this mask element indexes into.
6112 int Input = Idx / NumElems;
6113
6114 // Turn the index into an offset from the start of the input vector.
6115 Idx -= Input * NumElems;
6116
6117 // Extract the vector element by hand.
6118 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6119 SVOp->getOperand(Input),
6120 DAG.getIntPtrConstant(Idx)));
6121 }
6122
6123 // Construct the output using a BUILD_VECTOR.
6124 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6125 SVOps.size());
6126 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006127 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006128 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006129 } else {
6130 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006131 (InputUsed[0] % 2) * NumLaneElems,
6132 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006133 // If only one input was used, use an undefined vector for the other.
6134 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6135 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006136 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006137 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006138 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006139 }
6140
6141 Mask.clear();
6142 }
Craig Topper8f35c132012-01-20 09:29:03 +00006143
6144 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006145 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006146}
6147
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006148/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6149/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006150static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006151LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006152 SDValue V1 = SVOp->getOperand(0);
6153 SDValue V2 = SVOp->getOperand(1);
6154 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006155 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006156
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006157 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6158
Benjamin Kramer9c683542012-01-30 15:16:21 +00006159 std::pair<int, int> Locs[4];
6160 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006161 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006162
Evan Chengace3c172008-07-22 21:13:36 +00006163 unsigned NumHi = 0;
6164 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006165 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006166 int Idx = PermMask[i];
6167 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006168 Locs[i] = std::make_pair(-1, -1);
6169 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006170 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6171 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006172 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006173 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006174 NumLo++;
6175 } else {
6176 Locs[i] = std::make_pair(1, NumHi);
6177 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006178 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006179 NumHi++;
6180 }
6181 }
6182 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006183
Evan Chengace3c172008-07-22 21:13:36 +00006184 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006185 // If no more than two elements come from either vector. This can be
6186 // implemented with two shuffles. First shuffle gather the elements.
6187 // The second shuffle, which takes the first shuffle as both of its
6188 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006189 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006190
Benjamin Kramer9c683542012-01-30 15:16:21 +00006191 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006192
Benjamin Kramer9c683542012-01-30 15:16:21 +00006193 for (unsigned i = 0; i != 4; ++i)
6194 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006195 unsigned Idx = (i < 2) ? 0 : 4;
6196 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006197 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006198 }
Evan Chengace3c172008-07-22 21:13:36 +00006199
Nate Begeman9008ca62009-04-27 18:41:29 +00006200 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006201 }
6202
6203 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006204 // Otherwise, we must have three elements from one vector, call it X, and
6205 // one element from the other, call it Y. First, use a shufps to build an
6206 // intermediate vector with the one element from Y and the element from X
6207 // that will be in the same half in the final destination (the indexes don't
6208 // matter). Then, use a shufps to build the final vector, taking the half
6209 // containing the element from Y from the intermediate, and the other half
6210 // from X.
6211 if (NumHi == 3) {
6212 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006213 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006214 std::swap(V1, V2);
6215 }
6216
6217 // Find the element from V2.
6218 unsigned HiIndex;
6219 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006220 int Val = PermMask[HiIndex];
6221 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006222 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006223 if (Val >= 4)
6224 break;
6225 }
6226
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 Mask1[0] = PermMask[HiIndex];
6228 Mask1[1] = -1;
6229 Mask1[2] = PermMask[HiIndex^1];
6230 Mask1[3] = -1;
6231 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006232
6233 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006234 Mask1[0] = PermMask[0];
6235 Mask1[1] = PermMask[1];
6236 Mask1[2] = HiIndex & 1 ? 6 : 4;
6237 Mask1[3] = HiIndex & 1 ? 4 : 6;
6238 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006239 }
Craig Topper69947b92012-04-23 06:57:04 +00006240
6241 Mask1[0] = HiIndex & 1 ? 2 : 0;
6242 Mask1[1] = HiIndex & 1 ? 0 : 2;
6243 Mask1[2] = PermMask[2];
6244 Mask1[3] = PermMask[3];
6245 if (Mask1[2] >= 0)
6246 Mask1[2] += 4;
6247 if (Mask1[3] >= 0)
6248 Mask1[3] += 4;
6249 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006250 }
6251
6252 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006253 int LoMask[] = { -1, -1, -1, -1 };
6254 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006255
Benjamin Kramer9c683542012-01-30 15:16:21 +00006256 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006257 unsigned MaskIdx = 0;
6258 unsigned LoIdx = 0;
6259 unsigned HiIdx = 2;
6260 for (unsigned i = 0; i != 4; ++i) {
6261 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006262 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006263 MaskIdx = 1;
6264 LoIdx = 0;
6265 HiIdx = 2;
6266 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006267 int Idx = PermMask[i];
6268 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006269 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006270 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006271 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006272 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006273 LoIdx++;
6274 } else {
6275 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006276 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006277 HiIdx++;
6278 }
6279 }
6280
Nate Begeman9008ca62009-04-27 18:41:29 +00006281 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6282 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006283 int MaskOps[] = { -1, -1, -1, -1 };
6284 for (unsigned i = 0; i != 4; ++i)
6285 if (Locs[i].first != -1)
6286 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006287 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006288}
6289
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006290static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006291 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006292 V = V.getOperand(0);
6293 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6294 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006295 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6296 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6297 // BUILD_VECTOR (load), undef
6298 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006299 if (MayFoldLoad(V))
6300 return true;
6301 return false;
6302}
6303
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006304// FIXME: the version above should always be used. Since there's
6305// a bug where several vector shuffles can't be folded because the
6306// DAG is not updated during lowering and a node claims to have two
6307// uses while it only has one, use this version, and let isel match
6308// another instruction if the load really happens to have more than
6309// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006310// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006311static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006312 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006313 V = V.getOperand(0);
6314 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6315 V = V.getOperand(0);
6316 if (ISD::isNormalLoad(V.getNode()))
6317 return true;
6318 return false;
6319}
6320
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006321static
Evan Cheng835580f2010-10-07 20:50:20 +00006322SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6323 EVT VT = Op.getValueType();
6324
6325 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006326 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6327 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006328 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6329 V1, DAG));
6330}
6331
6332static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006333SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006334 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006335 SDValue V1 = Op.getOperand(0);
6336 SDValue V2 = Op.getOperand(1);
6337 EVT VT = Op.getValueType();
6338
6339 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6340
Craig Topper1accb7e2012-01-10 06:54:16 +00006341 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006342 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6343
Evan Cheng0899f5c2011-08-31 02:05:24 +00006344 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6345 return DAG.getNode(ISD::BITCAST, dl, VT,
6346 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6347 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6348 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006349}
6350
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006351static
6352SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6353 SDValue V1 = Op.getOperand(0);
6354 SDValue V2 = Op.getOperand(1);
6355 EVT VT = Op.getValueType();
6356
6357 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6358 "unsupported shuffle type");
6359
6360 if (V2.getOpcode() == ISD::UNDEF)
6361 V2 = V1;
6362
6363 // v4i32 or v4f32
6364 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6365}
6366
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006367static
Craig Topper1accb7e2012-01-10 06:54:16 +00006368SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006369 SDValue V1 = Op.getOperand(0);
6370 SDValue V2 = Op.getOperand(1);
6371 EVT VT = Op.getValueType();
6372 unsigned NumElems = VT.getVectorNumElements();
6373
6374 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6375 // operand of these instructions is only memory, so check if there's a
6376 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6377 // same masks.
6378 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006379
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006380 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006381 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006382 CanFoldLoad = true;
6383
6384 // When V1 is a load, it can be folded later into a store in isel, example:
6385 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6386 // turns into:
6387 // (MOVLPSmr addr:$src1, VR128:$src2)
6388 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006389 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006390 CanFoldLoad = true;
6391
Dan Gohman65fd6562011-11-03 21:49:52 +00006392 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006393 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006394 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006395 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6396
6397 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006398 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006399 if (SVOp->getMaskElt(1) != -1)
6400 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006401 }
6402
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006403 // movl and movlp will both match v2i64, but v2i64 is never matched by
6404 // movl earlier because we make it strict to avoid messing with the movlp load
6405 // folding logic (see the code above getMOVLP call). Match it here then,
6406 // this is horrible, but will stay like this until we move all shuffle
6407 // matching to x86 specific nodes. Note that for the 1st condition all
6408 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006409 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006410 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6411 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006412 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006413 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006414 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006415 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006416
6417 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6418
6419 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006420 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006421 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006422}
6423
Nadav Rotem154819d2012-04-09 07:45:58 +00006424SDValue
6425X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006426 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6427 EVT VT = Op.getValueType();
6428 DebugLoc dl = Op.getDebugLoc();
6429 SDValue V1 = Op.getOperand(0);
6430 SDValue V2 = Op.getOperand(1);
6431
6432 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006433 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006434
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006435 // Handle splat operations
6436 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006437 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006438 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006439
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006440 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006441 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006442 if (Broadcast.getNode())
6443 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006444
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006445 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006446 if ((Size == 128 && NumElem <= 4) ||
6447 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006448 return SDValue();
6449
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006450 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006451 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006452 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006453
6454 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6455 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006456 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6457 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006458 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6459 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006460 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006461 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006462 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006463 // FIXME: Figure out a cleaner way to do this.
6464 // Try to make use of movq to zero out the top part.
6465 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6466 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6467 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006468 EVT NewVT = NewOp.getValueType();
6469 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6470 NewVT, true, false))
6471 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006472 DAG, Subtarget, dl);
6473 }
6474 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6475 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006476 if (NewOp.getNode()) {
6477 EVT NewVT = NewOp.getValueType();
6478 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6479 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6480 DAG, Subtarget, dl);
6481 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006482 }
6483 }
6484 return SDValue();
6485}
6486
Dan Gohman475871a2008-07-27 21:46:04 +00006487SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006488X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006489 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006490 SDValue V1 = Op.getOperand(0);
6491 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006492 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006493 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006494 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006495 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006496 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006497 bool V1IsSplat = false;
6498 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006499 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006500 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006501 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006502 MachineFunction &MF = DAG.getMachineFunction();
6503 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006504
Craig Topper3426a3e2011-11-14 06:46:21 +00006505 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006506
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006507 if (V1IsUndef && V2IsUndef)
6508 return DAG.getUNDEF(VT);
6509
6510 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006511
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006512 // Vector shuffle lowering takes 3 steps:
6513 //
6514 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6515 // narrowing and commutation of operands should be handled.
6516 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6517 // shuffle nodes.
6518 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6519 // so the shuffle can be broken into other shuffles and the legalizer can
6520 // try the lowering again.
6521 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006522 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006523 // be matched during isel, all of them must be converted to a target specific
6524 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006525
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006526 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6527 // narrowing and commutation of operands should be handled. The actual code
6528 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006529 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006530 if (NewOp.getNode())
6531 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006532
Craig Topper5aaffa82012-02-19 02:53:47 +00006533 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6534
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006535 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6536 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006537 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006538 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006539 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006540 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006541
Craig Topperdd637ae2012-02-19 05:41:45 +00006542 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006543 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006544 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006545
Craig Topperdd637ae2012-02-19 05:41:45 +00006546 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006547 return getMOVHighToLow(Op, dl, DAG);
6548
6549 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006550 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006551 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006552 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006553
Craig Topper5aaffa82012-02-19 02:53:47 +00006554 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006555 // The actual implementation will match the mask in the if above and then
6556 // during isel it can match several different instructions, not only pshufd
6557 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006558 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6559 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006560
Craig Topper5aaffa82012-02-19 02:53:47 +00006561 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006562
Craig Topperdbd98a42012-02-07 06:28:42 +00006563 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6564 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6565
Craig Topper1accb7e2012-01-10 06:54:16 +00006566 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006567 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6568
Craig Topperb3982da2011-12-31 23:50:21 +00006569 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006570 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006571 }
Eric Christopherfd179292009-08-27 18:07:15 +00006572
Evan Chengf26ffe92008-05-29 08:22:04 +00006573 // Check if this can be converted into a logical shift.
6574 bool isLeft = false;
6575 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006576 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006577 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006578 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006579 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006580 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006581 EVT EltVT = VT.getVectorElementType();
6582 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006583 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006584 }
Eric Christopherfd179292009-08-27 18:07:15 +00006585
Craig Topper5aaffa82012-02-19 02:53:47 +00006586 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006587 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006588 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006589 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006590 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006591 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6592
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006593 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006594 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6595 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006596 }
Eric Christopherfd179292009-08-27 18:07:15 +00006597
Nate Begeman9008ca62009-04-27 18:41:29 +00006598 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006599 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006600 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006601
Craig Topperdd637ae2012-02-19 05:41:45 +00006602 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006603 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006604
Craig Topperdd637ae2012-02-19 05:41:45 +00006605 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006606 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006607
Craig Topperdd637ae2012-02-19 05:41:45 +00006608 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006609 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006610
Craig Topperdd637ae2012-02-19 05:41:45 +00006611 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006612 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006613
Craig Topperdd637ae2012-02-19 05:41:45 +00006614 if (ShouldXformToMOVHLPS(M, VT) ||
6615 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006616 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006617
Evan Chengf26ffe92008-05-29 08:22:04 +00006618 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006619 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006620 EVT EltVT = VT.getVectorElementType();
6621 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006622 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006623 }
Eric Christopherfd179292009-08-27 18:07:15 +00006624
Evan Cheng9eca5e82006-10-25 21:49:50 +00006625 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006626 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6627 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006628 V1IsSplat = isSplatVector(V1.getNode());
6629 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006630
Chris Lattner8a594482007-11-25 00:24:49 +00006631 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006632 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6633 CommuteVectorShuffleMask(M, NumElems);
6634 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006635 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006636 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006637 }
6638
Craig Topperbeabc6c2011-12-05 06:56:46 +00006639 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006640 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006641 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006642 return V1;
6643 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6644 // the instruction selector will not match, so get a canonical MOVL with
6645 // swapped operands to undo the commute.
6646 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006647 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648
Craig Topperbeabc6c2011-12-05 06:56:46 +00006649 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006650 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006651
Craig Topperbeabc6c2011-12-05 06:56:46 +00006652 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006653 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006654
Evan Cheng9bbbb982006-10-25 20:48:19 +00006655 if (V2IsSplat) {
6656 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006657 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006658 // new vector_shuffle with the corrected mask.p
6659 SmallVector<int, 8> NewMask(M.begin(), M.end());
6660 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006661 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006662 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006663 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006664 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665 }
6666
Evan Cheng9eca5e82006-10-25 21:49:50 +00006667 if (Commuted) {
6668 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006669 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006670 CommuteVectorShuffleMask(M, NumElems);
6671 std::swap(V1, V2);
6672 std::swap(V1IsSplat, V2IsSplat);
6673 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006674
Craig Topper39a9e482012-02-11 06:24:48 +00006675 if (isUNPCKLMask(M, VT, HasAVX2))
6676 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006677
Craig Topper39a9e482012-02-11 06:24:48 +00006678 if (isUNPCKHMask(M, VT, HasAVX2))
6679 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006680 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006681
Nate Begeman9008ca62009-04-27 18:41:29 +00006682 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006683 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006684 return CommuteVectorShuffle(SVOp, DAG);
6685
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006686 // The checks below are all present in isShuffleMaskLegal, but they are
6687 // inlined here right now to enable us to directly emit target specific
6688 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006689
Craig Topper0e2037b2012-01-20 05:53:00 +00006690 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006691 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006692 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006693 DAG);
6694
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006695 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6696 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006697 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006698 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006699 }
6700
Craig Toppera9a568a2012-05-02 08:03:44 +00006701 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006702 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006703 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006704 DAG);
6705
Craig Toppera9a568a2012-05-02 08:03:44 +00006706 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006707 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006708 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006709 DAG);
6710
Craig Topper1a7700a2012-01-19 08:19:12 +00006711 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006712 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006713 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006714
Craig Topper94438ba2011-12-16 08:06:31 +00006715 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006716 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006717 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006718 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006719
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006720 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006721 // Generate target specific nodes for 128 or 256-bit shuffles only
6722 // supported in the AVX instruction set.
6723 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006724
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006725 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006726 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006727 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6728
Craig Topper70b883b2011-11-28 10:14:51 +00006729 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006730 if (isVPERMILPMask(M, VT, HasAVX)) {
6731 if (HasAVX2 && VT == MVT::v8i32)
6732 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006733 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006734 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006735 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006736 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006737
Craig Topper70b883b2011-11-28 10:14:51 +00006738 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006739 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006740 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006741 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006742
Craig Topper1842ba02012-04-23 06:38:28 +00006743 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006744 if (BlendOp.getNode())
6745 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006746
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006747 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006748 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006749 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006750 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006751 }
Craig Topper92040742012-04-16 06:43:40 +00006752 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6753 &permclMask[0], 8);
6754 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006755 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006756 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006757 }
Craig Topper095c5282012-04-15 23:48:57 +00006758
Craig Topper8325c112012-04-16 00:41:45 +00006759 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6760 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006761 getShuffleCLImmediate(SVOp), DAG);
6762
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006763
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006764 //===--------------------------------------------------------------------===//
6765 // Since no target specific shuffle was selected for this generic one,
6766 // lower it into other known shuffles. FIXME: this isn't true yet, but
6767 // this is the plan.
6768 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006769
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006770 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6771 if (VT == MVT::v8i16) {
6772 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6773 if (NewOp.getNode())
6774 return NewOp;
6775 }
6776
6777 if (VT == MVT::v16i8) {
6778 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6779 if (NewOp.getNode())
6780 return NewOp;
6781 }
6782
6783 // Handle all 128-bit wide vectors with 4 elements, and match them with
6784 // several different shuffle types.
6785 if (NumElems == 4 && VT.getSizeInBits() == 128)
6786 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6787
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006788 // Handle general 256-bit shuffles
6789 if (VT.is256BitVector())
6790 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6791
Dan Gohman475871a2008-07-27 21:46:04 +00006792 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006793}
6794
Dan Gohman475871a2008-07-27 21:46:04 +00006795SDValue
6796X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006797 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006798 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006799 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006800
6801 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
6802 return SDValue();
6803
Duncan Sands83ec4b62008-06-06 12:08:01 +00006804 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006805 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006806 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006808 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006809 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006810 }
6811
6812 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006813 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6814 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6815 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006816 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6817 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006818 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006819 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006820 Op.getOperand(0)),
6821 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006822 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006823 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006824 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006825 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006826 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006827 }
6828
6829 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006830 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6831 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006832 // result has a single use which is a store or a bitcast to i32. And in
6833 // the case of a store, it's not worth it if the index is a constant 0,
6834 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006835 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006836 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006837 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006838 if ((User->getOpcode() != ISD::STORE ||
6839 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6840 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006841 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006843 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006845 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006846 Op.getOperand(0)),
6847 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006848 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006849 }
6850
6851 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006852 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006853 if (isa<ConstantSDNode>(Op.getOperand(1)))
6854 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006855 }
Dan Gohman475871a2008-07-27 21:46:04 +00006856 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006857}
6858
6859
Dan Gohman475871a2008-07-27 21:46:04 +00006860SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006861X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6862 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006863 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006864 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006865
David Greene74a579d2011-02-10 16:57:36 +00006866 SDValue Vec = Op.getOperand(0);
6867 EVT VecVT = Vec.getValueType();
6868
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006869 // If this is a 256-bit vector result, first extract the 128-bit vector and
6870 // then extract the element from the 128-bit vector.
6871 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00006872 DebugLoc dl = Op.getNode()->getDebugLoc();
6873 unsigned NumElems = VecVT.getVectorNumElements();
6874 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006875 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6876
6877 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006878 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006879
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006880 if (IdxVal >= NumElems/2)
6881 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006882 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006883 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006884 }
6885
6886 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6887
Craig Topperd0a31172012-01-10 06:37:29 +00006888 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006889 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006890 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006891 return Res;
6892 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006893
Owen Andersone50ed302009-08-10 22:56:29 +00006894 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006895 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006896 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006897 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006898 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006899 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006900 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006901 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6902 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006903 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006905 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006906 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006907 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006908 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006909 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006910 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006912 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006913 }
6914
6915 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006916 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006917 if (Idx == 0)
6918 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006919
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00006921 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006922 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006923 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006924 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006925 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006926 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00006927 }
6928
6929 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006930 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6931 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6932 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006933 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934 if (Idx == 0)
6935 return Op;
6936
6937 // UNPCKHPD the element to the lowest double word, then movsd.
6938 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6939 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006940 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006941 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006942 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006943 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006944 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006945 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006946 }
6947
Dan Gohman475871a2008-07-27 21:46:04 +00006948 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949}
6950
Dan Gohman475871a2008-07-27 21:46:04 +00006951SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006952X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6953 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006954 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006955 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006956 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006957
Dan Gohman475871a2008-07-27 21:46:04 +00006958 SDValue N0 = Op.getOperand(0);
6959 SDValue N1 = Op.getOperand(1);
6960 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006961
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006962 if (VT.getSizeInBits() == 256)
6963 return SDValue();
6964
Dan Gohman8a55ce42009-09-23 21:02:20 +00006965 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006966 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006967 unsigned Opc;
6968 if (VT == MVT::v8i16)
6969 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006970 else if (VT == MVT::v16i8)
6971 Opc = X86ISD::PINSRB;
6972 else
6973 Opc = X86ISD::PINSRB;
6974
Nate Begeman14d12ca2008-02-11 04:19:36 +00006975 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6976 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006977 if (N1.getValueType() != MVT::i32)
6978 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6979 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006980 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006981 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00006982 }
6983
6984 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006985 // Bits [7:6] of the constant are the source select. This will always be
6986 // zero here. The DAG Combiner may combine an extract_elt index into these
6987 // bits. For example (insert (extract, 3), 2) could be matched by putting
6988 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006989 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006990 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006991 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006992 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006993 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006994 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006995 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006996 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00006997 }
6998
6999 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007000 // PINSR* works with constant index.
7001 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007002 }
Dan Gohman475871a2008-07-27 21:46:04 +00007003 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007004}
7005
Dan Gohman475871a2008-07-27 21:46:04 +00007006SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007007X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007008 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007009 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007010
David Greene6b381262011-02-09 15:32:06 +00007011 DebugLoc dl = Op.getDebugLoc();
7012 SDValue N0 = Op.getOperand(0);
7013 SDValue N1 = Op.getOperand(1);
7014 SDValue N2 = Op.getOperand(2);
7015
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007016 // If this is a 256-bit vector result, first extract the 128-bit vector,
7017 // insert the element into the extracted half and then place it back.
7018 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007019 if (!isa<ConstantSDNode>(N2))
7020 return SDValue();
7021
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007022 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007023 unsigned NumElems = VT.getVectorNumElements();
7024 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007025 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007026
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007027 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007028 bool Upper = IdxVal >= NumElems/2;
7029 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7030 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007031
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007032 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007033 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007034 }
7035
Craig Topperd0a31172012-01-10 06:37:29 +00007036 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007037 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7038
Dan Gohman8a55ce42009-09-23 21:02:20 +00007039 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007040 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007041
Dan Gohman8a55ce42009-09-23 21:02:20 +00007042 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007043 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7044 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 if (N1.getValueType() != MVT::i32)
7046 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7047 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007048 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007049 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007050 }
Dan Gohman475871a2008-07-27 21:46:04 +00007051 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007052}
7053
Dan Gohman475871a2008-07-27 21:46:04 +00007054SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007055X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007056 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007057 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007058 EVT OpVT = Op.getValueType();
7059
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007060 // If this is a 256-bit vector result, first insert into a 128-bit
7061 // vector and then insert into the 256-bit vector.
7062 if (OpVT.getSizeInBits() > 128) {
7063 // Insert into a 128-bit vector.
7064 EVT VT128 = EVT::getVectorVT(*Context,
7065 OpVT.getVectorElementType(),
7066 OpVT.getVectorNumElements() / 2);
7067
7068 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7069
7070 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007071 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007072 }
7073
Craig Topperd77d2fe2012-04-29 20:22:05 +00007074 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007075 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007077
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topperd77d2fe2012-04-29 20:22:05 +00007079 assert(OpVT.getSizeInBits() == 128 && "Expected an SSE type!");
7080 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007081 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007082}
7083
David Greene91585092011-01-26 15:38:49 +00007084// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7085// a simple subregister reference or explicit instructions to grab
7086// upper bits of a vector.
7087SDValue
7088X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7089 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007090 DebugLoc dl = Op.getNode()->getDebugLoc();
7091 SDValue Vec = Op.getNode()->getOperand(0);
7092 SDValue Idx = Op.getNode()->getOperand(1);
7093
Craig Topperb14940a2012-04-22 20:55:18 +00007094 if (Op.getNode()->getValueType(0).getSizeInBits() == 128 &&
7095 Vec.getNode()->getValueType(0).getSizeInBits() == 256 &&
7096 isa<ConstantSDNode>(Idx)) {
7097 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7098 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007099 }
David Greene91585092011-01-26 15:38:49 +00007100 }
7101 return SDValue();
7102}
7103
David Greenecfe33c42011-01-26 19:13:22 +00007104// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7105// simple superregister reference or explicit instructions to insert
7106// the upper bits of a vector.
7107SDValue
7108X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7109 if (Subtarget->hasAVX()) {
7110 DebugLoc dl = Op.getNode()->getDebugLoc();
7111 SDValue Vec = Op.getNode()->getOperand(0);
7112 SDValue SubVec = Op.getNode()->getOperand(1);
7113 SDValue Idx = Op.getNode()->getOperand(2);
7114
Craig Topperb14940a2012-04-22 20:55:18 +00007115 if (Op.getNode()->getValueType(0).getSizeInBits() == 256 &&
7116 SubVec.getNode()->getValueType(0).getSizeInBits() == 128 &&
7117 isa<ConstantSDNode>(Idx)) {
7118 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7119 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007120 }
7121 }
7122 return SDValue();
7123}
7124
Bill Wendling056292f2008-09-16 21:48:12 +00007125// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7126// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7127// one of the above mentioned nodes. It has to be wrapped because otherwise
7128// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7129// be used to form addressing mode. These wrapped nodes will be selected
7130// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007131SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007132X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007133 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007134
Chris Lattner41621a22009-06-26 19:22:52 +00007135 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7136 // global base reg.
7137 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007138 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007139 CodeModel::Model M = getTargetMachine().getCodeModel();
7140
Chris Lattner4f066492009-07-11 20:29:19 +00007141 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007142 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007143 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007144 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007145 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007146 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007147 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007148
Evan Cheng1606e8e2009-03-13 07:51:59 +00007149 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007150 CP->getAlignment(),
7151 CP->getOffset(), OpFlag);
7152 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007153 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007154 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007155 if (OpFlag) {
7156 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007157 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007158 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007159 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 }
7161
7162 return Result;
7163}
7164
Dan Gohmand858e902010-04-17 15:26:15 +00007165SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007166 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007167
Chris Lattner18c59872009-06-27 04:16:01 +00007168 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7169 // global base reg.
7170 unsigned char OpFlag = 0;
7171 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007172 CodeModel::Model M = getTargetMachine().getCodeModel();
7173
Chris Lattner4f066492009-07-11 20:29:19 +00007174 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007175 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007176 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007177 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007178 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007179 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007180 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007181
Chris Lattner18c59872009-06-27 04:16:01 +00007182 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7183 OpFlag);
7184 DebugLoc DL = JT->getDebugLoc();
7185 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007186
Chris Lattner18c59872009-06-27 04:16:01 +00007187 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007188 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007189 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7190 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007191 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007192 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007193
Chris Lattner18c59872009-06-27 04:16:01 +00007194 return Result;
7195}
7196
7197SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007198X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007199 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007200
Chris Lattner18c59872009-06-27 04:16:01 +00007201 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7202 // global base reg.
7203 unsigned char OpFlag = 0;
7204 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007205 CodeModel::Model M = getTargetMachine().getCodeModel();
7206
Chris Lattner4f066492009-07-11 20:29:19 +00007207 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007208 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7209 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7210 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007211 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007212 } else if (Subtarget->isPICStyleGOT()) {
7213 OpFlag = X86II::MO_GOT;
7214 } else if (Subtarget->isPICStyleStubPIC()) {
7215 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7216 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7217 OpFlag = X86II::MO_DARWIN_NONLAZY;
7218 }
Eric Christopherfd179292009-08-27 18:07:15 +00007219
Chris Lattner18c59872009-06-27 04:16:01 +00007220 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007221
Chris Lattner18c59872009-06-27 04:16:01 +00007222 DebugLoc DL = Op.getDebugLoc();
7223 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007224
7225
Chris Lattner18c59872009-06-27 04:16:01 +00007226 // With PIC, the address is actually $g + Offset.
7227 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007228 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007229 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7230 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007231 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007232 Result);
7233 }
Eric Christopherfd179292009-08-27 18:07:15 +00007234
Eli Friedman586272d2011-08-11 01:48:05 +00007235 // For symbols that require a load from a stub to get the address, emit the
7236 // load.
7237 if (isGlobalStubReference(OpFlag))
7238 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007239 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007240
Chris Lattner18c59872009-06-27 04:16:01 +00007241 return Result;
7242}
7243
Dan Gohman475871a2008-07-27 21:46:04 +00007244SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007245X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007246 // Create the TargetBlockAddressAddress node.
7247 unsigned char OpFlags =
7248 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007249 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007250 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007251 DebugLoc dl = Op.getDebugLoc();
7252 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7253 /*isTarget=*/true, OpFlags);
7254
Dan Gohmanf705adb2009-10-30 01:28:02 +00007255 if (Subtarget->isPICStyleRIPRel() &&
7256 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007257 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7258 else
7259 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007260
Dan Gohman29cbade2009-11-20 23:18:13 +00007261 // With PIC, the address is actually $g + Offset.
7262 if (isGlobalRelativeToPICBase(OpFlags)) {
7263 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7264 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7265 Result);
7266 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007267
7268 return Result;
7269}
7270
7271SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007272X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007273 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007274 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007275 // Create the TargetGlobalAddress node, folding in the constant
7276 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007277 unsigned char OpFlags =
7278 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007279 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007280 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007281 if (OpFlags == X86II::MO_NO_FLAG &&
7282 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007283 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007284 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007285 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007286 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007287 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007288 }
Eric Christopherfd179292009-08-27 18:07:15 +00007289
Chris Lattner4f066492009-07-11 20:29:19 +00007290 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007291 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007292 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7293 else
7294 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007295
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007296 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007297 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007298 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7299 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007300 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007301 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007302
Chris Lattner36c25012009-07-10 07:34:39 +00007303 // For globals that require a load from a stub to get the address, emit the
7304 // load.
7305 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007306 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007307 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007308
Dan Gohman6520e202008-10-18 02:06:02 +00007309 // If there was a non-zero offset that we didn't fold, create an explicit
7310 // addition for it.
7311 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007312 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007313 DAG.getConstant(Offset, getPointerTy()));
7314
Evan Cheng0db9fe62006-04-25 20:13:52 +00007315 return Result;
7316}
7317
Evan Chengda43bcf2008-09-24 00:05:32 +00007318SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007319X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007320 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007321 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007322 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007323}
7324
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007325static SDValue
7326GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007327 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007328 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007329 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007330 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007331 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007332 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007333 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007334 GA->getOffset(),
7335 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007336
7337 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7338 : X86ISD::TLSADDR;
7339
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007340 if (InFlag) {
7341 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007342 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007343 } else {
7344 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007345 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007346 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007347
7348 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007349 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007350
Rafael Espindola15f1b662009-04-24 12:59:40 +00007351 SDValue Flag = Chain.getValue(1);
7352 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007353}
7354
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007355// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007356static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007357LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007358 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007359 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007360 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7361 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007362 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007363 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007364 InFlag = Chain.getValue(1);
7365
Chris Lattnerb903bed2009-06-26 21:20:29 +00007366 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007367}
7368
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007369// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007370static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007371LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007372 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007373 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7374 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007375}
7376
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007377static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7378 SelectionDAG &DAG,
7379 const EVT PtrVT,
7380 bool is64Bit) {
7381 DebugLoc dl = GA->getDebugLoc();
7382
7383 // Get the start address of the TLS block for this module.
7384 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7385 .getInfo<X86MachineFunctionInfo>();
7386 MFI->incNumLocalDynamicTLSAccesses();
7387
7388 SDValue Base;
7389 if (is64Bit) {
7390 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7391 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7392 } else {
7393 SDValue InFlag;
7394 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7395 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7396 InFlag = Chain.getValue(1);
7397 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7398 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7399 }
7400
7401 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7402 // of Base.
7403
7404 // Build x@dtpoff.
7405 unsigned char OperandFlags = X86II::MO_DTPOFF;
7406 unsigned WrapperKind = X86ISD::Wrapper;
7407 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7408 GA->getValueType(0),
7409 GA->getOffset(), OperandFlags);
7410 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7411
7412 // Add x@dtpoff with the base.
7413 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7414}
7415
Hans Wennborg228756c2012-05-11 10:11:01 +00007416// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007417static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007418 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007419 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007420 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007421
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007422 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7423 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7424 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007425
Michael J. Spencerec38de22010-10-10 22:04:20 +00007426 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007427 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007428 MachinePointerInfo(Ptr),
7429 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007430
Chris Lattnerb903bed2009-06-26 21:20:29 +00007431 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007432 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7433 // initialexec.
7434 unsigned WrapperKind = X86ISD::Wrapper;
7435 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007436 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007437 } else if (model == TLSModel::InitialExec) {
7438 if (is64Bit) {
7439 OperandFlags = X86II::MO_GOTTPOFF;
7440 WrapperKind = X86ISD::WrapperRIP;
7441 } else {
7442 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7443 }
Chris Lattner18c59872009-06-27 04:16:01 +00007444 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007445 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007446 }
Eric Christopherfd179292009-08-27 18:07:15 +00007447
Hans Wennborg228756c2012-05-11 10:11:01 +00007448 // emit "addl x@ntpoff,%eax" (local exec)
7449 // or "addl x@indntpoff,%eax" (initial exec)
7450 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007451 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007452 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007453 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007454 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007455
Hans Wennborg228756c2012-05-11 10:11:01 +00007456 if (model == TLSModel::InitialExec) {
7457 if (isPIC && !is64Bit) {
7458 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7459 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7460 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007461 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007462
7463 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7464 MachinePointerInfo::getGOT(), false, false, false,
7465 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007466 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007467
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007468 // The address of the thread local variable is the add of the thread
7469 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007470 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007471}
7472
Dan Gohman475871a2008-07-27 21:46:04 +00007473SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007474X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007475
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007476 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007477 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007478
Eric Christopher30ef0e52010-06-03 04:07:48 +00007479 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007480 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007481
Eric Christopher30ef0e52010-06-03 04:07:48 +00007482 switch (model) {
7483 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007484 if (Subtarget->is64Bit())
7485 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7486 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007487 case TLSModel::LocalDynamic:
7488 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7489 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007490 case TLSModel::InitialExec:
7491 case TLSModel::LocalExec:
7492 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007493 Subtarget->is64Bit(),
7494 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007495 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007496 llvm_unreachable("Unknown TLS model.");
7497 }
7498
7499 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007500 // Darwin only has one model of TLS. Lower to that.
7501 unsigned char OpFlag = 0;
7502 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7503 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007504
Eric Christopher30ef0e52010-06-03 04:07:48 +00007505 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7506 // global base reg.
7507 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7508 !Subtarget->is64Bit();
7509 if (PIC32)
7510 OpFlag = X86II::MO_TLVP_PIC_BASE;
7511 else
7512 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007513 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007514 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007515 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007516 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007517 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007518
Eric Christopher30ef0e52010-06-03 04:07:48 +00007519 // With PIC32, the address is actually $g + Offset.
7520 if (PIC32)
7521 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7522 DAG.getNode(X86ISD::GlobalBaseReg,
7523 DebugLoc(), getPointerTy()),
7524 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007525
Eric Christopher30ef0e52010-06-03 04:07:48 +00007526 // Lowering the machine isd will make sure everything is in the right
7527 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007528 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007529 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007530 SDValue Args[] = { Chain, Offset };
7531 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007532
Eric Christopher30ef0e52010-06-03 04:07:48 +00007533 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7534 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7535 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007536
Eric Christopher30ef0e52010-06-03 04:07:48 +00007537 // And our return value (tls address) is in the standard call return value
7538 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007539 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007540 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7541 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007542 }
7543
7544 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007545 // Just use the implicit TLS architecture
7546 // Need to generate someting similar to:
7547 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7548 // ; from TEB
7549 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7550 // mov rcx, qword [rdx+rcx*8]
7551 // mov eax, .tls$:tlsvar
7552 // [rax+rcx] contains the address
7553 // Windows 64bit: gs:0x58
7554 // Windows 32bit: fs:__tls_array
7555
7556 // If GV is an alias then use the aliasee for determining
7557 // thread-localness.
7558 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7559 GV = GA->resolveAliasedGlobal(false);
7560 DebugLoc dl = GA->getDebugLoc();
7561 SDValue Chain = DAG.getEntryNode();
7562
7563 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7564 // %gs:0x58 (64-bit).
7565 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7566 ? Type::getInt8PtrTy(*DAG.getContext(),
7567 256)
7568 : Type::getInt32PtrTy(*DAG.getContext(),
7569 257));
7570
7571 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7572 Subtarget->is64Bit()
7573 ? DAG.getIntPtrConstant(0x58)
7574 : DAG.getExternalSymbol("_tls_array",
7575 getPointerTy()),
7576 MachinePointerInfo(Ptr),
7577 false, false, false, 0);
7578
7579 // Load the _tls_index variable
7580 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7581 if (Subtarget->is64Bit())
7582 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7583 IDX, MachinePointerInfo(), MVT::i32,
7584 false, false, 0);
7585 else
7586 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7587 false, false, false, 0);
7588
7589 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007590 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007591 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7592
7593 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7594 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7595 false, false, false, 0);
7596
7597 // Get the offset of start of .tls section
7598 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7599 GA->getValueType(0),
7600 GA->getOffset(), X86II::MO_SECREL);
7601 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7602
7603 // The address of the thread local variable is the add of the thread
7604 // pointer with the offset of the variable.
7605 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007606 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007607
David Blaikie4d6ccb52012-01-20 21:51:11 +00007608 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007609}
7610
Evan Cheng0db9fe62006-04-25 20:13:52 +00007611
Chad Rosierb90d2a92012-01-03 23:19:12 +00007612/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7613/// and take a 2 x i32 value to shift plus a shift amount.
7614SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007615 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007616 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007617 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007618 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007619 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007620 SDValue ShOpLo = Op.getOperand(0);
7621 SDValue ShOpHi = Op.getOperand(1);
7622 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007623 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007625 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007626
Dan Gohman475871a2008-07-27 21:46:04 +00007627 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007628 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007629 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7630 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007631 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007632 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7633 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007634 }
Evan Chenge3413162006-01-09 18:33:28 +00007635
Owen Anderson825b72b2009-08-11 20:47:22 +00007636 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7637 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007638 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007639 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007640
Dan Gohman475871a2008-07-27 21:46:04 +00007641 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007643 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7644 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007645
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007646 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007647 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7648 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007649 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007650 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7651 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007652 }
7653
Dan Gohman475871a2008-07-27 21:46:04 +00007654 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007655 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007656}
Evan Chenga3195e82006-01-12 22:54:21 +00007657
Dan Gohmand858e902010-04-17 15:26:15 +00007658SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7659 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007660 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007661
Dale Johannesen0488fb62010-09-30 23:57:10 +00007662 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007663 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007664
Owen Anderson825b72b2009-08-11 20:47:22 +00007665 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007666 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007667
Eli Friedman36df4992009-05-27 00:47:34 +00007668 // These are really Legal; return the operand so the caller accepts it as
7669 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007670 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007671 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007672 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007673 Subtarget->is64Bit()) {
7674 return Op;
7675 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007676
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007677 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007678 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007679 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007680 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007681 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007682 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007683 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007684 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007685 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007686 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7687}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007688
Owen Andersone50ed302009-08-10 22:56:29 +00007689SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007690 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007691 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007692 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007693 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007694 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007695 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007696 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007697 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007698 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007700
Chris Lattner492a43e2010-09-22 01:28:21 +00007701 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007702
Stuart Hastings84be9582011-06-02 15:57:11 +00007703 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7704 MachineMemOperand *MMO;
7705 if (FI) {
7706 int SSFI = FI->getIndex();
7707 MMO =
7708 DAG.getMachineFunction()
7709 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7710 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7711 } else {
7712 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7713 StackSlot = StackSlot.getOperand(1);
7714 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007715 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007716 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7717 X86ISD::FILD, DL,
7718 Tys, Ops, array_lengthof(Ops),
7719 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007720
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007721 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007722 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007723 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007724
7725 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7726 // shouldn't be necessary except that RFP cannot be live across
7727 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007728 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007729 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7730 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007731 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007733 SDValue Ops[] = {
7734 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7735 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007736 MachineMemOperand *MMO =
7737 DAG.getMachineFunction()
7738 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007739 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007740
Chris Lattner492a43e2010-09-22 01:28:21 +00007741 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7742 Ops, array_lengthof(Ops),
7743 Op.getValueType(), MMO);
7744 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007745 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007746 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007747 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007748
Evan Cheng0db9fe62006-04-25 20:13:52 +00007749 return Result;
7750}
7751
Bill Wendling8b8a6362009-01-17 03:56:04 +00007752// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007753SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7754 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007755 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007756 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007757 movq %rax, %xmm0
7758 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7759 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7760 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007761 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007762 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007763 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007764 addpd %xmm1, %xmm0
7765 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007766 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007767
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007768 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007769 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007770
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007771 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007772 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7773 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007774 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007775
Chris Lattner97484792012-01-25 09:56:22 +00007776 SmallVector<Constant*,2> CV1;
7777 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007778 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007779 CV1.push_back(
7780 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7781 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007782 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007783
Bill Wendling397ae212012-01-05 02:13:20 +00007784 // Load the 64-bit value into an XMM register.
7785 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7786 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007787 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007788 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007789 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007790 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7791 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7792 CLod0);
7793
Owen Anderson825b72b2009-08-11 20:47:22 +00007794 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007795 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007796 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007797 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007798 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007799 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007800
Craig Topperd0a31172012-01-10 06:37:29 +00007801 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007802 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7803 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7804 } else {
7805 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7806 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7807 S2F, 0x4E, DAG);
7808 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7809 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7810 Sub);
7811 }
7812
7813 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007814 DAG.getIntPtrConstant(0));
7815}
7816
Bill Wendling8b8a6362009-01-17 03:56:04 +00007817// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007818SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7819 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007820 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007821 // FP constant to bias correct the final result.
7822 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007823 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007824
7825 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007826 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007827 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007828
Eli Friedmanf3704762011-08-29 21:15:46 +00007829 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007830 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007831
Owen Anderson825b72b2009-08-11 20:47:22 +00007832 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007833 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007834 DAG.getIntPtrConstant(0));
7835
7836 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007837 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007838 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007839 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007841 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007842 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007843 MVT::v2f64, Bias)));
7844 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007845 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007846 DAG.getIntPtrConstant(0));
7847
7848 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007849 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007850
7851 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007852 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007853
Craig Topper69947b92012-04-23 06:57:04 +00007854 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007855 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007856 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007857 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007858 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007859
7860 // Handle final rounding.
7861 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007862}
7863
Dan Gohmand858e902010-04-17 15:26:15 +00007864SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7865 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007866 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007867 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007868
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007869 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007870 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7871 // the optimization here.
7872 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007873 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007874
Owen Andersone50ed302009-08-10 22:56:29 +00007875 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007876 EVT DstVT = Op.getValueType();
7877 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007878 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007879 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007880 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007881 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007882 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007883
7884 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007885 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007886 if (SrcVT == MVT::i32) {
7887 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7888 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7889 getPointerTy(), StackSlot, WordOff);
7890 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007891 StackSlot, MachinePointerInfo(),
7892 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007893 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007894 OffsetSlot, MachinePointerInfo(),
7895 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007896 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7897 return Fild;
7898 }
7899
7900 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7901 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007902 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007903 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007904 // For i64 source, we need to add the appropriate power of 2 if the input
7905 // was negative. This is the same as the optimization in
7906 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7907 // we must be careful to do the computation in x87 extended precision, not
7908 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00007909 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
7910 MachineMemOperand *MMO =
7911 DAG.getMachineFunction()
7912 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7913 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007914
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007915 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7916 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007917 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
7918 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007919
7920 APInt FF(32, 0x5F800000ULL);
7921
7922 // Check whether the sign bit is set.
7923 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7924 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7925 ISD::SETLT);
7926
7927 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7928 SDValue FudgePtr = DAG.getConstantPool(
7929 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7930 getPointerTy());
7931
7932 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7933 SDValue Zero = DAG.getIntPtrConstant(0);
7934 SDValue Four = DAG.getIntPtrConstant(4);
7935 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7936 Zero, Four);
7937 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7938
7939 // Load the value out, extending it from f32 to f80.
7940 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007941 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007942 FudgePtr, MachinePointerInfo::getConstantPool(),
7943 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007944 // Extend everything to 80 bits to force it to be done on x87.
7945 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7946 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007947}
7948
Dan Gohman475871a2008-07-27 21:46:04 +00007949std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00007950FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00007951 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007952
Owen Andersone50ed302009-08-10 22:56:29 +00007953 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007954
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007955 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7957 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007958 }
7959
Owen Anderson825b72b2009-08-11 20:47:22 +00007960 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7961 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007962 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007963
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007964 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007965 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007966 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007967 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007968 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007970 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007971 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007972
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007973 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
7974 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00007975 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007976 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007977 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007978 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007979
Evan Cheng0db9fe62006-04-25 20:13:52 +00007980 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007981 if (!IsSigned && isIntegerTypeFTOL(DstTy))
7982 Opc = X86ISD::WIN_FTOL;
7983 else
7984 switch (DstTy.getSimpleVT().SimpleTy) {
7985 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
7986 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7987 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7988 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
7989 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007990
Dan Gohman475871a2008-07-27 21:46:04 +00007991 SDValue Chain = DAG.getEntryNode();
7992 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007993 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00007994 // FIXME This causes a redundant load/store if the SSE-class value is already
7995 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00007996 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007997 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007998 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007999 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008000 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008001 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008002 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008003 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008004 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008005
Chris Lattner492a43e2010-09-22 01:28:21 +00008006 MachineMemOperand *MMO =
8007 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8008 MachineMemOperand::MOLoad, MemSize, MemSize);
8009 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8010 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008011 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008012 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008013 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8014 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008015
Chris Lattner07290932010-09-22 01:05:16 +00008016 MachineMemOperand *MMO =
8017 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8018 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008019
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008020 if (Opc != X86ISD::WIN_FTOL) {
8021 // Build the FP_TO_INT*_IN_MEM
8022 SDValue Ops[] = { Chain, Value, StackSlot };
8023 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8024 Ops, 3, DstTy, MMO);
8025 return std::make_pair(FIST, StackSlot);
8026 } else {
8027 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8028 DAG.getVTList(MVT::Other, MVT::Glue),
8029 Chain, Value);
8030 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8031 MVT::i32, ftol.getValue(1));
8032 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8033 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008034 SDValue Ops[] = { eax, edx };
8035 SDValue pair = IsReplace
8036 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8037 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008038 return std::make_pair(pair, SDValue());
8039 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008040}
8041
Dan Gohmand858e902010-04-17 15:26:15 +00008042SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8043 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008044 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008045 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008046
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008047 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8048 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008049 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008050 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8051 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008052
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008053 if (StackSlot.getNode())
8054 // Load the result.
8055 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8056 FIST, StackSlot, MachinePointerInfo(),
8057 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008058
8059 // The node is the result.
8060 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008061}
8062
Dan Gohmand858e902010-04-17 15:26:15 +00008063SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8064 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008065 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8066 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008067 SDValue FIST = Vals.first, StackSlot = Vals.second;
8068 assert(FIST.getNode() && "Unexpected failure");
8069
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008070 if (StackSlot.getNode())
8071 // Load the result.
8072 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8073 FIST, StackSlot, MachinePointerInfo(),
8074 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008075
8076 // The node is the result.
8077 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008078}
8079
Dan Gohmand858e902010-04-17 15:26:15 +00008080SDValue X86TargetLowering::LowerFABS(SDValue Op,
8081 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008082 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008083 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008084 EVT VT = Op.getValueType();
8085 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008086 if (VT.isVector())
8087 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00008088 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 if (EltVT == MVT::f64) {
Chad Rosiera20e1e72012-08-01 18:39:17 +00008090 C = ConstantVector::getSplat(2,
Chris Lattner4ca829e2012-01-25 06:02:56 +00008091 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008092 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00008093 C = ConstantVector::getSplat(4,
8094 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008095 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00008096 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008097 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008098 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008099 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008100 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008101}
8102
Dan Gohmand858e902010-04-17 15:26:15 +00008103SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008104 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008105 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008106 EVT VT = Op.getValueType();
8107 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008108 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8109 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008110 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008111 NumElts = VT.getVectorNumElements();
8112 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008113 Constant *C;
8114 if (EltVT == MVT::f64)
8115 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8116 else
8117 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8118 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008119 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008120 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008121 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008122 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008123 if (VT.isVector()) {
Chad Rosiera860b182011-12-15 01:02:25 +00008124 MVT XORVT = VT.getSizeInBits() == 128 ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008125 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008126 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008127 DAG.getNode(ISD::BITCAST, dl, XORVT,
8128 Op.getOperand(0)),
8129 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008130 }
Craig Topper69947b92012-04-23 06:57:04 +00008131
8132 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008133}
8134
Dan Gohmand858e902010-04-17 15:26:15 +00008135SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008136 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008137 SDValue Op0 = Op.getOperand(0);
8138 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008139 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008140 EVT VT = Op.getValueType();
8141 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008142
8143 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008144 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008145 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008146 SrcVT = VT;
8147 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008148 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008149 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008150 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008151 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008152 }
8153
8154 // At this point the operands and the result should have the same
8155 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008156
Evan Cheng68c47cb2007-01-05 07:55:56 +00008157 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008158 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008159 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008160 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8161 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008162 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008163 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8164 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8165 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8166 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008167 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008168 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008169 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008170 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008171 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008172 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008173 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008174
8175 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008176 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008177 // Op0 is MVT::f32, Op1 is MVT::f64.
8178 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8179 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8180 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008181 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008182 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008183 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008184 }
8185
Evan Cheng73d6cf12007-01-05 21:37:56 +00008186 // Clear first operand sign bit.
8187 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008188 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008189 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8190 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008191 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008192 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8193 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8194 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8195 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008196 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008197 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008198 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008199 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008200 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008201 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008202 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008203
8204 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008205 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008206}
8207
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008208SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8209 SDValue N0 = Op.getOperand(0);
8210 DebugLoc dl = Op.getDebugLoc();
8211 EVT VT = Op.getValueType();
8212
8213 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8214 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8215 DAG.getConstant(1, VT));
8216 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8217}
8218
Dan Gohman076aee32009-03-04 19:44:21 +00008219/// Emit nodes that will be selected as "test Op0,Op0", or something
8220/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008221SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008222 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008223 DebugLoc dl = Op.getDebugLoc();
8224
Dan Gohman31125812009-03-07 01:58:32 +00008225 // CF and OF aren't always set the way we want. Determine which
8226 // of these we need.
8227 bool NeedCF = false;
8228 bool NeedOF = false;
8229 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008230 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008231 case X86::COND_A: case X86::COND_AE:
8232 case X86::COND_B: case X86::COND_BE:
8233 NeedCF = true;
8234 break;
8235 case X86::COND_G: case X86::COND_GE:
8236 case X86::COND_L: case X86::COND_LE:
8237 case X86::COND_O: case X86::COND_NO:
8238 NeedOF = true;
8239 break;
Dan Gohman31125812009-03-07 01:58:32 +00008240 }
8241
Dan Gohman076aee32009-03-04 19:44:21 +00008242 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008243 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8244 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008245 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8246 // Emit a CMP with 0, which is the TEST pattern.
8247 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8248 DAG.getConstant(0, Op.getValueType()));
8249
8250 unsigned Opcode = 0;
8251 unsigned NumOperands = 0;
8252 switch (Op.getNode()->getOpcode()) {
8253 case ISD::ADD:
8254 // Due to an isel shortcoming, be conservative if this add is likely to be
8255 // selected as part of a load-modify-store instruction. When the root node
8256 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8257 // uses of other nodes in the match, such as the ADD in this case. This
8258 // leads to the ADD being left around and reselected, with the result being
8259 // two adds in the output. Alas, even if none our users are stores, that
8260 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8261 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8262 // climbing the DAG back to the root, and it doesn't seem to be worth the
8263 // effort.
8264 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008265 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8266 if (UI->getOpcode() != ISD::CopyToReg &&
8267 UI->getOpcode() != ISD::SETCC &&
8268 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008269 goto default_case;
8270
8271 if (ConstantSDNode *C =
8272 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8273 // An add of one will be selected as an INC.
8274 if (C->getAPIntValue() == 1) {
8275 Opcode = X86ISD::INC;
8276 NumOperands = 1;
8277 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008278 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008279
8280 // An add of negative one (subtract of one) will be selected as a DEC.
8281 if (C->getAPIntValue().isAllOnesValue()) {
8282 Opcode = X86ISD::DEC;
8283 NumOperands = 1;
8284 break;
8285 }
Dan Gohman076aee32009-03-04 19:44:21 +00008286 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008287
8288 // Otherwise use a regular EFLAGS-setting add.
8289 Opcode = X86ISD::ADD;
8290 NumOperands = 2;
8291 break;
8292 case ISD::AND: {
8293 // If the primary and result isn't used, don't bother using X86ISD::AND,
8294 // because a TEST instruction will be better.
8295 bool NonFlagUse = false;
8296 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8297 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8298 SDNode *User = *UI;
8299 unsigned UOpNo = UI.getOperandNo();
8300 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8301 // Look pass truncate.
8302 UOpNo = User->use_begin().getOperandNo();
8303 User = *User->use_begin();
8304 }
8305
8306 if (User->getOpcode() != ISD::BRCOND &&
8307 User->getOpcode() != ISD::SETCC &&
8308 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8309 NonFlagUse = true;
8310 break;
8311 }
Dan Gohman076aee32009-03-04 19:44:21 +00008312 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008313
8314 if (!NonFlagUse)
8315 break;
8316 }
8317 // FALL THROUGH
8318 case ISD::SUB:
8319 case ISD::OR:
8320 case ISD::XOR:
8321 // Due to the ISEL shortcoming noted above, be conservative if this op is
8322 // likely to be selected as part of a load-modify-store instruction.
8323 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8324 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8325 if (UI->getOpcode() == ISD::STORE)
8326 goto default_case;
8327
8328 // Otherwise use a regular EFLAGS-setting instruction.
8329 switch (Op.getNode()->getOpcode()) {
8330 default: llvm_unreachable("unexpected operator!");
Manman Ren87253c22012-06-07 00:42:47 +00008331 case ISD::SUB:
Manman Ren39ad5682012-08-08 00:51:41 +00008332 Opcode = X86ISD::SUB;
Manman Ren87253c22012-06-07 00:42:47 +00008333 break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008334 case ISD::OR: Opcode = X86ISD::OR; break;
8335 case ISD::XOR: Opcode = X86ISD::XOR; break;
8336 case ISD::AND: Opcode = X86ISD::AND; break;
8337 }
8338
8339 NumOperands = 2;
8340 break;
8341 case X86ISD::ADD:
8342 case X86ISD::SUB:
8343 case X86ISD::INC:
8344 case X86ISD::DEC:
8345 case X86ISD::OR:
8346 case X86ISD::XOR:
8347 case X86ISD::AND:
8348 return SDValue(Op.getNode(), 1);
8349 default:
8350 default_case:
8351 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008352 }
8353
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008354 if (Opcode == 0)
8355 // Emit a CMP with 0, which is the TEST pattern.
8356 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8357 DAG.getConstant(0, Op.getValueType()));
8358
Manman Ren87253c22012-06-07 00:42:47 +00008359 if (Opcode == X86ISD::CMP) {
8360 SDValue New = DAG.getNode(Opcode, dl, MVT::i32, Op.getOperand(0),
8361 Op.getOperand(1));
Manman Rene6fc9d42012-06-07 19:27:33 +00008362 // We can't replace usage of SUB with CMP.
8363 // The SUB node will be removed later because there is no use of it.
Manman Ren87253c22012-06-07 00:42:47 +00008364 return SDValue(New.getNode(), 0);
8365 }
8366
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008367 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8368 SmallVector<SDValue, 4> Ops;
8369 for (unsigned i = 0; i != NumOperands; ++i)
8370 Ops.push_back(Op.getOperand(i));
8371
8372 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8373 DAG.ReplaceAllUsesWith(Op, New);
8374 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008375}
8376
8377/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8378/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008379SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008380 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008381 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8382 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008383 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008384
8385 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008386 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8387 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8388 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8389 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8390 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8391 Op0, Op1);
8392 return SDValue(Sub.getNode(), 1);
8393 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008394 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008395}
8396
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008397/// Convert a comparison if required by the subtarget.
8398SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8399 SelectionDAG &DAG) const {
8400 // If the subtarget does not support the FUCOMI instruction, floating-point
8401 // comparisons have to be converted.
8402 if (Subtarget->hasCMov() ||
8403 Cmp.getOpcode() != X86ISD::CMP ||
8404 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8405 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8406 return Cmp;
8407
8408 // The instruction selector will select an FUCOM instruction instead of
8409 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8410 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8411 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8412 DebugLoc dl = Cmp.getDebugLoc();
8413 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8414 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8415 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8416 DAG.getConstant(8, MVT::i8));
8417 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8418 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8419}
8420
Evan Chengd40d03e2010-01-06 19:38:29 +00008421/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8422/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008423SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8424 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008425 SDValue Op0 = And.getOperand(0);
8426 SDValue Op1 = And.getOperand(1);
8427 if (Op0.getOpcode() == ISD::TRUNCATE)
8428 Op0 = Op0.getOperand(0);
8429 if (Op1.getOpcode() == ISD::TRUNCATE)
8430 Op1 = Op1.getOperand(0);
8431
Evan Chengd40d03e2010-01-06 19:38:29 +00008432 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008433 if (Op1.getOpcode() == ISD::SHL)
8434 std::swap(Op0, Op1);
8435 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008436 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8437 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008438 // If we looked past a truncate, check that it's only truncating away
8439 // known zeros.
8440 unsigned BitWidth = Op0.getValueSizeInBits();
8441 unsigned AndBitWidth = And.getValueSizeInBits();
8442 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008443 APInt Zeros, Ones;
8444 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008445 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8446 return SDValue();
8447 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008448 LHS = Op1;
8449 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008450 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008451 } else if (Op1.getOpcode() == ISD::Constant) {
8452 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008453 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008454 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008455
8456 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008457 LHS = AndLHS.getOperand(0);
8458 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008459 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008460
8461 // Use BT if the immediate can't be encoded in a TEST instruction.
8462 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8463 LHS = AndLHS;
8464 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8465 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008466 }
Evan Cheng0488db92007-09-25 01:57:46 +00008467
Evan Chengd40d03e2010-01-06 19:38:29 +00008468 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008469 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008470 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008471 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008472 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008473 // Also promote i16 to i32 for performance / code size reason.
8474 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008475 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008476 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008477
Evan Chengd40d03e2010-01-06 19:38:29 +00008478 // If the operand types disagree, extend the shift amount to match. Since
8479 // BT ignores high bits (like shifts) we can use anyextend.
8480 if (LHS.getValueType() != RHS.getValueType())
8481 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008482
Evan Chengd40d03e2010-01-06 19:38:29 +00008483 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8484 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8485 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8486 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008487 }
8488
Evan Cheng54de3ea2010-01-05 06:52:31 +00008489 return SDValue();
8490}
8491
Dan Gohmand858e902010-04-17 15:26:15 +00008492SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008493
8494 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8495
Evan Cheng54de3ea2010-01-05 06:52:31 +00008496 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8497 SDValue Op0 = Op.getOperand(0);
8498 SDValue Op1 = Op.getOperand(1);
8499 DebugLoc dl = Op.getDebugLoc();
8500 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8501
8502 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008503 // Lower (X & (1 << N)) == 0 to BT(X, N).
8504 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8505 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008506 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008507 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008508 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008509 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8510 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8511 if (NewSetCC.getNode())
8512 return NewSetCC;
8513 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008514
Chris Lattner481eebc2010-12-19 21:23:48 +00008515 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8516 // these.
8517 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008518 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008519 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8520 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008521
Chris Lattner481eebc2010-12-19 21:23:48 +00008522 // If the input is a setcc, then reuse the input setcc or use a new one with
8523 // the inverted condition.
8524 if (Op0.getOpcode() == X86ISD::SETCC) {
8525 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8526 bool Invert = (CC == ISD::SETNE) ^
8527 cast<ConstantSDNode>(Op1)->isNullValue();
8528 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008529
Evan Cheng2c755ba2010-02-27 07:36:59 +00008530 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008531 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8532 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8533 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008534 }
8535
Evan Chenge5b51ac2010-04-17 06:13:15 +00008536 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008537 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008538 if (X86CC == X86::COND_INVALID)
8539 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008540
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008541 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008542 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008544 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008545}
8546
Craig Topper89af15e2011-09-18 08:03:58 +00008547// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008548// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008549static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008550 EVT VT = Op.getValueType();
8551
Duncan Sands28b77e92011-09-06 19:07:46 +00008552 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008553 "Unsupported value type for operation");
8554
Craig Topper66ddd152012-04-27 22:54:43 +00008555 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008556 DebugLoc dl = Op.getDebugLoc();
8557 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008558
8559 // Extract the LHS vectors
8560 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008561 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8562 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008563
8564 // Extract the RHS vectors
8565 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008566 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8567 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008568
8569 // Issue the operation on the smaller types and concatenate the result back
8570 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8571 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8572 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8573 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8574 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8575}
8576
8577
Dan Gohmand858e902010-04-17 15:26:15 +00008578SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008579 SDValue Cond;
8580 SDValue Op0 = Op.getOperand(0);
8581 SDValue Op1 = Op.getOperand(1);
8582 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008583 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008584 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8585 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008586 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008587
8588 if (isFP) {
8589 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008590 EVT EltVT = Op0.getValueType().getVectorElementType();
Duncan Sands5b8a1db2012-02-05 14:20:11 +00008591 assert(EltVT == MVT::f32 || EltVT == MVT::f64); (void)EltVT;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008592
Nate Begeman30a0de92008-07-17 16:51:19 +00008593 bool Swap = false;
8594
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008595 // SSE Condition code mapping:
8596 // 0 - EQ
8597 // 1 - LT
8598 // 2 - LE
8599 // 3 - UNORD
8600 // 4 - NEQ
8601 // 5 - NLT
8602 // 6 - NLE
8603 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008604 switch (SetCCOpcode) {
8605 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008606 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008607 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008608 case ISD::SETOGT:
8609 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008610 case ISD::SETLT:
8611 case ISD::SETOLT: SSECC = 1; break;
8612 case ISD::SETOGE:
8613 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008614 case ISD::SETLE:
8615 case ISD::SETOLE: SSECC = 2; break;
8616 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008617 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008618 case ISD::SETNE: SSECC = 4; break;
8619 case ISD::SETULE: Swap = true;
8620 case ISD::SETUGE: SSECC = 5; break;
8621 case ISD::SETULT: Swap = true;
8622 case ISD::SETUGT: SSECC = 6; break;
8623 case ISD::SETO: SSECC = 7; break;
8624 }
8625 if (Swap)
8626 std::swap(Op0, Op1);
8627
Nate Begemanfb8ead02008-07-25 19:05:58 +00008628 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008629 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008630 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008631 SDValue UNORD, EQ;
Craig Topper1906d322012-01-22 23:36:02 +00008632 UNORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8633 DAG.getConstant(3, MVT::i8));
8634 EQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8635 DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008636 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper69947b92012-04-23 06:57:04 +00008637 }
8638 if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008639 SDValue ORD, NEQ;
Craig Topper1906d322012-01-22 23:36:02 +00008640 ORD = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8641 DAG.getConstant(7, MVT::i8));
8642 NEQ = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8643 DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008644 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008645 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008646 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008647 }
8648 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008649 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8650 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008651 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008652
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008653 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008654 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008655 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008656
Nate Begeman30a0de92008-07-17 16:51:19 +00008657 // We are handling one of the integer comparisons here. Since SSE only has
8658 // GT and EQ comparisons for integer, swapping operands and multiple
8659 // operations may be required for some comparisons.
Craig Topper67609fd2012-01-22 22:42:16 +00008660 unsigned Opc = 0;
Nate Begeman30a0de92008-07-17 16:51:19 +00008661 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008662
Nate Begeman30a0de92008-07-17 16:51:19 +00008663 switch (SetCCOpcode) {
8664 default: break;
8665 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008666 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008667 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008668 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008669 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008670 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008671 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008672 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008673 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008674 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008675 }
8676 if (Swap)
8677 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008678
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008679 // Check that the operation in question is available (most are plain SSE2,
8680 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper67609fd2012-01-22 22:42:16 +00008681 if (Opc == X86ISD::PCMPGT && VT == MVT::v2i64 && !Subtarget->hasSSE42())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008682 return SDValue();
Craig Topper67609fd2012-01-22 22:42:16 +00008683 if (Opc == X86ISD::PCMPEQ && VT == MVT::v2i64 && !Subtarget->hasSSE41())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008684 return SDValue();
8685
Nate Begeman30a0de92008-07-17 16:51:19 +00008686 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8687 // bits of the inputs before performing those operations.
8688 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008689 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008690 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8691 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008692 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008693 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8694 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008695 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8696 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008698
Dale Johannesenace16102009-02-03 19:33:06 +00008699 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008700
8701 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008702 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008703 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008704
Nate Begeman30a0de92008-07-17 16:51:19 +00008705 return Result;
8706}
Evan Cheng0488db92007-09-25 01:57:46 +00008707
Evan Cheng370e5342008-12-03 08:38:43 +00008708// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008709static bool isX86LogicalCmp(SDValue Op) {
8710 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008711 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8712 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008713 return true;
8714 if (Op.getResNo() == 1 &&
8715 (Opc == X86ISD::ADD ||
8716 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008717 Opc == X86ISD::ADC ||
8718 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008719 Opc == X86ISD::SMUL ||
8720 Opc == X86ISD::UMUL ||
8721 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008722 Opc == X86ISD::DEC ||
8723 Opc == X86ISD::OR ||
8724 Opc == X86ISD::XOR ||
8725 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008726 return true;
8727
Chris Lattner9637d5b2010-12-05 07:49:54 +00008728 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8729 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008730
Dan Gohman076aee32009-03-04 19:44:21 +00008731 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008732}
8733
Chris Lattnera2b56002010-12-05 01:23:24 +00008734static bool isZero(SDValue V) {
8735 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8736 return C && C->isNullValue();
8737}
8738
Chris Lattner96908b12010-12-05 02:00:51 +00008739static bool isAllOnes(SDValue V) {
8740 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8741 return C && C->isAllOnesValue();
8742}
8743
Evan Chengb64dd5f2012-08-07 22:21:00 +00008744static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8745 if (V.getOpcode() != ISD::TRUNCATE)
8746 return false;
8747
8748 SDValue VOp0 = V.getOperand(0);
8749 unsigned InBits = VOp0.getValueSizeInBits();
8750 unsigned Bits = V.getValueSizeInBits();
8751 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8752}
8753
Dan Gohmand858e902010-04-17 15:26:15 +00008754SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008755 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008756 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008757 SDValue Op1 = Op.getOperand(1);
8758 SDValue Op2 = Op.getOperand(2);
8759 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008760 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008761
Dan Gohman1a492952009-10-20 16:22:37 +00008762 if (Cond.getOpcode() == ISD::SETCC) {
8763 SDValue NewCond = LowerSETCC(Cond, DAG);
8764 if (NewCond.getNode())
8765 Cond = NewCond;
8766 }
Evan Cheng734503b2006-09-11 02:19:56 +00008767
Chris Lattnera2b56002010-12-05 01:23:24 +00008768 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008769 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008770 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008771 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008772 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008773 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8774 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008775 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008776
Chris Lattnera2b56002010-12-05 01:23:24 +00008777 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008778
8779 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008780 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8781 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008782
8783 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008784 // Apply further optimizations for special cases
8785 // (select (x != 0), -1, 0) -> neg & sbb
8786 // (select (x == 0), 0, -1) -> neg & sbb
8787 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008788 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008789 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8790 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008791 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8792 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008793 CmpOp0);
8794 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8795 DAG.getConstant(X86::COND_B, MVT::i8),
8796 SDValue(Neg.getNode(), 1));
8797 return Res;
8798 }
8799
Chris Lattnera2b56002010-12-05 01:23:24 +00008800 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8801 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008802 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008803
Chris Lattner96908b12010-12-05 02:00:51 +00008804 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008805 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8806 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008807
Chris Lattner96908b12010-12-05 02:00:51 +00008808 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8809 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008810
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008811 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008812 if (N2C == 0 || !N2C->isNullValue())
8813 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8814 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008815 }
8816 }
8817
Chris Lattnera2b56002010-12-05 01:23:24 +00008818 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008819 if (Cond.getOpcode() == ISD::AND &&
8820 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8821 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008822 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008823 Cond = Cond.getOperand(0);
8824 }
8825
Evan Cheng3f41d662007-10-08 22:16:29 +00008826 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8827 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008828 unsigned CondOpcode = Cond.getOpcode();
8829 if (CondOpcode == X86ISD::SETCC ||
8830 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008831 CC = Cond.getOperand(0);
8832
Dan Gohman475871a2008-07-27 21:46:04 +00008833 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008834 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008835 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008836
Evan Cheng3f41d662007-10-08 22:16:29 +00008837 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008838 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008839 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008840 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008841
Chris Lattnerd1980a52009-03-12 06:52:53 +00008842 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8843 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008844 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008845 addTest = false;
8846 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008847 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8848 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8849 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8850 Cond.getOperand(0).getValueType() != MVT::i8)) {
8851 SDValue LHS = Cond.getOperand(0);
8852 SDValue RHS = Cond.getOperand(1);
8853 unsigned X86Opcode;
8854 unsigned X86Cond;
8855 SDVTList VTs;
8856 switch (CondOpcode) {
8857 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8858 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8859 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8860 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8861 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8862 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8863 default: llvm_unreachable("unexpected overflowing operator");
8864 }
8865 if (CondOpcode == ISD::UMULO)
8866 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8867 MVT::i32);
8868 else
8869 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8870
8871 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8872
8873 if (CondOpcode == ISD::UMULO)
8874 Cond = X86Op.getValue(2);
8875 else
8876 Cond = X86Op.getValue(1);
8877
8878 CC = DAG.getConstant(X86Cond, MVT::i8);
8879 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008880 }
8881
8882 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00008883 // Look pass the truncate if the high bits are known zero.
8884 if (isTruncWithZeroHighBitsInput(Cond, DAG))
8885 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00008886
8887 // We know the result of AND is compared against zero. Try to match
8888 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008889 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008890 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008891 if (NewSetCC.getNode()) {
8892 CC = NewSetCC.getOperand(0);
8893 Cond = NewSetCC.getOperand(1);
8894 addTest = false;
8895 }
8896 }
8897 }
8898
8899 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008900 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008901 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008902 }
8903
Benjamin Kramere915ff32010-12-22 23:09:28 +00008904 // a < b ? -1 : 0 -> RES = ~setcc_carry
8905 // a < b ? 0 : -1 -> RES = setcc_carry
8906 // a >= b ? -1 : 0 -> RES = setcc_carry
8907 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00008908 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008909 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00008910 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8911
8912 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8913 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8914 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8915 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8916 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8917 return DAG.getNOT(DL, Res, Res.getValueType());
8918 return Res;
8919 }
8920 }
8921
Evan Cheng0488db92007-09-25 01:57:46 +00008922 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8923 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008924 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008925 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008926 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008927}
8928
Evan Cheng370e5342008-12-03 08:38:43 +00008929// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8930// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8931// from the AND / OR.
8932static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8933 Opc = Op.getOpcode();
8934 if (Opc != ISD::OR && Opc != ISD::AND)
8935 return false;
8936 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8937 Op.getOperand(0).hasOneUse() &&
8938 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8939 Op.getOperand(1).hasOneUse());
8940}
8941
Evan Cheng961d6d42009-02-02 08:19:07 +00008942// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8943// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008944static bool isXor1OfSetCC(SDValue Op) {
8945 if (Op.getOpcode() != ISD::XOR)
8946 return false;
8947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8948 if (N1C && N1C->getAPIntValue() == 1) {
8949 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8950 Op.getOperand(0).hasOneUse();
8951 }
8952 return false;
8953}
8954
Dan Gohmand858e902010-04-17 15:26:15 +00008955SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008956 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008957 SDValue Chain = Op.getOperand(0);
8958 SDValue Cond = Op.getOperand(1);
8959 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008960 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008961 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008962 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008963
Dan Gohman1a492952009-10-20 16:22:37 +00008964 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008965 // Check for setcc([su]{add,sub,mul}o == 0).
8966 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8967 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8968 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8969 Cond.getOperand(0).getResNo() == 1 &&
8970 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
8971 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
8972 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
8973 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
8974 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
8975 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
8976 Inverted = true;
8977 Cond = Cond.getOperand(0);
8978 } else {
8979 SDValue NewCond = LowerSETCC(Cond, DAG);
8980 if (NewCond.getNode())
8981 Cond = NewCond;
8982 }
Dan Gohman1a492952009-10-20 16:22:37 +00008983 }
Chris Lattnere55484e2008-12-25 05:34:37 +00008984#if 0
8985 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00008986 else if (Cond.getOpcode() == X86ISD::ADD ||
8987 Cond.getOpcode() == X86ISD::SUB ||
8988 Cond.getOpcode() == X86ISD::SMUL ||
8989 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00008990 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00008991#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00008992
Evan Chengad9c0a32009-12-15 00:53:42 +00008993 // Look pass (and (setcc_carry (cmp ...)), 1).
8994 if (Cond.getOpcode() == ISD::AND &&
8995 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8996 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008997 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008998 Cond = Cond.getOperand(0);
8999 }
9000
Evan Cheng3f41d662007-10-08 22:16:29 +00009001 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9002 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009003 unsigned CondOpcode = Cond.getOpcode();
9004 if (CondOpcode == X86ISD::SETCC ||
9005 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009006 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009007
Dan Gohman475871a2008-07-27 21:46:04 +00009008 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009009 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009010 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009011 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009012 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009013 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009014 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009015 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009016 default: break;
9017 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009018 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009019 // These can only come from an arithmetic instruction with overflow,
9020 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009021 Cond = Cond.getNode()->getOperand(1);
9022 addTest = false;
9023 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009024 }
Evan Cheng0488db92007-09-25 01:57:46 +00009025 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009026 }
9027 CondOpcode = Cond.getOpcode();
9028 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9029 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9030 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9031 Cond.getOperand(0).getValueType() != MVT::i8)) {
9032 SDValue LHS = Cond.getOperand(0);
9033 SDValue RHS = Cond.getOperand(1);
9034 unsigned X86Opcode;
9035 unsigned X86Cond;
9036 SDVTList VTs;
9037 switch (CondOpcode) {
9038 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9039 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9040 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9041 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9042 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9043 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9044 default: llvm_unreachable("unexpected overflowing operator");
9045 }
9046 if (Inverted)
9047 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9048 if (CondOpcode == ISD::UMULO)
9049 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9050 MVT::i32);
9051 else
9052 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9053
9054 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9055
9056 if (CondOpcode == ISD::UMULO)
9057 Cond = X86Op.getValue(2);
9058 else
9059 Cond = X86Op.getValue(1);
9060
9061 CC = DAG.getConstant(X86Cond, MVT::i8);
9062 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009063 } else {
9064 unsigned CondOpc;
9065 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9066 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009067 if (CondOpc == ISD::OR) {
9068 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9069 // two branches instead of an explicit OR instruction with a
9070 // separate test.
9071 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009072 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009073 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009074 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009075 Chain, Dest, CC, Cmp);
9076 CC = Cond.getOperand(1).getOperand(0);
9077 Cond = Cmp;
9078 addTest = false;
9079 }
9080 } else { // ISD::AND
9081 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9082 // two branches instead of an explicit AND instruction with a
9083 // separate test. However, we only do this if this block doesn't
9084 // have a fall-through edge, because this requires an explicit
9085 // jmp when the condition is false.
9086 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009087 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009088 Op.getNode()->hasOneUse()) {
9089 X86::CondCode CCode =
9090 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9091 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009092 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009093 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009094 // Look for an unconditional branch following this conditional branch.
9095 // We need this because we need to reverse the successors in order
9096 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009097 if (User->getOpcode() == ISD::BR) {
9098 SDValue FalseBB = User->getOperand(1);
9099 SDNode *NewBR =
9100 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009101 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009102 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009103 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009104
Dale Johannesene4d209d2009-02-03 20:21:25 +00009105 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009106 Chain, Dest, CC, Cmp);
9107 X86::CondCode CCode =
9108 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9109 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009110 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009111 Cond = Cmp;
9112 addTest = false;
9113 }
9114 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009115 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009116 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9117 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9118 // It should be transformed during dag combiner except when the condition
9119 // is set by a arithmetics with overflow node.
9120 X86::CondCode CCode =
9121 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9122 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009123 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009124 Cond = Cond.getOperand(0).getOperand(1);
9125 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009126 } else if (Cond.getOpcode() == ISD::SETCC &&
9127 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9128 // For FCMP_OEQ, we can emit
9129 // two branches instead of an explicit AND instruction with a
9130 // separate test. However, we only do this if this block doesn't
9131 // have a fall-through edge, because this requires an explicit
9132 // jmp when the condition is false.
9133 if (Op.getNode()->hasOneUse()) {
9134 SDNode *User = *Op.getNode()->use_begin();
9135 // Look for an unconditional branch following this conditional branch.
9136 // We need this because we need to reverse the successors in order
9137 // to implement FCMP_OEQ.
9138 if (User->getOpcode() == ISD::BR) {
9139 SDValue FalseBB = User->getOperand(1);
9140 SDNode *NewBR =
9141 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9142 assert(NewBR == User);
9143 (void)NewBR;
9144 Dest = FalseBB;
9145
9146 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9147 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009148 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009149 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9150 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9151 Chain, Dest, CC, Cmp);
9152 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9153 Cond = Cmp;
9154 addTest = false;
9155 }
9156 }
9157 } else if (Cond.getOpcode() == ISD::SETCC &&
9158 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9159 // For FCMP_UNE, we can emit
9160 // two branches instead of an explicit AND instruction with a
9161 // separate test. However, we only do this if this block doesn't
9162 // have a fall-through edge, because this requires an explicit
9163 // jmp when the condition is false.
9164 if (Op.getNode()->hasOneUse()) {
9165 SDNode *User = *Op.getNode()->use_begin();
9166 // Look for an unconditional branch following this conditional branch.
9167 // We need this because we need to reverse the successors in order
9168 // to implement FCMP_UNE.
9169 if (User->getOpcode() == ISD::BR) {
9170 SDValue FalseBB = User->getOperand(1);
9171 SDNode *NewBR =
9172 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9173 assert(NewBR == User);
9174 (void)NewBR;
9175
9176 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9177 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009178 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009179 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9180 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9181 Chain, Dest, CC, Cmp);
9182 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9183 Cond = Cmp;
9184 addTest = false;
9185 Dest = FalseBB;
9186 }
9187 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009188 }
Evan Cheng0488db92007-09-25 01:57:46 +00009189 }
9190
9191 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009192 // Look pass the truncate if the high bits are known zero.
9193 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9194 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009195
9196 // We know the result of AND is compared against zero. Try to match
9197 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009198 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009199 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9200 if (NewSetCC.getNode()) {
9201 CC = NewSetCC.getOperand(0);
9202 Cond = NewSetCC.getOperand(1);
9203 addTest = false;
9204 }
9205 }
9206 }
9207
9208 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009209 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009210 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009211 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009212 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009213 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009214 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009215}
9216
Anton Korobeynikove060b532007-04-17 19:34:00 +00009217
9218// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9219// Calls to _alloca is needed to probe the stack when allocating more than 4k
9220// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9221// that the guard pages used by the OS virtual memory manager are allocated in
9222// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009223SDValue
9224X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009225 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009226 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009227 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009228 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009229 "are being used");
9230 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009231 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009232
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009233 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009234 SDValue Chain = Op.getOperand(0);
9235 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009236 // FIXME: Ensure alignment here
9237
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009238 bool Is64Bit = Subtarget->is64Bit();
9239 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009240
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009241 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009242 MachineFunction &MF = DAG.getMachineFunction();
9243 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009244
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009245 if (Is64Bit) {
9246 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009247 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009248 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009249
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009250 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009251 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009252 if (I->hasNestAttr())
9253 report_fatal_error("Cannot use segmented stacks with functions that "
9254 "have nested arguments.");
9255 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009256
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009257 const TargetRegisterClass *AddrRegClass =
9258 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9259 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9260 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9261 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9262 DAG.getRegister(Vreg, SPTy));
9263 SDValue Ops1[2] = { Value, Chain };
9264 return DAG.getMergeValues(Ops1, 2, dl);
9265 } else {
9266 SDValue Flag;
9267 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009268
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009269 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9270 Flag = Chain.getValue(1);
9271 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009272
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009273 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9274 Flag = Chain.getValue(1);
9275
9276 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9277
9278 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9279 return DAG.getMergeValues(Ops1, 2, dl);
9280 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009281}
9282
Dan Gohmand858e902010-04-17 15:26:15 +00009283SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009284 MachineFunction &MF = DAG.getMachineFunction();
9285 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9286
Dan Gohman69de1932008-02-06 22:27:42 +00009287 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009288 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009289
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009290 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009291 // vastart just stores the address of the VarArgsFrameIndex slot into the
9292 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009293 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9294 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009295 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9296 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009297 }
9298
9299 // __va_list_tag:
9300 // gp_offset (0 - 6 * 8)
9301 // fp_offset (48 - 48 + 8 * 16)
9302 // overflow_arg_area (point to parameters coming in memory).
9303 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009304 SmallVector<SDValue, 8> MemOps;
9305 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009306 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009307 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009308 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9309 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009310 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009311 MemOps.push_back(Store);
9312
9313 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009314 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009315 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009316 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009317 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9318 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009319 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009320 MemOps.push_back(Store);
9321
9322 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009323 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009324 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009325 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9326 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009327 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9328 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009329 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009330 MemOps.push_back(Store);
9331
9332 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009333 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009334 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009335 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9336 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009337 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9338 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009339 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009340 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009341 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009342}
9343
Dan Gohmand858e902010-04-17 15:26:15 +00009344SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009345 assert(Subtarget->is64Bit() &&
9346 "LowerVAARG only handles 64-bit va_arg!");
9347 assert((Subtarget->isTargetLinux() ||
9348 Subtarget->isTargetDarwin()) &&
9349 "Unhandled target in LowerVAARG");
9350 assert(Op.getNode()->getNumOperands() == 4);
9351 SDValue Chain = Op.getOperand(0);
9352 SDValue SrcPtr = Op.getOperand(1);
9353 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9354 unsigned Align = Op.getConstantOperandVal(3);
9355 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009356
Dan Gohman320afb82010-10-12 18:00:49 +00009357 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009358 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009359 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9360 uint8_t ArgMode;
9361
9362 // Decide which area this value should be read from.
9363 // TODO: Implement the AMD64 ABI in its entirety. This simple
9364 // selection mechanism works only for the basic types.
9365 if (ArgVT == MVT::f80) {
9366 llvm_unreachable("va_arg for f80 not yet implemented");
9367 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9368 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9369 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9370 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9371 } else {
9372 llvm_unreachable("Unhandled argument type in LowerVAARG");
9373 }
9374
9375 if (ArgMode == 2) {
9376 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009377 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009378 !(DAG.getMachineFunction()
9379 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009380 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009381 }
9382
9383 // Insert VAARG_64 node into the DAG
9384 // VAARG_64 returns two values: Variable Argument Address, Chain
9385 SmallVector<SDValue, 11> InstOps;
9386 InstOps.push_back(Chain);
9387 InstOps.push_back(SrcPtr);
9388 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9389 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9390 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9391 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9392 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9393 VTs, &InstOps[0], InstOps.size(),
9394 MVT::i64,
9395 MachinePointerInfo(SV),
9396 /*Align=*/0,
9397 /*Volatile=*/false,
9398 /*ReadMem=*/true,
9399 /*WriteMem=*/true);
9400 Chain = VAARG.getValue(1);
9401
9402 // Load the next argument and return it
9403 return DAG.getLoad(ArgVT, dl,
9404 Chain,
9405 VAARG,
9406 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009407 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009408}
9409
Dan Gohmand858e902010-04-17 15:26:15 +00009410SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009411 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009412 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009413 SDValue Chain = Op.getOperand(0);
9414 SDValue DstPtr = Op.getOperand(1);
9415 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009416 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9417 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009418 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009419
Chris Lattnere72f2022010-09-21 05:40:29 +00009420 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009421 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009422 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009423 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009424}
9425
Craig Topper80e46362012-01-23 06:16:53 +00009426// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9427// may or may not be a constant. Takes immediate version of shift as input.
9428static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9429 SDValue SrcOp, SDValue ShAmt,
9430 SelectionDAG &DAG) {
9431 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9432
9433 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009434 // Constant may be a TargetConstant. Use a regular constant.
9435 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009436 switch (Opc) {
9437 default: llvm_unreachable("Unknown target vector shift node");
9438 case X86ISD::VSHLI:
9439 case X86ISD::VSRLI:
9440 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009441 return DAG.getNode(Opc, dl, VT, SrcOp,
9442 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009443 }
9444 }
9445
9446 // Change opcode to non-immediate version
9447 switch (Opc) {
9448 default: llvm_unreachable("Unknown target vector shift node");
9449 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9450 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9451 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9452 }
9453
9454 // Need to build a vector containing shift amount
9455 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9456 SDValue ShOps[4];
9457 ShOps[0] = ShAmt;
9458 ShOps[1] = DAG.getConstant(0, MVT::i32);
9459 ShOps[2] = DAG.getUNDEF(MVT::i32);
9460 ShOps[3] = DAG.getUNDEF(MVT::i32);
9461 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009462
9463 // The return type has to be a 128-bit type with the same element
9464 // type as the input type.
9465 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9466 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9467
9468 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009469 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9470}
9471
Dan Gohman475871a2008-07-27 21:46:04 +00009472SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009473X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009474 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009475 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009476 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009477 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009478 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009479 case Intrinsic::x86_sse_comieq_ss:
9480 case Intrinsic::x86_sse_comilt_ss:
9481 case Intrinsic::x86_sse_comile_ss:
9482 case Intrinsic::x86_sse_comigt_ss:
9483 case Intrinsic::x86_sse_comige_ss:
9484 case Intrinsic::x86_sse_comineq_ss:
9485 case Intrinsic::x86_sse_ucomieq_ss:
9486 case Intrinsic::x86_sse_ucomilt_ss:
9487 case Intrinsic::x86_sse_ucomile_ss:
9488 case Intrinsic::x86_sse_ucomigt_ss:
9489 case Intrinsic::x86_sse_ucomige_ss:
9490 case Intrinsic::x86_sse_ucomineq_ss:
9491 case Intrinsic::x86_sse2_comieq_sd:
9492 case Intrinsic::x86_sse2_comilt_sd:
9493 case Intrinsic::x86_sse2_comile_sd:
9494 case Intrinsic::x86_sse2_comigt_sd:
9495 case Intrinsic::x86_sse2_comige_sd:
9496 case Intrinsic::x86_sse2_comineq_sd:
9497 case Intrinsic::x86_sse2_ucomieq_sd:
9498 case Intrinsic::x86_sse2_ucomilt_sd:
9499 case Intrinsic::x86_sse2_ucomile_sd:
9500 case Intrinsic::x86_sse2_ucomigt_sd:
9501 case Intrinsic::x86_sse2_ucomige_sd:
9502 case Intrinsic::x86_sse2_ucomineq_sd: {
9503 unsigned Opc = 0;
9504 ISD::CondCode CC = ISD::SETCC_INVALID;
9505 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009506 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009507 case Intrinsic::x86_sse_comieq_ss:
9508 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009509 Opc = X86ISD::COMI;
9510 CC = ISD::SETEQ;
9511 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009512 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009513 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009514 Opc = X86ISD::COMI;
9515 CC = ISD::SETLT;
9516 break;
9517 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009518 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009519 Opc = X86ISD::COMI;
9520 CC = ISD::SETLE;
9521 break;
9522 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009523 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009524 Opc = X86ISD::COMI;
9525 CC = ISD::SETGT;
9526 break;
9527 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009528 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009529 Opc = X86ISD::COMI;
9530 CC = ISD::SETGE;
9531 break;
9532 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009533 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009534 Opc = X86ISD::COMI;
9535 CC = ISD::SETNE;
9536 break;
9537 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009538 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009539 Opc = X86ISD::UCOMI;
9540 CC = ISD::SETEQ;
9541 break;
9542 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009543 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009544 Opc = X86ISD::UCOMI;
9545 CC = ISD::SETLT;
9546 break;
9547 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009548 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009549 Opc = X86ISD::UCOMI;
9550 CC = ISD::SETLE;
9551 break;
9552 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009553 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009554 Opc = X86ISD::UCOMI;
9555 CC = ISD::SETGT;
9556 break;
9557 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009558 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009559 Opc = X86ISD::UCOMI;
9560 CC = ISD::SETGE;
9561 break;
9562 case Intrinsic::x86_sse_ucomineq_ss:
9563 case Intrinsic::x86_sse2_ucomineq_sd:
9564 Opc = X86ISD::UCOMI;
9565 CC = ISD::SETNE;
9566 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009567 }
Evan Cheng734503b2006-09-11 02:19:56 +00009568
Dan Gohman475871a2008-07-27 21:46:04 +00009569 SDValue LHS = Op.getOperand(1);
9570 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009571 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009572 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009573 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9574 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9575 DAG.getConstant(X86CC, MVT::i8), Cond);
9576 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009577 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009578 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009579 case Intrinsic::x86_sse2_pmulu_dq:
9580 case Intrinsic::x86_avx2_pmulu_dq:
9581 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9582 Op.getOperand(1), Op.getOperand(2));
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009583 case Intrinsic::x86_sse3_hadd_ps:
9584 case Intrinsic::x86_sse3_hadd_pd:
9585 case Intrinsic::x86_avx_hadd_ps_256:
9586 case Intrinsic::x86_avx_hadd_pd_256:
9587 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9588 Op.getOperand(1), Op.getOperand(2));
9589 case Intrinsic::x86_sse3_hsub_ps:
9590 case Intrinsic::x86_sse3_hsub_pd:
9591 case Intrinsic::x86_avx_hsub_ps_256:
9592 case Intrinsic::x86_avx_hsub_pd_256:
9593 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9594 Op.getOperand(1), Op.getOperand(2));
Craig Topper4bb3f342012-01-25 05:37:32 +00009595 case Intrinsic::x86_ssse3_phadd_w_128:
9596 case Intrinsic::x86_ssse3_phadd_d_128:
9597 case Intrinsic::x86_avx2_phadd_w:
9598 case Intrinsic::x86_avx2_phadd_d:
9599 return DAG.getNode(X86ISD::HADD, dl, Op.getValueType(),
9600 Op.getOperand(1), Op.getOperand(2));
9601 case Intrinsic::x86_ssse3_phsub_w_128:
9602 case Intrinsic::x86_ssse3_phsub_d_128:
9603 case Intrinsic::x86_avx2_phsub_w:
9604 case Intrinsic::x86_avx2_phsub_d:
9605 return DAG.getNode(X86ISD::HSUB, dl, Op.getValueType(),
9606 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009607 case Intrinsic::x86_avx2_psllv_d:
9608 case Intrinsic::x86_avx2_psllv_q:
9609 case Intrinsic::x86_avx2_psllv_d_256:
9610 case Intrinsic::x86_avx2_psllv_q_256:
9611 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9612 Op.getOperand(1), Op.getOperand(2));
9613 case Intrinsic::x86_avx2_psrlv_d:
9614 case Intrinsic::x86_avx2_psrlv_q:
9615 case Intrinsic::x86_avx2_psrlv_d_256:
9616 case Intrinsic::x86_avx2_psrlv_q_256:
9617 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9618 Op.getOperand(1), Op.getOperand(2));
9619 case Intrinsic::x86_avx2_psrav_d:
9620 case Intrinsic::x86_avx2_psrav_d_256:
9621 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9622 Op.getOperand(1), Op.getOperand(2));
Craig Topper969ba282012-01-25 06:43:11 +00009623 case Intrinsic::x86_ssse3_pshuf_b_128:
9624 case Intrinsic::x86_avx2_pshuf_b:
9625 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9626 Op.getOperand(1), Op.getOperand(2));
9627 case Intrinsic::x86_ssse3_psign_b_128:
9628 case Intrinsic::x86_ssse3_psign_w_128:
9629 case Intrinsic::x86_ssse3_psign_d_128:
9630 case Intrinsic::x86_avx2_psign_b:
9631 case Intrinsic::x86_avx2_psign_w:
9632 case Intrinsic::x86_avx2_psign_d:
9633 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9634 Op.getOperand(1), Op.getOperand(2));
Craig Toppere566cd02012-01-26 07:18:03 +00009635 case Intrinsic::x86_sse41_insertps:
9636 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9637 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
9638 case Intrinsic::x86_avx_vperm2f128_ps_256:
9639 case Intrinsic::x86_avx_vperm2f128_pd_256:
9640 case Intrinsic::x86_avx_vperm2f128_si_256:
9641 case Intrinsic::x86_avx2_vperm2i128:
9642 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9643 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topperffa6c402012-04-16 07:13:00 +00009644 case Intrinsic::x86_avx2_permd:
9645 case Intrinsic::x86_avx2_permps:
9646 // Operands intentionally swapped. Mask is last operand to intrinsic,
9647 // but second operand for node/intruction.
9648 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9649 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009650
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009651 // ptest and testp intrinsics. The intrinsic these come from are designed to
9652 // return an integer value, not just an instruction so lower it to the ptest
9653 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009654 case Intrinsic::x86_sse41_ptestz:
9655 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009656 case Intrinsic::x86_sse41_ptestnzc:
9657 case Intrinsic::x86_avx_ptestz_256:
9658 case Intrinsic::x86_avx_ptestc_256:
9659 case Intrinsic::x86_avx_ptestnzc_256:
9660 case Intrinsic::x86_avx_vtestz_ps:
9661 case Intrinsic::x86_avx_vtestc_ps:
9662 case Intrinsic::x86_avx_vtestnzc_ps:
9663 case Intrinsic::x86_avx_vtestz_pd:
9664 case Intrinsic::x86_avx_vtestc_pd:
9665 case Intrinsic::x86_avx_vtestnzc_pd:
9666 case Intrinsic::x86_avx_vtestz_ps_256:
9667 case Intrinsic::x86_avx_vtestc_ps_256:
9668 case Intrinsic::x86_avx_vtestnzc_ps_256:
9669 case Intrinsic::x86_avx_vtestz_pd_256:
9670 case Intrinsic::x86_avx_vtestc_pd_256:
9671 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9672 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009673 unsigned X86CC = 0;
9674 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009675 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009676 case Intrinsic::x86_avx_vtestz_ps:
9677 case Intrinsic::x86_avx_vtestz_pd:
9678 case Intrinsic::x86_avx_vtestz_ps_256:
9679 case Intrinsic::x86_avx_vtestz_pd_256:
9680 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009681 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009682 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009683 // ZF = 1
9684 X86CC = X86::COND_E;
9685 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009686 case Intrinsic::x86_avx_vtestc_ps:
9687 case Intrinsic::x86_avx_vtestc_pd:
9688 case Intrinsic::x86_avx_vtestc_ps_256:
9689 case Intrinsic::x86_avx_vtestc_pd_256:
9690 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009691 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009692 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009693 // CF = 1
9694 X86CC = X86::COND_B;
9695 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009696 case Intrinsic::x86_avx_vtestnzc_ps:
9697 case Intrinsic::x86_avx_vtestnzc_pd:
9698 case Intrinsic::x86_avx_vtestnzc_ps_256:
9699 case Intrinsic::x86_avx_vtestnzc_pd_256:
9700 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009701 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009702 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009703 // ZF and CF = 0
9704 X86CC = X86::COND_A;
9705 break;
9706 }
Eric Christopherfd179292009-08-27 18:07:15 +00009707
Eric Christopher71c67532009-07-29 00:28:05 +00009708 SDValue LHS = Op.getOperand(1);
9709 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009710 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9711 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009712 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9713 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9714 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009715 }
Evan Cheng5759f972008-05-04 09:15:50 +00009716
Craig Topper80e46362012-01-23 06:16:53 +00009717 // SSE/AVX shift intrinsics
9718 case Intrinsic::x86_sse2_psll_w:
9719 case Intrinsic::x86_sse2_psll_d:
9720 case Intrinsic::x86_sse2_psll_q:
9721 case Intrinsic::x86_avx2_psll_w:
9722 case Intrinsic::x86_avx2_psll_d:
9723 case Intrinsic::x86_avx2_psll_q:
9724 return DAG.getNode(X86ISD::VSHL, dl, Op.getValueType(),
9725 Op.getOperand(1), Op.getOperand(2));
9726 case Intrinsic::x86_sse2_psrl_w:
9727 case Intrinsic::x86_sse2_psrl_d:
9728 case Intrinsic::x86_sse2_psrl_q:
9729 case Intrinsic::x86_avx2_psrl_w:
9730 case Intrinsic::x86_avx2_psrl_d:
9731 case Intrinsic::x86_avx2_psrl_q:
9732 return DAG.getNode(X86ISD::VSRL, dl, Op.getValueType(),
9733 Op.getOperand(1), Op.getOperand(2));
9734 case Intrinsic::x86_sse2_psra_w:
9735 case Intrinsic::x86_sse2_psra_d:
9736 case Intrinsic::x86_avx2_psra_w:
9737 case Intrinsic::x86_avx2_psra_d:
9738 return DAG.getNode(X86ISD::VSRA, dl, Op.getValueType(),
9739 Op.getOperand(1), Op.getOperand(2));
Evan Cheng5759f972008-05-04 09:15:50 +00009740 case Intrinsic::x86_sse2_pslli_w:
9741 case Intrinsic::x86_sse2_pslli_d:
9742 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009743 case Intrinsic::x86_avx2_pslli_w:
9744 case Intrinsic::x86_avx2_pslli_d:
9745 case Intrinsic::x86_avx2_pslli_q:
9746 return getTargetVShiftNode(X86ISD::VSHLI, dl, Op.getValueType(),
9747 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009748 case Intrinsic::x86_sse2_psrli_w:
9749 case Intrinsic::x86_sse2_psrli_d:
9750 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009751 case Intrinsic::x86_avx2_psrli_w:
9752 case Intrinsic::x86_avx2_psrli_d:
9753 case Intrinsic::x86_avx2_psrli_q:
9754 return getTargetVShiftNode(X86ISD::VSRLI, dl, Op.getValueType(),
9755 Op.getOperand(1), Op.getOperand(2), DAG);
Evan Cheng5759f972008-05-04 09:15:50 +00009756 case Intrinsic::x86_sse2_psrai_w:
9757 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009758 case Intrinsic::x86_avx2_psrai_w:
9759 case Intrinsic::x86_avx2_psrai_d:
9760 return getTargetVShiftNode(X86ISD::VSRAI, dl, Op.getValueType(),
9761 Op.getOperand(1), Op.getOperand(2), DAG);
9762 // Fix vector shift instructions where the last operand is a non-immediate
9763 // i32 value.
Evan Cheng5759f972008-05-04 09:15:50 +00009764 case Intrinsic::x86_mmx_pslli_w:
9765 case Intrinsic::x86_mmx_pslli_d:
9766 case Intrinsic::x86_mmx_pslli_q:
9767 case Intrinsic::x86_mmx_psrli_w:
9768 case Intrinsic::x86_mmx_psrli_d:
9769 case Intrinsic::x86_mmx_psrli_q:
9770 case Intrinsic::x86_mmx_psrai_w:
9771 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009772 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009773 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009774 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009775
9776 unsigned NewIntNo = 0;
Evan Cheng5759f972008-05-04 09:15:50 +00009777 switch (IntNo) {
Craig Topper80e46362012-01-23 06:16:53 +00009778 case Intrinsic::x86_mmx_pslli_w:
9779 NewIntNo = Intrinsic::x86_mmx_psll_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009780 break;
Craig Topper80e46362012-01-23 06:16:53 +00009781 case Intrinsic::x86_mmx_pslli_d:
9782 NewIntNo = Intrinsic::x86_mmx_psll_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009783 break;
Craig Topper80e46362012-01-23 06:16:53 +00009784 case Intrinsic::x86_mmx_pslli_q:
9785 NewIntNo = Intrinsic::x86_mmx_psll_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009786 break;
Craig Topper80e46362012-01-23 06:16:53 +00009787 case Intrinsic::x86_mmx_psrli_w:
9788 NewIntNo = Intrinsic::x86_mmx_psrl_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009789 break;
Craig Topper80e46362012-01-23 06:16:53 +00009790 case Intrinsic::x86_mmx_psrli_d:
9791 NewIntNo = Intrinsic::x86_mmx_psrl_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009792 break;
Craig Topper80e46362012-01-23 06:16:53 +00009793 case Intrinsic::x86_mmx_psrli_q:
9794 NewIntNo = Intrinsic::x86_mmx_psrl_q;
Evan Cheng5759f972008-05-04 09:15:50 +00009795 break;
Craig Topper80e46362012-01-23 06:16:53 +00009796 case Intrinsic::x86_mmx_psrai_w:
9797 NewIntNo = Intrinsic::x86_mmx_psra_w;
Evan Cheng5759f972008-05-04 09:15:50 +00009798 break;
Craig Topper80e46362012-01-23 06:16:53 +00009799 case Intrinsic::x86_mmx_psrai_d:
9800 NewIntNo = Intrinsic::x86_mmx_psra_d;
Evan Cheng5759f972008-05-04 09:15:50 +00009801 break;
Craig Topper80e46362012-01-23 06:16:53 +00009802 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009803 }
Mon P Wangefa42202009-09-03 19:56:25 +00009804
9805 // The vector shift intrinsics with scalars uses 32b shift amounts but
9806 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9807 // to be zero.
Craig Topper80e46362012-01-23 06:16:53 +00009808 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, ShAmt,
9809 DAG.getConstant(0, MVT::i32));
Dale Johannesen0488fb62010-09-30 23:57:10 +00009810// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009811
Owen Andersone50ed302009-08-10 22:56:29 +00009812 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009813 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009814 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009815 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009816 Op.getOperand(1), ShAmt);
9817 }
Craig Topper4feb6472012-08-06 06:22:36 +00009818 case Intrinsic::x86_sse42_pcmpistria128:
9819 case Intrinsic::x86_sse42_pcmpestria128:
9820 case Intrinsic::x86_sse42_pcmpistric128:
9821 case Intrinsic::x86_sse42_pcmpestric128:
9822 case Intrinsic::x86_sse42_pcmpistrio128:
9823 case Intrinsic::x86_sse42_pcmpestrio128:
9824 case Intrinsic::x86_sse42_pcmpistris128:
9825 case Intrinsic::x86_sse42_pcmpestris128:
9826 case Intrinsic::x86_sse42_pcmpistriz128:
9827 case Intrinsic::x86_sse42_pcmpestriz128: {
9828 unsigned Opcode;
9829 unsigned X86CC;
9830 switch (IntNo) {
9831 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9832 case Intrinsic::x86_sse42_pcmpistria128:
9833 Opcode = X86ISD::PCMPISTRI;
9834 X86CC = X86::COND_A;
9835 break;
9836 case Intrinsic::x86_sse42_pcmpestria128:
9837 Opcode = X86ISD::PCMPESTRI;
9838 X86CC = X86::COND_A;
9839 break;
9840 case Intrinsic::x86_sse42_pcmpistric128:
9841 Opcode = X86ISD::PCMPISTRI;
9842 X86CC = X86::COND_B;
9843 break;
9844 case Intrinsic::x86_sse42_pcmpestric128:
9845 Opcode = X86ISD::PCMPESTRI;
9846 X86CC = X86::COND_B;
9847 break;
9848 case Intrinsic::x86_sse42_pcmpistrio128:
9849 Opcode = X86ISD::PCMPISTRI;
9850 X86CC = X86::COND_O;
9851 break;
9852 case Intrinsic::x86_sse42_pcmpestrio128:
9853 Opcode = X86ISD::PCMPESTRI;
9854 X86CC = X86::COND_O;
9855 break;
9856 case Intrinsic::x86_sse42_pcmpistris128:
9857 Opcode = X86ISD::PCMPISTRI;
9858 X86CC = X86::COND_S;
9859 break;
9860 case Intrinsic::x86_sse42_pcmpestris128:
9861 Opcode = X86ISD::PCMPESTRI;
9862 X86CC = X86::COND_S;
9863 break;
9864 case Intrinsic::x86_sse42_pcmpistriz128:
9865 Opcode = X86ISD::PCMPISTRI;
9866 X86CC = X86::COND_E;
9867 break;
9868 case Intrinsic::x86_sse42_pcmpestriz128:
9869 Opcode = X86ISD::PCMPESTRI;
9870 X86CC = X86::COND_E;
9871 break;
9872 }
9873 SmallVector<SDValue, 5> NewOps;
9874 NewOps.append(Op->op_begin()+1, Op->op_end());
9875 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9876 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
9877 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9878 DAG.getConstant(X86CC, MVT::i8),
9879 SDValue(PCMP.getNode(), 1));
9880 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
9881 }
9882 case Intrinsic::x86_sse42_pcmpistri128:
9883 case Intrinsic::x86_sse42_pcmpestri128: {
9884 unsigned Opcode;
9885 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
9886 Opcode = X86ISD::PCMPISTRI;
9887 else
9888 Opcode = X86ISD::PCMPESTRI;
9889
9890 SmallVector<SDValue, 5> NewOps;
9891 NewOps.append(Op->op_begin()+1, Op->op_end());
9892 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9893 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
9894 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009895 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009896}
Evan Cheng72261582005-12-20 06:22:03 +00009897
Benjamin Kramerb9bee042012-07-12 09:31:43 +00009898SDValue
9899X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
9900 DebugLoc dl = Op.getDebugLoc();
9901 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9902 switch (IntNo) {
9903 default: return SDValue(); // Don't custom lower most intrinsics.
9904
9905 // RDRAND intrinsics.
9906 case Intrinsic::x86_rdrand_16:
9907 case Intrinsic::x86_rdrand_32:
9908 case Intrinsic::x86_rdrand_64: {
9909 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +00009910 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
9911 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +00009912
9913 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
9914 // return the value from Rand, which is always 0, casted to i32.
9915 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
9916 DAG.getConstant(1, Op->getValueType(1)),
9917 DAG.getConstant(X86::COND_B, MVT::i32),
9918 SDValue(Result.getNode(), 1) };
9919 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
9920 DAG.getVTList(Op->getValueType(1), MVT::Glue),
9921 Ops, 4);
9922
9923 // Return { result, isValid, chain }.
9924 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +00009925 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +00009926 }
9927 }
9928}
9929
Dan Gohmand858e902010-04-17 15:26:15 +00009930SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9931 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009932 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9933 MFI->setReturnAddressIsTaken(true);
9934
Bill Wendling64e87322009-01-16 19:25:27 +00009935 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009936 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009937
9938 if (Depth > 0) {
9939 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9940 SDValue Offset =
9941 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009942 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009943 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009944 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009945 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009946 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009947 }
9948
9949 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009950 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009951 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009952 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009953}
9954
Dan Gohmand858e902010-04-17 15:26:15 +00009955SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009956 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9957 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009958
Owen Andersone50ed302009-08-10 22:56:29 +00009959 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009960 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009961 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9962 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009963 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009964 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009965 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9966 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009967 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009968 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009969}
9970
Dan Gohman475871a2008-07-27 21:46:04 +00009971SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009972 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009973 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009974}
9975
Dan Gohmand858e902010-04-17 15:26:15 +00009976SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009977 SDValue Chain = Op.getOperand(0);
9978 SDValue Offset = Op.getOperand(1);
9979 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009980 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009981
Dan Gohmand8816272010-08-11 18:14:00 +00009982 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9983 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9984 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009985 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009986
Dan Gohmand8816272010-08-11 18:14:00 +00009987 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9988 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009989 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009990 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9991 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009992 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009993
Dale Johannesene4d209d2009-02-03 20:21:25 +00009994 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009995 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009996 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009997}
9998
Duncan Sands4a544a72011-09-06 13:37:06 +00009999SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10000 SelectionDAG &DAG) const {
10001 return Op.getOperand(0);
10002}
10003
10004SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10005 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010006 SDValue Root = Op.getOperand(0);
10007 SDValue Trmp = Op.getOperand(1); // trampoline
10008 SDValue FPtr = Op.getOperand(2); // nested function
10009 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010010 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010011
Dan Gohman69de1932008-02-06 22:27:42 +000010012 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010013
10014 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010015 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010016
10017 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010018 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10019 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010020
Evan Cheng0e6a0522011-07-18 20:57:22 +000010021 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10022 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010023
10024 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10025
10026 // Load the pointer to the nested function into R11.
10027 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010028 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010029 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010030 Addr, MachinePointerInfo(TrmpAddr),
10031 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010032
Owen Anderson825b72b2009-08-11 20:47:22 +000010033 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10034 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010035 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10036 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010037 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010038
10039 // Load the 'nest' parameter value into R10.
10040 // R10 is specified in X86CallingConv.td
10041 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010042 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10043 DAG.getConstant(10, MVT::i64));
10044 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010045 Addr, MachinePointerInfo(TrmpAddr, 10),
10046 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010047
Owen Anderson825b72b2009-08-11 20:47:22 +000010048 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10049 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010050 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10051 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010052 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010053
10054 // Jump to the nested function.
10055 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010056 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10057 DAG.getConstant(20, MVT::i64));
10058 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010059 Addr, MachinePointerInfo(TrmpAddr, 20),
10060 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010061
10062 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010063 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10064 DAG.getConstant(22, MVT::i64));
10065 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010066 MachinePointerInfo(TrmpAddr, 22),
10067 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010068
Duncan Sands4a544a72011-09-06 13:37:06 +000010069 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010070 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010071 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010072 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010073 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010074 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010075
10076 switch (CC) {
10077 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010078 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010079 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010080 case CallingConv::X86_StdCall: {
10081 // Pass 'nest' parameter in ECX.
10082 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010083 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010084
10085 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010086 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010087 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010088
Chris Lattner58d74912008-03-12 17:45:29 +000010089 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010090 unsigned InRegCount = 0;
10091 unsigned Idx = 1;
10092
10093 for (FunctionType::param_iterator I = FTy->param_begin(),
10094 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010095 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010096 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010097 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010098
10099 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010100 report_fatal_error("Nest register in use - reduce number of inreg"
10101 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010102 }
10103 }
10104 break;
10105 }
10106 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010107 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010108 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010109 // Pass 'nest' parameter in EAX.
10110 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010111 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010112 break;
10113 }
10114
Dan Gohman475871a2008-07-27 21:46:04 +000010115 SDValue OutChains[4];
10116 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010117
Owen Anderson825b72b2009-08-11 20:47:22 +000010118 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10119 DAG.getConstant(10, MVT::i32));
10120 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010121
Chris Lattnera62fe662010-02-05 19:20:30 +000010122 // This is storing the opcode for MOV32ri.
10123 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010124 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010125 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010126 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010127 Trmp, MachinePointerInfo(TrmpAddr),
10128 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010129
Owen Anderson825b72b2009-08-11 20:47:22 +000010130 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10131 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010132 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10133 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010134 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010135
Chris Lattnera62fe662010-02-05 19:20:30 +000010136 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010137 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10138 DAG.getConstant(5, MVT::i32));
10139 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010140 MachinePointerInfo(TrmpAddr, 5),
10141 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010142
Owen Anderson825b72b2009-08-11 20:47:22 +000010143 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10144 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010145 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10146 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010147 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010148
Duncan Sands4a544a72011-09-06 13:37:06 +000010149 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010150 }
10151}
10152
Dan Gohmand858e902010-04-17 15:26:15 +000010153SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10154 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010155 /*
10156 The rounding mode is in bits 11:10 of FPSR, and has the following
10157 settings:
10158 00 Round to nearest
10159 01 Round to -inf
10160 10 Round to +inf
10161 11 Round to 0
10162
10163 FLT_ROUNDS, on the other hand, expects the following:
10164 -1 Undefined
10165 0 Round to 0
10166 1 Round to nearest
10167 2 Round to +inf
10168 3 Round to -inf
10169
10170 To perform the conversion, we do:
10171 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10172 */
10173
10174 MachineFunction &MF = DAG.getMachineFunction();
10175 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010176 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010177 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010178 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010179 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010180
10181 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010182 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010183 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010184
Michael J. Spencerec38de22010-10-10 22:04:20 +000010185
Chris Lattner2156b792010-09-22 01:11:26 +000010186 MachineMemOperand *MMO =
10187 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10188 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010189
Chris Lattner2156b792010-09-22 01:11:26 +000010190 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10191 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10192 DAG.getVTList(MVT::Other),
10193 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010194
10195 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010196 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010197 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010198
10199 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010200 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010201 DAG.getNode(ISD::SRL, DL, MVT::i16,
10202 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010203 CWD, DAG.getConstant(0x800, MVT::i16)),
10204 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010205 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010206 DAG.getNode(ISD::SRL, DL, MVT::i16,
10207 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010208 CWD, DAG.getConstant(0x400, MVT::i16)),
10209 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010210
Dan Gohman475871a2008-07-27 21:46:04 +000010211 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010212 DAG.getNode(ISD::AND, DL, MVT::i16,
10213 DAG.getNode(ISD::ADD, DL, MVT::i16,
10214 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010215 DAG.getConstant(1, MVT::i16)),
10216 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010217
10218
Duncan Sands83ec4b62008-06-06 12:08:01 +000010219 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010220 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010221}
10222
Dan Gohmand858e902010-04-17 15:26:15 +000010223SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010224 EVT VT = Op.getValueType();
10225 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010226 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010227 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010228
10229 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010230 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010231 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010232 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010233 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010234 }
Evan Cheng18efe262007-12-14 02:13:44 +000010235
Evan Cheng152804e2007-12-14 08:30:15 +000010236 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010237 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010238 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010239
10240 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010241 SDValue Ops[] = {
10242 Op,
10243 DAG.getConstant(NumBits+NumBits-1, OpVT),
10244 DAG.getConstant(X86::COND_E, MVT::i8),
10245 Op.getValue(1)
10246 };
10247 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010248
10249 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010250 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010251
Owen Anderson825b72b2009-08-11 20:47:22 +000010252 if (VT == MVT::i8)
10253 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010254 return Op;
10255}
10256
Chandler Carruthacc068e2011-12-24 10:55:54 +000010257SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10258 SelectionDAG &DAG) const {
10259 EVT VT = Op.getValueType();
10260 EVT OpVT = VT;
10261 unsigned NumBits = VT.getSizeInBits();
10262 DebugLoc dl = Op.getDebugLoc();
10263
10264 Op = Op.getOperand(0);
10265 if (VT == MVT::i8) {
10266 // Zero extend to i32 since there is not an i8 bsr.
10267 OpVT = MVT::i32;
10268 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10269 }
10270
10271 // Issue a bsr (scan bits in reverse).
10272 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10273 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10274
10275 // And xor with NumBits-1.
10276 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10277
10278 if (VT == MVT::i8)
10279 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10280 return Op;
10281}
10282
Dan Gohmand858e902010-04-17 15:26:15 +000010283SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010284 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010285 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010286 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010287 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010288
10289 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010290 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010291 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010292
10293 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010294 SDValue Ops[] = {
10295 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010296 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010297 DAG.getConstant(X86::COND_E, MVT::i8),
10298 Op.getValue(1)
10299 };
Chandler Carruth77821022011-12-24 12:12:34 +000010300 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010301}
10302
Craig Topper13894fa2011-08-24 06:14:18 +000010303// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10304// ones, and then concatenate the result back.
10305static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010306 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010307
10308 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10309 "Unsupported value type for operation");
10310
Craig Topper66ddd152012-04-27 22:54:43 +000010311 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010312 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010313
10314 // Extract the LHS vectors
10315 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010316 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10317 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010318
10319 // Extract the RHS vectors
10320 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010321 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10322 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010323
10324 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10325 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10326
10327 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10328 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10329 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10330}
10331
10332SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10333 assert(Op.getValueType().getSizeInBits() == 256 &&
10334 Op.getValueType().isInteger() &&
10335 "Only handle AVX 256-bit vector integer operation");
10336 return Lower256IntArith(Op, DAG);
10337}
10338
10339SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10340 assert(Op.getValueType().getSizeInBits() == 256 &&
10341 Op.getValueType().isInteger() &&
10342 "Only handle AVX 256-bit vector integer operation");
10343 return Lower256IntArith(Op, DAG);
10344}
10345
10346SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10347 EVT VT = Op.getValueType();
10348
10349 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010350 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010351 return Lower256IntArith(Op, DAG);
10352
Craig Topper5b209e82012-02-05 03:14:49 +000010353 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10354 "Only know how to lower V2I64/V4I64 multiply");
10355
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010356 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010357
Craig Topper5b209e82012-02-05 03:14:49 +000010358 // Ahi = psrlqi(a, 32);
10359 // Bhi = psrlqi(b, 32);
10360 //
10361 // AloBlo = pmuludq(a, b);
10362 // AloBhi = pmuludq(a, Bhi);
10363 // AhiBlo = pmuludq(Ahi, b);
10364
10365 // AloBhi = psllqi(AloBhi, 32);
10366 // AhiBlo = psllqi(AhiBlo, 32);
10367 // return AloBlo + AloBhi + AhiBlo;
10368
Craig Topperaaa643c2011-11-09 07:28:55 +000010369 SDValue A = Op.getOperand(0);
10370 SDValue B = Op.getOperand(1);
10371
Craig Topper5b209e82012-02-05 03:14:49 +000010372 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010373
Craig Topper5b209e82012-02-05 03:14:49 +000010374 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10375 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010376
Craig Topper5b209e82012-02-05 03:14:49 +000010377 // Bit cast to 32-bit vectors for MULUDQ
10378 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10379 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10380 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10381 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10382 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010383
Craig Topper5b209e82012-02-05 03:14:49 +000010384 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10385 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10386 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010387
Craig Topper5b209e82012-02-05 03:14:49 +000010388 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10389 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010390
Dale Johannesene4d209d2009-02-03 20:21:25 +000010391 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010392 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010393}
10394
Nadav Rotem43012222011-05-11 08:12:09 +000010395SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10396
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010397 EVT VT = Op.getValueType();
10398 DebugLoc dl = Op.getDebugLoc();
10399 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010400 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010401 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010402
Craig Topper1accb7e2012-01-10 06:54:16 +000010403 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010404 return SDValue();
10405
Nadav Rotem43012222011-05-11 08:12:09 +000010406 // Optimize shl/srl/sra with constant shift amount.
10407 if (isSplatVector(Amt.getNode())) {
10408 SDValue SclrAmt = Amt->getOperand(0);
10409 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10410 uint64_t ShiftAmt = C->getZExtValue();
10411
Craig Toppered2e13d2012-01-22 19:15:14 +000010412 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10413 (Subtarget->hasAVX2() &&
10414 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10415 if (Op.getOpcode() == ISD::SHL)
10416 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10417 DAG.getConstant(ShiftAmt, MVT::i32));
10418 if (Op.getOpcode() == ISD::SRL)
10419 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10420 DAG.getConstant(ShiftAmt, MVT::i32));
10421 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10422 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10423 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010424 }
10425
Craig Toppered2e13d2012-01-22 19:15:14 +000010426 if (VT == MVT::v16i8) {
10427 if (Op.getOpcode() == ISD::SHL) {
10428 // Make a large shift.
10429 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10430 DAG.getConstant(ShiftAmt, MVT::i32));
10431 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10432 // Zero out the rightmost bits.
10433 SmallVector<SDValue, 16> V(16,
10434 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10435 MVT::i8));
10436 return DAG.getNode(ISD::AND, dl, VT, SHL,
10437 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010438 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010439 if (Op.getOpcode() == ISD::SRL) {
10440 // Make a large shift.
10441 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10442 DAG.getConstant(ShiftAmt, MVT::i32));
10443 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10444 // Zero out the leftmost bits.
10445 SmallVector<SDValue, 16> V(16,
10446 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10447 MVT::i8));
10448 return DAG.getNode(ISD::AND, dl, VT, SRL,
10449 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10450 }
10451 if (Op.getOpcode() == ISD::SRA) {
10452 if (ShiftAmt == 7) {
10453 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010454 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010455 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010456 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010457
Craig Toppered2e13d2012-01-22 19:15:14 +000010458 // R s>> a === ((R u>> a) ^ m) - m
10459 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10460 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10461 MVT::i8));
10462 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10463 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10464 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10465 return Res;
10466 }
Craig Topper731dfd02012-04-23 03:42:40 +000010467 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010468 }
Craig Topper46154eb2011-11-11 07:39:23 +000010469
Craig Topper0d86d462011-11-20 00:12:05 +000010470 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10471 if (Op.getOpcode() == ISD::SHL) {
10472 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010473 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10474 DAG.getConstant(ShiftAmt, MVT::i32));
10475 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010476 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010477 SmallVector<SDValue, 32> V(32,
10478 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10479 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010480 return DAG.getNode(ISD::AND, dl, VT, SHL,
10481 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010482 }
Craig Topper0d86d462011-11-20 00:12:05 +000010483 if (Op.getOpcode() == ISD::SRL) {
10484 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010485 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10486 DAG.getConstant(ShiftAmt, MVT::i32));
10487 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010488 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010489 SmallVector<SDValue, 32> V(32,
10490 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10491 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010492 return DAG.getNode(ISD::AND, dl, VT, SRL,
10493 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10494 }
10495 if (Op.getOpcode() == ISD::SRA) {
10496 if (ShiftAmt == 7) {
10497 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010498 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010499 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010500 }
10501
10502 // R s>> a === ((R u>> a) ^ m) - m
10503 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10504 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10505 MVT::i8));
10506 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10507 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10508 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10509 return Res;
10510 }
Craig Topper731dfd02012-04-23 03:42:40 +000010511 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010512 }
Nadav Rotem43012222011-05-11 08:12:09 +000010513 }
10514 }
10515
10516 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010517 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010518 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10519 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010520
Chris Lattner7302d802012-02-06 21:56:39 +000010521 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10522 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010523 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10524 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010525 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010526 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010527
10528 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010529 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010530 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10531 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10532 }
Nadav Rotem43012222011-05-11 08:12:09 +000010533 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010534 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010535
Nate Begeman51409212010-07-28 00:21:48 +000010536 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010537 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10538 DAG.getConstant(5, MVT::i32));
10539 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010540
Lang Hames8b99c1e2011-12-17 01:08:46 +000010541 // Turn 'a' into a mask suitable for VSELECT
10542 SDValue VSelM = DAG.getConstant(0x80, VT);
10543 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010544 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010545
Lang Hames8b99c1e2011-12-17 01:08:46 +000010546 SDValue CM1 = DAG.getConstant(0x0f, VT);
10547 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010548
Lang Hames8b99c1e2011-12-17 01:08:46 +000010549 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10550 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010551 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10552 DAG.getConstant(4, MVT::i32), DAG);
10553 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010554 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10555
Nate Begeman51409212010-07-28 00:21:48 +000010556 // a += a
10557 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010558 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010559 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010560
Lang Hames8b99c1e2011-12-17 01:08:46 +000010561 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10562 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010563 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10564 DAG.getConstant(2, MVT::i32), DAG);
10565 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010566 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10567
Nate Begeman51409212010-07-28 00:21:48 +000010568 // a += a
10569 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010570 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010571 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010572
Lang Hames8b99c1e2011-12-17 01:08:46 +000010573 // return VSELECT(r, r+r, a);
10574 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010575 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010576 return R;
10577 }
Craig Topper46154eb2011-11-11 07:39:23 +000010578
10579 // Decompose 256-bit shifts into smaller 128-bit shifts.
10580 if (VT.getSizeInBits() == 256) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010581 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010582 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10583 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10584
10585 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010586 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10587 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010588
10589 // Recreate the shift amount vectors
10590 SDValue Amt1, Amt2;
10591 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10592 // Constant shift amount
10593 SmallVector<SDValue, 4> Amt1Csts;
10594 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010595 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010596 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010597 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010598 Amt2Csts.push_back(Amt->getOperand(i));
10599
10600 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10601 &Amt1Csts[0], NumElems/2);
10602 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10603 &Amt2Csts[0], NumElems/2);
10604 } else {
10605 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010606 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10607 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010608 }
10609
10610 // Issue new vector shifts for the smaller types
10611 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10612 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10613
10614 // Concatenate the result back
10615 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10616 }
10617
Nate Begeman51409212010-07-28 00:21:48 +000010618 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010619}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010620
Dan Gohmand858e902010-04-17 15:26:15 +000010621SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010622 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10623 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010624 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10625 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010626 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010627 SDValue LHS = N->getOperand(0);
10628 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010629 unsigned BaseOp = 0;
10630 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010631 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010632 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010633 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010634 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010635 // A subtract of one will be selected as a INC. Note that INC doesn't
10636 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010637 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10638 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010639 BaseOp = X86ISD::INC;
10640 Cond = X86::COND_O;
10641 break;
10642 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010643 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010644 Cond = X86::COND_O;
10645 break;
10646 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010647 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010648 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010649 break;
10650 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010651 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10652 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010653 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10654 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010655 BaseOp = X86ISD::DEC;
10656 Cond = X86::COND_O;
10657 break;
10658 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010659 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010660 Cond = X86::COND_O;
10661 break;
10662 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010663 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010664 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010665 break;
10666 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010667 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010668 Cond = X86::COND_O;
10669 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010670 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10671 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10672 MVT::i32);
10673 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010674
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010675 SDValue SetCC =
10676 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10677 DAG.getConstant(X86::COND_O, MVT::i32),
10678 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010679
Dan Gohman6e5fda22011-07-22 18:45:15 +000010680 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010681 }
Bill Wendling74c37652008-12-09 22:08:41 +000010682 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010683
Bill Wendling61edeb52008-12-02 01:06:39 +000010684 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010685 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010686 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010687
Bill Wendling61edeb52008-12-02 01:06:39 +000010688 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010689 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10690 DAG.getConstant(Cond, MVT::i32),
10691 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010692
Dan Gohman6e5fda22011-07-22 18:45:15 +000010693 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010694}
10695
Chad Rosier30450e82011-12-22 22:35:21 +000010696SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10697 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010698 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010699 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10700 EVT VT = Op.getValueType();
10701
Craig Toppered2e13d2012-01-22 19:15:14 +000010702 if (!Subtarget->hasSSE2() || !VT.isVector())
10703 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010704
Craig Toppered2e13d2012-01-22 19:15:14 +000010705 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10706 ExtraVT.getScalarType().getSizeInBits();
10707 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10708
10709 switch (VT.getSimpleVT().SimpleTy) {
10710 default: return SDValue();
10711 case MVT::v8i32:
10712 case MVT::v16i16:
10713 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010714 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010715 if (!Subtarget->hasAVX2()) {
10716 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000010717 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000010718
Craig Toppered2e13d2012-01-22 19:15:14 +000010719 // Extract the LHS vectors
10720 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010721 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10722 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010723
Craig Toppered2e13d2012-01-22 19:15:14 +000010724 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10725 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010726
Craig Toppered2e13d2012-01-22 19:15:14 +000010727 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000010728 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000010729 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10730 ExtraNumElems/2);
10731 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010732
Craig Toppered2e13d2012-01-22 19:15:14 +000010733 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10734 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010735
Craig Toppered2e13d2012-01-22 19:15:14 +000010736 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10737 }
10738 // fall through
10739 case MVT::v4i32:
10740 case MVT::v8i16: {
10741 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10742 Op.getOperand(0), ShAmt, DAG);
10743 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010744 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010745 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010746}
10747
10748
Eric Christopher9a9d2752010-07-22 02:48:34 +000010749SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10750 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010751
Eric Christopher77ed1352011-07-08 00:04:56 +000010752 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10753 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010754 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010755 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010756 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010757 SDValue Ops[] = {
10758 DAG.getRegister(X86::ESP, MVT::i32), // Base
10759 DAG.getTargetConstant(1, MVT::i8), // Scale
10760 DAG.getRegister(0, MVT::i32), // Index
10761 DAG.getTargetConstant(0, MVT::i32), // Disp
10762 DAG.getRegister(0, MVT::i32), // Segment.
10763 Zero,
10764 Chain
10765 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010766 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010767 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10768 array_lengthof(Ops));
10769 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010770 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010771
Eric Christopher9a9d2752010-07-22 02:48:34 +000010772 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010773 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010774 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010775
Chris Lattner132929a2010-08-14 17:26:09 +000010776 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10777 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10778 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10779 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010780
Chris Lattner132929a2010-08-14 17:26:09 +000010781 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10782 if (!Op1 && !Op2 && !Op3 && Op4)
10783 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010784
Chris Lattner132929a2010-08-14 17:26:09 +000010785 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10786 if (Op1 && !Op2 && !Op3 && !Op4)
10787 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010788
10789 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010790 // (MFENCE)>;
10791 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010792}
10793
Eli Friedman14648462011-07-27 22:21:52 +000010794SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10795 SelectionDAG &DAG) const {
10796 DebugLoc dl = Op.getDebugLoc();
10797 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10798 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10799 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10800 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10801
10802 // The only fence that needs an instruction is a sequentially-consistent
10803 // cross-thread fence.
10804 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10805 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10806 // no-sse2). There isn't any reason to disable it if the target processor
10807 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000010808 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010809 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10810
10811 SDValue Chain = Op.getOperand(0);
10812 SDValue Zero = DAG.getConstant(0, MVT::i32);
10813 SDValue Ops[] = {
10814 DAG.getRegister(X86::ESP, MVT::i32), // Base
10815 DAG.getTargetConstant(1, MVT::i8), // Scale
10816 DAG.getRegister(0, MVT::i32), // Index
10817 DAG.getTargetConstant(0, MVT::i32), // Disp
10818 DAG.getRegister(0, MVT::i32), // Segment.
10819 Zero,
10820 Chain
10821 };
10822 SDNode *Res =
10823 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10824 array_lengthof(Ops));
10825 return SDValue(Res, 0);
10826 }
10827
10828 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10829 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10830}
10831
10832
Dan Gohmand858e902010-04-17 15:26:15 +000010833SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010834 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010835 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010836 unsigned Reg = 0;
10837 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010838 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000010839 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010840 case MVT::i8: Reg = X86::AL; size = 1; break;
10841 case MVT::i16: Reg = X86::AX; size = 2; break;
10842 case MVT::i32: Reg = X86::EAX; size = 4; break;
10843 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010844 assert(Subtarget->is64Bit() && "Node not type legal!");
10845 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010846 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010847 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010848 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010849 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010850 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010851 Op.getOperand(1),
10852 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010853 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010854 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010855 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010856 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10857 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10858 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010859 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010860 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010861 return cpOut;
10862}
10863
Duncan Sands1607f052008-12-01 11:39:25 +000010864SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010865 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010866 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010867 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010868 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010869 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010870 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010871 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10872 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010873 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010874 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10875 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010876 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010877 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010878 rdx.getValue(1)
10879 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010880 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010881}
10882
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010883SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010884 SelectionDAG &DAG) const {
10885 EVT SrcVT = Op.getOperand(0).getValueType();
10886 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000010887 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010888 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010889 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010890 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010891 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010892 // i64 <=> MMX conversions are Legal.
10893 if (SrcVT==MVT::i64 && DstVT.isVector())
10894 return Op;
10895 if (DstVT==MVT::i64 && SrcVT.isVector())
10896 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010897 // MMX <=> MMX conversions are Legal.
10898 if (SrcVT.isVector() && DstVT.isVector())
10899 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010900 // All other conversions need to be expanded.
10901 return SDValue();
10902}
Chris Lattner5b856542010-12-20 00:59:46 +000010903
Dan Gohmand858e902010-04-17 15:26:15 +000010904SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010905 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010906 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010907 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010908 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010909 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010910 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010911 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010912 Node->getOperand(0),
10913 Node->getOperand(1), negOp,
10914 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010915 cast<AtomicSDNode>(Node)->getAlignment(),
10916 cast<AtomicSDNode>(Node)->getOrdering(),
10917 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010918}
10919
Eli Friedman327236c2011-08-24 20:50:09 +000010920static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10921 SDNode *Node = Op.getNode();
10922 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010923 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010924
10925 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010926 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10927 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10928 // (The only way to get a 16-byte store is cmpxchg16b)
10929 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10930 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10931 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010932 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10933 cast<AtomicSDNode>(Node)->getMemoryVT(),
10934 Node->getOperand(0),
10935 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010936 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010937 cast<AtomicSDNode>(Node)->getOrdering(),
10938 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010939 return Swap.getValue(1);
10940 }
10941 // Other atomic stores have a simple pattern.
10942 return Op;
10943}
10944
Chris Lattner5b856542010-12-20 00:59:46 +000010945static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10946 EVT VT = Op.getNode()->getValueType(0);
10947
10948 // Let legalize expand this if it isn't a legal type yet.
10949 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10950 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010951
Chris Lattner5b856542010-12-20 00:59:46 +000010952 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010953
Chris Lattner5b856542010-12-20 00:59:46 +000010954 unsigned Opc;
10955 bool ExtraOp = false;
10956 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000010957 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000010958 case ISD::ADDC: Opc = X86ISD::ADD; break;
10959 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10960 case ISD::SUBC: Opc = X86ISD::SUB; break;
10961 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10962 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010963
Chris Lattner5b856542010-12-20 00:59:46 +000010964 if (!ExtraOp)
10965 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10966 Op.getOperand(1));
10967 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10968 Op.getOperand(1), Op.getOperand(2));
10969}
10970
Evan Cheng0db9fe62006-04-25 20:13:52 +000010971/// LowerOperation - Provide custom lowering hooks for some operations.
10972///
Dan Gohmand858e902010-04-17 15:26:15 +000010973SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010974 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010975 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010976 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010977 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010978 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010979 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10980 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010981 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010982 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010983 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010984 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10985 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10986 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010987 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010988 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010989 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10990 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10991 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010992 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010993 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010994 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010995 case ISD::SHL_PARTS:
10996 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010997 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010998 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010999 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011000 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011001 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011002 case ISD::FABS: return LowerFABS(Op, DAG);
11003 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011004 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011005 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011006 case ISD::SETCC: return LowerSETCC(Op, DAG);
11007 case ISD::SELECT: return LowerSELECT(Op, DAG);
11008 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011009 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011010 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011011 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011012 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011013 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011014 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011015 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11016 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011017 case ISD::FRAME_TO_ARGS_OFFSET:
11018 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011019 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011020 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011021 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11022 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011023 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011024 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011025 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011026 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011027 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011028 case ISD::SRA:
11029 case ISD::SRL:
11030 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011031 case ISD::SADDO:
11032 case ISD::UADDO:
11033 case ISD::SSUBO:
11034 case ISD::USUBO:
11035 case ISD::SMULO:
11036 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011037 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011038 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011039 case ISD::ADDC:
11040 case ISD::ADDE:
11041 case ISD::SUBC:
11042 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011043 case ISD::ADD: return LowerADD(Op, DAG);
11044 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011045 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011046}
11047
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011048static void ReplaceATOMIC_LOAD(SDNode *Node,
11049 SmallVectorImpl<SDValue> &Results,
11050 SelectionDAG &DAG) {
11051 DebugLoc dl = Node->getDebugLoc();
11052 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11053
11054 // Convert wide load -> cmpxchg8b/cmpxchg16b
11055 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11056 // (The only way to get a 16-byte load is cmpxchg16b)
11057 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011058 SDValue Zero = DAG.getConstant(0, VT);
11059 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011060 Node->getOperand(0),
11061 Node->getOperand(1), Zero, Zero,
11062 cast<AtomicSDNode>(Node)->getMemOperand(),
11063 cast<AtomicSDNode>(Node)->getOrdering(),
11064 cast<AtomicSDNode>(Node)->getSynchScope());
11065 Results.push_back(Swap.getValue(0));
11066 Results.push_back(Swap.getValue(1));
11067}
11068
Duncan Sands1607f052008-12-01 11:39:25 +000011069void X86TargetLowering::
11070ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011071 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011072 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011073 assert (Node->getValueType(0) == MVT::i64 &&
11074 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011075
11076 SDValue Chain = Node->getOperand(0);
11077 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011078 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011079 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011080 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011081 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011082 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011083 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011084 SDValue Result =
11085 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11086 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011087 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011088 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011089 Results.push_back(Result.getValue(2));
11090}
11091
Duncan Sands126d9072008-07-04 11:47:58 +000011092/// ReplaceNodeResults - Replace a node with an illegal result type
11093/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011094void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11095 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011096 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011097 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011098 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011099 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011100 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011101 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011102 case ISD::ADDC:
11103 case ISD::ADDE:
11104 case ISD::SUBC:
11105 case ISD::SUBE:
11106 // We don't want to expand or promote these.
11107 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011108 case ISD::FP_TO_SINT:
11109 case ISD::FP_TO_UINT: {
11110 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11111
11112 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11113 return;
11114
Eli Friedman948e95a2009-05-23 09:59:16 +000011115 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011116 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011117 SDValue FIST = Vals.first, StackSlot = Vals.second;
11118 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011119 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011120 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011121 if (StackSlot.getNode() != 0)
11122 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11123 MachinePointerInfo(),
11124 false, false, false, 0));
11125 else
11126 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011127 }
11128 return;
11129 }
11130 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011131 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011132 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011133 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011134 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011135 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011136 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011137 eax.getValue(2));
11138 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11139 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011140 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011141 Results.push_back(edx.getValue(1));
11142 return;
11143 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011144 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011145 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011146 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011147 bool Regs64bit = T == MVT::i128;
11148 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011149 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011150 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11151 DAG.getConstant(0, HalfT));
11152 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11153 DAG.getConstant(1, HalfT));
11154 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11155 Regs64bit ? X86::RAX : X86::EAX,
11156 cpInL, SDValue());
11157 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11158 Regs64bit ? X86::RDX : X86::EDX,
11159 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011160 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011161 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11162 DAG.getConstant(0, HalfT));
11163 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11164 DAG.getConstant(1, HalfT));
11165 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11166 Regs64bit ? X86::RBX : X86::EBX,
11167 swapInL, cpInH.getValue(1));
11168 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011169 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011170 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011171 SDValue Ops[] = { swapInH.getValue(0),
11172 N->getOperand(1),
11173 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011174 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011175 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011176 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11177 X86ISD::LCMPXCHG8_DAG;
11178 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011179 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011180 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11181 Regs64bit ? X86::RAX : X86::EAX,
11182 HalfT, Result.getValue(1));
11183 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11184 Regs64bit ? X86::RDX : X86::EDX,
11185 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011186 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011187 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011188 Results.push_back(cpOutH.getValue(1));
11189 return;
11190 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011191 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000011192 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
11193 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011194 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000011195 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
11196 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011197 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000011198 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
11199 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011200 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011201 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11202 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011203 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011204 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11205 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011206 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011207 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11208 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011209 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011210 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11211 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011212 case ISD::ATOMIC_LOAD:
11213 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011214 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011215}
11216
Evan Cheng72261582005-12-20 06:22:03 +000011217const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11218 switch (Opcode) {
11219 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011220 case X86ISD::BSF: return "X86ISD::BSF";
11221 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011222 case X86ISD::SHLD: return "X86ISD::SHLD";
11223 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011224 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011225 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011226 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011227 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011228 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011229 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011230 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11231 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11232 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011233 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011234 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011235 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011236 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011237 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011238 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011239 case X86ISD::COMI: return "X86ISD::COMI";
11240 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011241 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011242 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011243 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11244 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011245 case X86ISD::CMOV: return "X86ISD::CMOV";
11246 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011247 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011248 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11249 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011250 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011251 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011252 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011253 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011254 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011255 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11256 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011257 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011258 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011259 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011260 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011261 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011262 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11263 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11264 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011265 case X86ISD::HADD: return "X86ISD::HADD";
11266 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011267 case X86ISD::FHADD: return "X86ISD::FHADD";
11268 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011269 case X86ISD::FMAX: return "X86ISD::FMAX";
11270 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011271 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11272 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011273 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011274 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011275 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011276 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011277 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011278 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011279 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011280 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11281 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011282 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11283 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11284 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11285 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11286 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11287 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011288 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11289 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Craig Toppered2e13d2012-01-22 19:15:14 +000011290 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11291 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011292 case X86ISD::VSHL: return "X86ISD::VSHL";
11293 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011294 case X86ISD::VSRA: return "X86ISD::VSRA";
11295 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11296 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11297 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011298 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011299 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11300 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011301 case X86ISD::ADD: return "X86ISD::ADD";
11302 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011303 case X86ISD::ADC: return "X86ISD::ADC";
11304 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011305 case X86ISD::SMUL: return "X86ISD::SMUL";
11306 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011307 case X86ISD::INC: return "X86ISD::INC";
11308 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011309 case X86ISD::OR: return "X86ISD::OR";
11310 case X86ISD::XOR: return "X86ISD::XOR";
11311 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011312 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011313 case X86ISD::BLSI: return "X86ISD::BLSI";
11314 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11315 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011316 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011317 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011318 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011319 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11320 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11321 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011322 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011323 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011324 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011325 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011326 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011327 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11328 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011329 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11330 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11331 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011332 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11333 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011334 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11335 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011336 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011337 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011338 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011339 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11340 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011341 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011342 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011343 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011344 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011345 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011346 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011347 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011348 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011349 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011350 case X86ISD::FMADD: return "X86ISD::FMADD";
11351 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11352 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11353 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11354 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11355 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011356 }
11357}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011358
Chris Lattnerc9addb72007-03-30 23:15:24 +000011359// isLegalAddressingMode - Return true if the addressing mode represented
11360// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011361bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011362 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011363 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011364 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011365 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011366
Chris Lattnerc9addb72007-03-30 23:15:24 +000011367 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011368 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011369 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011370
Chris Lattnerc9addb72007-03-30 23:15:24 +000011371 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011372 unsigned GVFlags =
11373 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011374
Chris Lattnerdfed4132009-07-10 07:38:24 +000011375 // If a reference to this global requires an extra load, we can't fold it.
11376 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011377 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011378
Chris Lattnerdfed4132009-07-10 07:38:24 +000011379 // If BaseGV requires a register for the PIC base, we cannot also have a
11380 // BaseReg specified.
11381 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011382 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011383
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011384 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011385 if ((M != CodeModel::Small || R != Reloc::Static) &&
11386 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011387 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011388 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011389
Chris Lattnerc9addb72007-03-30 23:15:24 +000011390 switch (AM.Scale) {
11391 case 0:
11392 case 1:
11393 case 2:
11394 case 4:
11395 case 8:
11396 // These scales always work.
11397 break;
11398 case 3:
11399 case 5:
11400 case 9:
11401 // These scales are formed with basereg+scalereg. Only accept if there is
11402 // no basereg yet.
11403 if (AM.HasBaseReg)
11404 return false;
11405 break;
11406 default: // Other stuff never works.
11407 return false;
11408 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011409
Chris Lattnerc9addb72007-03-30 23:15:24 +000011410 return true;
11411}
11412
11413
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011414bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011415 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011416 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011417 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11418 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011419 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011420 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011421 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011422}
11423
Evan Cheng70e10d32012-07-17 06:53:39 +000011424bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11425 return Imm == (int32_t)Imm;
11426}
11427
11428bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011429 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011430 return Imm == (int32_t)Imm;
11431}
11432
Owen Andersone50ed302009-08-10 22:56:29 +000011433bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011434 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011435 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011436 unsigned NumBits1 = VT1.getSizeInBits();
11437 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011438 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011439 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011440 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011441}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011442
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011443bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011444 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011445 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011446}
11447
Owen Andersone50ed302009-08-10 22:56:29 +000011448bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011449 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011450 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011451}
11452
Owen Andersone50ed302009-08-10 22:56:29 +000011453bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011454 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011455 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011456}
11457
Evan Cheng60c07e12006-07-05 22:17:51 +000011458/// isShuffleMaskLegal - Targets can use this to indicate that they only
11459/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11460/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11461/// are assumed to be legal.
11462bool
Eric Christopherfd179292009-08-27 18:07:15 +000011463X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011464 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011465 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011466 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011467 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011468
Nate Begemana09008b2009-10-19 02:17:23 +000011469 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011470 return (VT.getVectorNumElements() == 2 ||
11471 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11472 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011473 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011474 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011475 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11476 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011477 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011478 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11479 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011480 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11481 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011482}
11483
Dan Gohman7d8143f2008-04-09 20:09:42 +000011484bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011485X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011486 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011487 unsigned NumElts = VT.getVectorNumElements();
11488 // FIXME: This collection of masks seems suspect.
11489 if (NumElts == 2)
11490 return true;
11491 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11492 return (isMOVLMask(Mask, VT) ||
11493 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011494 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11495 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011496 }
11497 return false;
11498}
11499
11500//===----------------------------------------------------------------------===//
11501// X86 Scheduler Hooks
11502//===----------------------------------------------------------------------===//
11503
Mon P Wang63307c32008-05-05 19:05:59 +000011504// private utility function
11505MachineBasicBlock *
11506X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11507 MachineBasicBlock *MBB,
11508 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011509 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011510 unsigned LoadOpc,
11511 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011512 unsigned notOpc,
11513 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011514 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011515 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011516 // For the atomic bitwise operator, we generate
11517 // thisMBB:
11518 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011519 // ld t1 = [bitinstr.addr]
11520 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011521 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011522 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011523 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011524 // bz newMBB
11525 // fallthrough -->nextMBB
11526 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11527 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011528 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011529 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011530
Mon P Wang63307c32008-05-05 19:05:59 +000011531 /// First build the CFG
11532 MachineFunction *F = MBB->getParent();
11533 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011534 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11535 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11536 F->insert(MBBIter, newMBB);
11537 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011538
Dan Gohman14152b42010-07-06 20:24:04 +000011539 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11540 nextMBB->splice(nextMBB->begin(), thisMBB,
11541 llvm::next(MachineBasicBlock::iterator(bInstr)),
11542 thisMBB->end());
11543 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011544
Mon P Wang63307c32008-05-05 19:05:59 +000011545 // Update thisMBB to fall through to newMBB
11546 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011547
Mon P Wang63307c32008-05-05 19:05:59 +000011548 // newMBB jumps to itself and fall through to nextMBB
11549 newMBB->addSuccessor(nextMBB);
11550 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011551
Mon P Wang63307c32008-05-05 19:05:59 +000011552 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011553 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011554 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011555 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011556 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011557 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011558 int numArgs = bInstr->getNumOperands() - 1;
11559 for (int i=0; i < numArgs; ++i)
11560 argOpers[i] = &bInstr->getOperand(i+1);
11561
11562 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011563 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011564 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011565
Dale Johannesen140be2d2008-08-19 18:47:28 +000011566 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011567 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011568 for (int i=0; i <= lastAddrIndx; ++i)
11569 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011570
Dale Johannesen140be2d2008-08-19 18:47:28 +000011571 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011572 assert((argOpers[valArgIndx]->isReg() ||
11573 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011574 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011575 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011576 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011577 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011578 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011579 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011580 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011581
Richard Smith42fc29e2012-04-13 22:47:00 +000011582 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11583 if (Invert) {
11584 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11585 }
11586 else
11587 t3 = t2;
11588
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011589 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011590 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011591
Dale Johannesene4d209d2009-02-03 20:21:25 +000011592 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011593 for (int i=0; i <= lastAddrIndx; ++i)
11594 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011595 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011596 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011597 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11598 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011599
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011600 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011601 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011602
Mon P Wang63307c32008-05-05 19:05:59 +000011603 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011604 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011605
Dan Gohman14152b42010-07-06 20:24:04 +000011606 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011607 return nextMBB;
11608}
11609
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011610// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011611MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011612X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11613 MachineBasicBlock *MBB,
11614 unsigned regOpcL,
11615 unsigned regOpcH,
11616 unsigned immOpcL,
11617 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011618 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011619 // For the atomic bitwise operator, we generate
11620 // thisMBB (instructions are in pairs, except cmpxchg8b)
11621 // ld t1,t2 = [bitinstr.addr]
11622 // newMBB:
11623 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11624 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011625 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011626 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011627 // mov ECX, EBX <- t5, t6
11628 // mov EAX, EDX <- t1, t2
11629 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11630 // mov t3, t4 <- EAX, EDX
11631 // bz newMBB
11632 // result in out1, out2
11633 // fallthrough -->nextMBB
11634
Craig Topperc9099502012-04-20 06:31:50 +000011635 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011636 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011637 const unsigned NotOpc = X86::NOT32r;
11638 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11639 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11640 MachineFunction::iterator MBBIter = MBB;
11641 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011642
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011643 /// First build the CFG
11644 MachineFunction *F = MBB->getParent();
11645 MachineBasicBlock *thisMBB = MBB;
11646 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11647 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11648 F->insert(MBBIter, newMBB);
11649 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011650
Dan Gohman14152b42010-07-06 20:24:04 +000011651 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11652 nextMBB->splice(nextMBB->begin(), thisMBB,
11653 llvm::next(MachineBasicBlock::iterator(bInstr)),
11654 thisMBB->end());
11655 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011656
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011657 // Update thisMBB to fall through to newMBB
11658 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011659
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011660 // newMBB jumps to itself and fall through to nextMBB
11661 newMBB->addSuccessor(nextMBB);
11662 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011663
Dale Johannesene4d209d2009-02-03 20:21:25 +000011664 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011665 // Insert instructions into newMBB based on incoming instruction
11666 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011667 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011668 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011669 MachineOperand& dest1Oper = bInstr->getOperand(0);
11670 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011671 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11672 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011673 argOpers[i] = &bInstr->getOperand(i+2);
11674
Dan Gohman71ea4e52010-05-14 21:01:44 +000011675 // We use some of the operands multiple times, so conservatively just
11676 // clear any kill flags that might be present.
11677 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11678 argOpers[i]->setIsKill(false);
11679 }
11680
Evan Chengad5b52f2010-01-08 19:14:57 +000011681 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011682 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011683
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011684 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011685 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011686 for (int i=0; i <= lastAddrIndx; ++i)
11687 (*MIB).addOperand(*argOpers[i]);
11688 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011689 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011690 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011691 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011692 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011693 MachineOperand newOp3 = *(argOpers[3]);
11694 if (newOp3.isImm())
11695 newOp3.setImm(newOp3.getImm()+4);
11696 else
11697 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011698 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011699 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011700
11701 // t3/4 are defined later, at the bottom of the loop
11702 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11703 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011704 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011705 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011706 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011707 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11708
Evan Cheng306b4ca2010-01-08 23:41:50 +000011709 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000011710 // the PHI instructions.
11711 t1 = dest1Oper.getReg();
11712 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011713
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011714 int valArgIndx = lastAddrIndx + 1;
11715 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011716 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011717 "invalid operand");
11718 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11719 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011720 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011721 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011722 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011723 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011724 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011725 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011726 (*MIB).addOperand(*argOpers[valArgIndx]);
11727 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011728 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011729 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011730 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011731 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011732 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011733 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011734 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011735 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011736 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011737 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011738
Richard Smith42fc29e2012-04-13 22:47:00 +000011739 unsigned t7, t8;
11740 if (Invert) {
11741 t7 = F->getRegInfo().createVirtualRegister(RC);
11742 t8 = F->getRegInfo().createVirtualRegister(RC);
11743 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
11744 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
11745 } else {
11746 t7 = t5;
11747 t8 = t6;
11748 }
11749
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011750 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011751 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011752 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011753 MIB.addReg(t2);
11754
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011755 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000011756 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011757 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000011758 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000011759
Dale Johannesene4d209d2009-02-03 20:21:25 +000011760 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011761 for (int i=0; i <= lastAddrIndx; ++i)
11762 (*MIB).addOperand(*argOpers[i]);
11763
11764 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011765 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11766 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011767
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011768 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011769 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011770 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011771 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011772
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011773 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011774 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011775
Dan Gohman14152b42010-07-06 20:24:04 +000011776 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011777 return nextMBB;
11778}
11779
11780// private utility function
11781MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011782X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11783 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011784 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011785 // For the atomic min/max operator, we generate
11786 // thisMBB:
11787 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011788 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011789 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011790 // cmp t1, t2
11791 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011792 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011793 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11794 // bz newMBB
11795 // fallthrough -->nextMBB
11796 //
11797 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11798 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011799 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011800 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011801
Mon P Wang63307c32008-05-05 19:05:59 +000011802 /// First build the CFG
11803 MachineFunction *F = MBB->getParent();
11804 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011805 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11806 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11807 F->insert(MBBIter, newMBB);
11808 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011809
Dan Gohman14152b42010-07-06 20:24:04 +000011810 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11811 nextMBB->splice(nextMBB->begin(), thisMBB,
11812 llvm::next(MachineBasicBlock::iterator(mInstr)),
11813 thisMBB->end());
11814 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011815
Mon P Wang63307c32008-05-05 19:05:59 +000011816 // Update thisMBB to fall through to newMBB
11817 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011818
Mon P Wang63307c32008-05-05 19:05:59 +000011819 // newMBB jumps to newMBB and fall through to nextMBB
11820 newMBB->addSuccessor(nextMBB);
11821 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011822
Dale Johannesene4d209d2009-02-03 20:21:25 +000011823 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011824 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011825 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011826 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011827 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011828 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011829 int numArgs = mInstr->getNumOperands() - 1;
11830 for (int i=0; i < numArgs; ++i)
11831 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011832
Mon P Wang63307c32008-05-05 19:05:59 +000011833 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011834 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011835 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011836
Craig Topperc9099502012-04-20 06:31:50 +000011837 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011838 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011839 for (int i=0; i <= lastAddrIndx; ++i)
11840 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011841
Mon P Wang63307c32008-05-05 19:05:59 +000011842 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011843 assert((argOpers[valArgIndx]->isReg() ||
11844 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011845 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011846
Craig Topperc9099502012-04-20 06:31:50 +000011847 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011848 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011849 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011850 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011851 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011852 (*MIB).addOperand(*argOpers[valArgIndx]);
11853
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011854 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011855 MIB.addReg(t1);
11856
Dale Johannesene4d209d2009-02-03 20:21:25 +000011857 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011858 MIB.addReg(t1);
11859 MIB.addReg(t2);
11860
11861 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000011862 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011863 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011864 MIB.addReg(t2);
11865 MIB.addReg(t1);
11866
11867 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011868 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011869 for (int i=0; i <= lastAddrIndx; ++i)
11870 (*MIB).addOperand(*argOpers[i]);
11871 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011872 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011873 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11874 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011875
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011876 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011877 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011878
Mon P Wang63307c32008-05-05 19:05:59 +000011879 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011880 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011881
Dan Gohman14152b42010-07-06 20:24:04 +000011882 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011883 return nextMBB;
11884}
11885
Eric Christopherf83a5de2009-08-27 18:08:16 +000011886// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011887// or XMM0_V32I8 in AVX all of this code can be replaced with that
11888// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011889MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011890X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011891 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000011892 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011893 "Target must have SSE4.2 or AVX features enabled");
11894
Eric Christopherb120ab42009-08-18 22:50:32 +000011895 DebugLoc dl = MI->getDebugLoc();
11896 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011897 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011898 if (!Subtarget->hasAVX()) {
11899 if (memArg)
11900 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11901 else
11902 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11903 } else {
11904 if (memArg)
11905 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11906 else
11907 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11908 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011909
Eric Christopher41c902f2010-11-30 08:20:21 +000011910 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011911 for (unsigned i = 0; i < numArgs; ++i) {
11912 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011913 if (!(Op.isReg() && Op.isImplicit()))
11914 MIB.addOperand(Op);
11915 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011916 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000011917 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011918 .addReg(X86::XMM0);
11919
Dan Gohman14152b42010-07-06 20:24:04 +000011920 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011921 return BB;
11922}
11923
11924MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011925X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011926 DebugLoc dl = MI->getDebugLoc();
11927 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011928
Eric Christopher228232b2010-11-30 07:20:12 +000011929 // Address into RAX/EAX, other two args into ECX, EDX.
11930 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11931 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11932 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11933 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011934 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011935
Eric Christopher228232b2010-11-30 07:20:12 +000011936 unsigned ValOps = X86::AddrNumOperands;
11937 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11938 .addReg(MI->getOperand(ValOps).getReg());
11939 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11940 .addReg(MI->getOperand(ValOps+1).getReg());
11941
11942 // The instruction doesn't actually take any operands though.
11943 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011944
Eric Christopher228232b2010-11-30 07:20:12 +000011945 MI->eraseFromParent(); // The pseudo is gone now.
11946 return BB;
11947}
11948
11949MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011950X86TargetLowering::EmitVAARG64WithCustomInserter(
11951 MachineInstr *MI,
11952 MachineBasicBlock *MBB) const {
11953 // Emit va_arg instruction on X86-64.
11954
11955 // Operands to this pseudo-instruction:
11956 // 0 ) Output : destination address (reg)
11957 // 1-5) Input : va_list address (addr, i64mem)
11958 // 6 ) ArgSize : Size (in bytes) of vararg type
11959 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11960 // 8 ) Align : Alignment of type
11961 // 9 ) EFLAGS (implicit-def)
11962
11963 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11964 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11965
11966 unsigned DestReg = MI->getOperand(0).getReg();
11967 MachineOperand &Base = MI->getOperand(1);
11968 MachineOperand &Scale = MI->getOperand(2);
11969 MachineOperand &Index = MI->getOperand(3);
11970 MachineOperand &Disp = MI->getOperand(4);
11971 MachineOperand &Segment = MI->getOperand(5);
11972 unsigned ArgSize = MI->getOperand(6).getImm();
11973 unsigned ArgMode = MI->getOperand(7).getImm();
11974 unsigned Align = MI->getOperand(8).getImm();
11975
11976 // Memory Reference
11977 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11978 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11979 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11980
11981 // Machine Information
11982 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11983 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11984 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11985 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11986 DebugLoc DL = MI->getDebugLoc();
11987
11988 // struct va_list {
11989 // i32 gp_offset
11990 // i32 fp_offset
11991 // i64 overflow_area (address)
11992 // i64 reg_save_area (address)
11993 // }
11994 // sizeof(va_list) = 24
11995 // alignment(va_list) = 8
11996
11997 unsigned TotalNumIntRegs = 6;
11998 unsigned TotalNumXMMRegs = 8;
11999 bool UseGPOffset = (ArgMode == 1);
12000 bool UseFPOffset = (ArgMode == 2);
12001 unsigned MaxOffset = TotalNumIntRegs * 8 +
12002 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12003
12004 /* Align ArgSize to a multiple of 8 */
12005 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12006 bool NeedsAlign = (Align > 8);
12007
12008 MachineBasicBlock *thisMBB = MBB;
12009 MachineBasicBlock *overflowMBB;
12010 MachineBasicBlock *offsetMBB;
12011 MachineBasicBlock *endMBB;
12012
12013 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12014 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12015 unsigned OffsetReg = 0;
12016
12017 if (!UseGPOffset && !UseFPOffset) {
12018 // If we only pull from the overflow region, we don't create a branch.
12019 // We don't need to alter control flow.
12020 OffsetDestReg = 0; // unused
12021 OverflowDestReg = DestReg;
12022
12023 offsetMBB = NULL;
12024 overflowMBB = thisMBB;
12025 endMBB = thisMBB;
12026 } else {
12027 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12028 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12029 // If not, pull from overflow_area. (branch to overflowMBB)
12030 //
12031 // thisMBB
12032 // | .
12033 // | .
12034 // offsetMBB overflowMBB
12035 // | .
12036 // | .
12037 // endMBB
12038
12039 // Registers for the PHI in endMBB
12040 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12041 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12042
12043 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12044 MachineFunction *MF = MBB->getParent();
12045 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12046 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12047 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12048
12049 MachineFunction::iterator MBBIter = MBB;
12050 ++MBBIter;
12051
12052 // Insert the new basic blocks
12053 MF->insert(MBBIter, offsetMBB);
12054 MF->insert(MBBIter, overflowMBB);
12055 MF->insert(MBBIter, endMBB);
12056
12057 // Transfer the remainder of MBB and its successor edges to endMBB.
12058 endMBB->splice(endMBB->begin(), thisMBB,
12059 llvm::next(MachineBasicBlock::iterator(MI)),
12060 thisMBB->end());
12061 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12062
12063 // Make offsetMBB and overflowMBB successors of thisMBB
12064 thisMBB->addSuccessor(offsetMBB);
12065 thisMBB->addSuccessor(overflowMBB);
12066
12067 // endMBB is a successor of both offsetMBB and overflowMBB
12068 offsetMBB->addSuccessor(endMBB);
12069 overflowMBB->addSuccessor(endMBB);
12070
12071 // Load the offset value into a register
12072 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12073 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12074 .addOperand(Base)
12075 .addOperand(Scale)
12076 .addOperand(Index)
12077 .addDisp(Disp, UseFPOffset ? 4 : 0)
12078 .addOperand(Segment)
12079 .setMemRefs(MMOBegin, MMOEnd);
12080
12081 // Check if there is enough room left to pull this argument.
12082 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12083 .addReg(OffsetReg)
12084 .addImm(MaxOffset + 8 - ArgSizeA8);
12085
12086 // Branch to "overflowMBB" if offset >= max
12087 // Fall through to "offsetMBB" otherwise
12088 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12089 .addMBB(overflowMBB);
12090 }
12091
12092 // In offsetMBB, emit code to use the reg_save_area.
12093 if (offsetMBB) {
12094 assert(OffsetReg != 0);
12095
12096 // Read the reg_save_area address.
12097 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12098 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12099 .addOperand(Base)
12100 .addOperand(Scale)
12101 .addOperand(Index)
12102 .addDisp(Disp, 16)
12103 .addOperand(Segment)
12104 .setMemRefs(MMOBegin, MMOEnd);
12105
12106 // Zero-extend the offset
12107 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12108 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12109 .addImm(0)
12110 .addReg(OffsetReg)
12111 .addImm(X86::sub_32bit);
12112
12113 // Add the offset to the reg_save_area to get the final address.
12114 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12115 .addReg(OffsetReg64)
12116 .addReg(RegSaveReg);
12117
12118 // Compute the offset for the next argument
12119 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12120 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12121 .addReg(OffsetReg)
12122 .addImm(UseFPOffset ? 16 : 8);
12123
12124 // Store it back into the va_list.
12125 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12126 .addOperand(Base)
12127 .addOperand(Scale)
12128 .addOperand(Index)
12129 .addDisp(Disp, UseFPOffset ? 4 : 0)
12130 .addOperand(Segment)
12131 .addReg(NextOffsetReg)
12132 .setMemRefs(MMOBegin, MMOEnd);
12133
12134 // Jump to endMBB
12135 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12136 .addMBB(endMBB);
12137 }
12138
12139 //
12140 // Emit code to use overflow area
12141 //
12142
12143 // Load the overflow_area address into a register.
12144 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12145 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12146 .addOperand(Base)
12147 .addOperand(Scale)
12148 .addOperand(Index)
12149 .addDisp(Disp, 8)
12150 .addOperand(Segment)
12151 .setMemRefs(MMOBegin, MMOEnd);
12152
12153 // If we need to align it, do so. Otherwise, just copy the address
12154 // to OverflowDestReg.
12155 if (NeedsAlign) {
12156 // Align the overflow address
12157 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12158 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12159
12160 // aligned_addr = (addr + (align-1)) & ~(align-1)
12161 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12162 .addReg(OverflowAddrReg)
12163 .addImm(Align-1);
12164
12165 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12166 .addReg(TmpReg)
12167 .addImm(~(uint64_t)(Align-1));
12168 } else {
12169 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12170 .addReg(OverflowAddrReg);
12171 }
12172
12173 // Compute the next overflow address after this argument.
12174 // (the overflow address should be kept 8-byte aligned)
12175 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12176 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12177 .addReg(OverflowDestReg)
12178 .addImm(ArgSizeA8);
12179
12180 // Store the new overflow address.
12181 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12182 .addOperand(Base)
12183 .addOperand(Scale)
12184 .addOperand(Index)
12185 .addDisp(Disp, 8)
12186 .addOperand(Segment)
12187 .addReg(NextAddrReg)
12188 .setMemRefs(MMOBegin, MMOEnd);
12189
12190 // If we branched, emit the PHI to the front of endMBB.
12191 if (offsetMBB) {
12192 BuildMI(*endMBB, endMBB->begin(), DL,
12193 TII->get(X86::PHI), DestReg)
12194 .addReg(OffsetDestReg).addMBB(offsetMBB)
12195 .addReg(OverflowDestReg).addMBB(overflowMBB);
12196 }
12197
12198 // Erase the pseudo instruction
12199 MI->eraseFromParent();
12200
12201 return endMBB;
12202}
12203
12204MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012205X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12206 MachineInstr *MI,
12207 MachineBasicBlock *MBB) const {
12208 // Emit code to save XMM registers to the stack. The ABI says that the
12209 // number of registers to save is given in %al, so it's theoretically
12210 // possible to do an indirect jump trick to avoid saving all of them,
12211 // however this code takes a simpler approach and just executes all
12212 // of the stores if %al is non-zero. It's less code, and it's probably
12213 // easier on the hardware branch predictor, and stores aren't all that
12214 // expensive anyway.
12215
12216 // Create the new basic blocks. One block contains all the XMM stores,
12217 // and one block is the final destination regardless of whether any
12218 // stores were performed.
12219 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12220 MachineFunction *F = MBB->getParent();
12221 MachineFunction::iterator MBBIter = MBB;
12222 ++MBBIter;
12223 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12224 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12225 F->insert(MBBIter, XMMSaveMBB);
12226 F->insert(MBBIter, EndMBB);
12227
Dan Gohman14152b42010-07-06 20:24:04 +000012228 // Transfer the remainder of MBB and its successor edges to EndMBB.
12229 EndMBB->splice(EndMBB->begin(), MBB,
12230 llvm::next(MachineBasicBlock::iterator(MI)),
12231 MBB->end());
12232 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12233
Dan Gohmand6708ea2009-08-15 01:38:56 +000012234 // The original block will now fall through to the XMM save block.
12235 MBB->addSuccessor(XMMSaveMBB);
12236 // The XMMSaveMBB will fall through to the end block.
12237 XMMSaveMBB->addSuccessor(EndMBB);
12238
12239 // Now add the instructions.
12240 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12241 DebugLoc DL = MI->getDebugLoc();
12242
12243 unsigned CountReg = MI->getOperand(0).getReg();
12244 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12245 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12246
12247 if (!Subtarget->isTargetWin64()) {
12248 // If %al is 0, branch around the XMM save block.
12249 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012250 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012251 MBB->addSuccessor(EndMBB);
12252 }
12253
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012254 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012255 // In the XMM save block, save all the XMM argument registers.
12256 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12257 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012258 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012259 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012260 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012261 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012262 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012263 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012264 .addFrameIndex(RegSaveFrameIndex)
12265 .addImm(/*Scale=*/1)
12266 .addReg(/*IndexReg=*/0)
12267 .addImm(/*Disp=*/Offset)
12268 .addReg(/*Segment=*/0)
12269 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012270 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012271 }
12272
Dan Gohman14152b42010-07-06 20:24:04 +000012273 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012274
12275 return EndMBB;
12276}
Mon P Wang63307c32008-05-05 19:05:59 +000012277
Lang Hames6e3f7e42012-02-03 01:13:49 +000012278// The EFLAGS operand of SelectItr might be missing a kill marker
12279// because there were multiple uses of EFLAGS, and ISel didn't know
12280// which to mark. Figure out whether SelectItr should have had a
12281// kill marker, and set it if it should. Returns the correct kill
12282// marker value.
12283static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12284 MachineBasicBlock* BB,
12285 const TargetRegisterInfo* TRI) {
12286 // Scan forward through BB for a use/def of EFLAGS.
12287 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12288 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012289 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012290 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012291 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012292 if (mi.definesRegister(X86::EFLAGS))
12293 break; // Should have kill-flag - update below.
12294 }
12295
12296 // If we hit the end of the block, check whether EFLAGS is live into a
12297 // successor.
12298 if (miI == BB->end()) {
12299 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12300 sEnd = BB->succ_end();
12301 sItr != sEnd; ++sItr) {
12302 MachineBasicBlock* succ = *sItr;
12303 if (succ->isLiveIn(X86::EFLAGS))
12304 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012305 }
12306 }
12307
Lang Hames6e3f7e42012-02-03 01:13:49 +000012308 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12309 // out. SelectMI should have a kill flag on EFLAGS.
12310 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012311 return true;
12312}
12313
Evan Cheng60c07e12006-07-05 22:17:51 +000012314MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012315X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012316 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012317 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12318 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012319
Chris Lattner52600972009-09-02 05:57:00 +000012320 // To "insert" a SELECT_CC instruction, we actually have to insert the
12321 // diamond control-flow pattern. The incoming instruction knows the
12322 // destination vreg to set, the condition code register to branch on, the
12323 // true/false values to select between, and a branch opcode to use.
12324 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12325 MachineFunction::iterator It = BB;
12326 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012327
Chris Lattner52600972009-09-02 05:57:00 +000012328 // thisMBB:
12329 // ...
12330 // TrueVal = ...
12331 // cmpTY ccX, r1, r2
12332 // bCC copy1MBB
12333 // fallthrough --> copy0MBB
12334 MachineBasicBlock *thisMBB = BB;
12335 MachineFunction *F = BB->getParent();
12336 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12337 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012338 F->insert(It, copy0MBB);
12339 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012340
Bill Wendling730c07e2010-06-25 20:48:10 +000012341 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12342 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012343 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12344 if (!MI->killsRegister(X86::EFLAGS) &&
12345 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12346 copy0MBB->addLiveIn(X86::EFLAGS);
12347 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012348 }
12349
Dan Gohman14152b42010-07-06 20:24:04 +000012350 // Transfer the remainder of BB and its successor edges to sinkMBB.
12351 sinkMBB->splice(sinkMBB->begin(), BB,
12352 llvm::next(MachineBasicBlock::iterator(MI)),
12353 BB->end());
12354 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12355
12356 // Add the true and fallthrough blocks as its successors.
12357 BB->addSuccessor(copy0MBB);
12358 BB->addSuccessor(sinkMBB);
12359
12360 // Create the conditional branch instruction.
12361 unsigned Opc =
12362 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12363 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12364
Chris Lattner52600972009-09-02 05:57:00 +000012365 // copy0MBB:
12366 // %FalseValue = ...
12367 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012368 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012369
Chris Lattner52600972009-09-02 05:57:00 +000012370 // sinkMBB:
12371 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12372 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012373 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12374 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012375 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12376 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12377
Dan Gohman14152b42010-07-06 20:24:04 +000012378 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012379 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012380}
12381
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012382MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012383X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12384 bool Is64Bit) const {
12385 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12386 DebugLoc DL = MI->getDebugLoc();
12387 MachineFunction *MF = BB->getParent();
12388 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12389
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012390 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012391
12392 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12393 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12394
12395 // BB:
12396 // ... [Till the alloca]
12397 // If stacklet is not large enough, jump to mallocMBB
12398 //
12399 // bumpMBB:
12400 // Allocate by subtracting from RSP
12401 // Jump to continueMBB
12402 //
12403 // mallocMBB:
12404 // Allocate by call to runtime
12405 //
12406 // continueMBB:
12407 // ...
12408 // [rest of original BB]
12409 //
12410
12411 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12412 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12413 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12414
12415 MachineRegisterInfo &MRI = MF->getRegInfo();
12416 const TargetRegisterClass *AddrRegClass =
12417 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12418
12419 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12420 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12421 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012422 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012423 sizeVReg = MI->getOperand(1).getReg(),
12424 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12425
12426 MachineFunction::iterator MBBIter = BB;
12427 ++MBBIter;
12428
12429 MF->insert(MBBIter, bumpMBB);
12430 MF->insert(MBBIter, mallocMBB);
12431 MF->insert(MBBIter, continueMBB);
12432
12433 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12434 (MachineBasicBlock::iterator(MI)), BB->end());
12435 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12436
12437 // Add code to the main basic block to check if the stack limit has been hit,
12438 // and if so, jump to mallocMBB otherwise to bumpMBB.
12439 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012440 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012441 .addReg(tmpSPVReg).addReg(sizeVReg);
12442 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012443 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012444 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012445 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12446
12447 // bumpMBB simply decreases the stack pointer, since we know the current
12448 // stacklet has enough space.
12449 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012450 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012451 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012452 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012453 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12454
12455 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012456 const uint32_t *RegMask =
12457 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012458 if (Is64Bit) {
12459 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12460 .addReg(sizeVReg);
12461 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012462 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012463 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012464 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012465 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012466 } else {
12467 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12468 .addImm(12);
12469 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12470 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012471 .addExternalSymbol("__morestack_allocate_stack_space")
12472 .addRegMask(RegMask)
12473 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012474 }
12475
12476 if (!Is64Bit)
12477 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12478 .addImm(16);
12479
12480 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12481 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12482 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12483
12484 // Set up the CFG correctly.
12485 BB->addSuccessor(bumpMBB);
12486 BB->addSuccessor(mallocMBB);
12487 mallocMBB->addSuccessor(continueMBB);
12488 bumpMBB->addSuccessor(continueMBB);
12489
12490 // Take care of the PHI nodes.
12491 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12492 MI->getOperand(0).getReg())
12493 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12494 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12495
12496 // Delete the original pseudo instruction.
12497 MI->eraseFromParent();
12498
12499 // And we're done.
12500 return continueMBB;
12501}
12502
12503MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012504X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012505 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012506 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12507 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012508
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012509 assert(!Subtarget->isTargetEnvMacho());
12510
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012511 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12512 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012513
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012514 if (Subtarget->isTargetWin64()) {
12515 if (Subtarget->isTargetCygMing()) {
12516 // ___chkstk(Mingw64):
12517 // Clobbers R10, R11, RAX and EFLAGS.
12518 // Updates RSP.
12519 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12520 .addExternalSymbol("___chkstk")
12521 .addReg(X86::RAX, RegState::Implicit)
12522 .addReg(X86::RSP, RegState::Implicit)
12523 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12524 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12525 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12526 } else {
12527 // __chkstk(MSVCRT): does not update stack pointer.
12528 // Clobbers R10, R11 and EFLAGS.
12529 // FIXME: RAX(allocated size) might be reused and not killed.
12530 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12531 .addExternalSymbol("__chkstk")
12532 .addReg(X86::RAX, RegState::Implicit)
12533 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12534 // RAX has the offset to subtracted from RSP.
12535 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12536 .addReg(X86::RSP)
12537 .addReg(X86::RAX);
12538 }
12539 } else {
12540 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012541 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12542
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012543 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12544 .addExternalSymbol(StackProbeSymbol)
12545 .addReg(X86::EAX, RegState::Implicit)
12546 .addReg(X86::ESP, RegState::Implicit)
12547 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12548 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12549 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12550 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012551
Dan Gohman14152b42010-07-06 20:24:04 +000012552 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012553 return BB;
12554}
Chris Lattner52600972009-09-02 05:57:00 +000012555
12556MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012557X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12558 MachineBasicBlock *BB) const {
12559 // This is pretty easy. We're taking the value that we received from
12560 // our load from the relocation, sticking it in either RDI (x86-64)
12561 // or EAX and doing an indirect call. The return value will then
12562 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012563 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012564 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012565 DebugLoc DL = MI->getDebugLoc();
12566 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012567
12568 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012569 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012570
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012571 // Get a register mask for the lowered call.
12572 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12573 // proper register mask.
12574 const uint32_t *RegMask =
12575 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012576 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012577 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12578 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012579 .addReg(X86::RIP)
12580 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012581 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012582 MI->getOperand(3).getTargetFlags())
12583 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012584 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012585 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012586 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012587 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012588 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12589 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012590 .addReg(0)
12591 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012592 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012593 MI->getOperand(3).getTargetFlags())
12594 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012595 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012596 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012597 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012598 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012599 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12600 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012601 .addReg(TII->getGlobalBaseReg(F))
12602 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012603 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012604 MI->getOperand(3).getTargetFlags())
12605 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012606 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012607 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012608 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012609 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012610
Dan Gohman14152b42010-07-06 20:24:04 +000012611 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012612 return BB;
12613}
12614
12615MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012616X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012617 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012618 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012619 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012620 case X86::TAILJMPd64:
12621 case X86::TAILJMPr64:
12622 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012623 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012624 case X86::TCRETURNdi64:
12625 case X86::TCRETURNri64:
12626 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012627 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012628 case X86::WIN_ALLOCA:
12629 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012630 case X86::SEG_ALLOCA_32:
12631 return EmitLoweredSegAlloca(MI, BB, false);
12632 case X86::SEG_ALLOCA_64:
12633 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012634 case X86::TLSCall_32:
12635 case X86::TLSCall_64:
12636 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012637 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012638 case X86::CMOV_FR32:
12639 case X86::CMOV_FR64:
12640 case X86::CMOV_V4F32:
12641 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012642 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012643 case X86::CMOV_V8F32:
12644 case X86::CMOV_V4F64:
12645 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012646 case X86::CMOV_GR16:
12647 case X86::CMOV_GR32:
12648 case X86::CMOV_RFP32:
12649 case X86::CMOV_RFP64:
12650 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012651 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012652
Dale Johannesen849f2142007-07-03 00:53:03 +000012653 case X86::FP32_TO_INT16_IN_MEM:
12654 case X86::FP32_TO_INT32_IN_MEM:
12655 case X86::FP32_TO_INT64_IN_MEM:
12656 case X86::FP64_TO_INT16_IN_MEM:
12657 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012658 case X86::FP64_TO_INT64_IN_MEM:
12659 case X86::FP80_TO_INT16_IN_MEM:
12660 case X86::FP80_TO_INT32_IN_MEM:
12661 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012662 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12663 DebugLoc DL = MI->getDebugLoc();
12664
Evan Cheng60c07e12006-07-05 22:17:51 +000012665 // Change the floating point control register to use "round towards zero"
12666 // mode when truncating to an integer value.
12667 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012668 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012669 addFrameReference(BuildMI(*BB, MI, DL,
12670 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012671
12672 // Load the old value of the high byte of the control word...
12673 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012674 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012675 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012676 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012677
12678 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012679 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012680 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012681
12682 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012683 addFrameReference(BuildMI(*BB, MI, DL,
12684 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012685
12686 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012687 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012688 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012689
12690 // Get the X86 opcode to use.
12691 unsigned Opc;
12692 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012693 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012694 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12695 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12696 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12697 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12698 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12699 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012700 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12701 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12702 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012703 }
12704
12705 X86AddressMode AM;
12706 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012707 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012708 AM.BaseType = X86AddressMode::RegBase;
12709 AM.Base.Reg = Op.getReg();
12710 } else {
12711 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012712 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012713 }
12714 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012715 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012716 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012717 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012718 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012719 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012720 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012721 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012722 AM.GV = Op.getGlobal();
12723 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012724 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012725 }
Dan Gohman14152b42010-07-06 20:24:04 +000012726 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012727 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012728
12729 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012730 addFrameReference(BuildMI(*BB, MI, DL,
12731 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012732
Dan Gohman14152b42010-07-06 20:24:04 +000012733 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012734 return BB;
12735 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012736 // String/text processing lowering.
12737 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012738 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012739 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12740 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012741 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012742 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12743 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012744 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012745 return EmitPCMP(MI, BB, 5, false /* in mem */);
12746 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012747 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012748 return EmitPCMP(MI, BB, 5, true /* in mem */);
12749
Eric Christopher228232b2010-11-30 07:20:12 +000012750 // Thread synchronization.
12751 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012752 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012753
Eric Christopherb120ab42009-08-18 22:50:32 +000012754 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012755 case X86::ATOMAND32:
12756 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012757 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012758 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012759 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012760 &X86::GR32RegClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012761 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012762 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12763 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012764 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012765 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012766 &X86::GR32RegClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012767 case X86::ATOMXOR32:
12768 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012769 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012770 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012771 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012772 &X86::GR32RegClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012773 case X86::ATOMNAND32:
12774 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012775 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012776 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012777 X86::NOT32r, X86::EAX,
Craig Topperc9099502012-04-20 06:31:50 +000012778 &X86::GR32RegClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012779 case X86::ATOMMIN32:
12780 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12781 case X86::ATOMMAX32:
12782 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12783 case X86::ATOMUMIN32:
12784 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12785 case X86::ATOMUMAX32:
12786 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012787
12788 case X86::ATOMAND16:
12789 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12790 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012791 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012792 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012793 &X86::GR16RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012794 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012795 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012796 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012797 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012798 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012799 &X86::GR16RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012800 case X86::ATOMXOR16:
12801 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12802 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012803 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012804 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012805 &X86::GR16RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012806 case X86::ATOMNAND16:
12807 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12808 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012809 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012810 X86::NOT16r, X86::AX,
Craig Topperc9099502012-04-20 06:31:50 +000012811 &X86::GR16RegClass, true);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012812 case X86::ATOMMIN16:
12813 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12814 case X86::ATOMMAX16:
12815 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12816 case X86::ATOMUMIN16:
12817 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12818 case X86::ATOMUMAX16:
12819 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12820
12821 case X86::ATOMAND8:
12822 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12823 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012824 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012825 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000012826 &X86::GR8RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012827 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012828 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012829 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012830 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012831 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000012832 &X86::GR8RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012833 case X86::ATOMXOR8:
12834 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12835 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012836 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012837 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000012838 &X86::GR8RegClass);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012839 case X86::ATOMNAND8:
12840 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12841 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012842 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012843 X86::NOT8r, X86::AL,
Craig Topperc9099502012-04-20 06:31:50 +000012844 &X86::GR8RegClass, true);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012845 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012846 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012847 case X86::ATOMAND64:
12848 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012849 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012850 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012851 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000012852 &X86::GR64RegClass);
Dale Johannesena99e3842008-08-20 00:48:50 +000012853 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012854 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12855 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012856 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012857 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000012858 &X86::GR64RegClass);
Dale Johannesena99e3842008-08-20 00:48:50 +000012859 case X86::ATOMXOR64:
12860 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012861 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012862 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012863 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000012864 &X86::GR64RegClass);
Dale Johannesena99e3842008-08-20 00:48:50 +000012865 case X86::ATOMNAND64:
12866 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12867 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012868 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012869 X86::NOT64r, X86::RAX,
Craig Topperc9099502012-04-20 06:31:50 +000012870 &X86::GR64RegClass, true);
Dale Johannesena99e3842008-08-20 00:48:50 +000012871 case X86::ATOMMIN64:
12872 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12873 case X86::ATOMMAX64:
12874 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12875 case X86::ATOMUMIN64:
12876 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12877 case X86::ATOMUMAX64:
12878 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012879
12880 // This group does 64-bit operations on a 32-bit host.
12881 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012882 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012883 X86::AND32rr, X86::AND32rr,
12884 X86::AND32ri, X86::AND32ri,
12885 false);
12886 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012887 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012888 X86::OR32rr, X86::OR32rr,
12889 X86::OR32ri, X86::OR32ri,
12890 false);
12891 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012892 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012893 X86::XOR32rr, X86::XOR32rr,
12894 X86::XOR32ri, X86::XOR32ri,
12895 false);
12896 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012897 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012898 X86::AND32rr, X86::AND32rr,
12899 X86::AND32ri, X86::AND32ri,
12900 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012901 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012902 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012903 X86::ADD32rr, X86::ADC32rr,
12904 X86::ADD32ri, X86::ADC32ri,
12905 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012906 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012907 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012908 X86::SUB32rr, X86::SBB32rr,
12909 X86::SUB32ri, X86::SBB32ri,
12910 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012911 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012912 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012913 X86::MOV32rr, X86::MOV32rr,
12914 X86::MOV32ri, X86::MOV32ri,
12915 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012916 case X86::VASTART_SAVE_XMM_REGS:
12917 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012918
12919 case X86::VAARG_64:
12920 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012921 }
12922}
12923
12924//===----------------------------------------------------------------------===//
12925// X86 Optimization Hooks
12926//===----------------------------------------------------------------------===//
12927
Dan Gohman475871a2008-07-27 21:46:04 +000012928void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012929 APInt &KnownZero,
12930 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012931 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012932 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000012933 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012934 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012935 assert((Opc >= ISD::BUILTIN_OP_END ||
12936 Opc == ISD::INTRINSIC_WO_CHAIN ||
12937 Opc == ISD::INTRINSIC_W_CHAIN ||
12938 Opc == ISD::INTRINSIC_VOID) &&
12939 "Should use MaskedValueIsZero if you don't know whether Op"
12940 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012941
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000012942 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012943 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012944 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012945 case X86ISD::ADD:
12946 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012947 case X86ISD::ADC:
12948 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012949 case X86ISD::SMUL:
12950 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012951 case X86ISD::INC:
12952 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012953 case X86ISD::OR:
12954 case X86ISD::XOR:
12955 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012956 // These nodes' second result is a boolean.
12957 if (Op.getResNo() == 0)
12958 break;
12959 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012960 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000012961 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012962 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012963 case ISD::INTRINSIC_WO_CHAIN: {
12964 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12965 unsigned NumLoBits = 0;
12966 switch (IntId) {
12967 default: break;
12968 case Intrinsic::x86_sse_movmsk_ps:
12969 case Intrinsic::x86_avx_movmsk_ps_256:
12970 case Intrinsic::x86_sse2_movmsk_pd:
12971 case Intrinsic::x86_avx_movmsk_pd_256:
12972 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000012973 case Intrinsic::x86_sse2_pmovmskb_128:
12974 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000012975 // High bits of movmskp{s|d}, pmovmskb are known zero.
12976 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000012977 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000012978 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12979 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12980 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12981 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12982 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12983 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000012984 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012985 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000012986 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000012987 break;
12988 }
12989 }
12990 break;
12991 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012992 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012993}
Chris Lattner259e97c2006-01-31 19:43:35 +000012994
Owen Andersonbc146b02010-09-21 20:42:50 +000012995unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12996 unsigned Depth) const {
12997 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12998 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12999 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013000
Owen Andersonbc146b02010-09-21 20:42:50 +000013001 // Fallback case.
13002 return 1;
13003}
13004
Evan Cheng206ee9d2006-07-07 08:33:52 +000013005/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013006/// node is a GlobalAddress + offset.
13007bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013008 const GlobalValue* &GA,
13009 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013010 if (N->getOpcode() == X86ISD::Wrapper) {
13011 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013012 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013013 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013014 return true;
13015 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013016 }
Evan Chengad4196b2008-05-12 19:56:52 +000013017 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013018}
13019
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013020/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13021/// same as extracting the high 128-bit part of 256-bit vector and then
13022/// inserting the result into the low part of a new 256-bit vector
13023static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13024 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013025 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013026
13027 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013028 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013029 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13030 SVOp->getMaskElt(j) >= 0)
13031 return false;
13032
13033 return true;
13034}
13035
13036/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13037/// same as extracting the low 128-bit part of 256-bit vector and then
13038/// inserting the result into the high part of a new 256-bit vector
13039static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13040 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013041 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013042
13043 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013044 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013045 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13046 SVOp->getMaskElt(j) >= 0)
13047 return false;
13048
13049 return true;
13050}
13051
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013052/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13053static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013054 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013055 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013056 DebugLoc dl = N->getDebugLoc();
13057 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13058 SDValue V1 = SVOp->getOperand(0);
13059 SDValue V2 = SVOp->getOperand(1);
13060 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013061 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013062
13063 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13064 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13065 //
13066 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013067 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013068 // V UNDEF BUILD_VECTOR UNDEF
13069 // \ / \ /
13070 // CONCAT_VECTOR CONCAT_VECTOR
13071 // \ /
13072 // \ /
13073 // RESULT: V + zero extended
13074 //
13075 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13076 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13077 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13078 return SDValue();
13079
13080 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13081 return SDValue();
13082
13083 // To match the shuffle mask, the first half of the mask should
13084 // be exactly the first vector, and all the rest a splat with the
13085 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013086 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013087 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13088 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13089 return SDValue();
13090
Chad Rosier3d1161e2012-01-03 21:05:52 +000013091 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13092 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013093 if (Ld->hasNUsesOfValue(1, 0)) {
13094 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13095 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13096 SDValue ResNode =
13097 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13098 Ld->getMemoryVT(),
13099 Ld->getPointerInfo(),
13100 Ld->getAlignment(),
13101 false/*isVolatile*/, true/*ReadMem*/,
13102 false/*WriteMem*/);
13103 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13104 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013105 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013106
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013107 // Emit a zeroed vector and insert the desired subvector on its
13108 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013109 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013110 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013111 return DCI.CombineTo(N, InsV);
13112 }
13113
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013114 //===--------------------------------------------------------------------===//
13115 // Combine some shuffles into subvector extracts and inserts:
13116 //
13117
13118 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13119 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013120 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13121 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013122 return DCI.CombineTo(N, InsV);
13123 }
13124
13125 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13126 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013127 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13128 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013129 return DCI.CombineTo(N, InsV);
13130 }
13131
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013132 return SDValue();
13133}
13134
13135/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013136static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013137 TargetLowering::DAGCombinerInfo &DCI,
13138 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013139 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013140 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013141
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013142 // Don't create instructions with illegal types after legalize types has run.
13143 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13144 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13145 return SDValue();
13146
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013147 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
13148 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
13149 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013150 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013151
13152 // Only handle 128 wide vector from here on.
13153 if (VT.getSizeInBits() != 128)
13154 return SDValue();
13155
13156 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13157 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13158 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013159 SmallVector<SDValue, 16> Elts;
13160 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013161 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013162
Nate Begemanfdea31a2010-03-24 20:49:50 +000013163 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013164}
Evan Chengd880b972008-05-09 21:53:03 +000013165
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013166
Craig Topperc16f8512012-04-25 06:39:39 +000013167/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013168/// a sequence of vector shuffle operations.
13169/// It is possible when we truncate 256-bit vector to 128-bit vector
13170
Chad Rosiera20e1e72012-08-01 18:39:17 +000013171SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013172 DAGCombinerInfo &DCI) const {
13173 if (!DCI.isBeforeLegalizeOps())
13174 return SDValue();
13175
Craig Topper3ef43cf2012-04-24 06:36:35 +000013176 if (!Subtarget->hasAVX())
13177 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013178
13179 EVT VT = N->getValueType(0);
13180 SDValue Op = N->getOperand(0);
13181 EVT OpVT = Op.getValueType();
13182 DebugLoc dl = N->getDebugLoc();
13183
13184 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13185
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013186 if (Subtarget->hasAVX2()) {
13187 // AVX2: v4i64 -> v4i32
13188
13189 // VPERMD
13190 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13191
13192 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13193 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13194 ShufMask);
13195
Craig Topperd63fa652012-04-22 18:51:37 +000013196 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13197 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013198 }
13199
13200 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013201 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013202 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013203
13204 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013205 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013206
13207 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13208 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13209
13210 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013211 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013212
Craig Topperd63fa652012-04-22 18:51:37 +000013213 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, DAG.getUNDEF(VT), ShufMask1);
13214 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, DAG.getUNDEF(VT), ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013215
13216 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013217 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013218
Elena Demikhovsky73252572012-02-01 10:33:05 +000013219 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013220 }
Craig Topperd63fa652012-04-22 18:51:37 +000013221
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013222 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13223
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013224 if (Subtarget->hasAVX2()) {
13225 // AVX2: v8i32 -> v8i16
13226
13227 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013228
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013229 // PSHUFB
13230 SmallVector<SDValue,32> pshufbMask;
13231 for (unsigned i = 0; i < 2; ++i) {
13232 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13233 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13234 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13235 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13236 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13237 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13238 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13239 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13240 for (unsigned j = 0; j < 8; ++j)
13241 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13242 }
Craig Topperd63fa652012-04-22 18:51:37 +000013243 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13244 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013245 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13246
13247 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13248
13249 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013250 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013251 &ShufMask[0]);
13252
Craig Topperd63fa652012-04-22 18:51:37 +000013253 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13254 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013255
13256 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13257 }
13258
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013259 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013260 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013261
13262 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013263 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013264
13265 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13266 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13267
13268 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013269 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13270 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013271
Craig Topperd63fa652012-04-22 18:51:37 +000013272 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, DAG.getUNDEF(MVT::v16i8),
Elena Demikhovsky73252572012-02-01 10:33:05 +000013273 ShufMask1);
Craig Topperd63fa652012-04-22 18:51:37 +000013274 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, DAG.getUNDEF(MVT::v16i8),
Elena Demikhovsky73252572012-02-01 10:33:05 +000013275 ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013276
13277 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13278 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13279
13280 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013281 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013282
Elena Demikhovsky73252572012-02-01 10:33:05 +000013283 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013284 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013285 }
13286
13287 return SDValue();
13288}
13289
Craig Topper89f4e662012-03-20 07:17:59 +000013290/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13291/// specific shuffle of a load can be folded into a single element load.
13292/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13293/// shuffles have been customed lowered so we need to handle those here.
13294static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13295 TargetLowering::DAGCombinerInfo &DCI) {
13296 if (DCI.isBeforeLegalizeOps())
13297 return SDValue();
13298
13299 SDValue InVec = N->getOperand(0);
13300 SDValue EltNo = N->getOperand(1);
13301
13302 if (!isa<ConstantSDNode>(EltNo))
13303 return SDValue();
13304
13305 EVT VT = InVec.getValueType();
13306
13307 bool HasShuffleIntoBitcast = false;
13308 if (InVec.getOpcode() == ISD::BITCAST) {
13309 // Don't duplicate a load with other uses.
13310 if (!InVec.hasOneUse())
13311 return SDValue();
13312 EVT BCVT = InVec.getOperand(0).getValueType();
13313 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13314 return SDValue();
13315 InVec = InVec.getOperand(0);
13316 HasShuffleIntoBitcast = true;
13317 }
13318
13319 if (!isTargetShuffle(InVec.getOpcode()))
13320 return SDValue();
13321
13322 // Don't duplicate a load with other uses.
13323 if (!InVec.hasOneUse())
13324 return SDValue();
13325
13326 SmallVector<int, 16> ShuffleMask;
13327 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013328 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13329 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013330 return SDValue();
13331
13332 // Select the input vector, guarding against out of range extract vector.
13333 unsigned NumElems = VT.getVectorNumElements();
13334 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13335 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13336 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13337 : InVec.getOperand(1);
13338
13339 // If inputs to shuffle are the same for both ops, then allow 2 uses
13340 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13341
13342 if (LdNode.getOpcode() == ISD::BITCAST) {
13343 // Don't duplicate a load with other uses.
13344 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13345 return SDValue();
13346
13347 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13348 LdNode = LdNode.getOperand(0);
13349 }
13350
13351 if (!ISD::isNormalLoad(LdNode.getNode()))
13352 return SDValue();
13353
13354 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13355
13356 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13357 return SDValue();
13358
13359 if (HasShuffleIntoBitcast) {
13360 // If there's a bitcast before the shuffle, check if the load type and
13361 // alignment is valid.
13362 unsigned Align = LN0->getAlignment();
13363 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13364 unsigned NewAlign = TLI.getTargetData()->
13365 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13366
13367 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13368 return SDValue();
13369 }
13370
13371 // All checks match so transform back to vector_shuffle so that DAG combiner
13372 // can finish the job
13373 DebugLoc dl = N->getDebugLoc();
13374
13375 // Create shuffle node taking into account the case that its a unary shuffle
13376 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13377 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13378 InVec.getOperand(0), Shuffle,
13379 &ShuffleMask[0]);
13380 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13381 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13382 EltNo);
13383}
13384
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013385/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13386/// generation and convert it from being a bunch of shuffles and extracts
13387/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013388static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013389 TargetLowering::DAGCombinerInfo &DCI) {
13390 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13391 if (NewOp.getNode())
13392 return NewOp;
13393
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013394 SDValue InputVector = N->getOperand(0);
13395
13396 // Only operate on vectors of 4 elements, where the alternative shuffling
13397 // gets to be more expensive.
13398 if (InputVector.getValueType() != MVT::v4i32)
13399 return SDValue();
13400
13401 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13402 // single use which is a sign-extend or zero-extend, and all elements are
13403 // used.
13404 SmallVector<SDNode *, 4> Uses;
13405 unsigned ExtractedElements = 0;
13406 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13407 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13408 if (UI.getUse().getResNo() != InputVector.getResNo())
13409 return SDValue();
13410
13411 SDNode *Extract = *UI;
13412 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13413 return SDValue();
13414
13415 if (Extract->getValueType(0) != MVT::i32)
13416 return SDValue();
13417 if (!Extract->hasOneUse())
13418 return SDValue();
13419 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13420 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13421 return SDValue();
13422 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13423 return SDValue();
13424
13425 // Record which element was extracted.
13426 ExtractedElements |=
13427 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13428
13429 Uses.push_back(Extract);
13430 }
13431
13432 // If not all the elements were used, this may not be worthwhile.
13433 if (ExtractedElements != 15)
13434 return SDValue();
13435
13436 // Ok, we've now decided to do the transformation.
13437 DebugLoc dl = InputVector.getDebugLoc();
13438
13439 // Store the value to a temporary stack slot.
13440 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013441 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13442 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013443
13444 // Replace each use (extract) with a load of the appropriate element.
13445 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13446 UE = Uses.end(); UI != UE; ++UI) {
13447 SDNode *Extract = *UI;
13448
Nadav Rotem86694292011-05-17 08:31:57 +000013449 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013450 SDValue Idx = Extract->getOperand(1);
13451 unsigned EltSize =
13452 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13453 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013454 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013455 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13456
Nadav Rotem86694292011-05-17 08:31:57 +000013457 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013458 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013459
13460 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013461 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013462 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013463 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013464
13465 // Replace the exact with the load.
13466 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13467 }
13468
13469 // The replacement was made in place; don't return anything.
13470 return SDValue();
13471}
13472
Duncan Sands6bcd2192011-09-17 16:49:39 +000013473/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13474/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013475static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013476 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013477 const X86Subtarget *Subtarget) {
13478 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013479 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013480 // Get the LHS/RHS of the select.
13481 SDValue LHS = N->getOperand(1);
13482 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013483 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013484
Dan Gohman670e5392009-09-21 18:03:22 +000013485 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013486 // instructions match the semantics of the common C idiom x<y?x:y but not
13487 // x<=y?x:y, because of how they handle negative zero (which can be
13488 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013489 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13490 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013491 (Subtarget->hasSSE2() ||
13492 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013493 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013494
Chris Lattner47b4ce82009-03-11 05:48:52 +000013495 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013496 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013497 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13498 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013499 switch (CC) {
13500 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013501 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013502 // Converting this to a min would handle NaNs incorrectly, and swapping
13503 // the operands would cause it to handle comparisons between positive
13504 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013505 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013506 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013507 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13508 break;
13509 std::swap(LHS, RHS);
13510 }
Dan Gohman670e5392009-09-21 18:03:22 +000013511 Opcode = X86ISD::FMIN;
13512 break;
13513 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013514 // Converting this to a min would handle comparisons between positive
13515 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013516 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013517 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13518 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013519 Opcode = X86ISD::FMIN;
13520 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013521 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013522 // Converting this to a min would handle both negative zeros and NaNs
13523 // incorrectly, but we can swap the operands to fix both.
13524 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013525 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013526 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013527 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013528 Opcode = X86ISD::FMIN;
13529 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013530
Dan Gohman670e5392009-09-21 18:03:22 +000013531 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013532 // Converting this to a max would handle comparisons between positive
13533 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013534 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013535 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013536 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013537 Opcode = X86ISD::FMAX;
13538 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013539 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013540 // Converting this to a max would handle NaNs incorrectly, and swapping
13541 // the operands would cause it to handle comparisons between positive
13542 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013543 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013544 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013545 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13546 break;
13547 std::swap(LHS, RHS);
13548 }
Dan Gohman670e5392009-09-21 18:03:22 +000013549 Opcode = X86ISD::FMAX;
13550 break;
13551 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013552 // Converting this to a max would handle both negative zeros and NaNs
13553 // incorrectly, but we can swap the operands to fix both.
13554 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013555 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013556 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013557 case ISD::SETGE:
13558 Opcode = X86ISD::FMAX;
13559 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013560 }
Dan Gohman670e5392009-09-21 18:03:22 +000013561 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013562 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13563 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013564 switch (CC) {
13565 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013566 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013567 // Converting this to a min would handle comparisons between positive
13568 // and negative zero incorrectly, and swapping the operands would
13569 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013570 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013571 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013572 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013573 break;
13574 std::swap(LHS, RHS);
13575 }
Dan Gohman670e5392009-09-21 18:03:22 +000013576 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013577 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013578 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013579 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013580 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013581 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13582 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013583 Opcode = X86ISD::FMIN;
13584 break;
13585 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013586 // Converting this to a min would handle both negative zeros and NaNs
13587 // incorrectly, but we can swap the operands to fix both.
13588 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013589 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013590 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013591 case ISD::SETGE:
13592 Opcode = X86ISD::FMIN;
13593 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013594
Dan Gohman670e5392009-09-21 18:03:22 +000013595 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013596 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013597 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013598 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013599 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013600 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013601 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013602 // Converting this to a max would handle comparisons between positive
13603 // and negative zero incorrectly, and swapping the operands would
13604 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013605 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013606 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013607 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013608 break;
13609 std::swap(LHS, RHS);
13610 }
Dan Gohman670e5392009-09-21 18:03:22 +000013611 Opcode = X86ISD::FMAX;
13612 break;
13613 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013614 // Converting this to a max would handle both negative zeros and NaNs
13615 // incorrectly, but we can swap the operands to fix both.
13616 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013617 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013618 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013619 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013620 Opcode = X86ISD::FMAX;
13621 break;
13622 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013623 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013624
Chris Lattner47b4ce82009-03-11 05:48:52 +000013625 if (Opcode)
13626 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013627 }
Eric Christopherfd179292009-08-27 18:07:15 +000013628
Chris Lattnerd1980a52009-03-12 06:52:53 +000013629 // If this is a select between two integer constants, try to do some
13630 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013631 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13632 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013633 // Don't do this for crazy integer types.
13634 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13635 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013636 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013637 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013638
Chris Lattnercee56e72009-03-13 05:53:31 +000013639 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013640 // Efficiently invertible.
13641 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13642 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13643 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13644 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013645 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013646 }
Eric Christopherfd179292009-08-27 18:07:15 +000013647
Chris Lattnerd1980a52009-03-12 06:52:53 +000013648 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013649 if (FalseC->getAPIntValue() == 0 &&
13650 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013651 if (NeedsCondInvert) // Invert the condition if needed.
13652 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13653 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013654
Chris Lattnerd1980a52009-03-12 06:52:53 +000013655 // Zero extend the condition if needed.
13656 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013657
Chris Lattnercee56e72009-03-13 05:53:31 +000013658 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013659 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013660 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013661 }
Eric Christopherfd179292009-08-27 18:07:15 +000013662
Chris Lattner97a29a52009-03-13 05:22:11 +000013663 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013664 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013665 if (NeedsCondInvert) // Invert the condition if needed.
13666 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13667 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013668
Chris Lattner97a29a52009-03-13 05:22:11 +000013669 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013670 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13671 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013672 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013673 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013674 }
Eric Christopherfd179292009-08-27 18:07:15 +000013675
Chris Lattnercee56e72009-03-13 05:53:31 +000013676 // Optimize cases that will turn into an LEA instruction. This requires
13677 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013678 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013679 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013680 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013681
Chris Lattnercee56e72009-03-13 05:53:31 +000013682 bool isFastMultiplier = false;
13683 if (Diff < 10) {
13684 switch ((unsigned char)Diff) {
13685 default: break;
13686 case 1: // result = add base, cond
13687 case 2: // result = lea base( , cond*2)
13688 case 3: // result = lea base(cond, cond*2)
13689 case 4: // result = lea base( , cond*4)
13690 case 5: // result = lea base(cond, cond*4)
13691 case 8: // result = lea base( , cond*8)
13692 case 9: // result = lea base(cond, cond*8)
13693 isFastMultiplier = true;
13694 break;
13695 }
13696 }
Eric Christopherfd179292009-08-27 18:07:15 +000013697
Chris Lattnercee56e72009-03-13 05:53:31 +000013698 if (isFastMultiplier) {
13699 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13700 if (NeedsCondInvert) // Invert the condition if needed.
13701 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13702 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013703
Chris Lattnercee56e72009-03-13 05:53:31 +000013704 // Zero extend the condition if needed.
13705 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13706 Cond);
13707 // Scale the condition by the difference.
13708 if (Diff != 1)
13709 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13710 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013711
Chris Lattnercee56e72009-03-13 05:53:31 +000013712 // Add the base if non-zero.
13713 if (FalseC->getAPIntValue() != 0)
13714 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13715 SDValue(FalseC, 0));
13716 return Cond;
13717 }
Eric Christopherfd179292009-08-27 18:07:15 +000013718 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013719 }
13720 }
Eric Christopherfd179292009-08-27 18:07:15 +000013721
Evan Cheng56f582d2012-01-04 01:41:39 +000013722 // Canonicalize max and min:
13723 // (x > y) ? x : y -> (x >= y) ? x : y
13724 // (x < y) ? x : y -> (x <= y) ? x : y
13725 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
13726 // the need for an extra compare
13727 // against zero. e.g.
13728 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
13729 // subl %esi, %edi
13730 // testl %edi, %edi
13731 // movl $0, %eax
13732 // cmovgl %edi, %eax
13733 // =>
13734 // xorl %eax, %eax
13735 // subl %esi, $edi
13736 // cmovsl %eax, %edi
13737 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
13738 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13739 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
13740 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
13741 switch (CC) {
13742 default: break;
13743 case ISD::SETLT:
13744 case ISD::SETGT: {
13745 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
13746 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
13747 Cond.getOperand(0), Cond.getOperand(1), NewCC);
13748 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
13749 }
13750 }
13751 }
13752
Nadav Rotemcc616562012-01-15 19:27:55 +000013753 // If we know that this node is legal then we know that it is going to be
13754 // matched by one of the SSE/AVX BLEND instructions. These instructions only
13755 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
13756 // to simplify previous instructions.
13757 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13758 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000013759 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000013760 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000013761
13762 // Don't optimize vector selects that map to mask-registers.
13763 if (BitWidth == 1)
13764 return SDValue();
13765
Nadav Rotemcc616562012-01-15 19:27:55 +000013766 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
13767 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
13768
13769 APInt KnownZero, KnownOne;
13770 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
13771 DCI.isBeforeLegalizeOps());
13772 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
13773 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
13774 DCI.CommitTargetLoweringOpt(TLO);
13775 }
13776
Dan Gohman475871a2008-07-27 21:46:04 +000013777 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013778}
13779
Michael Liao2a33cec2012-08-10 19:58:13 +000013780// Check whether a boolean test is testing a boolean value generated by
13781// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
13782// code.
13783//
13784// Simplify the following patterns:
13785// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
13786// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
13787// to (Op EFLAGS Cond)
13788//
13789// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
13790// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
13791// to (Op EFLAGS !Cond)
13792//
13793// where Op could be BRCOND or CMOV.
13794//
13795static SDValue BoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
13796 // Quit if not CMP and SUB with its value result used.
13797 if (Cmp.getOpcode() != X86ISD::CMP &&
13798 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
13799 return SDValue();
13800
13801 // Quit if not used as a boolean value.
13802 if (CC != X86::COND_E && CC != X86::COND_NE)
13803 return SDValue();
13804
13805 // Check CMP operands. One of them should be 0 or 1 and the other should be
13806 // an SetCC or extended from it.
13807 SDValue Op1 = Cmp.getOperand(0);
13808 SDValue Op2 = Cmp.getOperand(1);
13809
13810 SDValue SetCC;
13811 const ConstantSDNode* C = 0;
13812 bool needOppositeCond = (CC == X86::COND_E);
13813
13814 if ((C = dyn_cast<ConstantSDNode>(Op1)))
13815 SetCC = Op2;
13816 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
13817 SetCC = Op1;
13818 else // Quit if all operands are not constants.
13819 return SDValue();
13820
13821 if (C->getZExtValue() == 1)
13822 needOppositeCond = !needOppositeCond;
13823 else if (C->getZExtValue() != 0)
13824 // Quit if the constant is neither 0 or 1.
13825 return SDValue();
13826
13827 // Skip 'zext' node.
13828 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
13829 SetCC = SetCC.getOperand(0);
13830
13831 // Quit if not SETCC.
13832 // FIXME: So far we only handle the boolean value generated from SETCC. If
13833 // there is other ways to generate boolean values, we need handle them here
13834 // as well.
13835 if (SetCC.getOpcode() != X86ISD::SETCC)
13836 return SDValue();
13837
13838 // Set the condition code or opposite one if necessary.
13839 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
13840 if (needOppositeCond)
13841 CC = X86::GetOppositeBranchCondition(CC);
13842
13843 return SetCC.getOperand(1);
13844}
13845
Michael Liao9eac20a2012-08-11 23:47:06 +000013846static bool IsValidFCMOVCondition(X86::CondCode CC) {
13847 switch (CC) {
13848 default:
13849 return false;
13850 case X86::COND_B:
13851 case X86::COND_BE:
13852 case X86::COND_E:
13853 case X86::COND_P:
13854 case X86::COND_AE:
13855 case X86::COND_A:
13856 case X86::COND_NE:
13857 case X86::COND_NP:
13858 return true;
13859 }
13860}
13861
Chris Lattnerd1980a52009-03-12 06:52:53 +000013862/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13863static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13864 TargetLowering::DAGCombinerInfo &DCI) {
13865 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013866
Chris Lattnerd1980a52009-03-12 06:52:53 +000013867 // If the flag operand isn't dead, don't touch this CMOV.
13868 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13869 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013870
Evan Chengb5a55d92011-05-24 01:48:22 +000013871 SDValue FalseOp = N->getOperand(0);
13872 SDValue TrueOp = N->getOperand(1);
13873 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13874 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000013875
Evan Chengb5a55d92011-05-24 01:48:22 +000013876 if (CC == X86::COND_E || CC == X86::COND_NE) {
13877 switch (Cond.getOpcode()) {
13878 default: break;
13879 case X86ISD::BSR:
13880 case X86ISD::BSF:
13881 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13882 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13883 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13884 }
13885 }
13886
Michael Liao2a33cec2012-08-10 19:58:13 +000013887 SDValue Flags;
13888
13889 Flags = BoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000013890 if (Flags.getNode() &&
13891 // Extra check as FCMOV only supports a subset of X86 cond.
13892 (FalseOp.getValueType() != MVT::f80 || IsValidFCMOVCondition(CC))) {
Michael Liao2a33cec2012-08-10 19:58:13 +000013893 SDValue Ops[] = { FalseOp, TrueOp,
13894 DAG.getConstant(CC, MVT::i8), Flags };
13895 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
13896 Ops, array_lengthof(Ops));
13897 }
13898
Chris Lattnerd1980a52009-03-12 06:52:53 +000013899 // If this is a select between two integer constants, try to do some
13900 // optimizations. Note that the operands are ordered the opposite of SELECT
13901 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013902 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13903 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013904 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13905 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013906 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13907 CC = X86::GetOppositeBranchCondition(CC);
13908 std::swap(TrueC, FalseC);
13909 }
Eric Christopherfd179292009-08-27 18:07:15 +000013910
Chris Lattnerd1980a52009-03-12 06:52:53 +000013911 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013912 // This is efficient for any integer data type (including i8/i16) and
13913 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013914 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013915 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13916 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013917
Chris Lattnerd1980a52009-03-12 06:52:53 +000013918 // Zero extend the condition if needed.
13919 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013920
Chris Lattnerd1980a52009-03-12 06:52:53 +000013921 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13922 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013923 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013924 if (N->getNumValues() == 2) // Dead flag value?
13925 return DCI.CombineTo(N, Cond, SDValue());
13926 return Cond;
13927 }
Eric Christopherfd179292009-08-27 18:07:15 +000013928
Chris Lattnercee56e72009-03-13 05:53:31 +000013929 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13930 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013931 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013932 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13933 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013934
Chris Lattner97a29a52009-03-13 05:22:11 +000013935 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013936 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13937 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013938 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13939 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013940
Chris Lattner97a29a52009-03-13 05:22:11 +000013941 if (N->getNumValues() == 2) // Dead flag value?
13942 return DCI.CombineTo(N, Cond, SDValue());
13943 return Cond;
13944 }
Eric Christopherfd179292009-08-27 18:07:15 +000013945
Chris Lattnercee56e72009-03-13 05:53:31 +000013946 // Optimize cases that will turn into an LEA instruction. This requires
13947 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013948 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013949 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013950 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013951
Chris Lattnercee56e72009-03-13 05:53:31 +000013952 bool isFastMultiplier = false;
13953 if (Diff < 10) {
13954 switch ((unsigned char)Diff) {
13955 default: break;
13956 case 1: // result = add base, cond
13957 case 2: // result = lea base( , cond*2)
13958 case 3: // result = lea base(cond, cond*2)
13959 case 4: // result = lea base( , cond*4)
13960 case 5: // result = lea base(cond, cond*4)
13961 case 8: // result = lea base( , cond*8)
13962 case 9: // result = lea base(cond, cond*8)
13963 isFastMultiplier = true;
13964 break;
13965 }
13966 }
Eric Christopherfd179292009-08-27 18:07:15 +000013967
Chris Lattnercee56e72009-03-13 05:53:31 +000013968 if (isFastMultiplier) {
13969 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013970 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13971 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013972 // Zero extend the condition if needed.
13973 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13974 Cond);
13975 // Scale the condition by the difference.
13976 if (Diff != 1)
13977 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13978 DAG.getConstant(Diff, Cond.getValueType()));
13979
13980 // Add the base if non-zero.
13981 if (FalseC->getAPIntValue() != 0)
13982 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13983 SDValue(FalseC, 0));
13984 if (N->getNumValues() == 2) // Dead flag value?
13985 return DCI.CombineTo(N, Cond, SDValue());
13986 return Cond;
13987 }
Eric Christopherfd179292009-08-27 18:07:15 +000013988 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013989 }
13990 }
13991 return SDValue();
13992}
13993
13994
Evan Cheng0b0cd912009-03-28 05:57:29 +000013995/// PerformMulCombine - Optimize a single multiply with constant into two
13996/// in order to implement it with two cheaper instructions, e.g.
13997/// LEA + SHL, LEA + LEA.
13998static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13999 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014000 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14001 return SDValue();
14002
Owen Andersone50ed302009-08-10 22:56:29 +000014003 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014004 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014005 return SDValue();
14006
14007 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14008 if (!C)
14009 return SDValue();
14010 uint64_t MulAmt = C->getZExtValue();
14011 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14012 return SDValue();
14013
14014 uint64_t MulAmt1 = 0;
14015 uint64_t MulAmt2 = 0;
14016 if ((MulAmt % 9) == 0) {
14017 MulAmt1 = 9;
14018 MulAmt2 = MulAmt / 9;
14019 } else if ((MulAmt % 5) == 0) {
14020 MulAmt1 = 5;
14021 MulAmt2 = MulAmt / 5;
14022 } else if ((MulAmt % 3) == 0) {
14023 MulAmt1 = 3;
14024 MulAmt2 = MulAmt / 3;
14025 }
14026 if (MulAmt2 &&
14027 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14028 DebugLoc DL = N->getDebugLoc();
14029
14030 if (isPowerOf2_64(MulAmt2) &&
14031 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14032 // If second multiplifer is pow2, issue it first. We want the multiply by
14033 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14034 // is an add.
14035 std::swap(MulAmt1, MulAmt2);
14036
14037 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014038 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014039 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014040 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014041 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014042 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014043 DAG.getConstant(MulAmt1, VT));
14044
Eric Christopherfd179292009-08-27 18:07:15 +000014045 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014046 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014047 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014048 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014049 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014050 DAG.getConstant(MulAmt2, VT));
14051
14052 // Do not add new nodes to DAG combiner worklist.
14053 DCI.CombineTo(N, NewMul, false);
14054 }
14055 return SDValue();
14056}
14057
Evan Chengad9c0a32009-12-15 00:53:42 +000014058static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14059 SDValue N0 = N->getOperand(0);
14060 SDValue N1 = N->getOperand(1);
14061 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14062 EVT VT = N0.getValueType();
14063
14064 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14065 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014066 if (VT.isInteger() && !VT.isVector() &&
14067 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014068 N0.getOperand(1).getOpcode() == ISD::Constant) {
14069 SDValue N00 = N0.getOperand(0);
14070 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14071 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14072 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14073 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14074 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14075 APInt ShAmt = N1C->getAPIntValue();
14076 Mask = Mask.shl(ShAmt);
14077 if (Mask != 0)
14078 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14079 N00, DAG.getConstant(Mask, VT));
14080 }
14081 }
14082
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014083
14084 // Hardware support for vector shifts is sparse which makes us scalarize the
14085 // vector operations in many cases. Also, on sandybridge ADD is faster than
14086 // shl.
14087 // (shl V, 1) -> add V,V
14088 if (isSplatVector(N1.getNode())) {
14089 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14090 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14091 // We shift all of the values by one. In many cases we do not have
14092 // hardware support for this operation. This is better expressed as an ADD
14093 // of two values.
14094 if (N1C && (1 == N1C->getZExtValue())) {
14095 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14096 }
14097 }
14098
Evan Chengad9c0a32009-12-15 00:53:42 +000014099 return SDValue();
14100}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014101
Nate Begeman740ab032009-01-26 00:52:55 +000014102/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14103/// when possible.
14104static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014105 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014106 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014107 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014108 if (N->getOpcode() == ISD::SHL) {
14109 SDValue V = PerformSHLCombine(N, DAG);
14110 if (V.getNode()) return V;
14111 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014112
Nate Begeman740ab032009-01-26 00:52:55 +000014113 // On X86 with SSE2 support, we can transform this to a vector shift if
14114 // all elements are shifted by the same amount. We can't do this in legalize
14115 // because the a constant vector is typically transformed to a constant pool
14116 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014117 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014118 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014119
Craig Topper7be5dfd2011-11-12 09:58:49 +000014120 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14121 (!Subtarget->hasAVX2() ||
14122 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014123 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014124
Mon P Wang3becd092009-01-28 08:12:05 +000014125 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014126 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014127 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014128 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014129 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14130 unsigned NumElts = VT.getVectorNumElements();
14131 unsigned i = 0;
14132 for (; i != NumElts; ++i) {
14133 SDValue Arg = ShAmtOp.getOperand(i);
14134 if (Arg.getOpcode() == ISD::UNDEF) continue;
14135 BaseShAmt = Arg;
14136 break;
14137 }
Craig Topper37c26772012-01-17 04:44:50 +000014138 // Handle the case where the build_vector is all undef
14139 // FIXME: Should DAG allow this?
14140 if (i == NumElts)
14141 return SDValue();
14142
Mon P Wang3becd092009-01-28 08:12:05 +000014143 for (; i != NumElts; ++i) {
14144 SDValue Arg = ShAmtOp.getOperand(i);
14145 if (Arg.getOpcode() == ISD::UNDEF) continue;
14146 if (Arg != BaseShAmt) {
14147 return SDValue();
14148 }
14149 }
14150 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014151 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014152 SDValue InVec = ShAmtOp.getOperand(0);
14153 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14154 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14155 unsigned i = 0;
14156 for (; i != NumElts; ++i) {
14157 SDValue Arg = InVec.getOperand(i);
14158 if (Arg.getOpcode() == ISD::UNDEF) continue;
14159 BaseShAmt = Arg;
14160 break;
14161 }
14162 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14163 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014164 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014165 if (C->getZExtValue() == SplatIdx)
14166 BaseShAmt = InVec.getOperand(1);
14167 }
14168 }
Mon P Wang845b1892012-02-01 22:15:20 +000014169 if (BaseShAmt.getNode() == 0) {
14170 // Don't create instructions with illegal types after legalize
14171 // types has run.
14172 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14173 !DCI.isBeforeLegalize())
14174 return SDValue();
14175
Mon P Wangefa42202009-09-03 19:56:25 +000014176 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14177 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014178 }
Mon P Wang3becd092009-01-28 08:12:05 +000014179 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014180 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014181
Mon P Wangefa42202009-09-03 19:56:25 +000014182 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014183 if (EltVT.bitsGT(MVT::i32))
14184 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14185 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014186 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014187
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014188 // The shift amount is identical so we can do a vector shift.
14189 SDValue ValOp = N->getOperand(0);
14190 switch (N->getOpcode()) {
14191 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014192 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014193 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014194 switch (VT.getSimpleVT().SimpleTy) {
14195 default: return SDValue();
14196 case MVT::v2i64:
14197 case MVT::v4i32:
14198 case MVT::v8i16:
14199 case MVT::v4i64:
14200 case MVT::v8i32:
14201 case MVT::v16i16:
14202 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14203 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014204 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014205 switch (VT.getSimpleVT().SimpleTy) {
14206 default: return SDValue();
14207 case MVT::v4i32:
14208 case MVT::v8i16:
14209 case MVT::v8i32:
14210 case MVT::v16i16:
14211 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14212 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014213 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014214 switch (VT.getSimpleVT().SimpleTy) {
14215 default: return SDValue();
14216 case MVT::v2i64:
14217 case MVT::v4i32:
14218 case MVT::v8i16:
14219 case MVT::v4i64:
14220 case MVT::v8i32:
14221 case MVT::v16i16:
14222 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14223 }
Nate Begeman740ab032009-01-26 00:52:55 +000014224 }
Nate Begeman740ab032009-01-26 00:52:55 +000014225}
14226
Nate Begemanb65c1752010-12-17 22:55:37 +000014227
Stuart Hastings865f0932011-06-03 23:53:54 +000014228// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14229// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14230// and friends. Likewise for OR -> CMPNEQSS.
14231static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14232 TargetLowering::DAGCombinerInfo &DCI,
14233 const X86Subtarget *Subtarget) {
14234 unsigned opcode;
14235
14236 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14237 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014238 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014239 SDValue N0 = N->getOperand(0);
14240 SDValue N1 = N->getOperand(1);
14241 SDValue CMP0 = N0->getOperand(1);
14242 SDValue CMP1 = N1->getOperand(1);
14243 DebugLoc DL = N->getDebugLoc();
14244
14245 // The SETCCs should both refer to the same CMP.
14246 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14247 return SDValue();
14248
14249 SDValue CMP00 = CMP0->getOperand(0);
14250 SDValue CMP01 = CMP0->getOperand(1);
14251 EVT VT = CMP00.getValueType();
14252
14253 if (VT == MVT::f32 || VT == MVT::f64) {
14254 bool ExpectingFlags = false;
14255 // Check for any users that want flags:
14256 for (SDNode::use_iterator UI = N->use_begin(),
14257 UE = N->use_end();
14258 !ExpectingFlags && UI != UE; ++UI)
14259 switch (UI->getOpcode()) {
14260 default:
14261 case ISD::BR_CC:
14262 case ISD::BRCOND:
14263 case ISD::SELECT:
14264 ExpectingFlags = true;
14265 break;
14266 case ISD::CopyToReg:
14267 case ISD::SIGN_EXTEND:
14268 case ISD::ZERO_EXTEND:
14269 case ISD::ANY_EXTEND:
14270 break;
14271 }
14272
14273 if (!ExpectingFlags) {
14274 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14275 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14276
14277 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14278 X86::CondCode tmp = cc0;
14279 cc0 = cc1;
14280 cc1 = tmp;
14281 }
14282
14283 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14284 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14285 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14286 X86ISD::NodeType NTOperator = is64BitFP ?
14287 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14288 // FIXME: need symbolic constants for these magic numbers.
14289 // See X86ATTInstPrinter.cpp:printSSECC().
14290 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14291 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14292 DAG.getConstant(x86cc, MVT::i8));
14293 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14294 OnesOrZeroesF);
14295 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14296 DAG.getConstant(1, MVT::i32));
14297 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14298 return OneBitOfTruth;
14299 }
14300 }
14301 }
14302 }
14303 return SDValue();
14304}
14305
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014306/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14307/// so it can be folded inside ANDNP.
14308static bool CanFoldXORWithAllOnes(const SDNode *N) {
14309 EVT VT = N->getValueType(0);
14310
14311 // Match direct AllOnes for 128 and 256-bit vectors
14312 if (ISD::isBuildVectorAllOnes(N))
14313 return true;
14314
14315 // Look through a bit convert.
14316 if (N->getOpcode() == ISD::BITCAST)
14317 N = N->getOperand(0).getNode();
14318
14319 // Sometimes the operand may come from a insert_subvector building a 256-bit
14320 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014321 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000014322 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14323 SDValue V1 = N->getOperand(0);
14324 SDValue V2 = N->getOperand(1);
14325
14326 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14327 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14328 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14329 ISD::isBuildVectorAllOnes(V2.getNode()))
14330 return true;
14331 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014332
14333 return false;
14334}
14335
Nate Begemanb65c1752010-12-17 22:55:37 +000014336static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14337 TargetLowering::DAGCombinerInfo &DCI,
14338 const X86Subtarget *Subtarget) {
14339 if (DCI.isBeforeLegalizeOps())
14340 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014341
Stuart Hastings865f0932011-06-03 23:53:54 +000014342 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14343 if (R.getNode())
14344 return R;
14345
Craig Topper54a11172011-10-14 07:06:56 +000014346 EVT VT = N->getValueType(0);
14347
Craig Topperb4c94572011-10-21 06:55:01 +000014348 // Create ANDN, BLSI, and BLSR instructions
14349 // BLSI is X & (-X)
14350 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014351 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14352 SDValue N0 = N->getOperand(0);
14353 SDValue N1 = N->getOperand(1);
14354 DebugLoc DL = N->getDebugLoc();
14355
14356 // Check LHS for not
14357 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14358 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14359 // Check RHS for not
14360 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14361 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14362
Craig Topperb4c94572011-10-21 06:55:01 +000014363 // Check LHS for neg
14364 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14365 isZero(N0.getOperand(0)))
14366 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14367
14368 // Check RHS for neg
14369 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14370 isZero(N1.getOperand(0)))
14371 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14372
14373 // Check LHS for X-1
14374 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14375 isAllOnes(N0.getOperand(1)))
14376 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14377
14378 // Check RHS for X-1
14379 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14380 isAllOnes(N1.getOperand(1)))
14381 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14382
Craig Topper54a11172011-10-14 07:06:56 +000014383 return SDValue();
14384 }
14385
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014386 // Want to form ANDNP nodes:
14387 // 1) In the hopes of then easily combining them with OR and AND nodes
14388 // to form PBLEND/PSIGN.
14389 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014390 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014391 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014392
Nate Begemanb65c1752010-12-17 22:55:37 +000014393 SDValue N0 = N->getOperand(0);
14394 SDValue N1 = N->getOperand(1);
14395 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014396
Nate Begemanb65c1752010-12-17 22:55:37 +000014397 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014398 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014399 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14400 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014401 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014402
14403 // Check RHS for vnot
14404 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014405 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14406 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014407 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014408
Nate Begemanb65c1752010-12-17 22:55:37 +000014409 return SDValue();
14410}
14411
Evan Cheng760d1942010-01-04 21:22:48 +000014412static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014413 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014414 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014415 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014416 return SDValue();
14417
Stuart Hastings865f0932011-06-03 23:53:54 +000014418 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14419 if (R.getNode())
14420 return R;
14421
Evan Cheng760d1942010-01-04 21:22:48 +000014422 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014423
Evan Cheng760d1942010-01-04 21:22:48 +000014424 SDValue N0 = N->getOperand(0);
14425 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014426
Nate Begemanb65c1752010-12-17 22:55:37 +000014427 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014428 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014429 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014430 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14431 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014432
Craig Topper1666cb62011-11-19 07:07:26 +000014433 // Canonicalize pandn to RHS
14434 if (N0.getOpcode() == X86ISD::ANDNP)
14435 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014436 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014437 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14438 SDValue Mask = N1.getOperand(0);
14439 SDValue X = N1.getOperand(1);
14440 SDValue Y;
14441 if (N0.getOperand(0) == Mask)
14442 Y = N0.getOperand(1);
14443 if (N0.getOperand(1) == Mask)
14444 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014445
Craig Topper1666cb62011-11-19 07:07:26 +000014446 // Check to see if the mask appeared in both the AND and ANDNP and
14447 if (!Y.getNode())
14448 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014449
Craig Topper1666cb62011-11-19 07:07:26 +000014450 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014451 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014452 if (Mask.getOpcode() == ISD::BITCAST)
14453 Mask = Mask.getOperand(0);
14454 if (X.getOpcode() == ISD::BITCAST)
14455 X = X.getOperand(0);
14456 if (Y.getOpcode() == ISD::BITCAST)
14457 Y = Y.getOperand(0);
14458
Craig Topper1666cb62011-11-19 07:07:26 +000014459 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014460
Craig Toppered2e13d2012-01-22 19:15:14 +000014461 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014462 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14463 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014464 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014465 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014466
14467 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014468 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014469 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14470 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14471 if ((SraAmt + 1) != EltBits)
14472 return SDValue();
14473
14474 DebugLoc DL = N->getDebugLoc();
14475
14476 // Now we know we at least have a plendvb with the mask val. See if
14477 // we can form a psignb/w/d.
14478 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014479 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14480 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014481 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14482 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14483 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014484 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014485 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014486 }
14487 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014488 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014489 return SDValue();
14490
14491 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14492
14493 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14494 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14495 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014496 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014497 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014498 }
14499 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014500
Craig Topper1666cb62011-11-19 07:07:26 +000014501 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14502 return SDValue();
14503
Nate Begemanb65c1752010-12-17 22:55:37 +000014504 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014505 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14506 std::swap(N0, N1);
14507 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14508 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014509 if (!N0.hasOneUse() || !N1.hasOneUse())
14510 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014511
14512 SDValue ShAmt0 = N0.getOperand(1);
14513 if (ShAmt0.getValueType() != MVT::i8)
14514 return SDValue();
14515 SDValue ShAmt1 = N1.getOperand(1);
14516 if (ShAmt1.getValueType() != MVT::i8)
14517 return SDValue();
14518 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14519 ShAmt0 = ShAmt0.getOperand(0);
14520 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14521 ShAmt1 = ShAmt1.getOperand(0);
14522
14523 DebugLoc DL = N->getDebugLoc();
14524 unsigned Opc = X86ISD::SHLD;
14525 SDValue Op0 = N0.getOperand(0);
14526 SDValue Op1 = N1.getOperand(0);
14527 if (ShAmt0.getOpcode() == ISD::SUB) {
14528 Opc = X86ISD::SHRD;
14529 std::swap(Op0, Op1);
14530 std::swap(ShAmt0, ShAmt1);
14531 }
14532
Evan Cheng8b1190a2010-04-28 01:18:01 +000014533 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014534 if (ShAmt1.getOpcode() == ISD::SUB) {
14535 SDValue Sum = ShAmt1.getOperand(0);
14536 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014537 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14538 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14539 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14540 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014541 return DAG.getNode(Opc, DL, VT,
14542 Op0, Op1,
14543 DAG.getNode(ISD::TRUNCATE, DL,
14544 MVT::i8, ShAmt0));
14545 }
14546 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14547 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14548 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014549 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014550 return DAG.getNode(Opc, DL, VT,
14551 N0.getOperand(0), N1.getOperand(0),
14552 DAG.getNode(ISD::TRUNCATE, DL,
14553 MVT::i8, ShAmt0));
14554 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014555
Evan Cheng760d1942010-01-04 21:22:48 +000014556 return SDValue();
14557}
14558
Manman Ren92363622012-06-07 22:39:10 +000014559// Generate NEG and CMOV for integer abs.
14560static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14561 EVT VT = N->getValueType(0);
14562
14563 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14564 // 8-bit integer abs to NEG and CMOV.
14565 if (VT.isInteger() && VT.getSizeInBits() == 8)
14566 return SDValue();
14567
14568 SDValue N0 = N->getOperand(0);
14569 SDValue N1 = N->getOperand(1);
14570 DebugLoc DL = N->getDebugLoc();
14571
14572 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14573 // and change it to SUB and CMOV.
14574 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14575 N0.getOpcode() == ISD::ADD &&
14576 N0.getOperand(1) == N1 &&
14577 N1.getOpcode() == ISD::SRA &&
14578 N1.getOperand(0) == N0.getOperand(0))
14579 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14580 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14581 // Generate SUB & CMOV.
14582 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14583 DAG.getConstant(0, VT), N0.getOperand(0));
14584
14585 SDValue Ops[] = { N0.getOperand(0), Neg,
14586 DAG.getConstant(X86::COND_GE, MVT::i8),
14587 SDValue(Neg.getNode(), 1) };
14588 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14589 Ops, array_lengthof(Ops));
14590 }
14591 return SDValue();
14592}
14593
Craig Topper3738ccd2011-12-27 06:27:23 +000014594// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014595static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14596 TargetLowering::DAGCombinerInfo &DCI,
14597 const X86Subtarget *Subtarget) {
14598 if (DCI.isBeforeLegalizeOps())
14599 return SDValue();
14600
Manman Ren45d53b82012-06-08 18:58:26 +000014601 if (Subtarget->hasCMov()) {
14602 SDValue RV = performIntegerAbsCombine(N, DAG);
14603 if (RV.getNode())
14604 return RV;
14605 }
Manman Ren92363622012-06-07 22:39:10 +000014606
14607 // Try forming BMI if it is available.
14608 if (!Subtarget->hasBMI())
14609 return SDValue();
14610
Craig Topperb4c94572011-10-21 06:55:01 +000014611 EVT VT = N->getValueType(0);
14612
14613 if (VT != MVT::i32 && VT != MVT::i64)
14614 return SDValue();
14615
Craig Topper3738ccd2011-12-27 06:27:23 +000014616 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14617
Craig Topperb4c94572011-10-21 06:55:01 +000014618 // Create BLSMSK instructions by finding X ^ (X-1)
14619 SDValue N0 = N->getOperand(0);
14620 SDValue N1 = N->getOperand(1);
14621 DebugLoc DL = N->getDebugLoc();
14622
14623 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14624 isAllOnes(N0.getOperand(1)))
14625 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14626
14627 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14628 isAllOnes(N1.getOperand(1)))
14629 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14630
14631 return SDValue();
14632}
14633
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014634/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14635static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014636 TargetLowering::DAGCombinerInfo &DCI,
14637 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014638 LoadSDNode *Ld = cast<LoadSDNode>(N);
14639 EVT RegVT = Ld->getValueType(0);
14640 EVT MemVT = Ld->getMemoryVT();
14641 DebugLoc dl = Ld->getDebugLoc();
14642 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14643
14644 ISD::LoadExtType Ext = Ld->getExtensionType();
14645
Nadav Rotemca6f2962011-09-18 19:00:23 +000014646 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014647 // shuffle. We need SSE4 for the shuffles.
14648 // TODO: It is possible to support ZExt by zeroing the undef values
14649 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000014650 if (RegVT.isVector() && RegVT.isInteger() &&
14651 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014652 assert(MemVT != RegVT && "Cannot extend to the same type");
14653 assert(MemVT.isVector() && "Must load a vector from memory");
14654
14655 unsigned NumElems = RegVT.getVectorNumElements();
14656 unsigned RegSz = RegVT.getSizeInBits();
14657 unsigned MemSz = MemVT.getSizeInBits();
14658 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014659
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014660 // All sizes must be a power of two.
14661 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
14662 return SDValue();
14663
14664 // Attempt to load the original value using scalar loads.
14665 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014666 MVT SclrLoadTy = MVT::i8;
14667 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14668 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14669 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014670 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014671 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014672 }
14673 }
14674
Nadav Rotem5cd95e12012-07-11 13:27:05 +000014675 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
14676 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
14677 (64 <= MemSz))
14678 SclrLoadTy = MVT::f64;
14679
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014680 // Calculate the number of scalar loads that we need to perform
14681 // in order to load our vector from memory.
14682 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014683
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014684 // Represent our vector as a sequence of elements which are the
14685 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014686 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14687 RegSz/SclrLoadTy.getSizeInBits());
14688
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014689 // Represent the data using the same element type that is stored in
14690 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014691 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14692 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014693
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014694 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
14695 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014696
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014697 // We can't shuffle using an illegal type.
14698 if (!TLI.isTypeLegal(WideVecVT))
14699 return SDValue();
14700
14701 SmallVector<SDValue, 8> Chains;
14702 SDValue Ptr = Ld->getBasePtr();
14703 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
14704 TLI.getPointerTy());
14705 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
14706
14707 for (unsigned i = 0; i < NumLoads; ++i) {
14708 // Perform a single load.
14709 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14710 Ptr, Ld->getPointerInfo(),
14711 Ld->isVolatile(), Ld->isNonTemporal(),
14712 Ld->isInvariant(), Ld->getAlignment());
14713 Chains.push_back(ScalarLoad.getValue(1));
14714 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
14715 // another round of DAGCombining.
14716 if (i == 0)
14717 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
14718 else
14719 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
14720 ScalarLoad, DAG.getIntPtrConstant(i));
14721
14722 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14723 }
14724
14725 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14726 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014727
14728 // Bitcast the loaded value to a vector of the original element type, in
14729 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014730 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014731 unsigned SizeRatio = RegSz/MemSz;
14732
14733 // Redistribute the loaded elements into the different locations.
14734 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000014735 for (unsigned i = 0; i != NumElems; ++i)
14736 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014737
14738 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000014739 DAG.getUNDEF(WideVecVT),
14740 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014741
14742 // Bitcast to the requested type.
14743 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14744 // Replace the original load with the new sequence
14745 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014746 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014747 }
14748
14749 return SDValue();
14750}
14751
Chris Lattner149a4e52008-02-22 02:09:43 +000014752/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014753static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014754 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014755 StoreSDNode *St = cast<StoreSDNode>(N);
14756 EVT VT = St->getValue().getValueType();
14757 EVT StVT = St->getMemoryVT();
14758 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014759 SDValue StoredVal = St->getOperand(1);
14760 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14761
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014762 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000014763 // On Sandy Bridge, 256-bit memory operations are executed by two
14764 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
14765 // memory operation.
14766 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000014767 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14768 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000014769 SDValue Value0 = StoredVal.getOperand(0);
14770 SDValue Value1 = StoredVal.getOperand(1);
14771
14772 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14773 SDValue Ptr0 = St->getBasePtr();
14774 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14775
14776 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14777 St->getPointerInfo(), St->isVolatile(),
14778 St->isNonTemporal(), St->getAlignment());
14779 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14780 St->getPointerInfo(), St->isVolatile(),
14781 St->isNonTemporal(), St->getAlignment());
14782 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14783 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014784
14785 // Optimize trunc store (of multiple scalars) to shuffle and store.
14786 // First, pack all of the elements in one place. Next, store to memory
14787 // in fewer chunks.
14788 if (St->isTruncatingStore() && VT.isVector()) {
14789 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14790 unsigned NumElems = VT.getVectorNumElements();
14791 assert(StVT != VT && "Cannot truncate to the same type");
14792 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14793 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14794
14795 // From, To sizes and ElemCount must be pow of two
14796 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014797 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014798 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014799 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014800
Nadav Rotem614061b2011-08-10 19:30:14 +000014801 unsigned SizeRatio = FromSz / ToSz;
14802
14803 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14804
14805 // Create a type on which we perform the shuffle
14806 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14807 StVT.getScalarType(), NumElems*SizeRatio);
14808
14809 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14810
14811 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14812 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000014813 for (unsigned i = 0; i != NumElems; ++i)
14814 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000014815
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000014816 // Can't shuffle using an illegal type.
14817 if (!TLI.isTypeLegal(WideVecVT))
14818 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000014819
14820 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000014821 DAG.getUNDEF(WideVecVT),
14822 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000014823 // At this point all of the data is stored at the bottom of the
14824 // register. We now need to save it to mem.
14825
14826 // Find the largest store unit
14827 MVT StoreType = MVT::i8;
14828 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14829 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14830 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000014831 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000014832 StoreType = Tp;
14833 }
14834
Nadav Rotem5cd95e12012-07-11 13:27:05 +000014835 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
14836 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
14837 (64 <= NumElems * ToSz))
14838 StoreType = MVT::f64;
14839
Nadav Rotem614061b2011-08-10 19:30:14 +000014840 // Bitcast the original vector into a vector of store-size units
14841 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000014842 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000014843 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14844 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14845 SmallVector<SDValue, 8> Chains;
14846 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14847 TLI.getPointerTy());
14848 SDValue Ptr = St->getBasePtr();
14849
14850 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000014851 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014852 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14853 StoreType, ShuffWide,
14854 DAG.getIntPtrConstant(i));
14855 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14856 St->getPointerInfo(), St->isVolatile(),
14857 St->isNonTemporal(), St->getAlignment());
14858 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14859 Chains.push_back(Ch);
14860 }
14861
14862 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14863 Chains.size());
14864 }
14865
14866
Chris Lattner149a4e52008-02-22 02:09:43 +000014867 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14868 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014869 // A preferable solution to the general problem is to figure out the right
14870 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014871
14872 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014873 if (VT.getSizeInBits() != 64)
14874 return SDValue();
14875
Devang Patel578efa92009-06-05 21:57:13 +000014876 const Function *F = DAG.getMachineFunction().getFunction();
14877 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014878 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000014879 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000014880 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014881 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014882 isa<LoadSDNode>(St->getValue()) &&
14883 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14884 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014885 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014886 LoadSDNode *Ld = 0;
14887 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014888 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014889 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014890 // Must be a store of a load. We currently handle two cases: the load
14891 // is a direct child, and it's under an intervening TokenFactor. It is
14892 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014893 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014894 Ld = cast<LoadSDNode>(St->getChain());
14895 else if (St->getValue().hasOneUse() &&
14896 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000014897 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014898 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014899 TokenFactorIndex = i;
14900 Ld = cast<LoadSDNode>(St->getValue());
14901 } else
14902 Ops.push_back(ChainVal->getOperand(i));
14903 }
14904 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014905
Evan Cheng536e6672009-03-12 05:59:15 +000014906 if (!Ld || !ISD::isNormalLoad(Ld))
14907 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014908
Evan Cheng536e6672009-03-12 05:59:15 +000014909 // If this is not the MMX case, i.e. we are just turning i64 load/store
14910 // into f64 load/store, avoid the transformation if there are multiple
14911 // uses of the loaded value.
14912 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14913 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014914
Evan Cheng536e6672009-03-12 05:59:15 +000014915 DebugLoc LdDL = Ld->getDebugLoc();
14916 DebugLoc StDL = N->getDebugLoc();
14917 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14918 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14919 // pair instead.
14920 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014921 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014922 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14923 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014924 Ld->isNonTemporal(), Ld->isInvariant(),
14925 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014926 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014927 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014928 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014929 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014930 Ops.size());
14931 }
Evan Cheng536e6672009-03-12 05:59:15 +000014932 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014933 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014934 St->isVolatile(), St->isNonTemporal(),
14935 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014936 }
Evan Cheng536e6672009-03-12 05:59:15 +000014937
14938 // Otherwise, lower to two pairs of 32-bit loads / stores.
14939 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014940 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14941 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014942
Owen Anderson825b72b2009-08-11 20:47:22 +000014943 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014944 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014945 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014946 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014947 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014948 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014949 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014950 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014951 MinAlign(Ld->getAlignment(), 4));
14952
14953 SDValue NewChain = LoLd.getValue(1);
14954 if (TokenFactorIndex != -1) {
14955 Ops.push_back(LoLd);
14956 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014957 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014958 Ops.size());
14959 }
14960
14961 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014962 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14963 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014964
14965 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014966 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014967 St->isVolatile(), St->isNonTemporal(),
14968 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014969 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014970 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014971 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014972 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014973 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014974 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014975 }
Dan Gohman475871a2008-07-27 21:46:04 +000014976 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014977}
14978
Duncan Sands17470be2011-09-22 20:15:48 +000014979/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14980/// and return the operands for the horizontal operation in LHS and RHS. A
14981/// horizontal operation performs the binary operation on successive elements
14982/// of its first operand, then on successive elements of its second operand,
14983/// returning the resulting values in a vector. For example, if
14984/// A = < float a0, float a1, float a2, float a3 >
14985/// and
14986/// B = < float b0, float b1, float b2, float b3 >
14987/// then the result of doing a horizontal operation on A and B is
14988/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14989/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14990/// A horizontal-op B, for some already available A and B, and if so then LHS is
14991/// set to A, RHS to B, and the routine returns 'true'.
14992/// Note that the binary operation should have the property that if one of the
14993/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000014994static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000014995 // Look for the following pattern: if
14996 // A = < float a0, float a1, float a2, float a3 >
14997 // B = < float b0, float b1, float b2, float b3 >
14998 // and
14999 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15000 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15001 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15002 // which is A horizontal-op B.
15003
15004 // At least one of the operands should be a vector shuffle.
15005 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15006 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15007 return false;
15008
15009 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015010
15011 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15012 "Unsupported vector type for horizontal add/sub");
15013
15014 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15015 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015016 unsigned NumElts = VT.getVectorNumElements();
15017 unsigned NumLanes = VT.getSizeInBits()/128;
15018 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015019 assert((NumLaneElts % 2 == 0) &&
15020 "Vector type should have an even number of elements in each lane");
15021 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015022
15023 // View LHS in the form
15024 // LHS = VECTOR_SHUFFLE A, B, LMask
15025 // If LHS is not a shuffle then pretend it is the shuffle
15026 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15027 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15028 // type VT.
15029 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015030 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015031 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15032 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15033 A = LHS.getOperand(0);
15034 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15035 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015036 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15037 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015038 } else {
15039 if (LHS.getOpcode() != ISD::UNDEF)
15040 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015041 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015042 LMask[i] = i;
15043 }
15044
15045 // Likewise, view RHS in the form
15046 // RHS = VECTOR_SHUFFLE C, D, RMask
15047 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015048 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015049 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15050 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15051 C = RHS.getOperand(0);
15052 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15053 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015054 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15055 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015056 } else {
15057 if (RHS.getOpcode() != ISD::UNDEF)
15058 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015059 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015060 RMask[i] = i;
15061 }
15062
15063 // Check that the shuffles are both shuffling the same vectors.
15064 if (!(A == C && B == D) && !(A == D && B == C))
15065 return false;
15066
15067 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15068 if (!A.getNode() && !B.getNode())
15069 return false;
15070
15071 // If A and B occur in reverse order in RHS, then "swap" them (which means
15072 // rewriting the mask).
15073 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015074 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015075
15076 // At this point LHS and RHS are equivalent to
15077 // LHS = VECTOR_SHUFFLE A, B, LMask
15078 // RHS = VECTOR_SHUFFLE A, B, RMask
15079 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015080 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015081 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015082
Craig Topperf8363302011-12-02 08:18:41 +000015083 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015084 if (LIdx < 0 || RIdx < 0 ||
15085 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15086 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015087 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015088
Craig Topperf8363302011-12-02 08:18:41 +000015089 // Check that successive elements are being operated on. If not, this is
15090 // not a horizontal operation.
15091 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15092 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015093 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015094 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015095 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015096 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015097 }
15098
15099 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15100 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15101 return true;
15102}
15103
15104/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15105static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15106 const X86Subtarget *Subtarget) {
15107 EVT VT = N->getValueType(0);
15108 SDValue LHS = N->getOperand(0);
15109 SDValue RHS = N->getOperand(1);
15110
15111 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015112 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015113 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015114 isHorizontalBinOp(LHS, RHS, true))
15115 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15116 return SDValue();
15117}
15118
15119/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15120static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15121 const X86Subtarget *Subtarget) {
15122 EVT VT = N->getValueType(0);
15123 SDValue LHS = N->getOperand(0);
15124 SDValue RHS = N->getOperand(1);
15125
15126 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015127 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015128 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015129 isHorizontalBinOp(LHS, RHS, false))
15130 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15131 return SDValue();
15132}
15133
Chris Lattner6cf73262008-01-25 06:14:17 +000015134/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15135/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015136static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015137 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15138 // F[X]OR(0.0, x) -> x
15139 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015140 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15141 if (C->getValueAPF().isPosZero())
15142 return N->getOperand(1);
15143 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15144 if (C->getValueAPF().isPosZero())
15145 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015146 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015147}
15148
15149/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015150static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015151 // FAND(0.0, x) -> 0.0
15152 // FAND(x, 0.0) -> 0.0
15153 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15154 if (C->getValueAPF().isPosZero())
15155 return N->getOperand(0);
15156 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15157 if (C->getValueAPF().isPosZero())
15158 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015159 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015160}
15161
Dan Gohmane5af2d32009-01-29 01:59:02 +000015162static SDValue PerformBTCombine(SDNode *N,
15163 SelectionDAG &DAG,
15164 TargetLowering::DAGCombinerInfo &DCI) {
15165 // BT ignores high bits in the bit index operand.
15166 SDValue Op1 = N->getOperand(1);
15167 if (Op1.hasOneUse()) {
15168 unsigned BitWidth = Op1.getValueSizeInBits();
15169 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15170 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015171 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15172 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015173 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015174 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15175 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15176 DCI.CommitTargetLoweringOpt(TLO);
15177 }
15178 return SDValue();
15179}
Chris Lattner83e6c992006-10-04 06:57:07 +000015180
Eli Friedman7a5e5552009-06-07 06:52:44 +000015181static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15182 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015183 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015184 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015185 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015186 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015187 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015188 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015189 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015190 }
15191 return SDValue();
15192}
15193
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015194static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15195 TargetLowering::DAGCombinerInfo &DCI,
15196 const X86Subtarget *Subtarget) {
15197 if (!DCI.isBeforeLegalizeOps())
15198 return SDValue();
15199
Craig Topper3ef43cf2012-04-24 06:36:35 +000015200 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015201 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015202
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015203 EVT VT = N->getValueType(0);
15204 SDValue Op = N->getOperand(0);
15205 EVT OpVT = Op.getValueType();
15206 DebugLoc dl = N->getDebugLoc();
15207
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015208 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15209 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015210
Craig Topper3ef43cf2012-04-24 06:36:35 +000015211 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015212 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015213
15214 // Optimize vectors in AVX mode
15215 // Sign extend v8i16 to v8i32 and
15216 // v4i32 to v4i64
15217 //
15218 // Divide input vector into two parts
15219 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15220 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15221 // concat the vectors to original VT
15222
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015223 unsigned NumElems = OpVT.getVectorNumElements();
15224 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015225 for (unsigned i = 0; i != NumElems/2; ++i)
15226 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015227
15228 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, DAG.getUNDEF(OpVT),
Craig Topperdf966f62012-04-22 19:17:57 +000015229 &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015230
15231 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015232 for (unsigned i = 0; i != NumElems/2; ++i)
15233 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015234
15235 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, DAG.getUNDEF(OpVT),
Craig Topperdf966f62012-04-22 19:17:57 +000015236 &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015237
Craig Topper3ef43cf2012-04-24 06:36:35 +000015238 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015239 VT.getVectorNumElements()/2);
15240
Craig Topper3ef43cf2012-04-24 06:36:35 +000015241 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015242 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15243
15244 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15245 }
15246 return SDValue();
15247}
15248
Michael Liaof6c24ee2012-08-10 14:39:24 +000015249static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015250 const X86Subtarget* Subtarget) {
15251 DebugLoc dl = N->getDebugLoc();
15252 EVT VT = N->getValueType(0);
15253
15254 EVT ScalarVT = VT.getScalarType();
15255 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) || !Subtarget->hasFMA())
15256 return SDValue();
15257
15258 SDValue A = N->getOperand(0);
15259 SDValue B = N->getOperand(1);
15260 SDValue C = N->getOperand(2);
15261
15262 bool NegA = (A.getOpcode() == ISD::FNEG);
15263 bool NegB = (B.getOpcode() == ISD::FNEG);
15264 bool NegC = (C.getOpcode() == ISD::FNEG);
15265
Michael Liaof6c24ee2012-08-10 14:39:24 +000015266 // Negative multiplication when NegA xor NegB
15267 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015268 if (NegA)
15269 A = A.getOperand(0);
15270 if (NegB)
15271 B = B.getOperand(0);
15272 if (NegC)
15273 C = C.getOperand(0);
15274
15275 unsigned Opcode;
15276 if (!NegMul)
15277 Opcode = (!NegC)? X86ISD::FMADD : X86ISD::FMSUB;
15278 else
15279 Opcode = (!NegC)? X86ISD::FNMADD : X86ISD::FNMSUB;
15280 return DAG.getNode(Opcode, dl, VT, A, B, C);
15281}
15282
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015283static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015284 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015285 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015286 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15287 // (and (i32 x86isd::setcc_carry), 1)
15288 // This eliminates the zext. This transformation is necessary because
15289 // ISD::SETCC is always legalized to i8.
15290 DebugLoc dl = N->getDebugLoc();
15291 SDValue N0 = N->getOperand(0);
15292 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015293 EVT OpVT = N0.getValueType();
15294
Evan Cheng2e489c42009-12-16 00:53:11 +000015295 if (N0.getOpcode() == ISD::AND &&
15296 N0.hasOneUse() &&
15297 N0.getOperand(0).hasOneUse()) {
15298 SDValue N00 = N0.getOperand(0);
15299 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15300 return SDValue();
15301 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15302 if (!C || C->getZExtValue() != 1)
15303 return SDValue();
15304 return DAG.getNode(ISD::AND, dl, VT,
15305 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15306 N00.getOperand(0), N00.getOperand(1)),
15307 DAG.getConstant(1, VT));
15308 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015309
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015310 // Optimize vectors in AVX mode:
15311 //
15312 // v8i16 -> v8i32
15313 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15314 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15315 // Concat upper and lower parts.
15316 //
15317 // v4i32 -> v4i64
15318 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15319 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15320 // Concat upper and lower parts.
15321 //
Craig Topperc16f8512012-04-25 06:39:39 +000015322 if (!DCI.isBeforeLegalizeOps())
15323 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015324
Craig Topperc16f8512012-04-25 06:39:39 +000015325 if (!Subtarget->hasAVX())
15326 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015327
Craig Topperc16f8512012-04-25 06:39:39 +000015328 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15329 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015330
Craig Topperc16f8512012-04-25 06:39:39 +000015331 if (Subtarget->hasAVX2())
15332 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015333
Craig Topperc16f8512012-04-25 06:39:39 +000015334 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15335 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15336 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015337
Craig Topperc16f8512012-04-25 06:39:39 +000015338 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15339 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015340
Craig Topperc16f8512012-04-25 06:39:39 +000015341 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15342 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15343
15344 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015345 }
15346
Evan Cheng2e489c42009-12-16 00:53:11 +000015347 return SDValue();
15348}
15349
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015350// Optimize x == -y --> x+y == 0
15351// x != -y --> x+y != 0
15352static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15353 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15354 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015355 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015356
15357 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15358 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15359 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15360 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15361 LHS.getValueType(), RHS, LHS.getOperand(1));
15362 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15363 addV, DAG.getConstant(0, addV.getValueType()), CC);
15364 }
15365 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15366 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15367 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15368 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15369 RHS.getValueType(), LHS, RHS.getOperand(1));
15370 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15371 addV, DAG.getConstant(0, addV.getValueType()), CC);
15372 }
15373 return SDValue();
15374}
15375
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015376// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
15377static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015378 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015379 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15380 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015381
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015382 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15383 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15384 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015385 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015386 return DAG.getNode(ISD::AND, DL, MVT::i8,
15387 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015388 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015389 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015390
Michael Liao2a33cec2012-08-10 19:58:13 +000015391 SDValue Flags;
15392
15393 Flags = BoolTestSetCCCombine(EFLAGS, CC);
15394 if (Flags.getNode()) {
15395 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15396 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15397 }
15398
15399 return SDValue();
15400}
15401
15402// Optimize branch condition evaluation.
15403//
15404static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15405 TargetLowering::DAGCombinerInfo &DCI,
15406 const X86Subtarget *Subtarget) {
15407 DebugLoc DL = N->getDebugLoc();
15408 SDValue Chain = N->getOperand(0);
15409 SDValue Dest = N->getOperand(1);
15410 SDValue EFLAGS = N->getOperand(3);
15411 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15412
15413 SDValue Flags;
15414
15415 Flags = BoolTestSetCCCombine(EFLAGS, CC);
15416 if (Flags.getNode()) {
15417 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15418 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15419 Flags);
15420 }
15421
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015422 return SDValue();
15423}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015424
Craig Topper7fd5e162012-04-24 06:02:29 +000015425static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015426 SDValue Op0 = N->getOperand(0);
15427 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015428
15429 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015430 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015431 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015432 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015433 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15434 // Notice that we use SINT_TO_FP because we know that the high bits
15435 // are zero and SINT_TO_FP is better supported by the hardware.
15436 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15437 }
15438
15439 return SDValue();
15440}
15441
Benjamin Kramer1396c402011-06-18 11:09:41 +000015442static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15443 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015444 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015445 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015446
15447 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015448 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015449 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015450 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015451 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15452 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15453 }
15454
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015455 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15456 // a 32-bit target where SSE doesn't support i64->FP operations.
15457 if (Op0.getOpcode() == ISD::LOAD) {
15458 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15459 EVT VT = Ld->getValueType(0);
15460 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15461 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15462 !XTLI->getSubtarget()->is64Bit() &&
15463 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015464 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15465 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015466 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15467 return FILDChain;
15468 }
15469 }
15470 return SDValue();
15471}
15472
Craig Topper7fd5e162012-04-24 06:02:29 +000015473static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15474 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015475
15476 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015477 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15478 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015479 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015480 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15481 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15482 }
15483
15484 return SDValue();
15485}
15486
Chris Lattner23a01992010-12-20 01:37:09 +000015487// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15488static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15489 X86TargetLowering::DAGCombinerInfo &DCI) {
15490 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15491 // the result is either zero or one (depending on the input carry bit).
15492 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15493 if (X86::isZeroNode(N->getOperand(0)) &&
15494 X86::isZeroNode(N->getOperand(1)) &&
15495 // We don't have a good way to replace an EFLAGS use, so only do this when
15496 // dead right now.
15497 SDValue(N, 1).use_empty()) {
15498 DebugLoc DL = N->getDebugLoc();
15499 EVT VT = N->getValueType(0);
15500 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15501 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15502 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15503 DAG.getConstant(X86::COND_B,MVT::i8),
15504 N->getOperand(2)),
15505 DAG.getConstant(1, VT));
15506 return DCI.CombineTo(N, Res1, CarryOut);
15507 }
15508
15509 return SDValue();
15510}
15511
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015512// fold (add Y, (sete X, 0)) -> adc 0, Y
15513// (add Y, (setne X, 0)) -> sbb -1, Y
15514// (sub (sete X, 0), Y) -> sbb 0, Y
15515// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015516static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015517 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015518
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015519 // Look through ZExts.
15520 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15521 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15522 return SDValue();
15523
15524 SDValue SetCC = Ext.getOperand(0);
15525 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15526 return SDValue();
15527
15528 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15529 if (CC != X86::COND_E && CC != X86::COND_NE)
15530 return SDValue();
15531
15532 SDValue Cmp = SetCC.getOperand(1);
15533 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015534 !X86::isZeroNode(Cmp.getOperand(1)) ||
15535 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015536 return SDValue();
15537
15538 SDValue CmpOp0 = Cmp.getOperand(0);
15539 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15540 DAG.getConstant(1, CmpOp0.getValueType()));
15541
15542 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15543 if (CC == X86::COND_NE)
15544 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15545 DL, OtherVal.getValueType(), OtherVal,
15546 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15547 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15548 DL, OtherVal.getValueType(), OtherVal,
15549 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15550}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015551
Craig Topper54f952a2011-11-19 09:02:40 +000015552/// PerformADDCombine - Do target-specific dag combines on integer adds.
15553static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15554 const X86Subtarget *Subtarget) {
15555 EVT VT = N->getValueType(0);
15556 SDValue Op0 = N->getOperand(0);
15557 SDValue Op1 = N->getOperand(1);
15558
15559 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015560 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000015561 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000015562 isHorizontalBinOp(Op0, Op1, true))
15563 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
15564
15565 return OptimizeConditionalInDecrement(N, DAG);
15566}
15567
15568static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
15569 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015570 SDValue Op0 = N->getOperand(0);
15571 SDValue Op1 = N->getOperand(1);
15572
15573 // X86 can't encode an immediate LHS of a sub. See if we can push the
15574 // negation into a preceding instruction.
15575 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015576 // If the RHS of the sub is a XOR with one use and a constant, invert the
15577 // immediate. Then add one to the LHS of the sub so we can turn
15578 // X-Y -> X+~Y+1, saving one register.
15579 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
15580 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000015581 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015582 EVT VT = Op0.getValueType();
15583 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
15584 Op1.getOperand(0),
15585 DAG.getConstant(~XorC, VT));
15586 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000015587 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015588 }
15589 }
15590
Craig Topper54f952a2011-11-19 09:02:40 +000015591 // Try to synthesize horizontal adds from adds of shuffles.
15592 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000015593 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000015594 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
15595 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000015596 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
15597
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015598 return OptimizeConditionalInDecrement(N, DAG);
15599}
15600
Dan Gohman475871a2008-07-27 21:46:04 +000015601SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000015602 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000015603 SelectionDAG &DAG = DCI.DAG;
15604 switch (N->getOpcode()) {
15605 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015606 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000015607 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000015608 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000015609 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015610 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000015611 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
15612 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000015613 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000015614 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000015615 case ISD::SHL:
15616 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000015617 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000015618 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000015619 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000015620 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015621 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000015622 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000015623 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015624 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000015625 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000015626 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
15627 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000015628 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000015629 case X86ISD::FOR: return PerformFORCombine(N, DAG);
15630 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000015631 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015632 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015633 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000015634 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015635 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015636 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015637 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015638 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Michael Liao2a33cec2012-08-10 19:58:13 +000015639 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000015640 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000015641 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000015642 case X86ISD::UNPCKH:
15643 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015644 case X86ISD::MOVHLPS:
15645 case X86ISD::MOVLHPS:
15646 case X86ISD::PSHUFD:
15647 case X86ISD::PSHUFHW:
15648 case X86ISD::PSHUFLW:
15649 case X86ISD::MOVSS:
15650 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000015651 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000015652 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015653 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015654 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000015655 }
15656
Dan Gohman475871a2008-07-27 21:46:04 +000015657 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000015658}
15659
Evan Chenge5b51ac2010-04-17 06:13:15 +000015660/// isTypeDesirableForOp - Return true if the target has native support for
15661/// the specified value type and it is 'desirable' to use the type for the
15662/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
15663/// instruction encodings are longer and some i16 instructions are slow.
15664bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
15665 if (!isTypeLegal(VT))
15666 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000015667 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000015668 return true;
15669
15670 switch (Opc) {
15671 default:
15672 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000015673 case ISD::LOAD:
15674 case ISD::SIGN_EXTEND:
15675 case ISD::ZERO_EXTEND:
15676 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000015677 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000015678 case ISD::SRL:
15679 case ISD::SUB:
15680 case ISD::ADD:
15681 case ISD::MUL:
15682 case ISD::AND:
15683 case ISD::OR:
15684 case ISD::XOR:
15685 return false;
15686 }
15687}
15688
15689/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000015690/// beneficial for dag combiner to promote the specified node. If true, it
15691/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000015692bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000015693 EVT VT = Op.getValueType();
15694 if (VT != MVT::i16)
15695 return false;
15696
Evan Cheng4c26e932010-04-19 19:29:22 +000015697 bool Promote = false;
15698 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015699 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000015700 default: break;
15701 case ISD::LOAD: {
15702 LoadSDNode *LD = cast<LoadSDNode>(Op);
15703 // If the non-extending load has a single use and it's not live out, then it
15704 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000015705 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
15706 Op.hasOneUse()*/) {
15707 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
15708 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
15709 // The only case where we'd want to promote LOAD (rather then it being
15710 // promoted as an operand is when it's only use is liveout.
15711 if (UI->getOpcode() != ISD::CopyToReg)
15712 return false;
15713 }
15714 }
Evan Cheng4c26e932010-04-19 19:29:22 +000015715 Promote = true;
15716 break;
15717 }
15718 case ISD::SIGN_EXTEND:
15719 case ISD::ZERO_EXTEND:
15720 case ISD::ANY_EXTEND:
15721 Promote = true;
15722 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015723 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000015724 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000015725 SDValue N0 = Op.getOperand(0);
15726 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000015727 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000015728 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000015729 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015730 break;
15731 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000015732 case ISD::ADD:
15733 case ISD::MUL:
15734 case ISD::AND:
15735 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000015736 case ISD::XOR:
15737 Commute = true;
15738 // fallthrough
15739 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000015740 SDValue N0 = Op.getOperand(0);
15741 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000015742 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015743 return false;
15744 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000015745 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015746 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000015747 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000015748 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000015749 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015750 }
15751 }
15752
15753 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000015754 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015755}
15756
Evan Cheng60c07e12006-07-05 22:17:51 +000015757//===----------------------------------------------------------------------===//
15758// X86 Inline Assembly Support
15759//===----------------------------------------------------------------------===//
15760
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015761namespace {
15762 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015763 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015764 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015765
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015766 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015767 StringRef piece(*args[i]);
15768 if (!s.startswith(piece)) // Check if the piece matches.
15769 return false;
15770
15771 s = s.substr(piece.size());
15772 StringRef::size_type pos = s.find_first_not_of(" \t");
15773 if (pos == 0) // We matched a prefix.
15774 return false;
15775
15776 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015777 }
15778
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015779 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015780 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000015781 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015782}
15783
Chris Lattnerb8105652009-07-20 17:51:36 +000015784bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
15785 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000015786
15787 std::string AsmStr = IA->getAsmString();
15788
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015789 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
15790 if (!Ty || Ty->getBitWidth() % 16 != 0)
15791 return false;
15792
Chris Lattnerb8105652009-07-20 17:51:36 +000015793 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000015794 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000015795 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000015796
15797 switch (AsmPieces.size()) {
15798 default: return false;
15799 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000015800 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015801 // we will turn this bswap into something that will be lowered to logical
15802 // ops instead of emitting the bswap asm. For now, we don't support 486 or
15803 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000015804 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015805 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
15806 matchAsm(AsmPieces[0], "bswapl", "$0") ||
15807 matchAsm(AsmPieces[0], "bswapq", "$0") ||
15808 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
15809 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
15810 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000015811 // No need to check constraints, nothing other than the equivalent of
15812 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000015813 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015814 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015815
Chris Lattnerb8105652009-07-20 17:51:36 +000015816 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000015817 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015818 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015819 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
15820 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000015821 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015822 const std::string &ConstraintsStr = IA->getConstraintString();
15823 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000015824 std::sort(AsmPieces.begin(), AsmPieces.end());
15825 if (AsmPieces.size() == 4 &&
15826 AsmPieces[0] == "~{cc}" &&
15827 AsmPieces[1] == "~{dirflag}" &&
15828 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015829 AsmPieces[3] == "~{fpsr}")
15830 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015831 }
15832 break;
15833 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000015834 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015835 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015836 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
15837 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
15838 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015839 AsmPieces.clear();
15840 const std::string &ConstraintsStr = IA->getConstraintString();
15841 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
15842 std::sort(AsmPieces.begin(), AsmPieces.end());
15843 if (AsmPieces.size() == 4 &&
15844 AsmPieces[0] == "~{cc}" &&
15845 AsmPieces[1] == "~{dirflag}" &&
15846 AsmPieces[2] == "~{flags}" &&
15847 AsmPieces[3] == "~{fpsr}")
15848 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000015849 }
Evan Cheng55d42002011-01-08 01:24:27 +000015850
15851 if (CI->getType()->isIntegerTy(64)) {
15852 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
15853 if (Constraints.size() >= 2 &&
15854 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
15855 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
15856 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000015857 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
15858 matchAsm(AsmPieces[1], "bswap", "%edx") &&
15859 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000015860 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015861 }
15862 }
15863 break;
15864 }
15865 return false;
15866}
15867
15868
15869
Chris Lattnerf4dff842006-07-11 02:54:03 +000015870/// getConstraintType - Given a constraint letter, return the type of
15871/// constraint it is for this target.
15872X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000015873X86TargetLowering::getConstraintType(const std::string &Constraint) const {
15874 if (Constraint.size() == 1) {
15875 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000015876 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000015877 case 'q':
15878 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000015879 case 'f':
15880 case 't':
15881 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000015882 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000015883 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000015884 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000015885 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000015886 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000015887 case 'a':
15888 case 'b':
15889 case 'c':
15890 case 'd':
15891 case 'S':
15892 case 'D':
15893 case 'A':
15894 return C_Register;
15895 case 'I':
15896 case 'J':
15897 case 'K':
15898 case 'L':
15899 case 'M':
15900 case 'N':
15901 case 'G':
15902 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015903 case 'e':
15904 case 'Z':
15905 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015906 default:
15907 break;
15908 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015909 }
Chris Lattner4234f572007-03-25 02:14:49 +000015910 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015911}
15912
John Thompson44ab89e2010-10-29 17:29:13 +000015913/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015914/// This object must already have been set up with the operand type
15915/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015916TargetLowering::ConstraintWeight
15917 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015918 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015919 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015920 Value *CallOperandVal = info.CallOperandVal;
15921 // If we don't have a value, we can't do a match,
15922 // but allow it at the lowest weight.
15923 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015924 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015925 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015926 // Look at the constraint type.
15927 switch (*constraint) {
15928 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015929 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15930 case 'R':
15931 case 'q':
15932 case 'Q':
15933 case 'a':
15934 case 'b':
15935 case 'c':
15936 case 'd':
15937 case 'S':
15938 case 'D':
15939 case 'A':
15940 if (CallOperandVal->getType()->isIntegerTy())
15941 weight = CW_SpecificReg;
15942 break;
15943 case 'f':
15944 case 't':
15945 case 'u':
15946 if (type->isFloatingPointTy())
15947 weight = CW_SpecificReg;
15948 break;
15949 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015950 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015951 weight = CW_SpecificReg;
15952 break;
15953 case 'x':
15954 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000015955 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000015956 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000015957 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015958 break;
15959 case 'I':
15960 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15961 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015962 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015963 }
15964 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015965 case 'J':
15966 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15967 if (C->getZExtValue() <= 63)
15968 weight = CW_Constant;
15969 }
15970 break;
15971 case 'K':
15972 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15973 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15974 weight = CW_Constant;
15975 }
15976 break;
15977 case 'L':
15978 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15979 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15980 weight = CW_Constant;
15981 }
15982 break;
15983 case 'M':
15984 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15985 if (C->getZExtValue() <= 3)
15986 weight = CW_Constant;
15987 }
15988 break;
15989 case 'N':
15990 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15991 if (C->getZExtValue() <= 0xff)
15992 weight = CW_Constant;
15993 }
15994 break;
15995 case 'G':
15996 case 'C':
15997 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15998 weight = CW_Constant;
15999 }
16000 break;
16001 case 'e':
16002 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16003 if ((C->getSExtValue() >= -0x80000000LL) &&
16004 (C->getSExtValue() <= 0x7fffffffLL))
16005 weight = CW_Constant;
16006 }
16007 break;
16008 case 'Z':
16009 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16010 if (C->getZExtValue() <= 0xffffffff)
16011 weight = CW_Constant;
16012 }
16013 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016014 }
16015 return weight;
16016}
16017
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016018/// LowerXConstraint - try to replace an X constraint, which matches anything,
16019/// with another that has more specific requirements based on the type of the
16020/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016021const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016022LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016023 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16024 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016025 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016026 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016027 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016028 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016029 return "x";
16030 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016031
Chris Lattner5e764232008-04-26 23:02:14 +000016032 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016033}
16034
Chris Lattner48884cd2007-08-25 00:47:38 +000016035/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16036/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016037void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016038 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016039 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016040 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016041 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016042
Eric Christopher100c8332011-06-02 23:16:42 +000016043 // Only support length 1 constraints for now.
16044 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016045
Eric Christopher100c8332011-06-02 23:16:42 +000016046 char ConstraintLetter = Constraint[0];
16047 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016048 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016049 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016050 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016051 if (C->getZExtValue() <= 31) {
16052 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016053 break;
16054 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016055 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016056 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016057 case 'J':
16058 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016059 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016060 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16061 break;
16062 }
16063 }
16064 return;
16065 case 'K':
16066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016067 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016068 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16069 break;
16070 }
16071 }
16072 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016073 case 'N':
16074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016075 if (C->getZExtValue() <= 255) {
16076 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016077 break;
16078 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016079 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016080 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016081 case 'e': {
16082 // 32-bit signed value
16083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016084 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16085 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016086 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016087 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016088 break;
16089 }
16090 // FIXME gcc accepts some relocatable values here too, but only in certain
16091 // memory models; it's complicated.
16092 }
16093 return;
16094 }
16095 case 'Z': {
16096 // 32-bit unsigned value
16097 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016098 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16099 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016100 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16101 break;
16102 }
16103 }
16104 // FIXME gcc accepts some relocatable values here too, but only in certain
16105 // memory models; it's complicated.
16106 return;
16107 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016108 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016109 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016110 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016111 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016112 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016113 break;
16114 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016115
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016116 // In any sort of PIC mode addresses need to be computed at runtime by
16117 // adding in a register or some sort of table lookup. These can't
16118 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016119 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016120 return;
16121
Chris Lattnerdc43a882007-05-03 16:52:29 +000016122 // If we are in non-pic codegen mode, we allow the address of a global (with
16123 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016124 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016125 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016126
Chris Lattner49921962009-05-08 18:23:14 +000016127 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16128 while (1) {
16129 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16130 Offset += GA->getOffset();
16131 break;
16132 } else if (Op.getOpcode() == ISD::ADD) {
16133 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16134 Offset += C->getZExtValue();
16135 Op = Op.getOperand(0);
16136 continue;
16137 }
16138 } else if (Op.getOpcode() == ISD::SUB) {
16139 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16140 Offset += -C->getZExtValue();
16141 Op = Op.getOperand(0);
16142 continue;
16143 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016144 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016145
Chris Lattner49921962009-05-08 18:23:14 +000016146 // Otherwise, this isn't something we can handle, reject it.
16147 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016148 }
Eric Christopherfd179292009-08-27 18:07:15 +000016149
Dan Gohman46510a72010-04-15 01:51:59 +000016150 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016151 // If we require an extra load to get this address, as in PIC mode, we
16152 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016153 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16154 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016155 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016156
Devang Patel0d881da2010-07-06 22:08:15 +000016157 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16158 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016159 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016160 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016161 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016162
Gabor Greifba36cb52008-08-28 21:40:38 +000016163 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016164 Ops.push_back(Result);
16165 return;
16166 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016167 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016168}
16169
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016170std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016171X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016172 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016173 // First, see if this is a constraint that directly corresponds to an LLVM
16174 // register class.
16175 if (Constraint.size() == 1) {
16176 // GCC Constraint Letters
16177 switch (Constraint[0]) {
16178 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016179 // TODO: Slight differences here in allocation order and leaving
16180 // RIP in the class. Do they matter any more here than they do
16181 // in the normal allocation?
16182 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16183 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016184 if (VT == MVT::i32 || VT == MVT::f32)
16185 return std::make_pair(0U, &X86::GR32RegClass);
16186 if (VT == MVT::i16)
16187 return std::make_pair(0U, &X86::GR16RegClass);
16188 if (VT == MVT::i8 || VT == MVT::i1)
16189 return std::make_pair(0U, &X86::GR8RegClass);
16190 if (VT == MVT::i64 || VT == MVT::f64)
16191 return std::make_pair(0U, &X86::GR64RegClass);
16192 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016193 }
16194 // 32-bit fallthrough
16195 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016196 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016197 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16198 if (VT == MVT::i16)
16199 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16200 if (VT == MVT::i8 || VT == MVT::i1)
16201 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16202 if (VT == MVT::i64)
16203 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016204 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016205 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016206 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016207 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016208 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016209 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016210 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016211 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016212 return std::make_pair(0U, &X86::GR32RegClass);
16213 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016214 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016215 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016216 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016217 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016218 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016219 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016220 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16221 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016222 case 'f': // FP Stack registers.
16223 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16224 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016225 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016226 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016227 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016228 return std::make_pair(0U, &X86::RFP64RegClass);
16229 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016230 case 'y': // MMX_REGS if MMX allowed.
16231 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016232 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016233 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016234 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016235 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016236 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016237 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016238
Owen Anderson825b72b2009-08-11 20:47:22 +000016239 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016240 default: break;
16241 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016242 case MVT::f32:
16243 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016244 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016245 case MVT::f64:
16246 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016247 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016248 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016249 case MVT::v16i8:
16250 case MVT::v8i16:
16251 case MVT::v4i32:
16252 case MVT::v2i64:
16253 case MVT::v4f32:
16254 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016255 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016256 // AVX types.
16257 case MVT::v32i8:
16258 case MVT::v16i16:
16259 case MVT::v8i32:
16260 case MVT::v4i64:
16261 case MVT::v8f32:
16262 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016263 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016264 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016265 break;
16266 }
16267 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016268
Chris Lattnerf76d1802006-07-31 23:26:50 +000016269 // Use the default implementation in TargetLowering to convert the register
16270 // constraint into a member of a register class.
16271 std::pair<unsigned, const TargetRegisterClass*> Res;
16272 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016273
16274 // Not found as a standard register?
16275 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016276 // Map st(0) -> st(7) -> ST0
16277 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16278 tolower(Constraint[1]) == 's' &&
16279 tolower(Constraint[2]) == 't' &&
16280 Constraint[3] == '(' &&
16281 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16282 Constraint[5] == ')' &&
16283 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016284
Chris Lattner56d77c72009-09-13 22:41:48 +000016285 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016286 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016287 return Res;
16288 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016289
Chris Lattner56d77c72009-09-13 22:41:48 +000016290 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016291 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016292 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016293 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016294 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016295 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016296
16297 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016298 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016299 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016300 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016301 return Res;
16302 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016303
Dale Johannesen330169f2008-11-13 21:52:36 +000016304 // 'A' means EAX + EDX.
16305 if (Constraint == "A") {
16306 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016307 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016308 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016309 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016310 return Res;
16311 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016312
Chris Lattnerf76d1802006-07-31 23:26:50 +000016313 // Otherwise, check to see if this is a register class of the wrong value
16314 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16315 // turn into {ax},{dx}.
16316 if (Res.second->hasType(VT))
16317 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016318
Chris Lattnerf76d1802006-07-31 23:26:50 +000016319 // All of the single-register GCC register classes map their values onto
16320 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16321 // really want an 8-bit or 32-bit register, map to the appropriate register
16322 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016323 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016324 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016325 unsigned DestReg = 0;
16326 switch (Res.first) {
16327 default: break;
16328 case X86::AX: DestReg = X86::AL; break;
16329 case X86::DX: DestReg = X86::DL; break;
16330 case X86::CX: DestReg = X86::CL; break;
16331 case X86::BX: DestReg = X86::BL; break;
16332 }
16333 if (DestReg) {
16334 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016335 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016336 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016337 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016338 unsigned DestReg = 0;
16339 switch (Res.first) {
16340 default: break;
16341 case X86::AX: DestReg = X86::EAX; break;
16342 case X86::DX: DestReg = X86::EDX; break;
16343 case X86::CX: DestReg = X86::ECX; break;
16344 case X86::BX: DestReg = X86::EBX; break;
16345 case X86::SI: DestReg = X86::ESI; break;
16346 case X86::DI: DestReg = X86::EDI; break;
16347 case X86::BP: DestReg = X86::EBP; break;
16348 case X86::SP: DestReg = X86::ESP; break;
16349 }
16350 if (DestReg) {
16351 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016352 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016353 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016354 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016355 unsigned DestReg = 0;
16356 switch (Res.first) {
16357 default: break;
16358 case X86::AX: DestReg = X86::RAX; break;
16359 case X86::DX: DestReg = X86::RDX; break;
16360 case X86::CX: DestReg = X86::RCX; break;
16361 case X86::BX: DestReg = X86::RBX; break;
16362 case X86::SI: DestReg = X86::RSI; break;
16363 case X86::DI: DestReg = X86::RDI; break;
16364 case X86::BP: DestReg = X86::RBP; break;
16365 case X86::SP: DestReg = X86::RSP; break;
16366 }
16367 if (DestReg) {
16368 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016369 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016370 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016371 }
Craig Topperc9099502012-04-20 06:31:50 +000016372 } else if (Res.second == &X86::FR32RegClass ||
16373 Res.second == &X86::FR64RegClass ||
16374 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016375 // Handle references to XMM physical registers that got mapped into the
16376 // wrong class. This can happen with constraints like {xmm0} where the
16377 // target independent register mapper will just pick the first match it can
16378 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016379
16380 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016381 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016382 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016383 Res.second = &X86::FR64RegClass;
16384 else if (X86::VR128RegClass.hasType(VT))
16385 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016386 else if (X86::VR256RegClass.hasType(VT))
16387 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016388 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016389
Chris Lattnerf76d1802006-07-31 23:26:50 +000016390 return Res;
16391}