blob: 5dae9dca95f4bc39d4db77e4bad2158ea7ba5557 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Craig Topper1bf724b2012-02-19 07:15:48 +000016#include "X86ISelLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000019#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/VariadicFunction.h"
Evan Cheng55d42002011-01-08 01:24:27 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/GlobalAlias.h"
38#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/LLVMContext.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000042#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000044#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/MC/MCSymbol.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000161
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000162 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000163 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000164
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000166 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000169 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000170 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000172
Eric Christopherde5e1012011-03-11 01:05:58 +0000173 // For 64-bit since we have so many registers use the ILP scheduler, for
174 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000175 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000176 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000177 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000178 else if (Subtarget->is64Bit())
179 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000180 else
181 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000182 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000183
Preston Gurd2e2efd92012-09-04 18:22:17 +0000184 // Bypass i32 with i8 on Atom when compiling with O2
185 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
Preston Gurd8d662b52012-10-04 21:33:40 +0000186 addBypassSlowDiv(32, 8);
Preston Gurd2e2efd92012-09-04 18:22:17 +0000187
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000188 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000189 // Setup Windows compiler runtime calls.
190 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000191 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000192 setLibcallName(RTLIB::SREM_I64, "_allrem");
193 setLibcallName(RTLIB::UREM_I64, "_aullrem");
194 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000196 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000197 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
198 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000200
201 // The _ftol2 runtime function has an unusual calling conv, which
202 // is modeled by a special pseudo-instruction.
203 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
204 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
206 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000207 }
208
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000209 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000210 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000211 setUseUnderscoreSetJmp(false);
212 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000213 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 // MS runtime is weird: it exports _setjmp, but longjmp!
215 setUseUnderscoreSetJmp(true);
216 setUseUnderscoreLongJmp(false);
217 } else {
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(true);
220 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000223 addRegisterClass(MVT::i8, &X86::GR8RegClass);
224 addRegisterClass(MVT::i16, &X86::GR16RegClass);
225 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000227 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000230
Scott Michelfdc40a02009-02-17 22:15:04 +0000231 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000233 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000235 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
237 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000238
239 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
243 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000246
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
248 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
250 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000252
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000256 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000257 // We have an algorithm for SSE2->double, and we turn this into a
258 // 64-bit FILD followed by conditional FADD for other targets.
259 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000260 // We have an algorithm for SSE2, and we turn this into a 64-bit
261 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264
265 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
266 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
268 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000269
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000270 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000271 // SSE has no i16 to fp conversion, only i32
272 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000276 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000280 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
282 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000283 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284
Dale Johannesen73328d12007-09-19 23:55:34 +0000285 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
286 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
288 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000289
Evan Cheng02568ff2006-01-30 22:13:22 +0000290 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
291 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
293 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000294
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000295 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000297 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000299 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302 }
303
304 // Handle FP_TO_UINT by promoting the destination to a larger signed
305 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
307 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309
Evan Cheng25ab6902006-09-08 06:48:29 +0000310 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
312 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000313 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000314 // Since AVX is a superset of SSE3, only check for SSE here.
315 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 // Expand FP_TO_UINT into a select.
317 // FIXME: We would like to use a Custom expander here eventually to do
318 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000321 // With SSE3 we can use fisttpll to convert to a signed i64; without
322 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000325
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000326 if (isTargetFTOL()) {
327 // Use the _ftol2 runtime function, which has a pseudo-instruction
328 // to handle its weird calling convention.
329 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
330 }
331
Chris Lattner399610a2006-12-05 18:22:22 +0000332 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000333 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
335 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000336 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000337 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000338 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000339 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000340 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000341 }
Chris Lattner21f66852005-12-23 05:15:23 +0000342
Dan Gohmanb00ee212008-02-18 19:34:53 +0000343 // Scalar integer divide and remainder are lowered to use operations that
344 // produce two results, to match the available instructions. This exposes
345 // the two-result form to trivial CSE, which is able to combine x/y and x%y
346 // into a single instruction.
347 //
348 // Scalar integer multiply-high is also lowered to use two-result
349 // operations, to match the available instructions. However, plain multiply
350 // (low) operations are left as Legal, as there are single-result
351 // instructions for this in x86. Using the two-result multiply instructions
352 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000353 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000354 MVT VT = IntVTs[i];
355 setOperationAction(ISD::MULHS, VT, Expand);
356 setOperationAction(ISD::MULHU, VT, Expand);
357 setOperationAction(ISD::SDIV, VT, Expand);
358 setOperationAction(ISD::UDIV, VT, Expand);
359 setOperationAction(ISD::SREM, VT, Expand);
360 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000361
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000362 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000363 setOperationAction(ISD::ADDC, VT, Custom);
364 setOperationAction(ISD::ADDE, VT, Custom);
365 setOperationAction(ISD::SUBC, VT, Custom);
366 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000368
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
370 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
371 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
372 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
378 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
379 setOperationAction(ISD::FREM , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f64 , Expand);
381 setOperationAction(ISD::FREM , MVT::f80 , Expand);
382 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000383
Chandler Carruth77821022011-12-24 12:12:34 +0000384 // Promote the i8 variants and force them on up to i32 which has a shorter
385 // encoding.
386 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
387 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
388 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
389 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000390 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000391 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
393 if (Subtarget->is64Bit())
394 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000395 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000396 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
397 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
398 if (Subtarget->is64Bit())
399 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
400 }
Craig Topper37f21672011-10-11 06:44:02 +0000401
402 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000403 // When promoting the i8 variants, force them to i32 for a shorter
404 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000405 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000406 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
407 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
408 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000409 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
411 if (Subtarget->is64Bit())
412 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000413 } else {
414 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000417 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
420 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000421 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
423 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 }
425
Benjamin Kramer1292c222010-12-04 20:32:23 +0000426 if (Subtarget->hasPOPCNT()) {
427 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
428 } else {
429 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
432 if (Subtarget->is64Bit())
433 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
434 }
435
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
437 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000438
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000439 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000440 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000441 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000442 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000443 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
448 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000449 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000454 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000456 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000459 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
460 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000461 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000462 // other SjLj exception interfaces are implemented and please don't build
463 // your own exception handling based on them.
464 // LLVM/Clang supports zero-cost DWARF exception handling.
465 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
466 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000467
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000468 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
472 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000473 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
475 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000476 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
479 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
480 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
481 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000482 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000483 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000484 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
486 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
487 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000488 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
490 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
491 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000492 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493
Craig Topper1accb7e2012-01-10 06:54:16 +0000494 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000496
Eric Christopher9a9d2752010-07-22 02:48:34 +0000497 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000498 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000499
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000500 // On X86 and X86-64, atomic operations are lowered to locked instructions.
501 // Locked instructions, in turn, have implicit fence semantics (all memory
502 // operations are flushed before issuing the locked instruction, and they
503 // are not buffered), so we can fold away the common pattern of
504 // fence-atomic-fence.
505 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000506
Mon P Wang63307c32008-05-05 19:05:59 +0000507 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000508 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000509 MVT VT = IntVTs[i];
510 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000512 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000513 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000514
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000515 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000516 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
518 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
519 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
520 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
521 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
522 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
523 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000524 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
526 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
527 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000528 }
529
Eli Friedman43f51ae2011-08-26 21:21:21 +0000530 if (Subtarget->hasCmpxchg16b()) {
531 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
532 }
533
Evan Cheng3c992d22006-03-07 02:02:57 +0000534 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000535 if (!Subtarget->isTargetDarwin() &&
536 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000537 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000539 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000540
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
542 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
543 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
544 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000545 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000546 setExceptionPointerRegister(X86::RAX);
547 setExceptionSelectorRegister(X86::RDX);
548 } else {
549 setExceptionPointerRegister(X86::EAX);
550 setExceptionSelectorRegister(X86::EDX);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
553 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000554
Duncan Sands4a544a72011-09-06 13:37:06 +0000555 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
556 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000557
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000559 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000560
Nate Begemanacc398c2006-01-25 18:21:52 +0000561 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::VASTART , MVT::Other, Custom);
563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000564 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VAARG , MVT::Other, Custom);
566 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000567 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::VAARG , MVT::Other, Expand);
569 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000570 }
Evan Chengae642192007-03-02 23:16:35 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
573 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000574
575 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
576 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
577 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000578 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000579 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
580 MVT::i64 : MVT::i32, Custom);
581 else
582 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
583 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000584
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000585 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000587 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000588 addRegisterClass(MVT::f32, &X86::FR32RegClass);
589 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000590
Evan Cheng223547a2006-01-31 22:28:30 +0000591 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FABS , MVT::f64, Custom);
593 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000594
595 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FNEG , MVT::f64, Custom);
597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000598
Evan Cheng68c47cb2007-01-05 07:55:56 +0000599 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
601 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000602
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000603 // Lower this to FGETSIGNx86 plus an AND.
604 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
605 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
606
Evan Chengd25e9e82006-02-02 00:28:23 +0000607 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FSIN , MVT::f64, Expand);
609 setOperationAction(ISD::FCOS , MVT::f64, Expand);
610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612
Chris Lattnera54aa942006-01-29 06:26:08 +0000613 // Expand FP immediates into loads from the stack, except for the special
614 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 addLegalFPImmediate(APFloat(+0.0)); // xorpd
616 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000617 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618 // Use SSE for f32, x87 for f64.
619 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000620 addRegisterClass(MVT::f32, &X86::FR32RegClass);
621 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625
626 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000630
631 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634
635 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f32, Expand);
637 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
Nate Begemane1795842008-02-14 08:57:00 +0000639 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000640 addLegalFPImmediate(APFloat(+0.0f)); // xorps
641 addLegalFPImmediate(APFloat(+0.0)); // FLD0
642 addLegalFPImmediate(APFloat(+1.0)); // FLD1
643 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
644 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
645
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000646 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
648 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000649 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000650 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000653 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
654 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
657 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000660
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000661 if (!TM.Options.UnsafeFPMath) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000662 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000664 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000667 addLegalFPImmediate(APFloat(+0.0)); // FLD0
668 addLegalFPImmediate(APFloat(+1.0)); // FLD1
669 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
670 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000671 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
672 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
673 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
674 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000675 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Cameron Zwarich33390842011-07-08 21:39:21 +0000677 // We don't support FMA.
678 setOperationAction(ISD::FMA, MVT::f64, Expand);
679 setOperationAction(ISD::FMA, MVT::f32, Expand);
680
Dale Johannesen59a58732007-08-05 18:49:15 +0000681 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000682 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000683 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
685 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000686 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000687 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000688 addLegalFPImmediate(TmpFlt); // FLD0
689 TmpFlt.changeSign();
690 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000691
692 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000693 APFloat TmpFlt2(+1.0);
694 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
695 &ignored);
696 addLegalFPImmediate(TmpFlt2); // FLD1
697 TmpFlt2.changeSign();
698 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
699 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000700
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000701 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
703 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000704 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000705
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000706 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
707 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
708 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
709 setOperationAction(ISD::FRINT, MVT::f80, Expand);
710 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000712 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000713
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000714 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
716 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
717 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FLOG, MVT::f80, Expand);
720 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
721 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
722 setOperationAction(ISD::FEXP, MVT::f80, Expand);
723 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000724
Mon P Wangf007a8b2008-11-06 05:31:54 +0000725 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000726 // (for widening) or expand (for scalarization). Then we will selectively
727 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000728 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
729 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000730 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000731 setOperationAction(ISD::ADD , VT, Expand);
732 setOperationAction(ISD::SUB , VT, Expand);
733 setOperationAction(ISD::FADD, VT, Expand);
734 setOperationAction(ISD::FNEG, VT, Expand);
735 setOperationAction(ISD::FSUB, VT, Expand);
736 setOperationAction(ISD::MUL , VT, Expand);
737 setOperationAction(ISD::FMUL, VT, Expand);
738 setOperationAction(ISD::SDIV, VT, Expand);
739 setOperationAction(ISD::UDIV, VT, Expand);
740 setOperationAction(ISD::FDIV, VT, Expand);
741 setOperationAction(ISD::SREM, VT, Expand);
742 setOperationAction(ISD::UREM, VT, Expand);
743 setOperationAction(ISD::LOAD, VT, Expand);
744 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
747 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
748 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
749 setOperationAction(ISD::FABS, VT, Expand);
750 setOperationAction(ISD::FSIN, VT, Expand);
751 setOperationAction(ISD::FCOS, VT, Expand);
752 setOperationAction(ISD::FREM, VT, Expand);
753 setOperationAction(ISD::FMA, VT, Expand);
754 setOperationAction(ISD::FPOWI, VT, Expand);
755 setOperationAction(ISD::FSQRT, VT, Expand);
756 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
757 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000758 setOperationAction(ISD::FCEIL, VT, Expand);
759 setOperationAction(ISD::FTRUNC, VT, Expand);
760 setOperationAction(ISD::FRINT, VT, Expand);
761 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000762 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
763 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
764 setOperationAction(ISD::SDIVREM, VT, Expand);
765 setOperationAction(ISD::UDIVREM, VT, Expand);
766 setOperationAction(ISD::FPOW, VT, Expand);
767 setOperationAction(ISD::CTPOP, VT, Expand);
768 setOperationAction(ISD::CTTZ, VT, Expand);
769 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
770 setOperationAction(ISD::CTLZ, VT, Expand);
771 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
772 setOperationAction(ISD::SHL, VT, Expand);
773 setOperationAction(ISD::SRA, VT, Expand);
774 setOperationAction(ISD::SRL, VT, Expand);
775 setOperationAction(ISD::ROTL, VT, Expand);
776 setOperationAction(ISD::ROTR, VT, Expand);
777 setOperationAction(ISD::BSWAP, VT, Expand);
778 setOperationAction(ISD::SETCC, VT, Expand);
779 setOperationAction(ISD::FLOG, VT, Expand);
780 setOperationAction(ISD::FLOG2, VT, Expand);
781 setOperationAction(ISD::FLOG10, VT, Expand);
782 setOperationAction(ISD::FEXP, VT, Expand);
783 setOperationAction(ISD::FEXP2, VT, Expand);
784 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
785 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
786 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
787 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
788 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
789 setOperationAction(ISD::TRUNCATE, VT, Expand);
790 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
791 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
792 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
793 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000794 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
795 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000796 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000797 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000798 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
799 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
800 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000801 }
802
Evan Chengc7ce29b2009-02-13 22:36:38 +0000803 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
804 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000805 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000806 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000807 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Dale Johannesen0488fb62010-09-30 23:57:10 +0000810 // MMX-sized vectors (other than x86mmx) are expected to be expanded
811 // into smaller operations.
812 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
813 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
814 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
815 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
816 setOperationAction(ISD::AND, MVT::v8i8, Expand);
817 setOperationAction(ISD::AND, MVT::v4i16, Expand);
818 setOperationAction(ISD::AND, MVT::v2i32, Expand);
819 setOperationAction(ISD::AND, MVT::v1i64, Expand);
820 setOperationAction(ISD::OR, MVT::v8i8, Expand);
821 setOperationAction(ISD::OR, MVT::v4i16, Expand);
822 setOperationAction(ISD::OR, MVT::v2i32, Expand);
823 setOperationAction(ISD::OR, MVT::v1i64, Expand);
824 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
825 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
826 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
827 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
828 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
829 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
830 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
831 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
833 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
834 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
835 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
836 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000837 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
838 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
839 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
840 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000841
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000844
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
846 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
847 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
848 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
849 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
850 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000851 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
853 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
854 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
855 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
856 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000857 }
858
Craig Topper1accb7e2012-01-10 06:54:16 +0000859 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000860 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000861
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000862 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
863 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000864 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
865 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
866 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
867 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000868
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
870 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
871 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
872 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000873 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
875 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
876 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
877 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
878 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
879 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
880 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
881 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
882 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
883 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
884 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
885 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000886 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000887
Nadav Rotem354efd82011-09-18 14:57:03 +0000888 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000889 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
890 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
891 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
894 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000900 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000901 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000902 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000904 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000905 // Do not attempt to custom lower non-128-bit vectors
906 if (!VT.is128BitVector())
907 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000908 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
910 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000911 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
914 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
915 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
916 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
917 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
918 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000919
Nate Begemancdd1eec2008-02-12 22:51:28 +0000920 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000925 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000926 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000927 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000928
929 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000930 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000931 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000932
Craig Topper0d1f1762012-08-12 00:34:56 +0000933 setOperationAction(ISD::AND, VT, Promote);
934 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
935 setOperationAction(ISD::OR, VT, Promote);
936 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
937 setOperationAction(ISD::XOR, VT, Promote);
938 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
939 setOperationAction(ISD::LOAD, VT, Promote);
940 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
941 setOperationAction(ISD::SELECT, VT, Promote);
942 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000943 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000944
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000946
Evan Cheng2c3ae372006-04-12 21:21:57 +0000947 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
949 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
950 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
951 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
954 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000955
Michael Liaoa7554632012-10-23 17:36:08 +0000956 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
957 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000958 // As there is no 64-bit GPR available, we need build a special custom
959 // sequence to convert from v2i32 to v2f32.
960 if (!Subtarget->is64Bit())
961 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000962
Michael Liao9d796db2012-10-10 16:32:15 +0000963 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000964 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000965
Michael Liaob8150d82012-09-10 18:33:51 +0000966 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000967 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000968
Craig Topperd0a31172012-01-10 06:37:29 +0000969 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000970 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
971 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
972 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
973 setOperationAction(ISD::FRINT, MVT::f32, Legal);
974 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
975 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
976 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
977 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
978 setOperationAction(ISD::FRINT, MVT::f64, Legal);
979 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
980
Craig Topper12fb5c62012-09-08 17:42:27 +0000981 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000982 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
983 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
984 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
985 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000986 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +0000987 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
988 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
989 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
990 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +0000991
Nate Begeman14d12ca2008-02-11 04:19:36 +0000992 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000993 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000994
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000995 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
996 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
997 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
998 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
999 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001000
Nate Begeman14d12ca2008-02-11 04:19:36 +00001001 // i8 and i16 vectors are custom , because the source register and source
1002 // source memory operand types are not the same width. f32 vectors are
1003 // custom since the immediate controlling the insert encodes additional
1004 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1006 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1007 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1008 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1012 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1013 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001014
Pete Coopera77214a2011-11-14 19:38:42 +00001015 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001016 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001017 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001018 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1019 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001020 }
1021 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001022
Craig Topper1accb7e2012-01-10 06:54:16 +00001023 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001024 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001025 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001026
Nadav Rotem43012222011-05-11 08:12:09 +00001027 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001028 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001029
Nadav Rotem43012222011-05-11 08:12:09 +00001030 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001031 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001032
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001033 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001034 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1035 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1036
1037 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1038 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1039
1040 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1041 } else {
1042 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1043 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1044
1045 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1046 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1047
1048 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1049 }
Nadav Rotem43012222011-05-11 08:12:09 +00001050 }
1051
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001052 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001053 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1054 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1055 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1056 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1057 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1058 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001059
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001061 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1062 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001063
Owen Anderson825b72b2009-08-11 20:47:22 +00001064 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1065 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1066 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1067 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1068 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001069 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001070 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1071 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1072 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1073 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001074 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001075 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001076
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1078 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1079 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1080 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1081 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001082 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001083 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1084 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1085 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1086 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001088 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001089
Michael Liaobedcbd42012-10-16 18:14:11 +00001090 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001091 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001092
1093 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1094
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001095 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1096 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001097 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001098
Michael Liaoa7554632012-10-23 17:36:08 +00001099 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1100 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1101 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1102
Michael Liaob8150d82012-09-10 18:33:51 +00001103 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1104
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001105 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1106 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1107
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001108 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1109 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1110
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001111 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001112 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001113
Duncan Sands28b77e92011-09-06 19:07:46 +00001114 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1115 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1116 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1117 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001118
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001119 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1120 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1121 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1122
Craig Topperaaa643c2011-11-09 07:28:55 +00001123 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1124 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1125 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1126 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001127
Nadav Rotem0509db22012-12-28 05:45:24 +00001128 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1129 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1130 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1131 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1132 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1133 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001134
Craig Topperbf404372012-08-31 15:40:30 +00001135 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001136 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1137 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1138 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1139 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1140 setOperationAction(ISD::FMA, MVT::f32, Legal);
1141 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001142 }
Craig Topper880ef452012-08-11 22:34:26 +00001143
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001144 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001145 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1146 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1147 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1148 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001149
Craig Topperaaa643c2011-11-09 07:28:55 +00001150 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1151 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1152 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1153 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001154
Craig Topperaaa643c2011-11-09 07:28:55 +00001155 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1156 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1157 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001158 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001159
1160 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001161
1162 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1163 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1164
1165 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1166 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1167
1168 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001169 } else {
1170 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1171 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1172 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1173 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1174
1175 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1176 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1177 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1178 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1179
1180 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1181 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1182 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1183 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001184
1185 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1186 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1187
1188 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1189 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1190
1191 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001192 }
Craig Topper13894fa2011-08-24 06:14:18 +00001193
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001194 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001195 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1196 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001197 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001198
1199 // Extract subvector is special because the value type
1200 // (result) is 128-bit but the source is 256-bit wide.
1201 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001202 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001203
1204 // Do not attempt to custom lower other non-256-bit vectors
1205 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001206 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001207
Craig Topper0d1f1762012-08-12 00:34:56 +00001208 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1209 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1210 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1211 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1212 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1213 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1214 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001215 }
1216
David Greene54d8eba2011-01-27 22:38:56 +00001217 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001218 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001219 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001220
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001221 // Do not attempt to promote non-256-bit vectors
1222 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001223 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001224
Craig Topper0d1f1762012-08-12 00:34:56 +00001225 setOperationAction(ISD::AND, VT, Promote);
1226 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1227 setOperationAction(ISD::OR, VT, Promote);
1228 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1229 setOperationAction(ISD::XOR, VT, Promote);
1230 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1231 setOperationAction(ISD::LOAD, VT, Promote);
1232 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1233 setOperationAction(ISD::SELECT, VT, Promote);
1234 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001235 }
David Greene9b9838d2009-06-29 16:47:10 +00001236 }
1237
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001238 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1239 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001240 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1241 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001242 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1243 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001244 }
1245
Evan Cheng6be2c582006-04-05 23:38:46 +00001246 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001248 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001249
Eli Friedman962f5492010-06-02 19:35:46 +00001250 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1251 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001252 //
Eli Friedman962f5492010-06-02 19:35:46 +00001253 // FIXME: We really should do custom legalization for addition and
1254 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1255 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001256 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1257 // Add/Sub/Mul with overflow operations are custom lowered.
1258 MVT VT = IntVTs[i];
1259 setOperationAction(ISD::SADDO, VT, Custom);
1260 setOperationAction(ISD::UADDO, VT, Custom);
1261 setOperationAction(ISD::SSUBO, VT, Custom);
1262 setOperationAction(ISD::USUBO, VT, Custom);
1263 setOperationAction(ISD::SMULO, VT, Custom);
1264 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001265 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001266
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001267 // There are no 8-bit 3-address imul/mul instructions
1268 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1269 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001270
Evan Chengd54f2d52009-03-31 19:38:51 +00001271 if (!Subtarget->is64Bit()) {
1272 // These libcalls are not available in 32-bit.
1273 setLibcallName(RTLIB::SHL_I128, 0);
1274 setLibcallName(RTLIB::SRL_I128, 0);
1275 setLibcallName(RTLIB::SRA_I128, 0);
1276 }
1277
Evan Cheng206ee9d2006-07-07 08:33:52 +00001278 // We have target-specific dag combine patterns for the following nodes:
1279 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001280 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001281 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001282 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001283 setTargetDAGCombine(ISD::SHL);
1284 setTargetDAGCombine(ISD::SRA);
1285 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001286 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001287 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001288 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001289 setTargetDAGCombine(ISD::FADD);
1290 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001291 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001292 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001293 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001294 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001295 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001296 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001297 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001298 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001299 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001300 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001301 if (Subtarget->is64Bit())
1302 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001303 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001304
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001305 computeRegisterProperties();
1306
Evan Cheng05219282011-01-06 06:52:41 +00001307 // On Darwin, -Os means optimize for size without hurting performance,
1308 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001309 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001310 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001311 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001312 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1313 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1314 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001315 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001316 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001317
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001318 // Predictable cmov don't hurt on atom because it's in-order.
1319 predictableSelectIsExpensive = !Subtarget->isAtom();
1320
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001321 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001322}
1323
Duncan Sands28b77e92011-09-06 19:07:46 +00001324EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1325 if (!VT.isVector()) return MVT::i8;
1326 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001327}
1328
Evan Cheng29286502008-01-23 23:17:41 +00001329/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1330/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001331static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001332 if (MaxAlign == 16)
1333 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001334 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001335 if (VTy->getBitWidth() == 128)
1336 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001337 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001338 unsigned EltAlign = 0;
1339 getMaxByValAlign(ATy->getElementType(), EltAlign);
1340 if (EltAlign > MaxAlign)
1341 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001342 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001343 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1344 unsigned EltAlign = 0;
1345 getMaxByValAlign(STy->getElementType(i), EltAlign);
1346 if (EltAlign > MaxAlign)
1347 MaxAlign = EltAlign;
1348 if (MaxAlign == 16)
1349 break;
1350 }
1351 }
Evan Cheng29286502008-01-23 23:17:41 +00001352}
1353
1354/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1355/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001356/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1357/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001358unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001359 if (Subtarget->is64Bit()) {
1360 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001361 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001362 if (TyAlign > 8)
1363 return TyAlign;
1364 return 8;
1365 }
1366
Evan Cheng29286502008-01-23 23:17:41 +00001367 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001368 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001369 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001370 return Align;
1371}
Chris Lattner2b02a442007-02-25 08:29:00 +00001372
Evan Chengf0df0312008-05-15 08:39:06 +00001373/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001374/// and store operations as a result of memset, memcpy, and memmove
1375/// lowering. If DstAlign is zero that means it's safe to destination
1376/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1377/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001378/// probably because the source does not need to be loaded. If 'IsMemset' is
1379/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1380/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1381/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001382/// It returns EVT::Other if the type should be determined using generic
1383/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001384EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001385X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1386 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001387 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001388 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001389 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001390 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001391 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001392 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1393 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001394 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001395 (Subtarget->isUnalignedMemAccessFast() ||
1396 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001397 (SrcAlign == 0 || SrcAlign >= 16)))) {
1398 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001399 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001400 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001401 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001402 return MVT::v8f32;
1403 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001404 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001405 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001406 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001407 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001408 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001409 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001410 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001411 // Do not use f64 to lower memcpy if source is string constant. It's
1412 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001413 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001414 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001415 }
Evan Chengf0df0312008-05-15 08:39:06 +00001416 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 return MVT::i64;
1418 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001419}
1420
Evan Cheng7d342672012-12-12 01:32:07 +00001421bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001422 if (VT == MVT::f32)
1423 return X86ScalarSSEf32;
1424 else if (VT == MVT::f64)
1425 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001426 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001427}
1428
Evan Cheng376642e2012-12-10 23:21:26 +00001429bool
1430X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1431 if (Fast)
1432 *Fast = Subtarget->isUnalignedMemAccessFast();
1433 return true;
1434}
1435
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001436/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1437/// current function. The returned value is a member of the
1438/// MachineJumpTableInfo::JTEntryKind enum.
1439unsigned X86TargetLowering::getJumpTableEncoding() const {
1440 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1441 // symbol.
1442 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1443 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001444 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001445
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001446 // Otherwise, use the normal jump table encoding heuristics.
1447 return TargetLowering::getJumpTableEncoding();
1448}
1449
Chris Lattnerc64daab2010-01-26 05:02:42 +00001450const MCExpr *
1451X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1452 const MachineBasicBlock *MBB,
1453 unsigned uid,MCContext &Ctx) const{
1454 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1455 Subtarget->isPICStyleGOT());
1456 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1457 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001458 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1459 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001460}
1461
Evan Chengcc415862007-11-09 01:32:10 +00001462/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1463/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001464SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001465 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001466 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001467 // This doesn't have DebugLoc associated with it, but is not really the
1468 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001469 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001470 return Table;
1471}
1472
Chris Lattner589c6f62010-01-26 06:28:43 +00001473/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1474/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1475/// MCExpr.
1476const MCExpr *X86TargetLowering::
1477getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1478 MCContext &Ctx) const {
1479 // X86-64 uses RIP relative addressing based on the jump table label.
1480 if (Subtarget->isPICStyleRIPRel())
1481 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1482
1483 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001484 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001485}
1486
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001487// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001488std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001489X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001490 const TargetRegisterClass *RRC = 0;
1491 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001492 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001493 default:
1494 return TargetLowering::findRepresentativeClass(VT);
1495 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001496 RRC = Subtarget->is64Bit() ?
1497 (const TargetRegisterClass*)&X86::GR64RegClass :
1498 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001499 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001500 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001501 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001502 break;
1503 case MVT::f32: case MVT::f64:
1504 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1505 case MVT::v4f32: case MVT::v2f64:
1506 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1507 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001508 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001509 break;
1510 }
1511 return std::make_pair(RRC, Cost);
1512}
1513
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001514bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1515 unsigned &Offset) const {
1516 if (!Subtarget->isTargetLinux())
1517 return false;
1518
1519 if (Subtarget->is64Bit()) {
1520 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1521 Offset = 0x28;
1522 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1523 AddressSpace = 256;
1524 else
1525 AddressSpace = 257;
1526 } else {
1527 // %gs:0x14 on i386
1528 Offset = 0x14;
1529 AddressSpace = 256;
1530 }
1531 return true;
1532}
1533
Chris Lattner2b02a442007-02-25 08:29:00 +00001534//===----------------------------------------------------------------------===//
1535// Return Value Calling Convention Implementation
1536//===----------------------------------------------------------------------===//
1537
Chris Lattner59ed56b2007-02-28 04:55:35 +00001538#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001539
Michael J. Spencerec38de22010-10-10 22:04:20 +00001540bool
Eric Christopher471e4222011-06-08 23:55:35 +00001541X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001542 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001543 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001544 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001545 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001546 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001547 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001548 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001549}
1550
Dan Gohman98ca4f22009-08-05 01:29:28 +00001551SDValue
1552X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001553 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001555 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001556 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001557 MachineFunction &MF = DAG.getMachineFunction();
1558 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattner9774c912007-02-27 05:28:59 +00001560 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001561 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562 RVLocs, *DAG.getContext());
1563 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001564
Evan Chengdcea1632010-02-04 02:40:39 +00001565 // Add the regs to the liveout set for the function.
1566 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1567 for (unsigned i = 0; i != RVLocs.size(); ++i)
1568 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1569 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Dan Gohman475871a2008-07-27 21:46:04 +00001571 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001572
Dan Gohman475871a2008-07-27 21:46:04 +00001573 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001574 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1575 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001576 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1577 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001579 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001580 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1581 CCValAssign &VA = RVLocs[i];
1582 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001583 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001584 EVT ValVT = ValToCopy.getValueType();
1585
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001586 // Promote values to the appropriate types
1587 if (VA.getLocInfo() == CCValAssign::SExt)
1588 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1589 else if (VA.getLocInfo() == CCValAssign::ZExt)
1590 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1591 else if (VA.getLocInfo() == CCValAssign::AExt)
1592 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1593 else if (VA.getLocInfo() == CCValAssign::BCvt)
1594 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1595
Dale Johannesenc4510512010-09-24 19:05:48 +00001596 // If this is x86-64, and we disabled SSE, we can't return FP values,
1597 // or SSE or MMX vectors.
1598 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1599 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001600 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001601 report_fatal_error("SSE register return with SSE disabled");
1602 }
1603 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1604 // llvm-gcc has never done it right and no one has noticed, so this
1605 // should be OK for now.
1606 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001607 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001608 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001609
Chris Lattner447ff682008-03-11 03:23:40 +00001610 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1611 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001612 if (VA.getLocReg() == X86::ST0 ||
1613 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001614 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1615 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001616 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001617 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001618 RetOps.push_back(ValToCopy);
1619 // Don't emit a copytoreg.
1620 continue;
1621 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001622
Evan Cheng242b38b2009-02-23 09:03:22 +00001623 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1624 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001625 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001626 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001627 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001628 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001629 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1630 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001631 // If we don't have SSE2 available, convert to v4f32 so the generated
1632 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001633 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001634 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001635 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001636 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001637 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001638
Dale Johannesendd64c412009-02-04 00:33:20 +00001639 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001640 Flag = Chain.getValue(1);
1641 }
Dan Gohman61a92132008-04-21 23:59:07 +00001642
1643 // The x86-64 ABI for returning structs by value requires that we copy
1644 // the sret argument into %rax for the return. We saved the argument into
1645 // a virtual register in the entry block, so now we copy the value out
1646 // and into %rax.
1647 if (Subtarget->is64Bit() &&
1648 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1649 MachineFunction &MF = DAG.getMachineFunction();
1650 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1651 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001652 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001653 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001654 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001655
Dale Johannesendd64c412009-02-04 00:33:20 +00001656 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001657 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001658
1659 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001660 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001661 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001662
Chris Lattner447ff682008-03-11 03:23:40 +00001663 RetOps[0] = Chain; // Update chain.
1664
1665 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001666 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001667 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001668
1669 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001670 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001671}
1672
Evan Chengbf010eb2012-04-10 01:51:00 +00001673bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001674 if (N->getNumValues() != 1)
1675 return false;
1676 if (!N->hasNUsesOfValue(1, 0))
1677 return false;
1678
Evan Chengbf010eb2012-04-10 01:51:00 +00001679 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001680 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001681 if (Copy->getOpcode() == ISD::CopyToReg) {
1682 // If the copy has a glue operand, we conservatively assume it isn't safe to
1683 // perform a tail call.
1684 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1685 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001686 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001687 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001688 return false;
1689
Evan Cheng1bf891a2010-12-01 22:59:46 +00001690 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001691 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001692 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001693 if (UI->getOpcode() != X86ISD::RET_FLAG)
1694 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001695 HasRet = true;
1696 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001697
Evan Chengbf010eb2012-04-10 01:51:00 +00001698 if (!HasRet)
1699 return false;
1700
1701 Chain = TCChain;
1702 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001703}
1704
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001705MVT
1706X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001707 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001708 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001709 // TODO: Is this also valid on 32-bit?
1710 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001711 ReturnMVT = MVT::i8;
1712 else
1713 ReturnMVT = MVT::i32;
1714
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001715 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001716 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001717}
1718
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719/// LowerCallResult - Lower the result values of a call into the
1720/// appropriate copies out of appropriate physical registers.
1721///
1722SDValue
1723X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001724 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 const SmallVectorImpl<ISD::InputArg> &Ins,
1726 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001727 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001728
Chris Lattnere32bbf62007-02-28 07:09:55 +00001729 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001730 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001731 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001732 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001733 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001735
Chris Lattner3085e152007-02-25 08:59:22 +00001736 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001737 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001738 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001739 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Torok Edwin3f142c32009-02-01 18:15:56 +00001741 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001743 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001744 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001745 }
1746
Evan Cheng79fb3b42009-02-20 20:43:02 +00001747 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001748
1749 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001750 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001751 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001752 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001753 // instead.
1754 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1755 // If we prefer to use the value in xmm registers, copy it out as f80 and
1756 // use a truncate to move it from fp stack reg to xmm reg.
1757 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001758 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001759 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1760 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001761 Val = Chain.getValue(0);
1762
1763 // Round the f80 to the right size, which also moves it to the appropriate
1764 // xmm register.
1765 if (CopyVT != VA.getValVT())
1766 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1767 // This truncation won't change the value.
1768 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001769 } else {
1770 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1771 CopyVT, InFlag).getValue(1);
1772 Val = Chain.getValue(0);
1773 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001774 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001775 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001776 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001777
Dan Gohman98ca4f22009-08-05 01:29:28 +00001778 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001779}
1780
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001781//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001782// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001783//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001784// StdCall calling convention seems to be standard for many Windows' API
1785// routines and around. It differs from C calling convention just a little:
1786// callee should clean up the stack, not caller. Symbols should be also
1787// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001788// For info on fast calling convention see Fast Calling Convention (tail call)
1789// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001790
Dan Gohman98ca4f22009-08-05 01:29:28 +00001791/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001792/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001793enum StructReturnType {
1794 NotStructReturn,
1795 RegStructReturn,
1796 StackStructReturn
1797};
1798static StructReturnType
1799callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001801 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001802
Rafael Espindola1cee7102012-07-25 13:41:10 +00001803 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1804 if (!Flags.isSRet())
1805 return NotStructReturn;
1806 if (Flags.isInReg())
1807 return RegStructReturn;
1808 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001809}
1810
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001811/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001812/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001813static StructReturnType
1814argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001816 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001817
Rafael Espindola1cee7102012-07-25 13:41:10 +00001818 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1819 if (!Flags.isSRet())
1820 return NotStructReturn;
1821 if (Flags.isInReg())
1822 return RegStructReturn;
1823 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001824}
1825
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001826/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1827/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001828/// the specific parameter attribute. The copy will be passed as a byval
1829/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001830static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001831CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001832 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1833 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001834 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001835
Dale Johannesendd64c412009-02-04 00:33:20 +00001836 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001837 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001838 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001839}
1840
Chris Lattner29689432010-03-11 00:22:57 +00001841/// IsTailCallConvention - Return true if the calling convention is one that
1842/// supports tail call optimization.
1843static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001844 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1845 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001846}
1847
Evan Cheng485fafc2011-03-21 01:19:09 +00001848bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001849 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001850 return false;
1851
1852 CallSite CS(CI);
1853 CallingConv::ID CalleeCC = CS.getCallingConv();
1854 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1855 return false;
1856
1857 return true;
1858}
1859
Evan Cheng0c439eb2010-01-27 00:07:07 +00001860/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1861/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001862static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1863 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001864 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001865}
1866
Dan Gohman98ca4f22009-08-05 01:29:28 +00001867SDValue
1868X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001869 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001870 const SmallVectorImpl<ISD::InputArg> &Ins,
1871 DebugLoc dl, SelectionDAG &DAG,
1872 const CCValAssign &VA,
1873 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001874 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001875 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001877 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1878 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001879 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001880 EVT ValVT;
1881
1882 // If value is passed by pointer we have address passed instead of the value
1883 // itself.
1884 if (VA.getLocInfo() == CCValAssign::Indirect)
1885 ValVT = VA.getLocVT();
1886 else
1887 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001888
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001889 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001890 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001891 // In case of tail call optimization mark all arguments mutable. Since they
1892 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001893 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001894 unsigned Bytes = Flags.getByValSize();
1895 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1896 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001897 return DAG.getFrameIndex(FI, getPointerTy());
1898 } else {
1899 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001900 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001901 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1902 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001903 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001904 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001905 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001906}
1907
Dan Gohman475871a2008-07-27 21:46:04 +00001908SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001910 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911 bool isVarArg,
1912 const SmallVectorImpl<ISD::InputArg> &Ins,
1913 DebugLoc dl,
1914 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001915 SmallVectorImpl<SDValue> &InVals)
1916 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001917 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001918 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001919
Gordon Henriksen86737662008-01-05 16:56:59 +00001920 const Function* Fn = MF.getFunction();
1921 if (Fn->hasExternalLinkage() &&
1922 Subtarget->isTargetCygMing() &&
1923 Fn->getName() == "main")
1924 FuncInfo->setForceFramePointer(true);
1925
Evan Cheng1bc78042006-04-26 01:20:17 +00001926 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001927 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001928 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001929 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001930
Chris Lattner29689432010-03-11 00:22:57 +00001931 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001932 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001933
Chris Lattner638402b2007-02-28 07:00:42 +00001934 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001935 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001936 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001937 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001938
1939 // Allocate shadow area for Win64
1940 if (IsWin64) {
1941 CCInfo.AllocateStack(32, 8);
1942 }
1943
Duncan Sands45907662010-10-31 13:21:44 +00001944 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001945
Chris Lattnerf39f7712007-02-28 05:46:49 +00001946 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001947 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001948 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1949 CCValAssign &VA = ArgLocs[i];
1950 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1951 // places.
1952 assert(VA.getValNo() != LastVal &&
1953 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001954 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001955 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Chris Lattnerf39f7712007-02-28 05:46:49 +00001957 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001958 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001959 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001961 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001963 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001965 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001967 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001968 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001969 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001970 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001971 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001972 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001973 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001974 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001975 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001976
Devang Patel68e6bee2011-02-21 23:21:26 +00001977 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001979
Chris Lattnerf39f7712007-02-28 05:46:49 +00001980 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1981 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1982 // right size.
1983 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001984 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001985 DAG.getValueType(VA.getValVT()));
1986 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001987 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001988 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001989 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001990 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001992 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001993 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001994 if (RegVT.isVector())
1995 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
1996 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001997 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001998 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001999 } else {
2000 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002002 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002003
2004 // If value is passed via pointer - do a load.
2005 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002006 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002007 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002008
Dan Gohman98ca4f22009-08-05 01:29:28 +00002009 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002010 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002011
Dan Gohman61a92132008-04-21 23:59:07 +00002012 // The x86-64 ABI for returning structs by value requires that we copy
2013 // the sret argument into %rax for the return. Save the argument into
2014 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002015 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002016 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2017 unsigned Reg = FuncInfo->getSRetReturnReg();
2018 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00002020 FuncInfo->setSRetReturnReg(Reg);
2021 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002022 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002024 }
2025
Chris Lattnerf39f7712007-02-28 05:46:49 +00002026 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002027 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002028 if (FuncIsMadeTailCallSafe(CallConv,
2029 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002030 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002031
Evan Cheng1bc78042006-04-26 01:20:17 +00002032 // If the function takes variable number of arguments, make a frame index for
2033 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002034 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002035 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2036 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002037 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 }
2039 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002040 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2041
2042 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002043 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002044 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002046 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002047 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2048 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002049 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002050 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2051 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2052 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002053 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002054 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002055
2056 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002057 // The XMM registers which might contain var arg parameters are shadowed
2058 // in their paired GPR. So we only need to save the GPR to their home
2059 // slots.
2060 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002062 } else {
2063 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2064 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002065
Chad Rosier30450e82011-12-22 22:35:21 +00002066 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2067 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002068 }
2069 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2070 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002071
Bill Wendling831737d2012-12-30 10:32:01 +00002072 bool NoImplicitFloatOps = Fn->getAttributes().
2073 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002074 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002075 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002076 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2077 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002078 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002079 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002080 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002081 // Kernel mode asks for SSE to be disabled, so don't push them
2082 // on the stack.
2083 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002084
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002085 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002086 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002087 // Get to the caller-allocated home save location. Add 8 to account
2088 // for the return address.
2089 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002090 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002091 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002092 // Fixup to set vararg frame on shadow area (4 x i64).
2093 if (NumIntRegs < 4)
2094 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002095 } else {
2096 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002097 // registers, then we must store them to their spots on the stack so
2098 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002099 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2100 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2101 FuncInfo->setRegSaveFrameIndex(
2102 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002103 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002104 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002105
Gordon Henriksen86737662008-01-05 16:56:59 +00002106 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002107 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002108 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2109 getPointerTy());
2110 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002111 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002112 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2113 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002114 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002115 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002116 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002117 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002118 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002119 MachinePointerInfo::getFixedStack(
2120 FuncInfo->getRegSaveFrameIndex(), Offset),
2121 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002122 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002123 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002124 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002125
Dan Gohmanface41a2009-08-16 21:24:25 +00002126 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2127 // Now store the XMM (fp + vector) parameter registers.
2128 SmallVector<SDValue, 11> SaveXMMOps;
2129 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002130
Craig Topperc9099502012-04-20 06:31:50 +00002131 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002132 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2133 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002134
Dan Gohman1e93df62010-04-17 14:41:14 +00002135 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2136 FuncInfo->getRegSaveFrameIndex()));
2137 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2138 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002139
Dan Gohmanface41a2009-08-16 21:24:25 +00002140 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002141 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002142 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002143 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2144 SaveXMMOps.push_back(Val);
2145 }
2146 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2147 MVT::Other,
2148 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002150
2151 if (!MemOps.empty())
2152 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2153 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002154 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002156
Gordon Henriksen86737662008-01-05 16:56:59 +00002157 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002158 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2159 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002160 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002161 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002162 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002163 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002164 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002165 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002166 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002167 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002168
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002170 // RegSaveFrameIndex is X86-64 only.
2171 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002172 if (CallConv == CallingConv::X86_FastCall ||
2173 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002174 // fastcc functions can't have varargs.
2175 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002176 }
Evan Cheng25caf632006-05-23 21:06:34 +00002177
Rafael Espindola76927d752011-08-30 19:39:58 +00002178 FuncInfo->setArgumentStackSize(StackSize);
2179
Dan Gohman98ca4f22009-08-05 01:29:28 +00002180 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002181}
2182
Dan Gohman475871a2008-07-27 21:46:04 +00002183SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002184X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2185 SDValue StackPtr, SDValue Arg,
2186 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002187 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002188 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002189 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002190 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002191 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002192 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002193 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002194
2195 return DAG.getStore(Chain, dl, Arg, PtrOff,
2196 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002197 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002198}
2199
Bill Wendling64e87322009-01-16 19:25:27 +00002200/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002201/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002202SDValue
2203X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002204 SDValue &OutRetAddr, SDValue Chain,
2205 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002206 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002207 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002208 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002209 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002210
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002211 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002212 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002213 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002214 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002215}
2216
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002217/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002218/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002219static SDValue
2220EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002221 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2222 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002223 // Store the return address to the appropriate stack slot.
2224 if (!FPDiff) return Chain;
2225 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002226 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002227 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002228 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002229 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002230 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002231 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002232 return Chain;
2233}
2234
Dan Gohman98ca4f22009-08-05 01:29:28 +00002235SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002236X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002237 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002238 SelectionDAG &DAG = CLI.DAG;
2239 DebugLoc &dl = CLI.DL;
2240 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2241 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2242 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2243 SDValue Chain = CLI.Chain;
2244 SDValue Callee = CLI.Callee;
2245 CallingConv::ID CallConv = CLI.CallConv;
2246 bool &isTailCall = CLI.IsTailCall;
2247 bool isVarArg = CLI.IsVarArg;
2248
Dan Gohman98ca4f22009-08-05 01:29:28 +00002249 MachineFunction &MF = DAG.getMachineFunction();
2250 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002251 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002252 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002253 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002254 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002255
Nick Lewycky22de16d2012-01-19 00:34:10 +00002256 if (MF.getTarget().Options.DisableTailCalls)
2257 isTailCall = false;
2258
Evan Cheng5f941932010-02-05 02:21:12 +00002259 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002260 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002261 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002262 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002263 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002264 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002265
2266 // Sibcalls are automatically detected tailcalls which do not require
2267 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002268 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002269 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002270
2271 if (isTailCall)
2272 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002273 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002274
Chris Lattner29689432010-03-11 00:22:57 +00002275 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002276 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002277
Chris Lattner638402b2007-02-28 07:00:42 +00002278 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002279 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002280 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002281 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002282
2283 // Allocate shadow area for Win64
2284 if (IsWin64) {
2285 CCInfo.AllocateStack(32, 8);
2286 }
2287
Duncan Sands45907662010-10-31 13:21:44 +00002288 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002289
Chris Lattner423c5f42007-02-28 05:31:48 +00002290 // Get a count of how many bytes are to be pushed on the stack.
2291 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002292 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002293 // This is a sibcall. The memory operands are available in caller's
2294 // own caller's stack.
2295 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002296 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2297 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002298 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002299
Gordon Henriksen86737662008-01-05 16:56:59 +00002300 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002301 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002302 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002303 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2304 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2305
Gordon Henriksen86737662008-01-05 16:56:59 +00002306 FPDiff = NumBytesCallerPushed - NumBytes;
2307
2308 // Set the delta of movement of the returnaddr stackslot.
2309 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002310 if (FPDiff < X86Info->getTCReturnAddrDelta())
2311 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002312 }
2313
Evan Chengf22f9b32010-02-06 03:28:46 +00002314 if (!IsSibcall)
2315 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002316
Dan Gohman475871a2008-07-27 21:46:04 +00002317 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002318 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002319 if (isTailCall && FPDiff)
2320 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2321 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002322
Dan Gohman475871a2008-07-27 21:46:04 +00002323 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2324 SmallVector<SDValue, 8> MemOpChains;
2325 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002326
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002327 // Walk the register/memloc assignments, inserting copies/loads. In the case
2328 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002329 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2330 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002331 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002332 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002333 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002334 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002335
Chris Lattner423c5f42007-02-28 05:31:48 +00002336 // Promote the value if needed.
2337 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002338 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002339 case CCValAssign::Full: break;
2340 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002341 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002342 break;
2343 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002344 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002345 break;
2346 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002347 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002348 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002349 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002350 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2351 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002352 } else
2353 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2354 break;
2355 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002356 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002357 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002358 case CCValAssign::Indirect: {
2359 // Store the argument.
2360 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002361 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002362 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002363 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002364 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002365 Arg = SpillSlot;
2366 break;
2367 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002368 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002369
Chris Lattner423c5f42007-02-28 05:31:48 +00002370 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002371 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2372 if (isVarArg && IsWin64) {
2373 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2374 // shadow reg if callee is a varargs function.
2375 unsigned ShadowReg = 0;
2376 switch (VA.getLocReg()) {
2377 case X86::XMM0: ShadowReg = X86::RCX; break;
2378 case X86::XMM1: ShadowReg = X86::RDX; break;
2379 case X86::XMM2: ShadowReg = X86::R8; break;
2380 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002381 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002382 if (ShadowReg)
2383 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002384 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002385 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002386 assert(VA.isMemLoc());
2387 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002388 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2389 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002390 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2391 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002392 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002394
Evan Cheng32fe1032006-05-25 00:59:30 +00002395 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002396 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002397 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002398
Chris Lattner88e1fd52009-07-09 04:24:46 +00002399 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002400 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2401 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002403 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2404 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002405 } else {
2406 // If we are tail calling and generating PIC/GOT style code load the
2407 // address of the callee into ECX. The value in ecx is used as target of
2408 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2409 // for tail calls on PIC/GOT architectures. Normally we would just put the
2410 // address of GOT into ebx and then call target@PLT. But for tail calls
2411 // ebx would be restored (since ebx is callee saved) before jumping to the
2412 // target@PLT.
2413
2414 // Note: The actual moving to ECX is done further down.
2415 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2416 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2417 !G->getGlobal()->hasProtectedVisibility())
2418 Callee = LowerGlobalAddress(Callee, DAG);
2419 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002420 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002421 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002422 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002423
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002424 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002425 // From AMD64 ABI document:
2426 // For calls that may call functions that use varargs or stdargs
2427 // (prototype-less calls or calls to functions containing ellipsis (...) in
2428 // the declaration) %al is used as hidden argument to specify the number
2429 // of SSE registers used. The contents of %al do not need to match exactly
2430 // the number of registers, but must be an ubound on the number of SSE
2431 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002432
Gordon Henriksen86737662008-01-05 16:56:59 +00002433 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002434 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002435 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2436 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2437 };
2438 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002439 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002440 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002441
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002442 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2443 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002444 }
2445
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002446 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 if (isTailCall) {
2448 // Force all the incoming stack arguments to be loaded from the stack
2449 // before any new outgoing arguments are stored to the stack, because the
2450 // outgoing stack slots may alias the incoming argument stack slots, and
2451 // the alias isn't otherwise explicit. This is slightly more conservative
2452 // than necessary, because it means that each store effectively depends
2453 // on every argument instead of just those arguments it would clobber.
2454 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2455
Dan Gohman475871a2008-07-27 21:46:04 +00002456 SmallVector<SDValue, 8> MemOpChains2;
2457 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002458 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002459 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002460 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2461 CCValAssign &VA = ArgLocs[i];
2462 if (VA.isRegLoc())
2463 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002464 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002465 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002466 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 // Create frame index.
2468 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002469 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002470 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002471 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002472
Duncan Sands276dcbd2008-03-21 09:14:45 +00002473 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002474 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002475 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002477 StackPtr = DAG.getCopyFromReg(Chain, dl,
2478 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002479 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002480 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002481
Dan Gohman98ca4f22009-08-05 01:29:28 +00002482 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2483 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002484 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002485 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002486 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002487 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002488 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002489 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002490 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002491 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002492 }
2493 }
2494
2495 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002497 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002498
2499 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002500 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2501 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002502 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002503 }
2504
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002505 // Build a sequence of copy-to-reg nodes chained together with token chain
2506 // and flag operands which copy the outgoing args into registers.
2507 SDValue InFlag;
2508 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2509 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2510 RegsToPass[i].second, InFlag);
2511 InFlag = Chain.getValue(1);
2512 }
2513
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002514 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2515 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2516 // In the 64-bit large code model, we have to make all calls
2517 // through a register, since the call instruction's 32-bit
2518 // pc-relative offset may not be large enough to hold the whole
2519 // address.
2520 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002521 // If the callee is a GlobalAddress node (quite common, every direct call
2522 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2523 // it.
2524
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002525 // We should use extra load for direct calls to dllimported functions in
2526 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002527 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002528 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002529 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002530 bool ExtraLoad = false;
2531 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002532
Chris Lattner48a7d022009-07-09 05:02:21 +00002533 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2534 // external symbols most go through the PLT in PIC mode. If the symbol
2535 // has hidden or protected visibility, or if it is static or local, then
2536 // we don't need to use the PLT - we can directly call it.
2537 if (Subtarget->isTargetELF() &&
2538 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002539 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002540 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002541 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002542 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002543 (!Subtarget->getTargetTriple().isMacOSX() ||
2544 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002545 // PC-relative references to external symbols should go through $stub,
2546 // unless we're building with the leopard linker or later, which
2547 // automatically synthesizes these stubs.
2548 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002549 } else if (Subtarget->isPICStyleRIPRel() &&
2550 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002551 cast<Function>(GV)->getAttributes().
2552 hasAttribute(AttributeSet::FunctionIndex,
2553 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002554 // If the function is marked as non-lazy, generate an indirect call
2555 // which loads from the GOT directly. This avoids runtime overhead
2556 // at the cost of eager binding (and one extra byte of encoding).
2557 OpFlags = X86II::MO_GOTPCREL;
2558 WrapperKind = X86ISD::WrapperRIP;
2559 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002560 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002561
Devang Patel0d881da2010-07-06 22:08:15 +00002562 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002563 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002564
2565 // Add a wrapper if needed.
2566 if (WrapperKind != ISD::DELETED_NODE)
2567 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2568 // Add extra indirection if needed.
2569 if (ExtraLoad)
2570 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2571 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002572 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002573 }
Bill Wendling056292f2008-09-16 21:48:12 +00002574 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002575 unsigned char OpFlags = 0;
2576
Evan Cheng1bf891a2010-12-01 22:59:46 +00002577 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2578 // external symbols should go through the PLT.
2579 if (Subtarget->isTargetELF() &&
2580 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2581 OpFlags = X86II::MO_PLT;
2582 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002583 (!Subtarget->getTargetTriple().isMacOSX() ||
2584 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002585 // PC-relative references to external symbols should go through $stub,
2586 // unless we're building with the leopard linker or later, which
2587 // automatically synthesizes these stubs.
2588 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002589 }
Eric Christopherfd179292009-08-27 18:07:15 +00002590
Chris Lattner48a7d022009-07-09 05:02:21 +00002591 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2592 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002593 }
2594
Chris Lattnerd96d0722007-02-25 06:40:16 +00002595 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002596 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002597 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002598
Evan Chengf22f9b32010-02-06 03:28:46 +00002599 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002600 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2601 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002602 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002604
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002605 Ops.push_back(Chain);
2606 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002607
Dan Gohman98ca4f22009-08-05 01:29:28 +00002608 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002609 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002610
Gordon Henriksen86737662008-01-05 16:56:59 +00002611 // Add argument registers to the end of the list so that they are known live
2612 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002613 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2614 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2615 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002616
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002617 // Add a register mask operand representing the call-preserved registers.
2618 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2619 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2620 assert(Mask && "Missing call preserved mask for calling convention");
2621 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002622
Gabor Greifba36cb52008-08-28 21:40:38 +00002623 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002624 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002625
Dan Gohman98ca4f22009-08-05 01:29:28 +00002626 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002627 // We used to do:
2628 //// If this is the first return lowered for this function, add the regs
2629 //// to the liveout set for the function.
2630 // This isn't right, although it's probably harmless on x86; liveouts
2631 // should be computed from returns not tail calls. Consider a void
2632 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002633 return DAG.getNode(X86ISD::TC_RETURN, dl,
2634 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002635 }
2636
Dale Johannesenace16102009-02-03 19:33:06 +00002637 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002638 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002639
Chris Lattner2d297092006-05-23 18:50:38 +00002640 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002641 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002642 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2643 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002644 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002645 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002646 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002647 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002648 // pops the hidden struct pointer, so we have to push it back.
2649 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002650 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002651 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002652 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002653 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002654
Gordon Henriksenae636f82008-01-03 16:47:34 +00002655 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002656 if (!IsSibcall) {
2657 Chain = DAG.getCALLSEQ_END(Chain,
2658 DAG.getIntPtrConstant(NumBytes, true),
2659 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2660 true),
2661 InFlag);
2662 InFlag = Chain.getValue(1);
2663 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002664
Chris Lattner3085e152007-02-25 08:59:22 +00002665 // Handle result values, copying them out of physregs into vregs that we
2666 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002667 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2668 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002669}
2670
Evan Cheng25ab6902006-09-08 06:48:29 +00002671//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002672// Fast Calling Convention (tail call) implementation
2673//===----------------------------------------------------------------------===//
2674
2675// Like std call, callee cleans arguments, convention except that ECX is
2676// reserved for storing the tail called function address. Only 2 registers are
2677// free for argument passing (inreg). Tail call optimization is performed
2678// provided:
2679// * tailcallopt is enabled
2680// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002681// On X86_64 architecture with GOT-style position independent code only local
2682// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002683// To keep the stack aligned according to platform abi the function
2684// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2685// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002686// If a tail called function callee has more arguments than the caller the
2687// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002688// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002689// original REtADDR, but before the saved framepointer or the spilled registers
2690// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2691// stack layout:
2692// arg1
2693// arg2
2694// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002695// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002696// move area ]
2697// (possible EBP)
2698// ESI
2699// EDI
2700// local1 ..
2701
2702/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2703/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002704unsigned
2705X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2706 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002707 MachineFunction &MF = DAG.getMachineFunction();
2708 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002709 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002710 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002711 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002712 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002713 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002714 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2715 // Number smaller than 12 so just add the difference.
2716 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2717 } else {
2718 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002719 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002720 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002721 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002722 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002723}
2724
Evan Cheng5f941932010-02-05 02:21:12 +00002725/// MatchingStackOffset - Return true if the given stack call argument is
2726/// already available in the same position (relatively) of the caller's
2727/// incoming argument stack.
2728static
2729bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2730 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2731 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002732 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2733 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002734 if (Arg.getOpcode() == ISD::CopyFromReg) {
2735 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002736 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002737 return false;
2738 MachineInstr *Def = MRI->getVRegDef(VR);
2739 if (!Def)
2740 return false;
2741 if (!Flags.isByVal()) {
2742 if (!TII->isLoadFromStackSlot(Def, FI))
2743 return false;
2744 } else {
2745 unsigned Opcode = Def->getOpcode();
2746 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2747 Def->getOperand(1).isFI()) {
2748 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002749 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002750 } else
2751 return false;
2752 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002753 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2754 if (Flags.isByVal())
2755 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002756 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002757 // define @foo(%struct.X* %A) {
2758 // tail call @bar(%struct.X* byval %A)
2759 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002760 return false;
2761 SDValue Ptr = Ld->getBasePtr();
2762 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2763 if (!FINode)
2764 return false;
2765 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002766 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002767 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002768 FI = FINode->getIndex();
2769 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002770 } else
2771 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002772
Evan Cheng4cae1332010-03-05 08:38:04 +00002773 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002774 if (!MFI->isFixedObjectIndex(FI))
2775 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002776 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002777}
2778
Dan Gohman98ca4f22009-08-05 01:29:28 +00002779/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2780/// for tail call optimization. Targets which want to do tail call
2781/// optimization should implement this function.
2782bool
2783X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002784 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002785 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002786 bool isCalleeStructRet,
2787 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002788 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002789 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002790 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002791 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002792 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002793 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002794 CalleeCC != CallingConv::C)
2795 return false;
2796
Evan Cheng7096ae42010-01-29 06:45:59 +00002797 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002798 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002799 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002800
2801 // If the function return type is x86_fp80 and the callee return type is not,
2802 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2803 // perform a tailcall optimization here.
2804 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2805 return false;
2806
Evan Cheng13617962010-04-30 01:12:32 +00002807 CallingConv::ID CallerCC = CallerF->getCallingConv();
2808 bool CCMatch = CallerCC == CalleeCC;
2809
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002810 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002811 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002812 return true;
2813 return false;
2814 }
2815
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002816 // Look for obvious safe cases to perform tail call optimization that do not
2817 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002818
Evan Cheng2c12cb42010-03-26 16:26:03 +00002819 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2820 // emit a special epilogue.
2821 if (RegInfo->needsStackRealignment(MF))
2822 return false;
2823
Evan Chenga375d472010-03-15 18:54:48 +00002824 // Also avoid sibcall optimization if either caller or callee uses struct
2825 // return semantics.
2826 if (isCalleeStructRet || isCallerStructRet)
2827 return false;
2828
Chad Rosier2416da32011-06-24 21:15:36 +00002829 // An stdcall caller is expected to clean up its arguments; the callee
2830 // isn't going to do that.
2831 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2832 return false;
2833
Chad Rosier871f6642011-05-18 19:59:50 +00002834 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002835 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002836 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002837
2838 // Optimizing for varargs on Win64 is unlikely to be safe without
2839 // additional testing.
2840 if (Subtarget->isTargetWin64())
2841 return false;
2842
Chad Rosier871f6642011-05-18 19:59:50 +00002843 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002844 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002845 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002846
Chad Rosier871f6642011-05-18 19:59:50 +00002847 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2848 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2849 if (!ArgLocs[i].isRegLoc())
2850 return false;
2851 }
2852
Chad Rosier30450e82011-12-22 22:35:21 +00002853 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2854 // stack. Therefore, if it's not used by the call it is not safe to optimize
2855 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002856 bool Unused = false;
2857 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2858 if (!Ins[i].Used) {
2859 Unused = true;
2860 break;
2861 }
2862 }
2863 if (Unused) {
2864 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002865 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002866 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002867 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002868 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002869 CCValAssign &VA = RVLocs[i];
2870 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2871 return false;
2872 }
2873 }
2874
Evan Cheng13617962010-04-30 01:12:32 +00002875 // If the calling conventions do not match, then we'd better make sure the
2876 // results are returned in the same way as what the caller expects.
2877 if (!CCMatch) {
2878 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002879 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002880 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002881 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2882
2883 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002884 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002885 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002886 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2887
2888 if (RVLocs1.size() != RVLocs2.size())
2889 return false;
2890 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2891 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2892 return false;
2893 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2894 return false;
2895 if (RVLocs1[i].isRegLoc()) {
2896 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2897 return false;
2898 } else {
2899 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2900 return false;
2901 }
2902 }
2903 }
2904
Evan Chenga6bff982010-01-30 01:22:00 +00002905 // If the callee takes no arguments then go on to check the results of the
2906 // call.
2907 if (!Outs.empty()) {
2908 // Check if stack adjustment is needed. For now, do not do this if any
2909 // argument is passed on the stack.
2910 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002911 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002912 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002913
2914 // Allocate shadow area for Win64
2915 if (Subtarget->isTargetWin64()) {
2916 CCInfo.AllocateStack(32, 8);
2917 }
2918
Duncan Sands45907662010-10-31 13:21:44 +00002919 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002920 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002921 MachineFunction &MF = DAG.getMachineFunction();
2922 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2923 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002924
2925 // Check if the arguments are already laid out in the right way as
2926 // the caller's fixed stack objects.
2927 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002928 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2929 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002930 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002931 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2932 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002933 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002934 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002935 if (VA.getLocInfo() == CCValAssign::Indirect)
2936 return false;
2937 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002938 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2939 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002940 return false;
2941 }
2942 }
2943 }
Evan Cheng9c044672010-05-29 01:35:22 +00002944
2945 // If the tailcall address may be in a register, then make sure it's
2946 // possible to register allocate for it. In 32-bit, the call address can
2947 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002948 // callee-saved registers are restored. These happen to be the same
2949 // registers used to pass 'inreg' arguments so watch out for those.
2950 if (!Subtarget->is64Bit() &&
2951 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002952 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002953 unsigned NumInRegs = 0;
2954 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2955 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002956 if (!VA.isRegLoc())
2957 continue;
2958 unsigned Reg = VA.getLocReg();
2959 switch (Reg) {
2960 default: break;
2961 case X86::EAX: case X86::EDX: case X86::ECX:
2962 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002963 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002964 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002965 }
2966 }
2967 }
Evan Chenga6bff982010-01-30 01:22:00 +00002968 }
Evan Chengb1712452010-01-27 06:25:16 +00002969
Evan Cheng86809cc2010-02-03 03:28:02 +00002970 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002971}
2972
Dan Gohman3df24e62008-09-03 23:12:08 +00002973FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002974X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2975 const TargetLibraryInfo *libInfo) const {
2976 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002977}
2978
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002979//===----------------------------------------------------------------------===//
2980// Other Lowering Hooks
2981//===----------------------------------------------------------------------===//
2982
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002983static bool MayFoldLoad(SDValue Op) {
2984 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2985}
2986
2987static bool MayFoldIntoStore(SDValue Op) {
2988 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2989}
2990
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002991static bool isTargetShuffle(unsigned Opcode) {
2992 switch(Opcode) {
2993 default: return false;
2994 case X86ISD::PSHUFD:
2995 case X86ISD::PSHUFHW:
2996 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002997 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002998 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002999 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003000 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003001 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003002 case X86ISD::MOVLPS:
3003 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003004 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003005 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003006 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003007 case X86ISD::MOVSS:
3008 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003009 case X86ISD::UNPCKL:
3010 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003011 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003012 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003013 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003014 return true;
3015 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003016}
3017
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003018static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003019 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003020 switch(Opc) {
3021 default: llvm_unreachable("Unknown x86 shuffle node");
3022 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003023 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003024 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003025 return DAG.getNode(Opc, dl, VT, V1);
3026 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003027}
3028
3029static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003030 SDValue V1, unsigned TargetMask,
3031 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003032 switch(Opc) {
3033 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003034 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003035 case X86ISD::PSHUFHW:
3036 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003037 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003038 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003039 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3040 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003041}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003042
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003043static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003044 SDValue V1, SDValue V2, unsigned TargetMask,
3045 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003046 switch(Opc) {
3047 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00003048 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00003049 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003050 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003051 return DAG.getNode(Opc, dl, VT, V1, V2,
3052 DAG.getConstant(TargetMask, MVT::i8));
3053 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003054}
3055
3056static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3057 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3058 switch(Opc) {
3059 default: llvm_unreachable("Unknown x86 shuffle node");
3060 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003061 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003062 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003063 case X86ISD::MOVLPS:
3064 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003065 case X86ISD::MOVSS:
3066 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003067 case X86ISD::UNPCKL:
3068 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003069 return DAG.getNode(Opc, dl, VT, V1, V2);
3070 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003071}
3072
Dan Gohmand858e902010-04-17 15:26:15 +00003073SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003074 MachineFunction &MF = DAG.getMachineFunction();
3075 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3076 int ReturnAddrIndex = FuncInfo->getRAIndex();
3077
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003078 if (ReturnAddrIndex == 0) {
3079 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003080 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003081 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003082 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003083 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003084 }
3085
Evan Cheng25ab6902006-09-08 06:48:29 +00003086 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003087}
3088
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003089bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3090 bool hasSymbolicDisplacement) {
3091 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003092 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003093 return false;
3094
3095 // If we don't have a symbolic displacement - we don't have any extra
3096 // restrictions.
3097 if (!hasSymbolicDisplacement)
3098 return true;
3099
3100 // FIXME: Some tweaks might be needed for medium code model.
3101 if (M != CodeModel::Small && M != CodeModel::Kernel)
3102 return false;
3103
3104 // For small code model we assume that latest object is 16MB before end of 31
3105 // bits boundary. We may also accept pretty large negative constants knowing
3106 // that all objects are in the positive half of address space.
3107 if (M == CodeModel::Small && Offset < 16*1024*1024)
3108 return true;
3109
3110 // For kernel code model we know that all object resist in the negative half
3111 // of 32bits address space. We may not accept negative offsets, since they may
3112 // be just off and we may accept pretty large positive ones.
3113 if (M == CodeModel::Kernel && Offset > 0)
3114 return true;
3115
3116 return false;
3117}
3118
Evan Chengef41ff62011-06-23 17:54:54 +00003119/// isCalleePop - Determines whether the callee is required to pop its
3120/// own arguments. Callee pop is necessary to support tail calls.
3121bool X86::isCalleePop(CallingConv::ID CallingConv,
3122 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3123 if (IsVarArg)
3124 return false;
3125
3126 switch (CallingConv) {
3127 default:
3128 return false;
3129 case CallingConv::X86_StdCall:
3130 return !is64Bit;
3131 case CallingConv::X86_FastCall:
3132 return !is64Bit;
3133 case CallingConv::X86_ThisCall:
3134 return !is64Bit;
3135 case CallingConv::Fast:
3136 return TailCallOpt;
3137 case CallingConv::GHC:
3138 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003139 case CallingConv::HiPE:
3140 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003141 }
3142}
3143
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003144/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3145/// specific condition code, returning the condition code and the LHS/RHS of the
3146/// comparison to make.
3147static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3148 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003149 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003150 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3151 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3152 // X > -1 -> X == 0, jump !sign.
3153 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003154 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003155 }
3156 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003157 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003158 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003159 }
3160 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003161 // X < 1 -> X <= 0
3162 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003163 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003164 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003165 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003166
Evan Chengd9558e02006-01-06 00:43:03 +00003167 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003168 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003169 case ISD::SETEQ: return X86::COND_E;
3170 case ISD::SETGT: return X86::COND_G;
3171 case ISD::SETGE: return X86::COND_GE;
3172 case ISD::SETLT: return X86::COND_L;
3173 case ISD::SETLE: return X86::COND_LE;
3174 case ISD::SETNE: return X86::COND_NE;
3175 case ISD::SETULT: return X86::COND_B;
3176 case ISD::SETUGT: return X86::COND_A;
3177 case ISD::SETULE: return X86::COND_BE;
3178 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003179 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003181
Chris Lattner4c78e022008-12-23 23:42:27 +00003182 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003183
Chris Lattner4c78e022008-12-23 23:42:27 +00003184 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003185 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3186 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003187 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3188 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003189 }
3190
Chris Lattner4c78e022008-12-23 23:42:27 +00003191 switch (SetCCOpcode) {
3192 default: break;
3193 case ISD::SETOLT:
3194 case ISD::SETOLE:
3195 case ISD::SETUGT:
3196 case ISD::SETUGE:
3197 std::swap(LHS, RHS);
3198 break;
3199 }
3200
3201 // On a floating point condition, the flags are set as follows:
3202 // ZF PF CF op
3203 // 0 | 0 | 0 | X > Y
3204 // 0 | 0 | 1 | X < Y
3205 // 1 | 0 | 0 | X == Y
3206 // 1 | 1 | 1 | unordered
3207 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003208 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003209 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003210 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003211 case ISD::SETOLT: // flipped
3212 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003213 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003214 case ISD::SETOLE: // flipped
3215 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003216 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003217 case ISD::SETUGT: // flipped
3218 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003219 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003220 case ISD::SETUGE: // flipped
3221 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003222 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003223 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003224 case ISD::SETNE: return X86::COND_NE;
3225 case ISD::SETUO: return X86::COND_P;
3226 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003227 case ISD::SETOEQ:
3228 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003229 }
Evan Chengd9558e02006-01-06 00:43:03 +00003230}
3231
Evan Cheng4a460802006-01-11 00:33:36 +00003232/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3233/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003234/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003235static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003236 switch (X86CC) {
3237 default:
3238 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003239 case X86::COND_B:
3240 case X86::COND_BE:
3241 case X86::COND_E:
3242 case X86::COND_P:
3243 case X86::COND_A:
3244 case X86::COND_AE:
3245 case X86::COND_NE:
3246 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003247 return true;
3248 }
3249}
3250
Evan Chengeb2f9692009-10-27 19:56:55 +00003251/// isFPImmLegal - Returns true if the target can instruction select the
3252/// specified FP immediate natively. If false, the legalizer will
3253/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003254bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003255 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3256 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3257 return true;
3258 }
3259 return false;
3260}
3261
Nate Begeman9008ca62009-04-27 18:41:29 +00003262/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3263/// the specified range (L, H].
3264static bool isUndefOrInRange(int Val, int Low, int Hi) {
3265 return (Val < 0) || (Val >= Low && Val < Hi);
3266}
3267
3268/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3269/// specified value.
3270static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003271 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003272}
3273
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003274/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003275/// from position Pos and ending in Pos+Size, falls within the specified
3276/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003277static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003278 unsigned Pos, unsigned Size, int Low) {
3279 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003280 if (!isUndefOrEqual(Mask[i], Low))
3281 return false;
3282 return true;
3283}
3284
Nate Begeman9008ca62009-04-27 18:41:29 +00003285/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3286/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3287/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003288static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003289 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003290 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 return (Mask[0] < 2 && Mask[1] < 2);
3293 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003294}
3295
Nate Begeman9008ca62009-04-27 18:41:29 +00003296/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3297/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003298static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3299 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003300 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003301
Nate Begeman9008ca62009-04-27 18:41:29 +00003302 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003303 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3304 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003305
Evan Cheng506d3df2006-03-29 23:07:14 +00003306 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003307 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003308 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003309 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003310
Craig Toppera9a568a2012-05-02 08:03:44 +00003311 if (VT == MVT::v16i16) {
3312 // Lower quadword copied in order or undef.
3313 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3314 return false;
3315
3316 // Upper quadword shuffled.
3317 for (unsigned i = 12; i != 16; ++i)
3318 if (!isUndefOrInRange(Mask[i], 12, 16))
3319 return false;
3320 }
3321
Evan Cheng506d3df2006-03-29 23:07:14 +00003322 return true;
3323}
3324
Nate Begeman9008ca62009-04-27 18:41:29 +00003325/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3326/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003327static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3328 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003329 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003330
Rafael Espindola15684b22009-04-24 12:40:33 +00003331 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003332 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3333 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003334
Rafael Espindola15684b22009-04-24 12:40:33 +00003335 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003336 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003337 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003338 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003339
Craig Toppera9a568a2012-05-02 08:03:44 +00003340 if (VT == MVT::v16i16) {
3341 // Upper quadword copied in order.
3342 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3343 return false;
3344
3345 // Lower quadword shuffled.
3346 for (unsigned i = 8; i != 12; ++i)
3347 if (!isUndefOrInRange(Mask[i], 8, 12))
3348 return false;
3349 }
3350
Rafael Espindola15684b22009-04-24 12:40:33 +00003351 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003352}
3353
Nate Begemana09008b2009-10-19 02:17:23 +00003354/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3355/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003356static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3357 const X86Subtarget *Subtarget) {
3358 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003359 (VT.getSizeInBits() == 256 && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003360 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003361
Craig Topper0e2037b2012-01-20 05:53:00 +00003362 unsigned NumElts = VT.getVectorNumElements();
3363 unsigned NumLanes = VT.getSizeInBits()/128;
3364 unsigned NumLaneElts = NumElts/NumLanes;
3365
3366 // Do not handle 64-bit element shuffles with palignr.
3367 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003368 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003369
Craig Topper0e2037b2012-01-20 05:53:00 +00003370 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3371 unsigned i;
3372 for (i = 0; i != NumLaneElts; ++i) {
3373 if (Mask[i+l] >= 0)
3374 break;
3375 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003376
Craig Topper0e2037b2012-01-20 05:53:00 +00003377 // Lane is all undef, go to next lane
3378 if (i == NumLaneElts)
3379 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003380
Craig Topper0e2037b2012-01-20 05:53:00 +00003381 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003382
Craig Topper0e2037b2012-01-20 05:53:00 +00003383 // Make sure its in this lane in one of the sources
3384 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3385 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003386 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003387
3388 // If not lane 0, then we must match lane 0
3389 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3390 return false;
3391
3392 // Correct second source to be contiguous with first source
3393 if (Start >= (int)NumElts)
3394 Start -= NumElts - NumLaneElts;
3395
3396 // Make sure we're shifting in the right direction.
3397 if (Start <= (int)(i+l))
3398 return false;
3399
3400 Start -= i;
3401
3402 // Check the rest of the elements to see if they are consecutive.
3403 for (++i; i != NumLaneElts; ++i) {
3404 int Idx = Mask[i+l];
3405
3406 // Make sure its in this lane
3407 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3408 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3409 return false;
3410
3411 // If not lane 0, then we must match lane 0
3412 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3413 return false;
3414
3415 if (Idx >= (int)NumElts)
3416 Idx -= NumElts - NumLaneElts;
3417
3418 if (!isUndefOrEqual(Idx, Start+i))
3419 return false;
3420
3421 }
Nate Begemana09008b2009-10-19 02:17:23 +00003422 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003423
Nate Begemana09008b2009-10-19 02:17:23 +00003424 return true;
3425}
3426
Craig Topper1a7700a2012-01-19 08:19:12 +00003427/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3428/// the two vector operands have swapped position.
3429static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3430 unsigned NumElems) {
3431 for (unsigned i = 0; i != NumElems; ++i) {
3432 int idx = Mask[i];
3433 if (idx < 0)
3434 continue;
3435 else if (idx < (int)NumElems)
3436 Mask[i] = idx + NumElems;
3437 else
3438 Mask[i] = idx - NumElems;
3439 }
3440}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003441
Craig Topper1a7700a2012-01-19 08:19:12 +00003442/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3443/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3444/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3445/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003446static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003447 bool Commuted = false) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003448 if (!HasFp256 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003449 return false;
3450
Craig Topper1a7700a2012-01-19 08:19:12 +00003451 unsigned NumElems = VT.getVectorNumElements();
3452 unsigned NumLanes = VT.getSizeInBits()/128;
3453 unsigned NumLaneElems = NumElems/NumLanes;
3454
3455 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003456 return false;
3457
3458 // VSHUFPSY divides the resulting vector into 4 chunks.
3459 // The sources are also splitted into 4 chunks, and each destination
3460 // chunk must come from a different source chunk.
3461 //
3462 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3463 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3464 //
3465 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3466 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3467 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003468 // VSHUFPDY divides the resulting vector into 4 chunks.
3469 // The sources are also splitted into 4 chunks, and each destination
3470 // chunk must come from a different source chunk.
3471 //
3472 // SRC1 => X3 X2 X1 X0
3473 // SRC2 => Y3 Y2 Y1 Y0
3474 //
3475 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3476 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003477 unsigned HalfLaneElems = NumLaneElems/2;
3478 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3479 for (unsigned i = 0; i != NumLaneElems; ++i) {
3480 int Idx = Mask[i+l];
3481 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3482 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3483 return false;
3484 // For VSHUFPSY, the mask of the second half must be the same as the
3485 // first but with the appropriate offsets. This works in the same way as
3486 // VPERMILPS works with masks.
3487 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3488 continue;
3489 if (!isUndefOrEqual(Idx, Mask[i]+l))
3490 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003491 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003492 }
3493
3494 return true;
3495}
3496
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003497/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3498/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003499static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003500 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003501 return false;
3502
Craig Topper7a9a28b2012-08-12 02:23:29 +00003503 unsigned NumElems = VT.getVectorNumElements();
3504
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003505 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003506 return false;
3507
Evan Cheng2064a2b2006-03-28 06:50:32 +00003508 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003509 return isUndefOrEqual(Mask[0], 6) &&
3510 isUndefOrEqual(Mask[1], 7) &&
3511 isUndefOrEqual(Mask[2], 2) &&
3512 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003513}
3514
Nate Begeman0b10b912009-11-07 23:17:15 +00003515/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3516/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3517/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003518static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003519 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003520 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003521
Craig Topper7a9a28b2012-08-12 02:23:29 +00003522 unsigned NumElems = VT.getVectorNumElements();
3523
Nate Begeman0b10b912009-11-07 23:17:15 +00003524 if (NumElems != 4)
3525 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003526
Craig Topperdd637ae2012-02-19 05:41:45 +00003527 return isUndefOrEqual(Mask[0], 2) &&
3528 isUndefOrEqual(Mask[1], 3) &&
3529 isUndefOrEqual(Mask[2], 2) &&
3530 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003531}
3532
Evan Cheng5ced1d82006-04-06 23:23:56 +00003533/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3534/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003535static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003536 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003537 return false;
3538
Craig Topperdd637ae2012-02-19 05:41:45 +00003539 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003540
Evan Cheng5ced1d82006-04-06 23:23:56 +00003541 if (NumElems != 2 && NumElems != 4)
3542 return false;
3543
Chad Rosier238ae312012-04-30 17:47:15 +00003544 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003545 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003546 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003547
Chad Rosier238ae312012-04-30 17:47:15 +00003548 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003549 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003550 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003551
3552 return true;
3553}
3554
Nate Begeman0b10b912009-11-07 23:17:15 +00003555/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3556/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003557static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003558 if (!VT.is128BitVector())
3559 return false;
3560
Craig Topperdd637ae2012-02-19 05:41:45 +00003561 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003562
Craig Topper7a9a28b2012-08-12 02:23:29 +00003563 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003564 return false;
3565
Chad Rosier238ae312012-04-30 17:47:15 +00003566 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003567 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003568 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003569
Chad Rosier238ae312012-04-30 17:47:15 +00003570 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3571 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003572 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003573
3574 return true;
3575}
3576
Elena Demikhovsky15963732012-06-26 08:04:10 +00003577//
3578// Some special combinations that can be optimized.
3579//
3580static
3581SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3582 SelectionDAG &DAG) {
3583 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003584 DebugLoc dl = SVOp->getDebugLoc();
3585
3586 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3587 return SDValue();
3588
3589 ArrayRef<int> Mask = SVOp->getMask();
3590
3591 // These are the special masks that may be optimized.
3592 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3593 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3594 bool MatchEvenMask = true;
3595 bool MatchOddMask = true;
3596 for (int i=0; i<8; ++i) {
3597 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3598 MatchEvenMask = false;
3599 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3600 MatchOddMask = false;
3601 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003602
Elena Demikhovsky32510202012-09-04 12:49:02 +00003603 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003604 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003605
Elena Demikhovsky15963732012-06-26 08:04:10 +00003606 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3607
Elena Demikhovsky32510202012-09-04 12:49:02 +00003608 SDValue Op0 = SVOp->getOperand(0);
3609 SDValue Op1 = SVOp->getOperand(1);
3610
3611 if (MatchEvenMask) {
3612 // Shift the second operand right to 32 bits.
3613 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3614 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3615 } else {
3616 // Shift the first operand left to 32 bits.
3617 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3618 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3619 }
3620 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3621 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003622}
3623
Evan Cheng0038e592006-03-28 00:39:58 +00003624/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3625/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003626static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003627 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003628 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003629
3630 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3631 "Unsupported vector type for unpckh");
3632
Craig Topper6347e862011-11-21 06:57:39 +00003633 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003634 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003635 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003636
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003637 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3638 // independently on 128-bit lanes.
3639 unsigned NumLanes = VT.getSizeInBits()/128;
3640 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003641
Craig Topper94438ba2011-12-16 08:06:31 +00003642 for (unsigned l = 0; l != NumLanes; ++l) {
3643 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3644 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003645 i += 2, ++j) {
3646 int BitI = Mask[i];
3647 int BitI1 = Mask[i+1];
3648 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003649 return false;
David Greenea20244d2011-03-02 17:23:43 +00003650 if (V2IsSplat) {
3651 if (!isUndefOrEqual(BitI1, NumElts))
3652 return false;
3653 } else {
3654 if (!isUndefOrEqual(BitI1, j + NumElts))
3655 return false;
3656 }
Evan Cheng39623da2006-04-20 08:58:49 +00003657 }
Evan Cheng0038e592006-03-28 00:39:58 +00003658 }
David Greenea20244d2011-03-02 17:23:43 +00003659
Evan Cheng0038e592006-03-28 00:39:58 +00003660 return true;
3661}
3662
Evan Cheng4fcb9222006-03-28 02:43:26 +00003663/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3664/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003665static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003666 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003667 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003668
3669 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3670 "Unsupported vector type for unpckh");
3671
Craig Topper6347e862011-11-21 06:57:39 +00003672 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003673 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003674 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003675
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +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
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003681 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003682 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3683 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003684 int BitI = Mask[i];
3685 int BitI1 = Mask[i+1];
3686 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003687 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003688 if (V2IsSplat) {
3689 if (isUndefOrEqual(BitI1, NumElts))
3690 return false;
3691 } else {
3692 if (!isUndefOrEqual(BitI1, j+NumElts))
3693 return false;
3694 }
Evan Cheng39623da2006-04-20 08:58:49 +00003695 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003696 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003697 return true;
3698}
3699
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003700/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3701/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3702/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003703static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003704 bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003705 unsigned NumElts = VT.getVectorNumElements();
3706
3707 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3708 "Unsupported vector type for unpckh");
3709
3710 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003711 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003712 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003713
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003714 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3715 // FIXME: Need a better way to get rid of this, there's no latency difference
3716 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3717 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003718 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003719 return false;
3720
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003721 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3722 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003723 unsigned NumLanes = VT.getSizeInBits()/128;
3724 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003725
Craig Topper94438ba2011-12-16 08:06:31 +00003726 for (unsigned l = 0; l != NumLanes; ++l) {
3727 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3728 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003729 i += 2, ++j) {
3730 int BitI = Mask[i];
3731 int BitI1 = Mask[i+1];
3732
3733 if (!isUndefOrEqual(BitI, j))
3734 return false;
3735 if (!isUndefOrEqual(BitI1, j))
3736 return false;
3737 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003738 }
David Greenea20244d2011-03-02 17:23:43 +00003739
Rafael Espindola15684b22009-04-24 12:40:33 +00003740 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003741}
3742
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003743/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3744/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3745/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003746static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003747 unsigned NumElts = VT.getVectorNumElements();
3748
3749 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3750 "Unsupported vector type for unpckh");
3751
3752 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003753 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003754 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003755
Craig Topper94438ba2011-12-16 08:06:31 +00003756 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3757 // independently on 128-bit lanes.
3758 unsigned NumLanes = VT.getSizeInBits()/128;
3759 unsigned NumLaneElts = NumElts/NumLanes;
3760
3761 for (unsigned l = 0; l != NumLanes; ++l) {
3762 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3763 i != (l+1)*NumLaneElts; i += 2, ++j) {
3764 int BitI = Mask[i];
3765 int BitI1 = Mask[i+1];
3766 if (!isUndefOrEqual(BitI, j))
3767 return false;
3768 if (!isUndefOrEqual(BitI1, j))
3769 return false;
3770 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003771 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003772 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003773}
3774
Evan Cheng017dcc62006-04-21 01:05:10 +00003775/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3776/// specifies a shuffle of elements that is suitable for input to MOVSS,
3777/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003778static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003779 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003780 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003781 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003782 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003783
Craig Topperc612d792012-01-02 09:17:37 +00003784 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003785
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003787 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003788
Craig Topperc612d792012-01-02 09:17:37 +00003789 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003790 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003791 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003792
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003793 return true;
3794}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003795
Craig Topper70b883b2011-11-28 10:14:51 +00003796/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003797/// as permutations between 128-bit chunks or halves. As an example: this
3798/// shuffle bellow:
3799/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3800/// The first half comes from the second half of V1 and the second half from the
3801/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003802static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3803 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003804 return false;
3805
3806 // The shuffle result is divided into half A and half B. In total the two
3807 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3808 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003809 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003810 bool MatchA = false, MatchB = false;
3811
3812 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003813 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003814 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3815 MatchA = true;
3816 break;
3817 }
3818 }
3819
3820 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003821 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003822 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3823 MatchB = true;
3824 break;
3825 }
3826 }
3827
3828 return MatchA && MatchB;
3829}
3830
Craig Topper70b883b2011-11-28 10:14:51 +00003831/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3832/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003833static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003834 EVT VT = SVOp->getValueType(0);
3835
Craig Topperc612d792012-01-02 09:17:37 +00003836 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003837
Craig Topperc612d792012-01-02 09:17:37 +00003838 unsigned FstHalf = 0, SndHalf = 0;
3839 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003840 if (SVOp->getMaskElt(i) > 0) {
3841 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3842 break;
3843 }
3844 }
Craig Topperc612d792012-01-02 09:17:37 +00003845 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003846 if (SVOp->getMaskElt(i) > 0) {
3847 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3848 break;
3849 }
3850 }
3851
3852 return (FstHalf | (SndHalf << 4));
3853}
3854
Craig Topper70b883b2011-11-28 10:14:51 +00003855/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003856/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3857/// Note that VPERMIL mask matching is different depending whether theunderlying
3858/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3859/// to the same elements of the low, but to the higher half of the source.
3860/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003861/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003862static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3863 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003864 return false;
3865
Craig Topperc612d792012-01-02 09:17:37 +00003866 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003867 // Only match 256-bit with 32/64-bit types
3868 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003869 return false;
3870
Craig Topperc612d792012-01-02 09:17:37 +00003871 unsigned NumLanes = VT.getSizeInBits()/128;
3872 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003873 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003874 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003875 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003876 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003877 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003878 continue;
3879 // VPERMILPS handling
3880 if (Mask[i] < 0)
3881 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003882 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003883 return false;
3884 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003885 }
3886
3887 return true;
3888}
3889
Craig Topper5aaffa82012-02-19 02:53:47 +00003890/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003891/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003892/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003893static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003895 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003896 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003897
3898 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003899 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003900 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003901
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003903 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003904
Craig Topperc612d792012-01-02 09:17:37 +00003905 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3907 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3908 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003909 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003910
Evan Cheng39623da2006-04-20 08:58:49 +00003911 return true;
3912}
3913
Evan Chengd9539472006-04-14 21:59:03 +00003914/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3915/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003916/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003917static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003918 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003919 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003920 return false;
3921
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003922 unsigned NumElems = VT.getVectorNumElements();
3923
3924 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3925 (VT.getSizeInBits() == 256 && NumElems != 8))
3926 return false;
3927
3928 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003929 for (unsigned i = 0; i != NumElems; i += 2)
3930 if (!isUndefOrEqual(Mask[i], i+1) ||
3931 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003933
3934 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003935}
3936
3937/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3938/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003939/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003940static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003941 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003942 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003943 return false;
3944
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003945 unsigned NumElems = VT.getVectorNumElements();
3946
3947 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3948 (VT.getSizeInBits() == 256 && NumElems != 8))
3949 return false;
3950
3951 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003952 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003953 if (!isUndefOrEqual(Mask[i], i) ||
3954 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003956
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003957 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003958}
3959
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003960/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3961/// specifies a shuffle of elements that is suitable for input to 256-bit
3962/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003963static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3964 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00003965 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003966
Craig Topper7a9a28b2012-08-12 02:23:29 +00003967 unsigned NumElts = VT.getVectorNumElements();
3968 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003969 return false;
3970
Craig Topperc612d792012-01-02 09:17:37 +00003971 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003972 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003973 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003974 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003975 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003976 return false;
3977 return true;
3978}
3979
Evan Cheng0b457f02008-09-25 20:50:48 +00003980/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003981/// specifies a shuffle of elements that is suitable for input to 128-bit
3982/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003983static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003984 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003985 return false;
3986
Craig Topperc612d792012-01-02 09:17:37 +00003987 unsigned e = VT.getVectorNumElements() / 2;
3988 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003989 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003990 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003991 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003992 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003993 return false;
3994 return true;
3995}
3996
David Greenec38a03e2011-02-03 15:50:00 +00003997/// isVEXTRACTF128Index - Return true if the specified
3998/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3999/// suitable for input to VEXTRACTF128.
4000bool X86::isVEXTRACTF128Index(SDNode *N) {
4001 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4002 return false;
4003
4004 // The index should be aligned on a 128-bit boundary.
4005 uint64_t Index =
4006 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4007
4008 unsigned VL = N->getValueType(0).getVectorNumElements();
4009 unsigned VBits = N->getValueType(0).getSizeInBits();
4010 unsigned ElSize = VBits / VL;
4011 bool Result = (Index * ElSize) % 128 == 0;
4012
4013 return Result;
4014}
4015
David Greeneccacdc12011-02-04 16:08:29 +00004016/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4017/// operand specifies a subvector insert that is suitable for input to
4018/// VINSERTF128.
4019bool X86::isVINSERTF128Index(SDNode *N) {
4020 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4021 return false;
4022
4023 // The index should be aligned on a 128-bit boundary.
4024 uint64_t Index =
4025 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4026
4027 unsigned VL = N->getValueType(0).getVectorNumElements();
4028 unsigned VBits = N->getValueType(0).getSizeInBits();
4029 unsigned ElSize = VBits / VL;
4030 bool Result = (Index * ElSize) % 128 == 0;
4031
4032 return Result;
4033}
4034
Evan Cheng63d33002006-03-22 08:01:21 +00004035/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004036/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004037/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004038static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004039 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004040
Craig Topper1a7700a2012-01-19 08:19:12 +00004041 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4042 "Unsupported vector type for PSHUF/SHUFP");
4043
4044 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4045 // independently on 128-bit lanes.
4046 unsigned NumElts = VT.getVectorNumElements();
4047 unsigned NumLanes = VT.getSizeInBits()/128;
4048 unsigned NumLaneElts = NumElts/NumLanes;
4049
4050 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4051 "Only supports 2 or 4 elements per lane");
4052
4053 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004054 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004055 for (unsigned i = 0; i != NumElts; ++i) {
4056 int Elt = N->getMaskElt(i);
4057 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004058 Elt &= NumLaneElts - 1;
4059 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004060 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004061 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004062
Evan Cheng63d33002006-03-22 08:01:21 +00004063 return Mask;
4064}
4065
Evan Cheng506d3df2006-03-29 23:07:14 +00004066/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004067/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004068static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004069 EVT VT = N->getValueType(0);
4070
4071 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4072 "Unsupported vector type for PSHUFHW");
4073
4074 unsigned NumElts = VT.getVectorNumElements();
4075
Evan Cheng506d3df2006-03-29 23:07:14 +00004076 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004077 for (unsigned l = 0; l != NumElts; l += 8) {
4078 // 8 nodes per lane, but we only care about the last 4.
4079 for (unsigned i = 0; i < 4; ++i) {
4080 int Elt = N->getMaskElt(l+i+4);
4081 if (Elt < 0) continue;
4082 Elt &= 0x3; // only 2-bits.
4083 Mask |= Elt << (i * 2);
4084 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004085 }
Craig Topper6b28d352012-05-03 07:12:59 +00004086
Evan Cheng506d3df2006-03-29 23:07:14 +00004087 return Mask;
4088}
4089
4090/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004091/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004092static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004093 EVT VT = N->getValueType(0);
4094
4095 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4096 "Unsupported vector type for PSHUFHW");
4097
4098 unsigned NumElts = VT.getVectorNumElements();
4099
Evan Cheng506d3df2006-03-29 23:07:14 +00004100 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004101 for (unsigned l = 0; l != NumElts; l += 8) {
4102 // 8 nodes per lane, but we only care about the first 4.
4103 for (unsigned i = 0; i < 4; ++i) {
4104 int Elt = N->getMaskElt(l+i);
4105 if (Elt < 0) continue;
4106 Elt &= 0x3; // only 2-bits
4107 Mask |= Elt << (i * 2);
4108 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004109 }
Craig Topper6b28d352012-05-03 07:12:59 +00004110
Evan Cheng506d3df2006-03-29 23:07:14 +00004111 return Mask;
4112}
4113
Nate Begemana09008b2009-10-19 02:17:23 +00004114/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4115/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004116static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4117 EVT VT = SVOp->getValueType(0);
4118 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004119
Craig Topper0e2037b2012-01-20 05:53:00 +00004120 unsigned NumElts = VT.getVectorNumElements();
4121 unsigned NumLanes = VT.getSizeInBits()/128;
4122 unsigned NumLaneElts = NumElts/NumLanes;
4123
4124 int Val = 0;
4125 unsigned i;
4126 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004127 Val = SVOp->getMaskElt(i);
4128 if (Val >= 0)
4129 break;
4130 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004131 if (Val >= (int)NumElts)
4132 Val -= NumElts - NumLaneElts;
4133
Eli Friedman63f8dde2011-07-25 21:36:45 +00004134 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004135 return (Val - i) * EltSize;
4136}
4137
David Greenec38a03e2011-02-03 15:50:00 +00004138/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4139/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4140/// instructions.
4141unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4142 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4143 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4144
4145 uint64_t Index =
4146 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4147
4148 EVT VecVT = N->getOperand(0).getValueType();
4149 EVT ElVT = VecVT.getVectorElementType();
4150
4151 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004152 return Index / NumElemsPerChunk;
4153}
4154
David Greeneccacdc12011-02-04 16:08:29 +00004155/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4156/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4157/// instructions.
4158unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4159 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4160 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4161
4162 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004163 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004164
4165 EVT VecVT = N->getValueType(0);
4166 EVT ElVT = VecVT.getVectorElementType();
4167
4168 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004169 return Index / NumElemsPerChunk;
4170}
4171
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004172/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4173/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4174/// Handles 256-bit.
4175static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4176 EVT VT = N->getValueType(0);
4177
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004178 unsigned NumElts = VT.getVectorNumElements();
4179
Craig Topper095c5282012-04-15 23:48:57 +00004180 assert((VT.is256BitVector() && NumElts == 4) &&
4181 "Unsupported vector type for VPERMQ/VPERMPD");
4182
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004183 unsigned Mask = 0;
4184 for (unsigned i = 0; i != NumElts; ++i) {
4185 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004186 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004187 continue;
4188 Mask |= Elt << (i*2);
4189 }
4190
4191 return Mask;
4192}
Evan Cheng37b73872009-07-30 08:33:02 +00004193/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4194/// constant +0.0.
4195bool X86::isZeroNode(SDValue Elt) {
4196 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004197 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004198 (isa<ConstantFPSDNode>(Elt) &&
4199 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4200}
4201
Nate Begeman9008ca62009-04-27 18:41:29 +00004202/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4203/// their permute mask.
4204static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4205 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004206 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004207 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004208 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004209
Nate Begeman5a5ca152009-04-29 05:20:52 +00004210 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004211 int Idx = SVOp->getMaskElt(i);
4212 if (Idx >= 0) {
4213 if (Idx < (int)NumElems)
4214 Idx += NumElems;
4215 else
4216 Idx -= NumElems;
4217 }
4218 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004219 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004220 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4221 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004222}
4223
Evan Cheng533a0aa2006-04-19 20:35:22 +00004224/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4225/// match movhlps. The lower half elements should come from upper half of
4226/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004227/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004228static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004229 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004230 return false;
4231 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004232 return false;
4233 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004234 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004235 return false;
4236 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004237 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004238 return false;
4239 return true;
4240}
4241
Evan Cheng5ced1d82006-04-06 23:23:56 +00004242/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004243/// is promoted to a vector. It also returns the LoadSDNode by reference if
4244/// required.
4245static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004246 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4247 return false;
4248 N = N->getOperand(0).getNode();
4249 if (!ISD::isNON_EXTLoad(N))
4250 return false;
4251 if (LD)
4252 *LD = cast<LoadSDNode>(N);
4253 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004254}
4255
Dan Gohman65fd6562011-11-03 21:49:52 +00004256// Test whether the given value is a vector value which will be legalized
4257// into a load.
4258static bool WillBeConstantPoolLoad(SDNode *N) {
4259 if (N->getOpcode() != ISD::BUILD_VECTOR)
4260 return false;
4261
4262 // Check for any non-constant elements.
4263 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4264 switch (N->getOperand(i).getNode()->getOpcode()) {
4265 case ISD::UNDEF:
4266 case ISD::ConstantFP:
4267 case ISD::Constant:
4268 break;
4269 default:
4270 return false;
4271 }
4272
4273 // Vectors of all-zeros and all-ones are materialized with special
4274 // instructions rather than being loaded.
4275 return !ISD::isBuildVectorAllZeros(N) &&
4276 !ISD::isBuildVectorAllOnes(N);
4277}
4278
Evan Cheng533a0aa2006-04-19 20:35:22 +00004279/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4280/// match movlp{s|d}. The lower half elements should come from lower half of
4281/// V1 (and in order), and the upper half elements should come from the upper
4282/// half of V2 (and in order). And since V1 will become the source of the
4283/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004284static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004285 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004286 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004287 return false;
4288
Evan Cheng466685d2006-10-09 20:57:25 +00004289 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004290 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004291 // Is V2 is a vector load, don't do this transformation. We will try to use
4292 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004293 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004294 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004295
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004296 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004297
Evan Cheng533a0aa2006-04-19 20:35:22 +00004298 if (NumElems != 2 && NumElems != 4)
4299 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004300 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004301 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004302 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004303 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004304 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004305 return false;
4306 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004307}
4308
Evan Cheng39623da2006-04-20 08:58:49 +00004309/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4310/// all the same.
4311static bool isSplatVector(SDNode *N) {
4312 if (N->getOpcode() != ISD::BUILD_VECTOR)
4313 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004314
Dan Gohman475871a2008-07-27 21:46:04 +00004315 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004316 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4317 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004318 return false;
4319 return true;
4320}
4321
Evan Cheng213d2cf2007-05-17 18:45:50 +00004322/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004323/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004324/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004325static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004326 SDValue V1 = N->getOperand(0);
4327 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004328 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4329 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004331 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004332 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004333 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4334 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004335 if (Opc != ISD::BUILD_VECTOR ||
4336 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 return false;
4338 } else if (Idx >= 0) {
4339 unsigned Opc = V1.getOpcode();
4340 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4341 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004342 if (Opc != ISD::BUILD_VECTOR ||
4343 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004344 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004345 }
4346 }
4347 return true;
4348}
4349
4350/// getZeroVector - Returns a vector of specified type with all zero elements.
4351///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004352static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004353 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004354 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004355 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004356
Dale Johannesen0488fb62010-09-30 23:57:10 +00004357 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004358 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004359 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004360 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004361 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004362 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4363 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4364 } else { // SSE1
4365 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4366 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4367 }
Craig Topper9d352402012-04-23 07:24:41 +00004368 } else if (Size == 256) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004369 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004370 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4371 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4372 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4373 } else {
4374 // 256-bit logic and arithmetic instructions in AVX are all
4375 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4376 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4377 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4378 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4379 }
Craig Topper9d352402012-04-23 07:24:41 +00004380 } else
4381 llvm_unreachable("Unexpected vector type");
4382
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004383 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004384}
4385
Chris Lattner8a594482007-11-25 00:24:49 +00004386/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004387/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4388/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4389/// Then bitcast to their original type, ensuring they get CSE'd.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004390static SDValue getOnesVector(EVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004391 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004392 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004393 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004394
Owen Anderson825b72b2009-08-11 20:47:22 +00004395 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004396 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004397 if (Size == 256) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004398 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004399 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4400 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4401 } else { // AVX
4402 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004403 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004404 }
Craig Topper9d352402012-04-23 07:24:41 +00004405 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004406 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004407 } else
4408 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004409
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004410 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004411}
4412
Evan Cheng39623da2006-04-20 08:58:49 +00004413/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4414/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004415static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004416 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004417 if (Mask[i] > (int)NumElems) {
4418 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004419 }
Evan Cheng39623da2006-04-20 08:58:49 +00004420 }
Evan Cheng39623da2006-04-20 08:58:49 +00004421}
4422
Evan Cheng017dcc62006-04-21 01:05:10 +00004423/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4424/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004425static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 SDValue V2) {
4427 unsigned NumElems = VT.getVectorNumElements();
4428 SmallVector<int, 8> Mask;
4429 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004430 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004431 Mask.push_back(i);
4432 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004433}
4434
Nate Begeman9008ca62009-04-27 18:41:29 +00004435/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004436static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 SDValue V2) {
4438 unsigned NumElems = VT.getVectorNumElements();
4439 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004440 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 Mask.push_back(i);
4442 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004443 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004445}
4446
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004448static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 SDValue V2) {
4450 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004452 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 Mask.push_back(i + Half);
4454 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004455 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004456 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004457}
4458
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004459// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004460// a generic shuffle instruction because the target has no such instructions.
4461// Generate shuffles which repeat i16 and i8 several times until they can be
4462// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004463static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004464 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004467
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 while (NumElems > 4) {
4469 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004470 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004472 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 EltNo -= NumElems/2;
4474 }
4475 NumElems >>= 1;
4476 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004477 return V;
4478}
Eric Christopherfd179292009-08-27 18:07:15 +00004479
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4481static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4482 EVT VT = V.getValueType();
4483 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004484 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004485
Craig Topper9d352402012-04-23 07:24:41 +00004486 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004487 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004488 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004489 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4490 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004491 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004492 // To use VPERMILPS to splat scalars, the second half of indicies must
4493 // refer to the higher part, which is a duplication of the lower one,
4494 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004495 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4496 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004497
4498 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4499 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4500 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004501 } else
4502 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503
4504 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4505}
4506
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004507/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004508static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4509 EVT SrcVT = SV->getValueType(0);
4510 SDValue V1 = SV->getOperand(0);
4511 DebugLoc dl = SV->getDebugLoc();
4512
4513 int EltNo = SV->getSplatIndex();
4514 int NumElems = SrcVT.getVectorNumElements();
4515 unsigned Size = SrcVT.getSizeInBits();
4516
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004517 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4518 "Unknown how to promote splat for type");
4519
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004520 // Extract the 128-bit part containing the splat element and update
4521 // the splat element index when it refers to the higher register.
4522 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004523 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4524 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004525 EltNo -= NumElems/2;
4526 }
4527
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004528 // All i16 and i8 vector types can't be used directly by a generic shuffle
4529 // instruction because the target has no such instruction. Generate shuffles
4530 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004531 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004532 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004533 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004534 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004535
4536 // Recreate the 256-bit vector and place the same 128-bit vector
4537 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004538 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004539 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004540 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004541 }
4542
4543 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004544}
4545
Evan Chengba05f722006-04-21 23:03:30 +00004546/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004547/// vector of zero or undef vector. This produces a shuffle where the low
4548/// element of V2 is swizzled into the zero/undef vector, landing at element
4549/// 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 +00004550static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004551 bool IsZero,
4552 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004553 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004554 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004555 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004556 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004557 unsigned NumElems = VT.getVectorNumElements();
4558 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004559 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004560 // If this is the insertion idx, put the low elt of V2 here.
4561 MaskVec.push_back(i == Idx ? NumElems : i);
4562 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004563}
4564
Craig Toppera1ffc682012-03-20 06:42:26 +00004565/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4566/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004567/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004568static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004569 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004570 unsigned NumElems = VT.getVectorNumElements();
4571 SDValue ImmN;
4572
Craig Topper89f4e662012-03-20 07:17:59 +00004573 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004574 switch(N->getOpcode()) {
4575 case X86ISD::SHUFP:
4576 ImmN = N->getOperand(N->getNumOperands()-1);
4577 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4578 break;
4579 case X86ISD::UNPCKH:
4580 DecodeUNPCKHMask(VT, Mask);
4581 break;
4582 case X86ISD::UNPCKL:
4583 DecodeUNPCKLMask(VT, Mask);
4584 break;
4585 case X86ISD::MOVHLPS:
4586 DecodeMOVHLPSMask(NumElems, Mask);
4587 break;
4588 case X86ISD::MOVLHPS:
4589 DecodeMOVLHPSMask(NumElems, Mask);
4590 break;
4591 case X86ISD::PSHUFD:
4592 case X86ISD::VPERMILP:
4593 ImmN = N->getOperand(N->getNumOperands()-1);
4594 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004595 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004596 break;
4597 case X86ISD::PSHUFHW:
4598 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004599 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004600 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004601 break;
4602 case X86ISD::PSHUFLW:
4603 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004604 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004605 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004606 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004607 case X86ISD::VPERMI:
4608 ImmN = N->getOperand(N->getNumOperands()-1);
4609 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4610 IsUnary = true;
4611 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004612 case X86ISD::MOVSS:
4613 case X86ISD::MOVSD: {
4614 // The index 0 always comes from the first element of the second source,
4615 // this is why MOVSS and MOVSD are used in the first place. The other
4616 // elements come from the other positions of the first source vector
4617 Mask.push_back(NumElems);
4618 for (unsigned i = 1; i != NumElems; ++i) {
4619 Mask.push_back(i);
4620 }
4621 break;
4622 }
4623 case X86ISD::VPERM2X128:
4624 ImmN = N->getOperand(N->getNumOperands()-1);
4625 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004626 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004627 break;
4628 case X86ISD::MOVDDUP:
4629 case X86ISD::MOVLHPD:
4630 case X86ISD::MOVLPD:
4631 case X86ISD::MOVLPS:
4632 case X86ISD::MOVSHDUP:
4633 case X86ISD::MOVSLDUP:
4634 case X86ISD::PALIGN:
4635 // Not yet implemented
4636 return false;
4637 default: llvm_unreachable("unknown target shuffle node");
4638 }
4639
4640 return true;
4641}
4642
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4644/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004645static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004646 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004647 if (Depth == 6)
4648 return SDValue(); // Limit search depth.
4649
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004650 SDValue V = SDValue(N, 0);
4651 EVT VT = V.getValueType();
4652 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004653
4654 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4655 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004656 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004657
Craig Topper3d092db2012-03-21 02:14:01 +00004658 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004659 return DAG.getUNDEF(VT.getVectorElementType());
4660
Craig Topperd156dc12012-02-06 07:17:51 +00004661 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004662 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4663 : SV->getOperand(1);
4664 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004665 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004666
4667 // Recurse into target specific vector shuffles to find scalars.
4668 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004669 MVT ShufVT = V.getValueType().getSimpleVT();
4670 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004671 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004672 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004673
Craig Topperd978c542012-05-06 19:46:21 +00004674 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004675 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004676
Craig Topper3d092db2012-03-21 02:14:01 +00004677 int Elt = ShuffleMask[Index];
4678 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004679 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004680
Craig Topper3d092db2012-03-21 02:14:01 +00004681 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004682 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004683 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004684 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004685 }
4686
4687 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004688 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689 V = V.getOperand(0);
4690 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004691 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004693 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694 return SDValue();
4695 }
4696
4697 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4698 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004699 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004700
4701 if (V.getOpcode() == ISD::BUILD_VECTOR)
4702 return V.getOperand(Index);
4703
4704 return SDValue();
4705}
4706
4707/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4708/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004709/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004710static
Craig Topper3d092db2012-03-21 02:14:01 +00004711unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004712 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004713 unsigned i;
4714 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004715 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004716 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004717 if (!(Elt.getNode() &&
4718 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4719 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004720 }
4721
4722 return i;
4723}
4724
Craig Topper3d092db2012-03-21 02:14:01 +00004725/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4726/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004727/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4728static
Craig Topper3d092db2012-03-21 02:14:01 +00004729bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4730 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4731 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004732 bool SeenV1 = false;
4733 bool SeenV2 = false;
4734
Craig Topper3d092db2012-03-21 02:14:01 +00004735 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004736 int Idx = SVOp->getMaskElt(i);
4737 // Ignore undef indicies
4738 if (Idx < 0)
4739 continue;
4740
Craig Topper3d092db2012-03-21 02:14:01 +00004741 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004742 SeenV1 = true;
4743 else
4744 SeenV2 = true;
4745
4746 // Only accept consecutive elements from the same vector
4747 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4748 return false;
4749 }
4750
4751 OpNum = SeenV1 ? 0 : 1;
4752 return true;
4753}
4754
4755/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4756/// logical left shift of a vector.
4757static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4758 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4759 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4760 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4761 false /* check zeros from right */, DAG);
4762 unsigned OpSrc;
4763
4764 if (!NumZeros)
4765 return false;
4766
4767 // Considering the elements in the mask that are not consecutive zeros,
4768 // check if they consecutively come from only one of the source vectors.
4769 //
4770 // V1 = {X, A, B, C} 0
4771 // \ \ \ /
4772 // vector_shuffle V1, V2 <1, 2, 3, X>
4773 //
4774 if (!isShuffleMaskConsecutive(SVOp,
4775 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004776 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004777 NumZeros, // Where to start looking in the src vector
4778 NumElems, // Number of elements in vector
4779 OpSrc)) // Which source operand ?
4780 return false;
4781
4782 isLeft = false;
4783 ShAmt = NumZeros;
4784 ShVal = SVOp->getOperand(OpSrc);
4785 return true;
4786}
4787
4788/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4789/// logical left shift of a vector.
4790static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4791 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4792 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4793 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4794 true /* check zeros from left */, DAG);
4795 unsigned OpSrc;
4796
4797 if (!NumZeros)
4798 return false;
4799
4800 // Considering the elements in the mask that are not consecutive zeros,
4801 // check if they consecutively come from only one of the source vectors.
4802 //
4803 // 0 { A, B, X, X } = V2
4804 // / \ / /
4805 // vector_shuffle V1, V2 <X, X, 4, 5>
4806 //
4807 if (!isShuffleMaskConsecutive(SVOp,
4808 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004809 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004810 0, // Where to start looking in the src vector
4811 NumElems, // Number of elements in vector
4812 OpSrc)) // Which source operand ?
4813 return false;
4814
4815 isLeft = true;
4816 ShAmt = NumZeros;
4817 ShVal = SVOp->getOperand(OpSrc);
4818 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004819}
4820
4821/// isVectorShift - Returns true if the shuffle can be implemented as a
4822/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004823static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004824 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004825 // Although the logic below support any bitwidth size, there are no
4826 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004827 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004828 return false;
4829
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004830 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4831 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4832 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004833
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004834 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004835}
4836
Evan Chengc78d3b42006-04-24 18:01:45 +00004837/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4838///
Dan Gohman475871a2008-07-27 21:46:04 +00004839static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004841 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004842 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004843 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004844 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004845 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004846
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004847 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004848 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004849 bool First = true;
4850 for (unsigned i = 0; i < 16; ++i) {
4851 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4852 if (ThisIsNonZero && First) {
4853 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004854 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004855 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004857 First = false;
4858 }
4859
4860 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004861 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004862 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4863 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004864 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004865 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004866 }
4867 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4869 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4870 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004871 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004873 } else
4874 ThisElt = LastElt;
4875
Gabor Greifba36cb52008-08-28 21:40:38 +00004876 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004878 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004879 }
4880 }
4881
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004882 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004883}
4884
Bill Wendlinga348c562007-03-22 18:42:45 +00004885/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004886///
Dan Gohman475871a2008-07-27 21:46:04 +00004887static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004888 unsigned NumNonZero, unsigned NumZero,
4889 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004890 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004891 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004892 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004893 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004894
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004895 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004896 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004897 bool First = true;
4898 for (unsigned i = 0; i < 8; ++i) {
4899 bool isNonZero = (NonZeros & (1 << i)) != 0;
4900 if (isNonZero) {
4901 if (First) {
4902 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004903 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004904 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004905 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004906 First = false;
4907 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004908 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004910 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004911 }
4912 }
4913
4914 return V;
4915}
4916
Evan Chengf26ffe92008-05-29 08:22:04 +00004917/// getVShift - Return a vector logical shift node.
4918///
Owen Andersone50ed302009-08-10 22:56:29 +00004919static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004920 unsigned NumBits, SelectionDAG &DAG,
4921 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004922 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004923 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004924 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004925 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4926 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004927 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004928 DAG.getConstant(NumBits,
4929 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004930}
4931
Dan Gohman475871a2008-07-27 21:46:04 +00004932SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004933X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004934 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004935
Evan Chengc3630942009-12-09 21:00:30 +00004936 // Check if the scalar load can be widened into a vector load. And if
4937 // the address is "base + cst" see if the cst can be "absorbed" into
4938 // the shuffle mask.
4939 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4940 SDValue Ptr = LD->getBasePtr();
4941 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4942 return SDValue();
4943 EVT PVT = LD->getValueType(0);
4944 if (PVT != MVT::i32 && PVT != MVT::f32)
4945 return SDValue();
4946
4947 int FI = -1;
4948 int64_t Offset = 0;
4949 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4950 FI = FINode->getIndex();
4951 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004952 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004953 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4954 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4955 Offset = Ptr.getConstantOperandVal(1);
4956 Ptr = Ptr.getOperand(0);
4957 } else {
4958 return SDValue();
4959 }
4960
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004961 // FIXME: 256-bit vector instructions don't require a strict alignment,
4962 // improve this code to support it better.
4963 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004964 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004965 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004966 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004967 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004968 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004969 // Can't change the alignment. FIXME: It's possible to compute
4970 // the exact stack offset and reference FI + adjust offset instead.
4971 // If someone *really* cares about this. That's the way to implement it.
4972 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004973 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004974 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004975 }
4976 }
4977
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004978 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004979 // Ptr + (Offset & ~15).
4980 if (Offset < 0)
4981 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004982 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004983 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004984 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004985 if (StartOffset)
4986 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4987 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4988
4989 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004990 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004991
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004992 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4993 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004994 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004995 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004996
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004997 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004998 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004999 Mask.push_back(EltNo);
5000
Craig Toppercc3000632012-01-30 07:50:31 +00005001 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005002 }
5003
5004 return SDValue();
5005}
5006
Michael J. Spencerec38de22010-10-10 22:04:20 +00005007/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5008/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005009/// load which has the same value as a build_vector whose operands are 'elts'.
5010///
5011/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005012///
Nate Begeman1449f292010-03-24 22:19:06 +00005013/// FIXME: we'd also like to handle the case where the last elements are zero
5014/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5015/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005016static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005017 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005018 EVT EltVT = VT.getVectorElementType();
5019 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005020
Nate Begemanfdea31a2010-03-24 20:49:50 +00005021 LoadSDNode *LDBase = NULL;
5022 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005023
Nate Begeman1449f292010-03-24 22:19:06 +00005024 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005025 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005026 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005027 for (unsigned i = 0; i < NumElems; ++i) {
5028 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005029
Nate Begemanfdea31a2010-03-24 20:49:50 +00005030 if (!Elt.getNode() ||
5031 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5032 return SDValue();
5033 if (!LDBase) {
5034 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5035 return SDValue();
5036 LDBase = cast<LoadSDNode>(Elt.getNode());
5037 LastLoadedElt = i;
5038 continue;
5039 }
5040 if (Elt.getOpcode() == ISD::UNDEF)
5041 continue;
5042
5043 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5044 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5045 return SDValue();
5046 LastLoadedElt = i;
5047 }
Nate Begeman1449f292010-03-24 22:19:06 +00005048
5049 // If we have found an entire vector of loads and undefs, then return a large
5050 // load of the entire vector width starting at the base pointer. If we found
5051 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005052 if (LastLoadedElt == NumElems - 1) {
5053 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005054 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005055 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005056 LDBase->isVolatile(), LDBase->isNonTemporal(),
5057 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005058 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005059 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005060 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005061 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005062 }
5063 if (NumElems == 4 && LastLoadedElt == 1 &&
5064 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005065 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5066 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005067 SDValue ResNode =
5068 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5069 LDBase->getPointerInfo(),
5070 LDBase->getAlignment(),
5071 false/*isVolatile*/, true/*ReadMem*/,
5072 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005073
5074 // Make sure the newly-created LOAD is in the same position as LDBase in
5075 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5076 // update uses of LDBase's output chain to use the TokenFactor.
5077 if (LDBase->hasAnyUseOfValue(1)) {
5078 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5079 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5080 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5081 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5082 SDValue(ResNode.getNode(), 1));
5083 }
5084
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005085 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005086 }
5087 return SDValue();
5088}
5089
Nadav Rotem9d68b062012-04-08 12:54:54 +00005090/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5091/// to generate a splat value for the following cases:
5092/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005094/// a scalar load, or a constant.
5095/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005096/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005097SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005098X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005099 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005100 return SDValue();
5101
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005102 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005103 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005104
Craig Topper5da8a802012-05-04 05:49:51 +00005105 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5106 "Unsupported vector type for broadcast.");
5107
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005108 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005109 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005110
Nadav Rotem9d68b062012-04-08 12:54:54 +00005111 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005112 default:
5113 // Unknown pattern found.
5114 return SDValue();
5115
5116 case ISD::BUILD_VECTOR: {
5117 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005118 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005119 return SDValue();
5120
Nadav Rotem9d68b062012-04-08 12:54:54 +00005121 Ld = Op.getOperand(0);
5122 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5123 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005124
5125 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005126 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127 // Constants may have multiple users.
5128 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005129 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005130 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005131 }
5132
5133 case ISD::VECTOR_SHUFFLE: {
5134 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5135
5136 // Shuffles must have a splat mask where the first element is
5137 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005138 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005139 return SDValue();
5140
5141 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005142 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005143 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5144
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005145 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005146 return SDValue();
5147
5148 // Use the register form of the broadcast instruction available on AVX2.
5149 if (VT.is256BitVector())
5150 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5151 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5152 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005153
5154 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005155 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005156 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005157
5158 // The scalar_to_vector node and the suspected
5159 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005160 // Constants may have multiple users.
5161 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005162 return SDValue();
5163 break;
5164 }
5165 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005166
Craig Topper7a9a28b2012-08-12 02:23:29 +00005167 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005168
5169 // Handle the broadcasting a single constant scalar from the constant pool
5170 // into a vector. On Sandybridge it is still better to load a constant vector
5171 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005172 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005173 EVT CVT = Ld.getValueType();
5174 assert(!CVT.isVector() && "Must not broadcast a vector type");
5175 unsigned ScalarSize = CVT.getSizeInBits();
5176
Craig Topper5da8a802012-05-04 05:49:51 +00005177 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005178 const Constant *C = 0;
5179 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5180 C = CI->getConstantIntValue();
5181 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5182 C = CF->getConstantFPValue();
5183
5184 assert(C && "Invalid constant type");
5185
Nadav Rotem154819d2012-04-09 07:45:58 +00005186 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005187 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005188 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005189 MachinePointerInfo::getConstantPool(),
5190 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005191
Nadav Rotem9d68b062012-04-08 12:54:54 +00005192 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5193 }
5194 }
5195
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005196 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005197 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5198
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005199 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005200 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005201 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5202 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5203
5204 // The scalar source must be a normal load.
5205 if (!IsLoad)
5206 return SDValue();
5207
Craig Topper5da8a802012-05-04 05:49:51 +00005208 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005209 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005210
Craig Toppera9376332012-01-10 08:23:59 +00005211 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005212 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005213 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005214 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005215 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005216 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005217
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005218 // Unsupported broadcast.
5219 return SDValue();
5220}
5221
Evan Chengc3630942009-12-09 21:00:30 +00005222SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005223X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5224 EVT VT = Op.getValueType();
5225
5226 // Skip if insert_vec_elt is not supported.
5227 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5228 return SDValue();
5229
5230 DebugLoc DL = Op.getDebugLoc();
5231 unsigned NumElems = Op.getNumOperands();
5232
5233 SDValue VecIn1;
5234 SDValue VecIn2;
5235 SmallVector<unsigned, 4> InsertIndices;
5236 SmallVector<int, 8> Mask(NumElems, -1);
5237
5238 for (unsigned i = 0; i != NumElems; ++i) {
5239 unsigned Opc = Op.getOperand(i).getOpcode();
5240
5241 if (Opc == ISD::UNDEF)
5242 continue;
5243
5244 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5245 // Quit if more than 1 elements need inserting.
5246 if (InsertIndices.size() > 1)
5247 return SDValue();
5248
5249 InsertIndices.push_back(i);
5250 continue;
5251 }
5252
5253 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5254 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5255
5256 // Quit if extracted from vector of different type.
5257 if (ExtractedFromVec.getValueType() != VT)
5258 return SDValue();
5259
5260 // Quit if non-constant index.
5261 if (!isa<ConstantSDNode>(ExtIdx))
5262 return SDValue();
5263
5264 if (VecIn1.getNode() == 0)
5265 VecIn1 = ExtractedFromVec;
5266 else if (VecIn1 != ExtractedFromVec) {
5267 if (VecIn2.getNode() == 0)
5268 VecIn2 = ExtractedFromVec;
5269 else if (VecIn2 != ExtractedFromVec)
5270 // Quit if more than 2 vectors to shuffle
5271 return SDValue();
5272 }
5273
5274 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5275
5276 if (ExtractedFromVec == VecIn1)
5277 Mask[i] = Idx;
5278 else if (ExtractedFromVec == VecIn2)
5279 Mask[i] = Idx + NumElems;
5280 }
5281
5282 if (VecIn1.getNode() == 0)
5283 return SDValue();
5284
5285 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5286 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5287 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5288 unsigned Idx = InsertIndices[i];
5289 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5290 DAG.getIntPtrConstant(Idx));
5291 }
5292
5293 return NV;
5294}
5295
5296SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005297X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005298 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005299
David Greenef125a292011-02-08 19:04:41 +00005300 EVT VT = Op.getValueType();
5301 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005302 unsigned NumElems = Op.getNumOperands();
5303
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005304 // Vectors containing all zeros can be matched by pxor and xorps later
5305 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5306 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5307 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005308 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005309 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005310
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005311 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005312 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005313
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005314 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005315 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5316 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005317 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005318 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005319 return Op;
5320
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005321 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005322 }
5323
Nadav Rotem154819d2012-04-09 07:45:58 +00005324 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005325 if (Broadcast.getNode())
5326 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005327
Owen Andersone50ed302009-08-10 22:56:29 +00005328 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005329
Evan Cheng0db9fe62006-04-25 20:13:52 +00005330 unsigned NumZero = 0;
5331 unsigned NumNonZero = 0;
5332 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005333 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005334 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005335 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005336 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005337 if (Elt.getOpcode() == ISD::UNDEF)
5338 continue;
5339 Values.insert(Elt);
5340 if (Elt.getOpcode() != ISD::Constant &&
5341 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005342 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005343 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005344 NumZero++;
5345 else {
5346 NonZeros |= (1 << i);
5347 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005348 }
5349 }
5350
Chris Lattner97a2a562010-08-26 05:24:29 +00005351 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5352 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005353 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005354
Chris Lattner67f453a2008-03-09 05:42:06 +00005355 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005356 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005357 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005358 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005359
Chris Lattner62098042008-03-09 01:05:04 +00005360 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5361 // the value are obviously zero, truncate the value to i32 and do the
5362 // insertion that way. Only do this if the value is non-constant or if the
5363 // value is a constant being inserted into element 0. It is cheaper to do
5364 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005366 (!IsAllConstants || Idx == 0)) {
5367 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005368 // Handle SSE only.
5369 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5370 EVT VecVT = MVT::v4i32;
5371 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005372
Chris Lattner62098042008-03-09 01:05:04 +00005373 // Truncate the value (which may itself be a constant) to i32, and
5374 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005375 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005376 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005377 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005378
Chris Lattner62098042008-03-09 01:05:04 +00005379 // Now we have our 32-bit value zero extended in the low element of
5380 // a vector. If Idx != 0, swizzle it into place.
5381 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005382 SmallVector<int, 4> Mask;
5383 Mask.push_back(Idx);
5384 for (unsigned i = 1; i != VecElts; ++i)
5385 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005386 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005387 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005388 }
Craig Topper07a27622012-01-22 03:07:48 +00005389 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005390 }
5391 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005392
Chris Lattner19f79692008-03-08 22:59:52 +00005393 // If we have a constant or non-constant insertion into the low element of
5394 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5395 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005396 // depending on what the source datatype is.
5397 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005398 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005399 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005400
5401 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005402 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005403 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005404 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005405 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5406 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005407 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005408 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005409 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5410 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005411 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005412 }
5413
5414 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005416 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005417 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005418 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005419 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005420 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005421 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005422 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005423 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005424 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005425 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005426 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005427
5428 // Is it a vector logical left shift?
5429 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005430 X86::isZeroNode(Op.getOperand(0)) &&
5431 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005432 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005433 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005434 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005435 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005436 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005437 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005438
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005439 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005440 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441
Chris Lattner19f79692008-03-08 22:59:52 +00005442 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5443 // is a non-constant being inserted into an element other than the low one,
5444 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5445 // movd/movss) to move this into the low element, then shuffle it into
5446 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005448 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005449
Evan Cheng0db9fe62006-04-25 20:13:52 +00005450 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005451 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005452 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005453 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005454 MaskVec.push_back(i == Idx ? 0 : 1);
5455 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 }
5457 }
5458
Chris Lattner67f453a2008-03-09 05:42:06 +00005459 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005460 if (Values.size() == 1) {
5461 if (EVTBits == 32) {
5462 // Instead of a shuffle like this:
5463 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5464 // Check if it's possible to issue this instead.
5465 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5466 unsigned Idx = CountTrailingZeros_32(NonZeros);
5467 SDValue Item = Op.getOperand(Idx);
5468 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5469 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5470 }
Dan Gohman475871a2008-07-27 21:46:04 +00005471 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005473
Dan Gohmana3941172007-07-24 22:55:08 +00005474 // A vector full of immediates; various special cases are already
5475 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005476 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005477 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005478
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005479 // For AVX-length vectors, build the individual 128-bit pieces and use
5480 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005481 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005482 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005483 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005484 V.push_back(Op.getOperand(i));
5485
5486 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5487
5488 // Build both the lower and upper subvector.
5489 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5490 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5491 NumElems/2);
5492
5493 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005494 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005495 }
5496
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005497 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005498 if (EVTBits == 64) {
5499 if (NumNonZero == 1) {
5500 // One half is zero or undef.
5501 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005502 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005503 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005504 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005505 }
Dan Gohman475871a2008-07-27 21:46:04 +00005506 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005507 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005508
5509 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005510 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005511 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005512 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005513 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005514 }
5515
Bill Wendling826f36f2007-03-28 00:57:11 +00005516 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005517 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005518 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005519 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005520 }
5521
5522 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005523 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005524 if (NumElems == 4 && NumZero > 0) {
5525 for (unsigned i = 0; i < 4; ++i) {
5526 bool isZero = !(NonZeros & (1 << i));
5527 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005528 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005529 else
Dale Johannesenace16102009-02-03 19:33:06 +00005530 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005531 }
5532
5533 for (unsigned i = 0; i < 2; ++i) {
5534 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5535 default: break;
5536 case 0:
5537 V[i] = V[i*2]; // Must be a zero vector.
5538 break;
5539 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005540 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005541 break;
5542 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005543 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005544 break;
5545 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005546 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005547 break;
5548 }
5549 }
5550
Benjamin Kramer9c683542012-01-30 15:16:21 +00005551 bool Reverse1 = (NonZeros & 0x3) == 2;
5552 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5553 int MaskVec[] = {
5554 Reverse1 ? 1 : 0,
5555 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005556 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5557 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005558 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005559 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005560 }
5561
Craig Topper7a9a28b2012-08-12 02:23:29 +00005562 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005563 // Check for a build vector of consecutive loads.
5564 for (unsigned i = 0; i < NumElems; ++i)
5565 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005566
Nate Begemanfdea31a2010-03-24 20:49:50 +00005567 // Check for elements which are consecutive loads.
5568 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5569 if (LD.getNode())
5570 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005571
Michael Liaofacace82012-10-19 17:15:18 +00005572 // Check for a build vector from mostly shuffle plus few inserting.
5573 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5574 if (Sh.getNode())
5575 return Sh;
5576
Michael J. Spencerec38de22010-10-10 22:04:20 +00005577 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005578 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005579 SDValue Result;
5580 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5581 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5582 else
5583 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005584
Chris Lattner24faf612010-08-28 17:59:08 +00005585 for (unsigned i = 1; i < NumElems; ++i) {
5586 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5587 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005588 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005589 }
5590 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005591 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005592
Chris Lattner6e80e442010-08-28 17:15:43 +00005593 // Otherwise, expand into a number of unpckl*, start by extending each of
5594 // our (non-undef) elements to the full vector width with the element in the
5595 // bottom slot of the vector (which generates no code for SSE).
5596 for (unsigned i = 0; i < NumElems; ++i) {
5597 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5598 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5599 else
5600 V[i] = DAG.getUNDEF(VT);
5601 }
5602
5603 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005604 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5605 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5606 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005607 unsigned EltStride = NumElems >> 1;
5608 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005609 for (unsigned i = 0; i < EltStride; ++i) {
5610 // If V[i+EltStride] is undef and this is the first round of mixing,
5611 // then it is safe to just drop this shuffle: V[i] is already in the
5612 // right place, the one element (since it's the first round) being
5613 // inserted as undef can be dropped. This isn't safe for successive
5614 // rounds because they will permute elements within both vectors.
5615 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5616 EltStride == NumElems/2)
5617 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005618
Chris Lattner6e80e442010-08-28 17:15:43 +00005619 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005620 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005621 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005622 }
5623 return V[0];
5624 }
Dan Gohman475871a2008-07-27 21:46:04 +00005625 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005626}
5627
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005628// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5629// to create 256-bit vectors from two other 128-bit ones.
5630static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5631 DebugLoc dl = Op.getDebugLoc();
5632 EVT ResVT = Op.getValueType();
5633
Craig Topper7a9a28b2012-08-12 02:23:29 +00005634 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005635
5636 SDValue V1 = Op.getOperand(0);
5637 SDValue V2 = Op.getOperand(1);
5638 unsigned NumElems = ResVT.getVectorNumElements();
5639
Craig Topper4c7972d2012-04-22 18:15:59 +00005640 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005641}
5642
Craig Topper55b24052012-09-11 06:15:32 +00005643static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005644 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005645
5646 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5647 // from two other 128-bit ones.
5648 return LowerAVXCONCAT_VECTORS(Op, DAG);
5649}
5650
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005651// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005652static SDValue
5653LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5654 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005655 SDValue V1 = SVOp->getOperand(0);
5656 SDValue V2 = SVOp->getOperand(1);
5657 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005658 EVT VT = SVOp->getValueType(0);
5659 EVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005660 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005661
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005662 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5663 return SDValue();
5664 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005665 return SDValue();
5666
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005667 // Check the mask for BLEND and build the value.
5668 unsigned MaskValue = 0;
5669 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
5670 unsigned NumLanes = (NumElems-1)/8 + 1;
5671 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005672
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005673 // Blend for v16i16 should be symetric for the both lanes.
5674 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005675
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005676 int SndLaneEltIdx = (NumLanes == 2) ?
5677 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005678 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005679
5680 if ((EltIdx == -1 || EltIdx == (int)i) &&
5681 (SndLaneEltIdx == -1 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
5682 continue;
5683
5684 if (((unsigned)EltIdx == (i + NumElems)) &&
5685 (SndLaneEltIdx == -1 ||
5686 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5687 MaskValue |= (1<<i);
5688 else
Craig Topper1842ba02012-04-23 06:38:28 +00005689 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005690 }
5691
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005692 // Convert i32 vectors to floating point if it is not AVX2.
5693 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
5694 EVT BlendVT = VT;
5695 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
5696 BlendVT = EVT::getVectorVT(*DAG.getContext(),
5697 EVT::getFloatingPointVT(EltVT.getSizeInBits()),
5698 NumElems);
5699 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5700 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5701 }
5702
5703 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5704 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005705 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005706}
5707
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708// v8i16 shuffles - Prefer shuffles in the following order:
5709// 1. [all] pshuflw, pshufhw, optional move
5710// 2. [ssse3] 1 x pshufb
5711// 3. [ssse3] 2 x pshufb + 1 x por
5712// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005713static SDValue
5714LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5715 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005716 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005717 SDValue V1 = SVOp->getOperand(0);
5718 SDValue V2 = SVOp->getOperand(1);
5719 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // Determine if more than 1 of the words in each of the low and high quadwords
5723 // of the result come from the same quadword of one of the two inputs. Undef
5724 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005725 unsigned LoQuad[] = { 0, 0, 0, 0 };
5726 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005727 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005729 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005730 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 MaskVals.push_back(EltIdx);
5732 if (EltIdx < 0) {
5733 ++Quad[0];
5734 ++Quad[1];
5735 ++Quad[2];
5736 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005737 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005738 }
5739 ++Quad[EltIdx / 4];
5740 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005741 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005742
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005744 unsigned MaxQuad = 1;
5745 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 if (LoQuad[i] > MaxQuad) {
5747 BestLoQuad = i;
5748 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005749 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005750 }
5751
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005753 MaxQuad = 1;
5754 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 if (HiQuad[i] > MaxQuad) {
5756 BestHiQuad = i;
5757 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005758 }
5759 }
5760
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005762 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 // single pshufb instruction is necessary. If There are more than 2 input
5764 // quads, disable the next transformation since it does not help SSSE3.
5765 bool V1Used = InputQuads[0] || InputQuads[1];
5766 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005767 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005769 BestLoQuad = InputQuads[0] ? 0 : 1;
5770 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 }
5772 if (InputQuads.count() > 2) {
5773 BestLoQuad = -1;
5774 BestHiQuad = -1;
5775 }
5776 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5779 // the shuffle mask. If a quad is scored as -1, that means that it contains
5780 // words from all 4 input quadwords.
5781 SDValue NewV;
5782 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005783 int MaskV[] = {
5784 BestLoQuad < 0 ? 0 : BestLoQuad,
5785 BestHiQuad < 0 ? 1 : BestHiQuad
5786 };
Eric Christopherfd179292009-08-27 18:07:15 +00005787 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005788 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5789 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5790 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005791
Nate Begemanb9a47b82009-02-23 08:49:38 +00005792 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5793 // source words for the shuffle, to aid later transformations.
5794 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005795 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005796 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005798 if (idx != (int)i)
5799 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005801 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 AllWordsInNewV = false;
5803 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005804 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005805
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5807 if (AllWordsInNewV) {
5808 for (int i = 0; i != 8; ++i) {
5809 int idx = MaskVals[i];
5810 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005811 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005812 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005813 if ((idx != i) && idx < 4)
5814 pshufhw = false;
5815 if ((idx != i) && idx > 3)
5816 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005817 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005818 V1 = NewV;
5819 V2Used = false;
5820 BestLoQuad = 0;
5821 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005822 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005823
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5825 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005826 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005827 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5828 unsigned TargetMask = 0;
5829 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005831 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5832 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5833 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005834 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005835 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005836 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005837 }
Eric Christopherfd179292009-08-27 18:07:15 +00005838
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 // If we have SSSE3, and all words of the result are from 1 input vector,
5840 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5841 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005842 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005844
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005846 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 // mask, and elements that come from V1 in the V2 mask, so that the two
5848 // results can be OR'd together.
5849 bool TwoInputs = V1Used && V2Used;
5850 for (unsigned i = 0; i != 8; ++i) {
5851 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005852 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5853 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5854 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5855 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005857 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005858 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005859 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005860 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005862 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005863
Nate Begemanb9a47b82009-02-23 08:49:38 +00005864 // Calculate the shuffle mask for the second input, shuffle it, and
5865 // OR it with the first shuffled input.
5866 pshufbMask.clear();
5867 for (unsigned i = 0; i != 8; ++i) {
5868 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005869 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5870 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5871 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5872 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005874 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005875 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005876 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005877 MVT::v16i8, &pshufbMask[0], 16));
5878 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005879 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 }
5881
5882 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5883 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005884 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005886 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005887 for (int i = 0; i != 4; ++i) {
5888 int idx = MaskVals[i];
5889 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 InOrder.set(i);
5891 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005892 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005894 }
5895 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005896 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005897 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005898
Craig Topperdd637ae2012-02-19 05:41:45 +00005899 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5900 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005901 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005902 NewV.getOperand(0),
5903 getShufflePSHUFLWImmediate(SVOp), DAG);
5904 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 }
Eric Christopherfd179292009-08-27 18:07:15 +00005906
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5908 // and update MaskVals with the new element order.
5909 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005910 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 for (unsigned i = 4; i != 8; ++i) {
5912 int idx = MaskVals[i];
5913 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005914 InOrder.set(i);
5915 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005916 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 }
5919 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005921 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005922
Craig Topperdd637ae2012-02-19 05:41:45 +00005923 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5924 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005925 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005926 NewV.getOperand(0),
5927 getShufflePSHUFHWImmediate(SVOp), DAG);
5928 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 }
Eric Christopherfd179292009-08-27 18:07:15 +00005930
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 // In case BestHi & BestLo were both -1, which means each quadword has a word
5932 // from each of the four input quadwords, calculate the InOrder bitvector now
5933 // before falling through to the insert/extract cleanup.
5934 if (BestLoQuad == -1 && BestHiQuad == -1) {
5935 NewV = V1;
5936 for (int i = 0; i != 8; ++i)
5937 if (MaskVals[i] < 0 || MaskVals[i] == i)
5938 InOrder.set(i);
5939 }
Eric Christopherfd179292009-08-27 18:07:15 +00005940
Nate Begemanb9a47b82009-02-23 08:49:38 +00005941 // The other elements are put in the right place using pextrw and pinsrw.
5942 for (unsigned i = 0; i != 8; ++i) {
5943 if (InOrder[i])
5944 continue;
5945 int EltIdx = MaskVals[i];
5946 if (EltIdx < 0)
5947 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005948 SDValue ExtOp = (EltIdx < 8) ?
5949 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5950 DAG.getIntPtrConstant(EltIdx)) :
5951 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 DAG.getIntPtrConstant(i));
5955 }
5956 return NewV;
5957}
5958
5959// v16i8 shuffles - Prefer shuffles in the following order:
5960// 1. [ssse3] 1 x pshufb
5961// 2. [ssse3] 2 x pshufb + 1 x por
5962// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5963static
Nate Begeman9008ca62009-04-27 18:41:29 +00005964SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005965 SelectionDAG &DAG,
5966 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005967 SDValue V1 = SVOp->getOperand(0);
5968 SDValue V2 = SVOp->getOperand(1);
5969 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005970 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005971
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005973 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005975
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005977 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005979
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005981 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 //
5983 // Otherwise, we have elements from both input vectors, and must zero out
5984 // elements that come from V2 in the first mask, and V1 in the second mask
5985 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005986 for (unsigned i = 0; i != 16; ++i) {
5987 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005988 if (EltIdx < 0 || EltIdx >= 16)
5989 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005990 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005993 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005995
5996 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5997 // the 2nd operand if it's undefined or zero.
5998 if (V2.getOpcode() == ISD::UNDEF ||
5999 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006001
Nate Begemanb9a47b82009-02-23 08:49:38 +00006002 // Calculate the shuffle mask for the second input, shuffle it, and
6003 // OR it with the first shuffled input.
6004 pshufbMask.clear();
6005 for (unsigned i = 0; i != 16; ++i) {
6006 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006007 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006008 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006009 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006010 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006011 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 MVT::v16i8, &pshufbMask[0], 16));
6013 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 }
Eric Christopherfd179292009-08-27 18:07:15 +00006015
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 // No SSSE3 - Calculate in place words and then fix all out of place words
6017 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6018 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006019 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6020 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006021 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 for (int i = 0; i != 8; ++i) {
6023 int Elt0 = MaskVals[i*2];
6024 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006025
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 // This word of the result is all undef, skip it.
6027 if (Elt0 < 0 && Elt1 < 0)
6028 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006029
Nate Begemanb9a47b82009-02-23 08:49:38 +00006030 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006031 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006032 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006033
Nate Begemanb9a47b82009-02-23 08:49:38 +00006034 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6035 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6036 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006037
6038 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6039 // using a single extract together, load it and store it.
6040 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006041 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006042 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006043 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006044 DAG.getIntPtrConstant(i));
6045 continue;
6046 }
6047
Nate Begemanb9a47b82009-02-23 08:49:38 +00006048 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006049 // source byte is not also odd, shift the extracted word left 8 bits
6050 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006051 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006052 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006053 DAG.getIntPtrConstant(Elt1 / 2));
6054 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006055 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006056 DAG.getConstant(8,
6057 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006058 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006059 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6060 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006061 }
6062 // If Elt0 is defined, extract it from the appropriate source. If the
6063 // source byte is not also even, shift the extracted word right 8 bits. If
6064 // Elt1 was also defined, OR the extracted values together before
6065 // inserting them in the result.
6066 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006067 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006068 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6069 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006070 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006071 DAG.getConstant(8,
6072 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006073 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006074 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6075 DAG.getConstant(0x00FF, MVT::i16));
6076 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006077 : InsElt0;
6078 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006079 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006080 DAG.getIntPtrConstant(i));
6081 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006082 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006083}
6084
Elena Demikhovsky41789462012-09-06 12:42:01 +00006085// v32i8 shuffles - Translate to VPSHUFB if possible.
6086static
6087SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006088 const X86Subtarget *Subtarget,
6089 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006090 EVT VT = SVOp->getValueType(0);
6091 SDValue V1 = SVOp->getOperand(0);
6092 SDValue V2 = SVOp->getOperand(1);
6093 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006094 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006095
6096 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006097 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6098 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006099
Michael Liao471b9172012-10-03 23:43:52 +00006100 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006101 // (1) one of input vector is undefined or zeroinitializer.
6102 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6103 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006104 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006105 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006106 return SDValue();
6107
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006108 if (V1IsAllZero && !V2IsAllZero) {
6109 CommuteVectorShuffleMask(MaskVals, 32);
6110 V1 = V2;
6111 }
6112 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006113 for (unsigned i = 0; i != 32; i++) {
6114 int EltIdx = MaskVals[i];
6115 if (EltIdx < 0 || EltIdx >= 32)
6116 EltIdx = 0x80;
6117 else {
6118 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6119 // Cross lane is not allowed.
6120 return SDValue();
6121 EltIdx &= 0xf;
6122 }
6123 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6124 }
6125 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6126 DAG.getNode(ISD::BUILD_VECTOR, dl,
6127 MVT::v32i8, &pshufbMask[0], 32));
6128}
6129
Evan Cheng7a831ce2007-12-15 03:00:47 +00006130/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006131/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006132/// done when every pair / quad of shuffle mask elements point to elements in
6133/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006134/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006135static
Nate Begeman9008ca62009-04-27 18:41:29 +00006136SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006137 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006138 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006139 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006140 MVT NewVT;
6141 unsigned Scale;
6142 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006143 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006144 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6145 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6146 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6147 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6148 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6149 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006150 }
6151
Nate Begeman9008ca62009-04-27 18:41:29 +00006152 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006153 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006154 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006155 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006156 int EltIdx = SVOp->getMaskElt(i+j);
6157 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006158 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006159 if (StartIdx < 0)
6160 StartIdx = (EltIdx / Scale);
6161 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006162 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006163 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006164 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006165 }
6166
Craig Topper11ac1f82012-05-04 04:08:44 +00006167 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6168 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006169 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006170}
6171
Evan Chengd880b972008-05-09 21:53:03 +00006172/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006173///
Owen Andersone50ed302009-08-10 22:56:29 +00006174static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006175 SDValue SrcOp, SelectionDAG &DAG,
6176 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006177 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006178 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006179 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006180 LD = dyn_cast<LoadSDNode>(SrcOp);
6181 if (!LD) {
6182 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6183 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006184 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006185 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006186 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006187 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006188 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006189 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006190 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006191 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006192 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6193 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6194 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006195 SrcOp.getOperand(0)
6196 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006197 }
6198 }
6199 }
6200
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006201 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006202 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006203 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006204 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006205}
6206
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006207/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6208/// which could not be matched by any known target speficic shuffle
6209static SDValue
6210LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006211
6212 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6213 if (NewOp.getNode())
6214 return NewOp;
6215
Craig Topper8f35c132012-01-20 09:29:03 +00006216 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006217
Craig Topper8f35c132012-01-20 09:29:03 +00006218 unsigned NumElems = VT.getVectorNumElements();
6219 unsigned NumLaneElems = NumElems / 2;
6220
Craig Topper8f35c132012-01-20 09:29:03 +00006221 DebugLoc dl = SVOp->getDebugLoc();
6222 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006223 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006224 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006225
Craig Topper9a2b6e12012-04-06 07:45:23 +00006226 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006227 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006228 // Build a shuffle mask for the output, discovering on the fly which
6229 // input vectors to use as shuffle operands (recorded in InputUsed).
6230 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006231 // out with UseBuildVector set.
6232 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006233 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006234 unsigned LaneStart = l * NumLaneElems;
6235 for (unsigned i = 0; i != NumLaneElems; ++i) {
6236 // The mask element. This indexes into the input.
6237 int Idx = SVOp->getMaskElt(i+LaneStart);
6238 if (Idx < 0) {
6239 // the mask element does not index into any input vector.
6240 Mask.push_back(-1);
6241 continue;
6242 }
Craig Topper8f35c132012-01-20 09:29:03 +00006243
Craig Topper9a2b6e12012-04-06 07:45:23 +00006244 // The input vector this mask element indexes into.
6245 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006246
Craig Topper9a2b6e12012-04-06 07:45:23 +00006247 // Turn the index into an offset from the start of the input vector.
6248 Idx -= Input * NumLaneElems;
6249
6250 // Find or create a shuffle vector operand to hold this input.
6251 unsigned OpNo;
6252 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6253 if (InputUsed[OpNo] == Input)
6254 // This input vector is already an operand.
6255 break;
6256 if (InputUsed[OpNo] < 0) {
6257 // Create a new operand for this input vector.
6258 InputUsed[OpNo] = Input;
6259 break;
6260 }
6261 }
6262
6263 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006264 // More than two input vectors used! Give up on trying to create a
6265 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6266 UseBuildVector = true;
6267 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006268 }
6269
6270 // Add the mask index for the new shuffle vector.
6271 Mask.push_back(Idx + OpNo * NumLaneElems);
6272 }
6273
Craig Topper8ae97ba2012-05-21 06:40:16 +00006274 if (UseBuildVector) {
6275 SmallVector<SDValue, 16> SVOps;
6276 for (unsigned i = 0; i != NumLaneElems; ++i) {
6277 // The mask element. This indexes into the input.
6278 int Idx = SVOp->getMaskElt(i+LaneStart);
6279 if (Idx < 0) {
6280 SVOps.push_back(DAG.getUNDEF(EltVT));
6281 continue;
6282 }
6283
6284 // The input vector this mask element indexes into.
6285 int Input = Idx / NumElems;
6286
6287 // Turn the index into an offset from the start of the input vector.
6288 Idx -= Input * NumElems;
6289
6290 // Extract the vector element by hand.
6291 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6292 SVOp->getOperand(Input),
6293 DAG.getIntPtrConstant(Idx)));
6294 }
6295
6296 // Construct the output using a BUILD_VECTOR.
6297 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6298 SVOps.size());
6299 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006300 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006301 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006302 } else {
6303 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006304 (InputUsed[0] % 2) * NumLaneElems,
6305 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006306 // If only one input was used, use an undefined vector for the other.
6307 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6308 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006309 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006310 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006311 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006312 }
6313
6314 Mask.clear();
6315 }
Craig Topper8f35c132012-01-20 09:29:03 +00006316
6317 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006318 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006319}
6320
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006321/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6322/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006323static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006324LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006325 SDValue V1 = SVOp->getOperand(0);
6326 SDValue V2 = SVOp->getOperand(1);
6327 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006328 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006329
Craig Topper7a9a28b2012-08-12 02:23:29 +00006330 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006331
Benjamin Kramer9c683542012-01-30 15:16:21 +00006332 std::pair<int, int> Locs[4];
6333 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006334 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006335
Evan Chengace3c172008-07-22 21:13:36 +00006336 unsigned NumHi = 0;
6337 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006338 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006339 int Idx = PermMask[i];
6340 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006341 Locs[i] = std::make_pair(-1, -1);
6342 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006343 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6344 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006345 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006346 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006347 NumLo++;
6348 } else {
6349 Locs[i] = std::make_pair(1, NumHi);
6350 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006351 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006352 NumHi++;
6353 }
6354 }
6355 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006356
Evan Chengace3c172008-07-22 21:13:36 +00006357 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006358 // If no more than two elements come from either vector. This can be
6359 // implemented with two shuffles. First shuffle gather the elements.
6360 // The second shuffle, which takes the first shuffle as both of its
6361 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006362 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006363
Benjamin Kramer9c683542012-01-30 15:16:21 +00006364 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006365
Benjamin Kramer9c683542012-01-30 15:16:21 +00006366 for (unsigned i = 0; i != 4; ++i)
6367 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006368 unsigned Idx = (i < 2) ? 0 : 4;
6369 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006370 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006371 }
Evan Chengace3c172008-07-22 21:13:36 +00006372
Nate Begeman9008ca62009-04-27 18:41:29 +00006373 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006374 }
6375
6376 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006377 // Otherwise, we must have three elements from one vector, call it X, and
6378 // one element from the other, call it Y. First, use a shufps to build an
6379 // intermediate vector with the one element from Y and the element from X
6380 // that will be in the same half in the final destination (the indexes don't
6381 // matter). Then, use a shufps to build the final vector, taking the half
6382 // containing the element from Y from the intermediate, and the other half
6383 // from X.
6384 if (NumHi == 3) {
6385 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006386 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006387 std::swap(V1, V2);
6388 }
6389
6390 // Find the element from V2.
6391 unsigned HiIndex;
6392 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006393 int Val = PermMask[HiIndex];
6394 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006395 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006396 if (Val >= 4)
6397 break;
6398 }
6399
Nate Begeman9008ca62009-04-27 18:41:29 +00006400 Mask1[0] = PermMask[HiIndex];
6401 Mask1[1] = -1;
6402 Mask1[2] = PermMask[HiIndex^1];
6403 Mask1[3] = -1;
6404 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006405
6406 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006407 Mask1[0] = PermMask[0];
6408 Mask1[1] = PermMask[1];
6409 Mask1[2] = HiIndex & 1 ? 6 : 4;
6410 Mask1[3] = HiIndex & 1 ? 4 : 6;
6411 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006412 }
Craig Topper69947b92012-04-23 06:57:04 +00006413
6414 Mask1[0] = HiIndex & 1 ? 2 : 0;
6415 Mask1[1] = HiIndex & 1 ? 0 : 2;
6416 Mask1[2] = PermMask[2];
6417 Mask1[3] = PermMask[3];
6418 if (Mask1[2] >= 0)
6419 Mask1[2] += 4;
6420 if (Mask1[3] >= 0)
6421 Mask1[3] += 4;
6422 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006423 }
6424
6425 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006426 int LoMask[] = { -1, -1, -1, -1 };
6427 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006428
Benjamin Kramer9c683542012-01-30 15:16:21 +00006429 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006430 unsigned MaskIdx = 0;
6431 unsigned LoIdx = 0;
6432 unsigned HiIdx = 2;
6433 for (unsigned i = 0; i != 4; ++i) {
6434 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006435 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006436 MaskIdx = 1;
6437 LoIdx = 0;
6438 HiIdx = 2;
6439 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006440 int Idx = PermMask[i];
6441 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006442 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006443 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006444 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006445 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006446 LoIdx++;
6447 } else {
6448 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006449 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006450 HiIdx++;
6451 }
6452 }
6453
Nate Begeman9008ca62009-04-27 18:41:29 +00006454 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6455 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006456 int MaskOps[] = { -1, -1, -1, -1 };
6457 for (unsigned i = 0; i != 4; ++i)
6458 if (Locs[i].first != -1)
6459 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006460 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006461}
6462
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006463static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006464 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006465 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006466
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006467 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6468 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006469 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6470 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6471 // BUILD_VECTOR (load), undef
6472 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006473
6474 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006475}
6476
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006477static
Evan Cheng835580f2010-10-07 20:50:20 +00006478SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6479 EVT VT = Op.getValueType();
6480
6481 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006482 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6483 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006484 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6485 V1, DAG));
6486}
6487
6488static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006489SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006490 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006491 SDValue V1 = Op.getOperand(0);
6492 SDValue V2 = Op.getOperand(1);
6493 EVT VT = Op.getValueType();
6494
6495 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6496
Craig Topper1accb7e2012-01-10 06:54:16 +00006497 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006498 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6499
Evan Cheng0899f5c2011-08-31 02:05:24 +00006500 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6501 return DAG.getNode(ISD::BITCAST, dl, VT,
6502 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6503 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6504 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006505}
6506
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006507static
6508SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6509 SDValue V1 = Op.getOperand(0);
6510 SDValue V2 = Op.getOperand(1);
6511 EVT VT = Op.getValueType();
6512
6513 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6514 "unsupported shuffle type");
6515
6516 if (V2.getOpcode() == ISD::UNDEF)
6517 V2 = V1;
6518
6519 // v4i32 or v4f32
6520 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6521}
6522
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006523static
Craig Topper1accb7e2012-01-10 06:54:16 +00006524SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006525 SDValue V1 = Op.getOperand(0);
6526 SDValue V2 = Op.getOperand(1);
6527 EVT VT = Op.getValueType();
6528 unsigned NumElems = VT.getVectorNumElements();
6529
6530 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6531 // operand of these instructions is only memory, so check if there's a
6532 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6533 // same masks.
6534 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006535
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006536 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006537 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006538 CanFoldLoad = true;
6539
6540 // When V1 is a load, it can be folded later into a store in isel, example:
6541 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6542 // turns into:
6543 // (MOVLPSmr addr:$src1, VR128:$src2)
6544 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006545 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006546 CanFoldLoad = true;
6547
Dan Gohman65fd6562011-11-03 21:49:52 +00006548 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006549 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006550 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006551 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6552
6553 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006554 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006555 if (SVOp->getMaskElt(1) != -1)
6556 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006557 }
6558
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006559 // movl and movlp will both match v2i64, but v2i64 is never matched by
6560 // movl earlier because we make it strict to avoid messing with the movlp load
6561 // folding logic (see the code above getMOVLP call). Match it here then,
6562 // this is horrible, but will stay like this until we move all shuffle
6563 // matching to x86 specific nodes. Note that for the 1st condition all
6564 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006565 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006566 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6567 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006568 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006569 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006570 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006571 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006572
6573 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6574
6575 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006576 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006577 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006578}
6579
Michael Liaod9d09602012-10-23 17:34:00 +00006580// Reduce a vector shuffle to zext.
6581SDValue
6582X86TargetLowering::lowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
6583 // PMOVZX is only available from SSE41.
6584 if (!Subtarget->hasSSE41())
6585 return SDValue();
6586
6587 EVT VT = Op.getValueType();
6588
6589 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006590 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006591 return SDValue();
6592
6593 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6594 DebugLoc DL = Op.getDebugLoc();
6595 SDValue V1 = Op.getOperand(0);
6596 SDValue V2 = Op.getOperand(1);
6597 unsigned NumElems = VT.getVectorNumElements();
6598
6599 // Extending is an unary operation and the element type of the source vector
6600 // won't be equal to or larger than i64.
6601 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6602 VT.getVectorElementType() == MVT::i64)
6603 return SDValue();
6604
6605 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6606 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006607 while ((1U << Shift) < NumElems) {
6608 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006609 break;
6610 Shift += 1;
6611 // The maximal ratio is 8, i.e. from i8 to i64.
6612 if (Shift > 3)
6613 return SDValue();
6614 }
6615
6616 // Check the shuffle mask.
6617 unsigned Mask = (1U << Shift) - 1;
6618 for (unsigned i = 0; i != NumElems; ++i) {
6619 int EltIdx = SVOp->getMaskElt(i);
6620 if ((i & Mask) != 0 && EltIdx != -1)
6621 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006622 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006623 return SDValue();
6624 }
6625
6626 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
6627 EVT NeVT = EVT::getIntegerVT(*DAG.getContext(), NBits);
6628 EVT NVT = EVT::getVectorVT(*DAG.getContext(), NeVT, NumElems >> Shift);
6629
6630 if (!isTypeLegal(NVT))
6631 return SDValue();
6632
6633 // Simplify the operand as it's prepared to be fed into shuffle.
6634 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6635 if (V1.getOpcode() == ISD::BITCAST &&
6636 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6637 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6638 V1.getOperand(0)
6639 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6640 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6641 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006642 ConstantSDNode *CIdx =
6643 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006644 // If it's foldable, i.e. normal load with single use, we will let code
6645 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006646 if (CIdx && CIdx->getZExtValue() == 0 &&
6647 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse()))
Michael Liaod9d09602012-10-23 17:34:00 +00006648 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
6649 }
6650
6651 return DAG.getNode(ISD::BITCAST, DL, VT,
6652 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6653}
6654
Nadav Rotem154819d2012-04-09 07:45:58 +00006655SDValue
6656X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006657 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6658 EVT VT = Op.getValueType();
6659 DebugLoc dl = Op.getDebugLoc();
6660 SDValue V1 = Op.getOperand(0);
6661 SDValue V2 = Op.getOperand(1);
6662
6663 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006664 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006665
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006666 // Handle splat operations
6667 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006668 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006669 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006670
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006671 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006672 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006673 if (Broadcast.getNode())
6674 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006675
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006676 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006677 if ((Size == 128 && NumElem <= 4) ||
Jakub Staszakd642baf2012-11-26 19:24:31 +00006678 (Size == 256 && NumElem <= 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006679 return SDValue();
6680
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006681 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006682 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006683 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006684
Michael Liaod9d09602012-10-23 17:34:00 +00006685 // Check integer expanding shuffles.
6686 SDValue NewOp = lowerVectorIntExtend(Op, DAG);
6687 if (NewOp.getNode())
6688 return NewOp;
6689
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006690 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6691 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006692 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6693 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006694 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6695 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006696 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006697 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006698 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006699 // FIXME: Figure out a cleaner way to do this.
6700 // Try to make use of movq to zero out the top part.
6701 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6702 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6703 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006704 EVT NewVT = NewOp.getValueType();
6705 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6706 NewVT, true, false))
6707 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006708 DAG, Subtarget, dl);
6709 }
6710 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6711 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006712 if (NewOp.getNode()) {
6713 EVT NewVT = NewOp.getValueType();
6714 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6715 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6716 DAG, Subtarget, dl);
6717 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006718 }
6719 }
6720 return SDValue();
6721}
6722
Dan Gohman475871a2008-07-27 21:46:04 +00006723SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006724X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006725 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006726 SDValue V1 = Op.getOperand(0);
6727 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006728 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006729 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006730 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006731 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006733 bool V1IsSplat = false;
6734 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006735 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006736 bool HasFp256 = Subtarget->hasFp256();
6737 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006738 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006739 bool OptForSize = MF.getFunction()->getAttributes().
6740 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006741
Craig Topper3426a3e2011-11-14 06:46:21 +00006742 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006743
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006744 if (V1IsUndef && V2IsUndef)
6745 return DAG.getUNDEF(VT);
6746
6747 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006748
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006749 // Vector shuffle lowering takes 3 steps:
6750 //
6751 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6752 // narrowing and commutation of operands should be handled.
6753 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6754 // shuffle nodes.
6755 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6756 // so the shuffle can be broken into other shuffles and the legalizer can
6757 // try the lowering again.
6758 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006759 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006760 // be matched during isel, all of them must be converted to a target specific
6761 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006762
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006763 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6764 // narrowing and commutation of operands should be handled. The actual code
6765 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006766 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006767 if (NewOp.getNode())
6768 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006769
Craig Topper5aaffa82012-02-19 02:53:47 +00006770 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6771
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006772 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6773 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006774 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006775 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006776 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006777 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006778
Craig Topperdd637ae2012-02-19 05:41:45 +00006779 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006780 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006781 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006782
Craig Topperdd637ae2012-02-19 05:41:45 +00006783 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006784 return getMOVHighToLow(Op, dl, DAG);
6785
6786 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006787 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006788 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006789 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006790
Craig Topper5aaffa82012-02-19 02:53:47 +00006791 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006792 // The actual implementation will match the mask in the if above and then
6793 // during isel it can match several different instructions, not only pshufd
6794 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006795 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6796 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006797
Craig Topper5aaffa82012-02-19 02:53:47 +00006798 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006799
Craig Topper1accb7e2012-01-10 06:54:16 +00006800 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006801 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6802
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006803 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6804 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6805 DAG);
6806
Craig Topperb3982da2011-12-31 23:50:21 +00006807 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006808 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006809 }
Eric Christopherfd179292009-08-27 18:07:15 +00006810
Evan Chengf26ffe92008-05-29 08:22:04 +00006811 // Check if this can be converted into a logical shift.
6812 bool isLeft = false;
6813 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006814 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006815 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006816 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006817 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006818 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006819 EVT EltVT = VT.getVectorElementType();
6820 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006821 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006822 }
Eric Christopherfd179292009-08-27 18:07:15 +00006823
Craig Topper5aaffa82012-02-19 02:53:47 +00006824 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006825 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006826 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006827 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006828 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006829 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6830
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006831 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006832 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6833 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006834 }
Eric Christopherfd179292009-08-27 18:07:15 +00006835
Nate Begeman9008ca62009-04-27 18:41:29 +00006836 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006837 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006838 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006839
Craig Topperdd637ae2012-02-19 05:41:45 +00006840 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006841 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006842
Craig Topperdd637ae2012-02-19 05:41:45 +00006843 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006844 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006845
Craig Topperdd637ae2012-02-19 05:41:45 +00006846 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006847 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006848
Craig Topperdd637ae2012-02-19 05:41:45 +00006849 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006850 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006851
Craig Topperdd637ae2012-02-19 05:41:45 +00006852 if (ShouldXformToMOVHLPS(M, VT) ||
6853 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006854 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006855
Evan Chengf26ffe92008-05-29 08:22:04 +00006856 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006857 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006858 EVT EltVT = VT.getVectorElementType();
6859 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006860 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006861 }
Eric Christopherfd179292009-08-27 18:07:15 +00006862
Evan Cheng9eca5e82006-10-25 21:49:50 +00006863 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006864 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6865 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006866 V1IsSplat = isSplatVector(V1.getNode());
6867 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006868
Chris Lattner8a594482007-11-25 00:24:49 +00006869 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006870 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6871 CommuteVectorShuffleMask(M, NumElems);
6872 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006873 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006874 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006875 }
6876
Craig Topperbeabc6c2011-12-05 06:56:46 +00006877 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006878 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006879 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006880 return V1;
6881 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6882 // the instruction selector will not match, so get a canonical MOVL with
6883 // swapped operands to undo the commute.
6884 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006885 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006886
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006887 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006888 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006889
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006890 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006891 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006892
Evan Cheng9bbbb982006-10-25 20:48:19 +00006893 if (V2IsSplat) {
6894 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006895 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006896 // new vector_shuffle with the corrected mask.p
6897 SmallVector<int, 8> NewMask(M.begin(), M.end());
6898 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006899 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006900 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006901 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006902 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006903 }
6904
Evan Cheng9eca5e82006-10-25 21:49:50 +00006905 if (Commuted) {
6906 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006907 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006908 CommuteVectorShuffleMask(M, NumElems);
6909 std::swap(V1, V2);
6910 std::swap(V1IsSplat, V2IsSplat);
6911 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006912
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006913 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006914 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006915
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006916 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006917 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006918 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006919
Nate Begeman9008ca62009-04-27 18:41:29 +00006920 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006921 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006922 return CommuteVectorShuffle(SVOp, DAG);
6923
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006924 // The checks below are all present in isShuffleMaskLegal, but they are
6925 // inlined here right now to enable us to directly emit target specific
6926 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006927
Craig Topper0e2037b2012-01-20 05:53:00 +00006928 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006929 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006930 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006931 DAG);
6932
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006933 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6934 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006935 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006936 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006937 }
6938
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006939 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006940 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006941 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006942 DAG);
6943
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006944 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006945 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006946 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006947 DAG);
6948
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006949 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006950 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006951 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006952
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006953 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006954 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006955 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006956 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006957
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006958 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006959 // Generate target specific nodes for 128 or 256-bit shuffles only
6960 // supported in the AVX instruction set.
6961 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006962
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006963 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006964 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006965 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6966
Craig Topper70b883b2011-11-28 10:14:51 +00006967 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006968 if (isVPERMILPMask(M, VT, HasFp256)) {
6969 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00006970 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006971 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006972 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006973 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006974 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006975
Craig Topper70b883b2011-11-28 10:14:51 +00006976 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006977 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00006978 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006979 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006980
Craig Topper1842ba02012-04-23 06:38:28 +00006981 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006982 if (BlendOp.getNode())
6983 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006984
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006985 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006986 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006987 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006988 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006989 }
Craig Topper92040742012-04-16 06:43:40 +00006990 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6991 &permclMask[0], 8);
6992 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006993 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006994 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006995 }
Craig Topper095c5282012-04-15 23:48:57 +00006996
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006997 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00006998 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006999 getShuffleCLImmediate(SVOp), DAG);
7000
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007001 //===--------------------------------------------------------------------===//
7002 // Since no target specific shuffle was selected for this generic one,
7003 // lower it into other known shuffles. FIXME: this isn't true yet, but
7004 // this is the plan.
7005 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007006
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007007 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7008 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007009 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007010 if (NewOp.getNode())
7011 return NewOp;
7012 }
7013
7014 if (VT == MVT::v16i8) {
7015 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7016 if (NewOp.getNode())
7017 return NewOp;
7018 }
7019
Elena Demikhovsky41789462012-09-06 12:42:01 +00007020 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007021 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007022 if (NewOp.getNode())
7023 return NewOp;
7024 }
7025
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007026 // Handle all 128-bit wide vectors with 4 elements, and match them with
7027 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007028 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007029 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7030
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007031 // Handle general 256-bit shuffles
7032 if (VT.is256BitVector())
7033 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7034
Dan Gohman475871a2008-07-27 21:46:04 +00007035 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007036}
7037
Dan Gohman475871a2008-07-27 21:46:04 +00007038SDValue
7039X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007040 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007041 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007042 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007043
Craig Topper7a9a28b2012-08-12 02:23:29 +00007044 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007045 return SDValue();
7046
Duncan Sands83ec4b62008-06-06 12:08:01 +00007047 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007048 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007049 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007051 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007052 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007053 }
7054
7055 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007056 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7057 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7058 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7060 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007061 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007063 Op.getOperand(0)),
7064 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007066 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007067 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007068 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007069 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007070 }
7071
7072 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007073 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7074 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007075 // result has a single use which is a store or a bitcast to i32. And in
7076 // the case of a store, it's not worth it if the index is a constant 0,
7077 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007078 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007079 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007080 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007081 if ((User->getOpcode() != ISD::STORE ||
7082 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7083 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007084 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007086 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007087 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007088 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007089 Op.getOperand(0)),
7090 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007091 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007092 }
7093
7094 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007095 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007096 if (isa<ConstantSDNode>(Op.getOperand(1)))
7097 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007098 }
Dan Gohman475871a2008-07-27 21:46:04 +00007099 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007100}
7101
Dan Gohman475871a2008-07-27 21:46:04 +00007102SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007103X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7104 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007105 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007106 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007107
David Greene74a579d2011-02-10 16:57:36 +00007108 SDValue Vec = Op.getOperand(0);
7109 EVT VecVT = Vec.getValueType();
7110
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007111 // If this is a 256-bit vector result, first extract the 128-bit vector and
7112 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007113 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007114 DebugLoc dl = Op.getNode()->getDebugLoc();
7115 unsigned NumElems = VecVT.getVectorNumElements();
7116 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007117 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7118
7119 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007120 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007121
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007122 if (IdxVal >= NumElems/2)
7123 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007124 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007125 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007126 }
7127
Craig Topper7a9a28b2012-08-12 02:23:29 +00007128 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007129
Craig Topperd0a31172012-01-10 06:37:29 +00007130 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007131 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007132 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007133 return Res;
7134 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007135
Owen Andersone50ed302009-08-10 22:56:29 +00007136 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007137 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007138 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007139 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007140 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007141 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007142 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7144 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007145 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007146 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007147 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007148 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007149 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007150 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007151 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007152 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007153 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007154 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007155 }
7156
7157 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007158 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007159 if (Idx == 0)
7160 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007161
Evan Cheng0db9fe62006-04-25 20:13:52 +00007162 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007163 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007164 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007165 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007166 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007167 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007168 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007169 }
7170
7171 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007172 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7173 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7174 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007175 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007176 if (Idx == 0)
7177 return Op;
7178
7179 // UNPCKHPD the element to the lowest double word, then movsd.
7180 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7181 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007182 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007183 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007184 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007185 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007186 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007187 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007188 }
7189
Dan Gohman475871a2008-07-27 21:46:04 +00007190 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007191}
7192
Dan Gohman475871a2008-07-27 21:46:04 +00007193SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007194X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7195 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007196 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007197 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007198 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007199
Dan Gohman475871a2008-07-27 21:46:04 +00007200 SDValue N0 = Op.getOperand(0);
7201 SDValue N1 = Op.getOperand(1);
7202 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007203
Craig Topper7a9a28b2012-08-12 02:23:29 +00007204 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007205 return SDValue();
7206
Dan Gohman8a55ce42009-09-23 21:02:20 +00007207 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007208 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007209 unsigned Opc;
7210 if (VT == MVT::v8i16)
7211 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007212 else if (VT == MVT::v16i8)
7213 Opc = X86ISD::PINSRB;
7214 else
7215 Opc = X86ISD::PINSRB;
7216
Nate Begeman14d12ca2008-02-11 04:19:36 +00007217 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7218 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 if (N1.getValueType() != MVT::i32)
7220 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7221 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007222 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007223 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007224 }
7225
7226 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007227 // Bits [7:6] of the constant are the source select. This will always be
7228 // zero here. The DAG Combiner may combine an extract_elt index into these
7229 // bits. For example (insert (extract, 3), 2) could be matched by putting
7230 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007231 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007232 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007233 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007234 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007235 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007236 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007237 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007238 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007239 }
7240
7241 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007242 // PINSR* works with constant index.
7243 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007244 }
Dan Gohman475871a2008-07-27 21:46:04 +00007245 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007246}
7247
Dan Gohman475871a2008-07-27 21:46:04 +00007248SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007249X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007250 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007251 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007252
David Greene6b381262011-02-09 15:32:06 +00007253 DebugLoc dl = Op.getDebugLoc();
7254 SDValue N0 = Op.getOperand(0);
7255 SDValue N1 = Op.getOperand(1);
7256 SDValue N2 = Op.getOperand(2);
7257
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007258 // If this is a 256-bit vector result, first extract the 128-bit vector,
7259 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007260 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007261 if (!isa<ConstantSDNode>(N2))
7262 return SDValue();
7263
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007264 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007265 unsigned NumElems = VT.getVectorNumElements();
7266 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007267 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007268
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007269 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007270 bool Upper = IdxVal >= NumElems/2;
7271 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7272 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007273
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007274 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007275 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007276 }
7277
Craig Topperd0a31172012-01-10 06:37:29 +00007278 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007279 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7280
Dan Gohman8a55ce42009-09-23 21:02:20 +00007281 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007282 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007283
Dan Gohman8a55ce42009-09-23 21:02:20 +00007284 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007285 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7286 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007287 if (N1.getValueType() != MVT::i32)
7288 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7289 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007290 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007291 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007292 }
Dan Gohman475871a2008-07-27 21:46:04 +00007293 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007294}
7295
Craig Topper55b24052012-09-11 06:15:32 +00007296static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007297 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007298 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007299 EVT OpVT = Op.getValueType();
7300
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007301 // If this is a 256-bit vector result, first insert into a 128-bit
7302 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007303 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007304 // Insert into a 128-bit vector.
7305 EVT VT128 = EVT::getVectorVT(*Context,
7306 OpVT.getVectorElementType(),
7307 OpVT.getVectorNumElements() / 2);
7308
7309 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7310
7311 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007312 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007313 }
7314
Craig Topperd77d2fe2012-04-29 20:22:05 +00007315 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007316 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007317 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007318
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007320 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007321 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007322 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007323}
7324
David Greene91585092011-01-26 15:38:49 +00007325// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7326// a simple subregister reference or explicit instructions to grab
7327// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007328static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7329 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007330 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007331 DebugLoc dl = Op.getNode()->getDebugLoc();
7332 SDValue Vec = Op.getNode()->getOperand(0);
7333 SDValue Idx = Op.getNode()->getOperand(1);
7334
Craig Topper7a9a28b2012-08-12 02:23:29 +00007335 if (Op.getNode()->getValueType(0).is128BitVector() &&
7336 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007337 isa<ConstantSDNode>(Idx)) {
7338 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7339 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007340 }
David Greene91585092011-01-26 15:38:49 +00007341 }
7342 return SDValue();
7343}
7344
David Greenecfe33c42011-01-26 19:13:22 +00007345// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7346// simple superregister reference or explicit instructions to insert
7347// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007348static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7349 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007350 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007351 DebugLoc dl = Op.getNode()->getDebugLoc();
7352 SDValue Vec = Op.getNode()->getOperand(0);
7353 SDValue SubVec = Op.getNode()->getOperand(1);
7354 SDValue Idx = Op.getNode()->getOperand(2);
7355
Craig Topper7a9a28b2012-08-12 02:23:29 +00007356 if (Op.getNode()->getValueType(0).is256BitVector() &&
7357 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007358 isa<ConstantSDNode>(Idx)) {
7359 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7360 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007361 }
7362 }
7363 return SDValue();
7364}
7365
Bill Wendling056292f2008-09-16 21:48:12 +00007366// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7367// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7368// one of the above mentioned nodes. It has to be wrapped because otherwise
7369// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7370// be used to form addressing mode. These wrapped nodes will be selected
7371// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007372SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007373X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007374 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007375
Chris Lattner41621a22009-06-26 19:22:52 +00007376 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7377 // global base reg.
7378 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007379 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007380 CodeModel::Model M = getTargetMachine().getCodeModel();
7381
Chris Lattner4f066492009-07-11 20:29:19 +00007382 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007383 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007384 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007385 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007386 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007387 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007388 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007389
Evan Cheng1606e8e2009-03-13 07:51:59 +00007390 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007391 CP->getAlignment(),
7392 CP->getOffset(), OpFlag);
7393 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007394 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007395 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007396 if (OpFlag) {
7397 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007398 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007399 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007400 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007401 }
7402
7403 return Result;
7404}
7405
Dan Gohmand858e902010-04-17 15:26:15 +00007406SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007407 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007408
Chris Lattner18c59872009-06-27 04:16:01 +00007409 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7410 // global base reg.
7411 unsigned char OpFlag = 0;
7412 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007413 CodeModel::Model M = getTargetMachine().getCodeModel();
7414
Chris Lattner4f066492009-07-11 20:29:19 +00007415 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007416 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007417 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007418 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007419 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007420 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007421 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007422
Chris Lattner18c59872009-06-27 04:16:01 +00007423 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7424 OpFlag);
7425 DebugLoc DL = JT->getDebugLoc();
7426 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007427
Chris Lattner18c59872009-06-27 04:16:01 +00007428 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007429 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007430 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7431 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007432 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007433 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007434
Chris Lattner18c59872009-06-27 04:16:01 +00007435 return Result;
7436}
7437
7438SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007439X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007440 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007441
Chris Lattner18c59872009-06-27 04:16:01 +00007442 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7443 // global base reg.
7444 unsigned char OpFlag = 0;
7445 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007446 CodeModel::Model M = getTargetMachine().getCodeModel();
7447
Chris Lattner4f066492009-07-11 20:29:19 +00007448 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007449 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7450 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7451 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007452 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007453 } else if (Subtarget->isPICStyleGOT()) {
7454 OpFlag = X86II::MO_GOT;
7455 } else if (Subtarget->isPICStyleStubPIC()) {
7456 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7457 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7458 OpFlag = X86II::MO_DARWIN_NONLAZY;
7459 }
Eric Christopherfd179292009-08-27 18:07:15 +00007460
Chris Lattner18c59872009-06-27 04:16:01 +00007461 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007462
Chris Lattner18c59872009-06-27 04:16:01 +00007463 DebugLoc DL = Op.getDebugLoc();
7464 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007465
Chris Lattner18c59872009-06-27 04:16:01 +00007466 // With PIC, the address is actually $g + Offset.
7467 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007468 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007469 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7470 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007471 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007472 Result);
7473 }
Eric Christopherfd179292009-08-27 18:07:15 +00007474
Eli Friedman586272d2011-08-11 01:48:05 +00007475 // For symbols that require a load from a stub to get the address, emit the
7476 // load.
7477 if (isGlobalStubReference(OpFlag))
7478 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007479 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007480
Chris Lattner18c59872009-06-27 04:16:01 +00007481 return Result;
7482}
7483
Dan Gohman475871a2008-07-27 21:46:04 +00007484SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007485X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007486 // Create the TargetBlockAddressAddress node.
7487 unsigned char OpFlags =
7488 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007489 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007490 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007491 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007492 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007493 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7494 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007495
Dan Gohmanf705adb2009-10-30 01:28:02 +00007496 if (Subtarget->isPICStyleRIPRel() &&
7497 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007498 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7499 else
7500 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007501
Dan Gohman29cbade2009-11-20 23:18:13 +00007502 // With PIC, the address is actually $g + Offset.
7503 if (isGlobalRelativeToPICBase(OpFlags)) {
7504 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7505 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7506 Result);
7507 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007508
7509 return Result;
7510}
7511
7512SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007513X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007514 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007515 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007516 // Create the TargetGlobalAddress node, folding in the constant
7517 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007518 unsigned char OpFlags =
7519 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007520 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007521 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007522 if (OpFlags == X86II::MO_NO_FLAG &&
7523 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007524 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007525 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007526 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007527 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007528 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007529 }
Eric Christopherfd179292009-08-27 18:07:15 +00007530
Chris Lattner4f066492009-07-11 20:29:19 +00007531 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007532 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007533 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7534 else
7535 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007536
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007537 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007538 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007539 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7540 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007541 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007542 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007543
Chris Lattner36c25012009-07-10 07:34:39 +00007544 // For globals that require a load from a stub to get the address, emit the
7545 // load.
7546 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007547 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007548 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007549
Dan Gohman6520e202008-10-18 02:06:02 +00007550 // If there was a non-zero offset that we didn't fold, create an explicit
7551 // addition for it.
7552 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007553 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007554 DAG.getConstant(Offset, getPointerTy()));
7555
Evan Cheng0db9fe62006-04-25 20:13:52 +00007556 return Result;
7557}
7558
Evan Chengda43bcf2008-09-24 00:05:32 +00007559SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007560X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007561 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007562 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007563 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007564}
7565
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007566static SDValue
7567GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007568 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007569 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007570 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007571 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007572 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007573 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007574 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007575 GA->getOffset(),
7576 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007577
7578 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7579 : X86ISD::TLSADDR;
7580
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007581 if (InFlag) {
7582 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007583 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007584 } else {
7585 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007586 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007587 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007588
7589 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007590 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007591
Rafael Espindola15f1b662009-04-24 12:59:40 +00007592 SDValue Flag = Chain.getValue(1);
7593 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007594}
7595
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007596// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007597static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007598LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007599 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007600 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007601 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7602 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007603 DAG.getNode(X86ISD::GlobalBaseReg,
7604 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007605 InFlag = Chain.getValue(1);
7606
Chris Lattnerb903bed2009-06-26 21:20:29 +00007607 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007608}
7609
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007610// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007611static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007612LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007613 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007614 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7615 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007616}
7617
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007618static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7619 SelectionDAG &DAG,
7620 const EVT PtrVT,
7621 bool is64Bit) {
7622 DebugLoc dl = GA->getDebugLoc();
7623
7624 // Get the start address of the TLS block for this module.
7625 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7626 .getInfo<X86MachineFunctionInfo>();
7627 MFI->incNumLocalDynamicTLSAccesses();
7628
7629 SDValue Base;
7630 if (is64Bit) {
7631 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7632 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7633 } else {
7634 SDValue InFlag;
7635 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7636 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7637 InFlag = Chain.getValue(1);
7638 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7639 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7640 }
7641
7642 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7643 // of Base.
7644
7645 // Build x@dtpoff.
7646 unsigned char OperandFlags = X86II::MO_DTPOFF;
7647 unsigned WrapperKind = X86ISD::Wrapper;
7648 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7649 GA->getValueType(0),
7650 GA->getOffset(), OperandFlags);
7651 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7652
7653 // Add x@dtpoff with the base.
7654 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7655}
7656
Hans Wennborg228756c2012-05-11 10:11:01 +00007657// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007658static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007659 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007660 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007661 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007662
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007663 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7664 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7665 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007666
Michael J. Spencerec38de22010-10-10 22:04:20 +00007667 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007668 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007669 MachinePointerInfo(Ptr),
7670 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007671
Chris Lattnerb903bed2009-06-26 21:20:29 +00007672 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007673 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7674 // initialexec.
7675 unsigned WrapperKind = X86ISD::Wrapper;
7676 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007677 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007678 } else if (model == TLSModel::InitialExec) {
7679 if (is64Bit) {
7680 OperandFlags = X86II::MO_GOTTPOFF;
7681 WrapperKind = X86ISD::WrapperRIP;
7682 } else {
7683 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7684 }
Chris Lattner18c59872009-06-27 04:16:01 +00007685 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007686 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007687 }
Eric Christopherfd179292009-08-27 18:07:15 +00007688
Hans Wennborg228756c2012-05-11 10:11:01 +00007689 // emit "addl x@ntpoff,%eax" (local exec)
7690 // or "addl x@indntpoff,%eax" (initial exec)
7691 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007692 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007693 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007694 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007695 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007696
Hans Wennborg228756c2012-05-11 10:11:01 +00007697 if (model == TLSModel::InitialExec) {
7698 if (isPIC && !is64Bit) {
7699 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7700 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7701 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007702 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007703
7704 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7705 MachinePointerInfo::getGOT(), false, false, false,
7706 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007707 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007708
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007709 // The address of the thread local variable is the add of the thread
7710 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007711 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007712}
7713
Dan Gohman475871a2008-07-27 21:46:04 +00007714SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007715X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007716
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007717 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007718 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007719
Eric Christopher30ef0e52010-06-03 04:07:48 +00007720 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007721 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007722
Eric Christopher30ef0e52010-06-03 04:07:48 +00007723 switch (model) {
7724 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007725 if (Subtarget->is64Bit())
7726 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7727 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007728 case TLSModel::LocalDynamic:
7729 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7730 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007731 case TLSModel::InitialExec:
7732 case TLSModel::LocalExec:
7733 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007734 Subtarget->is64Bit(),
7735 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007736 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007737 llvm_unreachable("Unknown TLS model.");
7738 }
7739
7740 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007741 // Darwin only has one model of TLS. Lower to that.
7742 unsigned char OpFlag = 0;
7743 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7744 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007745
Eric Christopher30ef0e52010-06-03 04:07:48 +00007746 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7747 // global base reg.
7748 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7749 !Subtarget->is64Bit();
7750 if (PIC32)
7751 OpFlag = X86II::MO_TLVP_PIC_BASE;
7752 else
7753 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007754 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007755 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007756 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007757 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007758 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007759
Eric Christopher30ef0e52010-06-03 04:07:48 +00007760 // With PIC32, the address is actually $g + Offset.
7761 if (PIC32)
7762 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7763 DAG.getNode(X86ISD::GlobalBaseReg,
7764 DebugLoc(), getPointerTy()),
7765 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007766
Eric Christopher30ef0e52010-06-03 04:07:48 +00007767 // Lowering the machine isd will make sure everything is in the right
7768 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007769 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007770 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007771 SDValue Args[] = { Chain, Offset };
7772 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007773
Eric Christopher30ef0e52010-06-03 04:07:48 +00007774 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7775 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7776 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007777
Eric Christopher30ef0e52010-06-03 04:07:48 +00007778 // And our return value (tls address) is in the standard call return value
7779 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007780 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007781 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7782 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007783 }
7784
7785 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007786 // Just use the implicit TLS architecture
7787 // Need to generate someting similar to:
7788 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7789 // ; from TEB
7790 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7791 // mov rcx, qword [rdx+rcx*8]
7792 // mov eax, .tls$:tlsvar
7793 // [rax+rcx] contains the address
7794 // Windows 64bit: gs:0x58
7795 // Windows 32bit: fs:__tls_array
7796
7797 // If GV is an alias then use the aliasee for determining
7798 // thread-localness.
7799 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7800 GV = GA->resolveAliasedGlobal(false);
7801 DebugLoc dl = GA->getDebugLoc();
7802 SDValue Chain = DAG.getEntryNode();
7803
7804 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7805 // %gs:0x58 (64-bit).
7806 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7807 ? Type::getInt8PtrTy(*DAG.getContext(),
7808 256)
7809 : Type::getInt32PtrTy(*DAG.getContext(),
7810 257));
7811
7812 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7813 Subtarget->is64Bit()
7814 ? DAG.getIntPtrConstant(0x58)
7815 : DAG.getExternalSymbol("_tls_array",
7816 getPointerTy()),
7817 MachinePointerInfo(Ptr),
7818 false, false, false, 0);
7819
7820 // Load the _tls_index variable
7821 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7822 if (Subtarget->is64Bit())
7823 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7824 IDX, MachinePointerInfo(), MVT::i32,
7825 false, false, 0);
7826 else
7827 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7828 false, false, false, 0);
7829
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007830 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007831 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007832 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7833
7834 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7835 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7836 false, false, false, 0);
7837
7838 // Get the offset of start of .tls section
7839 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7840 GA->getValueType(0),
7841 GA->getOffset(), X86II::MO_SECREL);
7842 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7843
7844 // The address of the thread local variable is the add of the thread
7845 // pointer with the offset of the variable.
7846 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007847 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007848
David Blaikie4d6ccb52012-01-20 21:51:11 +00007849 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007850}
7851
Chad Rosierb90d2a92012-01-03 23:19:12 +00007852/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7853/// and take a 2 x i32 value to shift plus a shift amount.
7854SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007855 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007856 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007857 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007858 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007859 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007860 SDValue ShOpLo = Op.getOperand(0);
7861 SDValue ShOpHi = Op.getOperand(1);
7862 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007863 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007864 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007865 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007866
Dan Gohman475871a2008-07-27 21:46:04 +00007867 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007868 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007869 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7870 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007871 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007872 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7873 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007874 }
Evan Chenge3413162006-01-09 18:33:28 +00007875
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7877 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007878 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007879 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007880
Dan Gohman475871a2008-07-27 21:46:04 +00007881 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007883 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7884 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007885
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007886 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007887 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7888 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007889 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007890 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7891 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007892 }
7893
Dan Gohman475871a2008-07-27 21:46:04 +00007894 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007895 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007896}
Evan Chenga3195e82006-01-12 22:54:21 +00007897
Dan Gohmand858e902010-04-17 15:26:15 +00007898SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7899 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007900 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007901
Dale Johannesen0488fb62010-09-30 23:57:10 +00007902 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007903 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007904
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007906 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007907
Eli Friedman36df4992009-05-27 00:47:34 +00007908 // These are really Legal; return the operand so the caller accepts it as
7909 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007911 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007913 Subtarget->is64Bit()) {
7914 return Op;
7915 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007916
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007917 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007918 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007919 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007920 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007921 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007922 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007923 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007924 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007925 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007926 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7927}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007928
Owen Andersone50ed302009-08-10 22:56:29 +00007929SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007930 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007931 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007932 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007933 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007934 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007935 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007936 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007937 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007938 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007939 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007940
Chris Lattner492a43e2010-09-22 01:28:21 +00007941 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007942
Stuart Hastings84be9582011-06-02 15:57:11 +00007943 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7944 MachineMemOperand *MMO;
7945 if (FI) {
7946 int SSFI = FI->getIndex();
7947 MMO =
7948 DAG.getMachineFunction()
7949 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7950 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7951 } else {
7952 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7953 StackSlot = StackSlot.getOperand(1);
7954 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007955 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007956 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7957 X86ISD::FILD, DL,
7958 Tys, Ops, array_lengthof(Ops),
7959 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007960
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007961 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007962 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007963 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007964
7965 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7966 // shouldn't be necessary except that RFP cannot be live across
7967 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007968 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007969 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7970 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007971 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007973 SDValue Ops[] = {
7974 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7975 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007976 MachineMemOperand *MMO =
7977 DAG.getMachineFunction()
7978 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007979 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007980
Chris Lattner492a43e2010-09-22 01:28:21 +00007981 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7982 Ops, array_lengthof(Ops),
7983 Op.getValueType(), MMO);
7984 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007985 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007986 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007987 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007988
Evan Cheng0db9fe62006-04-25 20:13:52 +00007989 return Result;
7990}
7991
Bill Wendling8b8a6362009-01-17 03:56:04 +00007992// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007993SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7994 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007995 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007996 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007997 movq %rax, %xmm0
7998 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7999 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8000 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008001 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008002 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008003 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008004 addpd %xmm1, %xmm0
8005 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008006 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008007
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008008 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008009 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008010
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008011 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008012 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8013 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008014 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008015
Chris Lattner97484792012-01-25 09:56:22 +00008016 SmallVector<Constant*,2> CV1;
8017 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00008018 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008019 CV1.push_back(
8020 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
8021 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008022 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008023
Bill Wendling397ae212012-01-05 02:13:20 +00008024 // Load the 64-bit value into an XMM register.
8025 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8026 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008027 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008028 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008029 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008030 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8031 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8032 CLod0);
8033
Owen Anderson825b72b2009-08-11 20:47:22 +00008034 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008035 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008036 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008037 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008038 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008039 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008040
Craig Topperd0a31172012-01-10 06:37:29 +00008041 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008042 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8043 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8044 } else {
8045 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8046 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8047 S2F, 0x4E, DAG);
8048 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8049 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8050 Sub);
8051 }
8052
8053 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008054 DAG.getIntPtrConstant(0));
8055}
8056
Bill Wendling8b8a6362009-01-17 03:56:04 +00008057// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008058SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8059 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008060 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008061 // FP constant to bias correct the final result.
8062 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008064
8065 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008066 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008067 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008068
Eli Friedmanf3704762011-08-29 21:15:46 +00008069 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008070 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008071
Owen Anderson825b72b2009-08-11 20:47:22 +00008072 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008073 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008074 DAG.getIntPtrConstant(0));
8075
8076 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008078 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008079 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008081 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008082 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 MVT::v2f64, Bias)));
8084 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008085 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008086 DAG.getIntPtrConstant(0));
8087
8088 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008090
8091 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008092 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008093
Craig Topper69947b92012-04-23 06:57:04 +00008094 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008095 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008096 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008097 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008098 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008099
8100 // Handle final rounding.
8101 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008102}
8103
Michael Liaoa7554632012-10-23 17:36:08 +00008104SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8105 SelectionDAG &DAG) const {
8106 SDValue N0 = Op.getOperand(0);
8107 EVT SVT = N0.getValueType();
8108 DebugLoc dl = Op.getDebugLoc();
8109
8110 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8111 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8112 "Custom UINT_TO_FP is not supported!");
8113
8114 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, SVT.getVectorNumElements());
8115 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8116 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8117}
8118
Dan Gohmand858e902010-04-17 15:26:15 +00008119SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8120 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008121 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008122 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008123
Michael Liaoa7554632012-10-23 17:36:08 +00008124 if (Op.getValueType().isVector())
8125 return lowerUINT_TO_FP_vec(Op, DAG);
8126
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008127 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008128 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8129 // the optimization here.
8130 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008131 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008132
Owen Andersone50ed302009-08-10 22:56:29 +00008133 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008134 EVT DstVT = Op.getValueType();
8135 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008136 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008137 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008138 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008139 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008140 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008141
8142 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008143 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008144 if (SrcVT == MVT::i32) {
8145 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8146 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8147 getPointerTy(), StackSlot, WordOff);
8148 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008149 StackSlot, MachinePointerInfo(),
8150 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008151 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008152 OffsetSlot, MachinePointerInfo(),
8153 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008154 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8155 return Fild;
8156 }
8157
8158 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8159 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008160 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008161 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008162 // For i64 source, we need to add the appropriate power of 2 if the input
8163 // was negative. This is the same as the optimization in
8164 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8165 // we must be careful to do the computation in x87 extended precision, not
8166 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008167 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8168 MachineMemOperand *MMO =
8169 DAG.getMachineFunction()
8170 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8171 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008172
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008173 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8174 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008175 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8176 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008177
8178 APInt FF(32, 0x5F800000ULL);
8179
8180 // Check whether the sign bit is set.
8181 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8182 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8183 ISD::SETLT);
8184
8185 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8186 SDValue FudgePtr = DAG.getConstantPool(
8187 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8188 getPointerTy());
8189
8190 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8191 SDValue Zero = DAG.getIntPtrConstant(0);
8192 SDValue Four = DAG.getIntPtrConstant(4);
8193 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8194 Zero, Four);
8195 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8196
8197 // Load the value out, extending it from f32 to f80.
8198 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008199 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008200 FudgePtr, MachinePointerInfo::getConstantPool(),
8201 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008202 // Extend everything to 80 bits to force it to be done on x87.
8203 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8204 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008205}
8206
Dan Gohman475871a2008-07-27 21:46:04 +00008207std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008208FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008209 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008210
Owen Andersone50ed302009-08-10 22:56:29 +00008211 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008212
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008213 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008214 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8215 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008216 }
8217
Owen Anderson825b72b2009-08-11 20:47:22 +00008218 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8219 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008220 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008221
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008222 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008223 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008224 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008225 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008226 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008227 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008228 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008229 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008230
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008231 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8232 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008233 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008234 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008235 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008236 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008237
Evan Cheng0db9fe62006-04-25 20:13:52 +00008238 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008239 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8240 Opc = X86ISD::WIN_FTOL;
8241 else
8242 switch (DstTy.getSimpleVT().SimpleTy) {
8243 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8244 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8245 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8246 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8247 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008248
Dan Gohman475871a2008-07-27 21:46:04 +00008249 SDValue Chain = DAG.getEntryNode();
8250 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008251 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008252 // FIXME This causes a redundant load/store if the SSE-class value is already
8253 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008254 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008255 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008256 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008257 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008258 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008259 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008260 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008261 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008262 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008263
Chris Lattner492a43e2010-09-22 01:28:21 +00008264 MachineMemOperand *MMO =
8265 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8266 MachineMemOperand::MOLoad, MemSize, MemSize);
8267 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8268 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008269 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008270 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008271 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8272 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008273
Chris Lattner07290932010-09-22 01:05:16 +00008274 MachineMemOperand *MMO =
8275 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8276 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008277
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008278 if (Opc != X86ISD::WIN_FTOL) {
8279 // Build the FP_TO_INT*_IN_MEM
8280 SDValue Ops[] = { Chain, Value, StackSlot };
8281 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8282 Ops, 3, DstTy, MMO);
8283 return std::make_pair(FIST, StackSlot);
8284 } else {
8285 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8286 DAG.getVTList(MVT::Other, MVT::Glue),
8287 Chain, Value);
8288 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8289 MVT::i32, ftol.getValue(1));
8290 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8291 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008292 SDValue Ops[] = { eax, edx };
8293 SDValue pair = IsReplace
8294 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8295 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008296 return std::make_pair(pair, SDValue());
8297 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008298}
8299
Nadav Rotem0509db22012-12-28 05:45:24 +00008300static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8301 const X86Subtarget *Subtarget) {
8302 EVT VT = Op->getValueType(0);
8303 SDValue In = Op->getOperand(0);
8304 EVT InVT = In.getValueType();
8305 DebugLoc dl = Op->getDebugLoc();
8306
8307 // Optimize vectors in AVX mode:
8308 //
8309 // v8i16 -> v8i32
8310 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8311 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8312 // Concat upper and lower parts.
8313 //
8314 // v4i32 -> v4i64
8315 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8316 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8317 // Concat upper and lower parts.
8318 //
8319
8320 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8321 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8322 return SDValue();
8323
8324 if (Subtarget->hasInt256())
8325 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8326
8327 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8328 SDValue Undef = DAG.getUNDEF(InVT);
8329 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8330 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8331 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8332
8333 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8334 VT.getVectorNumElements()/2);
8335
8336 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8337 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8338
8339 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8340}
8341
8342SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8343 SelectionDAG &DAG) const {
8344 if (Subtarget->hasFp256()) {
8345 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8346 if (Res.getNode())
8347 return Res;
8348 }
8349
8350 return SDValue();
8351}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008352SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8353 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008354 DebugLoc DL = Op.getDebugLoc();
8355 EVT VT = Op.getValueType();
8356 SDValue In = Op.getOperand(0);
8357 EVT SVT = In.getValueType();
8358
Nadav Rotem0509db22012-12-28 05:45:24 +00008359 if (Subtarget->hasFp256()) {
8360 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8361 if (Res.getNode())
8362 return Res;
8363 }
8364
Michael Liaoa7554632012-10-23 17:36:08 +00008365 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8366 VT.getVectorNumElements() != SVT.getVectorNumElements())
8367 return SDValue();
8368
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008369 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008370
8371 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008372 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008373 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8374
8375 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8376 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8377 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008378 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8379 DAG.getUNDEF(MVT::v8i16),
8380 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008381
8382 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8383}
8384
Michael Liaobedcbd42012-10-16 18:14:11 +00008385SDValue X86TargetLowering::lowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
8386 DebugLoc DL = Op.getDebugLoc();
8387 EVT VT = Op.getValueType();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008388 SDValue In = Op.getOperand(0);
8389 EVT SVT = In.getValueType();
Michael Liaobedcbd42012-10-16 18:14:11 +00008390
Nadav Rotem3c22a442012-12-27 07:45:10 +00008391 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8392 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8393 if (Subtarget->hasInt256()) {
8394 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8395 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8396 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8397 ShufMask);
8398 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8399 DAG.getIntPtrConstant(0));
8400 }
8401
8402 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8403 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8404 DAG.getIntPtrConstant(0));
8405 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8406 DAG.getIntPtrConstant(2));
8407
8408 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8409 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8410
8411 // The PSHUFD mask:
8412 static const int ShufMask1[] = {0, 2, 0, 0};
8413 SDValue Undef = DAG.getUNDEF(VT);
8414 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8415 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8416
8417 // The MOVLHPS mask:
8418 static const int ShufMask2[] = {0, 1, 4, 5};
8419 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8420 }
8421
8422 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8423 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8424 if (Subtarget->hasInt256()) {
8425 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8426
8427 SmallVector<SDValue,32> pshufbMask;
8428 for (unsigned i = 0; i < 2; ++i) {
8429 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8430 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8431 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8432 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8433 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8434 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8435 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8436 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8437 for (unsigned j = 0; j < 8; ++j)
8438 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8439 }
8440 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8441 &pshufbMask[0], 32);
8442 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8443 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8444
8445 static const int ShufMask[] = {0, 2, -1, -1};
8446 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8447 &ShufMask[0]);
8448 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8449 DAG.getIntPtrConstant(0));
8450 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8451 }
8452
8453 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8454 DAG.getIntPtrConstant(0));
8455
8456 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8457 DAG.getIntPtrConstant(4));
8458
8459 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8460 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8461
8462 // The PSHUFB mask:
8463 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8464 -1, -1, -1, -1, -1, -1, -1, -1};
8465
8466 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8467 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8468 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8469
8470 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8471 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8472
8473 // The MOVLHPS Mask:
8474 static const int ShufMask2[] = {0, 1, 4, 5};
8475 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8476 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8477 }
8478
8479 // Handle truncation of V256 to V128 using shuffles.
8480 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008481 return SDValue();
8482
Nadav Rotem3c22a442012-12-27 07:45:10 +00008483 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8484 "Invalid op");
8485 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008486
8487 unsigned NumElems = VT.getVectorNumElements();
8488 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8489 NumElems * 2);
8490
Michael Liaobedcbd42012-10-16 18:14:11 +00008491 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8492 // Prepare truncation shuffle mask
8493 for (unsigned i = 0; i != NumElems; ++i)
8494 MaskVec[i] = i * 2;
8495 SDValue V = DAG.getVectorShuffle(NVT, DL,
8496 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8497 DAG.getUNDEF(NVT), &MaskVec[0]);
8498 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8499 DAG.getIntPtrConstant(0));
8500}
8501
Dan Gohmand858e902010-04-17 15:26:15 +00008502SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8503 SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008504 if (Op.getValueType().isVector()) {
8505 if (Op.getValueType() == MVT::v8i16)
8506 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), Op.getValueType(),
8507 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8508 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008509 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008510 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008511
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008512 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8513 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008514 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008515 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8516 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008517
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008518 if (StackSlot.getNode())
8519 // Load the result.
8520 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8521 FIST, StackSlot, MachinePointerInfo(),
8522 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008523
8524 // The node is the result.
8525 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008526}
8527
Dan Gohmand858e902010-04-17 15:26:15 +00008528SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8529 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008530 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8531 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008532 SDValue FIST = Vals.first, StackSlot = Vals.second;
8533 assert(FIST.getNode() && "Unexpected failure");
8534
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008535 if (StackSlot.getNode())
8536 // Load the result.
8537 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8538 FIST, StackSlot, MachinePointerInfo(),
8539 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008540
8541 // The node is the result.
8542 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008543}
8544
Michael Liao9d796db2012-10-10 16:32:15 +00008545SDValue X86TargetLowering::lowerFP_EXTEND(SDValue Op,
8546 SelectionDAG &DAG) const {
8547 DebugLoc DL = Op.getDebugLoc();
8548 EVT VT = Op.getValueType();
8549 SDValue In = Op.getOperand(0);
8550 EVT SVT = In.getValueType();
8551
8552 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8553
8554 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8555 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8556 In, DAG.getUNDEF(SVT)));
8557}
8558
Craig Topper43620672012-09-08 07:31:51 +00008559SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008560 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008561 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008562 EVT VT = Op.getValueType();
8563 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008564 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8565 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008566 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008567 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008568 }
Craig Topper43620672012-09-08 07:31:51 +00008569 Constant *C;
8570 if (EltVT == MVT::f64)
8571 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8572 else
8573 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8574 C = ConstantVector::getSplat(NumElts, C);
8575 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8576 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008577 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008578 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008579 false, false, false, Alignment);
8580 if (VT.isVector()) {
8581 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8582 return DAG.getNode(ISD::BITCAST, dl, VT,
8583 DAG.getNode(ISD::AND, dl, ANDVT,
8584 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8585 Op.getOperand(0)),
8586 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8587 }
Dale Johannesenace16102009-02-03 19:33:06 +00008588 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008589}
8590
Dan Gohmand858e902010-04-17 15:26:15 +00008591SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008592 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008593 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008594 EVT VT = Op.getValueType();
8595 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008596 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8597 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008598 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008599 NumElts = VT.getVectorNumElements();
8600 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008601 Constant *C;
8602 if (EltVT == MVT::f64)
8603 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8604 else
8605 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8606 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008607 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8608 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008609 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008610 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008611 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008612 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008613 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008614 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008615 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008616 DAG.getNode(ISD::BITCAST, dl, XORVT,
8617 Op.getOperand(0)),
8618 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008619 }
Craig Topper69947b92012-04-23 06:57:04 +00008620
8621 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008622}
8623
Dan Gohmand858e902010-04-17 15:26:15 +00008624SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008625 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008626 SDValue Op0 = Op.getOperand(0);
8627 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008628 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008629 EVT VT = Op.getValueType();
8630 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008631
8632 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008633 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008634 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008635 SrcVT = VT;
8636 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008637 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008638 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008639 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008640 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008641 }
8642
8643 // At this point the operands and the result should have the same
8644 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008645
Evan Cheng68c47cb2007-01-05 07:55:56 +00008646 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008647 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008648 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008649 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8650 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008651 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008652 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8653 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8654 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8655 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008656 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008657 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008658 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008659 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008660 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008661 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008662 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008663
8664 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008665 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008666 // Op0 is MVT::f32, Op1 is MVT::f64.
8667 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8668 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8669 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008670 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008671 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008672 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008673 }
8674
Evan Cheng73d6cf12007-01-05 21:37:56 +00008675 // Clear first operand sign bit.
8676 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008677 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008678 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8679 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008680 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008681 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8682 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8683 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8684 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008685 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008686 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008687 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008688 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008689 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008690 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008691 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008692
8693 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008694 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008695}
8696
Craig Topper55b24052012-09-11 06:15:32 +00008697static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008698 SDValue N0 = Op.getOperand(0);
8699 DebugLoc dl = Op.getDebugLoc();
8700 EVT VT = Op.getValueType();
8701
8702 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8703 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8704 DAG.getConstant(1, VT));
8705 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8706}
8707
Michael Liaof966e4e2012-09-13 20:24:54 +00008708// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8709//
8710SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8711 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8712
8713 if (!Subtarget->hasSSE41())
8714 return SDValue();
8715
8716 if (!Op->hasOneUse())
8717 return SDValue();
8718
8719 SDNode *N = Op.getNode();
8720 DebugLoc DL = N->getDebugLoc();
8721
8722 SmallVector<SDValue, 8> Opnds;
8723 DenseMap<SDValue, unsigned> VecInMap;
8724 EVT VT = MVT::Other;
8725
8726 // Recognize a special case where a vector is casted into wide integer to
8727 // test all 0s.
8728 Opnds.push_back(N->getOperand(0));
8729 Opnds.push_back(N->getOperand(1));
8730
8731 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8732 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8733 // BFS traverse all OR'd operands.
8734 if (I->getOpcode() == ISD::OR) {
8735 Opnds.push_back(I->getOperand(0));
8736 Opnds.push_back(I->getOperand(1));
8737 // Re-evaluate the number of nodes to be traversed.
8738 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8739 continue;
8740 }
8741
8742 // Quit if a non-EXTRACT_VECTOR_ELT
8743 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8744 return SDValue();
8745
8746 // Quit if without a constant index.
8747 SDValue Idx = I->getOperand(1);
8748 if (!isa<ConstantSDNode>(Idx))
8749 return SDValue();
8750
8751 SDValue ExtractedFromVec = I->getOperand(0);
8752 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8753 if (M == VecInMap.end()) {
8754 VT = ExtractedFromVec.getValueType();
8755 // Quit if not 128/256-bit vector.
8756 if (!VT.is128BitVector() && !VT.is256BitVector())
8757 return SDValue();
8758 // Quit if not the same type.
8759 if (VecInMap.begin() != VecInMap.end() &&
8760 VT != VecInMap.begin()->first.getValueType())
8761 return SDValue();
8762 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8763 }
8764 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8765 }
8766
8767 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008768 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008769
8770 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8771 SmallVector<SDValue, 8> VecIns;
8772
8773 for (DenseMap<SDValue, unsigned>::const_iterator
8774 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8775 // Quit if not all elements are used.
8776 if (I->second != FullMask)
8777 return SDValue();
8778 VecIns.push_back(I->first);
8779 }
8780
8781 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8782
8783 // Cast all vectors into TestVT for PTEST.
8784 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8785 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8786
8787 // If more than one full vectors are evaluated, OR them first before PTEST.
8788 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8789 // Each iteration will OR 2 nodes and append the result until there is only
8790 // 1 node left, i.e. the final OR'd value of all vectors.
8791 SDValue LHS = VecIns[Slot];
8792 SDValue RHS = VecIns[Slot + 1];
8793 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8794 }
8795
8796 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8797 VecIns.back(), VecIns.back());
8798}
8799
Dan Gohman076aee32009-03-04 19:44:21 +00008800/// Emit nodes that will be selected as "test Op0,Op0", or something
8801/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008802SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008803 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008804 DebugLoc dl = Op.getDebugLoc();
8805
Dan Gohman31125812009-03-07 01:58:32 +00008806 // CF and OF aren't always set the way we want. Determine which
8807 // of these we need.
8808 bool NeedCF = false;
8809 bool NeedOF = false;
8810 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008811 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008812 case X86::COND_A: case X86::COND_AE:
8813 case X86::COND_B: case X86::COND_BE:
8814 NeedCF = true;
8815 break;
8816 case X86::COND_G: case X86::COND_GE:
8817 case X86::COND_L: case X86::COND_LE:
8818 case X86::COND_O: case X86::COND_NO:
8819 NeedOF = true;
8820 break;
Dan Gohman31125812009-03-07 01:58:32 +00008821 }
8822
Dan Gohman076aee32009-03-04 19:44:21 +00008823 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008824 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8825 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008826 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8827 // Emit a CMP with 0, which is the TEST pattern.
8828 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8829 DAG.getConstant(0, Op.getValueType()));
8830
8831 unsigned Opcode = 0;
8832 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008833
8834 // Truncate operations may prevent the merge of the SETCC instruction
8835 // and the arithmetic intruction before it. Attempt to truncate the operands
8836 // of the arithmetic instruction and use a reduced bit-width instruction.
8837 bool NeedTruncation = false;
8838 SDValue ArithOp = Op;
8839 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8840 SDValue Arith = Op->getOperand(0);
8841 // Both the trunc and the arithmetic op need to have one user each.
8842 if (Arith->hasOneUse())
8843 switch (Arith.getOpcode()) {
8844 default: break;
8845 case ISD::ADD:
8846 case ISD::SUB:
8847 case ISD::AND:
8848 case ISD::OR:
8849 case ISD::XOR: {
8850 NeedTruncation = true;
8851 ArithOp = Arith;
8852 }
8853 }
8854 }
8855
8856 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8857 // which may be the result of a CAST. We use the variable 'Op', which is the
8858 // non-casted variable when we check for possible users.
8859 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008860 case ISD::ADD:
8861 // Due to an isel shortcoming, be conservative if this add is likely to be
8862 // selected as part of a load-modify-store instruction. When the root node
8863 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8864 // uses of other nodes in the match, such as the ADD in this case. This
8865 // leads to the ADD being left around and reselected, with the result being
8866 // two adds in the output. Alas, even if none our users are stores, that
8867 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8868 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8869 // climbing the DAG back to the root, and it doesn't seem to be worth the
8870 // effort.
8871 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008872 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8873 if (UI->getOpcode() != ISD::CopyToReg &&
8874 UI->getOpcode() != ISD::SETCC &&
8875 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008876 goto default_case;
8877
8878 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008879 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008880 // An add of one will be selected as an INC.
8881 if (C->getAPIntValue() == 1) {
8882 Opcode = X86ISD::INC;
8883 NumOperands = 1;
8884 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008885 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008886
8887 // An add of negative one (subtract of one) will be selected as a DEC.
8888 if (C->getAPIntValue().isAllOnesValue()) {
8889 Opcode = X86ISD::DEC;
8890 NumOperands = 1;
8891 break;
8892 }
Dan Gohman076aee32009-03-04 19:44:21 +00008893 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008894
8895 // Otherwise use a regular EFLAGS-setting add.
8896 Opcode = X86ISD::ADD;
8897 NumOperands = 2;
8898 break;
8899 case ISD::AND: {
8900 // If the primary and result isn't used, don't bother using X86ISD::AND,
8901 // because a TEST instruction will be better.
8902 bool NonFlagUse = false;
8903 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8904 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8905 SDNode *User = *UI;
8906 unsigned UOpNo = UI.getOperandNo();
8907 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8908 // Look pass truncate.
8909 UOpNo = User->use_begin().getOperandNo();
8910 User = *User->use_begin();
8911 }
8912
8913 if (User->getOpcode() != ISD::BRCOND &&
8914 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008915 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008916 NonFlagUse = true;
8917 break;
8918 }
Dan Gohman076aee32009-03-04 19:44:21 +00008919 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008920
8921 if (!NonFlagUse)
8922 break;
8923 }
8924 // FALL THROUGH
8925 case ISD::SUB:
8926 case ISD::OR:
8927 case ISD::XOR:
8928 // Due to the ISEL shortcoming noted above, be conservative if this op is
8929 // likely to be selected as part of a load-modify-store instruction.
8930 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8931 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8932 if (UI->getOpcode() == ISD::STORE)
8933 goto default_case;
8934
8935 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008936 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008937 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008938 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008939 case ISD::XOR: Opcode = X86ISD::XOR; break;
8940 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008941 case ISD::OR: {
8942 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8943 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8944 if (EFLAGS.getNode())
8945 return EFLAGS;
8946 }
8947 Opcode = X86ISD::OR;
8948 break;
8949 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008950 }
8951
8952 NumOperands = 2;
8953 break;
8954 case X86ISD::ADD:
8955 case X86ISD::SUB:
8956 case X86ISD::INC:
8957 case X86ISD::DEC:
8958 case X86ISD::OR:
8959 case X86ISD::XOR:
8960 case X86ISD::AND:
8961 return SDValue(Op.getNode(), 1);
8962 default:
8963 default_case:
8964 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008965 }
8966
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008967 // If we found that truncation is beneficial, perform the truncation and
8968 // update 'Op'.
8969 if (NeedTruncation) {
8970 EVT VT = Op.getValueType();
8971 SDValue WideVal = Op->getOperand(0);
8972 EVT WideVT = WideVal.getValueType();
8973 unsigned ConvertedOp = 0;
8974 // Use a target machine opcode to prevent further DAGCombine
8975 // optimizations that may separate the arithmetic operations
8976 // from the setcc node.
8977 switch (WideVal.getOpcode()) {
8978 default: break;
8979 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8980 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8981 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8982 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8983 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8984 }
8985
8986 if (ConvertedOp) {
8987 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8988 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8989 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8990 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8991 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8992 }
8993 }
8994 }
8995
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008996 if (Opcode == 0)
8997 // Emit a CMP with 0, which is the TEST pattern.
8998 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8999 DAG.getConstant(0, Op.getValueType()));
9000
9001 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9002 SmallVector<SDValue, 4> Ops;
9003 for (unsigned i = 0; i != NumOperands; ++i)
9004 Ops.push_back(Op.getOperand(i));
9005
9006 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9007 DAG.ReplaceAllUsesWith(Op, New);
9008 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009009}
9010
9011/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9012/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009013SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009014 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9016 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009017 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009018
9019 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009020 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9021 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9022 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9023 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9024 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9025 Op0, Op1);
9026 return SDValue(Sub.getNode(), 1);
9027 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009028 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009029}
9030
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009031/// Convert a comparison if required by the subtarget.
9032SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9033 SelectionDAG &DAG) const {
9034 // If the subtarget does not support the FUCOMI instruction, floating-point
9035 // comparisons have to be converted.
9036 if (Subtarget->hasCMov() ||
9037 Cmp.getOpcode() != X86ISD::CMP ||
9038 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9039 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9040 return Cmp;
9041
9042 // The instruction selector will select an FUCOM instruction instead of
9043 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9044 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9045 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9046 DebugLoc dl = Cmp.getDebugLoc();
9047 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9048 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9049 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9050 DAG.getConstant(8, MVT::i8));
9051 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9052 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9053}
9054
Evan Cheng4e544802012-12-05 00:10:38 +00009055static bool isAllOnes(SDValue V) {
9056 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9057 return C && C->isAllOnesValue();
9058}
9059
Evan Chengd40d03e2010-01-06 19:38:29 +00009060/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9061/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009062SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9063 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009064 SDValue Op0 = And.getOperand(0);
9065 SDValue Op1 = And.getOperand(1);
9066 if (Op0.getOpcode() == ISD::TRUNCATE)
9067 Op0 = Op0.getOperand(0);
9068 if (Op1.getOpcode() == ISD::TRUNCATE)
9069 Op1 = Op1.getOperand(0);
9070
Evan Chengd40d03e2010-01-06 19:38:29 +00009071 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009072 if (Op1.getOpcode() == ISD::SHL)
9073 std::swap(Op0, Op1);
9074 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009075 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9076 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009077 // If we looked past a truncate, check that it's only truncating away
9078 // known zeros.
9079 unsigned BitWidth = Op0.getValueSizeInBits();
9080 unsigned AndBitWidth = And.getValueSizeInBits();
9081 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009082 APInt Zeros, Ones;
9083 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009084 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9085 return SDValue();
9086 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009087 LHS = Op1;
9088 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009089 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009090 } else if (Op1.getOpcode() == ISD::Constant) {
9091 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009092 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009093 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009094
9095 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009096 LHS = AndLHS.getOperand(0);
9097 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009098 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009099
9100 // Use BT if the immediate can't be encoded in a TEST instruction.
9101 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9102 LHS = AndLHS;
9103 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9104 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009105 }
Evan Cheng0488db92007-09-25 01:57:46 +00009106
Evan Chengd40d03e2010-01-06 19:38:29 +00009107 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009108 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9109 // the condition code later.
9110 bool Invert = false;
9111 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9112 Invert = true;
9113 LHS = LHS.getOperand(0);
9114 }
9115
Evan Chenge5b51ac2010-04-17 06:13:15 +00009116 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009117 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009118 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009119 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009120 // Also promote i16 to i32 for performance / code size reason.
9121 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009122 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009123 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009124
Evan Chengd40d03e2010-01-06 19:38:29 +00009125 // If the operand types disagree, extend the shift amount to match. Since
9126 // BT ignores high bits (like shifts) we can use anyextend.
9127 if (LHS.getValueType() != RHS.getValueType())
9128 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009129
Evan Chengd40d03e2010-01-06 19:38:29 +00009130 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009131 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9132 // Flip the condition if the LHS was a not instruction
9133 if (Invert)
9134 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009135 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9136 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009137 }
9138
Evan Cheng54de3ea2010-01-05 06:52:31 +00009139 return SDValue();
9140}
9141
Dan Gohmand858e902010-04-17 15:26:15 +00009142SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00009143
9144 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
9145
Evan Cheng54de3ea2010-01-05 06:52:31 +00009146 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
9147 SDValue Op0 = Op.getOperand(0);
9148 SDValue Op1 = Op.getOperand(1);
9149 DebugLoc dl = Op.getDebugLoc();
9150 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9151
9152 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00009153 // Lower (X & (1 << N)) == 0 to BT(X, N).
9154 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9155 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00009156 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009157 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00009158 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00009159 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9160 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9161 if (NewSetCC.getNode())
9162 return NewSetCC;
9163 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00009164
Chris Lattner481eebc2010-12-19 21:23:48 +00009165 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9166 // these.
9167 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00009168 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00009169 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9170 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009171
Chris Lattner481eebc2010-12-19 21:23:48 +00009172 // If the input is a setcc, then reuse the input setcc or use a new one with
9173 // the inverted condition.
9174 if (Op0.getOpcode() == X86ISD::SETCC) {
9175 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9176 bool Invert = (CC == ISD::SETNE) ^
9177 cast<ConstantSDNode>(Op1)->isNullValue();
9178 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009179
Evan Cheng2c755ba2010-02-27 07:36:59 +00009180 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00009181 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9182 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9183 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009184 }
9185
Evan Chenge5b51ac2010-04-17 06:13:15 +00009186 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00009187 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009188 if (X86CC == X86::COND_INVALID)
9189 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009190
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009191 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009192 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00009193 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00009194 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00009195}
9196
Craig Topper89af15e2011-09-18 08:03:58 +00009197// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009198// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009199static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009200 EVT VT = Op.getValueType();
9201
Craig Topper7a9a28b2012-08-12 02:23:29 +00009202 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009203 "Unsupported value type for operation");
9204
Craig Topper66ddd152012-04-27 22:54:43 +00009205 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009206 DebugLoc dl = Op.getDebugLoc();
9207 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009208
9209 // Extract the LHS vectors
9210 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009211 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9212 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009213
9214 // Extract the RHS vectors
9215 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009216 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9217 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009218
9219 // Issue the operation on the smaller types and concatenate the result back
9220 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9221 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
9222 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9223 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9224 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9225}
9226
Dan Gohmand858e902010-04-17 15:26:15 +00009227SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009228 SDValue Cond;
9229 SDValue Op0 = Op.getOperand(0);
9230 SDValue Op1 = Op.getOperand(1);
9231 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00009232 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00009233 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
9234 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009235 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009236
9237 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009238#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009239 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00009240 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9241#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009242
Craig Topper523908d2012-08-13 02:34:03 +00009243 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009244 bool Swap = false;
9245
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009246 // SSE Condition code mapping:
9247 // 0 - EQ
9248 // 1 - LT
9249 // 2 - LE
9250 // 3 - UNORD
9251 // 4 - NEQ
9252 // 5 - NLT
9253 // 6 - NLE
9254 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009255 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009256 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009257 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009258 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009259 case ISD::SETOGT:
9260 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009261 case ISD::SETLT:
9262 case ISD::SETOLT: SSECC = 1; break;
9263 case ISD::SETOGE:
9264 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009265 case ISD::SETLE:
9266 case ISD::SETOLE: SSECC = 2; break;
9267 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009268 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009269 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009270 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009271 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009272 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009273 case ISD::SETUGT: SSECC = 6; break;
9274 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009275 case ISD::SETUEQ:
9276 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009277 }
9278 if (Swap)
9279 std::swap(Op0, Op1);
9280
Nate Begemanfb8ead02008-07-25 19:05:58 +00009281 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009282 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009283 unsigned CC0, CC1;
9284 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009285 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009286 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9287 } else {
9288 assert(SetCCOpcode == ISD::SETONE);
9289 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009290 }
Craig Topper523908d2012-08-13 02:34:03 +00009291
9292 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9293 DAG.getConstant(CC0, MVT::i8));
9294 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9295 DAG.getConstant(CC1, MVT::i8));
9296 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009297 }
9298 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009299 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9300 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009301 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009302
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009303 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009304 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009305 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009306
Nate Begeman30a0de92008-07-17 16:51:19 +00009307 // We are handling one of the integer comparisons here. Since SSE only has
9308 // GT and EQ comparisons for integer, swapping operands and multiple
9309 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009310 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009311 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009312
Nate Begeman30a0de92008-07-17 16:51:19 +00009313 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009314 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009315 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009316 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009317 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009318 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009319 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009320 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009322 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009323 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009324 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009325 }
9326 if (Swap)
9327 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009328
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009329 // Check that the operation in question is available (most are plain SSE2,
9330 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009331 if (VT == MVT::v2i64) {
9332 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9333 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009334 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9335 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009336 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009337 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9338
9339 // First cast everything to the right type,
9340 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9341 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9342
9343 // Do the compare.
9344 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9345
9346 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009347 const int Mask[] = { 1, 0, 3, 2 };
9348 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9349 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009350
9351 if (Invert)
9352 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9353
9354 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9355 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009356 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009357
Nate Begeman30a0de92008-07-17 16:51:19 +00009358 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9359 // bits of the inputs before performing those operations.
9360 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009361 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009362 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9363 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009364 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009365 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9366 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009367 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9368 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009369 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009370
Dale Johannesenace16102009-02-03 19:33:06 +00009371 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009372
9373 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009374 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009375 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009376
Nate Begeman30a0de92008-07-17 16:51:19 +00009377 return Result;
9378}
Evan Cheng0488db92007-09-25 01:57:46 +00009379
Evan Cheng370e5342008-12-03 08:38:43 +00009380// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009381static bool isX86LogicalCmp(SDValue Op) {
9382 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009383 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9384 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009385 return true;
9386 if (Op.getResNo() == 1 &&
9387 (Opc == X86ISD::ADD ||
9388 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009389 Opc == X86ISD::ADC ||
9390 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009391 Opc == X86ISD::SMUL ||
9392 Opc == X86ISD::UMUL ||
9393 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009394 Opc == X86ISD::DEC ||
9395 Opc == X86ISD::OR ||
9396 Opc == X86ISD::XOR ||
9397 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009398 return true;
9399
Chris Lattner9637d5b2010-12-05 07:49:54 +00009400 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9401 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009402
Dan Gohman076aee32009-03-04 19:44:21 +00009403 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009404}
9405
Chris Lattnera2b56002010-12-05 01:23:24 +00009406static bool isZero(SDValue V) {
9407 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9408 return C && C->isNullValue();
9409}
9410
Evan Chengb64dd5f2012-08-07 22:21:00 +00009411static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9412 if (V.getOpcode() != ISD::TRUNCATE)
9413 return false;
9414
9415 SDValue VOp0 = V.getOperand(0);
9416 unsigned InBits = VOp0.getValueSizeInBits();
9417 unsigned Bits = V.getValueSizeInBits();
9418 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9419}
9420
Dan Gohmand858e902010-04-17 15:26:15 +00009421SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009422 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009423 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009424 SDValue Op1 = Op.getOperand(1);
9425 SDValue Op2 = Op.getOperand(2);
9426 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009427 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009428
Dan Gohman1a492952009-10-20 16:22:37 +00009429 if (Cond.getOpcode() == ISD::SETCC) {
9430 SDValue NewCond = LowerSETCC(Cond, DAG);
9431 if (NewCond.getNode())
9432 Cond = NewCond;
9433 }
Evan Cheng734503b2006-09-11 02:19:56 +00009434
Chris Lattnera2b56002010-12-05 01:23:24 +00009435 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009436 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009437 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009438 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009439 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009440 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9441 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009442 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009443
Chris Lattnera2b56002010-12-05 01:23:24 +00009444 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009445
9446 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009447 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9448 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009449
9450 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009451 // Apply further optimizations for special cases
9452 // (select (x != 0), -1, 0) -> neg & sbb
9453 // (select (x == 0), 0, -1) -> neg & sbb
9454 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009455 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009456 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9457 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009458 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9459 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009460 CmpOp0);
9461 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9462 DAG.getConstant(X86::COND_B, MVT::i8),
9463 SDValue(Neg.getNode(), 1));
9464 return Res;
9465 }
9466
Chris Lattnera2b56002010-12-05 01:23:24 +00009467 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9468 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009469 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009470
Chris Lattner96908b12010-12-05 02:00:51 +00009471 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009472 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9473 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009474
Chris Lattner96908b12010-12-05 02:00:51 +00009475 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9476 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009477
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009478 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009479 if (N2C == 0 || !N2C->isNullValue())
9480 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9481 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009482 }
9483 }
9484
Chris Lattnera2b56002010-12-05 01:23:24 +00009485 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009486 if (Cond.getOpcode() == ISD::AND &&
9487 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9488 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009489 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009490 Cond = Cond.getOperand(0);
9491 }
9492
Evan Cheng3f41d662007-10-08 22:16:29 +00009493 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9494 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009495 unsigned CondOpcode = Cond.getOpcode();
9496 if (CondOpcode == X86ISD::SETCC ||
9497 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009498 CC = Cond.getOperand(0);
9499
Dan Gohman475871a2008-07-27 21:46:04 +00009500 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009501 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009502 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009503
Evan Cheng3f41d662007-10-08 22:16:29 +00009504 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009505 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009506 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009507 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009508
Chris Lattnerd1980a52009-03-12 06:52:53 +00009509 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9510 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009511 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009512 addTest = false;
9513 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009514 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9515 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9516 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9517 Cond.getOperand(0).getValueType() != MVT::i8)) {
9518 SDValue LHS = Cond.getOperand(0);
9519 SDValue RHS = Cond.getOperand(1);
9520 unsigned X86Opcode;
9521 unsigned X86Cond;
9522 SDVTList VTs;
9523 switch (CondOpcode) {
9524 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9525 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9526 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9527 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9528 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9529 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9530 default: llvm_unreachable("unexpected overflowing operator");
9531 }
9532 if (CondOpcode == ISD::UMULO)
9533 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9534 MVT::i32);
9535 else
9536 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9537
9538 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9539
9540 if (CondOpcode == ISD::UMULO)
9541 Cond = X86Op.getValue(2);
9542 else
9543 Cond = X86Op.getValue(1);
9544
9545 CC = DAG.getConstant(X86Cond, MVT::i8);
9546 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009547 }
9548
9549 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009550 // Look pass the truncate if the high bits are known zero.
9551 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9552 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009553
9554 // We know the result of AND is compared against zero. Try to match
9555 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009556 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009557 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009558 if (NewSetCC.getNode()) {
9559 CC = NewSetCC.getOperand(0);
9560 Cond = NewSetCC.getOperand(1);
9561 addTest = false;
9562 }
9563 }
9564 }
9565
9566 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009567 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009568 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009569 }
9570
Benjamin Kramere915ff32010-12-22 23:09:28 +00009571 // a < b ? -1 : 0 -> RES = ~setcc_carry
9572 // a < b ? 0 : -1 -> RES = setcc_carry
9573 // a >= b ? -1 : 0 -> RES = setcc_carry
9574 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009575 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009576 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009577 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9578
9579 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9580 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9581 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9582 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9583 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9584 return DAG.getNOT(DL, Res, Res.getValueType());
9585 return Res;
9586 }
9587 }
9588
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009589 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9590 // widen the cmov and push the truncate through. This avoids introducing a new
9591 // branch during isel and doesn't add any extensions.
9592 if (Op.getValueType() == MVT::i8 &&
9593 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9594 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9595 if (T1.getValueType() == T2.getValueType() &&
9596 // Blacklist CopyFromReg to avoid partial register stalls.
9597 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9598 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009599 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009600 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9601 }
9602 }
9603
Evan Cheng0488db92007-09-25 01:57:46 +00009604 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9605 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009606 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009607 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009608 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009609}
9610
Nadav Rotem1a330af2012-12-27 22:47:16 +00009611SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9612 SelectionDAG &DAG) const {
9613 EVT VT = Op->getValueType(0);
9614 SDValue In = Op->getOperand(0);
9615 EVT InVT = In.getValueType();
9616 DebugLoc dl = Op->getDebugLoc();
9617
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009618 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9619 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9620 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009621
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009622 if (Subtarget->hasInt256())
9623 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009624
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009625 // Optimize vectors in AVX mode
9626 // Sign extend v8i16 to v8i32 and
9627 // v4i32 to v4i64
9628 //
9629 // Divide input vector into two parts
9630 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9631 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9632 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009633
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009634 unsigned NumElems = InVT.getVectorNumElements();
9635 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009636
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009637 SmallVector<int,8> ShufMask1(NumElems, -1);
9638 for (unsigned i = 0; i != NumElems/2; ++i)
9639 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009640
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009641 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009642
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009643 SmallVector<int,8> ShufMask2(NumElems, -1);
9644 for (unsigned i = 0; i != NumElems/2; ++i)
9645 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009646
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009647 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009648
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009649 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
9650 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009651
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009652 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9653 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009654
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009655 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009656}
9657
Evan Cheng370e5342008-12-03 08:38:43 +00009658// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9659// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9660// from the AND / OR.
9661static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9662 Opc = Op.getOpcode();
9663 if (Opc != ISD::OR && Opc != ISD::AND)
9664 return false;
9665 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9666 Op.getOperand(0).hasOneUse() &&
9667 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9668 Op.getOperand(1).hasOneUse());
9669}
9670
Evan Cheng961d6d42009-02-02 08:19:07 +00009671// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9672// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009673static bool isXor1OfSetCC(SDValue Op) {
9674 if (Op.getOpcode() != ISD::XOR)
9675 return false;
9676 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9677 if (N1C && N1C->getAPIntValue() == 1) {
9678 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9679 Op.getOperand(0).hasOneUse();
9680 }
9681 return false;
9682}
9683
Dan Gohmand858e902010-04-17 15:26:15 +00009684SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009685 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009686 SDValue Chain = Op.getOperand(0);
9687 SDValue Cond = Op.getOperand(1);
9688 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009689 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009690 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009691 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009692
Dan Gohman1a492952009-10-20 16:22:37 +00009693 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009694 // Check for setcc([su]{add,sub,mul}o == 0).
9695 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9696 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9697 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9698 Cond.getOperand(0).getResNo() == 1 &&
9699 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9700 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9701 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9702 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9703 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9704 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9705 Inverted = true;
9706 Cond = Cond.getOperand(0);
9707 } else {
9708 SDValue NewCond = LowerSETCC(Cond, DAG);
9709 if (NewCond.getNode())
9710 Cond = NewCond;
9711 }
Dan Gohman1a492952009-10-20 16:22:37 +00009712 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009713#if 0
9714 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009715 else if (Cond.getOpcode() == X86ISD::ADD ||
9716 Cond.getOpcode() == X86ISD::SUB ||
9717 Cond.getOpcode() == X86ISD::SMUL ||
9718 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009719 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009720#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009721
Evan Chengad9c0a32009-12-15 00:53:42 +00009722 // Look pass (and (setcc_carry (cmp ...)), 1).
9723 if (Cond.getOpcode() == ISD::AND &&
9724 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9725 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009726 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009727 Cond = Cond.getOperand(0);
9728 }
9729
Evan Cheng3f41d662007-10-08 22:16:29 +00009730 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9731 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009732 unsigned CondOpcode = Cond.getOpcode();
9733 if (CondOpcode == X86ISD::SETCC ||
9734 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009735 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009736
Dan Gohman475871a2008-07-27 21:46:04 +00009737 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009738 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009739 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009740 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009741 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009742 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009743 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009744 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009745 default: break;
9746 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009747 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009748 // These can only come from an arithmetic instruction with overflow,
9749 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009750 Cond = Cond.getNode()->getOperand(1);
9751 addTest = false;
9752 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009753 }
Evan Cheng0488db92007-09-25 01:57:46 +00009754 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009755 }
9756 CondOpcode = Cond.getOpcode();
9757 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9758 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9759 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9760 Cond.getOperand(0).getValueType() != MVT::i8)) {
9761 SDValue LHS = Cond.getOperand(0);
9762 SDValue RHS = Cond.getOperand(1);
9763 unsigned X86Opcode;
9764 unsigned X86Cond;
9765 SDVTList VTs;
9766 switch (CondOpcode) {
9767 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9768 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9769 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9770 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9771 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9772 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9773 default: llvm_unreachable("unexpected overflowing operator");
9774 }
9775 if (Inverted)
9776 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9777 if (CondOpcode == ISD::UMULO)
9778 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9779 MVT::i32);
9780 else
9781 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9782
9783 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9784
9785 if (CondOpcode == ISD::UMULO)
9786 Cond = X86Op.getValue(2);
9787 else
9788 Cond = X86Op.getValue(1);
9789
9790 CC = DAG.getConstant(X86Cond, MVT::i8);
9791 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009792 } else {
9793 unsigned CondOpc;
9794 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9795 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009796 if (CondOpc == ISD::OR) {
9797 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9798 // two branches instead of an explicit OR instruction with a
9799 // separate test.
9800 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009801 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009802 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009803 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009804 Chain, Dest, CC, Cmp);
9805 CC = Cond.getOperand(1).getOperand(0);
9806 Cond = Cmp;
9807 addTest = false;
9808 }
9809 } else { // ISD::AND
9810 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9811 // two branches instead of an explicit AND instruction with a
9812 // separate test. However, we only do this if this block doesn't
9813 // have a fall-through edge, because this requires an explicit
9814 // jmp when the condition is false.
9815 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009816 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009817 Op.getNode()->hasOneUse()) {
9818 X86::CondCode CCode =
9819 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9820 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009821 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009822 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009823 // Look for an unconditional branch following this conditional branch.
9824 // We need this because we need to reverse the successors in order
9825 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009826 if (User->getOpcode() == ISD::BR) {
9827 SDValue FalseBB = User->getOperand(1);
9828 SDNode *NewBR =
9829 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009830 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009831 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009832 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009833
Dale Johannesene4d209d2009-02-03 20:21:25 +00009834 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009835 Chain, Dest, CC, Cmp);
9836 X86::CondCode CCode =
9837 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9838 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009839 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009840 Cond = Cmp;
9841 addTest = false;
9842 }
9843 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009844 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009845 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9846 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9847 // It should be transformed during dag combiner except when the condition
9848 // is set by a arithmetics with overflow node.
9849 X86::CondCode CCode =
9850 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9851 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009852 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009853 Cond = Cond.getOperand(0).getOperand(1);
9854 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009855 } else if (Cond.getOpcode() == ISD::SETCC &&
9856 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9857 // For FCMP_OEQ, we can emit
9858 // two branches instead of an explicit AND instruction with a
9859 // separate test. However, we only do this if this block doesn't
9860 // have a fall-through edge, because this requires an explicit
9861 // jmp when the condition is false.
9862 if (Op.getNode()->hasOneUse()) {
9863 SDNode *User = *Op.getNode()->use_begin();
9864 // Look for an unconditional branch following this conditional branch.
9865 // We need this because we need to reverse the successors in order
9866 // to implement FCMP_OEQ.
9867 if (User->getOpcode() == ISD::BR) {
9868 SDValue FalseBB = User->getOperand(1);
9869 SDNode *NewBR =
9870 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9871 assert(NewBR == User);
9872 (void)NewBR;
9873 Dest = FalseBB;
9874
9875 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9876 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009877 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009878 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9879 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9880 Chain, Dest, CC, Cmp);
9881 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9882 Cond = Cmp;
9883 addTest = false;
9884 }
9885 }
9886 } else if (Cond.getOpcode() == ISD::SETCC &&
9887 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9888 // For FCMP_UNE, we can emit
9889 // two branches instead of an explicit AND instruction with a
9890 // separate test. However, we only do this if this block doesn't
9891 // have a fall-through edge, because this requires an explicit
9892 // jmp when the condition is false.
9893 if (Op.getNode()->hasOneUse()) {
9894 SDNode *User = *Op.getNode()->use_begin();
9895 // Look for an unconditional branch following this conditional branch.
9896 // We need this because we need to reverse the successors in order
9897 // to implement FCMP_UNE.
9898 if (User->getOpcode() == ISD::BR) {
9899 SDValue FalseBB = User->getOperand(1);
9900 SDNode *NewBR =
9901 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9902 assert(NewBR == User);
9903 (void)NewBR;
9904
9905 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9906 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009907 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009908 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9909 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9910 Chain, Dest, CC, Cmp);
9911 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9912 Cond = Cmp;
9913 addTest = false;
9914 Dest = FalseBB;
9915 }
9916 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009917 }
Evan Cheng0488db92007-09-25 01:57:46 +00009918 }
9919
9920 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009921 // Look pass the truncate if the high bits are known zero.
9922 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9923 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009924
9925 // We know the result of AND is compared against zero. Try to match
9926 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009927 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009928 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9929 if (NewSetCC.getNode()) {
9930 CC = NewSetCC.getOperand(0);
9931 Cond = NewSetCC.getOperand(1);
9932 addTest = false;
9933 }
9934 }
9935 }
9936
9937 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009938 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009939 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009940 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009941 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009942 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009943 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009944}
9945
Anton Korobeynikove060b532007-04-17 19:34:00 +00009946// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9947// Calls to _alloca is needed to probe the stack when allocating more than 4k
9948// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9949// that the guard pages used by the OS virtual memory manager are allocated in
9950// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009951SDValue
9952X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009953 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009954 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009955 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009956 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009957 "are being used");
9958 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009959 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009960
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009961 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009962 SDValue Chain = Op.getOperand(0);
9963 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009964 // FIXME: Ensure alignment here
9965
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009966 bool Is64Bit = Subtarget->is64Bit();
9967 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009968
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009969 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009970 MachineFunction &MF = DAG.getMachineFunction();
9971 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009972
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009973 if (Is64Bit) {
9974 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009975 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009976 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009977
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009978 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009979 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009980 if (I->hasNestAttr())
9981 report_fatal_error("Cannot use segmented stacks with functions that "
9982 "have nested arguments.");
9983 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009984
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009985 const TargetRegisterClass *AddrRegClass =
9986 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9987 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9988 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9989 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9990 DAG.getRegister(Vreg, SPTy));
9991 SDValue Ops1[2] = { Value, Chain };
9992 return DAG.getMergeValues(Ops1, 2, dl);
9993 } else {
9994 SDValue Flag;
9995 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009996
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009997 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9998 Flag = Chain.getValue(1);
9999 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010000
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010001 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10002 Flag = Chain.getValue(1);
10003
Michael Liaoc5c970e2012-10-31 04:14:09 +000010004 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10005 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010006
10007 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10008 return DAG.getMergeValues(Ops1, 2, dl);
10009 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010010}
10011
Dan Gohmand858e902010-04-17 15:26:15 +000010012SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010013 MachineFunction &MF = DAG.getMachineFunction();
10014 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10015
Dan Gohman69de1932008-02-06 22:27:42 +000010016 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010017 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010018
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010019 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010020 // vastart just stores the address of the VarArgsFrameIndex slot into the
10021 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010022 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10023 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010024 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10025 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010026 }
10027
10028 // __va_list_tag:
10029 // gp_offset (0 - 6 * 8)
10030 // fp_offset (48 - 48 + 8 * 16)
10031 // overflow_arg_area (point to parameters coming in memory).
10032 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010033 SmallVector<SDValue, 8> MemOps;
10034 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010035 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010036 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010037 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10038 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010039 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010040 MemOps.push_back(Store);
10041
10042 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010043 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010044 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010045 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010046 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10047 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010048 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010049 MemOps.push_back(Store);
10050
10051 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010052 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010053 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010054 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10055 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010056 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10057 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010058 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010059 MemOps.push_back(Store);
10060
10061 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010062 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010063 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010064 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10065 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010066 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10067 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010068 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010069 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010070 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010071}
10072
Dan Gohmand858e902010-04-17 15:26:15 +000010073SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010074 assert(Subtarget->is64Bit() &&
10075 "LowerVAARG only handles 64-bit va_arg!");
10076 assert((Subtarget->isTargetLinux() ||
10077 Subtarget->isTargetDarwin()) &&
10078 "Unhandled target in LowerVAARG");
10079 assert(Op.getNode()->getNumOperands() == 4);
10080 SDValue Chain = Op.getOperand(0);
10081 SDValue SrcPtr = Op.getOperand(1);
10082 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10083 unsigned Align = Op.getConstantOperandVal(3);
10084 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010085
Dan Gohman320afb82010-10-12 18:00:49 +000010086 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010087 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010088 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010089 uint8_t ArgMode;
10090
10091 // Decide which area this value should be read from.
10092 // TODO: Implement the AMD64 ABI in its entirety. This simple
10093 // selection mechanism works only for the basic types.
10094 if (ArgVT == MVT::f80) {
10095 llvm_unreachable("va_arg for f80 not yet implemented");
10096 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10097 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10098 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10099 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10100 } else {
10101 llvm_unreachable("Unhandled argument type in LowerVAARG");
10102 }
10103
10104 if (ArgMode == 2) {
10105 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010106 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010107 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010108 .getFunction()->getAttributes()
10109 .hasAttribute(AttributeSet::FunctionIndex,
10110 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010111 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010112 }
10113
10114 // Insert VAARG_64 node into the DAG
10115 // VAARG_64 returns two values: Variable Argument Address, Chain
10116 SmallVector<SDValue, 11> InstOps;
10117 InstOps.push_back(Chain);
10118 InstOps.push_back(SrcPtr);
10119 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10120 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10121 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10122 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10123 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10124 VTs, &InstOps[0], InstOps.size(),
10125 MVT::i64,
10126 MachinePointerInfo(SV),
10127 /*Align=*/0,
10128 /*Volatile=*/false,
10129 /*ReadMem=*/true,
10130 /*WriteMem=*/true);
10131 Chain = VAARG.getValue(1);
10132
10133 // Load the next argument and return it
10134 return DAG.getLoad(ArgVT, dl,
10135 Chain,
10136 VAARG,
10137 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010138 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010139}
10140
Craig Topper55b24052012-09-11 06:15:32 +000010141static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10142 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010143 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010144 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010145 SDValue Chain = Op.getOperand(0);
10146 SDValue DstPtr = Op.getOperand(1);
10147 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010148 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10149 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010150 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010151
Chris Lattnere72f2022010-09-21 05:40:29 +000010152 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010153 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010154 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010155 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010156}
10157
Craig Topper80e46362012-01-23 06:16:53 +000010158// getTargetVShiftNOde - Handle vector element shifts where the shift amount
10159// may or may not be a constant. Takes immediate version of shift as input.
10160static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10161 SDValue SrcOp, SDValue ShAmt,
10162 SelectionDAG &DAG) {
10163 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10164
10165 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010166 // Constant may be a TargetConstant. Use a regular constant.
10167 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010168 switch (Opc) {
10169 default: llvm_unreachable("Unknown target vector shift node");
10170 case X86ISD::VSHLI:
10171 case X86ISD::VSRLI:
10172 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010173 return DAG.getNode(Opc, dl, VT, SrcOp,
10174 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010175 }
10176 }
10177
10178 // Change opcode to non-immediate version
10179 switch (Opc) {
10180 default: llvm_unreachable("Unknown target vector shift node");
10181 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10182 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10183 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10184 }
10185
10186 // Need to build a vector containing shift amount
10187 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10188 SDValue ShOps[4];
10189 ShOps[0] = ShAmt;
10190 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010191 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010192 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010193
10194 // The return type has to be a 128-bit type with the same element
10195 // type as the input type.
10196 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10197 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10198
10199 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010200 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10201}
10202
Craig Topper55b24052012-09-11 06:15:32 +000010203static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010204 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010205 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010206 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010207 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010208 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010209 case Intrinsic::x86_sse_comieq_ss:
10210 case Intrinsic::x86_sse_comilt_ss:
10211 case Intrinsic::x86_sse_comile_ss:
10212 case Intrinsic::x86_sse_comigt_ss:
10213 case Intrinsic::x86_sse_comige_ss:
10214 case Intrinsic::x86_sse_comineq_ss:
10215 case Intrinsic::x86_sse_ucomieq_ss:
10216 case Intrinsic::x86_sse_ucomilt_ss:
10217 case Intrinsic::x86_sse_ucomile_ss:
10218 case Intrinsic::x86_sse_ucomigt_ss:
10219 case Intrinsic::x86_sse_ucomige_ss:
10220 case Intrinsic::x86_sse_ucomineq_ss:
10221 case Intrinsic::x86_sse2_comieq_sd:
10222 case Intrinsic::x86_sse2_comilt_sd:
10223 case Intrinsic::x86_sse2_comile_sd:
10224 case Intrinsic::x86_sse2_comigt_sd:
10225 case Intrinsic::x86_sse2_comige_sd:
10226 case Intrinsic::x86_sse2_comineq_sd:
10227 case Intrinsic::x86_sse2_ucomieq_sd:
10228 case Intrinsic::x86_sse2_ucomilt_sd:
10229 case Intrinsic::x86_sse2_ucomile_sd:
10230 case Intrinsic::x86_sse2_ucomigt_sd:
10231 case Intrinsic::x86_sse2_ucomige_sd:
10232 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010233 unsigned Opc;
10234 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010235 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010236 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010237 case Intrinsic::x86_sse_comieq_ss:
10238 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010239 Opc = X86ISD::COMI;
10240 CC = ISD::SETEQ;
10241 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010242 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010243 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010244 Opc = X86ISD::COMI;
10245 CC = ISD::SETLT;
10246 break;
10247 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010248 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010249 Opc = X86ISD::COMI;
10250 CC = ISD::SETLE;
10251 break;
10252 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010253 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010254 Opc = X86ISD::COMI;
10255 CC = ISD::SETGT;
10256 break;
10257 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010258 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010259 Opc = X86ISD::COMI;
10260 CC = ISD::SETGE;
10261 break;
10262 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010263 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010264 Opc = X86ISD::COMI;
10265 CC = ISD::SETNE;
10266 break;
10267 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010268 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010269 Opc = X86ISD::UCOMI;
10270 CC = ISD::SETEQ;
10271 break;
10272 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010273 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010274 Opc = X86ISD::UCOMI;
10275 CC = ISD::SETLT;
10276 break;
10277 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010278 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010279 Opc = X86ISD::UCOMI;
10280 CC = ISD::SETLE;
10281 break;
10282 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010283 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010284 Opc = X86ISD::UCOMI;
10285 CC = ISD::SETGT;
10286 break;
10287 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010288 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010289 Opc = X86ISD::UCOMI;
10290 CC = ISD::SETGE;
10291 break;
10292 case Intrinsic::x86_sse_ucomineq_ss:
10293 case Intrinsic::x86_sse2_ucomineq_sd:
10294 Opc = X86ISD::UCOMI;
10295 CC = ISD::SETNE;
10296 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010297 }
Evan Cheng734503b2006-09-11 02:19:56 +000010298
Dan Gohman475871a2008-07-27 21:46:04 +000010299 SDValue LHS = Op.getOperand(1);
10300 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010301 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010302 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010303 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10304 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10305 DAG.getConstant(X86CC, MVT::i8), Cond);
10306 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010307 }
Craig Topper6d688152012-08-14 07:43:25 +000010308
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010309 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010310 case Intrinsic::x86_sse2_pmulu_dq:
10311 case Intrinsic::x86_avx2_pmulu_dq:
10312 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10313 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010314
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010315 // SSE2/AVX2 sub with unsigned saturation intrinsics
10316 case Intrinsic::x86_sse2_psubus_b:
10317 case Intrinsic::x86_sse2_psubus_w:
10318 case Intrinsic::x86_avx2_psubus_b:
10319 case Intrinsic::x86_avx2_psubus_w:
10320 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10321 Op.getOperand(1), Op.getOperand(2));
10322
Craig Topper6d688152012-08-14 07:43:25 +000010323 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010324 case Intrinsic::x86_sse3_hadd_ps:
10325 case Intrinsic::x86_sse3_hadd_pd:
10326 case Intrinsic::x86_avx_hadd_ps_256:
10327 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010328 case Intrinsic::x86_sse3_hsub_ps:
10329 case Intrinsic::x86_sse3_hsub_pd:
10330 case Intrinsic::x86_avx_hsub_ps_256:
10331 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010332 case Intrinsic::x86_ssse3_phadd_w_128:
10333 case Intrinsic::x86_ssse3_phadd_d_128:
10334 case Intrinsic::x86_avx2_phadd_w:
10335 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010336 case Intrinsic::x86_ssse3_phsub_w_128:
10337 case Intrinsic::x86_ssse3_phsub_d_128:
10338 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010339 case Intrinsic::x86_avx2_phsub_d: {
10340 unsigned Opcode;
10341 switch (IntNo) {
10342 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10343 case Intrinsic::x86_sse3_hadd_ps:
10344 case Intrinsic::x86_sse3_hadd_pd:
10345 case Intrinsic::x86_avx_hadd_ps_256:
10346 case Intrinsic::x86_avx_hadd_pd_256:
10347 Opcode = X86ISD::FHADD;
10348 break;
10349 case Intrinsic::x86_sse3_hsub_ps:
10350 case Intrinsic::x86_sse3_hsub_pd:
10351 case Intrinsic::x86_avx_hsub_ps_256:
10352 case Intrinsic::x86_avx_hsub_pd_256:
10353 Opcode = X86ISD::FHSUB;
10354 break;
10355 case Intrinsic::x86_ssse3_phadd_w_128:
10356 case Intrinsic::x86_ssse3_phadd_d_128:
10357 case Intrinsic::x86_avx2_phadd_w:
10358 case Intrinsic::x86_avx2_phadd_d:
10359 Opcode = X86ISD::HADD;
10360 break;
10361 case Intrinsic::x86_ssse3_phsub_w_128:
10362 case Intrinsic::x86_ssse3_phsub_d_128:
10363 case Intrinsic::x86_avx2_phsub_w:
10364 case Intrinsic::x86_avx2_phsub_d:
10365 Opcode = X86ISD::HSUB;
10366 break;
10367 }
10368 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010369 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010370 }
10371
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010372 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10373 case Intrinsic::x86_sse2_pmaxu_b:
10374 case Intrinsic::x86_sse41_pmaxuw:
10375 case Intrinsic::x86_sse41_pmaxud:
10376 case Intrinsic::x86_avx2_pmaxu_b:
10377 case Intrinsic::x86_avx2_pmaxu_w:
10378 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010379 case Intrinsic::x86_sse2_pminu_b:
10380 case Intrinsic::x86_sse41_pminuw:
10381 case Intrinsic::x86_sse41_pminud:
10382 case Intrinsic::x86_avx2_pminu_b:
10383 case Intrinsic::x86_avx2_pminu_w:
10384 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010385 case Intrinsic::x86_sse41_pmaxsb:
10386 case Intrinsic::x86_sse2_pmaxs_w:
10387 case Intrinsic::x86_sse41_pmaxsd:
10388 case Intrinsic::x86_avx2_pmaxs_b:
10389 case Intrinsic::x86_avx2_pmaxs_w:
10390 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010391 case Intrinsic::x86_sse41_pminsb:
10392 case Intrinsic::x86_sse2_pmins_w:
10393 case Intrinsic::x86_sse41_pminsd:
10394 case Intrinsic::x86_avx2_pmins_b:
10395 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010396 case Intrinsic::x86_avx2_pmins_d: {
10397 unsigned Opcode;
10398 switch (IntNo) {
10399 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10400 case Intrinsic::x86_sse2_pmaxu_b:
10401 case Intrinsic::x86_sse41_pmaxuw:
10402 case Intrinsic::x86_sse41_pmaxud:
10403 case Intrinsic::x86_avx2_pmaxu_b:
10404 case Intrinsic::x86_avx2_pmaxu_w:
10405 case Intrinsic::x86_avx2_pmaxu_d:
10406 Opcode = X86ISD::UMAX;
10407 break;
10408 case Intrinsic::x86_sse2_pminu_b:
10409 case Intrinsic::x86_sse41_pminuw:
10410 case Intrinsic::x86_sse41_pminud:
10411 case Intrinsic::x86_avx2_pminu_b:
10412 case Intrinsic::x86_avx2_pminu_w:
10413 case Intrinsic::x86_avx2_pminu_d:
10414 Opcode = X86ISD::UMIN;
10415 break;
10416 case Intrinsic::x86_sse41_pmaxsb:
10417 case Intrinsic::x86_sse2_pmaxs_w:
10418 case Intrinsic::x86_sse41_pmaxsd:
10419 case Intrinsic::x86_avx2_pmaxs_b:
10420 case Intrinsic::x86_avx2_pmaxs_w:
10421 case Intrinsic::x86_avx2_pmaxs_d:
10422 Opcode = X86ISD::SMAX;
10423 break;
10424 case Intrinsic::x86_sse41_pminsb:
10425 case Intrinsic::x86_sse2_pmins_w:
10426 case Intrinsic::x86_sse41_pminsd:
10427 case Intrinsic::x86_avx2_pmins_b:
10428 case Intrinsic::x86_avx2_pmins_w:
10429 case Intrinsic::x86_avx2_pmins_d:
10430 Opcode = X86ISD::SMIN;
10431 break;
10432 }
10433 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010434 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010435 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010436
Craig Topper6d183e42012-12-29 16:44:25 +000010437 // SSE/SSE2/AVX floating point max/min intrinsics.
10438 case Intrinsic::x86_sse_max_ps:
10439 case Intrinsic::x86_sse2_max_pd:
10440 case Intrinsic::x86_avx_max_ps_256:
10441 case Intrinsic::x86_avx_max_pd_256:
10442 case Intrinsic::x86_sse_min_ps:
10443 case Intrinsic::x86_sse2_min_pd:
10444 case Intrinsic::x86_avx_min_ps_256:
10445 case Intrinsic::x86_avx_min_pd_256: {
10446 unsigned Opcode;
10447 switch (IntNo) {
10448 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10449 case Intrinsic::x86_sse_max_ps:
10450 case Intrinsic::x86_sse2_max_pd:
10451 case Intrinsic::x86_avx_max_ps_256:
10452 case Intrinsic::x86_avx_max_pd_256:
10453 Opcode = X86ISD::FMAX;
10454 break;
10455 case Intrinsic::x86_sse_min_ps:
10456 case Intrinsic::x86_sse2_min_pd:
10457 case Intrinsic::x86_avx_min_ps_256:
10458 case Intrinsic::x86_avx_min_pd_256:
10459 Opcode = X86ISD::FMIN;
10460 break;
10461 }
10462 return DAG.getNode(Opcode, dl, Op.getValueType(),
10463 Op.getOperand(1), Op.getOperand(2));
10464 }
10465
Craig Topper6d688152012-08-14 07:43:25 +000010466 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010467 case Intrinsic::x86_avx2_psllv_d:
10468 case Intrinsic::x86_avx2_psllv_q:
10469 case Intrinsic::x86_avx2_psllv_d_256:
10470 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010471 case Intrinsic::x86_avx2_psrlv_d:
10472 case Intrinsic::x86_avx2_psrlv_q:
10473 case Intrinsic::x86_avx2_psrlv_d_256:
10474 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010475 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010476 case Intrinsic::x86_avx2_psrav_d_256: {
10477 unsigned Opcode;
10478 switch (IntNo) {
10479 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10480 case Intrinsic::x86_avx2_psllv_d:
10481 case Intrinsic::x86_avx2_psllv_q:
10482 case Intrinsic::x86_avx2_psllv_d_256:
10483 case Intrinsic::x86_avx2_psllv_q_256:
10484 Opcode = ISD::SHL;
10485 break;
10486 case Intrinsic::x86_avx2_psrlv_d:
10487 case Intrinsic::x86_avx2_psrlv_q:
10488 case Intrinsic::x86_avx2_psrlv_d_256:
10489 case Intrinsic::x86_avx2_psrlv_q_256:
10490 Opcode = ISD::SRL;
10491 break;
10492 case Intrinsic::x86_avx2_psrav_d:
10493 case Intrinsic::x86_avx2_psrav_d_256:
10494 Opcode = ISD::SRA;
10495 break;
10496 }
10497 return DAG.getNode(Opcode, dl, Op.getValueType(),
10498 Op.getOperand(1), Op.getOperand(2));
10499 }
10500
Craig Topper969ba282012-01-25 06:43:11 +000010501 case Intrinsic::x86_ssse3_pshuf_b_128:
10502 case Intrinsic::x86_avx2_pshuf_b:
10503 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10504 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010505
Craig Topper969ba282012-01-25 06:43:11 +000010506 case Intrinsic::x86_ssse3_psign_b_128:
10507 case Intrinsic::x86_ssse3_psign_w_128:
10508 case Intrinsic::x86_ssse3_psign_d_128:
10509 case Intrinsic::x86_avx2_psign_b:
10510 case Intrinsic::x86_avx2_psign_w:
10511 case Intrinsic::x86_avx2_psign_d:
10512 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10513 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010514
Craig Toppere566cd02012-01-26 07:18:03 +000010515 case Intrinsic::x86_sse41_insertps:
10516 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10517 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010518
Craig Toppere566cd02012-01-26 07:18:03 +000010519 case Intrinsic::x86_avx_vperm2f128_ps_256:
10520 case Intrinsic::x86_avx_vperm2f128_pd_256:
10521 case Intrinsic::x86_avx_vperm2f128_si_256:
10522 case Intrinsic::x86_avx2_vperm2i128:
10523 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10524 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010525
Craig Topperffa6c402012-04-16 07:13:00 +000010526 case Intrinsic::x86_avx2_permd:
10527 case Intrinsic::x86_avx2_permps:
10528 // Operands intentionally swapped. Mask is last operand to intrinsic,
10529 // but second operand for node/intruction.
10530 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10531 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010532
Craig Topper22d8f0d2012-12-29 18:18:20 +000010533 case Intrinsic::x86_sse_sqrt_ps:
10534 case Intrinsic::x86_sse2_sqrt_pd:
10535 case Intrinsic::x86_avx_sqrt_ps_256:
10536 case Intrinsic::x86_avx_sqrt_pd_256:
10537 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10538
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010539 // ptest and testp intrinsics. The intrinsic these come from are designed to
10540 // return an integer value, not just an instruction so lower it to the ptest
10541 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010542 case Intrinsic::x86_sse41_ptestz:
10543 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010544 case Intrinsic::x86_sse41_ptestnzc:
10545 case Intrinsic::x86_avx_ptestz_256:
10546 case Intrinsic::x86_avx_ptestc_256:
10547 case Intrinsic::x86_avx_ptestnzc_256:
10548 case Intrinsic::x86_avx_vtestz_ps:
10549 case Intrinsic::x86_avx_vtestc_ps:
10550 case Intrinsic::x86_avx_vtestnzc_ps:
10551 case Intrinsic::x86_avx_vtestz_pd:
10552 case Intrinsic::x86_avx_vtestc_pd:
10553 case Intrinsic::x86_avx_vtestnzc_pd:
10554 case Intrinsic::x86_avx_vtestz_ps_256:
10555 case Intrinsic::x86_avx_vtestc_ps_256:
10556 case Intrinsic::x86_avx_vtestnzc_ps_256:
10557 case Intrinsic::x86_avx_vtestz_pd_256:
10558 case Intrinsic::x86_avx_vtestc_pd_256:
10559 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10560 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010561 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010562 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010563 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010564 case Intrinsic::x86_avx_vtestz_ps:
10565 case Intrinsic::x86_avx_vtestz_pd:
10566 case Intrinsic::x86_avx_vtestz_ps_256:
10567 case Intrinsic::x86_avx_vtestz_pd_256:
10568 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010569 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010570 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010571 // ZF = 1
10572 X86CC = X86::COND_E;
10573 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010574 case Intrinsic::x86_avx_vtestc_ps:
10575 case Intrinsic::x86_avx_vtestc_pd:
10576 case Intrinsic::x86_avx_vtestc_ps_256:
10577 case Intrinsic::x86_avx_vtestc_pd_256:
10578 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010579 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010580 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010581 // CF = 1
10582 X86CC = X86::COND_B;
10583 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010584 case Intrinsic::x86_avx_vtestnzc_ps:
10585 case Intrinsic::x86_avx_vtestnzc_pd:
10586 case Intrinsic::x86_avx_vtestnzc_ps_256:
10587 case Intrinsic::x86_avx_vtestnzc_pd_256:
10588 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010589 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010590 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010591 // ZF and CF = 0
10592 X86CC = X86::COND_A;
10593 break;
10594 }
Eric Christopherfd179292009-08-27 18:07:15 +000010595
Eric Christopher71c67532009-07-29 00:28:05 +000010596 SDValue LHS = Op.getOperand(1);
10597 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010598 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10599 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010600 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10601 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10602 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010603 }
Evan Cheng5759f972008-05-04 09:15:50 +000010604
Craig Topper80e46362012-01-23 06:16:53 +000010605 // SSE/AVX shift intrinsics
10606 case Intrinsic::x86_sse2_psll_w:
10607 case Intrinsic::x86_sse2_psll_d:
10608 case Intrinsic::x86_sse2_psll_q:
10609 case Intrinsic::x86_avx2_psll_w:
10610 case Intrinsic::x86_avx2_psll_d:
10611 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010612 case Intrinsic::x86_sse2_psrl_w:
10613 case Intrinsic::x86_sse2_psrl_d:
10614 case Intrinsic::x86_sse2_psrl_q:
10615 case Intrinsic::x86_avx2_psrl_w:
10616 case Intrinsic::x86_avx2_psrl_d:
10617 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010618 case Intrinsic::x86_sse2_psra_w:
10619 case Intrinsic::x86_sse2_psra_d:
10620 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010621 case Intrinsic::x86_avx2_psra_d: {
10622 unsigned Opcode;
10623 switch (IntNo) {
10624 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10625 case Intrinsic::x86_sse2_psll_w:
10626 case Intrinsic::x86_sse2_psll_d:
10627 case Intrinsic::x86_sse2_psll_q:
10628 case Intrinsic::x86_avx2_psll_w:
10629 case Intrinsic::x86_avx2_psll_d:
10630 case Intrinsic::x86_avx2_psll_q:
10631 Opcode = X86ISD::VSHL;
10632 break;
10633 case Intrinsic::x86_sse2_psrl_w:
10634 case Intrinsic::x86_sse2_psrl_d:
10635 case Intrinsic::x86_sse2_psrl_q:
10636 case Intrinsic::x86_avx2_psrl_w:
10637 case Intrinsic::x86_avx2_psrl_d:
10638 case Intrinsic::x86_avx2_psrl_q:
10639 Opcode = X86ISD::VSRL;
10640 break;
10641 case Intrinsic::x86_sse2_psra_w:
10642 case Intrinsic::x86_sse2_psra_d:
10643 case Intrinsic::x86_avx2_psra_w:
10644 case Intrinsic::x86_avx2_psra_d:
10645 Opcode = X86ISD::VSRA;
10646 break;
10647 }
10648 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010649 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010650 }
10651
10652 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010653 case Intrinsic::x86_sse2_pslli_w:
10654 case Intrinsic::x86_sse2_pslli_d:
10655 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010656 case Intrinsic::x86_avx2_pslli_w:
10657 case Intrinsic::x86_avx2_pslli_d:
10658 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010659 case Intrinsic::x86_sse2_psrli_w:
10660 case Intrinsic::x86_sse2_psrli_d:
10661 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010662 case Intrinsic::x86_avx2_psrli_w:
10663 case Intrinsic::x86_avx2_psrli_d:
10664 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010665 case Intrinsic::x86_sse2_psrai_w:
10666 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010667 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010668 case Intrinsic::x86_avx2_psrai_d: {
10669 unsigned Opcode;
10670 switch (IntNo) {
10671 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10672 case Intrinsic::x86_sse2_pslli_w:
10673 case Intrinsic::x86_sse2_pslli_d:
10674 case Intrinsic::x86_sse2_pslli_q:
10675 case Intrinsic::x86_avx2_pslli_w:
10676 case Intrinsic::x86_avx2_pslli_d:
10677 case Intrinsic::x86_avx2_pslli_q:
10678 Opcode = X86ISD::VSHLI;
10679 break;
10680 case Intrinsic::x86_sse2_psrli_w:
10681 case Intrinsic::x86_sse2_psrli_d:
10682 case Intrinsic::x86_sse2_psrli_q:
10683 case Intrinsic::x86_avx2_psrli_w:
10684 case Intrinsic::x86_avx2_psrli_d:
10685 case Intrinsic::x86_avx2_psrli_q:
10686 Opcode = X86ISD::VSRLI;
10687 break;
10688 case Intrinsic::x86_sse2_psrai_w:
10689 case Intrinsic::x86_sse2_psrai_d:
10690 case Intrinsic::x86_avx2_psrai_w:
10691 case Intrinsic::x86_avx2_psrai_d:
10692 Opcode = X86ISD::VSRAI;
10693 break;
10694 }
10695 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010696 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010697 }
10698
Craig Topper4feb6472012-08-06 06:22:36 +000010699 case Intrinsic::x86_sse42_pcmpistria128:
10700 case Intrinsic::x86_sse42_pcmpestria128:
10701 case Intrinsic::x86_sse42_pcmpistric128:
10702 case Intrinsic::x86_sse42_pcmpestric128:
10703 case Intrinsic::x86_sse42_pcmpistrio128:
10704 case Intrinsic::x86_sse42_pcmpestrio128:
10705 case Intrinsic::x86_sse42_pcmpistris128:
10706 case Intrinsic::x86_sse42_pcmpestris128:
10707 case Intrinsic::x86_sse42_pcmpistriz128:
10708 case Intrinsic::x86_sse42_pcmpestriz128: {
10709 unsigned Opcode;
10710 unsigned X86CC;
10711 switch (IntNo) {
10712 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10713 case Intrinsic::x86_sse42_pcmpistria128:
10714 Opcode = X86ISD::PCMPISTRI;
10715 X86CC = X86::COND_A;
10716 break;
10717 case Intrinsic::x86_sse42_pcmpestria128:
10718 Opcode = X86ISD::PCMPESTRI;
10719 X86CC = X86::COND_A;
10720 break;
10721 case Intrinsic::x86_sse42_pcmpistric128:
10722 Opcode = X86ISD::PCMPISTRI;
10723 X86CC = X86::COND_B;
10724 break;
10725 case Intrinsic::x86_sse42_pcmpestric128:
10726 Opcode = X86ISD::PCMPESTRI;
10727 X86CC = X86::COND_B;
10728 break;
10729 case Intrinsic::x86_sse42_pcmpistrio128:
10730 Opcode = X86ISD::PCMPISTRI;
10731 X86CC = X86::COND_O;
10732 break;
10733 case Intrinsic::x86_sse42_pcmpestrio128:
10734 Opcode = X86ISD::PCMPESTRI;
10735 X86CC = X86::COND_O;
10736 break;
10737 case Intrinsic::x86_sse42_pcmpistris128:
10738 Opcode = X86ISD::PCMPISTRI;
10739 X86CC = X86::COND_S;
10740 break;
10741 case Intrinsic::x86_sse42_pcmpestris128:
10742 Opcode = X86ISD::PCMPESTRI;
10743 X86CC = X86::COND_S;
10744 break;
10745 case Intrinsic::x86_sse42_pcmpistriz128:
10746 Opcode = X86ISD::PCMPISTRI;
10747 X86CC = X86::COND_E;
10748 break;
10749 case Intrinsic::x86_sse42_pcmpestriz128:
10750 Opcode = X86ISD::PCMPESTRI;
10751 X86CC = X86::COND_E;
10752 break;
10753 }
10754 SmallVector<SDValue, 5> NewOps;
10755 NewOps.append(Op->op_begin()+1, Op->op_end());
10756 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10757 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10758 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10759 DAG.getConstant(X86CC, MVT::i8),
10760 SDValue(PCMP.getNode(), 1));
10761 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10762 }
Craig Topper6d688152012-08-14 07:43:25 +000010763
Craig Topper4feb6472012-08-06 06:22:36 +000010764 case Intrinsic::x86_sse42_pcmpistri128:
10765 case Intrinsic::x86_sse42_pcmpestri128: {
10766 unsigned Opcode;
10767 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10768 Opcode = X86ISD::PCMPISTRI;
10769 else
10770 Opcode = X86ISD::PCMPESTRI;
10771
10772 SmallVector<SDValue, 5> NewOps;
10773 NewOps.append(Op->op_begin()+1, Op->op_end());
10774 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10775 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10776 }
Craig Topper0e292372012-08-24 04:03:22 +000010777 case Intrinsic::x86_fma_vfmadd_ps:
10778 case Intrinsic::x86_fma_vfmadd_pd:
10779 case Intrinsic::x86_fma_vfmsub_ps:
10780 case Intrinsic::x86_fma_vfmsub_pd:
10781 case Intrinsic::x86_fma_vfnmadd_ps:
10782 case Intrinsic::x86_fma_vfnmadd_pd:
10783 case Intrinsic::x86_fma_vfnmsub_ps:
10784 case Intrinsic::x86_fma_vfnmsub_pd:
10785 case Intrinsic::x86_fma_vfmaddsub_ps:
10786 case Intrinsic::x86_fma_vfmaddsub_pd:
10787 case Intrinsic::x86_fma_vfmsubadd_ps:
10788 case Intrinsic::x86_fma_vfmsubadd_pd:
10789 case Intrinsic::x86_fma_vfmadd_ps_256:
10790 case Intrinsic::x86_fma_vfmadd_pd_256:
10791 case Intrinsic::x86_fma_vfmsub_ps_256:
10792 case Intrinsic::x86_fma_vfmsub_pd_256:
10793 case Intrinsic::x86_fma_vfnmadd_ps_256:
10794 case Intrinsic::x86_fma_vfnmadd_pd_256:
10795 case Intrinsic::x86_fma_vfnmsub_ps_256:
10796 case Intrinsic::x86_fma_vfnmsub_pd_256:
10797 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10798 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10799 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10800 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010801 unsigned Opc;
10802 switch (IntNo) {
10803 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10804 case Intrinsic::x86_fma_vfmadd_ps:
10805 case Intrinsic::x86_fma_vfmadd_pd:
10806 case Intrinsic::x86_fma_vfmadd_ps_256:
10807 case Intrinsic::x86_fma_vfmadd_pd_256:
10808 Opc = X86ISD::FMADD;
10809 break;
10810 case Intrinsic::x86_fma_vfmsub_ps:
10811 case Intrinsic::x86_fma_vfmsub_pd:
10812 case Intrinsic::x86_fma_vfmsub_ps_256:
10813 case Intrinsic::x86_fma_vfmsub_pd_256:
10814 Opc = X86ISD::FMSUB;
10815 break;
10816 case Intrinsic::x86_fma_vfnmadd_ps:
10817 case Intrinsic::x86_fma_vfnmadd_pd:
10818 case Intrinsic::x86_fma_vfnmadd_ps_256:
10819 case Intrinsic::x86_fma_vfnmadd_pd_256:
10820 Opc = X86ISD::FNMADD;
10821 break;
10822 case Intrinsic::x86_fma_vfnmsub_ps:
10823 case Intrinsic::x86_fma_vfnmsub_pd:
10824 case Intrinsic::x86_fma_vfnmsub_ps_256:
10825 case Intrinsic::x86_fma_vfnmsub_pd_256:
10826 Opc = X86ISD::FNMSUB;
10827 break;
10828 case Intrinsic::x86_fma_vfmaddsub_ps:
10829 case Intrinsic::x86_fma_vfmaddsub_pd:
10830 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10831 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10832 Opc = X86ISD::FMADDSUB;
10833 break;
10834 case Intrinsic::x86_fma_vfmsubadd_ps:
10835 case Intrinsic::x86_fma_vfmsubadd_pd:
10836 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10837 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10838 Opc = X86ISD::FMSUBADD;
10839 break;
10840 }
10841
10842 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10843 Op.getOperand(2), Op.getOperand(3));
10844 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010845 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010846}
Evan Cheng72261582005-12-20 06:22:03 +000010847
Craig Topper55b24052012-09-11 06:15:32 +000010848static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010849 DebugLoc dl = Op.getDebugLoc();
10850 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10851 switch (IntNo) {
10852 default: return SDValue(); // Don't custom lower most intrinsics.
10853
10854 // RDRAND intrinsics.
10855 case Intrinsic::x86_rdrand_16:
10856 case Intrinsic::x86_rdrand_32:
10857 case Intrinsic::x86_rdrand_64: {
10858 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010859 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10860 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010861
10862 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10863 // return the value from Rand, which is always 0, casted to i32.
10864 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10865 DAG.getConstant(1, Op->getValueType(1)),
10866 DAG.getConstant(X86::COND_B, MVT::i32),
10867 SDValue(Result.getNode(), 1) };
10868 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10869 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10870 Ops, 4);
10871
10872 // Return { result, isValid, chain }.
10873 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010874 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010875 }
10876 }
10877}
10878
Dan Gohmand858e902010-04-17 15:26:15 +000010879SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10880 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010881 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10882 MFI->setReturnAddressIsTaken(true);
10883
Bill Wendling64e87322009-01-16 19:25:27 +000010884 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010885 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010886 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010887
10888 if (Depth > 0) {
10889 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10890 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010891 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10892 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10893 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010894 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010895 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010896 }
10897
10898 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010899 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010900 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010901 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010902}
10903
Dan Gohmand858e902010-04-17 15:26:15 +000010904SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010905 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10906 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010907
Owen Andersone50ed302009-08-10 22:56:29 +000010908 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010909 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010910 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10911 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010912 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010913 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010914 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10915 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010916 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010917 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010918}
10919
Dan Gohman475871a2008-07-27 21:46:04 +000010920SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010921 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010922 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010923}
10924
Dan Gohmand858e902010-04-17 15:26:15 +000010925SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010926 SDValue Chain = Op.getOperand(0);
10927 SDValue Offset = Op.getOperand(1);
10928 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010929 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010930
Dan Gohmand8816272010-08-11 18:14:00 +000010931 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10932 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10933 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010934 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010935
Dan Gohmand8816272010-08-11 18:14:00 +000010936 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010937 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010938 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010939 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10940 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010941 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010942
Dale Johannesene4d209d2009-02-03 20:21:25 +000010943 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010944 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010945 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010946}
10947
Michael Liao6c0e04c2012-10-15 22:39:43 +000010948SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
10949 SelectionDAG &DAG) const {
10950 DebugLoc DL = Op.getDebugLoc();
10951 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
10952 DAG.getVTList(MVT::i32, MVT::Other),
10953 Op.getOperand(0), Op.getOperand(1));
10954}
10955
10956SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
10957 SelectionDAG &DAG) const {
10958 DebugLoc DL = Op.getDebugLoc();
10959 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
10960 Op.getOperand(0), Op.getOperand(1));
10961}
10962
Craig Topper55b24052012-09-11 06:15:32 +000010963static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010964 return Op.getOperand(0);
10965}
10966
10967SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10968 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010969 SDValue Root = Op.getOperand(0);
10970 SDValue Trmp = Op.getOperand(1); // trampoline
10971 SDValue FPtr = Op.getOperand(2); // nested function
10972 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010973 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010974
Dan Gohman69de1932008-02-06 22:27:42 +000010975 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000010976 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010977
10978 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010979 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010980
10981 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010982 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10983 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010984
Michael Liao7abf67a2012-10-04 19:50:43 +000010985 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
10986 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000010987
10988 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10989
10990 // Load the pointer to the nested function into R11.
10991 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010992 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010993 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010994 Addr, MachinePointerInfo(TrmpAddr),
10995 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010996
Owen Anderson825b72b2009-08-11 20:47:22 +000010997 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10998 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010999 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11000 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011001 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011002
11003 // Load the 'nest' parameter value into R10.
11004 // R10 is specified in X86CallingConv.td
11005 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011006 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11007 DAG.getConstant(10, MVT::i64));
11008 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011009 Addr, MachinePointerInfo(TrmpAddr, 10),
11010 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011011
Owen Anderson825b72b2009-08-11 20:47:22 +000011012 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11013 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011014 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11015 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011016 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011017
11018 // Jump to the nested function.
11019 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011020 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11021 DAG.getConstant(20, MVT::i64));
11022 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011023 Addr, MachinePointerInfo(TrmpAddr, 20),
11024 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011025
11026 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011027 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11028 DAG.getConstant(22, MVT::i64));
11029 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011030 MachinePointerInfo(TrmpAddr, 22),
11031 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011032
Duncan Sands4a544a72011-09-06 13:37:06 +000011033 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011034 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011035 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011036 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011037 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011038 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011039
11040 switch (CC) {
11041 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011042 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011043 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011044 case CallingConv::X86_StdCall: {
11045 // Pass 'nest' parameter in ECX.
11046 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011047 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011048
11049 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011050 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011051 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011052
Chris Lattner58d74912008-03-12 17:45:29 +000011053 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011054 unsigned InRegCount = 0;
11055 unsigned Idx = 1;
11056
11057 for (FunctionType::param_iterator I = FTy->param_begin(),
11058 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011059 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011060 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011061 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011062
11063 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011064 report_fatal_error("Nest register in use - reduce number of inreg"
11065 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011066 }
11067 }
11068 break;
11069 }
11070 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011071 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011072 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011073 // Pass 'nest' parameter in EAX.
11074 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011075 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011076 break;
11077 }
11078
Dan Gohman475871a2008-07-27 21:46:04 +000011079 SDValue OutChains[4];
11080 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011081
Owen Anderson825b72b2009-08-11 20:47:22 +000011082 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11083 DAG.getConstant(10, MVT::i32));
11084 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011085
Chris Lattnera62fe662010-02-05 19:20:30 +000011086 // This is storing the opcode for MOV32ri.
11087 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011088 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011089 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011090 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011091 Trmp, MachinePointerInfo(TrmpAddr),
11092 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011093
Owen Anderson825b72b2009-08-11 20:47:22 +000011094 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11095 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011096 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11097 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011098 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011099
Chris Lattnera62fe662010-02-05 19:20:30 +000011100 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011101 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11102 DAG.getConstant(5, MVT::i32));
11103 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011104 MachinePointerInfo(TrmpAddr, 5),
11105 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011106
Owen Anderson825b72b2009-08-11 20:47:22 +000011107 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11108 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011109 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11110 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011111 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011112
Duncan Sands4a544a72011-09-06 13:37:06 +000011113 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011114 }
11115}
11116
Dan Gohmand858e902010-04-17 15:26:15 +000011117SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11118 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011119 /*
11120 The rounding mode is in bits 11:10 of FPSR, and has the following
11121 settings:
11122 00 Round to nearest
11123 01 Round to -inf
11124 10 Round to +inf
11125 11 Round to 0
11126
11127 FLT_ROUNDS, on the other hand, expects the following:
11128 -1 Undefined
11129 0 Round to 0
11130 1 Round to nearest
11131 2 Round to +inf
11132 3 Round to -inf
11133
11134 To perform the conversion, we do:
11135 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11136 */
11137
11138 MachineFunction &MF = DAG.getMachineFunction();
11139 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011140 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011141 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011142 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011143 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011144
11145 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011146 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011147 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011148
Chris Lattner2156b792010-09-22 01:11:26 +000011149 MachineMemOperand *MMO =
11150 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11151 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011152
Chris Lattner2156b792010-09-22 01:11:26 +000011153 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11154 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11155 DAG.getVTList(MVT::Other),
11156 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011157
11158 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011159 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011160 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011161
11162 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011163 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011164 DAG.getNode(ISD::SRL, DL, MVT::i16,
11165 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011166 CWD, DAG.getConstant(0x800, MVT::i16)),
11167 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011168 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011169 DAG.getNode(ISD::SRL, DL, MVT::i16,
11170 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011171 CWD, DAG.getConstant(0x400, MVT::i16)),
11172 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011173
Dan Gohman475871a2008-07-27 21:46:04 +000011174 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011175 DAG.getNode(ISD::AND, DL, MVT::i16,
11176 DAG.getNode(ISD::ADD, DL, MVT::i16,
11177 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011178 DAG.getConstant(1, MVT::i16)),
11179 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011180
Duncan Sands83ec4b62008-06-06 12:08:01 +000011181 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011182 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011183}
11184
Craig Topper55b24052012-09-11 06:15:32 +000011185static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011186 EVT VT = Op.getValueType();
11187 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011188 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011189 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011190
11191 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011192 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011193 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011194 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011195 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011196 }
Evan Cheng18efe262007-12-14 02:13:44 +000011197
Evan Cheng152804e2007-12-14 08:30:15 +000011198 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011199 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011200 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011201
11202 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011203 SDValue Ops[] = {
11204 Op,
11205 DAG.getConstant(NumBits+NumBits-1, OpVT),
11206 DAG.getConstant(X86::COND_E, MVT::i8),
11207 Op.getValue(1)
11208 };
11209 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011210
11211 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011212 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011213
Owen Anderson825b72b2009-08-11 20:47:22 +000011214 if (VT == MVT::i8)
11215 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011216 return Op;
11217}
11218
Craig Topper55b24052012-09-11 06:15:32 +000011219static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011220 EVT VT = Op.getValueType();
11221 EVT OpVT = VT;
11222 unsigned NumBits = VT.getSizeInBits();
11223 DebugLoc dl = Op.getDebugLoc();
11224
11225 Op = Op.getOperand(0);
11226 if (VT == MVT::i8) {
11227 // Zero extend to i32 since there is not an i8 bsr.
11228 OpVT = MVT::i32;
11229 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11230 }
11231
11232 // Issue a bsr (scan bits in reverse).
11233 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11234 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11235
11236 // And xor with NumBits-1.
11237 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11238
11239 if (VT == MVT::i8)
11240 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11241 return Op;
11242}
11243
Craig Topper55b24052012-09-11 06:15:32 +000011244static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011245 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011246 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011247 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011248 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011249
11250 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011251 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011252 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011253
11254 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011255 SDValue Ops[] = {
11256 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011257 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011258 DAG.getConstant(X86::COND_E, MVT::i8),
11259 Op.getValue(1)
11260 };
Chandler Carruth77821022011-12-24 12:12:34 +000011261 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011262}
11263
Craig Topper13894fa2011-08-24 06:14:18 +000011264// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11265// ones, and then concatenate the result back.
11266static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011267 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011268
Craig Topper7a9a28b2012-08-12 02:23:29 +000011269 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011270 "Unsupported value type for operation");
11271
Craig Topper66ddd152012-04-27 22:54:43 +000011272 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011273 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011274
11275 // Extract the LHS vectors
11276 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011277 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11278 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011279
11280 // Extract the RHS vectors
11281 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011282 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11283 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011284
11285 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11286 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11287
11288 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11289 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11290 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11291}
11292
Craig Topper55b24052012-09-11 06:15:32 +000011293static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011294 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011295 Op.getValueType().isInteger() &&
11296 "Only handle AVX 256-bit vector integer operation");
11297 return Lower256IntArith(Op, DAG);
11298}
11299
Craig Topper55b24052012-09-11 06:15:32 +000011300static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011301 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011302 Op.getValueType().isInteger() &&
11303 "Only handle AVX 256-bit vector integer operation");
11304 return Lower256IntArith(Op, DAG);
11305}
11306
Craig Topper55b24052012-09-11 06:15:32 +000011307static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11308 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011309 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011310 EVT VT = Op.getValueType();
11311
11312 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011313 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011314 return Lower256IntArith(Op, DAG);
11315
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011316 SDValue A = Op.getOperand(0);
11317 SDValue B = Op.getOperand(1);
11318
11319 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11320 if (VT == MVT::v4i32) {
11321 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11322 "Should not custom lower when pmuldq is available!");
11323
11324 // Extract the odd parts.
11325 const int UnpackMask[] = { 1, -1, 3, -1 };
11326 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11327 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11328
11329 // Multiply the even parts.
11330 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11331 // Now multiply odd parts.
11332 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11333
11334 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11335 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11336
11337 // Merge the two vectors back together with a shuffle. This expands into 2
11338 // shuffles.
11339 const int ShufMask[] = { 0, 4, 2, 6 };
11340 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11341 }
11342
Craig Topper5b209e82012-02-05 03:14:49 +000011343 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11344 "Only know how to lower V2I64/V4I64 multiply");
11345
Craig Topper5b209e82012-02-05 03:14:49 +000011346 // Ahi = psrlqi(a, 32);
11347 // Bhi = psrlqi(b, 32);
11348 //
11349 // AloBlo = pmuludq(a, b);
11350 // AloBhi = pmuludq(a, Bhi);
11351 // AhiBlo = pmuludq(Ahi, b);
11352
11353 // AloBhi = psllqi(AloBhi, 32);
11354 // AhiBlo = psllqi(AhiBlo, 32);
11355 // return AloBlo + AloBhi + AhiBlo;
11356
Craig Topper5b209e82012-02-05 03:14:49 +000011357 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011358
Craig Topper5b209e82012-02-05 03:14:49 +000011359 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11360 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011361
Craig Topper5b209e82012-02-05 03:14:49 +000011362 // Bit cast to 32-bit vectors for MULUDQ
11363 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11364 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11365 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11366 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11367 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011368
Craig Topper5b209e82012-02-05 03:14:49 +000011369 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11370 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11371 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011372
Craig Topper5b209e82012-02-05 03:14:49 +000011373 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11374 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011375
Dale Johannesene4d209d2009-02-03 20:21:25 +000011376 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011377 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011378}
11379
Nadav Rotem43012222011-05-11 08:12:09 +000011380SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11381
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011382 EVT VT = Op.getValueType();
11383 DebugLoc dl = Op.getDebugLoc();
11384 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011385 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011386 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011387
Craig Topper1accb7e2012-01-10 06:54:16 +000011388 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000011389 return SDValue();
11390
Nadav Rotem43012222011-05-11 08:12:09 +000011391 // Optimize shl/srl/sra with constant shift amount.
11392 if (isSplatVector(Amt.getNode())) {
11393 SDValue SclrAmt = Amt->getOperand(0);
11394 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11395 uint64_t ShiftAmt = C->getZExtValue();
11396
Craig Toppered2e13d2012-01-22 19:15:14 +000011397 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011398 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011399 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11400 if (Op.getOpcode() == ISD::SHL)
11401 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11402 DAG.getConstant(ShiftAmt, MVT::i32));
11403 if (Op.getOpcode() == ISD::SRL)
11404 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11405 DAG.getConstant(ShiftAmt, MVT::i32));
11406 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11407 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11408 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011409 }
11410
Craig Toppered2e13d2012-01-22 19:15:14 +000011411 if (VT == MVT::v16i8) {
11412 if (Op.getOpcode() == ISD::SHL) {
11413 // Make a large shift.
11414 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11415 DAG.getConstant(ShiftAmt, MVT::i32));
11416 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11417 // Zero out the rightmost bits.
11418 SmallVector<SDValue, 16> V(16,
11419 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11420 MVT::i8));
11421 return DAG.getNode(ISD::AND, dl, VT, SHL,
11422 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011423 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011424 if (Op.getOpcode() == ISD::SRL) {
11425 // Make a large shift.
11426 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11427 DAG.getConstant(ShiftAmt, MVT::i32));
11428 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11429 // Zero out the leftmost bits.
11430 SmallVector<SDValue, 16> V(16,
11431 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11432 MVT::i8));
11433 return DAG.getNode(ISD::AND, dl, VT, SRL,
11434 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11435 }
11436 if (Op.getOpcode() == ISD::SRA) {
11437 if (ShiftAmt == 7) {
11438 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011439 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011440 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011441 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011442
Craig Toppered2e13d2012-01-22 19:15:14 +000011443 // R s>> a === ((R u>> a) ^ m) - m
11444 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11445 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11446 MVT::i8));
11447 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11448 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11449 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11450 return Res;
11451 }
Craig Topper731dfd02012-04-23 03:42:40 +000011452 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011453 }
Craig Topper46154eb2011-11-11 07:39:23 +000011454
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011455 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011456 if (Op.getOpcode() == ISD::SHL) {
11457 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011458 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11459 DAG.getConstant(ShiftAmt, MVT::i32));
11460 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011461 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011462 SmallVector<SDValue, 32> V(32,
11463 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11464 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011465 return DAG.getNode(ISD::AND, dl, VT, SHL,
11466 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011467 }
Craig Topper0d86d462011-11-20 00:12:05 +000011468 if (Op.getOpcode() == ISD::SRL) {
11469 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011470 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11471 DAG.getConstant(ShiftAmt, MVT::i32));
11472 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011473 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011474 SmallVector<SDValue, 32> V(32,
11475 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11476 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011477 return DAG.getNode(ISD::AND, dl, VT, SRL,
11478 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11479 }
11480 if (Op.getOpcode() == ISD::SRA) {
11481 if (ShiftAmt == 7) {
11482 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011483 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011484 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011485 }
11486
11487 // R s>> a === ((R u>> a) ^ m) - m
11488 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11489 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11490 MVT::i8));
11491 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11492 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11493 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11494 return Res;
11495 }
Craig Topper731dfd02012-04-23 03:42:40 +000011496 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011497 }
Nadav Rotem43012222011-05-11 08:12:09 +000011498 }
11499 }
11500
11501 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011502 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011503 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
11504 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000011505
Chris Lattner7302d802012-02-06 21:56:39 +000011506 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
11507 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000011508 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
11509 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000011510 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011511 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000011512
11513 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011514 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011515 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11516 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11517 }
Nadav Rotem43012222011-05-11 08:12:09 +000011518 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011519 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011520
Nate Begeman51409212010-07-28 00:21:48 +000011521 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000011522 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
11523 DAG.getConstant(5, MVT::i32));
11524 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011525
Lang Hames8b99c1e2011-12-17 01:08:46 +000011526 // Turn 'a' into a mask suitable for VSELECT
11527 SDValue VSelM = DAG.getConstant(0x80, VT);
11528 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011529 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011530
Lang Hames8b99c1e2011-12-17 01:08:46 +000011531 SDValue CM1 = DAG.getConstant(0x0f, VT);
11532 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011533
Lang Hames8b99c1e2011-12-17 01:08:46 +000011534 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11535 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011536 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11537 DAG.getConstant(4, MVT::i32), DAG);
11538 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011539 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11540
Nate Begeman51409212010-07-28 00:21:48 +000011541 // a += a
11542 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011543 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011544 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011545
Lang Hames8b99c1e2011-12-17 01:08:46 +000011546 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11547 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011548 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11549 DAG.getConstant(2, MVT::i32), DAG);
11550 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011551 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11552
Nate Begeman51409212010-07-28 00:21:48 +000011553 // a += a
11554 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011555 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011556 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011557
Lang Hames8b99c1e2011-12-17 01:08:46 +000011558 // return VSELECT(r, r+r, a);
11559 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011560 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011561 return R;
11562 }
Craig Topper46154eb2011-11-11 07:39:23 +000011563
11564 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011565 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011566 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011567 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11568 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11569
11570 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011571 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11572 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011573
11574 // Recreate the shift amount vectors
11575 SDValue Amt1, Amt2;
11576 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11577 // Constant shift amount
11578 SmallVector<SDValue, 4> Amt1Csts;
11579 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011580 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011581 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011582 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011583 Amt2Csts.push_back(Amt->getOperand(i));
11584
11585 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11586 &Amt1Csts[0], NumElems/2);
11587 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11588 &Amt2Csts[0], NumElems/2);
11589 } else {
11590 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011591 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11592 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011593 }
11594
11595 // Issue new vector shifts for the smaller types
11596 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11597 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11598
11599 // Concatenate the result back
11600 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11601 }
11602
Nate Begeman51409212010-07-28 00:21:48 +000011603 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011604}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011605
Craig Topper55b24052012-09-11 06:15:32 +000011606static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011607 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11608 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011609 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11610 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011611 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011612 SDValue LHS = N->getOperand(0);
11613 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011614 unsigned BaseOp = 0;
11615 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011616 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011617 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011618 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011619 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011620 // A subtract of one will be selected as a INC. Note that INC doesn't
11621 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011622 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11623 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011624 BaseOp = X86ISD::INC;
11625 Cond = X86::COND_O;
11626 break;
11627 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011628 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011629 Cond = X86::COND_O;
11630 break;
11631 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011632 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011633 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011634 break;
11635 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011636 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11637 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011638 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11639 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011640 BaseOp = X86ISD::DEC;
11641 Cond = X86::COND_O;
11642 break;
11643 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011644 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011645 Cond = X86::COND_O;
11646 break;
11647 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011648 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011649 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011650 break;
11651 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011652 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011653 Cond = X86::COND_O;
11654 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011655 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11656 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11657 MVT::i32);
11658 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011659
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011660 SDValue SetCC =
11661 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11662 DAG.getConstant(X86::COND_O, MVT::i32),
11663 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011664
Dan Gohman6e5fda22011-07-22 18:45:15 +000011665 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011666 }
Bill Wendling74c37652008-12-09 22:08:41 +000011667 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011668
Bill Wendling61edeb52008-12-02 01:06:39 +000011669 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011670 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011671 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011672
Bill Wendling61edeb52008-12-02 01:06:39 +000011673 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011674 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11675 DAG.getConstant(Cond, MVT::i32),
11676 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011677
Dan Gohman6e5fda22011-07-22 18:45:15 +000011678 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011679}
11680
Chad Rosier30450e82011-12-22 22:35:21 +000011681SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11682 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011683 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011684 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11685 EVT VT = Op.getValueType();
11686
Craig Toppered2e13d2012-01-22 19:15:14 +000011687 if (!Subtarget->hasSSE2() || !VT.isVector())
11688 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011689
Craig Toppered2e13d2012-01-22 19:15:14 +000011690 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11691 ExtraVT.getScalarType().getSizeInBits();
11692 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11693
11694 switch (VT.getSimpleVT().SimpleTy) {
11695 default: return SDValue();
11696 case MVT::v8i32:
11697 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011698 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011699 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011700 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011701 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011702 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011703
Craig Toppered2e13d2012-01-22 19:15:14 +000011704 // Extract the LHS vectors
11705 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011706 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11707 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011708
Craig Toppered2e13d2012-01-22 19:15:14 +000011709 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11710 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011711
Craig Toppered2e13d2012-01-22 19:15:14 +000011712 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011713 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011714 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11715 ExtraNumElems/2);
11716 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011717
Craig Toppered2e13d2012-01-22 19:15:14 +000011718 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11719 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011720
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011721 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011722 }
11723 // fall through
11724 case MVT::v4i32:
11725 case MVT::v8i16: {
11726 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11727 Op.getOperand(0), ShAmt, DAG);
11728 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011729 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011730 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011731}
11732
Craig Topper55b24052012-09-11 06:15:32 +000011733static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11734 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011735 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011736
Eric Christopher77ed1352011-07-08 00:04:56 +000011737 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11738 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011739 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011740 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011741 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011742 SDValue Ops[] = {
11743 DAG.getRegister(X86::ESP, MVT::i32), // Base
11744 DAG.getTargetConstant(1, MVT::i8), // Scale
11745 DAG.getRegister(0, MVT::i32), // Index
11746 DAG.getTargetConstant(0, MVT::i32), // Disp
11747 DAG.getRegister(0, MVT::i32), // Segment.
11748 Zero,
11749 Chain
11750 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011751 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011752 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11753 array_lengthof(Ops));
11754 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011755 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011756
Eric Christopher9a9d2752010-07-22 02:48:34 +000011757 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011758 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011759 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011760
Chris Lattner132929a2010-08-14 17:26:09 +000011761 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11762 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11763 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11764 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011765
Chris Lattner132929a2010-08-14 17:26:09 +000011766 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11767 if (!Op1 && !Op2 && !Op3 && Op4)
11768 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011769
Chris Lattner132929a2010-08-14 17:26:09 +000011770 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11771 if (Op1 && !Op2 && !Op3 && !Op4)
11772 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011773
11774 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011775 // (MFENCE)>;
11776 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011777}
11778
Craig Topper55b24052012-09-11 06:15:32 +000011779static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11780 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011781 DebugLoc dl = Op.getDebugLoc();
11782 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11783 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11784 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11785 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11786
11787 // The only fence that needs an instruction is a sequentially-consistent
11788 // cross-thread fence.
11789 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11790 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11791 // no-sse2). There isn't any reason to disable it if the target processor
11792 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011793 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011794 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11795
11796 SDValue Chain = Op.getOperand(0);
11797 SDValue Zero = DAG.getConstant(0, MVT::i32);
11798 SDValue Ops[] = {
11799 DAG.getRegister(X86::ESP, MVT::i32), // Base
11800 DAG.getTargetConstant(1, MVT::i8), // Scale
11801 DAG.getRegister(0, MVT::i32), // Index
11802 DAG.getTargetConstant(0, MVT::i32), // Disp
11803 DAG.getRegister(0, MVT::i32), // Segment.
11804 Zero,
11805 Chain
11806 };
11807 SDNode *Res =
11808 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11809 array_lengthof(Ops));
11810 return SDValue(Res, 0);
11811 }
11812
11813 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11814 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11815}
11816
Craig Topper55b24052012-09-11 06:15:32 +000011817static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11818 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011819 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011820 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011821 unsigned Reg = 0;
11822 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011823 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011824 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011825 case MVT::i8: Reg = X86::AL; size = 1; break;
11826 case MVT::i16: Reg = X86::AX; size = 2; break;
11827 case MVT::i32: Reg = X86::EAX; size = 4; break;
11828 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011829 assert(Subtarget->is64Bit() && "Node not type legal!");
11830 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011831 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011832 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011833 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011834 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011835 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011836 Op.getOperand(1),
11837 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011838 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011839 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011840 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011841 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11842 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11843 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011844 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011845 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011846 return cpOut;
11847}
11848
Craig Topper55b24052012-09-11 06:15:32 +000011849static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11850 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011851 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011852 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011853 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011854 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011855 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011856 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11857 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011858 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011859 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11860 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011861 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011862 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011863 rdx.getValue(1)
11864 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011865 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011866}
11867
Craig Topper55b24052012-09-11 06:15:32 +000011868SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011869 EVT SrcVT = Op.getOperand(0).getValueType();
11870 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011871 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011872 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011873 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011874 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011875 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011876 // i64 <=> MMX conversions are Legal.
11877 if (SrcVT==MVT::i64 && DstVT.isVector())
11878 return Op;
11879 if (DstVT==MVT::i64 && SrcVT.isVector())
11880 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011881 // MMX <=> MMX conversions are Legal.
11882 if (SrcVT.isVector() && DstVT.isVector())
11883 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011884 // All other conversions need to be expanded.
11885 return SDValue();
11886}
Chris Lattner5b856542010-12-20 00:59:46 +000011887
Craig Topper55b24052012-09-11 06:15:32 +000011888static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011889 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011890 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011891 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011892 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011893 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011894 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011895 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011896 Node->getOperand(0),
11897 Node->getOperand(1), negOp,
11898 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011899 cast<AtomicSDNode>(Node)->getAlignment(),
11900 cast<AtomicSDNode>(Node)->getOrdering(),
11901 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011902}
11903
Eli Friedman327236c2011-08-24 20:50:09 +000011904static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11905 SDNode *Node = Op.getNode();
11906 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011907 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011908
11909 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011910 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11911 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11912 // (The only way to get a 16-byte store is cmpxchg16b)
11913 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11914 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11915 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011916 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11917 cast<AtomicSDNode>(Node)->getMemoryVT(),
11918 Node->getOperand(0),
11919 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011920 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011921 cast<AtomicSDNode>(Node)->getOrdering(),
11922 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011923 return Swap.getValue(1);
11924 }
11925 // Other atomic stores have a simple pattern.
11926 return Op;
11927}
11928
Chris Lattner5b856542010-12-20 00:59:46 +000011929static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11930 EVT VT = Op.getNode()->getValueType(0);
11931
11932 // Let legalize expand this if it isn't a legal type yet.
11933 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11934 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011935
Chris Lattner5b856542010-12-20 00:59:46 +000011936 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011937
Chris Lattner5b856542010-12-20 00:59:46 +000011938 unsigned Opc;
11939 bool ExtraOp = false;
11940 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011941 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011942 case ISD::ADDC: Opc = X86ISD::ADD; break;
11943 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11944 case ISD::SUBC: Opc = X86ISD::SUB; break;
11945 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11946 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011947
Chris Lattner5b856542010-12-20 00:59:46 +000011948 if (!ExtraOp)
11949 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11950 Op.getOperand(1));
11951 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11952 Op.getOperand(1), Op.getOperand(2));
11953}
11954
Evan Cheng0db9fe62006-04-25 20:13:52 +000011955/// LowerOperation - Provide custom lowering hooks for some operations.
11956///
Dan Gohmand858e902010-04-17 15:26:15 +000011957SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011958 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011959 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011960 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011961 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11962 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11963 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011964 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011965 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011966 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011967 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011968 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11969 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11970 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011971 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11972 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011973 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11974 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11975 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011976 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011977 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011978 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011979 case ISD::SHL_PARTS:
11980 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011981 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011982 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011983 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Michael Liaobedcbd42012-10-16 18:14:11 +000011984 case ISD::TRUNCATE: return lowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000011985 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
11986 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
11987 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011988 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011989 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Michael Liao9d796db2012-10-10 16:32:15 +000011990 case ISD::FP_EXTEND: return lowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011991 case ISD::FABS: return LowerFABS(Op, DAG);
11992 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011993 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011994 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011995 case ISD::SETCC: return LowerSETCC(Op, DAG);
11996 case ISD::SELECT: return LowerSELECT(Op, DAG);
11997 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011998 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011999 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012000 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012001 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012002 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012003 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012004 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12005 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012006 case ISD::FRAME_TO_ARGS_OFFSET:
12007 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012008 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012009 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012010 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12011 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012012 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12013 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012014 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012015 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012016 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012017 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012018 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012019 case ISD::SRA:
12020 case ISD::SRL:
12021 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012022 case ISD::SADDO:
12023 case ISD::UADDO:
12024 case ISD::SSUBO:
12025 case ISD::USUBO:
12026 case ISD::SMULO:
12027 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012028 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012029 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012030 case ISD::ADDC:
12031 case ISD::ADDE:
12032 case ISD::SUBC:
12033 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012034 case ISD::ADD: return LowerADD(Op, DAG);
12035 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012036 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012037}
12038
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012039static void ReplaceATOMIC_LOAD(SDNode *Node,
12040 SmallVectorImpl<SDValue> &Results,
12041 SelectionDAG &DAG) {
12042 DebugLoc dl = Node->getDebugLoc();
12043 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12044
12045 // Convert wide load -> cmpxchg8b/cmpxchg16b
12046 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12047 // (The only way to get a 16-byte load is cmpxchg16b)
12048 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012049 SDValue Zero = DAG.getConstant(0, VT);
12050 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012051 Node->getOperand(0),
12052 Node->getOperand(1), Zero, Zero,
12053 cast<AtomicSDNode>(Node)->getMemOperand(),
12054 cast<AtomicSDNode>(Node)->getOrdering(),
12055 cast<AtomicSDNode>(Node)->getSynchScope());
12056 Results.push_back(Swap.getValue(0));
12057 Results.push_back(Swap.getValue(1));
12058}
12059
Craig Topperc0878702012-08-17 06:55:11 +000012060static void
Duncan Sands1607f052008-12-01 11:39:25 +000012061ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012062 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012063 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012064 assert (Node->getValueType(0) == MVT::i64 &&
12065 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012066
12067 SDValue Chain = Node->getOperand(0);
12068 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012069 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012070 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012071 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012072 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012073 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012074 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012075 SDValue Result =
12076 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12077 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012078 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012079 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012080 Results.push_back(Result.getValue(2));
12081}
12082
Duncan Sands126d9072008-07-04 11:47:58 +000012083/// ReplaceNodeResults - Replace a node with an illegal result type
12084/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012085void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12086 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012087 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012088 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012089 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012090 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012091 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012092 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012093 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012094 case ISD::ADDC:
12095 case ISD::ADDE:
12096 case ISD::SUBC:
12097 case ISD::SUBE:
12098 // We don't want to expand or promote these.
12099 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012100 case ISD::FP_TO_SINT:
12101 case ISD::FP_TO_UINT: {
12102 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12103
12104 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12105 return;
12106
Eli Friedman948e95a2009-05-23 09:59:16 +000012107 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012108 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012109 SDValue FIST = Vals.first, StackSlot = Vals.second;
12110 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012111 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012112 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012113 if (StackSlot.getNode() != 0)
12114 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12115 MachinePointerInfo(),
12116 false, false, false, 0));
12117 else
12118 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012119 }
12120 return;
12121 }
Michael Liao991b6a22012-10-24 04:09:32 +000012122 case ISD::UINT_TO_FP: {
12123 if (N->getOperand(0).getValueType() != MVT::v2i32 &&
12124 N->getValueType(0) != MVT::v2f32)
12125 return;
12126 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12127 N->getOperand(0));
12128 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12129 MVT::f64);
12130 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12131 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12132 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12133 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12134 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12135 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12136 return;
12137 }
Michael Liao44c2d612012-10-10 16:53:28 +000012138 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012139 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12140 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012141 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12142 Results.push_back(V);
12143 return;
12144 }
Duncan Sands1607f052008-12-01 11:39:25 +000012145 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012146 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012147 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012148 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012149 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012150 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012151 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012152 eax.getValue(2));
12153 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12154 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012155 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012156 Results.push_back(edx.getValue(1));
12157 return;
12158 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012159 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012160 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012161 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012162 bool Regs64bit = T == MVT::i128;
12163 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012164 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012165 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12166 DAG.getConstant(0, HalfT));
12167 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12168 DAG.getConstant(1, HalfT));
12169 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12170 Regs64bit ? X86::RAX : X86::EAX,
12171 cpInL, SDValue());
12172 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12173 Regs64bit ? X86::RDX : X86::EDX,
12174 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012175 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012176 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12177 DAG.getConstant(0, HalfT));
12178 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12179 DAG.getConstant(1, HalfT));
12180 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12181 Regs64bit ? X86::RBX : X86::EBX,
12182 swapInL, cpInH.getValue(1));
12183 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012184 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012185 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012186 SDValue Ops[] = { swapInH.getValue(0),
12187 N->getOperand(1),
12188 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012189 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012190 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012191 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12192 X86ISD::LCMPXCHG8_DAG;
12193 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012194 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012195 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12196 Regs64bit ? X86::RAX : X86::EAX,
12197 HalfT, Result.getValue(1));
12198 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12199 Regs64bit ? X86::RDX : X86::EDX,
12200 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012201 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012202 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012203 Results.push_back(cpOutH.getValue(1));
12204 return;
12205 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012206 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012207 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012208 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012209 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012210 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012211 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012212 case ISD::ATOMIC_LOAD_MAX:
12213 case ISD::ATOMIC_LOAD_MIN:
12214 case ISD::ATOMIC_LOAD_UMAX:
12215 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012216 case ISD::ATOMIC_SWAP: {
12217 unsigned Opc;
12218 switch (N->getOpcode()) {
12219 default: llvm_unreachable("Unexpected opcode");
12220 case ISD::ATOMIC_LOAD_ADD:
12221 Opc = X86ISD::ATOMADD64_DAG;
12222 break;
12223 case ISD::ATOMIC_LOAD_AND:
12224 Opc = X86ISD::ATOMAND64_DAG;
12225 break;
12226 case ISD::ATOMIC_LOAD_NAND:
12227 Opc = X86ISD::ATOMNAND64_DAG;
12228 break;
12229 case ISD::ATOMIC_LOAD_OR:
12230 Opc = X86ISD::ATOMOR64_DAG;
12231 break;
12232 case ISD::ATOMIC_LOAD_SUB:
12233 Opc = X86ISD::ATOMSUB64_DAG;
12234 break;
12235 case ISD::ATOMIC_LOAD_XOR:
12236 Opc = X86ISD::ATOMXOR64_DAG;
12237 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012238 case ISD::ATOMIC_LOAD_MAX:
12239 Opc = X86ISD::ATOMMAX64_DAG;
12240 break;
12241 case ISD::ATOMIC_LOAD_MIN:
12242 Opc = X86ISD::ATOMMIN64_DAG;
12243 break;
12244 case ISD::ATOMIC_LOAD_UMAX:
12245 Opc = X86ISD::ATOMUMAX64_DAG;
12246 break;
12247 case ISD::ATOMIC_LOAD_UMIN:
12248 Opc = X86ISD::ATOMUMIN64_DAG;
12249 break;
Craig Topperc0878702012-08-17 06:55:11 +000012250 case ISD::ATOMIC_SWAP:
12251 Opc = X86ISD::ATOMSWAP64_DAG;
12252 break;
12253 }
12254 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012255 return;
Craig Topperc0878702012-08-17 06:55:11 +000012256 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012257 case ISD::ATOMIC_LOAD:
12258 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012259 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012260}
12261
Evan Cheng72261582005-12-20 06:22:03 +000012262const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12263 switch (Opcode) {
12264 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012265 case X86ISD::BSF: return "X86ISD::BSF";
12266 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012267 case X86ISD::SHLD: return "X86ISD::SHLD";
12268 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012269 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012270 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012271 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012272 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012273 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012274 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012275 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12276 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12277 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012278 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012279 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012280 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012281 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012282 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012283 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012284 case X86ISD::COMI: return "X86ISD::COMI";
12285 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012286 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012287 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012288 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12289 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012290 case X86ISD::CMOV: return "X86ISD::CMOV";
12291 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012292 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012293 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12294 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012295 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012296 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012297 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012298 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012299 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012300 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12301 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012302 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012303 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012304 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012305 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012306 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012307 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012308 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012309 case X86ISD::HADD: return "X86ISD::HADD";
12310 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012311 case X86ISD::FHADD: return "X86ISD::FHADD";
12312 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012313 case X86ISD::UMAX: return "X86ISD::UMAX";
12314 case X86ISD::UMIN: return "X86ISD::UMIN";
12315 case X86ISD::SMAX: return "X86ISD::SMAX";
12316 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012317 case X86ISD::FMAX: return "X86ISD::FMAX";
12318 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012319 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12320 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012321 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12322 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012323 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012324 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012325 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012326 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12327 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012328 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012329 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012330 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012331 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012332 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12333 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012334 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12335 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12336 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12337 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12338 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12339 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012340 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012341 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012342 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012343 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12344 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012345 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012346 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012347 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12348 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012349 case X86ISD::VSHL: return "X86ISD::VSHL";
12350 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012351 case X86ISD::VSRA: return "X86ISD::VSRA";
12352 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12353 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12354 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012355 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012356 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12357 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012358 case X86ISD::ADD: return "X86ISD::ADD";
12359 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012360 case X86ISD::ADC: return "X86ISD::ADC";
12361 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012362 case X86ISD::SMUL: return "X86ISD::SMUL";
12363 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012364 case X86ISD::INC: return "X86ISD::INC";
12365 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012366 case X86ISD::OR: return "X86ISD::OR";
12367 case X86ISD::XOR: return "X86ISD::XOR";
12368 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012369 case X86ISD::BLSI: return "X86ISD::BLSI";
12370 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12371 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012372 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012373 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012374 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012375 case X86ISD::PALIGN: return "X86ISD::PALIGN";
12376 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12377 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012378 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012379 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012380 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012381 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012382 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012383 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12384 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012385 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12386 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12387 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012388 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12389 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012390 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12391 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012392 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012393 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012394 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012395 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12396 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012397 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012398 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012399 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012400 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012401 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012402 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012403 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012404 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012405 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012406 case X86ISD::FMADD: return "X86ISD::FMADD";
12407 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12408 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12409 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12410 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12411 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012412 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12413 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012414 }
12415}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012416
Chris Lattnerc9addb72007-03-30 23:15:24 +000012417// isLegalAddressingMode - Return true if the addressing mode represented
12418// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012419bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012420 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012421 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012422 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012423 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012424
Chris Lattnerc9addb72007-03-30 23:15:24 +000012425 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012426 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012427 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012428
Chris Lattnerc9addb72007-03-30 23:15:24 +000012429 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012430 unsigned GVFlags =
12431 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012432
Chris Lattnerdfed4132009-07-10 07:38:24 +000012433 // If a reference to this global requires an extra load, we can't fold it.
12434 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012435 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012436
Chris Lattnerdfed4132009-07-10 07:38:24 +000012437 // If BaseGV requires a register for the PIC base, we cannot also have a
12438 // BaseReg specified.
12439 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012440 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012441
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012442 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012443 if ((M != CodeModel::Small || R != Reloc::Static) &&
12444 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012445 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012446 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012447
Chris Lattnerc9addb72007-03-30 23:15:24 +000012448 switch (AM.Scale) {
12449 case 0:
12450 case 1:
12451 case 2:
12452 case 4:
12453 case 8:
12454 // These scales always work.
12455 break;
12456 case 3:
12457 case 5:
12458 case 9:
12459 // These scales are formed with basereg+scalereg. Only accept if there is
12460 // no basereg yet.
12461 if (AM.HasBaseReg)
12462 return false;
12463 break;
12464 default: // Other stuff never works.
12465 return false;
12466 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012467
Chris Lattnerc9addb72007-03-30 23:15:24 +000012468 return true;
12469}
12470
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012471bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012472 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012473 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012474 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12475 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012476 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012477}
12478
Evan Cheng70e10d32012-07-17 06:53:39 +000012479bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012480 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012481}
12482
12483bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012484 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012485 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012486}
12487
Owen Andersone50ed302009-08-10 22:56:29 +000012488bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012489 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012490 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012491 unsigned NumBits1 = VT1.getSizeInBits();
12492 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012493 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012494}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012495
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012496bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012497 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012498 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012499}
12500
Owen Andersone50ed302009-08-10 22:56:29 +000012501bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012502 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012503 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012504}
12505
Evan Cheng2766a472012-12-06 19:13:27 +000012506bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12507 EVT VT1 = Val.getValueType();
12508 if (isZExtFree(VT1, VT2))
12509 return true;
12510
12511 if (Val.getOpcode() != ISD::LOAD)
12512 return false;
12513
12514 if (!VT1.isSimple() || !VT1.isInteger() ||
12515 !VT2.isSimple() || !VT2.isInteger())
12516 return false;
12517
12518 switch (VT1.getSimpleVT().SimpleTy) {
12519 default: break;
12520 case MVT::i8:
12521 case MVT::i16:
12522 case MVT::i32:
12523 // X86 has 8, 16, and 32-bit zero-extending loads.
12524 return true;
12525 }
12526
12527 return false;
12528}
12529
Owen Andersone50ed302009-08-10 22:56:29 +000012530bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012531 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012532 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012533}
12534
Evan Cheng60c07e12006-07-05 22:17:51 +000012535/// isShuffleMaskLegal - Targets can use this to indicate that they only
12536/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12537/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12538/// are assumed to be legal.
12539bool
Eric Christopherfd179292009-08-27 18:07:15 +000012540X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012541 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012542 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012543 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012544 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012545
Nate Begemana09008b2009-10-19 02:17:23 +000012546 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012547 return (VT.getVectorNumElements() == 2 ||
12548 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12549 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012550 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012551 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012552 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12553 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012554 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012555 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12556 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12557 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12558 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012559}
12560
Dan Gohman7d8143f2008-04-09 20:09:42 +000012561bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012562X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012563 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012564 unsigned NumElts = VT.getVectorNumElements();
12565 // FIXME: This collection of masks seems suspect.
12566 if (NumElts == 2)
12567 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012568 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012569 return (isMOVLMask(Mask, VT) ||
12570 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012571 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12572 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012573 }
12574 return false;
12575}
12576
12577//===----------------------------------------------------------------------===//
12578// X86 Scheduler Hooks
12579//===----------------------------------------------------------------------===//
12580
Michael Liaobe02a902012-11-08 07:28:54 +000012581/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012582static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12583 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012584 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012585
12586 const BasicBlock *BB = MBB->getBasicBlock();
12587 MachineFunction::iterator I = MBB;
12588 ++I;
12589
12590 // For the v = xbegin(), we generate
12591 //
12592 // thisMBB:
12593 // xbegin sinkMBB
12594 //
12595 // mainMBB:
12596 // eax = -1
12597 //
12598 // sinkMBB:
12599 // v = eax
12600
12601 MachineBasicBlock *thisMBB = MBB;
12602 MachineFunction *MF = MBB->getParent();
12603 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12604 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12605 MF->insert(I, mainMBB);
12606 MF->insert(I, sinkMBB);
12607
12608 // Transfer the remainder of BB and its successor edges to sinkMBB.
12609 sinkMBB->splice(sinkMBB->begin(), MBB,
12610 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12611 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12612
12613 // thisMBB:
12614 // xbegin sinkMBB
12615 // # fallthrough to mainMBB
12616 // # abortion to sinkMBB
12617 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12618 thisMBB->addSuccessor(mainMBB);
12619 thisMBB->addSuccessor(sinkMBB);
12620
12621 // mainMBB:
12622 // EAX = -1
12623 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12624 mainMBB->addSuccessor(sinkMBB);
12625
12626 // sinkMBB:
12627 // EAX is live into the sinkMBB
12628 sinkMBB->addLiveIn(X86::EAX);
12629 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12630 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12631 .addReg(X86::EAX);
12632
12633 MI->eraseFromParent();
12634 return sinkMBB;
12635}
12636
Michael Liaob118a072012-09-20 03:06:15 +000012637// Get CMPXCHG opcode for the specified data type.
12638static unsigned getCmpXChgOpcode(EVT VT) {
12639 switch (VT.getSimpleVT().SimpleTy) {
12640 case MVT::i8: return X86::LCMPXCHG8;
12641 case MVT::i16: return X86::LCMPXCHG16;
12642 case MVT::i32: return X86::LCMPXCHG32;
12643 case MVT::i64: return X86::LCMPXCHG64;
12644 default:
12645 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012646 }
Michael Liaob118a072012-09-20 03:06:15 +000012647 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012648}
12649
Michael Liaob118a072012-09-20 03:06:15 +000012650// Get LOAD opcode for the specified data type.
12651static unsigned getLoadOpcode(EVT VT) {
12652 switch (VT.getSimpleVT().SimpleTy) {
12653 case MVT::i8: return X86::MOV8rm;
12654 case MVT::i16: return X86::MOV16rm;
12655 case MVT::i32: return X86::MOV32rm;
12656 case MVT::i64: return X86::MOV64rm;
12657 default:
12658 break;
12659 }
12660 llvm_unreachable("Invalid operand size!");
12661}
12662
12663// Get opcode of the non-atomic one from the specified atomic instruction.
12664static unsigned getNonAtomicOpcode(unsigned Opc) {
12665 switch (Opc) {
12666 case X86::ATOMAND8: return X86::AND8rr;
12667 case X86::ATOMAND16: return X86::AND16rr;
12668 case X86::ATOMAND32: return X86::AND32rr;
12669 case X86::ATOMAND64: return X86::AND64rr;
12670 case X86::ATOMOR8: return X86::OR8rr;
12671 case X86::ATOMOR16: return X86::OR16rr;
12672 case X86::ATOMOR32: return X86::OR32rr;
12673 case X86::ATOMOR64: return X86::OR64rr;
12674 case X86::ATOMXOR8: return X86::XOR8rr;
12675 case X86::ATOMXOR16: return X86::XOR16rr;
12676 case X86::ATOMXOR32: return X86::XOR32rr;
12677 case X86::ATOMXOR64: return X86::XOR64rr;
12678 }
12679 llvm_unreachable("Unhandled atomic-load-op opcode!");
12680}
12681
12682// Get opcode of the non-atomic one from the specified atomic instruction with
12683// extra opcode.
12684static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12685 unsigned &ExtraOpc) {
12686 switch (Opc) {
12687 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12688 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12689 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12690 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012691 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012692 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12693 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12694 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012695 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012696 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12697 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12698 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012699 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012700 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12701 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12702 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012703 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012704 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12705 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12706 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12707 }
12708 llvm_unreachable("Unhandled atomic-load-op opcode!");
12709}
12710
12711// Get opcode of the non-atomic one from the specified atomic instruction for
12712// 64-bit data type on 32-bit target.
12713static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12714 switch (Opc) {
12715 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12716 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12717 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12718 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12719 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12720 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012721 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12722 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12723 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12724 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012725 }
12726 llvm_unreachable("Unhandled atomic-load-op opcode!");
12727}
12728
12729// Get opcode of the non-atomic one from the specified atomic instruction for
12730// 64-bit data type on 32-bit target with extra opcode.
12731static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12732 unsigned &HiOpc,
12733 unsigned &ExtraOpc) {
12734 switch (Opc) {
12735 case X86::ATOMNAND6432:
12736 ExtraOpc = X86::NOT32r;
12737 HiOpc = X86::AND32rr;
12738 return X86::AND32rr;
12739 }
12740 llvm_unreachable("Unhandled atomic-load-op opcode!");
12741}
12742
12743// Get pseudo CMOV opcode from the specified data type.
12744static unsigned getPseudoCMOVOpc(EVT VT) {
12745 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012746 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012747 case MVT::i16: return X86::CMOV_GR16;
12748 case MVT::i32: return X86::CMOV_GR32;
12749 default:
12750 break;
12751 }
12752 llvm_unreachable("Unknown CMOV opcode!");
12753}
12754
12755// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12756// They will be translated into a spin-loop or compare-exchange loop from
12757//
12758// ...
12759// dst = atomic-fetch-op MI.addr, MI.val
12760// ...
12761//
12762// to
12763//
12764// ...
12765// EAX = LOAD MI.addr
12766// loop:
12767// t1 = OP MI.val, EAX
12768// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12769// JNE loop
12770// sink:
12771// dst = EAX
12772// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012773MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012774X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12775 MachineBasicBlock *MBB) const {
12776 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12777 DebugLoc DL = MI->getDebugLoc();
12778
12779 MachineFunction *MF = MBB->getParent();
12780 MachineRegisterInfo &MRI = MF->getRegInfo();
12781
12782 const BasicBlock *BB = MBB->getBasicBlock();
12783 MachineFunction::iterator I = MBB;
12784 ++I;
12785
12786 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12787 "Unexpected number of operands");
12788
12789 assert(MI->hasOneMemOperand() &&
12790 "Expected atomic-load-op to have one memoperand");
12791
12792 // Memory Reference
12793 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12794 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12795
12796 unsigned DstReg, SrcReg;
12797 unsigned MemOpndSlot;
12798
12799 unsigned CurOp = 0;
12800
12801 DstReg = MI->getOperand(CurOp++).getReg();
12802 MemOpndSlot = CurOp;
12803 CurOp += X86::AddrNumOperands;
12804 SrcReg = MI->getOperand(CurOp++).getReg();
12805
12806 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012807 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaob118a072012-09-20 03:06:15 +000012808 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12809
12810 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12811 unsigned LOADOpc = getLoadOpcode(VT);
12812
12813 // For the atomic load-arith operator, we generate
12814 //
12815 // thisMBB:
12816 // EAX = LOAD [MI.addr]
12817 // mainMBB:
12818 // t1 = OP MI.val, EAX
12819 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12820 // JNE mainMBB
12821 // sinkMBB:
12822
12823 MachineBasicBlock *thisMBB = MBB;
12824 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12825 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12826 MF->insert(I, mainMBB);
12827 MF->insert(I, sinkMBB);
12828
12829 MachineInstrBuilder MIB;
12830
12831 // Transfer the remainder of BB and its successor edges to sinkMBB.
12832 sinkMBB->splice(sinkMBB->begin(), MBB,
12833 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12834 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12835
12836 // thisMBB:
12837 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12838 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12839 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12840 MIB.setMemRefs(MMOBegin, MMOEnd);
12841
12842 thisMBB->addSuccessor(mainMBB);
12843
12844 // mainMBB:
12845 MachineBasicBlock *origMainMBB = mainMBB;
12846 mainMBB->addLiveIn(AccPhyReg);
12847
12848 // Copy AccPhyReg as it is used more than once.
12849 unsigned AccReg = MRI.createVirtualRegister(RC);
12850 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12851 .addReg(AccPhyReg);
12852
12853 unsigned t1 = MRI.createVirtualRegister(RC);
12854 unsigned Opc = MI->getOpcode();
12855 switch (Opc) {
12856 default:
12857 llvm_unreachable("Unhandled atomic-load-op opcode!");
12858 case X86::ATOMAND8:
12859 case X86::ATOMAND16:
12860 case X86::ATOMAND32:
12861 case X86::ATOMAND64:
12862 case X86::ATOMOR8:
12863 case X86::ATOMOR16:
12864 case X86::ATOMOR32:
12865 case X86::ATOMOR64:
12866 case X86::ATOMXOR8:
12867 case X86::ATOMXOR16:
12868 case X86::ATOMXOR32:
12869 case X86::ATOMXOR64: {
12870 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12871 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12872 .addReg(AccReg);
12873 break;
12874 }
12875 case X86::ATOMNAND8:
12876 case X86::ATOMNAND16:
12877 case X86::ATOMNAND32:
12878 case X86::ATOMNAND64: {
12879 unsigned t2 = MRI.createVirtualRegister(RC);
12880 unsigned NOTOpc;
12881 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12882 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12883 .addReg(AccReg);
12884 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12885 break;
12886 }
Michael Liao08382492012-09-21 03:00:17 +000012887 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012888 case X86::ATOMMAX16:
12889 case X86::ATOMMAX32:
12890 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012891 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012892 case X86::ATOMMIN16:
12893 case X86::ATOMMIN32:
12894 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012895 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012896 case X86::ATOMUMAX16:
12897 case X86::ATOMUMAX32:
12898 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012899 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012900 case X86::ATOMUMIN16:
12901 case X86::ATOMUMIN32:
12902 case X86::ATOMUMIN64: {
12903 unsigned CMPOpc;
12904 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12905
12906 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12907 .addReg(SrcReg)
12908 .addReg(AccReg);
12909
12910 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012911 if (VT != MVT::i8) {
12912 // Native support
12913 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12914 .addReg(SrcReg)
12915 .addReg(AccReg);
12916 } else {
12917 // Promote i8 to i32 to use CMOV32
12918 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12919 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12920 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12921 unsigned t2 = MRI.createVirtualRegister(RC32);
12922
12923 unsigned Undef = MRI.createVirtualRegister(RC32);
12924 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12925
12926 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12927 .addReg(Undef)
12928 .addReg(SrcReg)
12929 .addImm(X86::sub_8bit);
12930 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12931 .addReg(Undef)
12932 .addReg(AccReg)
12933 .addImm(X86::sub_8bit);
12934
12935 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12936 .addReg(SrcReg32)
12937 .addReg(AccReg32);
12938
12939 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12940 .addReg(t2, 0, X86::sub_8bit);
12941 }
Michael Liaob118a072012-09-20 03:06:15 +000012942 } else {
12943 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012944 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012945 "Invalid atomic-load-op transformation!");
12946 unsigned SelOpc = getPseudoCMOVOpc(VT);
12947 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12948 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12949 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12950 .addReg(SrcReg).addReg(AccReg)
12951 .addImm(CC);
12952 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12953 }
12954 break;
12955 }
12956 }
12957
12958 // Copy AccPhyReg back from virtual register.
12959 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12960 .addReg(AccReg);
12961
12962 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12963 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12964 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12965 MIB.addReg(t1);
12966 MIB.setMemRefs(MMOBegin, MMOEnd);
12967
12968 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12969
12970 mainMBB->addSuccessor(origMainMBB);
12971 mainMBB->addSuccessor(sinkMBB);
12972
12973 // sinkMBB:
12974 sinkMBB->addLiveIn(AccPhyReg);
12975
12976 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12977 TII->get(TargetOpcode::COPY), DstReg)
12978 .addReg(AccPhyReg);
12979
12980 MI->eraseFromParent();
12981 return sinkMBB;
12982}
12983
12984// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12985// instructions. They will be translated into a spin-loop or compare-exchange
12986// loop from
12987//
12988// ...
12989// dst = atomic-fetch-op MI.addr, MI.val
12990// ...
12991//
12992// to
12993//
12994// ...
12995// EAX = LOAD [MI.addr + 0]
12996// EDX = LOAD [MI.addr + 4]
12997// loop:
12998// EBX = OP MI.val.lo, EAX
12999// ECX = OP MI.val.hi, EDX
13000// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13001// JNE loop
13002// sink:
13003// dst = EDX:EAX
13004// ...
13005MachineBasicBlock *
13006X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13007 MachineBasicBlock *MBB) const {
13008 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13009 DebugLoc DL = MI->getDebugLoc();
13010
13011 MachineFunction *MF = MBB->getParent();
13012 MachineRegisterInfo &MRI = MF->getRegInfo();
13013
13014 const BasicBlock *BB = MBB->getBasicBlock();
13015 MachineFunction::iterator I = MBB;
13016 ++I;
13017
13018 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
13019 "Unexpected number of operands");
13020
13021 assert(MI->hasOneMemOperand() &&
13022 "Expected atomic-load-op32 to have one memoperand");
13023
13024 // Memory Reference
13025 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13026 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13027
13028 unsigned DstLoReg, DstHiReg;
13029 unsigned SrcLoReg, SrcHiReg;
13030 unsigned MemOpndSlot;
13031
13032 unsigned CurOp = 0;
13033
13034 DstLoReg = MI->getOperand(CurOp++).getReg();
13035 DstHiReg = MI->getOperand(CurOp++).getReg();
13036 MemOpndSlot = CurOp;
13037 CurOp += X86::AddrNumOperands;
13038 SrcLoReg = MI->getOperand(CurOp++).getReg();
13039 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013040
Craig Topperc9099502012-04-20 06:31:50 +000013041 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013042 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013043
Michael Liaob118a072012-09-20 03:06:15 +000013044 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13045 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013046
Michael Liaob118a072012-09-20 03:06:15 +000013047 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013048 //
Michael Liaob118a072012-09-20 03:06:15 +000013049 // thisMBB:
13050 // EAX = LOAD [MI.addr + 0]
13051 // EDX = LOAD [MI.addr + 4]
13052 // mainMBB:
13053 // EBX = OP MI.vallo, EAX
13054 // ECX = OP MI.valhi, EDX
13055 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
13056 // JNE mainMBB
13057 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000013058
Mon P Wang63307c32008-05-05 19:05:59 +000013059 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013060 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13061 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13062 MF->insert(I, mainMBB);
13063 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013064
Michael Liaob118a072012-09-20 03:06:15 +000013065 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013066
Michael Liaob118a072012-09-20 03:06:15 +000013067 // Transfer the remainder of BB and its successor edges to sinkMBB.
13068 sinkMBB->splice(sinkMBB->begin(), MBB,
13069 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13070 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013071
Michael Liaob118a072012-09-20 03:06:15 +000013072 // thisMBB:
13073 // Lo
13074 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
13075 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13076 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13077 MIB.setMemRefs(MMOBegin, MMOEnd);
13078 // Hi
13079 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
13080 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Evan Chenga395f4d2012-10-11 00:15:48 +000013081 if (i == X86::AddrDisp)
Michael Liaob118a072012-09-20 03:06:15 +000013082 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
Evan Chenga395f4d2012-10-11 00:15:48 +000013083 else
Michael Liaob118a072012-09-20 03:06:15 +000013084 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13085 }
13086 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000013087
Michael Liaob118a072012-09-20 03:06:15 +000013088 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013089
Michael Liaob118a072012-09-20 03:06:15 +000013090 // mainMBB:
13091 MachineBasicBlock *origMainMBB = mainMBB;
13092 mainMBB->addLiveIn(X86::EAX);
13093 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013094
Michael Liaob118a072012-09-20 03:06:15 +000013095 // Copy EDX:EAX as they are used more than once.
13096 unsigned LoReg = MRI.createVirtualRegister(RC);
13097 unsigned HiReg = MRI.createVirtualRegister(RC);
13098 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
13099 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000013100
Michael Liaob118a072012-09-20 03:06:15 +000013101 unsigned t1L = MRI.createVirtualRegister(RC);
13102 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000013103
Michael Liaob118a072012-09-20 03:06:15 +000013104 unsigned Opc = MI->getOpcode();
13105 switch (Opc) {
13106 default:
13107 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13108 case X86::ATOMAND6432:
13109 case X86::ATOMOR6432:
13110 case X86::ATOMXOR6432:
13111 case X86::ATOMADD6432:
13112 case X86::ATOMSUB6432: {
13113 unsigned HiOpc;
13114 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaodd3383f2012-11-12 06:49:17 +000013115 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(LoReg).addReg(SrcLoReg);
13116 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(HiReg).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013117 break;
13118 }
13119 case X86::ATOMNAND6432: {
13120 unsigned HiOpc, NOTOpc;
13121 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
13122 unsigned t2L = MRI.createVirtualRegister(RC);
13123 unsigned t2H = MRI.createVirtualRegister(RC);
13124 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
13125 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
13126 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
13127 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
13128 break;
13129 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013130 case X86::ATOMMAX6432:
13131 case X86::ATOMMIN6432:
13132 case X86::ATOMUMAX6432:
13133 case X86::ATOMUMIN6432: {
13134 unsigned HiOpc;
13135 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13136 unsigned cL = MRI.createVirtualRegister(RC8);
13137 unsigned cH = MRI.createVirtualRegister(RC8);
13138 unsigned cL32 = MRI.createVirtualRegister(RC);
13139 unsigned cH32 = MRI.createVirtualRegister(RC);
13140 unsigned cc = MRI.createVirtualRegister(RC);
13141 // cl := cmp src_lo, lo
13142 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13143 .addReg(SrcLoReg).addReg(LoReg);
13144 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13145 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13146 // ch := cmp src_hi, hi
13147 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
13148 .addReg(SrcHiReg).addReg(HiReg);
13149 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13150 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13151 // cc := if (src_hi == hi) ? cl : ch;
13152 if (Subtarget->hasCMov()) {
13153 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13154 .addReg(cH32).addReg(cL32);
13155 } else {
13156 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13157 .addReg(cH32).addReg(cL32)
13158 .addImm(X86::COND_E);
13159 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13160 }
13161 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13162 if (Subtarget->hasCMov()) {
13163 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1L)
13164 .addReg(SrcLoReg).addReg(LoReg);
13165 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t1H)
13166 .addReg(SrcHiReg).addReg(HiReg);
13167 } else {
13168 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1L)
13169 .addReg(SrcLoReg).addReg(LoReg)
13170 .addImm(X86::COND_NE);
13171 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13172 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t1H)
13173 .addReg(SrcHiReg).addReg(HiReg)
13174 .addImm(X86::COND_NE);
13175 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13176 }
13177 break;
13178 }
Michael Liaob118a072012-09-20 03:06:15 +000013179 case X86::ATOMSWAP6432: {
13180 unsigned HiOpc;
13181 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13182 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
13183 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
13184 break;
13185 }
13186 }
Mon P Wang63307c32008-05-05 19:05:59 +000013187
Michael Liaob118a072012-09-20 03:06:15 +000013188 // Copy EDX:EAX back from HiReg:LoReg
13189 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
13190 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
13191 // Copy ECX:EBX from t1H:t1L
13192 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
13193 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013194
Michael Liaob118a072012-09-20 03:06:15 +000013195 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
13196 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
13197 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
13198 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013199
Michael Liaob118a072012-09-20 03:06:15 +000013200 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013201
Michael Liaob118a072012-09-20 03:06:15 +000013202 mainMBB->addSuccessor(origMainMBB);
13203 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013204
Michael Liaob118a072012-09-20 03:06:15 +000013205 // sinkMBB:
13206 sinkMBB->addLiveIn(X86::EAX);
13207 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000013208
Michael Liaob118a072012-09-20 03:06:15 +000013209 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13210 TII->get(TargetOpcode::COPY), DstLoReg)
13211 .addReg(X86::EAX);
13212 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13213 TII->get(TargetOpcode::COPY), DstHiReg)
13214 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000013215
Michael Liaob118a072012-09-20 03:06:15 +000013216 MI->eraseFromParent();
13217 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013218}
13219
Eric Christopherf83a5de2009-08-27 18:08:16 +000013220// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013221// or XMM0_V32I8 in AVX all of this code can be replaced with that
13222// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013223static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13224 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013225 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013226 switch (MI->getOpcode()) {
13227 default: llvm_unreachable("illegal opcode!");
13228 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13229 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13230 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13231 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13232 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13233 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13234 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13235 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013236 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013237
Craig Topper8aae8dd2012-11-10 08:57:41 +000013238 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013239 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013240
Craig Topper52ea2452012-11-10 09:25:36 +000013241 unsigned NumArgs = MI->getNumOperands();
13242 for (unsigned i = 1; i < NumArgs; ++i) {
13243 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013244 if (!(Op.isReg() && Op.isImplicit()))
13245 MIB.addOperand(Op);
13246 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013247 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013248 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13249
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013250 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013251 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013252 .addReg(X86::XMM0);
13253
Dan Gohman14152b42010-07-06 20:24:04 +000013254 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013255 return BB;
13256}
13257
Craig Topper9c7ae012012-11-10 01:23:36 +000013258// FIXME: Custom handling because TableGen doesn't support multiple implicit
13259// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013260static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13261 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013262 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013263 switch (MI->getOpcode()) {
13264 default: llvm_unreachable("illegal opcode!");
13265 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13266 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13267 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13268 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13269 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13270 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13271 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13272 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013273 }
13274
Craig Topper8aae8dd2012-11-10 08:57:41 +000013275 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013276 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013277
Craig Topper52ea2452012-11-10 09:25:36 +000013278 unsigned NumArgs = MI->getNumOperands(); // remove the results
13279 for (unsigned i = 1; i < NumArgs; ++i) {
13280 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013281 if (!(Op.isReg() && Op.isImplicit()))
13282 MIB.addOperand(Op);
13283 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013284 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013285 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13286
13287 BuildMI(*BB, MI, dl,
13288 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13289 .addReg(X86::ECX);
13290
13291 MI->eraseFromParent();
13292 return BB;
13293}
13294
Craig Topper2da36912012-11-11 22:45:02 +000013295static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13296 const TargetInstrInfo *TII,
13297 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013298 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013299
Eric Christopher228232b2010-11-30 07:20:12 +000013300 // Address into RAX/EAX, other two args into ECX, EDX.
13301 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13302 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13303 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13304 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013305 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013306
Eric Christopher228232b2010-11-30 07:20:12 +000013307 unsigned ValOps = X86::AddrNumOperands;
13308 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13309 .addReg(MI->getOperand(ValOps).getReg());
13310 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13311 .addReg(MI->getOperand(ValOps+1).getReg());
13312
13313 // The instruction doesn't actually take any operands though.
13314 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013315
Eric Christopher228232b2010-11-30 07:20:12 +000013316 MI->eraseFromParent(); // The pseudo is gone now.
13317 return BB;
13318}
13319
13320MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013321X86TargetLowering::EmitVAARG64WithCustomInserter(
13322 MachineInstr *MI,
13323 MachineBasicBlock *MBB) const {
13324 // Emit va_arg instruction on X86-64.
13325
13326 // Operands to this pseudo-instruction:
13327 // 0 ) Output : destination address (reg)
13328 // 1-5) Input : va_list address (addr, i64mem)
13329 // 6 ) ArgSize : Size (in bytes) of vararg type
13330 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13331 // 8 ) Align : Alignment of type
13332 // 9 ) EFLAGS (implicit-def)
13333
13334 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13335 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13336
13337 unsigned DestReg = MI->getOperand(0).getReg();
13338 MachineOperand &Base = MI->getOperand(1);
13339 MachineOperand &Scale = MI->getOperand(2);
13340 MachineOperand &Index = MI->getOperand(3);
13341 MachineOperand &Disp = MI->getOperand(4);
13342 MachineOperand &Segment = MI->getOperand(5);
13343 unsigned ArgSize = MI->getOperand(6).getImm();
13344 unsigned ArgMode = MI->getOperand(7).getImm();
13345 unsigned Align = MI->getOperand(8).getImm();
13346
13347 // Memory Reference
13348 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13349 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13350 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13351
13352 // Machine Information
13353 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13354 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13355 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13356 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13357 DebugLoc DL = MI->getDebugLoc();
13358
13359 // struct va_list {
13360 // i32 gp_offset
13361 // i32 fp_offset
13362 // i64 overflow_area (address)
13363 // i64 reg_save_area (address)
13364 // }
13365 // sizeof(va_list) = 24
13366 // alignment(va_list) = 8
13367
13368 unsigned TotalNumIntRegs = 6;
13369 unsigned TotalNumXMMRegs = 8;
13370 bool UseGPOffset = (ArgMode == 1);
13371 bool UseFPOffset = (ArgMode == 2);
13372 unsigned MaxOffset = TotalNumIntRegs * 8 +
13373 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13374
13375 /* Align ArgSize to a multiple of 8 */
13376 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13377 bool NeedsAlign = (Align > 8);
13378
13379 MachineBasicBlock *thisMBB = MBB;
13380 MachineBasicBlock *overflowMBB;
13381 MachineBasicBlock *offsetMBB;
13382 MachineBasicBlock *endMBB;
13383
13384 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13385 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13386 unsigned OffsetReg = 0;
13387
13388 if (!UseGPOffset && !UseFPOffset) {
13389 // If we only pull from the overflow region, we don't create a branch.
13390 // We don't need to alter control flow.
13391 OffsetDestReg = 0; // unused
13392 OverflowDestReg = DestReg;
13393
13394 offsetMBB = NULL;
13395 overflowMBB = thisMBB;
13396 endMBB = thisMBB;
13397 } else {
13398 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13399 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13400 // If not, pull from overflow_area. (branch to overflowMBB)
13401 //
13402 // thisMBB
13403 // | .
13404 // | .
13405 // offsetMBB overflowMBB
13406 // | .
13407 // | .
13408 // endMBB
13409
13410 // Registers for the PHI in endMBB
13411 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13412 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13413
13414 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13415 MachineFunction *MF = MBB->getParent();
13416 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13417 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13418 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13419
13420 MachineFunction::iterator MBBIter = MBB;
13421 ++MBBIter;
13422
13423 // Insert the new basic blocks
13424 MF->insert(MBBIter, offsetMBB);
13425 MF->insert(MBBIter, overflowMBB);
13426 MF->insert(MBBIter, endMBB);
13427
13428 // Transfer the remainder of MBB and its successor edges to endMBB.
13429 endMBB->splice(endMBB->begin(), thisMBB,
13430 llvm::next(MachineBasicBlock::iterator(MI)),
13431 thisMBB->end());
13432 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13433
13434 // Make offsetMBB and overflowMBB successors of thisMBB
13435 thisMBB->addSuccessor(offsetMBB);
13436 thisMBB->addSuccessor(overflowMBB);
13437
13438 // endMBB is a successor of both offsetMBB and overflowMBB
13439 offsetMBB->addSuccessor(endMBB);
13440 overflowMBB->addSuccessor(endMBB);
13441
13442 // Load the offset value into a register
13443 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13444 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13445 .addOperand(Base)
13446 .addOperand(Scale)
13447 .addOperand(Index)
13448 .addDisp(Disp, UseFPOffset ? 4 : 0)
13449 .addOperand(Segment)
13450 .setMemRefs(MMOBegin, MMOEnd);
13451
13452 // Check if there is enough room left to pull this argument.
13453 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13454 .addReg(OffsetReg)
13455 .addImm(MaxOffset + 8 - ArgSizeA8);
13456
13457 // Branch to "overflowMBB" if offset >= max
13458 // Fall through to "offsetMBB" otherwise
13459 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13460 .addMBB(overflowMBB);
13461 }
13462
13463 // In offsetMBB, emit code to use the reg_save_area.
13464 if (offsetMBB) {
13465 assert(OffsetReg != 0);
13466
13467 // Read the reg_save_area address.
13468 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13469 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13470 .addOperand(Base)
13471 .addOperand(Scale)
13472 .addOperand(Index)
13473 .addDisp(Disp, 16)
13474 .addOperand(Segment)
13475 .setMemRefs(MMOBegin, MMOEnd);
13476
13477 // Zero-extend the offset
13478 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13479 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13480 .addImm(0)
13481 .addReg(OffsetReg)
13482 .addImm(X86::sub_32bit);
13483
13484 // Add the offset to the reg_save_area to get the final address.
13485 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13486 .addReg(OffsetReg64)
13487 .addReg(RegSaveReg);
13488
13489 // Compute the offset for the next argument
13490 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13491 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13492 .addReg(OffsetReg)
13493 .addImm(UseFPOffset ? 16 : 8);
13494
13495 // Store it back into the va_list.
13496 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13497 .addOperand(Base)
13498 .addOperand(Scale)
13499 .addOperand(Index)
13500 .addDisp(Disp, UseFPOffset ? 4 : 0)
13501 .addOperand(Segment)
13502 .addReg(NextOffsetReg)
13503 .setMemRefs(MMOBegin, MMOEnd);
13504
13505 // Jump to endMBB
13506 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13507 .addMBB(endMBB);
13508 }
13509
13510 //
13511 // Emit code to use overflow area
13512 //
13513
13514 // Load the overflow_area address into a register.
13515 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13516 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13517 .addOperand(Base)
13518 .addOperand(Scale)
13519 .addOperand(Index)
13520 .addDisp(Disp, 8)
13521 .addOperand(Segment)
13522 .setMemRefs(MMOBegin, MMOEnd);
13523
13524 // If we need to align it, do so. Otherwise, just copy the address
13525 // to OverflowDestReg.
13526 if (NeedsAlign) {
13527 // Align the overflow address
13528 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13529 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13530
13531 // aligned_addr = (addr + (align-1)) & ~(align-1)
13532 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13533 .addReg(OverflowAddrReg)
13534 .addImm(Align-1);
13535
13536 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13537 .addReg(TmpReg)
13538 .addImm(~(uint64_t)(Align-1));
13539 } else {
13540 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13541 .addReg(OverflowAddrReg);
13542 }
13543
13544 // Compute the next overflow address after this argument.
13545 // (the overflow address should be kept 8-byte aligned)
13546 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13547 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13548 .addReg(OverflowDestReg)
13549 .addImm(ArgSizeA8);
13550
13551 // Store the new overflow address.
13552 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13553 .addOperand(Base)
13554 .addOperand(Scale)
13555 .addOperand(Index)
13556 .addDisp(Disp, 8)
13557 .addOperand(Segment)
13558 .addReg(NextAddrReg)
13559 .setMemRefs(MMOBegin, MMOEnd);
13560
13561 // If we branched, emit the PHI to the front of endMBB.
13562 if (offsetMBB) {
13563 BuildMI(*endMBB, endMBB->begin(), DL,
13564 TII->get(X86::PHI), DestReg)
13565 .addReg(OffsetDestReg).addMBB(offsetMBB)
13566 .addReg(OverflowDestReg).addMBB(overflowMBB);
13567 }
13568
13569 // Erase the pseudo instruction
13570 MI->eraseFromParent();
13571
13572 return endMBB;
13573}
13574
13575MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013576X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13577 MachineInstr *MI,
13578 MachineBasicBlock *MBB) const {
13579 // Emit code to save XMM registers to the stack. The ABI says that the
13580 // number of registers to save is given in %al, so it's theoretically
13581 // possible to do an indirect jump trick to avoid saving all of them,
13582 // however this code takes a simpler approach and just executes all
13583 // of the stores if %al is non-zero. It's less code, and it's probably
13584 // easier on the hardware branch predictor, and stores aren't all that
13585 // expensive anyway.
13586
13587 // Create the new basic blocks. One block contains all the XMM stores,
13588 // and one block is the final destination regardless of whether any
13589 // stores were performed.
13590 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13591 MachineFunction *F = MBB->getParent();
13592 MachineFunction::iterator MBBIter = MBB;
13593 ++MBBIter;
13594 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13595 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13596 F->insert(MBBIter, XMMSaveMBB);
13597 F->insert(MBBIter, EndMBB);
13598
Dan Gohman14152b42010-07-06 20:24:04 +000013599 // Transfer the remainder of MBB and its successor edges to EndMBB.
13600 EndMBB->splice(EndMBB->begin(), MBB,
13601 llvm::next(MachineBasicBlock::iterator(MI)),
13602 MBB->end());
13603 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13604
Dan Gohmand6708ea2009-08-15 01:38:56 +000013605 // The original block will now fall through to the XMM save block.
13606 MBB->addSuccessor(XMMSaveMBB);
13607 // The XMMSaveMBB will fall through to the end block.
13608 XMMSaveMBB->addSuccessor(EndMBB);
13609
13610 // Now add the instructions.
13611 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13612 DebugLoc DL = MI->getDebugLoc();
13613
13614 unsigned CountReg = MI->getOperand(0).getReg();
13615 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13616 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13617
13618 if (!Subtarget->isTargetWin64()) {
13619 // If %al is 0, branch around the XMM save block.
13620 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013621 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013622 MBB->addSuccessor(EndMBB);
13623 }
13624
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013625 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013626 // In the XMM save block, save all the XMM argument registers.
13627 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13628 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013629 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013630 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013631 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013632 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013633 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013634 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013635 .addFrameIndex(RegSaveFrameIndex)
13636 .addImm(/*Scale=*/1)
13637 .addReg(/*IndexReg=*/0)
13638 .addImm(/*Disp=*/Offset)
13639 .addReg(/*Segment=*/0)
13640 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013641 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013642 }
13643
Dan Gohman14152b42010-07-06 20:24:04 +000013644 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013645
13646 return EndMBB;
13647}
Mon P Wang63307c32008-05-05 19:05:59 +000013648
Lang Hames6e3f7e42012-02-03 01:13:49 +000013649// The EFLAGS operand of SelectItr might be missing a kill marker
13650// because there were multiple uses of EFLAGS, and ISel didn't know
13651// which to mark. Figure out whether SelectItr should have had a
13652// kill marker, and set it if it should. Returns the correct kill
13653// marker value.
13654static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13655 MachineBasicBlock* BB,
13656 const TargetRegisterInfo* TRI) {
13657 // Scan forward through BB for a use/def of EFLAGS.
13658 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13659 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013660 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013661 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013662 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013663 if (mi.definesRegister(X86::EFLAGS))
13664 break; // Should have kill-flag - update below.
13665 }
13666
13667 // If we hit the end of the block, check whether EFLAGS is live into a
13668 // successor.
13669 if (miI == BB->end()) {
13670 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13671 sEnd = BB->succ_end();
13672 sItr != sEnd; ++sItr) {
13673 MachineBasicBlock* succ = *sItr;
13674 if (succ->isLiveIn(X86::EFLAGS))
13675 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013676 }
13677 }
13678
Lang Hames6e3f7e42012-02-03 01:13:49 +000013679 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13680 // out. SelectMI should have a kill flag on EFLAGS.
13681 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013682 return true;
13683}
13684
Evan Cheng60c07e12006-07-05 22:17:51 +000013685MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013686X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013687 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013688 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13689 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013690
Chris Lattner52600972009-09-02 05:57:00 +000013691 // To "insert" a SELECT_CC instruction, we actually have to insert the
13692 // diamond control-flow pattern. The incoming instruction knows the
13693 // destination vreg to set, the condition code register to branch on, the
13694 // true/false values to select between, and a branch opcode to use.
13695 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13696 MachineFunction::iterator It = BB;
13697 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013698
Chris Lattner52600972009-09-02 05:57:00 +000013699 // thisMBB:
13700 // ...
13701 // TrueVal = ...
13702 // cmpTY ccX, r1, r2
13703 // bCC copy1MBB
13704 // fallthrough --> copy0MBB
13705 MachineBasicBlock *thisMBB = BB;
13706 MachineFunction *F = BB->getParent();
13707 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13708 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013709 F->insert(It, copy0MBB);
13710 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013711
Bill Wendling730c07e2010-06-25 20:48:10 +000013712 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13713 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013714 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13715 if (!MI->killsRegister(X86::EFLAGS) &&
13716 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13717 copy0MBB->addLiveIn(X86::EFLAGS);
13718 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013719 }
13720
Dan Gohman14152b42010-07-06 20:24:04 +000013721 // Transfer the remainder of BB and its successor edges to sinkMBB.
13722 sinkMBB->splice(sinkMBB->begin(), BB,
13723 llvm::next(MachineBasicBlock::iterator(MI)),
13724 BB->end());
13725 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13726
13727 // Add the true and fallthrough blocks as its successors.
13728 BB->addSuccessor(copy0MBB);
13729 BB->addSuccessor(sinkMBB);
13730
13731 // Create the conditional branch instruction.
13732 unsigned Opc =
13733 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
13734 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
13735
Chris Lattner52600972009-09-02 05:57:00 +000013736 // copy0MBB:
13737 // %FalseValue = ...
13738 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000013739 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000013740
Chris Lattner52600972009-09-02 05:57:00 +000013741 // sinkMBB:
13742 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
13743 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000013744 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13745 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000013746 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
13747 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
13748
Dan Gohman14152b42010-07-06 20:24:04 +000013749 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000013750 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000013751}
13752
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013753MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013754X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
13755 bool Is64Bit) const {
13756 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13757 DebugLoc DL = MI->getDebugLoc();
13758 MachineFunction *MF = BB->getParent();
13759 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13760
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013761 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013762
13763 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
13764 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
13765
13766 // BB:
13767 // ... [Till the alloca]
13768 // If stacklet is not large enough, jump to mallocMBB
13769 //
13770 // bumpMBB:
13771 // Allocate by subtracting from RSP
13772 // Jump to continueMBB
13773 //
13774 // mallocMBB:
13775 // Allocate by call to runtime
13776 //
13777 // continueMBB:
13778 // ...
13779 // [rest of original BB]
13780 //
13781
13782 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13783 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13784 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13785
13786 MachineRegisterInfo &MRI = MF->getRegInfo();
13787 const TargetRegisterClass *AddrRegClass =
13788 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
13789
13790 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13791 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
13792 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000013793 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013794 sizeVReg = MI->getOperand(1).getReg(),
13795 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
13796
13797 MachineFunction::iterator MBBIter = BB;
13798 ++MBBIter;
13799
13800 MF->insert(MBBIter, bumpMBB);
13801 MF->insert(MBBIter, mallocMBB);
13802 MF->insert(MBBIter, continueMBB);
13803
13804 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13805 (MachineBasicBlock::iterator(MI)), BB->end());
13806 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13807
13808 // Add code to the main basic block to check if the stack limit has been hit,
13809 // and if so, jump to mallocMBB otherwise to bumpMBB.
13810 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013811 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013812 .addReg(tmpSPVReg).addReg(sizeVReg);
13813 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013814 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013815 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013816 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13817
13818 // bumpMBB simply decreases the stack pointer, since we know the current
13819 // stacklet has enough space.
13820 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013821 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013822 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013823 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013824 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13825
13826 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013827 const uint32_t *RegMask =
13828 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013829 if (Is64Bit) {
13830 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13831 .addReg(sizeVReg);
13832 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013833 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013834 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013835 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013836 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013837 } else {
13838 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13839 .addImm(12);
13840 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13841 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013842 .addExternalSymbol("__morestack_allocate_stack_space")
13843 .addRegMask(RegMask)
13844 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013845 }
13846
13847 if (!Is64Bit)
13848 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13849 .addImm(16);
13850
13851 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13852 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13853 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13854
13855 // Set up the CFG correctly.
13856 BB->addSuccessor(bumpMBB);
13857 BB->addSuccessor(mallocMBB);
13858 mallocMBB->addSuccessor(continueMBB);
13859 bumpMBB->addSuccessor(continueMBB);
13860
13861 // Take care of the PHI nodes.
13862 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13863 MI->getOperand(0).getReg())
13864 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13865 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13866
13867 // Delete the original pseudo instruction.
13868 MI->eraseFromParent();
13869
13870 // And we're done.
13871 return continueMBB;
13872}
13873
13874MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013875X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013876 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013877 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13878 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013879
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013880 assert(!Subtarget->isTargetEnvMacho());
13881
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013882 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13883 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013884
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013885 if (Subtarget->isTargetWin64()) {
13886 if (Subtarget->isTargetCygMing()) {
13887 // ___chkstk(Mingw64):
13888 // Clobbers R10, R11, RAX and EFLAGS.
13889 // Updates RSP.
13890 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13891 .addExternalSymbol("___chkstk")
13892 .addReg(X86::RAX, RegState::Implicit)
13893 .addReg(X86::RSP, RegState::Implicit)
13894 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13895 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13896 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13897 } else {
13898 // __chkstk(MSVCRT): does not update stack pointer.
13899 // Clobbers R10, R11 and EFLAGS.
13900 // FIXME: RAX(allocated size) might be reused and not killed.
13901 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13902 .addExternalSymbol("__chkstk")
13903 .addReg(X86::RAX, RegState::Implicit)
13904 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13905 // RAX has the offset to subtracted from RSP.
13906 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13907 .addReg(X86::RSP)
13908 .addReg(X86::RAX);
13909 }
13910 } else {
13911 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013912 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13913
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013914 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13915 .addExternalSymbol(StackProbeSymbol)
13916 .addReg(X86::EAX, RegState::Implicit)
13917 .addReg(X86::ESP, RegState::Implicit)
13918 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13919 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13920 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13921 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013922
Dan Gohman14152b42010-07-06 20:24:04 +000013923 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013924 return BB;
13925}
Chris Lattner52600972009-09-02 05:57:00 +000013926
13927MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013928X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13929 MachineBasicBlock *BB) const {
13930 // This is pretty easy. We're taking the value that we received from
13931 // our load from the relocation, sticking it in either RDI (x86-64)
13932 // or EAX and doing an indirect call. The return value will then
13933 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013934 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013935 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013936 DebugLoc DL = MI->getDebugLoc();
13937 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013938
13939 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013940 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013941
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013942 // Get a register mask for the lowered call.
13943 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13944 // proper register mask.
13945 const uint32_t *RegMask =
13946 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013947 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013948 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13949 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013950 .addReg(X86::RIP)
13951 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013952 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013953 MI->getOperand(3).getTargetFlags())
13954 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013955 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013956 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013957 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013958 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013959 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13960 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013961 .addReg(0)
13962 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013963 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013964 MI->getOperand(3).getTargetFlags())
13965 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013966 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013967 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013968 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013969 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013970 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13971 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013972 .addReg(TII->getGlobalBaseReg(F))
13973 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013974 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013975 MI->getOperand(3).getTargetFlags())
13976 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013977 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013978 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013979 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013980 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013981
Dan Gohman14152b42010-07-06 20:24:04 +000013982 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013983 return BB;
13984}
13985
13986MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000013987X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
13988 MachineBasicBlock *MBB) const {
13989 DebugLoc DL = MI->getDebugLoc();
13990 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13991
13992 MachineFunction *MF = MBB->getParent();
13993 MachineRegisterInfo &MRI = MF->getRegInfo();
13994
13995 const BasicBlock *BB = MBB->getBasicBlock();
13996 MachineFunction::iterator I = MBB;
13997 ++I;
13998
13999 // Memory Reference
14000 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14001 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14002
14003 unsigned DstReg;
14004 unsigned MemOpndSlot = 0;
14005
14006 unsigned CurOp = 0;
14007
14008 DstReg = MI->getOperand(CurOp++).getReg();
14009 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14010 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14011 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14012 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14013
14014 MemOpndSlot = CurOp;
14015
14016 MVT PVT = getPointerTy();
14017 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14018 "Invalid Pointer Size!");
14019
14020 // For v = setjmp(buf), we generate
14021 //
14022 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014023 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014024 // SjLjSetup restoreMBB
14025 //
14026 // mainMBB:
14027 // v_main = 0
14028 //
14029 // sinkMBB:
14030 // v = phi(main, restore)
14031 //
14032 // restoreMBB:
14033 // v_restore = 1
14034
14035 MachineBasicBlock *thisMBB = MBB;
14036 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14037 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14038 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14039 MF->insert(I, mainMBB);
14040 MF->insert(I, sinkMBB);
14041 MF->push_back(restoreMBB);
14042
14043 MachineInstrBuilder MIB;
14044
14045 // Transfer the remainder of BB and its successor edges to sinkMBB.
14046 sinkMBB->splice(sinkMBB->begin(), MBB,
14047 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14048 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14049
14050 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014051 unsigned PtrStoreOpc = 0;
14052 unsigned LabelReg = 0;
14053 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14054 Reloc::Model RM = getTargetMachine().getRelocationModel();
14055 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14056 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014057
Michael Liao281ae5a2012-10-17 02:22:27 +000014058 // Prepare IP either in reg or imm.
14059 if (!UseImmLabel) {
14060 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14061 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14062 LabelReg = MRI.createVirtualRegister(PtrRC);
14063 if (Subtarget->is64Bit()) {
14064 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14065 .addReg(X86::RIP)
14066 .addImm(0)
14067 .addReg(0)
14068 .addMBB(restoreMBB)
14069 .addReg(0);
14070 } else {
14071 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14072 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14073 .addReg(XII->getGlobalBaseReg(MF))
14074 .addImm(0)
14075 .addReg(0)
14076 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14077 .addReg(0);
14078 }
14079 } else
14080 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014081 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014082 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014083 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14084 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014085 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014086 else
14087 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14088 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014089 if (!UseImmLabel)
14090 MIB.addReg(LabelReg);
14091 else
14092 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014093 MIB.setMemRefs(MMOBegin, MMOEnd);
14094 // Setup
14095 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14096 .addMBB(restoreMBB);
14097 MIB.addRegMask(RegInfo->getNoPreservedMask());
14098 thisMBB->addSuccessor(mainMBB);
14099 thisMBB->addSuccessor(restoreMBB);
14100
14101 // mainMBB:
14102 // EAX = 0
14103 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14104 mainMBB->addSuccessor(sinkMBB);
14105
14106 // sinkMBB:
14107 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14108 TII->get(X86::PHI), DstReg)
14109 .addReg(mainDstReg).addMBB(mainMBB)
14110 .addReg(restoreDstReg).addMBB(restoreMBB);
14111
14112 // restoreMBB:
14113 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14114 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14115 restoreMBB->addSuccessor(sinkMBB);
14116
14117 MI->eraseFromParent();
14118 return sinkMBB;
14119}
14120
14121MachineBasicBlock *
14122X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14123 MachineBasicBlock *MBB) const {
14124 DebugLoc DL = MI->getDebugLoc();
14125 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14126
14127 MachineFunction *MF = MBB->getParent();
14128 MachineRegisterInfo &MRI = MF->getRegInfo();
14129
14130 // Memory Reference
14131 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14132 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14133
14134 MVT PVT = getPointerTy();
14135 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14136 "Invalid Pointer Size!");
14137
14138 const TargetRegisterClass *RC =
14139 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14140 unsigned Tmp = MRI.createVirtualRegister(RC);
14141 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14142 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14143 unsigned SP = RegInfo->getStackRegister();
14144
14145 MachineInstrBuilder MIB;
14146
Michael Liao281ae5a2012-10-17 02:22:27 +000014147 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14148 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014149
14150 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14151 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14152
14153 // Reload FP
14154 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14155 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14156 MIB.addOperand(MI->getOperand(i));
14157 MIB.setMemRefs(MMOBegin, MMOEnd);
14158 // Reload IP
14159 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14160 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14161 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014162 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014163 else
14164 MIB.addOperand(MI->getOperand(i));
14165 }
14166 MIB.setMemRefs(MMOBegin, MMOEnd);
14167 // Reload SP
14168 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14169 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14170 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014171 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014172 else
14173 MIB.addOperand(MI->getOperand(i));
14174 }
14175 MIB.setMemRefs(MMOBegin, MMOEnd);
14176 // Jump
14177 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14178
14179 MI->eraseFromParent();
14180 return MBB;
14181}
14182
14183MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014184X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014185 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014186 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014187 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014188 case X86::TAILJMPd64:
14189 case X86::TAILJMPr64:
14190 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014191 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014192 case X86::TCRETURNdi64:
14193 case X86::TCRETURNri64:
14194 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014195 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014196 case X86::WIN_ALLOCA:
14197 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014198 case X86::SEG_ALLOCA_32:
14199 return EmitLoweredSegAlloca(MI, BB, false);
14200 case X86::SEG_ALLOCA_64:
14201 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014202 case X86::TLSCall_32:
14203 case X86::TLSCall_64:
14204 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014205 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014206 case X86::CMOV_FR32:
14207 case X86::CMOV_FR64:
14208 case X86::CMOV_V4F32:
14209 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014210 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014211 case X86::CMOV_V8F32:
14212 case X86::CMOV_V4F64:
14213 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014214 case X86::CMOV_GR16:
14215 case X86::CMOV_GR32:
14216 case X86::CMOV_RFP32:
14217 case X86::CMOV_RFP64:
14218 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014219 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014220
Dale Johannesen849f2142007-07-03 00:53:03 +000014221 case X86::FP32_TO_INT16_IN_MEM:
14222 case X86::FP32_TO_INT32_IN_MEM:
14223 case X86::FP32_TO_INT64_IN_MEM:
14224 case X86::FP64_TO_INT16_IN_MEM:
14225 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014226 case X86::FP64_TO_INT64_IN_MEM:
14227 case X86::FP80_TO_INT16_IN_MEM:
14228 case X86::FP80_TO_INT32_IN_MEM:
14229 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014230 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14231 DebugLoc DL = MI->getDebugLoc();
14232
Evan Cheng60c07e12006-07-05 22:17:51 +000014233 // Change the floating point control register to use "round towards zero"
14234 // mode when truncating to an integer value.
14235 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014236 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014237 addFrameReference(BuildMI(*BB, MI, DL,
14238 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014239
14240 // Load the old value of the high byte of the control word...
14241 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014242 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014243 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014244 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014245
14246 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014247 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014248 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014249
14250 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014251 addFrameReference(BuildMI(*BB, MI, DL,
14252 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014253
14254 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014255 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014256 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014257
14258 // Get the X86 opcode to use.
14259 unsigned Opc;
14260 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014261 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014262 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14263 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14264 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14265 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14266 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14267 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014268 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14269 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14270 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014271 }
14272
14273 X86AddressMode AM;
14274 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014275 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014276 AM.BaseType = X86AddressMode::RegBase;
14277 AM.Base.Reg = Op.getReg();
14278 } else {
14279 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014280 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014281 }
14282 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014283 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014284 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014285 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014286 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014287 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014288 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014289 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014290 AM.GV = Op.getGlobal();
14291 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014292 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014293 }
Dan Gohman14152b42010-07-06 20:24:04 +000014294 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014295 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014296
14297 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014298 addFrameReference(BuildMI(*BB, MI, DL,
14299 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014300
Dan Gohman14152b42010-07-06 20:24:04 +000014301 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014302 return BB;
14303 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014304 // String/text processing lowering.
14305 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014306 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014307 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014308 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014309 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014310 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014311 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014312 case X86::VPCMPESTRM128MEM:
14313 assert(Subtarget->hasSSE42() &&
14314 "Target must have SSE4.2 or AVX features enabled");
14315 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014316
14317 // String/text processing lowering.
14318 case X86::PCMPISTRIREG:
14319 case X86::VPCMPISTRIREG:
14320 case X86::PCMPISTRIMEM:
14321 case X86::VPCMPISTRIMEM:
14322 case X86::PCMPESTRIREG:
14323 case X86::VPCMPESTRIREG:
14324 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014325 case X86::VPCMPESTRIMEM:
14326 assert(Subtarget->hasSSE42() &&
14327 "Target must have SSE4.2 or AVX features enabled");
14328 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014329
Craig Topper8aae8dd2012-11-10 08:57:41 +000014330 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014331 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014332 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014333
Michael Liaobe02a902012-11-08 07:28:54 +000014334 // xbegin
14335 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014336 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014337
Craig Topper8aae8dd2012-11-10 08:57:41 +000014338 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014339 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014340 case X86::ATOMAND16:
14341 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014342 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014343 // Fall through
14344 case X86::ATOMOR8:
14345 case X86::ATOMOR16:
14346 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014347 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014348 // Fall through
14349 case X86::ATOMXOR16:
14350 case X86::ATOMXOR8:
14351 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014352 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014353 // Fall through
14354 case X86::ATOMNAND8:
14355 case X86::ATOMNAND16:
14356 case X86::ATOMNAND32:
14357 case X86::ATOMNAND64:
14358 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014359 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014360 case X86::ATOMMAX16:
14361 case X86::ATOMMAX32:
14362 case X86::ATOMMAX64:
14363 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014364 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014365 case X86::ATOMMIN16:
14366 case X86::ATOMMIN32:
14367 case X86::ATOMMIN64:
14368 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014369 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014370 case X86::ATOMUMAX16:
14371 case X86::ATOMUMAX32:
14372 case X86::ATOMUMAX64:
14373 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014374 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014375 case X86::ATOMUMIN16:
14376 case X86::ATOMUMIN32:
14377 case X86::ATOMUMIN64:
14378 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014379
14380 // This group does 64-bit operations on a 32-bit host.
14381 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014382 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014383 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014384 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014385 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014386 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014387 case X86::ATOMMAX6432:
14388 case X86::ATOMMIN6432:
14389 case X86::ATOMUMAX6432:
14390 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014391 case X86::ATOMSWAP6432:
14392 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014393
Dan Gohmand6708ea2009-08-15 01:38:56 +000014394 case X86::VASTART_SAVE_XMM_REGS:
14395 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014396
14397 case X86::VAARG_64:
14398 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014399
14400 case X86::EH_SjLj_SetJmp32:
14401 case X86::EH_SjLj_SetJmp64:
14402 return emitEHSjLjSetJmp(MI, BB);
14403
14404 case X86::EH_SjLj_LongJmp32:
14405 case X86::EH_SjLj_LongJmp64:
14406 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014407 }
14408}
14409
14410//===----------------------------------------------------------------------===//
14411// X86 Optimization Hooks
14412//===----------------------------------------------------------------------===//
14413
Dan Gohman475871a2008-07-27 21:46:04 +000014414void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014415 APInt &KnownZero,
14416 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014417 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014418 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014419 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014420 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014421 assert((Opc >= ISD::BUILTIN_OP_END ||
14422 Opc == ISD::INTRINSIC_WO_CHAIN ||
14423 Opc == ISD::INTRINSIC_W_CHAIN ||
14424 Opc == ISD::INTRINSIC_VOID) &&
14425 "Should use MaskedValueIsZero if you don't know whether Op"
14426 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014427
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014428 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014429 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014430 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014431 case X86ISD::ADD:
14432 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014433 case X86ISD::ADC:
14434 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014435 case X86ISD::SMUL:
14436 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014437 case X86ISD::INC:
14438 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014439 case X86ISD::OR:
14440 case X86ISD::XOR:
14441 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014442 // These nodes' second result is a boolean.
14443 if (Op.getResNo() == 0)
14444 break;
14445 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014446 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014447 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014448 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014449 case ISD::INTRINSIC_WO_CHAIN: {
14450 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14451 unsigned NumLoBits = 0;
14452 switch (IntId) {
14453 default: break;
14454 case Intrinsic::x86_sse_movmsk_ps:
14455 case Intrinsic::x86_avx_movmsk_ps_256:
14456 case Intrinsic::x86_sse2_movmsk_pd:
14457 case Intrinsic::x86_avx_movmsk_pd_256:
14458 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014459 case Intrinsic::x86_sse2_pmovmskb_128:
14460 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014461 // High bits of movmskp{s|d}, pmovmskb are known zero.
14462 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014463 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014464 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14465 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14466 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14467 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14468 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14469 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014470 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014471 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014472 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014473 break;
14474 }
14475 }
14476 break;
14477 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014478 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014479}
Chris Lattner259e97c2006-01-31 19:43:35 +000014480
Owen Andersonbc146b02010-09-21 20:42:50 +000014481unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14482 unsigned Depth) const {
14483 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14484 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14485 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014486
Owen Andersonbc146b02010-09-21 20:42:50 +000014487 // Fallback case.
14488 return 1;
14489}
14490
Evan Cheng206ee9d2006-07-07 08:33:52 +000014491/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014492/// node is a GlobalAddress + offset.
14493bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014494 const GlobalValue* &GA,
14495 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014496 if (N->getOpcode() == X86ISD::Wrapper) {
14497 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014498 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014499 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014500 return true;
14501 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014502 }
Evan Chengad4196b2008-05-12 19:56:52 +000014503 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014504}
14505
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014506/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14507/// same as extracting the high 128-bit part of 256-bit vector and then
14508/// inserting the result into the low part of a new 256-bit vector
14509static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14510 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014511 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014512
14513 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014514 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014515 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14516 SVOp->getMaskElt(j) >= 0)
14517 return false;
14518
14519 return true;
14520}
14521
14522/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14523/// same as extracting the low 128-bit part of 256-bit vector and then
14524/// inserting the result into the high part of a new 256-bit vector
14525static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14526 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014527 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014528
14529 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014530 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014531 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14532 SVOp->getMaskElt(j) >= 0)
14533 return false;
14534
14535 return true;
14536}
14537
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014538/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14539static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014540 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014541 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014542 DebugLoc dl = N->getDebugLoc();
14543 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14544 SDValue V1 = SVOp->getOperand(0);
14545 SDValue V2 = SVOp->getOperand(1);
14546 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014547 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014548
14549 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14550 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14551 //
14552 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014553 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014554 // V UNDEF BUILD_VECTOR UNDEF
14555 // \ / \ /
14556 // CONCAT_VECTOR CONCAT_VECTOR
14557 // \ /
14558 // \ /
14559 // RESULT: V + zero extended
14560 //
14561 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14562 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14563 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14564 return SDValue();
14565
14566 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14567 return SDValue();
14568
14569 // To match the shuffle mask, the first half of the mask should
14570 // be exactly the first vector, and all the rest a splat with the
14571 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014572 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014573 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14574 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14575 return SDValue();
14576
Chad Rosier3d1161e2012-01-03 21:05:52 +000014577 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14578 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014579 if (Ld->hasNUsesOfValue(1, 0)) {
14580 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14581 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14582 SDValue ResNode =
14583 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14584 Ld->getMemoryVT(),
14585 Ld->getPointerInfo(),
14586 Ld->getAlignment(),
14587 false/*isVolatile*/, true/*ReadMem*/,
14588 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014589
14590 // Make sure the newly-created LOAD is in the same position as Ld in
14591 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14592 // and update uses of Ld's output chain to use the TokenFactor.
14593 if (Ld->hasAnyUseOfValue(1)) {
14594 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14595 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14596 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14597 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14598 SDValue(ResNode.getNode(), 1));
14599 }
14600
Chad Rosier42726832012-05-07 18:47:44 +000014601 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14602 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014603 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014604
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014605 // Emit a zeroed vector and insert the desired subvector on its
14606 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014607 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014608 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014609 return DCI.CombineTo(N, InsV);
14610 }
14611
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014612 //===--------------------------------------------------------------------===//
14613 // Combine some shuffles into subvector extracts and inserts:
14614 //
14615
14616 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14617 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014618 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14619 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014620 return DCI.CombineTo(N, InsV);
14621 }
14622
14623 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14624 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014625 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14626 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014627 return DCI.CombineTo(N, InsV);
14628 }
14629
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014630 return SDValue();
14631}
14632
14633/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014634static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014635 TargetLowering::DAGCombinerInfo &DCI,
14636 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014637 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014638 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014639
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014640 // Don't create instructions with illegal types after legalize types has run.
14641 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14642 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14643 return SDValue();
14644
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014645 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014646 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014647 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014648 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014649
14650 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014651 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014652 return SDValue();
14653
14654 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14655 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14656 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014657 SmallVector<SDValue, 16> Elts;
14658 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014659 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014660
Nate Begemanfdea31a2010-03-24 20:49:50 +000014661 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014662}
Evan Chengd880b972008-05-09 21:53:03 +000014663
Craig Topper55b24052012-09-11 06:15:32 +000014664/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014665/// a sequence of vector shuffle operations.
14666/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014667static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14668 TargetLowering::DAGCombinerInfo &DCI,
14669 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014670 return SDValue();
14671}
14672
Craig Topper89f4e662012-03-20 07:17:59 +000014673/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14674/// specific shuffle of a load can be folded into a single element load.
14675/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14676/// shuffles have been customed lowered so we need to handle those here.
14677static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14678 TargetLowering::DAGCombinerInfo &DCI) {
14679 if (DCI.isBeforeLegalizeOps())
14680 return SDValue();
14681
14682 SDValue InVec = N->getOperand(0);
14683 SDValue EltNo = N->getOperand(1);
14684
14685 if (!isa<ConstantSDNode>(EltNo))
14686 return SDValue();
14687
14688 EVT VT = InVec.getValueType();
14689
14690 bool HasShuffleIntoBitcast = false;
14691 if (InVec.getOpcode() == ISD::BITCAST) {
14692 // Don't duplicate a load with other uses.
14693 if (!InVec.hasOneUse())
14694 return SDValue();
14695 EVT BCVT = InVec.getOperand(0).getValueType();
14696 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14697 return SDValue();
14698 InVec = InVec.getOperand(0);
14699 HasShuffleIntoBitcast = true;
14700 }
14701
14702 if (!isTargetShuffle(InVec.getOpcode()))
14703 return SDValue();
14704
14705 // Don't duplicate a load with other uses.
14706 if (!InVec.hasOneUse())
14707 return SDValue();
14708
14709 SmallVector<int, 16> ShuffleMask;
14710 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014711 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14712 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014713 return SDValue();
14714
14715 // Select the input vector, guarding against out of range extract vector.
14716 unsigned NumElems = VT.getVectorNumElements();
14717 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14718 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14719 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14720 : InVec.getOperand(1);
14721
14722 // If inputs to shuffle are the same for both ops, then allow 2 uses
14723 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14724
14725 if (LdNode.getOpcode() == ISD::BITCAST) {
14726 // Don't duplicate a load with other uses.
14727 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14728 return SDValue();
14729
14730 AllowedUses = 1; // only allow 1 load use if we have a bitcast
14731 LdNode = LdNode.getOperand(0);
14732 }
14733
14734 if (!ISD::isNormalLoad(LdNode.getNode()))
14735 return SDValue();
14736
14737 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
14738
14739 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
14740 return SDValue();
14741
14742 if (HasShuffleIntoBitcast) {
14743 // If there's a bitcast before the shuffle, check if the load type and
14744 // alignment is valid.
14745 unsigned Align = LN0->getAlignment();
14746 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000014747 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000014748 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
14749
14750 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
14751 return SDValue();
14752 }
14753
14754 // All checks match so transform back to vector_shuffle so that DAG combiner
14755 // can finish the job
14756 DebugLoc dl = N->getDebugLoc();
14757
14758 // Create shuffle node taking into account the case that its a unary shuffle
14759 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
14760 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
14761 InVec.getOperand(0), Shuffle,
14762 &ShuffleMask[0]);
14763 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
14764 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
14765 EltNo);
14766}
14767
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000014768/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
14769/// generation and convert it from being a bunch of shuffles and extracts
14770/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014771static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000014772 TargetLowering::DAGCombinerInfo &DCI) {
14773 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
14774 if (NewOp.getNode())
14775 return NewOp;
14776
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014777 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000014778 // Detect whether we are trying to convert from mmx to i32 and the bitcast
14779 // from mmx to v2i32 has a single usage.
14780 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
14781 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
14782 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
14783 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
14784 N->getValueType(0),
14785 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014786
14787 // Only operate on vectors of 4 elements, where the alternative shuffling
14788 // gets to be more expensive.
14789 if (InputVector.getValueType() != MVT::v4i32)
14790 return SDValue();
14791
14792 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
14793 // single use which is a sign-extend or zero-extend, and all elements are
14794 // used.
14795 SmallVector<SDNode *, 4> Uses;
14796 unsigned ExtractedElements = 0;
14797 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
14798 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
14799 if (UI.getUse().getResNo() != InputVector.getResNo())
14800 return SDValue();
14801
14802 SDNode *Extract = *UI;
14803 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14804 return SDValue();
14805
14806 if (Extract->getValueType(0) != MVT::i32)
14807 return SDValue();
14808 if (!Extract->hasOneUse())
14809 return SDValue();
14810 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
14811 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
14812 return SDValue();
14813 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
14814 return SDValue();
14815
14816 // Record which element was extracted.
14817 ExtractedElements |=
14818 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
14819
14820 Uses.push_back(Extract);
14821 }
14822
14823 // If not all the elements were used, this may not be worthwhile.
14824 if (ExtractedElements != 15)
14825 return SDValue();
14826
14827 // Ok, we've now decided to do the transformation.
14828 DebugLoc dl = InputVector.getDebugLoc();
14829
14830 // Store the value to a temporary stack slot.
14831 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000014832 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
14833 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014834
14835 // Replace each use (extract) with a load of the appropriate element.
14836 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
14837 UE = Uses.end(); UI != UE; ++UI) {
14838 SDNode *Extract = *UI;
14839
Nadav Rotem86694292011-05-17 08:31:57 +000014840 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014841 SDValue Idx = Extract->getOperand(1);
14842 unsigned EltSize =
14843 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
14844 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000014845 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014846 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
14847
Nadav Rotem86694292011-05-17 08:31:57 +000014848 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014849 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014850
14851 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000014852 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000014853 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014854 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014855
14856 // Replace the exact with the load.
14857 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
14858 }
14859
14860 // The replacement was made in place; don't return anything.
14861 return SDValue();
14862}
14863
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000014864/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
14865static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
14866 SDValue RHS, SelectionDAG &DAG,
14867 const X86Subtarget *Subtarget) {
14868 if (!VT.isVector())
14869 return 0;
14870
14871 switch (VT.getSimpleVT().SimpleTy) {
14872 default: return 0;
14873 case MVT::v32i8:
14874 case MVT::v16i16:
14875 case MVT::v8i32:
14876 if (!Subtarget->hasAVX2())
14877 return 0;
14878 case MVT::v16i8:
14879 case MVT::v8i16:
14880 case MVT::v4i32:
14881 if (!Subtarget->hasSSE2())
14882 return 0;
14883 }
14884
14885 // SSE2 has only a small subset of the operations.
14886 bool hasUnsigned = Subtarget->hasSSE41() ||
14887 (Subtarget->hasSSE2() && VT == MVT::v16i8);
14888 bool hasSigned = Subtarget->hasSSE41() ||
14889 (Subtarget->hasSSE2() && VT == MVT::v8i16);
14890
14891 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14892
14893 // Check for x CC y ? x : y.
14894 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14895 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14896 switch (CC) {
14897 default: break;
14898 case ISD::SETULT:
14899 case ISD::SETULE:
14900 return hasUnsigned ? X86ISD::UMIN : 0;
14901 case ISD::SETUGT:
14902 case ISD::SETUGE:
14903 return hasUnsigned ? X86ISD::UMAX : 0;
14904 case ISD::SETLT:
14905 case ISD::SETLE:
14906 return hasSigned ? X86ISD::SMIN : 0;
14907 case ISD::SETGT:
14908 case ISD::SETGE:
14909 return hasSigned ? X86ISD::SMAX : 0;
14910 }
14911 // Check for x CC y ? y : x -- a min/max with reversed arms.
14912 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14913 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
14914 switch (CC) {
14915 default: break;
14916 case ISD::SETULT:
14917 case ISD::SETULE:
14918 return hasUnsigned ? X86ISD::UMAX : 0;
14919 case ISD::SETUGT:
14920 case ISD::SETUGE:
14921 return hasUnsigned ? X86ISD::UMIN : 0;
14922 case ISD::SETLT:
14923 case ISD::SETLE:
14924 return hasSigned ? X86ISD::SMAX : 0;
14925 case ISD::SETGT:
14926 case ISD::SETGE:
14927 return hasSigned ? X86ISD::SMIN : 0;
14928 }
14929 }
14930
14931 return 0;
14932}
14933
Duncan Sands6bcd2192011-09-17 16:49:39 +000014934/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
14935/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014936static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000014937 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000014938 const X86Subtarget *Subtarget) {
14939 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000014940 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000014941 // Get the LHS/RHS of the select.
14942 SDValue LHS = N->getOperand(1);
14943 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000014944 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000014945
Dan Gohman670e5392009-09-21 18:03:22 +000014946 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000014947 // instructions match the semantics of the common C idiom x<y?x:y but not
14948 // x<=y?x:y, because of how they handle negative zero (which can be
14949 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000014950 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
14951 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000014952 (Subtarget->hasSSE2() ||
14953 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014954 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014955
Chris Lattner47b4ce82009-03-11 05:48:52 +000014956 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000014957 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000014958 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14959 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014960 switch (CC) {
14961 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014962 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014963 // Converting this to a min would handle NaNs incorrectly, and swapping
14964 // the operands would cause it to handle comparisons between positive
14965 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014966 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014967 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014968 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14969 break;
14970 std::swap(LHS, RHS);
14971 }
Dan Gohman670e5392009-09-21 18:03:22 +000014972 Opcode = X86ISD::FMIN;
14973 break;
14974 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014975 // Converting this to a min would handle comparisons between positive
14976 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014977 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014978 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
14979 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014980 Opcode = X86ISD::FMIN;
14981 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014982 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014983 // Converting this to a min would handle both negative zeros and NaNs
14984 // incorrectly, but we can swap the operands to fix both.
14985 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014986 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014987 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014988 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014989 Opcode = X86ISD::FMIN;
14990 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014991
Dan Gohman670e5392009-09-21 18:03:22 +000014992 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014993 // Converting this to a max would handle comparisons between positive
14994 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014995 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014996 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014997 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014998 Opcode = X86ISD::FMAX;
14999 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015000 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015001 // Converting this to a max would handle NaNs incorrectly, and swapping
15002 // the operands would cause it to handle comparisons between positive
15003 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015004 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015005 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015006 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15007 break;
15008 std::swap(LHS, RHS);
15009 }
Dan Gohman670e5392009-09-21 18:03:22 +000015010 Opcode = X86ISD::FMAX;
15011 break;
15012 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015013 // Converting this to a max would handle both negative zeros and NaNs
15014 // incorrectly, but we can swap the operands to fix both.
15015 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015016 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015017 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015018 case ISD::SETGE:
15019 Opcode = X86ISD::FMAX;
15020 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015021 }
Dan Gohman670e5392009-09-21 18:03:22 +000015022 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015023 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15024 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015025 switch (CC) {
15026 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015027 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015028 // Converting this to a min would handle comparisons between positive
15029 // and negative zero incorrectly, and swapping the operands would
15030 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015031 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015032 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015033 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015034 break;
15035 std::swap(LHS, RHS);
15036 }
Dan Gohman670e5392009-09-21 18:03:22 +000015037 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015038 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015039 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015040 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015041 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015042 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15043 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015044 Opcode = X86ISD::FMIN;
15045 break;
15046 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015047 // Converting this to a min would handle both negative zeros and NaNs
15048 // incorrectly, but we can swap the operands to fix both.
15049 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015050 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015051 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015052 case ISD::SETGE:
15053 Opcode = X86ISD::FMIN;
15054 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015055
Dan Gohman670e5392009-09-21 18:03:22 +000015056 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015057 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015058 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015059 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015060 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015061 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015062 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015063 // Converting this to a max would handle comparisons between positive
15064 // and negative zero incorrectly, and swapping the operands would
15065 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015066 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015067 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015068 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015069 break;
15070 std::swap(LHS, RHS);
15071 }
Dan Gohman670e5392009-09-21 18:03:22 +000015072 Opcode = X86ISD::FMAX;
15073 break;
15074 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015075 // Converting this to a max would handle both negative zeros and NaNs
15076 // incorrectly, but we can swap the operands to fix both.
15077 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015078 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015079 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015080 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015081 Opcode = X86ISD::FMAX;
15082 break;
15083 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015084 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015085
Chris Lattner47b4ce82009-03-11 05:48:52 +000015086 if (Opcode)
15087 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015088 }
Eric Christopherfd179292009-08-27 18:07:15 +000015089
Chris Lattnerd1980a52009-03-12 06:52:53 +000015090 // If this is a select between two integer constants, try to do some
15091 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015092 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15093 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015094 // Don't do this for crazy integer types.
15095 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15096 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015097 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015098 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015099
Chris Lattnercee56e72009-03-13 05:53:31 +000015100 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015101 // Efficiently invertible.
15102 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15103 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15104 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15105 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015106 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015107 }
Eric Christopherfd179292009-08-27 18:07:15 +000015108
Chris Lattnerd1980a52009-03-12 06:52:53 +000015109 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015110 if (FalseC->getAPIntValue() == 0 &&
15111 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015112 if (NeedsCondInvert) // Invert the condition if needed.
15113 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15114 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015115
Chris Lattnerd1980a52009-03-12 06:52:53 +000015116 // Zero extend the condition if needed.
15117 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015118
Chris Lattnercee56e72009-03-13 05:53:31 +000015119 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015120 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015121 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015122 }
Eric Christopherfd179292009-08-27 18:07:15 +000015123
Chris Lattner97a29a52009-03-13 05:22:11 +000015124 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015125 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015126 if (NeedsCondInvert) // Invert the condition if needed.
15127 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15128 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015129
Chris Lattner97a29a52009-03-13 05:22:11 +000015130 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015131 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15132 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015133 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015134 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015135 }
Eric Christopherfd179292009-08-27 18:07:15 +000015136
Chris Lattnercee56e72009-03-13 05:53:31 +000015137 // Optimize cases that will turn into an LEA instruction. This requires
15138 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015139 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015140 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015141 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015142
Chris Lattnercee56e72009-03-13 05:53:31 +000015143 bool isFastMultiplier = false;
15144 if (Diff < 10) {
15145 switch ((unsigned char)Diff) {
15146 default: break;
15147 case 1: // result = add base, cond
15148 case 2: // result = lea base( , cond*2)
15149 case 3: // result = lea base(cond, cond*2)
15150 case 4: // result = lea base( , cond*4)
15151 case 5: // result = lea base(cond, cond*4)
15152 case 8: // result = lea base( , cond*8)
15153 case 9: // result = lea base(cond, cond*8)
15154 isFastMultiplier = true;
15155 break;
15156 }
15157 }
Eric Christopherfd179292009-08-27 18:07:15 +000015158
Chris Lattnercee56e72009-03-13 05:53:31 +000015159 if (isFastMultiplier) {
15160 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15161 if (NeedsCondInvert) // Invert the condition if needed.
15162 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15163 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015164
Chris Lattnercee56e72009-03-13 05:53:31 +000015165 // Zero extend the condition if needed.
15166 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15167 Cond);
15168 // Scale the condition by the difference.
15169 if (Diff != 1)
15170 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15171 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015172
Chris Lattnercee56e72009-03-13 05:53:31 +000015173 // Add the base if non-zero.
15174 if (FalseC->getAPIntValue() != 0)
15175 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15176 SDValue(FalseC, 0));
15177 return Cond;
15178 }
Eric Christopherfd179292009-08-27 18:07:15 +000015179 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015180 }
15181 }
Eric Christopherfd179292009-08-27 18:07:15 +000015182
Evan Cheng56f582d2012-01-04 01:41:39 +000015183 // Canonicalize max and min:
15184 // (x > y) ? x : y -> (x >= y) ? x : y
15185 // (x < y) ? x : y -> (x <= y) ? x : y
15186 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15187 // the need for an extra compare
15188 // against zero. e.g.
15189 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15190 // subl %esi, %edi
15191 // testl %edi, %edi
15192 // movl $0, %eax
15193 // cmovgl %edi, %eax
15194 // =>
15195 // xorl %eax, %eax
15196 // subl %esi, $edi
15197 // cmovsl %eax, %edi
15198 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15199 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15200 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15201 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15202 switch (CC) {
15203 default: break;
15204 case ISD::SETLT:
15205 case ISD::SETGT: {
15206 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15207 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15208 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15209 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15210 }
15211 }
15212 }
15213
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015214 // Match VSELECTs into subs with unsigned saturation.
15215 if (!DCI.isBeforeLegalize() &&
15216 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15217 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15218 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15219 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15220 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15221
15222 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15223 // left side invert the predicate to simplify logic below.
15224 SDValue Other;
15225 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15226 Other = RHS;
15227 CC = ISD::getSetCCInverse(CC, true);
15228 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15229 Other = LHS;
15230 }
15231
15232 if (Other.getNode() && Other->getNumOperands() == 2 &&
15233 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15234 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15235 SDValue CondRHS = Cond->getOperand(1);
15236
15237 // Look for a general sub with unsigned saturation first.
15238 // x >= y ? x-y : 0 --> subus x, y
15239 // x > y ? x-y : 0 --> subus x, y
15240 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15241 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15242 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15243
15244 // If the RHS is a constant we have to reverse the const canonicalization.
15245 // x > C-1 ? x+-C : 0 --> subus x, C
15246 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15247 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15248 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15249 if (CondRHS.getConstantOperandVal(0) == -A-1) {
15250 SmallVector<SDValue, 32> V(VT.getVectorNumElements(),
15251 DAG.getConstant(-A, VT.getScalarType()));
15252 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
15253 DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
15254 V.data(), V.size()));
15255 }
15256 }
15257
15258 // Another special case: If C was a sign bit, the sub has been
15259 // canonicalized into a xor.
15260 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15261 // it's safe to decanonicalize the xor?
15262 // x s< 0 ? x^C : 0 --> subus x, C
15263 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15264 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15265 isSplatVector(OpRHS.getNode())) {
15266 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15267 if (A.isSignBit())
15268 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15269 }
15270 }
15271 }
15272
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015273 // Try to match a min/max vector operation.
15274 if (!DCI.isBeforeLegalize() &&
15275 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15276 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15277 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15278
Nadav Rotemcc616562012-01-15 19:27:55 +000015279 // If we know that this node is legal then we know that it is going to be
15280 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15281 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15282 // to simplify previous instructions.
15283 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15284 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015285 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015286 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015287
15288 // Don't optimize vector selects that map to mask-registers.
15289 if (BitWidth == 1)
15290 return SDValue();
15291
Nadav Rotemcc616562012-01-15 19:27:55 +000015292 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15293 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15294
15295 APInt KnownZero, KnownOne;
15296 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15297 DCI.isBeforeLegalizeOps());
15298 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15299 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15300 DCI.CommitTargetLoweringOpt(TLO);
15301 }
15302
Dan Gohman475871a2008-07-27 21:46:04 +000015303 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015304}
15305
Michael Liao2a33cec2012-08-10 19:58:13 +000015306// Check whether a boolean test is testing a boolean value generated by
15307// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15308// code.
15309//
15310// Simplify the following patterns:
15311// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15312// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15313// to (Op EFLAGS Cond)
15314//
15315// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15316// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15317// to (Op EFLAGS !Cond)
15318//
15319// where Op could be BRCOND or CMOV.
15320//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015321static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015322 // Quit if not CMP and SUB with its value result used.
15323 if (Cmp.getOpcode() != X86ISD::CMP &&
15324 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15325 return SDValue();
15326
15327 // Quit if not used as a boolean value.
15328 if (CC != X86::COND_E && CC != X86::COND_NE)
15329 return SDValue();
15330
15331 // Check CMP operands. One of them should be 0 or 1 and the other should be
15332 // an SetCC or extended from it.
15333 SDValue Op1 = Cmp.getOperand(0);
15334 SDValue Op2 = Cmp.getOperand(1);
15335
15336 SDValue SetCC;
15337 const ConstantSDNode* C = 0;
15338 bool needOppositeCond = (CC == X86::COND_E);
15339
15340 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15341 SetCC = Op2;
15342 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15343 SetCC = Op1;
15344 else // Quit if all operands are not constants.
15345 return SDValue();
15346
15347 if (C->getZExtValue() == 1)
15348 needOppositeCond = !needOppositeCond;
15349 else if (C->getZExtValue() != 0)
15350 // Quit if the constant is neither 0 or 1.
15351 return SDValue();
15352
15353 // Skip 'zext' node.
15354 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15355 SetCC = SetCC.getOperand(0);
15356
Michael Liao7fdc66b2012-09-10 16:36:16 +000015357 switch (SetCC.getOpcode()) {
15358 case X86ISD::SETCC:
15359 // Set the condition code or opposite one if necessary.
15360 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15361 if (needOppositeCond)
15362 CC = X86::GetOppositeBranchCondition(CC);
15363 return SetCC.getOperand(1);
15364 case X86ISD::CMOV: {
15365 // Check whether false/true value has canonical one, i.e. 0 or 1.
15366 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15367 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15368 // Quit if true value is not a constant.
15369 if (!TVal)
15370 return SDValue();
15371 // Quit if false value is not a constant.
15372 if (!FVal) {
15373 // A special case for rdrand, where 0 is set if false cond is found.
15374 SDValue Op = SetCC.getOperand(0);
15375 if (Op.getOpcode() != X86ISD::RDRAND)
15376 return SDValue();
15377 }
15378 // Quit if false value is not the constant 0 or 1.
15379 bool FValIsFalse = true;
15380 if (FVal && FVal->getZExtValue() != 0) {
15381 if (FVal->getZExtValue() != 1)
15382 return SDValue();
15383 // If FVal is 1, opposite cond is needed.
15384 needOppositeCond = !needOppositeCond;
15385 FValIsFalse = false;
15386 }
15387 // Quit if TVal is not the constant opposite of FVal.
15388 if (FValIsFalse && TVal->getZExtValue() != 1)
15389 return SDValue();
15390 if (!FValIsFalse && TVal->getZExtValue() != 0)
15391 return SDValue();
15392 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15393 if (needOppositeCond)
15394 CC = X86::GetOppositeBranchCondition(CC);
15395 return SetCC.getOperand(3);
15396 }
15397 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015398
Michael Liao7fdc66b2012-09-10 16:36:16 +000015399 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015400}
15401
Chris Lattnerd1980a52009-03-12 06:52:53 +000015402/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15403static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015404 TargetLowering::DAGCombinerInfo &DCI,
15405 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015406 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015407
Chris Lattnerd1980a52009-03-12 06:52:53 +000015408 // If the flag operand isn't dead, don't touch this CMOV.
15409 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15410 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015411
Evan Chengb5a55d92011-05-24 01:48:22 +000015412 SDValue FalseOp = N->getOperand(0);
15413 SDValue TrueOp = N->getOperand(1);
15414 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15415 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015416
Evan Chengb5a55d92011-05-24 01:48:22 +000015417 if (CC == X86::COND_E || CC == X86::COND_NE) {
15418 switch (Cond.getOpcode()) {
15419 default: break;
15420 case X86ISD::BSR:
15421 case X86ISD::BSF:
15422 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15423 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15424 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15425 }
15426 }
15427
Michael Liao2a33cec2012-08-10 19:58:13 +000015428 SDValue Flags;
15429
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015430 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015431 if (Flags.getNode() &&
15432 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015433 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015434 SDValue Ops[] = { FalseOp, TrueOp,
15435 DAG.getConstant(CC, MVT::i8), Flags };
15436 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15437 Ops, array_lengthof(Ops));
15438 }
15439
Chris Lattnerd1980a52009-03-12 06:52:53 +000015440 // If this is a select between two integer constants, try to do some
15441 // optimizations. Note that the operands are ordered the opposite of SELECT
15442 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015443 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15444 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015445 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15446 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015447 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15448 CC = X86::GetOppositeBranchCondition(CC);
15449 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015450 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015451 }
Eric Christopherfd179292009-08-27 18:07:15 +000015452
Chris Lattnerd1980a52009-03-12 06:52:53 +000015453 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015454 // This is efficient for any integer data type (including i8/i16) and
15455 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015456 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015457 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15458 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015459
Chris Lattnerd1980a52009-03-12 06:52:53 +000015460 // Zero extend the condition if needed.
15461 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015462
Chris Lattnerd1980a52009-03-12 06:52:53 +000015463 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15464 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015465 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015466 if (N->getNumValues() == 2) // Dead flag value?
15467 return DCI.CombineTo(N, Cond, SDValue());
15468 return Cond;
15469 }
Eric Christopherfd179292009-08-27 18:07:15 +000015470
Chris Lattnercee56e72009-03-13 05:53:31 +000015471 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15472 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015473 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015474 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15475 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015476
Chris Lattner97a29a52009-03-13 05:22:11 +000015477 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015478 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15479 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015480 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15481 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015482
Chris Lattner97a29a52009-03-13 05:22:11 +000015483 if (N->getNumValues() == 2) // Dead flag value?
15484 return DCI.CombineTo(N, Cond, SDValue());
15485 return Cond;
15486 }
Eric Christopherfd179292009-08-27 18:07:15 +000015487
Chris Lattnercee56e72009-03-13 05:53:31 +000015488 // Optimize cases that will turn into an LEA instruction. This requires
15489 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015490 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015491 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015492 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015493
Chris Lattnercee56e72009-03-13 05:53:31 +000015494 bool isFastMultiplier = false;
15495 if (Diff < 10) {
15496 switch ((unsigned char)Diff) {
15497 default: break;
15498 case 1: // result = add base, cond
15499 case 2: // result = lea base( , cond*2)
15500 case 3: // result = lea base(cond, cond*2)
15501 case 4: // result = lea base( , cond*4)
15502 case 5: // result = lea base(cond, cond*4)
15503 case 8: // result = lea base( , cond*8)
15504 case 9: // result = lea base(cond, cond*8)
15505 isFastMultiplier = true;
15506 break;
15507 }
15508 }
Eric Christopherfd179292009-08-27 18:07:15 +000015509
Chris Lattnercee56e72009-03-13 05:53:31 +000015510 if (isFastMultiplier) {
15511 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015512 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15513 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015514 // Zero extend the condition if needed.
15515 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15516 Cond);
15517 // Scale the condition by the difference.
15518 if (Diff != 1)
15519 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15520 DAG.getConstant(Diff, Cond.getValueType()));
15521
15522 // Add the base if non-zero.
15523 if (FalseC->getAPIntValue() != 0)
15524 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15525 SDValue(FalseC, 0));
15526 if (N->getNumValues() == 2) // Dead flag value?
15527 return DCI.CombineTo(N, Cond, SDValue());
15528 return Cond;
15529 }
Eric Christopherfd179292009-08-27 18:07:15 +000015530 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015531 }
15532 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015533
15534 // Handle these cases:
15535 // (select (x != c), e, c) -> select (x != c), e, x),
15536 // (select (x == c), c, e) -> select (x == c), x, e)
15537 // where the c is an integer constant, and the "select" is the combination
15538 // of CMOV and CMP.
15539 //
15540 // The rationale for this change is that the conditional-move from a constant
15541 // needs two instructions, however, conditional-move from a register needs
15542 // only one instruction.
15543 //
15544 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15545 // some instruction-combining opportunities. This opt needs to be
15546 // postponed as late as possible.
15547 //
15548 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15549 // the DCI.xxxx conditions are provided to postpone the optimization as
15550 // late as possible.
15551
15552 ConstantSDNode *CmpAgainst = 0;
15553 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15554 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
15555 dyn_cast<ConstantSDNode>(Cond.getOperand(0)) == 0) {
15556
15557 if (CC == X86::COND_NE &&
15558 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15559 CC = X86::GetOppositeBranchCondition(CC);
15560 std::swap(TrueOp, FalseOp);
15561 }
15562
15563 if (CC == X86::COND_E &&
15564 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15565 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15566 DAG.getConstant(CC, MVT::i8), Cond };
15567 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15568 array_lengthof(Ops));
15569 }
15570 }
15571 }
15572
Chris Lattnerd1980a52009-03-12 06:52:53 +000015573 return SDValue();
15574}
15575
Evan Cheng0b0cd912009-03-28 05:57:29 +000015576/// PerformMulCombine - Optimize a single multiply with constant into two
15577/// in order to implement it with two cheaper instructions, e.g.
15578/// LEA + SHL, LEA + LEA.
15579static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15580 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015581 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15582 return SDValue();
15583
Owen Andersone50ed302009-08-10 22:56:29 +000015584 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015585 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015586 return SDValue();
15587
15588 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15589 if (!C)
15590 return SDValue();
15591 uint64_t MulAmt = C->getZExtValue();
15592 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15593 return SDValue();
15594
15595 uint64_t MulAmt1 = 0;
15596 uint64_t MulAmt2 = 0;
15597 if ((MulAmt % 9) == 0) {
15598 MulAmt1 = 9;
15599 MulAmt2 = MulAmt / 9;
15600 } else if ((MulAmt % 5) == 0) {
15601 MulAmt1 = 5;
15602 MulAmt2 = MulAmt / 5;
15603 } else if ((MulAmt % 3) == 0) {
15604 MulAmt1 = 3;
15605 MulAmt2 = MulAmt / 3;
15606 }
15607 if (MulAmt2 &&
15608 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15609 DebugLoc DL = N->getDebugLoc();
15610
15611 if (isPowerOf2_64(MulAmt2) &&
15612 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15613 // If second multiplifer is pow2, issue it first. We want the multiply by
15614 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15615 // is an add.
15616 std::swap(MulAmt1, MulAmt2);
15617
15618 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015619 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015620 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015621 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015622 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015623 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015624 DAG.getConstant(MulAmt1, VT));
15625
Eric Christopherfd179292009-08-27 18:07:15 +000015626 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015627 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015628 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015629 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015630 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015631 DAG.getConstant(MulAmt2, VT));
15632
15633 // Do not add new nodes to DAG combiner worklist.
15634 DCI.CombineTo(N, NewMul, false);
15635 }
15636 return SDValue();
15637}
15638
Evan Chengad9c0a32009-12-15 00:53:42 +000015639static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15640 SDValue N0 = N->getOperand(0);
15641 SDValue N1 = N->getOperand(1);
15642 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15643 EVT VT = N0.getValueType();
15644
15645 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15646 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015647 if (VT.isInteger() && !VT.isVector() &&
15648 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015649 N0.getOperand(1).getOpcode() == ISD::Constant) {
15650 SDValue N00 = N0.getOperand(0);
15651 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15652 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15653 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15654 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15655 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15656 APInt ShAmt = N1C->getAPIntValue();
15657 Mask = Mask.shl(ShAmt);
15658 if (Mask != 0)
15659 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15660 N00, DAG.getConstant(Mask, VT));
15661 }
15662 }
15663
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015664 // Hardware support for vector shifts is sparse which makes us scalarize the
15665 // vector operations in many cases. Also, on sandybridge ADD is faster than
15666 // shl.
15667 // (shl V, 1) -> add V,V
15668 if (isSplatVector(N1.getNode())) {
15669 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15670 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15671 // We shift all of the values by one. In many cases we do not have
15672 // hardware support for this operation. This is better expressed as an ADD
15673 // of two values.
15674 if (N1C && (1 == N1C->getZExtValue())) {
15675 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15676 }
15677 }
15678
Evan Chengad9c0a32009-12-15 00:53:42 +000015679 return SDValue();
15680}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015681
Nate Begeman740ab032009-01-26 00:52:55 +000015682/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15683/// when possible.
15684static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015685 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015686 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015687 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015688 if (N->getOpcode() == ISD::SHL) {
15689 SDValue V = PerformSHLCombine(N, DAG);
15690 if (V.getNode()) return V;
15691 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015692
Nate Begeman740ab032009-01-26 00:52:55 +000015693 // On X86 with SSE2 support, we can transform this to a vector shift if
15694 // all elements are shifted by the same amount. We can't do this in legalize
15695 // because the a constant vector is typically transformed to a constant pool
15696 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015697 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015698 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015699
Craig Topper7be5dfd2011-11-12 09:58:49 +000015700 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015701 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015702 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015703 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015704
Mon P Wang3becd092009-01-28 08:12:05 +000015705 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015706 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015707 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015708 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015709 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15710 unsigned NumElts = VT.getVectorNumElements();
15711 unsigned i = 0;
15712 for (; i != NumElts; ++i) {
15713 SDValue Arg = ShAmtOp.getOperand(i);
15714 if (Arg.getOpcode() == ISD::UNDEF) continue;
15715 BaseShAmt = Arg;
15716 break;
15717 }
Craig Topper37c26772012-01-17 04:44:50 +000015718 // Handle the case where the build_vector is all undef
15719 // FIXME: Should DAG allow this?
15720 if (i == NumElts)
15721 return SDValue();
15722
Mon P Wang3becd092009-01-28 08:12:05 +000015723 for (; i != NumElts; ++i) {
15724 SDValue Arg = ShAmtOp.getOperand(i);
15725 if (Arg.getOpcode() == ISD::UNDEF) continue;
15726 if (Arg != BaseShAmt) {
15727 return SDValue();
15728 }
15729 }
15730 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015731 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015732 SDValue InVec = ShAmtOp.getOperand(0);
15733 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
15734 unsigned NumElts = InVec.getValueType().getVectorNumElements();
15735 unsigned i = 0;
15736 for (; i != NumElts; ++i) {
15737 SDValue Arg = InVec.getOperand(i);
15738 if (Arg.getOpcode() == ISD::UNDEF) continue;
15739 BaseShAmt = Arg;
15740 break;
15741 }
15742 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
15743 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000015744 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000015745 if (C->getZExtValue() == SplatIdx)
15746 BaseShAmt = InVec.getOperand(1);
15747 }
15748 }
Mon P Wang845b1892012-02-01 22:15:20 +000015749 if (BaseShAmt.getNode() == 0) {
15750 // Don't create instructions with illegal types after legalize
15751 // types has run.
15752 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
15753 !DCI.isBeforeLegalize())
15754 return SDValue();
15755
Mon P Wangefa42202009-09-03 19:56:25 +000015756 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
15757 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000015758 }
Mon P Wang3becd092009-01-28 08:12:05 +000015759 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015760 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000015761
Mon P Wangefa42202009-09-03 19:56:25 +000015762 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000015763 if (EltVT.bitsGT(MVT::i32))
15764 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
15765 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000015766 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000015767
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015768 // The shift amount is identical so we can do a vector shift.
15769 SDValue ValOp = N->getOperand(0);
15770 switch (N->getOpcode()) {
15771 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000015772 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015773 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015774 switch (VT.getSimpleVT().SimpleTy) {
15775 default: return SDValue();
15776 case MVT::v2i64:
15777 case MVT::v4i32:
15778 case MVT::v8i16:
15779 case MVT::v4i64:
15780 case MVT::v8i32:
15781 case MVT::v16i16:
15782 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
15783 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015784 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000015785 switch (VT.getSimpleVT().SimpleTy) {
15786 default: return SDValue();
15787 case MVT::v4i32:
15788 case MVT::v8i16:
15789 case MVT::v8i32:
15790 case MVT::v16i16:
15791 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
15792 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015793 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000015794 switch (VT.getSimpleVT().SimpleTy) {
15795 default: return SDValue();
15796 case MVT::v2i64:
15797 case MVT::v4i32:
15798 case MVT::v8i16:
15799 case MVT::v4i64:
15800 case MVT::v8i32:
15801 case MVT::v16i16:
15802 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
15803 }
Nate Begeman740ab032009-01-26 00:52:55 +000015804 }
Nate Begeman740ab032009-01-26 00:52:55 +000015805}
15806
Stuart Hastings865f0932011-06-03 23:53:54 +000015807// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
15808// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
15809// and friends. Likewise for OR -> CMPNEQSS.
15810static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
15811 TargetLowering::DAGCombinerInfo &DCI,
15812 const X86Subtarget *Subtarget) {
15813 unsigned opcode;
15814
15815 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
15816 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000015817 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000015818 SDValue N0 = N->getOperand(0);
15819 SDValue N1 = N->getOperand(1);
15820 SDValue CMP0 = N0->getOperand(1);
15821 SDValue CMP1 = N1->getOperand(1);
15822 DebugLoc DL = N->getDebugLoc();
15823
15824 // The SETCCs should both refer to the same CMP.
15825 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
15826 return SDValue();
15827
15828 SDValue CMP00 = CMP0->getOperand(0);
15829 SDValue CMP01 = CMP0->getOperand(1);
15830 EVT VT = CMP00.getValueType();
15831
15832 if (VT == MVT::f32 || VT == MVT::f64) {
15833 bool ExpectingFlags = false;
15834 // Check for any users that want flags:
15835 for (SDNode::use_iterator UI = N->use_begin(),
15836 UE = N->use_end();
15837 !ExpectingFlags && UI != UE; ++UI)
15838 switch (UI->getOpcode()) {
15839 default:
15840 case ISD::BR_CC:
15841 case ISD::BRCOND:
15842 case ISD::SELECT:
15843 ExpectingFlags = true;
15844 break;
15845 case ISD::CopyToReg:
15846 case ISD::SIGN_EXTEND:
15847 case ISD::ZERO_EXTEND:
15848 case ISD::ANY_EXTEND:
15849 break;
15850 }
15851
15852 if (!ExpectingFlags) {
15853 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
15854 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
15855
15856 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
15857 X86::CondCode tmp = cc0;
15858 cc0 = cc1;
15859 cc1 = tmp;
15860 }
15861
15862 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
15863 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
15864 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
15865 X86ISD::NodeType NTOperator = is64BitFP ?
15866 X86ISD::FSETCCsd : X86ISD::FSETCCss;
15867 // FIXME: need symbolic constants for these magic numbers.
15868 // See X86ATTInstPrinter.cpp:printSSECC().
15869 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
15870 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
15871 DAG.getConstant(x86cc, MVT::i8));
15872 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
15873 OnesOrZeroesF);
15874 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
15875 DAG.getConstant(1, MVT::i32));
15876 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
15877 return OneBitOfTruth;
15878 }
15879 }
15880 }
15881 }
15882 return SDValue();
15883}
15884
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015885/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
15886/// so it can be folded inside ANDNP.
15887static bool CanFoldXORWithAllOnes(const SDNode *N) {
15888 EVT VT = N->getValueType(0);
15889
15890 // Match direct AllOnes for 128 and 256-bit vectors
15891 if (ISD::isBuildVectorAllOnes(N))
15892 return true;
15893
15894 // Look through a bit convert.
15895 if (N->getOpcode() == ISD::BITCAST)
15896 N = N->getOperand(0).getNode();
15897
15898 // Sometimes the operand may come from a insert_subvector building a 256-bit
15899 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000015900 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000015901 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
15902 SDValue V1 = N->getOperand(0);
15903 SDValue V2 = N->getOperand(1);
15904
15905 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
15906 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
15907 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
15908 ISD::isBuildVectorAllOnes(V2.getNode()))
15909 return true;
15910 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000015911
15912 return false;
15913}
15914
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000015915// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
15916// register. In most cases we actually compare or select YMM-sized registers
15917// and mixing the two types creates horrible code. This method optimizes
15918// some of the transition sequences.
15919static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
15920 TargetLowering::DAGCombinerInfo &DCI,
15921 const X86Subtarget *Subtarget) {
15922 EVT VT = N->getValueType(0);
15923 if (VT.getSizeInBits() != 256)
15924 return SDValue();
15925
15926 assert((N->getOpcode() == ISD::ANY_EXTEND ||
15927 N->getOpcode() == ISD::ZERO_EXTEND ||
15928 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
15929
15930 SDValue Narrow = N->getOperand(0);
15931 EVT NarrowVT = Narrow->getValueType(0);
15932 if (NarrowVT.getSizeInBits() != 128)
15933 return SDValue();
15934
15935 if (Narrow->getOpcode() != ISD::XOR &&
15936 Narrow->getOpcode() != ISD::AND &&
15937 Narrow->getOpcode() != ISD::OR)
15938 return SDValue();
15939
15940 SDValue N0 = Narrow->getOperand(0);
15941 SDValue N1 = Narrow->getOperand(1);
15942 DebugLoc DL = Narrow->getDebugLoc();
15943
15944 // The Left side has to be a trunc.
15945 if (N0.getOpcode() != ISD::TRUNCATE)
15946 return SDValue();
15947
15948 // The type of the truncated inputs.
15949 EVT WideVT = N0->getOperand(0)->getValueType(0);
15950 if (WideVT != VT)
15951 return SDValue();
15952
15953 // The right side has to be a 'trunc' or a constant vector.
15954 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
15955 bool RHSConst = (isSplatVector(N1.getNode()) &&
15956 isa<ConstantSDNode>(N1->getOperand(0)));
15957 if (!RHSTrunc && !RHSConst)
15958 return SDValue();
15959
15960 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15961
15962 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
15963 return SDValue();
15964
15965 // Set N0 and N1 to hold the inputs to the new wide operation.
15966 N0 = N0->getOperand(0);
15967 if (RHSConst) {
15968 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
15969 N1->getOperand(0));
15970 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
15971 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
15972 } else if (RHSTrunc) {
15973 N1 = N1->getOperand(0);
15974 }
15975
15976 // Generate the wide operation.
15977 SDValue Op = DAG.getNode(N->getOpcode(), DL, WideVT, N0, N1);
15978 unsigned Opcode = N->getOpcode();
15979 switch (Opcode) {
15980 case ISD::ANY_EXTEND:
15981 return Op;
15982 case ISD::ZERO_EXTEND: {
15983 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
15984 APInt Mask = APInt::getAllOnesValue(InBits);
15985 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
15986 return DAG.getNode(ISD::AND, DL, VT,
15987 Op, DAG.getConstant(Mask, VT));
15988 }
15989 case ISD::SIGN_EXTEND:
15990 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
15991 Op, DAG.getValueType(NarrowVT));
15992 default:
15993 llvm_unreachable("Unexpected opcode");
15994 }
15995}
15996
Nate Begemanb65c1752010-12-17 22:55:37 +000015997static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
15998 TargetLowering::DAGCombinerInfo &DCI,
15999 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016000 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016001 if (DCI.isBeforeLegalizeOps())
16002 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016003
Stuart Hastings865f0932011-06-03 23:53:54 +000016004 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16005 if (R.getNode())
16006 return R;
16007
Craig Topperb926afc2012-12-17 05:12:30 +000016008 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016009 // BLSI is X & (-X)
16010 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016011 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16012 SDValue N0 = N->getOperand(0);
16013 SDValue N1 = N->getOperand(1);
16014 DebugLoc DL = N->getDebugLoc();
16015
Craig Topperb4c94572011-10-21 06:55:01 +000016016 // Check LHS for neg
16017 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16018 isZero(N0.getOperand(0)))
16019 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16020
16021 // Check RHS for neg
16022 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16023 isZero(N1.getOperand(0)))
16024 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16025
16026 // Check LHS for X-1
16027 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16028 isAllOnes(N0.getOperand(1)))
16029 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16030
16031 // Check RHS for X-1
16032 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16033 isAllOnes(N1.getOperand(1)))
16034 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16035
Craig Topper54a11172011-10-14 07:06:56 +000016036 return SDValue();
16037 }
16038
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016039 // Want to form ANDNP nodes:
16040 // 1) In the hopes of then easily combining them with OR and AND nodes
16041 // to form PBLEND/PSIGN.
16042 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016043 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016044 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016045
Nate Begemanb65c1752010-12-17 22:55:37 +000016046 SDValue N0 = N->getOperand(0);
16047 SDValue N1 = N->getOperand(1);
16048 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016049
Nate Begemanb65c1752010-12-17 22:55:37 +000016050 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016051 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016052 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16053 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016054 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016055
16056 // Check RHS for vnot
16057 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016058 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16059 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016060 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016061
Nate Begemanb65c1752010-12-17 22:55:37 +000016062 return SDValue();
16063}
16064
Evan Cheng760d1942010-01-04 21:22:48 +000016065static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016066 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016067 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016068 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016069 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016070 return SDValue();
16071
Stuart Hastings865f0932011-06-03 23:53:54 +000016072 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16073 if (R.getNode())
16074 return R;
16075
Evan Cheng760d1942010-01-04 21:22:48 +000016076 SDValue N0 = N->getOperand(0);
16077 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016078
Nate Begemanb65c1752010-12-17 22:55:37 +000016079 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016080 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016081 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016082 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016083 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016084
Craig Topper1666cb62011-11-19 07:07:26 +000016085 // Canonicalize pandn to RHS
16086 if (N0.getOpcode() == X86ISD::ANDNP)
16087 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016088 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016089 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16090 SDValue Mask = N1.getOperand(0);
16091 SDValue X = N1.getOperand(1);
16092 SDValue Y;
16093 if (N0.getOperand(0) == Mask)
16094 Y = N0.getOperand(1);
16095 if (N0.getOperand(1) == Mask)
16096 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016097
Craig Topper1666cb62011-11-19 07:07:26 +000016098 // Check to see if the mask appeared in both the AND and ANDNP and
16099 if (!Y.getNode())
16100 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016101
Craig Topper1666cb62011-11-19 07:07:26 +000016102 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016103 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016104 if (Mask.getOpcode() == ISD::BITCAST)
16105 Mask = Mask.getOperand(0);
16106 if (X.getOpcode() == ISD::BITCAST)
16107 X = X.getOperand(0);
16108 if (Y.getOpcode() == ISD::BITCAST)
16109 Y = Y.getOperand(0);
16110
Craig Topper1666cb62011-11-19 07:07:26 +000016111 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016112
Craig Toppered2e13d2012-01-22 19:15:14 +000016113 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016114 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16115 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016116 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016117 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016118
16119 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016120 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016121 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16122 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16123 if ((SraAmt + 1) != EltBits)
16124 return SDValue();
16125
16126 DebugLoc DL = N->getDebugLoc();
16127
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016128 // We are going to replace the AND, OR, NAND with either BLEND
16129 // or PSIGN, which only look at the MSB. The VSRAI instruction
16130 // does not affect the highest bit, so we can get rid of it.
16131 Mask = Mask.getOperand(0);
16132
Craig Topper1666cb62011-11-19 07:07:26 +000016133 // Now we know we at least have a plendvb with the mask val. See if
16134 // we can form a psignb/w/d.
16135 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016136 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16137 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016138 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16139 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16140 "Unsupported VT for PSIGN");
Nadav Rotemaf59e9a2012-12-07 21:43:11 +000016141 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask);
Craig Toppered2e13d2012-01-22 19:15:14 +000016142 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016143 }
16144 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016145 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016146 return SDValue();
16147
16148 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16149
16150 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16151 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16152 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016153 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016154 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016155 }
16156 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016157
Craig Topper1666cb62011-11-19 07:07:26 +000016158 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16159 return SDValue();
16160
Nate Begemanb65c1752010-12-17 22:55:37 +000016161 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016162 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16163 std::swap(N0, N1);
16164 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16165 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016166 if (!N0.hasOneUse() || !N1.hasOneUse())
16167 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016168
16169 SDValue ShAmt0 = N0.getOperand(1);
16170 if (ShAmt0.getValueType() != MVT::i8)
16171 return SDValue();
16172 SDValue ShAmt1 = N1.getOperand(1);
16173 if (ShAmt1.getValueType() != MVT::i8)
16174 return SDValue();
16175 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16176 ShAmt0 = ShAmt0.getOperand(0);
16177 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16178 ShAmt1 = ShAmt1.getOperand(0);
16179
16180 DebugLoc DL = N->getDebugLoc();
16181 unsigned Opc = X86ISD::SHLD;
16182 SDValue Op0 = N0.getOperand(0);
16183 SDValue Op1 = N1.getOperand(0);
16184 if (ShAmt0.getOpcode() == ISD::SUB) {
16185 Opc = X86ISD::SHRD;
16186 std::swap(Op0, Op1);
16187 std::swap(ShAmt0, ShAmt1);
16188 }
16189
Evan Cheng8b1190a2010-04-28 01:18:01 +000016190 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016191 if (ShAmt1.getOpcode() == ISD::SUB) {
16192 SDValue Sum = ShAmt1.getOperand(0);
16193 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016194 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16195 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16196 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16197 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016198 return DAG.getNode(Opc, DL, VT,
16199 Op0, Op1,
16200 DAG.getNode(ISD::TRUNCATE, DL,
16201 MVT::i8, ShAmt0));
16202 }
16203 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16204 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16205 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016206 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016207 return DAG.getNode(Opc, DL, VT,
16208 N0.getOperand(0), N1.getOperand(0),
16209 DAG.getNode(ISD::TRUNCATE, DL,
16210 MVT::i8, ShAmt0));
16211 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016212
Evan Cheng760d1942010-01-04 21:22:48 +000016213 return SDValue();
16214}
16215
Manman Ren92363622012-06-07 22:39:10 +000016216// Generate NEG and CMOV for integer abs.
16217static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16218 EVT VT = N->getValueType(0);
16219
16220 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16221 // 8-bit integer abs to NEG and CMOV.
16222 if (VT.isInteger() && VT.getSizeInBits() == 8)
16223 return SDValue();
16224
16225 SDValue N0 = N->getOperand(0);
16226 SDValue N1 = N->getOperand(1);
16227 DebugLoc DL = N->getDebugLoc();
16228
16229 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16230 // and change it to SUB and CMOV.
16231 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16232 N0.getOpcode() == ISD::ADD &&
16233 N0.getOperand(1) == N1 &&
16234 N1.getOpcode() == ISD::SRA &&
16235 N1.getOperand(0) == N0.getOperand(0))
16236 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16237 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16238 // Generate SUB & CMOV.
16239 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16240 DAG.getConstant(0, VT), N0.getOperand(0));
16241
16242 SDValue Ops[] = { N0.getOperand(0), Neg,
16243 DAG.getConstant(X86::COND_GE, MVT::i8),
16244 SDValue(Neg.getNode(), 1) };
16245 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16246 Ops, array_lengthof(Ops));
16247 }
16248 return SDValue();
16249}
16250
Craig Topper3738ccd2011-12-27 06:27:23 +000016251// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016252static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16253 TargetLowering::DAGCombinerInfo &DCI,
16254 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016255 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016256 if (DCI.isBeforeLegalizeOps())
16257 return SDValue();
16258
Manman Ren45d53b82012-06-08 18:58:26 +000016259 if (Subtarget->hasCMov()) {
16260 SDValue RV = performIntegerAbsCombine(N, DAG);
16261 if (RV.getNode())
16262 return RV;
16263 }
Manman Ren92363622012-06-07 22:39:10 +000016264
16265 // Try forming BMI if it is available.
16266 if (!Subtarget->hasBMI())
16267 return SDValue();
16268
Craig Topperb4c94572011-10-21 06:55:01 +000016269 if (VT != MVT::i32 && VT != MVT::i64)
16270 return SDValue();
16271
Craig Topper3738ccd2011-12-27 06:27:23 +000016272 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16273
Craig Topperb4c94572011-10-21 06:55:01 +000016274 // Create BLSMSK instructions by finding X ^ (X-1)
16275 SDValue N0 = N->getOperand(0);
16276 SDValue N1 = N->getOperand(1);
16277 DebugLoc DL = N->getDebugLoc();
16278
16279 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16280 isAllOnes(N0.getOperand(1)))
16281 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16282
16283 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16284 isAllOnes(N1.getOperand(1)))
16285 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16286
16287 return SDValue();
16288}
16289
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016290/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16291static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016292 TargetLowering::DAGCombinerInfo &DCI,
16293 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016294 LoadSDNode *Ld = cast<LoadSDNode>(N);
16295 EVT RegVT = Ld->getValueType(0);
16296 EVT MemVT = Ld->getMemoryVT();
16297 DebugLoc dl = Ld->getDebugLoc();
16298 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16299
16300 ISD::LoadExtType Ext = Ld->getExtensionType();
16301
Nadav Rotemca6f2962011-09-18 19:00:23 +000016302 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016303 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16304 // expansion is still better than scalar code.
16305 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16306 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016307 // TODO: It is possible to support ZExt by zeroing the undef values
16308 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016309 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16310 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016311 assert(MemVT != RegVT && "Cannot extend to the same type");
16312 assert(MemVT.isVector() && "Must load a vector from memory");
16313
16314 unsigned NumElems = RegVT.getVectorNumElements();
16315 unsigned RegSz = RegVT.getSizeInBits();
16316 unsigned MemSz = MemVT.getSizeInBits();
16317 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016318
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016319 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16320 return SDValue();
16321
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016322 // All sizes must be a power of two.
16323 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16324 return SDValue();
16325
16326 // Attempt to load the original value using scalar loads.
16327 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016328 MVT SclrLoadTy = MVT::i8;
16329 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16330 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16331 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016332 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016333 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016334 }
16335 }
16336
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016337 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16338 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16339 (64 <= MemSz))
16340 SclrLoadTy = MVT::f64;
16341
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016342 // Calculate the number of scalar loads that we need to perform
16343 // in order to load our vector from memory.
16344 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016345 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16346 return SDValue();
16347
16348 unsigned loadRegZize = RegSz;
16349 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16350 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016351
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016352 // Represent our vector as a sequence of elements which are the
16353 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016354 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016355 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016356
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016357 // Represent the data using the same element type that is stored in
16358 // memory. In practice, we ''widen'' MemVT.
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016359 EVT WideVecVT =
16360 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16361 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016362
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016363 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16364 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016365
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016366 // We can't shuffle using an illegal type.
16367 if (!TLI.isTypeLegal(WideVecVT))
16368 return SDValue();
16369
16370 SmallVector<SDValue, 8> Chains;
16371 SDValue Ptr = Ld->getBasePtr();
16372 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16373 TLI.getPointerTy());
16374 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16375
16376 for (unsigned i = 0; i < NumLoads; ++i) {
16377 // Perform a single load.
16378 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16379 Ptr, Ld->getPointerInfo(),
16380 Ld->isVolatile(), Ld->isNonTemporal(),
16381 Ld->isInvariant(), Ld->getAlignment());
16382 Chains.push_back(ScalarLoad.getValue(1));
16383 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16384 // another round of DAGCombining.
16385 if (i == 0)
16386 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16387 else
16388 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16389 ScalarLoad, DAG.getIntPtrConstant(i));
16390
16391 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16392 }
16393
16394 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16395 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016396
16397 // Bitcast the loaded value to a vector of the original element type, in
16398 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016399 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016400 unsigned SizeRatio = RegSz/MemSz;
16401
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016402 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016403 // If we have SSE4.1 we can directly emit a VSEXT node.
16404 if (Subtarget->hasSSE41()) {
16405 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16406 return DCI.CombineTo(N, Sext, TF, true);
16407 }
16408
16409 // Otherwise we'll shuffle the small elements in the high bits of the
16410 // larger type and perform an arithmetic shift. If the shift is not legal
16411 // it's better to scalarize.
16412 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16413 return SDValue();
16414
16415 // Redistribute the loaded elements into the different locations.
16416 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16417 for (unsigned i = 0; i != NumElems; ++i)
16418 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16419
16420 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16421 DAG.getUNDEF(WideVecVT),
16422 &ShuffleVec[0]);
16423
16424 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16425
16426 // Build the arithmetic shift.
16427 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16428 MemVT.getVectorElementType().getSizeInBits();
16429 SmallVector<SDValue, 8> C(NumElems,
16430 DAG.getConstant(Amt, RegVT.getScalarType()));
16431 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, RegVT, &C[0], C.size());
16432 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, BV);
16433
16434 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016435 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016436
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016437 // Redistribute the loaded elements into the different locations.
16438 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016439 for (unsigned i = 0; i != NumElems; ++i)
16440 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016441
16442 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016443 DAG.getUNDEF(WideVecVT),
16444 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016445
16446 // Bitcast to the requested type.
16447 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16448 // Replace the original load with the new sequence
16449 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016450 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016451 }
16452
16453 return SDValue();
16454}
16455
Chris Lattner149a4e52008-02-22 02:09:43 +000016456/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016457static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016458 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016459 StoreSDNode *St = cast<StoreSDNode>(N);
16460 EVT VT = St->getValue().getValueType();
16461 EVT StVT = St->getMemoryVT();
16462 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016463 SDValue StoredVal = St->getOperand(1);
16464 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16465
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016466 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016467 // On Sandy Bridge, 256-bit memory operations are executed by two
16468 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16469 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016470 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000016471 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
16472 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000016473 SDValue Value0 = StoredVal.getOperand(0);
16474 SDValue Value1 = StoredVal.getOperand(1);
16475
16476 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16477 SDValue Ptr0 = St->getBasePtr();
16478 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16479
16480 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16481 St->getPointerInfo(), St->isVolatile(),
16482 St->isNonTemporal(), St->getAlignment());
16483 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16484 St->getPointerInfo(), St->isVolatile(),
16485 St->isNonTemporal(), St->getAlignment());
16486 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16487 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016488
16489 // Optimize trunc store (of multiple scalars) to shuffle and store.
16490 // First, pack all of the elements in one place. Next, store to memory
16491 // in fewer chunks.
16492 if (St->isTruncatingStore() && VT.isVector()) {
16493 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16494 unsigned NumElems = VT.getVectorNumElements();
16495 assert(StVT != VT && "Cannot truncate to the same type");
16496 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16497 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16498
16499 // From, To sizes and ElemCount must be pow of two
16500 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016501 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016502 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016503 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016504
Nadav Rotem614061b2011-08-10 19:30:14 +000016505 unsigned SizeRatio = FromSz / ToSz;
16506
16507 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16508
16509 // Create a type on which we perform the shuffle
16510 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16511 StVT.getScalarType(), NumElems*SizeRatio);
16512
16513 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16514
16515 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16516 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016517 for (unsigned i = 0; i != NumElems; ++i)
16518 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016519
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016520 // Can't shuffle using an illegal type.
16521 if (!TLI.isTypeLegal(WideVecVT))
16522 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016523
16524 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016525 DAG.getUNDEF(WideVecVT),
16526 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016527 // At this point all of the data is stored at the bottom of the
16528 // register. We now need to save it to mem.
16529
16530 // Find the largest store unit
16531 MVT StoreType = MVT::i8;
16532 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16533 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16534 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016535 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016536 StoreType = Tp;
16537 }
16538
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016539 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16540 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16541 (64 <= NumElems * ToSz))
16542 StoreType = MVT::f64;
16543
Nadav Rotem614061b2011-08-10 19:30:14 +000016544 // Bitcast the original vector into a vector of store-size units
16545 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016546 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016547 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16548 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16549 SmallVector<SDValue, 8> Chains;
16550 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16551 TLI.getPointerTy());
16552 SDValue Ptr = St->getBasePtr();
16553
16554 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016555 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016556 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16557 StoreType, ShuffWide,
16558 DAG.getIntPtrConstant(i));
16559 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16560 St->getPointerInfo(), St->isVolatile(),
16561 St->isNonTemporal(), St->getAlignment());
16562 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16563 Chains.push_back(Ch);
16564 }
16565
16566 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16567 Chains.size());
16568 }
16569
Chris Lattner149a4e52008-02-22 02:09:43 +000016570 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16571 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016572 // A preferable solution to the general problem is to figure out the right
16573 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016574
16575 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016576 if (VT.getSizeInBits() != 64)
16577 return SDValue();
16578
Devang Patel578efa92009-06-05 21:57:13 +000016579 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016580 bool NoImplicitFloatOps = F->getAttributes().
16581 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016582 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016583 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016584 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016585 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016586 isa<LoadSDNode>(St->getValue()) &&
16587 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16588 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016589 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016590 LoadSDNode *Ld = 0;
16591 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016592 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016593 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016594 // Must be a store of a load. We currently handle two cases: the load
16595 // is a direct child, and it's under an intervening TokenFactor. It is
16596 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016597 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016598 Ld = cast<LoadSDNode>(St->getChain());
16599 else if (St->getValue().hasOneUse() &&
16600 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016601 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016602 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016603 TokenFactorIndex = i;
16604 Ld = cast<LoadSDNode>(St->getValue());
16605 } else
16606 Ops.push_back(ChainVal->getOperand(i));
16607 }
16608 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016609
Evan Cheng536e6672009-03-12 05:59:15 +000016610 if (!Ld || !ISD::isNormalLoad(Ld))
16611 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016612
Evan Cheng536e6672009-03-12 05:59:15 +000016613 // If this is not the MMX case, i.e. we are just turning i64 load/store
16614 // into f64 load/store, avoid the transformation if there are multiple
16615 // uses of the loaded value.
16616 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16617 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016618
Evan Cheng536e6672009-03-12 05:59:15 +000016619 DebugLoc LdDL = Ld->getDebugLoc();
16620 DebugLoc StDL = N->getDebugLoc();
16621 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16622 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16623 // pair instead.
16624 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016625 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016626 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16627 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016628 Ld->isNonTemporal(), Ld->isInvariant(),
16629 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016630 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016631 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016632 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016633 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016634 Ops.size());
16635 }
Evan Cheng536e6672009-03-12 05:59:15 +000016636 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016637 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016638 St->isVolatile(), St->isNonTemporal(),
16639 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016640 }
Evan Cheng536e6672009-03-12 05:59:15 +000016641
16642 // Otherwise, lower to two pairs of 32-bit loads / stores.
16643 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016644 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16645 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016646
Owen Anderson825b72b2009-08-11 20:47:22 +000016647 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016648 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016649 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016650 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016651 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016652 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016653 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016654 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016655 MinAlign(Ld->getAlignment(), 4));
16656
16657 SDValue NewChain = LoLd.getValue(1);
16658 if (TokenFactorIndex != -1) {
16659 Ops.push_back(LoLd);
16660 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016661 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016662 Ops.size());
16663 }
16664
16665 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016666 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16667 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016668
16669 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016670 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016671 St->isVolatile(), St->isNonTemporal(),
16672 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016673 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016674 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016675 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016676 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016677 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016678 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016679 }
Dan Gohman475871a2008-07-27 21:46:04 +000016680 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016681}
16682
Duncan Sands17470be2011-09-22 20:15:48 +000016683/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16684/// and return the operands for the horizontal operation in LHS and RHS. A
16685/// horizontal operation performs the binary operation on successive elements
16686/// of its first operand, then on successive elements of its second operand,
16687/// returning the resulting values in a vector. For example, if
16688/// A = < float a0, float a1, float a2, float a3 >
16689/// and
16690/// B = < float b0, float b1, float b2, float b3 >
16691/// then the result of doing a horizontal operation on A and B is
16692/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16693/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16694/// A horizontal-op B, for some already available A and B, and if so then LHS is
16695/// set to A, RHS to B, and the routine returns 'true'.
16696/// Note that the binary operation should have the property that if one of the
16697/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016698static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016699 // Look for the following pattern: if
16700 // A = < float a0, float a1, float a2, float a3 >
16701 // B = < float b0, float b1, float b2, float b3 >
16702 // and
16703 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
16704 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
16705 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
16706 // which is A horizontal-op B.
16707
16708 // At least one of the operands should be a vector shuffle.
16709 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
16710 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
16711 return false;
16712
16713 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000016714
16715 assert((VT.is128BitVector() || VT.is256BitVector()) &&
16716 "Unsupported vector type for horizontal add/sub");
16717
16718 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
16719 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000016720 unsigned NumElts = VT.getVectorNumElements();
16721 unsigned NumLanes = VT.getSizeInBits()/128;
16722 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000016723 assert((NumLaneElts % 2 == 0) &&
16724 "Vector type should have an even number of elements in each lane");
16725 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000016726
16727 // View LHS in the form
16728 // LHS = VECTOR_SHUFFLE A, B, LMask
16729 // If LHS is not a shuffle then pretend it is the shuffle
16730 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
16731 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
16732 // type VT.
16733 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000016734 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016735 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16736 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
16737 A = LHS.getOperand(0);
16738 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
16739 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016740 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
16741 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016742 } else {
16743 if (LHS.getOpcode() != ISD::UNDEF)
16744 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016745 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016746 LMask[i] = i;
16747 }
16748
16749 // Likewise, view RHS in the form
16750 // RHS = VECTOR_SHUFFLE C, D, RMask
16751 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000016752 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016753 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
16754 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
16755 C = RHS.getOperand(0);
16756 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
16757 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000016758 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
16759 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000016760 } else {
16761 if (RHS.getOpcode() != ISD::UNDEF)
16762 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000016763 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000016764 RMask[i] = i;
16765 }
16766
16767 // Check that the shuffles are both shuffling the same vectors.
16768 if (!(A == C && B == D) && !(A == D && B == C))
16769 return false;
16770
16771 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
16772 if (!A.getNode() && !B.getNode())
16773 return false;
16774
16775 // If A and B occur in reverse order in RHS, then "swap" them (which means
16776 // rewriting the mask).
16777 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000016778 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000016779
16780 // At this point LHS and RHS are equivalent to
16781 // LHS = VECTOR_SHUFFLE A, B, LMask
16782 // RHS = VECTOR_SHUFFLE A, B, RMask
16783 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000016784 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000016785 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000016786
Craig Topperf8363302011-12-02 08:18:41 +000016787 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016788 if (LIdx < 0 || RIdx < 0 ||
16789 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
16790 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000016791 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000016792
Craig Topperf8363302011-12-02 08:18:41 +000016793 // Check that successive elements are being operated on. If not, this is
16794 // not a horizontal operation.
16795 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
16796 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000016797 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000016798 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000016799 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000016800 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000016801 }
16802
16803 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
16804 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
16805 return true;
16806}
16807
16808/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
16809static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
16810 const X86Subtarget *Subtarget) {
16811 EVT VT = N->getValueType(0);
16812 SDValue LHS = N->getOperand(0);
16813 SDValue RHS = N->getOperand(1);
16814
16815 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016816 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016817 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016818 isHorizontalBinOp(LHS, RHS, true))
16819 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
16820 return SDValue();
16821}
16822
16823/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
16824static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
16825 const X86Subtarget *Subtarget) {
16826 EVT VT = N->getValueType(0);
16827 SDValue LHS = N->getOperand(0);
16828 SDValue RHS = N->getOperand(1);
16829
16830 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016831 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016832 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000016833 isHorizontalBinOp(LHS, RHS, false))
16834 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
16835 return SDValue();
16836}
16837
Chris Lattner6cf73262008-01-25 06:14:17 +000016838/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
16839/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016840static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000016841 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
16842 // F[X]OR(0.0, x) -> x
16843 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000016844 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16845 if (C->getValueAPF().isPosZero())
16846 return N->getOperand(1);
16847 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16848 if (C->getValueAPF().isPosZero())
16849 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000016850 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016851}
16852
Nadav Rotemd60cb112012-08-19 13:06:16 +000016853/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
16854/// X86ISD::FMAX nodes.
16855static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
16856 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
16857
16858 // Only perform optimizations if UnsafeMath is used.
16859 if (!DAG.getTarget().Options.UnsafeFPMath)
16860 return SDValue();
16861
16862 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000016863 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000016864 unsigned NewOp = 0;
16865 switch (N->getOpcode()) {
16866 default: llvm_unreachable("unknown opcode");
16867 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
16868 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
16869 }
16870
16871 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
16872 N->getOperand(0), N->getOperand(1));
16873}
16874
Chris Lattneraf723b92008-01-25 05:46:26 +000016875/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016876static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000016877 // FAND(0.0, x) -> 0.0
16878 // FAND(x, 0.0) -> 0.0
16879 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
16880 if (C->getValueAPF().isPosZero())
16881 return N->getOperand(0);
16882 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
16883 if (C->getValueAPF().isPosZero())
16884 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000016885 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000016886}
16887
Dan Gohmane5af2d32009-01-29 01:59:02 +000016888static SDValue PerformBTCombine(SDNode *N,
16889 SelectionDAG &DAG,
16890 TargetLowering::DAGCombinerInfo &DCI) {
16891 // BT ignores high bits in the bit index operand.
16892 SDValue Op1 = N->getOperand(1);
16893 if (Op1.hasOneUse()) {
16894 unsigned BitWidth = Op1.getValueSizeInBits();
16895 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
16896 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016897 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
16898 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000016899 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000016900 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
16901 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
16902 DCI.CommitTargetLoweringOpt(TLO);
16903 }
16904 return SDValue();
16905}
Chris Lattner83e6c992006-10-04 06:57:07 +000016906
Eli Friedman7a5e5552009-06-07 06:52:44 +000016907static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
16908 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016909 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000016910 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000016911 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000016912 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000016913 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000016914 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000016915 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016916 }
16917 return SDValue();
16918}
16919
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016920static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
16921 TargetLowering::DAGCombinerInfo &DCI,
16922 const X86Subtarget *Subtarget) {
16923 if (!DCI.isBeforeLegalizeOps())
16924 return SDValue();
16925
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016926 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000016927 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016928
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016929 EVT VT = N->getValueType(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016930 if (VT.isVector() && VT.getSizeInBits() == 256) {
16931 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
16932 if (R.getNode())
16933 return R;
16934 }
16935
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016936 return SDValue();
16937}
16938
Michael Liaof6c24ee2012-08-10 14:39:24 +000016939static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016940 const X86Subtarget* Subtarget) {
16941 DebugLoc dl = N->getDebugLoc();
16942 EVT VT = N->getValueType(0);
16943
Craig Topperb1bdd7d2012-08-30 06:56:15 +000016944 // Let legalize expand this if it isn't a legal type yet.
16945 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
16946 return SDValue();
16947
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016948 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000016949 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
16950 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016951 return SDValue();
16952
16953 SDValue A = N->getOperand(0);
16954 SDValue B = N->getOperand(1);
16955 SDValue C = N->getOperand(2);
16956
16957 bool NegA = (A.getOpcode() == ISD::FNEG);
16958 bool NegB = (B.getOpcode() == ISD::FNEG);
16959 bool NegC = (C.getOpcode() == ISD::FNEG);
16960
Michael Liaof6c24ee2012-08-10 14:39:24 +000016961 // Negative multiplication when NegA xor NegB
16962 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016963 if (NegA)
16964 A = A.getOperand(0);
16965 if (NegB)
16966 B = B.getOperand(0);
16967 if (NegC)
16968 C = C.getOperand(0);
16969
16970 unsigned Opcode;
16971 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000016972 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016973 else
Craig Topperbf404372012-08-31 15:40:30 +000016974 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
16975
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016976 return DAG.getNode(Opcode, dl, VT, A, B, C);
16977}
16978
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016979static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000016980 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016981 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000016982 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
16983 // (and (i32 x86isd::setcc_carry), 1)
16984 // This eliminates the zext. This transformation is necessary because
16985 // ISD::SETCC is always legalized to i8.
16986 DebugLoc dl = N->getDebugLoc();
16987 SDValue N0 = N->getOperand(0);
16988 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000016989
Evan Cheng2e489c42009-12-16 00:53:11 +000016990 if (N0.getOpcode() == ISD::AND &&
16991 N0.hasOneUse() &&
16992 N0.getOperand(0).hasOneUse()) {
16993 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016994 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
16995 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
16996 if (!C || C->getZExtValue() != 1)
16997 return SDValue();
16998 return DAG.getNode(ISD::AND, dl, VT,
16999 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17000 N00.getOperand(0), N00.getOperand(1)),
17001 DAG.getConstant(1, VT));
17002 }
17003 }
17004
17005 if (VT.isVector() && VT.getSizeInBits() == 256) {
17006 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17007 if (R.getNode())
17008 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017009 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017010
Evan Cheng2e489c42009-12-16 00:53:11 +000017011 return SDValue();
17012}
17013
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017014// Optimize x == -y --> x+y == 0
17015// x != -y --> x+y != 0
17016static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17017 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17018 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017019 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017020
17021 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17022 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17023 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17024 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17025 LHS.getValueType(), RHS, LHS.getOperand(1));
17026 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17027 addV, DAG.getConstant(0, addV.getValueType()), CC);
17028 }
17029 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17031 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17032 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17033 RHS.getValueType(), LHS, RHS.getOperand(1));
17034 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17035 addV, DAG.getConstant(0, addV.getValueType()), CC);
17036 }
17037 return SDValue();
17038}
17039
Shuxin Yanga5526a92012-10-31 23:11:48 +000017040// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17041// as "sbb reg,reg", since it can be extended without zext and produces
17042// an all-ones bit which is more useful than 0/1 in some cases.
17043static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17044 return DAG.getNode(ISD::AND, DL, MVT::i8,
17045 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17046 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17047 DAG.getConstant(1, MVT::i8));
17048}
17049
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017050// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017051static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17052 TargetLowering::DAGCombinerInfo &DCI,
17053 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017054 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017055 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17056 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017057
Shuxin Yanga5526a92012-10-31 23:11:48 +000017058 if (CC == X86::COND_A) {
17059 // Try to convert COND_A into COND_B in an attempt to facilitate
17060 // materializing "setb reg".
17061 //
17062 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17063 // cannot take an immediate as its first operand.
17064 //
17065 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
17066 EFLAGS.getValueType().isInteger() &&
17067 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17068 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17069 EFLAGS.getNode()->getVTList(),
17070 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17071 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17072 return MaterializeSETB(DL, NewEFLAGS, DAG);
17073 }
17074 }
17075
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017076 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17077 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17078 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017079 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017080 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017081
Michael Liao2a33cec2012-08-10 19:58:13 +000017082 SDValue Flags;
17083
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017084 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17085 if (Flags.getNode()) {
17086 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17087 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17088 }
17089
Michael Liao2a33cec2012-08-10 19:58:13 +000017090 return SDValue();
17091}
17092
17093// Optimize branch condition evaluation.
17094//
17095static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17096 TargetLowering::DAGCombinerInfo &DCI,
17097 const X86Subtarget *Subtarget) {
17098 DebugLoc DL = N->getDebugLoc();
17099 SDValue Chain = N->getOperand(0);
17100 SDValue Dest = N->getOperand(1);
17101 SDValue EFLAGS = N->getOperand(3);
17102 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17103
17104 SDValue Flags;
17105
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017106 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17107 if (Flags.getNode()) {
17108 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17109 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17110 Flags);
17111 }
17112
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017113 return SDValue();
17114}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017115
Benjamin Kramer1396c402011-06-18 11:09:41 +000017116static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17117 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017118 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017119 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017120
17121 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017122 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017123 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017124 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017125 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17126 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17127 }
17128
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017129 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17130 // a 32-bit target where SSE doesn't support i64->FP operations.
17131 if (Op0.getOpcode() == ISD::LOAD) {
17132 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17133 EVT VT = Ld->getValueType(0);
17134 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17135 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17136 !XTLI->getSubtarget()->is64Bit() &&
17137 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017138 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17139 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017140 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17141 return FILDChain;
17142 }
17143 }
17144 return SDValue();
17145}
17146
Chris Lattner23a01992010-12-20 01:37:09 +000017147// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17148static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17149 X86TargetLowering::DAGCombinerInfo &DCI) {
17150 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17151 // the result is either zero or one (depending on the input carry bit).
17152 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17153 if (X86::isZeroNode(N->getOperand(0)) &&
17154 X86::isZeroNode(N->getOperand(1)) &&
17155 // We don't have a good way to replace an EFLAGS use, so only do this when
17156 // dead right now.
17157 SDValue(N, 1).use_empty()) {
17158 DebugLoc DL = N->getDebugLoc();
17159 EVT VT = N->getValueType(0);
17160 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17161 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17162 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17163 DAG.getConstant(X86::COND_B,MVT::i8),
17164 N->getOperand(2)),
17165 DAG.getConstant(1, VT));
17166 return DCI.CombineTo(N, Res1, CarryOut);
17167 }
17168
17169 return SDValue();
17170}
17171
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017172// fold (add Y, (sete X, 0)) -> adc 0, Y
17173// (add Y, (setne X, 0)) -> sbb -1, Y
17174// (sub (sete X, 0), Y) -> sbb 0, Y
17175// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017176static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017177 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017178
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017179 // Look through ZExts.
17180 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17181 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17182 return SDValue();
17183
17184 SDValue SetCC = Ext.getOperand(0);
17185 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17186 return SDValue();
17187
17188 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17189 if (CC != X86::COND_E && CC != X86::COND_NE)
17190 return SDValue();
17191
17192 SDValue Cmp = SetCC.getOperand(1);
17193 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017194 !X86::isZeroNode(Cmp.getOperand(1)) ||
17195 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017196 return SDValue();
17197
17198 SDValue CmpOp0 = Cmp.getOperand(0);
17199 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17200 DAG.getConstant(1, CmpOp0.getValueType()));
17201
17202 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17203 if (CC == X86::COND_NE)
17204 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17205 DL, OtherVal.getValueType(), OtherVal,
17206 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17207 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17208 DL, OtherVal.getValueType(), OtherVal,
17209 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17210}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017211
Craig Topper54f952a2011-11-19 09:02:40 +000017212/// PerformADDCombine - Do target-specific dag combines on integer adds.
17213static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17214 const X86Subtarget *Subtarget) {
17215 EVT VT = N->getValueType(0);
17216 SDValue Op0 = N->getOperand(0);
17217 SDValue Op1 = N->getOperand(1);
17218
17219 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017220 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017221 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017222 isHorizontalBinOp(Op0, Op1, true))
17223 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17224
17225 return OptimizeConditionalInDecrement(N, DAG);
17226}
17227
17228static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17229 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017230 SDValue Op0 = N->getOperand(0);
17231 SDValue Op1 = N->getOperand(1);
17232
17233 // X86 can't encode an immediate LHS of a sub. See if we can push the
17234 // negation into a preceding instruction.
17235 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017236 // If the RHS of the sub is a XOR with one use and a constant, invert the
17237 // immediate. Then add one to the LHS of the sub so we can turn
17238 // X-Y -> X+~Y+1, saving one register.
17239 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17240 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017241 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017242 EVT VT = Op0.getValueType();
17243 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17244 Op1.getOperand(0),
17245 DAG.getConstant(~XorC, VT));
17246 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017247 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017248 }
17249 }
17250
Craig Topper54f952a2011-11-19 09:02:40 +000017251 // Try to synthesize horizontal adds from adds of shuffles.
17252 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017253 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017254 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017255 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017256 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17257
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017258 return OptimizeConditionalInDecrement(N, DAG);
17259}
17260
Michael Liaod9d09602012-10-23 17:34:00 +000017261/// performVZEXTCombine - Performs build vector combines
17262static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17263 TargetLowering::DAGCombinerInfo &DCI,
17264 const X86Subtarget *Subtarget) {
17265 // (vzext (bitcast (vzext (x)) -> (vzext x)
17266 SDValue In = N->getOperand(0);
17267 while (In.getOpcode() == ISD::BITCAST)
17268 In = In.getOperand(0);
17269
17270 if (In.getOpcode() != X86ISD::VZEXT)
17271 return SDValue();
17272
17273 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0), In.getOperand(0));
17274}
17275
Dan Gohman475871a2008-07-27 21:46:04 +000017276SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017277 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017278 SelectionDAG &DAG = DCI.DAG;
17279 switch (N->getOpcode()) {
17280 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017281 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017282 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017283 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017284 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017285 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017286 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17287 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017288 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017289 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017290 case ISD::SHL:
17291 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017292 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017293 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017294 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017295 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017296 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017297 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017298 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017299 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17300 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017301 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017302 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017303 case X86ISD::FMIN:
17304 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017305 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017306 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017307 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017308 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017309 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017310 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017311 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017312 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017313 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017314 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017315 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017316 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000017317 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000017318 case X86ISD::UNPCKH:
17319 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017320 case X86ISD::MOVHLPS:
17321 case X86ISD::MOVLHPS:
17322 case X86ISD::PSHUFD:
17323 case X86ISD::PSHUFHW:
17324 case X86ISD::PSHUFLW:
17325 case X86ISD::MOVSS:
17326 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017327 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017328 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017329 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017330 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017331 }
17332
Dan Gohman475871a2008-07-27 21:46:04 +000017333 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017334}
17335
Evan Chenge5b51ac2010-04-17 06:13:15 +000017336/// isTypeDesirableForOp - Return true if the target has native support for
17337/// the specified value type and it is 'desirable' to use the type for the
17338/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17339/// instruction encodings are longer and some i16 instructions are slow.
17340bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17341 if (!isTypeLegal(VT))
17342 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017343 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017344 return true;
17345
17346 switch (Opc) {
17347 default:
17348 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017349 case ISD::LOAD:
17350 case ISD::SIGN_EXTEND:
17351 case ISD::ZERO_EXTEND:
17352 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017353 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017354 case ISD::SRL:
17355 case ISD::SUB:
17356 case ISD::ADD:
17357 case ISD::MUL:
17358 case ISD::AND:
17359 case ISD::OR:
17360 case ISD::XOR:
17361 return false;
17362 }
17363}
17364
17365/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017366/// beneficial for dag combiner to promote the specified node. If true, it
17367/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017368bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017369 EVT VT = Op.getValueType();
17370 if (VT != MVT::i16)
17371 return false;
17372
Evan Cheng4c26e932010-04-19 19:29:22 +000017373 bool Promote = false;
17374 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017375 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017376 default: break;
17377 case ISD::LOAD: {
17378 LoadSDNode *LD = cast<LoadSDNode>(Op);
17379 // If the non-extending load has a single use and it's not live out, then it
17380 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017381 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17382 Op.hasOneUse()*/) {
17383 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17384 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17385 // The only case where we'd want to promote LOAD (rather then it being
17386 // promoted as an operand is when it's only use is liveout.
17387 if (UI->getOpcode() != ISD::CopyToReg)
17388 return false;
17389 }
17390 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017391 Promote = true;
17392 break;
17393 }
17394 case ISD::SIGN_EXTEND:
17395 case ISD::ZERO_EXTEND:
17396 case ISD::ANY_EXTEND:
17397 Promote = true;
17398 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017399 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017400 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017401 SDValue N0 = Op.getOperand(0);
17402 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017403 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017404 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017405 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017406 break;
17407 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017408 case ISD::ADD:
17409 case ISD::MUL:
17410 case ISD::AND:
17411 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017412 case ISD::XOR:
17413 Commute = true;
17414 // fallthrough
17415 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017416 SDValue N0 = Op.getOperand(0);
17417 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017418 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017419 return false;
17420 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017421 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017422 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017423 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017424 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017425 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017426 }
17427 }
17428
17429 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017430 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017431}
17432
Evan Cheng60c07e12006-07-05 22:17:51 +000017433//===----------------------------------------------------------------------===//
17434// X86 Inline Assembly Support
17435//===----------------------------------------------------------------------===//
17436
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017437namespace {
17438 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017439 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017440 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017441
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017442 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017443 StringRef piece(*args[i]);
17444 if (!s.startswith(piece)) // Check if the piece matches.
17445 return false;
17446
17447 s = s.substr(piece.size());
17448 StringRef::size_type pos = s.find_first_not_of(" \t");
17449 if (pos == 0) // We matched a prefix.
17450 return false;
17451
17452 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017453 }
17454
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017455 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017456 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017457 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017458}
17459
Chris Lattnerb8105652009-07-20 17:51:36 +000017460bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17461 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017462
17463 std::string AsmStr = IA->getAsmString();
17464
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017465 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17466 if (!Ty || Ty->getBitWidth() % 16 != 0)
17467 return false;
17468
Chris Lattnerb8105652009-07-20 17:51:36 +000017469 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017470 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017471 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017472
17473 switch (AsmPieces.size()) {
17474 default: return false;
17475 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017476 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017477 // we will turn this bswap into something that will be lowered to logical
17478 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17479 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017480 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017481 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17482 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17483 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17484 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17485 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17486 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017487 // No need to check constraints, nothing other than the equivalent of
17488 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017489 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017490 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017491
Chris Lattnerb8105652009-07-20 17:51:36 +000017492 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017493 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017494 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017495 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17496 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017497 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017498 const std::string &ConstraintsStr = IA->getConstraintString();
17499 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000017500 std::sort(AsmPieces.begin(), AsmPieces.end());
17501 if (AsmPieces.size() == 4 &&
17502 AsmPieces[0] == "~{cc}" &&
17503 AsmPieces[1] == "~{dirflag}" &&
17504 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017505 AsmPieces[3] == "~{fpsr}")
17506 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017507 }
17508 break;
17509 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017510 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017511 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017512 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17513 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17514 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017515 AsmPieces.clear();
17516 const std::string &ConstraintsStr = IA->getConstraintString();
17517 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
17518 std::sort(AsmPieces.begin(), AsmPieces.end());
17519 if (AsmPieces.size() == 4 &&
17520 AsmPieces[0] == "~{cc}" &&
17521 AsmPieces[1] == "~{dirflag}" &&
17522 AsmPieces[2] == "~{flags}" &&
17523 AsmPieces[3] == "~{fpsr}")
17524 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017525 }
Evan Cheng55d42002011-01-08 01:24:27 +000017526
17527 if (CI->getType()->isIntegerTy(64)) {
17528 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17529 if (Constraints.size() >= 2 &&
17530 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17531 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17532 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017533 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17534 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17535 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017536 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017537 }
17538 }
17539 break;
17540 }
17541 return false;
17542}
17543
Chris Lattnerf4dff842006-07-11 02:54:03 +000017544/// getConstraintType - Given a constraint letter, return the type of
17545/// constraint it is for this target.
17546X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017547X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17548 if (Constraint.size() == 1) {
17549 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017550 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017551 case 'q':
17552 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017553 case 'f':
17554 case 't':
17555 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017556 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017557 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017558 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017559 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017560 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017561 case 'a':
17562 case 'b':
17563 case 'c':
17564 case 'd':
17565 case 'S':
17566 case 'D':
17567 case 'A':
17568 return C_Register;
17569 case 'I':
17570 case 'J':
17571 case 'K':
17572 case 'L':
17573 case 'M':
17574 case 'N':
17575 case 'G':
17576 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017577 case 'e':
17578 case 'Z':
17579 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017580 default:
17581 break;
17582 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017583 }
Chris Lattner4234f572007-03-25 02:14:49 +000017584 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017585}
17586
John Thompson44ab89e2010-10-29 17:29:13 +000017587/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017588/// This object must already have been set up with the operand type
17589/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017590TargetLowering::ConstraintWeight
17591 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017592 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017593 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017594 Value *CallOperandVal = info.CallOperandVal;
17595 // If we don't have a value, we can't do a match,
17596 // but allow it at the lowest weight.
17597 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017598 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017599 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017600 // Look at the constraint type.
17601 switch (*constraint) {
17602 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017603 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17604 case 'R':
17605 case 'q':
17606 case 'Q':
17607 case 'a':
17608 case 'b':
17609 case 'c':
17610 case 'd':
17611 case 'S':
17612 case 'D':
17613 case 'A':
17614 if (CallOperandVal->getType()->isIntegerTy())
17615 weight = CW_SpecificReg;
17616 break;
17617 case 'f':
17618 case 't':
17619 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017620 if (type->isFloatingPointTy())
17621 weight = CW_SpecificReg;
17622 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017623 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017624 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17625 weight = CW_SpecificReg;
17626 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017627 case 'x':
17628 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017629 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017630 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017631 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017632 break;
17633 case 'I':
17634 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17635 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017636 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017637 }
17638 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017639 case 'J':
17640 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17641 if (C->getZExtValue() <= 63)
17642 weight = CW_Constant;
17643 }
17644 break;
17645 case 'K':
17646 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17647 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17648 weight = CW_Constant;
17649 }
17650 break;
17651 case 'L':
17652 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17653 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17654 weight = CW_Constant;
17655 }
17656 break;
17657 case 'M':
17658 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17659 if (C->getZExtValue() <= 3)
17660 weight = CW_Constant;
17661 }
17662 break;
17663 case 'N':
17664 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17665 if (C->getZExtValue() <= 0xff)
17666 weight = CW_Constant;
17667 }
17668 break;
17669 case 'G':
17670 case 'C':
17671 if (dyn_cast<ConstantFP>(CallOperandVal)) {
17672 weight = CW_Constant;
17673 }
17674 break;
17675 case 'e':
17676 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17677 if ((C->getSExtValue() >= -0x80000000LL) &&
17678 (C->getSExtValue() <= 0x7fffffffLL))
17679 weight = CW_Constant;
17680 }
17681 break;
17682 case 'Z':
17683 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17684 if (C->getZExtValue() <= 0xffffffff)
17685 weight = CW_Constant;
17686 }
17687 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017688 }
17689 return weight;
17690}
17691
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017692/// LowerXConstraint - try to replace an X constraint, which matches anything,
17693/// with another that has more specific requirements based on the type of the
17694/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000017695const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000017696LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000017697 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
17698 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000017699 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000017700 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000017701 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000017702 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000017703 return "x";
17704 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017705
Chris Lattner5e764232008-04-26 23:02:14 +000017706 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000017707}
17708
Chris Lattner48884cd2007-08-25 00:47:38 +000017709/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
17710/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000017711void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000017712 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000017713 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000017714 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000017715 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000017716
Eric Christopher100c8332011-06-02 23:16:42 +000017717 // Only support length 1 constraints for now.
17718 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000017719
Eric Christopher100c8332011-06-02 23:16:42 +000017720 char ConstraintLetter = Constraint[0];
17721 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017722 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000017723 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000017724 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017725 if (C->getZExtValue() <= 31) {
17726 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017727 break;
17728 }
Devang Patel84f7fd22007-03-17 00:13:28 +000017729 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017730 return;
Evan Cheng364091e2008-09-22 23:57:37 +000017731 case 'J':
17732 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000017733 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000017734 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17735 break;
17736 }
17737 }
17738 return;
17739 case 'K':
17740 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000017741 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000017742 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17743 break;
17744 }
17745 }
17746 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000017747 case 'N':
17748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000017749 if (C->getZExtValue() <= 255) {
17750 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000017751 break;
17752 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000017753 }
Chris Lattner48884cd2007-08-25 00:47:38 +000017754 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000017755 case 'e': {
17756 // 32-bit signed value
17757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017758 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17759 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017760 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017761 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000017762 break;
17763 }
17764 // FIXME gcc accepts some relocatable values here too, but only in certain
17765 // memory models; it's complicated.
17766 }
17767 return;
17768 }
17769 case 'Z': {
17770 // 32-bit unsigned value
17771 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000017772 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
17773 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017774 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
17775 break;
17776 }
17777 }
17778 // FIXME gcc accepts some relocatable values here too, but only in certain
17779 // memory models; it's complicated.
17780 return;
17781 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017782 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017783 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000017784 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000017785 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000017786 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000017787 break;
17788 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017789
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017790 // In any sort of PIC mode addresses need to be computed at runtime by
17791 // adding in a register or some sort of table lookup. These can't
17792 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000017793 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000017794 return;
17795
Chris Lattnerdc43a882007-05-03 16:52:29 +000017796 // If we are in non-pic codegen mode, we allow the address of a global (with
17797 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000017798 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017799 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000017800
Chris Lattner49921962009-05-08 18:23:14 +000017801 // Match either (GA), (GA+C), (GA+C1+C2), etc.
17802 while (1) {
17803 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
17804 Offset += GA->getOffset();
17805 break;
17806 } else if (Op.getOpcode() == ISD::ADD) {
17807 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17808 Offset += C->getZExtValue();
17809 Op = Op.getOperand(0);
17810 continue;
17811 }
17812 } else if (Op.getOpcode() == ISD::SUB) {
17813 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
17814 Offset += -C->getZExtValue();
17815 Op = Op.getOperand(0);
17816 continue;
17817 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017818 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017819
Chris Lattner49921962009-05-08 18:23:14 +000017820 // Otherwise, this isn't something we can handle, reject it.
17821 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000017822 }
Eric Christopherfd179292009-08-27 18:07:15 +000017823
Dan Gohman46510a72010-04-15 01:51:59 +000017824 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017825 // If we require an extra load to get this address, as in PIC mode, we
17826 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000017827 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
17828 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000017829 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000017830
Devang Patel0d881da2010-07-06 22:08:15 +000017831 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
17832 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000017833 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017834 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000017835 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017836
Gabor Greifba36cb52008-08-28 21:40:38 +000017837 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000017838 Ops.push_back(Result);
17839 return;
17840 }
Dale Johannesen1784d162010-06-25 21:55:36 +000017841 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000017842}
17843
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017844std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000017845X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000017846 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000017847 // First, see if this is a constraint that directly corresponds to an LLVM
17848 // register class.
17849 if (Constraint.size() == 1) {
17850 // GCC Constraint Letters
17851 switch (Constraint[0]) {
17852 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000017853 // TODO: Slight differences here in allocation order and leaving
17854 // RIP in the class. Do they matter any more here than they do
17855 // in the normal allocation?
17856 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
17857 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000017858 if (VT == MVT::i32 || VT == MVT::f32)
17859 return std::make_pair(0U, &X86::GR32RegClass);
17860 if (VT == MVT::i16)
17861 return std::make_pair(0U, &X86::GR16RegClass);
17862 if (VT == MVT::i8 || VT == MVT::i1)
17863 return std::make_pair(0U, &X86::GR8RegClass);
17864 if (VT == MVT::i64 || VT == MVT::f64)
17865 return std::make_pair(0U, &X86::GR64RegClass);
17866 break;
Eric Christopherd176af82011-06-29 17:23:50 +000017867 }
17868 // 32-bit fallthrough
17869 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000017870 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000017871 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
17872 if (VT == MVT::i16)
17873 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
17874 if (VT == MVT::i8 || VT == MVT::i1)
17875 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
17876 if (VT == MVT::i64)
17877 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000017878 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017879 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000017880 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017881 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017882 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017883 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017884 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000017885 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017886 return std::make_pair(0U, &X86::GR32RegClass);
17887 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017888 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000017889 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000017890 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017891 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000017892 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000017893 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000017894 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
17895 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000017896 case 'f': // FP Stack registers.
17897 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
17898 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000017899 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017900 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017901 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000017902 return std::make_pair(0U, &X86::RFP64RegClass);
17903 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000017904 case 'y': // MMX_REGS if MMX allowed.
17905 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000017906 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017907 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017908 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000017909 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000017910 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000017911 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000017912
Owen Anderson825b72b2009-08-11 20:47:22 +000017913 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000017914 default: break;
17915 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017916 case MVT::f32:
17917 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000017918 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000017919 case MVT::f64:
17920 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000017921 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017922 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000017923 case MVT::v16i8:
17924 case MVT::v8i16:
17925 case MVT::v4i32:
17926 case MVT::v2i64:
17927 case MVT::v4f32:
17928 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000017929 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000017930 // AVX types.
17931 case MVT::v32i8:
17932 case MVT::v16i16:
17933 case MVT::v8i32:
17934 case MVT::v4i64:
17935 case MVT::v8f32:
17936 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000017937 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000017938 }
Chris Lattnerad043e82007-04-09 05:11:28 +000017939 break;
17940 }
17941 }
Scott Michelfdc40a02009-02-17 22:15:04 +000017942
Chris Lattnerf76d1802006-07-31 23:26:50 +000017943 // Use the default implementation in TargetLowering to convert the register
17944 // constraint into a member of a register class.
17945 std::pair<unsigned, const TargetRegisterClass*> Res;
17946 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000017947
17948 // Not found as a standard register?
17949 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017950 // Map st(0) -> st(7) -> ST0
17951 if (Constraint.size() == 7 && Constraint[0] == '{' &&
17952 tolower(Constraint[1]) == 's' &&
17953 tolower(Constraint[2]) == 't' &&
17954 Constraint[3] == '(' &&
17955 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
17956 Constraint[5] == ')' &&
17957 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000017958
Chris Lattner56d77c72009-09-13 22:41:48 +000017959 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000017960 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017961 return Res;
17962 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017963
Chris Lattner56d77c72009-09-13 22:41:48 +000017964 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017965 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000017966 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000017967 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017968 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000017969 }
Chris Lattner56d77c72009-09-13 22:41:48 +000017970
17971 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000017972 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000017973 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000017974 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017975 return Res;
17976 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000017977
Dale Johannesen330169f2008-11-13 21:52:36 +000017978 // 'A' means EAX + EDX.
17979 if (Constraint == "A") {
17980 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000017981 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000017982 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000017983 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000017984 return Res;
17985 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017986
Chris Lattnerf76d1802006-07-31 23:26:50 +000017987 // Otherwise, check to see if this is a register class of the wrong value
17988 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
17989 // turn into {ax},{dx}.
17990 if (Res.second->hasType(VT))
17991 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000017992
Chris Lattnerf76d1802006-07-31 23:26:50 +000017993 // All of the single-register GCC register classes map their values onto
17994 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
17995 // really want an 8-bit or 32-bit register, map to the appropriate register
17996 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000017997 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017998 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000017999 unsigned DestReg = 0;
18000 switch (Res.first) {
18001 default: break;
18002 case X86::AX: DestReg = X86::AL; break;
18003 case X86::DX: DestReg = X86::DL; break;
18004 case X86::CX: DestReg = X86::CL; break;
18005 case X86::BX: DestReg = X86::BL; break;
18006 }
18007 if (DestReg) {
18008 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018009 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018010 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018011 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018012 unsigned DestReg = 0;
18013 switch (Res.first) {
18014 default: break;
18015 case X86::AX: DestReg = X86::EAX; break;
18016 case X86::DX: DestReg = X86::EDX; break;
18017 case X86::CX: DestReg = X86::ECX; break;
18018 case X86::BX: DestReg = X86::EBX; break;
18019 case X86::SI: DestReg = X86::ESI; break;
18020 case X86::DI: DestReg = X86::EDI; break;
18021 case X86::BP: DestReg = X86::EBP; break;
18022 case X86::SP: DestReg = X86::ESP; break;
18023 }
18024 if (DestReg) {
18025 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018026 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018027 }
Owen Anderson825b72b2009-08-11 20:47:22 +000018028 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018029 unsigned DestReg = 0;
18030 switch (Res.first) {
18031 default: break;
18032 case X86::AX: DestReg = X86::RAX; break;
18033 case X86::DX: DestReg = X86::RDX; break;
18034 case X86::CX: DestReg = X86::RCX; break;
18035 case X86::BX: DestReg = X86::RBX; break;
18036 case X86::SI: DestReg = X86::RSI; break;
18037 case X86::DI: DestReg = X86::RDI; break;
18038 case X86::BP: DestReg = X86::RBP; break;
18039 case X86::SP: DestReg = X86::RSP; break;
18040 }
18041 if (DestReg) {
18042 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018043 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018044 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018045 }
Craig Topperc9099502012-04-20 06:31:50 +000018046 } else if (Res.second == &X86::FR32RegClass ||
18047 Res.second == &X86::FR64RegClass ||
18048 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018049 // Handle references to XMM physical registers that got mapped into the
18050 // wrong class. This can happen with constraints like {xmm0} where the
18051 // target independent register mapper will just pick the first match it can
18052 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018053
18054 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018055 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018056 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018057 Res.second = &X86::FR64RegClass;
18058 else if (X86::VR128RegClass.hasType(VT))
18059 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018060 else if (X86::VR256RegClass.hasType(VT))
18061 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018062 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018063
Chris Lattnerf76d1802006-07-31 23:26:50 +000018064 return Res;
18065}
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018066
Nadav Roteme6237022012-11-05 19:32:46 +000018067//===----------------------------------------------------------------------===//
18068//
18069// X86 cost model.
18070//
18071//===----------------------------------------------------------------------===//
18072
18073struct X86CostTblEntry {
18074 int ISD;
18075 MVT Type;
18076 unsigned Cost;
18077};
18078
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000018079static int
18080FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018081 for (unsigned int i = 0; i < len; ++i)
18082 if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
18083 return i;
18084
18085 // Could not find an entry.
18086 return -1;
18087}
18088
Nadav Rotemb0428682012-11-06 19:33:53 +000018089struct X86TypeConversionCostTblEntry {
18090 int ISD;
18091 MVT Dst;
18092 MVT Src;
18093 unsigned Cost;
18094};
18095
Nadav Rotemd8eae8b2012-11-06 23:36:00 +000018096static int
18097FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
18098 int ISD, MVT Dst, MVT Src) {
Nadav Rotemb0428682012-11-06 19:33:53 +000018099 for (unsigned int i = 0; i < len; ++i)
18100 if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
18101 return i;
18102
18103 // Could not find an entry.
18104 return -1;
18105}
18106
Shuxin Yang5518a132012-12-09 03:12:46 +000018107ScalarTargetTransformInfo::PopcntHwSupport
18108X86ScalarTargetTransformImpl::getPopcntHwSupport(unsigned TyWidth) const {
18109 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
18110 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18111
18112 // TODO: Currently the __builtin_popcount() implementation using SSE3
18113 // instructions is inefficient. Once the problem is fixed, we should
18114 // call ST.hasSSE3() instead of ST.hasSSE4().
18115 return ST.hasSSE41() ? Fast : None;
18116}
18117
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018118unsigned
18119X86VectorTargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
18120 Type *Ty) const {
Nadav Roteme6237022012-11-05 19:32:46 +000018121 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018122 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Roteme6237022012-11-05 19:32:46 +000018123
18124 int ISD = InstructionOpcodeToISD(Opcode);
18125 assert(ISD && "Invalid opcode");
18126
Nadav Rotemb0428682012-11-06 19:33:53 +000018127 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018128
Nadav Roteme6237022012-11-05 19:32:46 +000018129 static const X86CostTblEntry AVX1CostTable[] = {
18130 // We don't have to scalarize unsupported ops. We can issue two half-sized
18131 // operations and we only need to extract the upper YMM half.
18132 // Two ops + 1 extract + 1 insert = 4.
18133 { ISD::MUL, MVT::v8i32, 4 },
18134 { ISD::SUB, MVT::v8i32, 4 },
18135 { ISD::ADD, MVT::v8i32, 4 },
18136 { ISD::MUL, MVT::v4i64, 4 },
18137 { ISD::SUB, MVT::v4i64, 4 },
18138 { ISD::ADD, MVT::v4i64, 4 },
18139 };
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018140
Nadav Roteme6237022012-11-05 19:32:46 +000018141 // Look for AVX1 lowering tricks.
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018142 if (ST.hasAVX()) {
18143 int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
18144 LT.second);
18145 if (Idx != -1)
18146 return LT.first * AVX1CostTable[Idx].Cost;
18147 }
Nadav Roteme6237022012-11-05 19:32:46 +000018148 // Fallback to the default implementation.
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018149 return VectorTargetTransformImpl::getArithmeticInstrCost(Opcode, Ty);
18150}
18151
Nadav Rotemf5637c32012-12-21 01:33:59 +000018152unsigned
18153X86VectorTargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
18154 unsigned Alignment,
18155 unsigned AddressSpace) const {
18156 // Legalize the type.
18157 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
Nick Lewycky71f30bf2012-12-24 19:58:45 +000018158 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
Nadav Rotemf5637c32012-12-21 01:33:59 +000018159 "Invalid Opcode");
18160
18161 const X86Subtarget &ST =
18162 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18163
18164 // Each load/store unit costs 1.
18165 unsigned Cost = LT.first * 1;
18166
18167 // On Sandybridge 256bit load/stores are double pumped
18168 // (but not on Haswell).
18169 if (LT.second.getSizeInBits() > 128 && !ST.hasAVX2())
18170 Cost*=2;
18171
18172 return Cost;
18173}
18174
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018175unsigned
18176X86VectorTargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
Richard Smithe010eb32012-11-05 22:01:44 +000018177 unsigned Index) const {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018178 assert(Val->isVectorTy() && "This must be a vector type");
18179
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018180 if (Index != -1U) {
Nadav Rotema4ab5292012-11-05 21:12:13 +000018181 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018182 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Val);
Nadav Rotema4ab5292012-11-05 21:12:13 +000018183
18184 // This type is legalized to a scalar type.
18185 if (!LT.second.isVector())
18186 return 0;
18187
18188 // The type may be split. Normalize the index to the new type.
18189 unsigned Width = LT.second.getVectorNumElements();
18190 Index = Index % Width;
18191
18192 // Floating point scalars are already located in index #0.
18193 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
18194 return 0;
18195 }
18196
Nadav Rotemb4b04c32012-11-03 00:39:56 +000018197 return VectorTargetTransformImpl::getVectorInstrCost(Opcode, Val, Index);
18198}
18199
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018200unsigned X86VectorTargetTransformInfo::getCmpSelInstrCost(unsigned Opcode,
18201 Type *ValTy,
18202 Type *CondTy) const {
18203 // Legalize the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +000018204 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018205
18206 MVT MTy = LT.second;
18207
18208 int ISD = InstructionOpcodeToISD(Opcode);
18209 assert(ISD && "Invalid opcode");
18210
18211 const X86Subtarget &ST =
18212 TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18213
18214 static const X86CostTblEntry SSE42CostTbl[] = {
18215 { ISD::SETCC, MVT::v2f64, 1 },
18216 { ISD::SETCC, MVT::v4f32, 1 },
18217 { ISD::SETCC, MVT::v2i64, 1 },
18218 { ISD::SETCC, MVT::v4i32, 1 },
18219 { ISD::SETCC, MVT::v8i16, 1 },
18220 { ISD::SETCC, MVT::v16i8, 1 },
18221 };
18222
18223 static const X86CostTblEntry AVX1CostTbl[] = {
18224 { ISD::SETCC, MVT::v4f64, 1 },
18225 { ISD::SETCC, MVT::v8f32, 1 },
18226 // AVX1 does not support 8-wide integer compare.
18227 { ISD::SETCC, MVT::v4i64, 4 },
18228 { ISD::SETCC, MVT::v8i32, 4 },
18229 { ISD::SETCC, MVT::v16i16, 4 },
18230 { ISD::SETCC, MVT::v32i8, 4 },
18231 };
18232
18233 static const X86CostTblEntry AVX2CostTbl[] = {
18234 { ISD::SETCC, MVT::v4i64, 1 },
18235 { ISD::SETCC, MVT::v8i32, 1 },
18236 { ISD::SETCC, MVT::v16i16, 1 },
18237 { ISD::SETCC, MVT::v32i8, 1 },
18238 };
18239
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018240 if (ST.hasAVX2()) {
18241 int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018242 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018243 return LT.first * AVX2CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018244 }
18245
18246 if (ST.hasAVX()) {
18247 int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
18248 if (Idx != -1)
18249 return LT.first * AVX1CostTbl[Idx].Cost;
18250 }
18251
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018252 if (ST.hasSSE42()) {
18253 int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018254 if (Idx != -1)
Jakub Staszak270bfbd2012-12-18 22:57:56 +000018255 return LT.first * SSE42CostTbl[Idx].Cost;
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018256 }
18257
18258 return VectorTargetTransformImpl::getCmpSelInstrCost(Opcode, ValTy, CondTy);
18259}
18260
Nadav Rotemb0428682012-11-06 19:33:53 +000018261unsigned X86VectorTargetTransformInfo::getCastInstrCost(unsigned Opcode,
18262 Type *Dst,
18263 Type *Src) const {
18264 int ISD = InstructionOpcodeToISD(Opcode);
18265 assert(ISD && "Invalid opcode");
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018266
Nadav Rotemb0428682012-11-06 19:33:53 +000018267 EVT SrcTy = TLI->getValueType(Src);
18268 EVT DstTy = TLI->getValueType(Dst);
18269
18270 if (!SrcTy.isSimple() || !DstTy.isSimple())
18271 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18272
18273 const X86Subtarget &ST = TLI->getTargetMachine().getSubtarget<X86Subtarget>();
18274
18275 static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
18276 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18277 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
18278 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18279 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
18280 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1 },
18281 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1 },
18282 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18283 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
18284 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 1 },
18285 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 1 },
Nadav Rotemb14a5f52012-11-09 07:02:24 +000018286 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
18287 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018288 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 6 },
18289 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 9 },
Nadav Rotema6fb97a2012-11-06 21:17:17 +000018290 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 3 },
Nadav Rotemb0428682012-11-06 19:33:53 +000018291 };
18292
18293 if (ST.hasAVX()) {
18294 int Idx = FindInConvertTable(AVXConversionTbl,
18295 array_lengthof(AVXConversionTbl),
18296 ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
18297 if (Idx != -1)
18298 return AVXConversionTbl[Idx].Cost;
18299 }
18300
18301 return VectorTargetTransformImpl::getCastInstrCost(Opcode, Dst, Src);
18302}
Nadav Rotem7ae3bcc2012-11-05 23:48:20 +000018303
Nadav Rotemae34b422012-12-28 08:19:03 +000018304
18305unsigned X86VectorTargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
18306 int Index) const {
18307 // We only estimate the cost of reverse shuffles.
18308 if (Kind != Reverse)
18309 return VectorTargetTransformImpl::getShuffleCost(Kind, Tp, Index);
18310
18311 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Tp);
18312 unsigned Cost = 1;
18313 if (LT.second.getSizeInBits() > 128)
18314 Cost = 3; // Extract + insert + copy.
18315
18316 // Multiple by the number of parts.
18317 return Cost * LT.first;
18318}
18319