blob: 8a7faf60e63c20e8bdb8d9b3c35734937a79b02b [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
Benjamin Kramer02c2ecf2013-03-07 18:48:40 +000088 // If the input is a buildvector just emit a smaller one.
89 if (Vec.getOpcode() == ISD::BUILD_VECTOR)
90 return DAG.getNode(ISD::BUILD_VECTOR, dl, ResultVT,
91 Vec->op_begin()+NormalizedIdxVal, ElemsPerChunk);
92
Craig Topperb8d9da12012-09-06 06:09:01 +000093 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000094 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
95 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000096
Craig Topperb14940a2012-04-22 20:55:18 +000097 return Result;
David Greenea5f26012011-02-07 19:36:54 +000098}
99
100/// Generate a DAG to put 128-bits into a vector > 128 bits. This
101/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000102/// simple superregister reference. Idx is an index in the 128 bits
103/// we want. It need not be aligned to a 128-bit bounday. That makes
104/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000105static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
106 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000107 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000108 // Inserting UNDEF is Result
109 if (Vec.getOpcode() == ISD::UNDEF)
110 return Result;
111
Craig Topperb14940a2012-04-22 20:55:18 +0000112 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000113 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000114
Craig Topperb14940a2012-04-22 20:55:18 +0000115 EVT ElVT = VT.getVectorElementType();
116 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000117
Craig Topperb14940a2012-04-22 20:55:18 +0000118 // Insert the relevant 128 bits.
119 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb14940a2012-04-22 20:55:18 +0000121 // This is the index of the first element of the 128-bit chunk
122 // we want.
123 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
124 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000125
Craig Topperb8d9da12012-09-06 06:09:01 +0000126 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000127 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
128 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000129}
130
Craig Topper4c7972d2012-04-22 18:15:59 +0000131/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
132/// instructions. This is used because creating CONCAT_VECTOR nodes of
133/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
134/// large BUILD_VECTORS.
135static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
136 unsigned NumElems, SelectionDAG &DAG,
137 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000138 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
139 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000143 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
144 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000145
Evan Cheng2bffee22011-02-01 01:14:13 +0000146 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000147 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000148 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000149 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000150 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000151
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000152 if (Subtarget->isTargetLinux())
153 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000154 if (Subtarget->isTargetELF())
155 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000158 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000159}
160
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000161X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000163 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000164 X86ScalarSSEf64 = Subtarget->hasSSE2();
165 X86ScalarSSEf32 = Subtarget->hasSSE1();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000166 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000167 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000168
Bill Wendling13bbe1f2013-04-05 21:52:40 +0000169 resetOperationActions();
170}
171
172void X86TargetLowering::resetOperationActions() {
173 const TargetMachine &TM = getTargetMachine();
174 static bool FirstTimeThrough = true;
175
176 // If none of the target options have changed, then we don't need to reset the
177 // operation actions.
178 if (!FirstTimeThrough && TO == TM.Options) return;
179
180 if (!FirstTimeThrough) {
181 // Reinitialize the actions.
182 initActions();
183 FirstTimeThrough = false;
184 }
185
186 TO = TM.Options;
187
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000189 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000190
191 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000192 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000193 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
194 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000195
Eric Christopherde5e1012011-03-11 01:05:58 +0000196 // For 64-bit since we have so many registers use the ILP scheduler, for
197 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000198 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000199 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000200 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000201 else if (Subtarget->is64Bit())
202 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000203 else
204 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000205 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000206
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000207 // Bypass expensive divides on Atom when compiling with O2
208 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default) {
Preston Gurd8d662b52012-10-04 21:33:40 +0000209 addBypassSlowDiv(32, 8);
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000210 if (Subtarget->is64Bit())
211 addBypassSlowDiv(64, 16);
212 }
Preston Gurd2e2efd92012-09-04 18:22:17 +0000213
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000214 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000215 // Setup Windows compiler runtime calls.
216 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000217 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000218 setLibcallName(RTLIB::SREM_I64, "_allrem");
219 setLibcallName(RTLIB::UREM_I64, "_aullrem");
220 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000221 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000222 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000223 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
224 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
225 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000226
227 // The _ftol2 runtime function has an unusual calling conv, which
228 // is modeled by a special pseudo-instruction.
229 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
230 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
231 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
232 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000233 }
234
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000235 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000236 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000237 setUseUnderscoreSetJmp(false);
238 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000239 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000240 // MS runtime is weird: it exports _setjmp, but longjmp!
241 setUseUnderscoreSetJmp(true);
242 setUseUnderscoreLongJmp(false);
243 } else {
244 setUseUnderscoreSetJmp(true);
245 setUseUnderscoreLongJmp(true);
246 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000249 addRegisterClass(MVT::i8, &X86::GR8RegClass);
250 addRegisterClass(MVT::i16, &X86::GR16RegClass);
251 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000252 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000253 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000254
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000256
Scott Michelfdc40a02009-02-17 22:15:04 +0000257 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000259 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000261 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
263 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000264
265 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
267 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
268 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
269 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
270 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
271 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000272
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000273 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
274 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
276 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
277 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000278
Evan Cheng25ab6902006-09-08 06:48:29 +0000279 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000281 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000282 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000283 // We have an algorithm for SSE2->double, and we turn this into a
284 // 64-bit FILD followed by conditional FADD for other targets.
285 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000286 // We have an algorithm for SSE2, and we turn this into a 64-bit
287 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000288 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000290
291 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
294 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000295
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000296 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000297 // SSE has no i16 to fp conversion, only i32
298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
304 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000305 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000306 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
308 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000309 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Dale Johannesen73328d12007-09-19 23:55:34 +0000311 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
312 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
314 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000315
Evan Cheng02568ff2006-01-30 22:13:22 +0000316 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
317 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
319 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000320
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000321 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000323 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000325 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
327 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328 }
329
330 // Handle FP_TO_UINT by promoting the destination to a larger signed
331 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
333 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
334 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000335
Evan Cheng25ab6902006-09-08 06:48:29 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
338 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000339 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000340 // Since AVX is a superset of SSE3, only check for SSE here.
341 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 // Expand FP_TO_UINT into a select.
343 // FIXME: We would like to use a Custom expander here eventually to do
344 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000347 // With SSE3 we can use fisttpll to convert to a signed i64; without
348 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000350 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000351
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000352 if (isTargetFTOL()) {
353 // Use the _ftol2 runtime function, which has a pseudo-instruction
354 // to handle its weird calling convention.
355 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
356 }
357
Chris Lattner399610a2006-12-05 18:22:22 +0000358 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000359 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000360 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
361 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000362 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000364 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000365 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000366 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000367 }
Chris Lattner21f66852005-12-23 05:15:23 +0000368
Dan Gohmanb00ee212008-02-18 19:34:53 +0000369 // Scalar integer divide and remainder are lowered to use operations that
370 // produce two results, to match the available instructions. This exposes
371 // the two-result form to trivial CSE, which is able to combine x/y and x%y
372 // into a single instruction.
373 //
374 // Scalar integer multiply-high is also lowered to use two-result
375 // operations, to match the available instructions. However, plain multiply
376 // (low) operations are left as Legal, as there are single-result
377 // instructions for this in x86. Using the two-result multiply instructions
378 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000379 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000380 MVT VT = IntVTs[i];
381 setOperationAction(ISD::MULHS, VT, Expand);
382 setOperationAction(ISD::MULHU, VT, Expand);
383 setOperationAction(ISD::SDIV, VT, Expand);
384 setOperationAction(ISD::UDIV, VT, Expand);
385 setOperationAction(ISD::SREM, VT, Expand);
386 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000387
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000388 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000389 setOperationAction(ISD::ADDC, VT, Custom);
390 setOperationAction(ISD::ADDE, VT, Custom);
391 setOperationAction(ISD::SUBC, VT, Custom);
392 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000393 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000394
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
396 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Tom Stellard3ef53832013-03-08 15:36:57 +0000397 setOperationAction(ISD::BR_CC , MVT::f32, Expand);
398 setOperationAction(ISD::BR_CC , MVT::f64, Expand);
399 setOperationAction(ISD::BR_CC , MVT::f80, Expand);
400 setOperationAction(ISD::BR_CC , MVT::i8, Expand);
401 setOperationAction(ISD::BR_CC , MVT::i16, Expand);
402 setOperationAction(ISD::BR_CC , MVT::i32, Expand);
403 setOperationAction(ISD::BR_CC , MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000405 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
408 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
409 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
410 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
411 setOperationAction(ISD::FREM , MVT::f32 , Expand);
412 setOperationAction(ISD::FREM , MVT::f64 , Expand);
413 setOperationAction(ISD::FREM , MVT::f80 , Expand);
414 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Chandler Carruth77821022011-12-24 12:12:34 +0000416 // Promote the i8 variants and force them on up to i32 which has a shorter
417 // encoding.
418 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
419 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
420 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
421 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000422 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000423 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
424 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
425 if (Subtarget->is64Bit())
426 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000427 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000428 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
429 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
430 if (Subtarget->is64Bit())
431 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
432 }
Craig Topper37f21672011-10-11 06:44:02 +0000433
434 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000435 // When promoting the i8 variants, force them to i32 for a shorter
436 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000437 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000438 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
439 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
440 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000441 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
442 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
443 if (Subtarget->is64Bit())
444 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000445 } else {
446 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
447 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
448 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000449 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
450 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
451 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
452 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000453 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000454 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
455 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 }
457
Benjamin Kramer1292c222010-12-04 20:32:23 +0000458 if (Subtarget->hasPOPCNT()) {
459 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
460 } else {
461 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
462 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
463 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
464 if (Subtarget->is64Bit())
465 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
466 }
467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
469 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000470
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000471 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000472 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000473 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000474 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
477 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
478 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
479 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
480 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000481 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
483 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
484 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
485 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000486 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000488 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000489 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Hal Finkele9150472013-03-27 19:10:42 +0000491 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Michael Liao6c0e04c2012-10-15 22:39:43 +0000492 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000493 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000494 // other SjLj exception interfaces are implemented and please don't build
495 // your own exception handling based on them.
496 // LLVM/Clang supports zero-cost DWARF exception handling.
497 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
498 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000499
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000500 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
502 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
503 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
504 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000505 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
507 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000508 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000509 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
511 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
512 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
513 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000514 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000515 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000516 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
518 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
519 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000520 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
522 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
523 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000524 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000525
Craig Topper1accb7e2012-01-10 06:54:16 +0000526 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000528
Eli Friedman14648462011-07-27 22:21:52 +0000529 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000530
Mon P Wang63307c32008-05-05 19:05:59 +0000531 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000532 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000533 MVT VT = IntVTs[i];
534 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
535 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000536 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000537 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000538
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000539 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000540 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
542 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
543 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
544 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
545 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
546 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
547 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000548 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
549 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
550 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
551 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000552 }
553
Eli Friedman43f51ae2011-08-26 21:21:21 +0000554 if (Subtarget->hasCmpxchg16b()) {
555 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
556 }
557
Evan Cheng3c992d22006-03-07 02:02:57 +0000558 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000559 if (!Subtarget->isTargetDarwin() &&
560 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000561 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000563 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000564
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
566 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
567 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
568 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000569 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000570 setExceptionPointerRegister(X86::RAX);
571 setExceptionSelectorRegister(X86::RDX);
572 } else {
573 setExceptionPointerRegister(X86::EAX);
574 setExceptionSelectorRegister(X86::EDX);
575 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
577 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000578
Duncan Sands4a544a72011-09-06 13:37:06 +0000579 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
580 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000581
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000583 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000584
Nate Begemanacc398c2006-01-25 18:21:52 +0000585 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::VASTART , MVT::Other, Custom);
587 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000588 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::VAARG , MVT::Other, Custom);
590 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000591 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::VAARG , MVT::Other, Expand);
593 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000594 }
Evan Chengae642192007-03-02 23:16:35 +0000595
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
597 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000598
599 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
600 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
601 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000602 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000603 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
604 MVT::i64 : MVT::i32, Custom);
605 else
606 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
607 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000608
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000609 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000611 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000612 addRegisterClass(MVT::f32, &X86::FR32RegClass);
613 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614
Evan Cheng223547a2006-01-31 22:28:30 +0000615 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::FABS , MVT::f64, Custom);
617 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000618
619 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FNEG , MVT::f64, Custom);
621 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000622
Evan Cheng68c47cb2007-01-05 07:55:56 +0000623 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
625 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000626
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000627 // Lower this to FGETSIGNx86 plus an AND.
628 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
629 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
630
Evan Chengd25e9e82006-02-02 00:28:23 +0000631 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000632 setOperationAction(ISD::FSIN , MVT::f64, Expand);
633 setOperationAction(ISD::FCOS , MVT::f64, Expand);
634 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
635 setOperationAction(ISD::FSIN , MVT::f32, Expand);
636 setOperationAction(ISD::FCOS , MVT::f32, Expand);
637 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638
Chris Lattnera54aa942006-01-29 06:26:08 +0000639 // Expand FP immediates into loads from the stack, except for the special
640 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 addLegalFPImmediate(APFloat(+0.0)); // xorpd
642 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000643 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000644 // Use SSE for f32, x87 for f64.
645 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000646 addRegisterClass(MVT::f32, &X86::FR32RegClass);
647 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000648
649 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651
652 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000654
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000656
657 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000660
661 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000662 setOperationAction(ISD::FSIN , MVT::f32, Expand);
663 setOperationAction(ISD::FCOS , MVT::f32, Expand);
664 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000665
Nate Begemane1795842008-02-14 08:57:00 +0000666 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000667 addLegalFPImmediate(APFloat(+0.0f)); // xorps
668 addLegalFPImmediate(APFloat(+0.0)); // FLD0
669 addLegalFPImmediate(APFloat(+1.0)); // FLD1
670 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
671 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
672
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000673 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000674 setOperationAction(ISD::FSIN , MVT::f64, Expand);
675 setOperationAction(ISD::FCOS , MVT::f64, Expand);
676 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000677 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000678 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000679 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000680 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000681 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
682 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000683
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
685 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
686 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
687 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000688
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000689 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000690 setOperationAction(ISD::FSIN , MVT::f64, Expand);
691 setOperationAction(ISD::FSIN , MVT::f32, Expand);
692 setOperationAction(ISD::FCOS , MVT::f64, Expand);
693 setOperationAction(ISD::FCOS , MVT::f32, Expand);
694 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
695 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000696 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000697 addLegalFPImmediate(APFloat(+0.0)); // FLD0
698 addLegalFPImmediate(APFloat(+1.0)); // FLD1
699 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
700 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000701 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
702 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
703 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
704 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000705 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000706
Cameron Zwarich33390842011-07-08 21:39:21 +0000707 // We don't support FMA.
708 setOperationAction(ISD::FMA, MVT::f64, Expand);
709 setOperationAction(ISD::FMA, MVT::f32, Expand);
710
Dale Johannesen59a58732007-08-05 18:49:15 +0000711 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000712 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000713 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
715 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000716 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000717 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000718 addLegalFPImmediate(TmpFlt); // FLD0
719 TmpFlt.changeSign();
720 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000721
722 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000723 APFloat TmpFlt2(+1.0);
724 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
725 &ignored);
726 addLegalFPImmediate(TmpFlt2); // FLD1
727 TmpFlt2.changeSign();
728 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
729 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000730
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000731 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000732 setOperationAction(ISD::FSIN , MVT::f80, Expand);
733 setOperationAction(ISD::FCOS , MVT::f80, Expand);
734 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000735 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000736
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000737 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
738 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
739 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
740 setOperationAction(ISD::FRINT, MVT::f80, Expand);
741 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000742 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000743 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000744
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000745 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
747 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
748 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000749
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::FLOG, MVT::f80, Expand);
751 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
752 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
753 setOperationAction(ISD::FEXP, MVT::f80, Expand);
754 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000755
Mon P Wangf007a8b2008-11-06 05:31:54 +0000756 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000757 // (for widening) or expand (for scalarization). Then we will selectively
758 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000759 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
760 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000761 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000762 setOperationAction(ISD::ADD , VT, Expand);
763 setOperationAction(ISD::SUB , VT, Expand);
764 setOperationAction(ISD::FADD, VT, Expand);
765 setOperationAction(ISD::FNEG, VT, Expand);
766 setOperationAction(ISD::FSUB, VT, Expand);
767 setOperationAction(ISD::MUL , VT, Expand);
768 setOperationAction(ISD::FMUL, VT, Expand);
769 setOperationAction(ISD::SDIV, VT, Expand);
770 setOperationAction(ISD::UDIV, VT, Expand);
771 setOperationAction(ISD::FDIV, VT, Expand);
772 setOperationAction(ISD::SREM, VT, Expand);
773 setOperationAction(ISD::UREM, VT, Expand);
774 setOperationAction(ISD::LOAD, VT, Expand);
775 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
776 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
777 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
778 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
779 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
780 setOperationAction(ISD::FABS, VT, Expand);
781 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000782 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000783 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000784 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000785 setOperationAction(ISD::FREM, VT, Expand);
786 setOperationAction(ISD::FMA, VT, Expand);
787 setOperationAction(ISD::FPOWI, VT, Expand);
788 setOperationAction(ISD::FSQRT, VT, Expand);
789 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
790 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000791 setOperationAction(ISD::FCEIL, VT, Expand);
792 setOperationAction(ISD::FTRUNC, VT, Expand);
793 setOperationAction(ISD::FRINT, VT, Expand);
794 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000795 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
796 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
797 setOperationAction(ISD::SDIVREM, VT, Expand);
798 setOperationAction(ISD::UDIVREM, VT, Expand);
799 setOperationAction(ISD::FPOW, VT, Expand);
800 setOperationAction(ISD::CTPOP, VT, Expand);
801 setOperationAction(ISD::CTTZ, VT, Expand);
802 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
803 setOperationAction(ISD::CTLZ, VT, Expand);
804 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
805 setOperationAction(ISD::SHL, VT, Expand);
806 setOperationAction(ISD::SRA, VT, Expand);
807 setOperationAction(ISD::SRL, VT, Expand);
808 setOperationAction(ISD::ROTL, VT, Expand);
809 setOperationAction(ISD::ROTR, VT, Expand);
810 setOperationAction(ISD::BSWAP, VT, Expand);
811 setOperationAction(ISD::SETCC, VT, Expand);
812 setOperationAction(ISD::FLOG, VT, Expand);
813 setOperationAction(ISD::FLOG2, VT, Expand);
814 setOperationAction(ISD::FLOG10, VT, Expand);
815 setOperationAction(ISD::FEXP, VT, Expand);
816 setOperationAction(ISD::FEXP2, VT, Expand);
817 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
818 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
819 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
820 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
821 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
822 setOperationAction(ISD::TRUNCATE, VT, Expand);
823 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
824 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
825 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
826 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000827 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
828 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000829 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000830 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000831 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
832 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
833 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000834 }
835
Evan Chengc7ce29b2009-02-13 22:36:38 +0000836 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
837 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000838 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000839 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000840 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000841 }
842
Dale Johannesen0488fb62010-09-30 23:57:10 +0000843 // MMX-sized vectors (other than x86mmx) are expected to be expanded
844 // into smaller operations.
845 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
846 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
847 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
848 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
849 setOperationAction(ISD::AND, MVT::v8i8, Expand);
850 setOperationAction(ISD::AND, MVT::v4i16, Expand);
851 setOperationAction(ISD::AND, MVT::v2i32, Expand);
852 setOperationAction(ISD::AND, MVT::v1i64, Expand);
853 setOperationAction(ISD::OR, MVT::v8i8, Expand);
854 setOperationAction(ISD::OR, MVT::v4i16, Expand);
855 setOperationAction(ISD::OR, MVT::v2i32, Expand);
856 setOperationAction(ISD::OR, MVT::v1i64, Expand);
857 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
858 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
859 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
860 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
861 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
862 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
863 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
864 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
865 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
866 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
867 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
868 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
869 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000870 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
871 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
872 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
873 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000874
Craig Topper1accb7e2012-01-10 06:54:16 +0000875 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000876 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
879 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
880 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
881 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
882 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
883 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000884 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
886 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
887 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
889 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000890 }
891
Craig Topper1accb7e2012-01-10 06:54:16 +0000892 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000893 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000894
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000895 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
896 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000897 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
898 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
899 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
900 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
903 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
904 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
905 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000906 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
908 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
909 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
910 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
911 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
912 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
913 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
914 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
915 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
916 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
917 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
918 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000919 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000920
Nadav Rotem354efd82011-09-18 14:57:03 +0000921 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000922 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
923 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
924 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000925
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
927 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
928 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
929 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000931
Evan Cheng2c3ae372006-04-12 21:21:57 +0000932 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000933 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000934 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000935 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000936 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000937 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000938 // Do not attempt to custom lower non-128-bit vectors
939 if (!VT.is128BitVector())
940 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000941 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
942 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000944 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000945
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
947 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
948 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
949 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
950 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
951 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000952
Nate Begemancdd1eec2008-02-12 22:51:28 +0000953 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
955 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000956 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000957
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000958 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000959 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000960 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000961
962 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000963 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000964 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000965
Craig Topper0d1f1762012-08-12 00:34:56 +0000966 setOperationAction(ISD::AND, VT, Promote);
967 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
968 setOperationAction(ISD::OR, VT, Promote);
969 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
970 setOperationAction(ISD::XOR, VT, Promote);
971 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
972 setOperationAction(ISD::LOAD, VT, Promote);
973 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
974 setOperationAction(ISD::SELECT, VT, Promote);
975 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000976 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000979
Evan Cheng2c3ae372006-04-12 21:21:57 +0000980 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
982 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
983 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
984 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
987 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000988
Michael Liaoa7554632012-10-23 17:36:08 +0000989 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
990 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000991 // As there is no 64-bit GPR available, we need build a special custom
992 // sequence to convert from v2i32 to v2f32.
993 if (!Subtarget->is64Bit())
994 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000995
Michael Liao9d796db2012-10-10 16:32:15 +0000996 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000997 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000998
Michael Liaob8150d82012-09-10 18:33:51 +0000999 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +00001000 }
Evan Chengc7ce29b2009-02-13 22:36:38 +00001001
Craig Topperd0a31172012-01-10 06:37:29 +00001002 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +00001003 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
1004 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
1005 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
1006 setOperationAction(ISD::FRINT, MVT::f32, Legal);
1007 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
1008 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1009 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1010 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1011 setOperationAction(ISD::FRINT, MVT::f64, Legal);
1012 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1013
Craig Topper12fb5c62012-09-08 17:42:27 +00001014 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001015 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
1016 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
1017 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
1018 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001019 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001020 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
1021 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
1022 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
1023 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001024
Nate Begeman14d12ca2008-02-11 04:19:36 +00001025 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001027
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001028 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1029 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1030 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1031 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1032 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001033
Nate Begeman14d12ca2008-02-11 04:19:36 +00001034 // i8 and i16 vectors are custom , because the source register and source
1035 // source memory operand types are not the same width. f32 vectors are
1036 // custom since the immediate controlling the insert encodes additional
1037 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1039 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1040 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1041 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001042
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1044 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1045 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1046 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001047
Pete Coopera77214a2011-11-14 19:38:42 +00001048 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001049 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001050 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001051 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1052 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001053 }
1054 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001055
Craig Topper1accb7e2012-01-10 06:54:16 +00001056 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001057 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001058 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001059
Nadav Rotem43012222011-05-11 08:12:09 +00001060 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001061 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001062
Nadav Rotem43012222011-05-11 08:12:09 +00001063 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001064 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001065
Michael Liao5c5f1902013-03-20 02:28:20 +00001066 // In the customized shift lowering, the legal cases in AVX2 will be
1067 // recognized.
1068 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1069 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001070
Michael Liao5c5f1902013-03-20 02:28:20 +00001071 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1072 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001073
Michael Liao5c5f1902013-03-20 02:28:20 +00001074 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001075
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001076 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1077 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001078 }
1079
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001080 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001081 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1082 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1083 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1084 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1085 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1086 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001087
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1090 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001091
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1093 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1094 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1095 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1096 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001097 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001098 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1099 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1100 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1101 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001103 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001104
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1106 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1107 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1108 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1109 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001110 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001111 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1112 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1113 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1114 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001116 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001117
Michael Liaobedcbd42012-10-16 18:14:11 +00001118 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001119 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001120
1121 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1122
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001123 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
Benjamin Kramerb8f0d892013-03-31 12:49:15 +00001124 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Promote);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001125 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001126 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001127
Michael Liaoa7554632012-10-23 17:36:08 +00001128 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1129 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1130 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1131
Michael Liaob8150d82012-09-10 18:33:51 +00001132 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1133
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001134 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1135 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1136
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001137 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1138 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1139
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001140 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001141 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001142
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001143 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1144
Duncan Sands28b77e92011-09-06 19:07:46 +00001145 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1146 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1147 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1148 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001149
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001150 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1151 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1152 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1153
Craig Topperaaa643c2011-11-09 07:28:55 +00001154 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1155 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1156 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1157 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001158
Nadav Rotem0509db22012-12-28 05:45:24 +00001159 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1160 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1161 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1162 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1163 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1164 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001165
Craig Topperbf404372012-08-31 15:40:30 +00001166 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001167 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1168 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1169 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1170 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1171 setOperationAction(ISD::FMA, MVT::f32, Legal);
1172 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001173 }
Craig Topper880ef452012-08-11 22:34:26 +00001174
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001175 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001176 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1177 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1178 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1179 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001180
Craig Topperaaa643c2011-11-09 07:28:55 +00001181 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1182 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1183 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1184 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001185
Craig Topperaaa643c2011-11-09 07:28:55 +00001186 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1187 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1188 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001189 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001190
1191 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001192
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001193 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001194 } else {
1195 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1196 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1197 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1198 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1199
1200 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1201 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1202 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1203 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1204
1205 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1206 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1207 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1208 // Don't lower v32i8 because there is no 128-bit byte mul
1209 }
Craig Topper13894fa2011-08-24 06:14:18 +00001210
Michael Liao5c5f1902013-03-20 02:28:20 +00001211 // In the customized shift lowering, the legal cases in AVX2 will be
1212 // recognized.
1213 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1214 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1215
1216 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1217 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1218
1219 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1220
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001221 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001222 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1223 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001224 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001225
1226 // Extract subvector is special because the value type
1227 // (result) is 128-bit but the source is 256-bit wide.
1228 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001229 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001230
1231 // Do not attempt to custom lower other non-256-bit vectors
1232 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001233 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001234
Craig Topper0d1f1762012-08-12 00:34:56 +00001235 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1236 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1237 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1238 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1239 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1240 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1241 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001242 }
1243
David Greene54d8eba2011-01-27 22:38:56 +00001244 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001245 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001246 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001247
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001248 // Do not attempt to promote non-256-bit vectors
1249 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001250 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001251
Craig Topper0d1f1762012-08-12 00:34:56 +00001252 setOperationAction(ISD::AND, VT, Promote);
1253 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1254 setOperationAction(ISD::OR, VT, Promote);
1255 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1256 setOperationAction(ISD::XOR, VT, Promote);
1257 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1258 setOperationAction(ISD::LOAD, VT, Promote);
1259 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1260 setOperationAction(ISD::SELECT, VT, Promote);
1261 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001262 }
David Greene9b9838d2009-06-29 16:47:10 +00001263 }
1264
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001265 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1266 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001267 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1268 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001269 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1270 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001271 }
1272
Evan Cheng6be2c582006-04-05 23:38:46 +00001273 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001275 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001276
Eli Friedman962f5492010-06-02 19:35:46 +00001277 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1278 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001279 //
Eli Friedman962f5492010-06-02 19:35:46 +00001280 // FIXME: We really should do custom legalization for addition and
1281 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1282 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001283 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1284 // Add/Sub/Mul with overflow operations are custom lowered.
1285 MVT VT = IntVTs[i];
1286 setOperationAction(ISD::SADDO, VT, Custom);
1287 setOperationAction(ISD::UADDO, VT, Custom);
1288 setOperationAction(ISD::SSUBO, VT, Custom);
1289 setOperationAction(ISD::USUBO, VT, Custom);
1290 setOperationAction(ISD::SMULO, VT, Custom);
1291 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001292 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001293
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001294 // There are no 8-bit 3-address imul/mul instructions
1295 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1296 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001297
Evan Chengd54f2d52009-03-31 19:38:51 +00001298 if (!Subtarget->is64Bit()) {
1299 // These libcalls are not available in 32-bit.
1300 setLibcallName(RTLIB::SHL_I128, 0);
1301 setLibcallName(RTLIB::SRL_I128, 0);
1302 setLibcallName(RTLIB::SRA_I128, 0);
1303 }
1304
Evan Cheng8688a582013-01-29 02:32:37 +00001305 // Combine sin / cos into one node or libcall if possible.
1306 if (Subtarget->hasSinCos()) {
1307 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1308 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001309 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001310 // For MacOSX, we don't want to the normal expansion of a libcall to
1311 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1312 // traffic.
1313 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1314 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1315 }
1316 }
1317
Evan Cheng206ee9d2006-07-07 08:33:52 +00001318 // We have target-specific dag combine patterns for the following nodes:
1319 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001320 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001321 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001322 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001323 setTargetDAGCombine(ISD::SHL);
1324 setTargetDAGCombine(ISD::SRA);
1325 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001326 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001327 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001328 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001329 setTargetDAGCombine(ISD::FADD);
1330 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001331 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001332 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001333 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001334 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001335 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001336 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001337 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001338 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001339 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001340 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001341 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001342 if (Subtarget->is64Bit())
1343 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001344 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001345
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001346 computeRegisterProperties();
1347
Evan Cheng05219282011-01-06 06:52:41 +00001348 // On Darwin, -Os means optimize for size without hurting performance,
1349 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001350 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1351 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1352 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1353 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1354 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1355 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001356 setPrefLoopAlignment(4); // 2^4 bytes.
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001357
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001358 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001359 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001360
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001361 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001362}
1363
Duncan Sands28b77e92011-09-06 19:07:46 +00001364EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1365 if (!VT.isVector()) return MVT::i8;
1366 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001367}
1368
Evan Cheng29286502008-01-23 23:17:41 +00001369/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1370/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001371static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001372 if (MaxAlign == 16)
1373 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001374 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001375 if (VTy->getBitWidth() == 128)
1376 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001377 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001378 unsigned EltAlign = 0;
1379 getMaxByValAlign(ATy->getElementType(), EltAlign);
1380 if (EltAlign > MaxAlign)
1381 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001382 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001383 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1384 unsigned EltAlign = 0;
1385 getMaxByValAlign(STy->getElementType(i), EltAlign);
1386 if (EltAlign > MaxAlign)
1387 MaxAlign = EltAlign;
1388 if (MaxAlign == 16)
1389 break;
1390 }
1391 }
Evan Cheng29286502008-01-23 23:17:41 +00001392}
1393
1394/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1395/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001396/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1397/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001398unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001399 if (Subtarget->is64Bit()) {
1400 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001401 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001402 if (TyAlign > 8)
1403 return TyAlign;
1404 return 8;
1405 }
1406
Evan Cheng29286502008-01-23 23:17:41 +00001407 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001408 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001409 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001410 return Align;
1411}
Chris Lattner2b02a442007-02-25 08:29:00 +00001412
Evan Chengf0df0312008-05-15 08:39:06 +00001413/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001414/// and store operations as a result of memset, memcpy, and memmove
1415/// lowering. If DstAlign is zero that means it's safe to destination
1416/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1417/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001418/// probably because the source does not need to be loaded. If 'IsMemset' is
1419/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1420/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1421/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001422/// It returns EVT::Other if the type should be determined using generic
1423/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001424EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001425X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1426 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001427 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001428 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001429 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001430 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001431 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001432 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1433 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001434 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001435 (Subtarget->isUnalignedMemAccessFast() ||
1436 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001437 (SrcAlign == 0 || SrcAlign >= 16)))) {
1438 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001439 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001440 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001441 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001442 return MVT::v8f32;
1443 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001444 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001445 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001446 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001447 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001448 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001449 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001450 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001451 // Do not use f64 to lower memcpy if source is string constant. It's
1452 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001453 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001454 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001455 }
Evan Chengf0df0312008-05-15 08:39:06 +00001456 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001457 return MVT::i64;
1458 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001459}
1460
Evan Cheng7d342672012-12-12 01:32:07 +00001461bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001462 if (VT == MVT::f32)
1463 return X86ScalarSSEf32;
1464 else if (VT == MVT::f64)
1465 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001466 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001467}
1468
Evan Cheng376642e2012-12-10 23:21:26 +00001469bool
1470X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1471 if (Fast)
1472 *Fast = Subtarget->isUnalignedMemAccessFast();
1473 return true;
1474}
1475
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001476/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1477/// current function. The returned value is a member of the
1478/// MachineJumpTableInfo::JTEntryKind enum.
1479unsigned X86TargetLowering::getJumpTableEncoding() const {
1480 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1481 // symbol.
1482 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1483 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001484 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001485
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001486 // Otherwise, use the normal jump table encoding heuristics.
1487 return TargetLowering::getJumpTableEncoding();
1488}
1489
Chris Lattnerc64daab2010-01-26 05:02:42 +00001490const MCExpr *
1491X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1492 const MachineBasicBlock *MBB,
1493 unsigned uid,MCContext &Ctx) const{
1494 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1495 Subtarget->isPICStyleGOT());
1496 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1497 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001498 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1499 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001500}
1501
Evan Chengcc415862007-11-09 01:32:10 +00001502/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1503/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001504SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001505 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001506 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001507 // This doesn't have DebugLoc associated with it, but is not really the
1508 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001509 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001510 return Table;
1511}
1512
Chris Lattner589c6f62010-01-26 06:28:43 +00001513/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1514/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1515/// MCExpr.
1516const MCExpr *X86TargetLowering::
1517getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1518 MCContext &Ctx) const {
1519 // X86-64 uses RIP relative addressing based on the jump table label.
1520 if (Subtarget->isPICStyleRIPRel())
1521 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1522
1523 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001524 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001525}
1526
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001527// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001528std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001529X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001530 const TargetRegisterClass *RRC = 0;
1531 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001532 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001533 default:
1534 return TargetLowering::findRepresentativeClass(VT);
1535 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001536 RRC = Subtarget->is64Bit() ?
1537 (const TargetRegisterClass*)&X86::GR64RegClass :
1538 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001539 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001540 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001541 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001542 break;
1543 case MVT::f32: case MVT::f64:
1544 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1545 case MVT::v4f32: case MVT::v2f64:
1546 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1547 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001548 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001549 break;
1550 }
1551 return std::make_pair(RRC, Cost);
1552}
1553
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001554bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1555 unsigned &Offset) const {
1556 if (!Subtarget->isTargetLinux())
1557 return false;
1558
1559 if (Subtarget->is64Bit()) {
1560 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1561 Offset = 0x28;
1562 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1563 AddressSpace = 256;
1564 else
1565 AddressSpace = 257;
1566 } else {
1567 // %gs:0x14 on i386
1568 Offset = 0x14;
1569 AddressSpace = 256;
1570 }
1571 return true;
1572}
1573
Chris Lattner2b02a442007-02-25 08:29:00 +00001574//===----------------------------------------------------------------------===//
1575// Return Value Calling Convention Implementation
1576//===----------------------------------------------------------------------===//
1577
Chris Lattner59ed56b2007-02-28 04:55:35 +00001578#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001579
Michael J. Spencerec38de22010-10-10 22:04:20 +00001580bool
Eric Christopher471e4222011-06-08 23:55:35 +00001581X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001582 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001583 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001584 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001585 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001586 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001587 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001588 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001589}
1590
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591SDValue
1592X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001593 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001595 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001596 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001597 MachineFunction &MF = DAG.getMachineFunction();
1598 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001599
Chris Lattner9774c912007-02-27 05:28:59 +00001600 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001601 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001602 RVLocs, *DAG.getContext());
1603 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001604
Dan Gohman475871a2008-07-27 21:46:04 +00001605 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001606 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001607 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1608 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001609 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1610 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001611
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001612 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001613 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1614 CCValAssign &VA = RVLocs[i];
1615 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001616 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001617 EVT ValVT = ValToCopy.getValueType();
1618
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001619 // Promote values to the appropriate types
1620 if (VA.getLocInfo() == CCValAssign::SExt)
1621 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1622 else if (VA.getLocInfo() == CCValAssign::ZExt)
1623 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1624 else if (VA.getLocInfo() == CCValAssign::AExt)
1625 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1626 else if (VA.getLocInfo() == CCValAssign::BCvt)
1627 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1628
Dale Johannesenc4510512010-09-24 19:05:48 +00001629 // If this is x86-64, and we disabled SSE, we can't return FP values,
1630 // or SSE or MMX vectors.
1631 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1632 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001633 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001634 report_fatal_error("SSE register return with SSE disabled");
1635 }
1636 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1637 // llvm-gcc has never done it right and no one has noticed, so this
1638 // should be OK for now.
1639 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001640 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001641 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001642
Chris Lattner447ff682008-03-11 03:23:40 +00001643 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1644 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001645 if (VA.getLocReg() == X86::ST0 ||
1646 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001647 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1648 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001649 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001651 RetOps.push_back(ValToCopy);
1652 // Don't emit a copytoreg.
1653 continue;
1654 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001655
Evan Cheng242b38b2009-02-23 09:03:22 +00001656 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1657 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001658 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001659 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001660 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001661 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001662 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1663 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001664 // If we don't have SSE2 available, convert to v4f32 so the generated
1665 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001666 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001667 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001668 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001669 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001670 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001671
Dale Johannesendd64c412009-02-04 00:33:20 +00001672 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001673 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001674 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001675 }
Dan Gohman61a92132008-04-21 23:59:07 +00001676
Eli Benderskya5597f02013-01-25 22:07:43 +00001677 // The x86-64 ABIs require that for returning structs by value we copy
1678 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001679 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00001680 // We saved the argument into a virtual register in the entry block,
1681 // so now we copy the value out and into %rax/%eax.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001682 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr() &&
1683 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00001684 MachineFunction &MF = DAG.getMachineFunction();
1685 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1686 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001687 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001688 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001689 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001690
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001691 unsigned RetValReg
1692 = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ?
1693 X86::RAX : X86::EAX;
Eli Benderskya5597f02013-01-25 22:07:43 +00001694 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001695 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001696
Eli Benderskya5597f02013-01-25 22:07:43 +00001697 // RAX/EAX now acts like a return value.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001698 RetOps.push_back(DAG.getRegister(RetValReg, getPointerTy()));
Dan Gohman61a92132008-04-21 23:59:07 +00001699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Chris Lattner447ff682008-03-11 03:23:40 +00001701 RetOps[0] = Chain; // Update chain.
1702
1703 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001705 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001706
1707 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001709}
1710
Evan Chengbf010eb2012-04-10 01:51:00 +00001711bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001712 if (N->getNumValues() != 1)
1713 return false;
1714 if (!N->hasNUsesOfValue(1, 0))
1715 return false;
1716
Evan Chengbf010eb2012-04-10 01:51:00 +00001717 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001718 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001719 if (Copy->getOpcode() == ISD::CopyToReg) {
1720 // If the copy has a glue operand, we conservatively assume it isn't safe to
1721 // perform a tail call.
1722 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1723 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001724 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001725 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001726 return false;
1727
Evan Cheng1bf891a2010-12-01 22:59:46 +00001728 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001729 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001730 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001731 if (UI->getOpcode() != X86ISD::RET_FLAG)
1732 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001733 HasRet = true;
1734 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001735
Evan Chengbf010eb2012-04-10 01:51:00 +00001736 if (!HasRet)
1737 return false;
1738
1739 Chain = TCChain;
1740 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001741}
1742
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001743MVT
1744X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001745 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001746 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001747 // TODO: Is this also valid on 32-bit?
1748 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001749 ReturnMVT = MVT::i8;
1750 else
1751 ReturnMVT = MVT::i32;
1752
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001753 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001754 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001755}
1756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757/// LowerCallResult - Lower the result values of a call into the
1758/// appropriate copies out of appropriate physical registers.
1759///
1760SDValue
1761X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001762 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001763 const SmallVectorImpl<ISD::InputArg> &Ins,
1764 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001765 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001766
Chris Lattnere32bbf62007-02-28 07:09:55 +00001767 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001768 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001769 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001770 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001771 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001773
Chris Lattner3085e152007-02-25 08:59:22 +00001774 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001775 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001776 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001777 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001778
Torok Edwin3f142c32009-02-01 18:15:56 +00001779 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001781 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001782 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001783 }
1784
Evan Cheng79fb3b42009-02-20 20:43:02 +00001785 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001786
1787 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001788 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001789 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001790 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001791 // instead.
1792 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1793 // If we prefer to use the value in xmm registers, copy it out as f80 and
1794 // use a truncate to move it from fp stack reg to xmm reg.
1795 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001796 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001797 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
Michael Liao2a8bea72013-04-19 22:22:57 +00001798 MVT::Other, MVT::Glue, Ops), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001799 Val = Chain.getValue(0);
1800
1801 // Round the f80 to the right size, which also moves it to the appropriate
1802 // xmm register.
1803 if (CopyVT != VA.getValVT())
1804 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1805 // This truncation won't change the value.
1806 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001807 } else {
1808 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1809 CopyVT, InFlag).getValue(1);
1810 Val = Chain.getValue(0);
1811 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001812 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001814 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001815
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001817}
1818
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001819//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001820// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001821//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001822// StdCall calling convention seems to be standard for many Windows' API
1823// routines and around. It differs from C calling convention just a little:
1824// callee should clean up the stack, not caller. Symbols should be also
1825// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001826// For info on fast calling convention see Fast Calling Convention (tail call)
1827// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001828
Dan Gohman98ca4f22009-08-05 01:29:28 +00001829/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001830/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001831enum StructReturnType {
1832 NotStructReturn,
1833 RegStructReturn,
1834 StackStructReturn
1835};
1836static StructReturnType
1837callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001838 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001839 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001840
Rafael Espindola1cee7102012-07-25 13:41:10 +00001841 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1842 if (!Flags.isSRet())
1843 return NotStructReturn;
1844 if (Flags.isInReg())
1845 return RegStructReturn;
1846 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001847}
1848
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001849/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001850/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001851static StructReturnType
1852argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001854 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001855
Rafael Espindola1cee7102012-07-25 13:41:10 +00001856 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1857 if (!Flags.isSRet())
1858 return NotStructReturn;
1859 if (Flags.isInReg())
1860 return RegStructReturn;
1861 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001862}
1863
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001864/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1865/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001866/// the specific parameter attribute. The copy will be passed as a byval
1867/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001868static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001869CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001870 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1871 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001872 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001873
Dale Johannesendd64c412009-02-04 00:33:20 +00001874 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001875 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001876 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001877}
1878
Chris Lattner29689432010-03-11 00:22:57 +00001879/// IsTailCallConvention - Return true if the calling convention is one that
1880/// supports tail call optimization.
1881static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001882 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1883 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001884}
1885
Evan Cheng485fafc2011-03-21 01:19:09 +00001886bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001887 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001888 return false;
1889
1890 CallSite CS(CI);
1891 CallingConv::ID CalleeCC = CS.getCallingConv();
1892 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1893 return false;
1894
1895 return true;
1896}
1897
Evan Cheng0c439eb2010-01-27 00:07:07 +00001898/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1899/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001900static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1901 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001902 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001903}
1904
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905SDValue
1906X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001907 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908 const SmallVectorImpl<ISD::InputArg> &Ins,
1909 DebugLoc dl, SelectionDAG &DAG,
1910 const CCValAssign &VA,
1911 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001912 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001913 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001915 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1916 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001917 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001918 EVT ValVT;
1919
1920 // If value is passed by pointer we have address passed instead of the value
1921 // itself.
1922 if (VA.getLocInfo() == CCValAssign::Indirect)
1923 ValVT = VA.getLocVT();
1924 else
1925 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001926
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001927 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001928 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001929 // In case of tail call optimization mark all arguments mutable. Since they
1930 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001931 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001932 unsigned Bytes = Flags.getByValSize();
1933 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1934 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001935 return DAG.getFrameIndex(FI, getPointerTy());
1936 } else {
1937 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001938 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001939 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1940 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001941 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001942 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001943 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001944}
1945
Dan Gohman475871a2008-07-27 21:46:04 +00001946SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001948 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001949 bool isVarArg,
1950 const SmallVectorImpl<ISD::InputArg> &Ins,
1951 DebugLoc dl,
1952 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001953 SmallVectorImpl<SDValue> &InVals)
1954 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001955 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001956 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Gordon Henriksen86737662008-01-05 16:56:59 +00001958 const Function* Fn = MF.getFunction();
1959 if (Fn->hasExternalLinkage() &&
1960 Subtarget->isTargetCygMing() &&
1961 Fn->getName() == "main")
1962 FuncInfo->setForceFramePointer(true);
1963
Evan Cheng1bc78042006-04-26 01:20:17 +00001964 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001966 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001967 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001968
Chris Lattner29689432010-03-11 00:22:57 +00001969 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001970 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971
Chris Lattner638402b2007-02-28 07:00:42 +00001972 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001973 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001974 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001976
1977 // Allocate shadow area for Win64
1978 if (IsWin64) {
1979 CCInfo.AllocateStack(32, 8);
1980 }
1981
Duncan Sands45907662010-10-31 13:21:44 +00001982 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Chris Lattnerf39f7712007-02-28 05:46:49 +00001984 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001985 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1987 CCValAssign &VA = ArgLocs[i];
1988 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1989 // places.
1990 assert(VA.getValNo() != LastVal &&
1991 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001992 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001993 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Chris Lattnerf39f7712007-02-28 05:46:49 +00001995 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001996 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001997 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001999 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00002001 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00002003 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00002005 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002006 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002007 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002008 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002009 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00002010 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002011 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002012 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002013 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002014
Devang Patel68e6bee2011-02-21 23:21:26 +00002015 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Chris Lattnerf39f7712007-02-28 05:46:49 +00002018 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2019 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2020 // right size.
2021 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002022 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002023 DAG.getValueType(VA.getValVT()));
2024 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002025 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002026 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002027 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002028 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002030 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002031 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002032 if (RegVT.isVector())
2033 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2034 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002035 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002036 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002037 } else {
2038 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002040 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002041
2042 // If value is passed via pointer - do a load.
2043 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002044 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002045 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002046
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002048 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002049
Eli Benderskya5597f02013-01-25 22:07:43 +00002050 // The x86-64 ABIs require that for returning structs by value we copy
2051 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002052 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00002053 // Save the argument into a virtual register so that we can access it
2054 // from the return points.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002055 if (MF.getFunction()->hasStructRetAttr() &&
2056 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00002057 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2058 unsigned Reg = FuncInfo->getSRetReturnReg();
2059 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002060 MVT PtrTy = getPointerTy();
2061 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002062 FuncInfo->setSRetReturnReg(Reg);
2063 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002064 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002066 }
2067
Chris Lattnerf39f7712007-02-28 05:46:49 +00002068 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002069 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002070 if (FuncIsMadeTailCallSafe(CallConv,
2071 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002072 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002073
Evan Cheng1bc78042006-04-26 01:20:17 +00002074 // If the function takes variable number of arguments, make a frame index for
2075 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002076 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002077 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2078 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002079 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002080 }
2081 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002082 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2083
2084 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002085 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002086 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002088 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002089 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2090 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002091 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2093 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2094 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002095 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002097
2098 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002099 // The XMM registers which might contain var arg parameters are shadowed
2100 // in their paired GPR. So we only need to save the GPR to their home
2101 // slots.
2102 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002103 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002104 } else {
2105 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2106 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002107
Chad Rosier30450e82011-12-22 22:35:21 +00002108 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2109 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002110 }
2111 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2112 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002113
Bill Wendling831737d2012-12-30 10:32:01 +00002114 bool NoImplicitFloatOps = Fn->getAttributes().
2115 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002116 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002117 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002118 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2119 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002120 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002121 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002122 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002123 // Kernel mode asks for SSE to be disabled, so don't push them
2124 // on the stack.
2125 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002126
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002127 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002128 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002129 // Get to the caller-allocated home save location. Add 8 to account
2130 // for the return address.
2131 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002132 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002133 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002134 // Fixup to set vararg frame on shadow area (4 x i64).
2135 if (NumIntRegs < 4)
2136 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002137 } else {
2138 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002139 // registers, then we must store them to their spots on the stack so
2140 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002141 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2142 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2143 FuncInfo->setRegSaveFrameIndex(
2144 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002145 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002146 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002147
Gordon Henriksen86737662008-01-05 16:56:59 +00002148 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002150 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2151 getPointerTy());
2152 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002153 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002154 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2155 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002156 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002157 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002158 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002159 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002160 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002161 MachinePointerInfo::getFixedStack(
2162 FuncInfo->getRegSaveFrameIndex(), Offset),
2163 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002165 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002166 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002167
Dan Gohmanface41a2009-08-16 21:24:25 +00002168 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2169 // Now store the XMM (fp + vector) parameter registers.
2170 SmallVector<SDValue, 11> SaveXMMOps;
2171 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002172
Craig Topperc9099502012-04-20 06:31:50 +00002173 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002174 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2175 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002176
Dan Gohman1e93df62010-04-17 14:41:14 +00002177 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2178 FuncInfo->getRegSaveFrameIndex()));
2179 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2180 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002181
Dan Gohmanface41a2009-08-16 21:24:25 +00002182 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002183 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002184 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002185 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2186 SaveXMMOps.push_back(Val);
2187 }
2188 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2189 MVT::Other,
2190 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002191 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002192
2193 if (!MemOps.empty())
2194 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2195 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002196 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002197 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002198
Gordon Henriksen86737662008-01-05 16:56:59 +00002199 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002200 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2201 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002202 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002203 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002204 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002205 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002206 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002207 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002208 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002209 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002210
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002212 // RegSaveFrameIndex is X86-64 only.
2213 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002214 if (CallConv == CallingConv::X86_FastCall ||
2215 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002216 // fastcc functions can't have varargs.
2217 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 }
Evan Cheng25caf632006-05-23 21:06:34 +00002219
Rafael Espindola76927d752011-08-30 19:39:58 +00002220 FuncInfo->setArgumentStackSize(StackSize);
2221
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002223}
2224
Dan Gohman475871a2008-07-27 21:46:04 +00002225SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002226X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2227 SDValue StackPtr, SDValue Arg,
2228 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002229 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002230 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002231 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002232 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002233 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002234 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002235 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002236
2237 return DAG.getStore(Chain, dl, Arg, PtrOff,
2238 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002239 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002240}
2241
Bill Wendling64e87322009-01-16 19:25:27 +00002242/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002243/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002244SDValue
2245X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002246 SDValue &OutRetAddr, SDValue Chain,
2247 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002248 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002249 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002250 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002251 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002252
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002253 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002254 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002255 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002256 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002257}
2258
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002259/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002260/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002261static SDValue
2262EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002263 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2264 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002265 // Store the return address to the appropriate stack slot.
2266 if (!FPDiff) return Chain;
2267 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002268 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002269 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002270 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002271 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002272 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002273 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002274 return Chain;
2275}
2276
Dan Gohman98ca4f22009-08-05 01:29:28 +00002277SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002278X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002279 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002280 SelectionDAG &DAG = CLI.DAG;
2281 DebugLoc &dl = CLI.DL;
2282 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2283 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2284 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2285 SDValue Chain = CLI.Chain;
2286 SDValue Callee = CLI.Callee;
2287 CallingConv::ID CallConv = CLI.CallConv;
2288 bool &isTailCall = CLI.IsTailCall;
2289 bool isVarArg = CLI.IsVarArg;
2290
Dan Gohman98ca4f22009-08-05 01:29:28 +00002291 MachineFunction &MF = DAG.getMachineFunction();
2292 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002293 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002294 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002295 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002296 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002297
Nick Lewycky22de16d2012-01-19 00:34:10 +00002298 if (MF.getTarget().Options.DisableTailCalls)
2299 isTailCall = false;
2300
Evan Cheng5f941932010-02-05 02:21:12 +00002301 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002302 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002303 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002304 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002305 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002306 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002307
2308 // Sibcalls are automatically detected tailcalls which do not require
2309 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002310 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002311 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002312
2313 if (isTailCall)
2314 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002315 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002316
Chris Lattner29689432010-03-11 00:22:57 +00002317 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002318 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002319
Chris Lattner638402b2007-02-28 07:00:42 +00002320 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002321 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002322 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002324
2325 // Allocate shadow area for Win64
2326 if (IsWin64) {
2327 CCInfo.AllocateStack(32, 8);
2328 }
2329
Duncan Sands45907662010-10-31 13:21:44 +00002330 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002331
Chris Lattner423c5f42007-02-28 05:31:48 +00002332 // Get a count of how many bytes are to be pushed on the stack.
2333 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002334 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002335 // This is a sibcall. The memory operands are available in caller's
2336 // own caller's stack.
2337 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002338 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2339 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002340 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002341
Gordon Henriksen86737662008-01-05 16:56:59 +00002342 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002343 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002345 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2346 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2347
Gordon Henriksen86737662008-01-05 16:56:59 +00002348 FPDiff = NumBytesCallerPushed - NumBytes;
2349
2350 // Set the delta of movement of the returnaddr stackslot.
2351 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002352 if (FPDiff < X86Info->getTCReturnAddrDelta())
2353 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002354 }
2355
Evan Chengf22f9b32010-02-06 03:28:46 +00002356 if (!IsSibcall)
2357 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002358
Dan Gohman475871a2008-07-27 21:46:04 +00002359 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002360 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002361 if (isTailCall && FPDiff)
2362 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2363 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002364
Dan Gohman475871a2008-07-27 21:46:04 +00002365 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2366 SmallVector<SDValue, 8> MemOpChains;
2367 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002368
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002369 // Walk the register/memloc assignments, inserting copies/loads. In the case
2370 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002371 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2372 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002373 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002374 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002375 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002376 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002377
Chris Lattner423c5f42007-02-28 05:31:48 +00002378 // Promote the value if needed.
2379 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002380 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002381 case CCValAssign::Full: break;
2382 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002383 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002384 break;
2385 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002386 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002387 break;
2388 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002389 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002390 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002391 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002392 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2393 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002394 } else
2395 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2396 break;
2397 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002398 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002399 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002400 case CCValAssign::Indirect: {
2401 // Store the argument.
2402 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002403 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002404 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002405 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002406 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002407 Arg = SpillSlot;
2408 break;
2409 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002411
Chris Lattner423c5f42007-02-28 05:31:48 +00002412 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002413 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2414 if (isVarArg && IsWin64) {
2415 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2416 // shadow reg if callee is a varargs function.
2417 unsigned ShadowReg = 0;
2418 switch (VA.getLocReg()) {
2419 case X86::XMM0: ShadowReg = X86::RCX; break;
2420 case X86::XMM1: ShadowReg = X86::RDX; break;
2421 case X86::XMM2: ShadowReg = X86::R8; break;
2422 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002423 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002424 if (ShadowReg)
2425 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002426 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002427 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002428 assert(VA.isMemLoc());
2429 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002430 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2431 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002432 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2433 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002434 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002436
Evan Cheng32fe1032006-05-25 00:59:30 +00002437 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002438 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002439 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002440
Chris Lattner88e1fd52009-07-09 04:24:46 +00002441 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002442 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2443 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002444 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002445 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2446 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002447 } else {
2448 // If we are tail calling and generating PIC/GOT style code load the
2449 // address of the callee into ECX. The value in ecx is used as target of
2450 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2451 // for tail calls on PIC/GOT architectures. Normally we would just put the
2452 // address of GOT into ebx and then call target@PLT. But for tail calls
2453 // ebx would be restored (since ebx is callee saved) before jumping to the
2454 // target@PLT.
2455
2456 // Note: The actual moving to ECX is done further down.
2457 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2458 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2459 !G->getGlobal()->hasProtectedVisibility())
2460 Callee = LowerGlobalAddress(Callee, DAG);
2461 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002462 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002463 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002464 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002465
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002466 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 // From AMD64 ABI document:
2468 // For calls that may call functions that use varargs or stdargs
2469 // (prototype-less calls or calls to functions containing ellipsis (...) in
2470 // the declaration) %al is used as hidden argument to specify the number
2471 // of SSE registers used. The contents of %al do not need to match exactly
2472 // the number of registers, but must be an ubound on the number of SSE
2473 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002474
Gordon Henriksen86737662008-01-05 16:56:59 +00002475 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002476 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002477 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2478 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2479 };
2480 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002481 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002482 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002483
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002484 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2485 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002486 }
2487
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002488 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002489 if (isTailCall) {
2490 // Force all the incoming stack arguments to be loaded from the stack
2491 // before any new outgoing arguments are stored to the stack, because the
2492 // outgoing stack slots may alias the incoming argument stack slots, and
2493 // the alias isn't otherwise explicit. This is slightly more conservative
2494 // than necessary, because it means that each store effectively depends
2495 // on every argument instead of just those arguments it would clobber.
2496 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2497
Dan Gohman475871a2008-07-27 21:46:04 +00002498 SmallVector<SDValue, 8> MemOpChains2;
2499 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002500 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002501 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002502 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2503 CCValAssign &VA = ArgLocs[i];
2504 if (VA.isRegLoc())
2505 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002506 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002507 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002508 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002509 // Create frame index.
2510 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002511 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002512 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002513 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002514
Duncan Sands276dcbd2008-03-21 09:14:45 +00002515 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002516 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002517 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002518 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002519 StackPtr = DAG.getCopyFromReg(Chain, dl,
2520 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002521 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002522 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002523
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2525 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002526 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002527 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002528 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002529 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002530 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002531 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002532 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002533 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 }
2535 }
2536
2537 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002539 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002540
2541 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002542 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2543 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002544 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002545 }
2546
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002547 // Build a sequence of copy-to-reg nodes chained together with token chain
2548 // and flag operands which copy the outgoing args into registers.
2549 SDValue InFlag;
2550 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2551 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2552 RegsToPass[i].second, InFlag);
2553 InFlag = Chain.getValue(1);
2554 }
2555
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002556 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2557 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2558 // In the 64-bit large code model, we have to make all calls
2559 // through a register, since the call instruction's 32-bit
2560 // pc-relative offset may not be large enough to hold the whole
2561 // address.
2562 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002563 // If the callee is a GlobalAddress node (quite common, every direct call
2564 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2565 // it.
2566
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002567 // We should use extra load for direct calls to dllimported functions in
2568 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002569 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002570 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002571 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002572 bool ExtraLoad = false;
2573 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002574
Chris Lattner48a7d022009-07-09 05:02:21 +00002575 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2576 // external symbols most go through the PLT in PIC mode. If the symbol
2577 // has hidden or protected visibility, or if it is static or local, then
2578 // we don't need to use the PLT - we can directly call it.
2579 if (Subtarget->isTargetELF() &&
2580 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002581 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002582 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002583 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002584 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002585 (!Subtarget->getTargetTriple().isMacOSX() ||
2586 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002587 // PC-relative references to external symbols should go through $stub,
2588 // unless we're building with the leopard linker or later, which
2589 // automatically synthesizes these stubs.
2590 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002591 } else if (Subtarget->isPICStyleRIPRel() &&
2592 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002593 cast<Function>(GV)->getAttributes().
2594 hasAttribute(AttributeSet::FunctionIndex,
2595 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002596 // If the function is marked as non-lazy, generate an indirect call
2597 // which loads from the GOT directly. This avoids runtime overhead
2598 // at the cost of eager binding (and one extra byte of encoding).
2599 OpFlags = X86II::MO_GOTPCREL;
2600 WrapperKind = X86ISD::WrapperRIP;
2601 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002602 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002603
Devang Patel0d881da2010-07-06 22:08:15 +00002604 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002605 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002606
2607 // Add a wrapper if needed.
2608 if (WrapperKind != ISD::DELETED_NODE)
2609 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2610 // Add extra indirection if needed.
2611 if (ExtraLoad)
2612 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2613 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002614 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002615 }
Bill Wendling056292f2008-09-16 21:48:12 +00002616 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002617 unsigned char OpFlags = 0;
2618
Evan Cheng1bf891a2010-12-01 22:59:46 +00002619 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2620 // external symbols should go through the PLT.
2621 if (Subtarget->isTargetELF() &&
2622 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2623 OpFlags = X86II::MO_PLT;
2624 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002625 (!Subtarget->getTargetTriple().isMacOSX() ||
2626 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002627 // PC-relative references to external symbols should go through $stub,
2628 // unless we're building with the leopard linker or later, which
2629 // automatically synthesizes these stubs.
2630 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002631 }
Eric Christopherfd179292009-08-27 18:07:15 +00002632
Chris Lattner48a7d022009-07-09 05:02:21 +00002633 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2634 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002635 }
2636
Chris Lattnerd96d0722007-02-25 06:40:16 +00002637 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002638 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002639 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002640
Evan Chengf22f9b32010-02-06 03:28:46 +00002641 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002642 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2643 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002644 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002645 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002646
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002647 Ops.push_back(Chain);
2648 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002649
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002652
Gordon Henriksen86737662008-01-05 16:56:59 +00002653 // Add argument registers to the end of the list so that they are known live
2654 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002655 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2656 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2657 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002658
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002659 // Add a register mask operand representing the call-preserved registers.
2660 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2661 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2662 assert(Mask && "Missing call preserved mask for calling convention");
2663 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002664
Gabor Greifba36cb52008-08-28 21:40:38 +00002665 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002666 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002667
Dan Gohman98ca4f22009-08-05 01:29:28 +00002668 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002669 // We used to do:
2670 //// If this is the first return lowered for this function, add the regs
2671 //// to the liveout set for the function.
2672 // This isn't right, although it's probably harmless on x86; liveouts
2673 // should be computed from returns not tail calls. Consider a void
2674 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002675 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002676 }
2677
Dale Johannesenace16102009-02-03 19:33:06 +00002678 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002679 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002680
Chris Lattner2d297092006-05-23 18:50:38 +00002681 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002682 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002683 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2684 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002685 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002686 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002687 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002688 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002689 // pops the hidden struct pointer, so we have to push it back.
2690 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002691 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002692 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002693 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002694 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002695
Gordon Henriksenae636f82008-01-03 16:47:34 +00002696 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002697 if (!IsSibcall) {
2698 Chain = DAG.getCALLSEQ_END(Chain,
2699 DAG.getIntPtrConstant(NumBytes, true),
2700 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2701 true),
2702 InFlag);
2703 InFlag = Chain.getValue(1);
2704 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002705
Chris Lattner3085e152007-02-25 08:59:22 +00002706 // Handle result values, copying them out of physregs into vregs that we
2707 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002708 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2709 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002710}
2711
Evan Cheng25ab6902006-09-08 06:48:29 +00002712//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002713// Fast Calling Convention (tail call) implementation
2714//===----------------------------------------------------------------------===//
2715
2716// Like std call, callee cleans arguments, convention except that ECX is
2717// reserved for storing the tail called function address. Only 2 registers are
2718// free for argument passing (inreg). Tail call optimization is performed
2719// provided:
2720// * tailcallopt is enabled
2721// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002722// On X86_64 architecture with GOT-style position independent code only local
2723// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002724// To keep the stack aligned according to platform abi the function
2725// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2726// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002727// If a tail called function callee has more arguments than the caller the
2728// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002729// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002730// original REtADDR, but before the saved framepointer or the spilled registers
2731// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2732// stack layout:
2733// arg1
2734// arg2
2735// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002736// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002737// move area ]
2738// (possible EBP)
2739// ESI
2740// EDI
2741// local1 ..
2742
2743/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2744/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002745unsigned
2746X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2747 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002748 MachineFunction &MF = DAG.getMachineFunction();
2749 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002750 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002751 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002752 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002753 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002754 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002755 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2756 // Number smaller than 12 so just add the difference.
2757 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2758 } else {
2759 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002760 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002761 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002762 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002763 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002764}
2765
Evan Cheng5f941932010-02-05 02:21:12 +00002766/// MatchingStackOffset - Return true if the given stack call argument is
2767/// already available in the same position (relatively) of the caller's
2768/// incoming argument stack.
2769static
2770bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2771 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2772 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002773 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2774 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002775 if (Arg.getOpcode() == ISD::CopyFromReg) {
2776 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002777 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002778 return false;
2779 MachineInstr *Def = MRI->getVRegDef(VR);
2780 if (!Def)
2781 return false;
2782 if (!Flags.isByVal()) {
2783 if (!TII->isLoadFromStackSlot(Def, FI))
2784 return false;
2785 } else {
2786 unsigned Opcode = Def->getOpcode();
2787 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2788 Def->getOperand(1).isFI()) {
2789 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002790 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002791 } else
2792 return false;
2793 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002794 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2795 if (Flags.isByVal())
2796 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002797 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002798 // define @foo(%struct.X* %A) {
2799 // tail call @bar(%struct.X* byval %A)
2800 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002801 return false;
2802 SDValue Ptr = Ld->getBasePtr();
2803 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2804 if (!FINode)
2805 return false;
2806 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002807 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002808 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002809 FI = FINode->getIndex();
2810 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002811 } else
2812 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002813
Evan Cheng4cae1332010-03-05 08:38:04 +00002814 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002815 if (!MFI->isFixedObjectIndex(FI))
2816 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002817 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002818}
2819
Dan Gohman98ca4f22009-08-05 01:29:28 +00002820/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2821/// for tail call optimization. Targets which want to do tail call
2822/// optimization should implement this function.
2823bool
2824X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002825 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002826 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002827 bool isCalleeStructRet,
2828 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002829 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002830 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002831 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002832 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002833 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002834 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002835 CalleeCC != CallingConv::C)
2836 return false;
2837
Evan Cheng7096ae42010-01-29 06:45:59 +00002838 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002839 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002840 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002841
2842 // If the function return type is x86_fp80 and the callee return type is not,
2843 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2844 // perform a tailcall optimization here.
2845 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2846 return false;
2847
Evan Cheng13617962010-04-30 01:12:32 +00002848 CallingConv::ID CallerCC = CallerF->getCallingConv();
2849 bool CCMatch = CallerCC == CalleeCC;
2850
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002851 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002852 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002853 return true;
2854 return false;
2855 }
2856
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002857 // Look for obvious safe cases to perform tail call optimization that do not
2858 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002859
Evan Cheng2c12cb42010-03-26 16:26:03 +00002860 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2861 // emit a special epilogue.
2862 if (RegInfo->needsStackRealignment(MF))
2863 return false;
2864
Evan Chenga375d472010-03-15 18:54:48 +00002865 // Also avoid sibcall optimization if either caller or callee uses struct
2866 // return semantics.
2867 if (isCalleeStructRet || isCallerStructRet)
2868 return false;
2869
Chad Rosier2416da32011-06-24 21:15:36 +00002870 // An stdcall caller is expected to clean up its arguments; the callee
2871 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002872 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002873 return false;
2874
Chad Rosier871f6642011-05-18 19:59:50 +00002875 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002876 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002877 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002878
2879 // Optimizing for varargs on Win64 is unlikely to be safe without
2880 // additional testing.
2881 if (Subtarget->isTargetWin64())
2882 return false;
2883
Chad Rosier871f6642011-05-18 19:59:50 +00002884 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002885 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002886 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002887
Chad Rosier871f6642011-05-18 19:59:50 +00002888 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2889 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2890 if (!ArgLocs[i].isRegLoc())
2891 return false;
2892 }
2893
Chad Rosier30450e82011-12-22 22:35:21 +00002894 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2895 // stack. Therefore, if it's not used by the call it is not safe to optimize
2896 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002897 bool Unused = false;
2898 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2899 if (!Ins[i].Used) {
2900 Unused = true;
2901 break;
2902 }
2903 }
2904 if (Unused) {
2905 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002906 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002907 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002908 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002909 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002910 CCValAssign &VA = RVLocs[i];
2911 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2912 return false;
2913 }
2914 }
2915
Evan Cheng13617962010-04-30 01:12:32 +00002916 // If the calling conventions do not match, then we'd better make sure the
2917 // results are returned in the same way as what the caller expects.
2918 if (!CCMatch) {
2919 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002920 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002921 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002922 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2923
2924 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002925 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002926 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002927 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2928
2929 if (RVLocs1.size() != RVLocs2.size())
2930 return false;
2931 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2932 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2933 return false;
2934 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2935 return false;
2936 if (RVLocs1[i].isRegLoc()) {
2937 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2938 return false;
2939 } else {
2940 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2941 return false;
2942 }
2943 }
2944 }
2945
Evan Chenga6bff982010-01-30 01:22:00 +00002946 // If the callee takes no arguments then go on to check the results of the
2947 // call.
2948 if (!Outs.empty()) {
2949 // Check if stack adjustment is needed. For now, do not do this if any
2950 // argument is passed on the stack.
2951 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002952 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002953 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002954
2955 // Allocate shadow area for Win64
2956 if (Subtarget->isTargetWin64()) {
2957 CCInfo.AllocateStack(32, 8);
2958 }
2959
Duncan Sands45907662010-10-31 13:21:44 +00002960 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002961 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002962 MachineFunction &MF = DAG.getMachineFunction();
2963 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2964 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002965
2966 // Check if the arguments are already laid out in the right way as
2967 // the caller's fixed stack objects.
2968 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002969 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2970 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002971 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002972 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2973 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002974 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002975 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002976 if (VA.getLocInfo() == CCValAssign::Indirect)
2977 return false;
2978 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002979 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2980 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002981 return false;
2982 }
2983 }
2984 }
Evan Cheng9c044672010-05-29 01:35:22 +00002985
2986 // If the tailcall address may be in a register, then make sure it's
2987 // possible to register allocate for it. In 32-bit, the call address can
2988 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002989 // callee-saved registers are restored. These happen to be the same
2990 // registers used to pass 'inreg' arguments so watch out for those.
2991 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002992 ((!isa<GlobalAddressSDNode>(Callee) &&
2993 !isa<ExternalSymbolSDNode>(Callee)) ||
2994 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002995 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002996 // In PIC we need an extra register to formulate the address computation
2997 // for the callee.
2998 unsigned MaxInRegs =
2999 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
3000
Evan Cheng9c044672010-05-29 01:35:22 +00003001 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3002 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00003003 if (!VA.isRegLoc())
3004 continue;
3005 unsigned Reg = VA.getLocReg();
3006 switch (Reg) {
3007 default: break;
3008 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003009 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00003010 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00003011 break;
Evan Cheng9c044672010-05-29 01:35:22 +00003012 }
3013 }
3014 }
Evan Chenga6bff982010-01-30 01:22:00 +00003015 }
Evan Chengb1712452010-01-27 06:25:16 +00003016
Evan Cheng86809cc2010-02-03 03:28:02 +00003017 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003018}
3019
Dan Gohman3df24e62008-09-03 23:12:08 +00003020FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003021X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3022 const TargetLibraryInfo *libInfo) const {
3023 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003024}
3025
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003026//===----------------------------------------------------------------------===//
3027// Other Lowering Hooks
3028//===----------------------------------------------------------------------===//
3029
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003030static bool MayFoldLoad(SDValue Op) {
3031 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3032}
3033
3034static bool MayFoldIntoStore(SDValue Op) {
3035 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3036}
3037
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003038static bool isTargetShuffle(unsigned Opcode) {
3039 switch(Opcode) {
3040 default: return false;
3041 case X86ISD::PSHUFD:
3042 case X86ISD::PSHUFHW:
3043 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003044 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003045 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003046 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003047 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003048 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003049 case X86ISD::MOVLPS:
3050 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003051 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003052 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003053 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003054 case X86ISD::MOVSS:
3055 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003056 case X86ISD::UNPCKL:
3057 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003058 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003059 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003060 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003061 return true;
3062 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003063}
3064
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003065static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003066 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003067 switch(Opc) {
3068 default: llvm_unreachable("Unknown x86 shuffle node");
3069 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003070 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003071 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003072 return DAG.getNode(Opc, dl, VT, V1);
3073 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003074}
3075
3076static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003077 SDValue V1, unsigned TargetMask,
3078 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003079 switch(Opc) {
3080 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003081 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003082 case X86ISD::PSHUFHW:
3083 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003084 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003085 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003086 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3087 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003088}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003089
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003090static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003091 SDValue V1, SDValue V2, unsigned TargetMask,
3092 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003093 switch(Opc) {
3094 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003095 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003096 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003097 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003098 return DAG.getNode(Opc, dl, VT, V1, V2,
3099 DAG.getConstant(TargetMask, MVT::i8));
3100 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003101}
3102
3103static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3104 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3105 switch(Opc) {
3106 default: llvm_unreachable("Unknown x86 shuffle node");
3107 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003108 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003109 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003110 case X86ISD::MOVLPS:
3111 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003112 case X86ISD::MOVSS:
3113 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003114 case X86ISD::UNPCKL:
3115 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003116 return DAG.getNode(Opc, dl, VT, V1, V2);
3117 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003118}
3119
Dan Gohmand858e902010-04-17 15:26:15 +00003120SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003121 MachineFunction &MF = DAG.getMachineFunction();
3122 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3123 int ReturnAddrIndex = FuncInfo->getRAIndex();
3124
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003125 if (ReturnAddrIndex == 0) {
3126 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003127 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003128 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003129 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003130 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003131 }
3132
Evan Cheng25ab6902006-09-08 06:48:29 +00003133 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003134}
3135
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003136bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3137 bool hasSymbolicDisplacement) {
3138 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003139 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003140 return false;
3141
3142 // If we don't have a symbolic displacement - we don't have any extra
3143 // restrictions.
3144 if (!hasSymbolicDisplacement)
3145 return true;
3146
3147 // FIXME: Some tweaks might be needed for medium code model.
3148 if (M != CodeModel::Small && M != CodeModel::Kernel)
3149 return false;
3150
3151 // For small code model we assume that latest object is 16MB before end of 31
3152 // bits boundary. We may also accept pretty large negative constants knowing
3153 // that all objects are in the positive half of address space.
3154 if (M == CodeModel::Small && Offset < 16*1024*1024)
3155 return true;
3156
3157 // For kernel code model we know that all object resist in the negative half
3158 // of 32bits address space. We may not accept negative offsets, since they may
3159 // be just off and we may accept pretty large positive ones.
3160 if (M == CodeModel::Kernel && Offset > 0)
3161 return true;
3162
3163 return false;
3164}
3165
Evan Chengef41ff62011-06-23 17:54:54 +00003166/// isCalleePop - Determines whether the callee is required to pop its
3167/// own arguments. Callee pop is necessary to support tail calls.
3168bool X86::isCalleePop(CallingConv::ID CallingConv,
3169 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3170 if (IsVarArg)
3171 return false;
3172
3173 switch (CallingConv) {
3174 default:
3175 return false;
3176 case CallingConv::X86_StdCall:
3177 return !is64Bit;
3178 case CallingConv::X86_FastCall:
3179 return !is64Bit;
3180 case CallingConv::X86_ThisCall:
3181 return !is64Bit;
3182 case CallingConv::Fast:
3183 return TailCallOpt;
3184 case CallingConv::GHC:
3185 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003186 case CallingConv::HiPE:
3187 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003188 }
3189}
3190
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003191/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3192/// specific condition code, returning the condition code and the LHS/RHS of the
3193/// comparison to make.
3194static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3195 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003196 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003197 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3198 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3199 // X > -1 -> X == 0, jump !sign.
3200 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003201 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003202 }
3203 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003204 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003205 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003206 }
3207 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003208 // X < 1 -> X <= 0
3209 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003210 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003211 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003212 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003213
Evan Chengd9558e02006-01-06 00:43:03 +00003214 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003215 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003216 case ISD::SETEQ: return X86::COND_E;
3217 case ISD::SETGT: return X86::COND_G;
3218 case ISD::SETGE: return X86::COND_GE;
3219 case ISD::SETLT: return X86::COND_L;
3220 case ISD::SETLE: return X86::COND_LE;
3221 case ISD::SETNE: return X86::COND_NE;
3222 case ISD::SETULT: return X86::COND_B;
3223 case ISD::SETUGT: return X86::COND_A;
3224 case ISD::SETULE: return X86::COND_BE;
3225 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003226 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003227 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003228
Chris Lattner4c78e022008-12-23 23:42:27 +00003229 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003230
Chris Lattner4c78e022008-12-23 23:42:27 +00003231 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003232 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3233 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003234 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3235 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003236 }
3237
Chris Lattner4c78e022008-12-23 23:42:27 +00003238 switch (SetCCOpcode) {
3239 default: break;
3240 case ISD::SETOLT:
3241 case ISD::SETOLE:
3242 case ISD::SETUGT:
3243 case ISD::SETUGE:
3244 std::swap(LHS, RHS);
3245 break;
3246 }
3247
3248 // On a floating point condition, the flags are set as follows:
3249 // ZF PF CF op
3250 // 0 | 0 | 0 | X > Y
3251 // 0 | 0 | 1 | X < Y
3252 // 1 | 0 | 0 | X == Y
3253 // 1 | 1 | 1 | unordered
3254 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003255 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003256 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003257 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003258 case ISD::SETOLT: // flipped
3259 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003260 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003261 case ISD::SETOLE: // flipped
3262 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003263 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003264 case ISD::SETUGT: // flipped
3265 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003266 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003267 case ISD::SETUGE: // flipped
3268 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003269 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003270 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003271 case ISD::SETNE: return X86::COND_NE;
3272 case ISD::SETUO: return X86::COND_P;
3273 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003274 case ISD::SETOEQ:
3275 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003276 }
Evan Chengd9558e02006-01-06 00:43:03 +00003277}
3278
Evan Cheng4a460802006-01-11 00:33:36 +00003279/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3280/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003281/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003282static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003283 switch (X86CC) {
3284 default:
3285 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003286 case X86::COND_B:
3287 case X86::COND_BE:
3288 case X86::COND_E:
3289 case X86::COND_P:
3290 case X86::COND_A:
3291 case X86::COND_AE:
3292 case X86::COND_NE:
3293 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003294 return true;
3295 }
3296}
3297
Evan Chengeb2f9692009-10-27 19:56:55 +00003298/// isFPImmLegal - Returns true if the target can instruction select the
3299/// specified FP immediate natively. If false, the legalizer will
3300/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003301bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003302 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3303 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3304 return true;
3305 }
3306 return false;
3307}
3308
Nate Begeman9008ca62009-04-27 18:41:29 +00003309/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3310/// the specified range (L, H].
3311static bool isUndefOrInRange(int Val, int Low, int Hi) {
3312 return (Val < 0) || (Val >= Low && Val < Hi);
3313}
3314
3315/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3316/// specified value.
3317static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003318 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003319}
3320
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003321/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003322/// from position Pos and ending in Pos+Size, falls within the specified
3323/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003324static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003325 unsigned Pos, unsigned Size, int Low) {
3326 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003327 if (!isUndefOrEqual(Mask[i], Low))
3328 return false;
3329 return true;
3330}
3331
Nate Begeman9008ca62009-04-27 18:41:29 +00003332/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3333/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3334/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003335static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003336 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003337 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003338 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003339 return (Mask[0] < 2 && Mask[1] < 2);
3340 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003341}
3342
Nate Begeman9008ca62009-04-27 18:41:29 +00003343/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3344/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003345static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3346 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003347 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003348
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003350 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3351 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003352
Evan Cheng506d3df2006-03-29 23:07:14 +00003353 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003354 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003355 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003356 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003357
Craig Toppera9a568a2012-05-02 08:03:44 +00003358 if (VT == MVT::v16i16) {
3359 // Lower quadword copied in order or undef.
3360 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3361 return false;
3362
3363 // Upper quadword shuffled.
3364 for (unsigned i = 12; i != 16; ++i)
3365 if (!isUndefOrInRange(Mask[i], 12, 16))
3366 return false;
3367 }
3368
Evan Cheng506d3df2006-03-29 23:07:14 +00003369 return true;
3370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3373/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003374static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3375 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003376 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003377
Rafael Espindola15684b22009-04-24 12:40:33 +00003378 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003379 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3380 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003381
Rafael Espindola15684b22009-04-24 12:40:33 +00003382 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003383 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003384 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003385 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003386
Craig Toppera9a568a2012-05-02 08:03:44 +00003387 if (VT == MVT::v16i16) {
3388 // Upper quadword copied in order.
3389 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3390 return false;
3391
3392 // Lower quadword shuffled.
3393 for (unsigned i = 8; i != 12; ++i)
3394 if (!isUndefOrInRange(Mask[i], 8, 12))
3395 return false;
3396 }
3397
Rafael Espindola15684b22009-04-24 12:40:33 +00003398 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003399}
3400
Nate Begemana09008b2009-10-19 02:17:23 +00003401/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3402/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003403static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3404 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003405 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3406 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003407 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003408
Craig Topper0e2037b2012-01-20 05:53:00 +00003409 unsigned NumElts = VT.getVectorNumElements();
3410 unsigned NumLanes = VT.getSizeInBits()/128;
3411 unsigned NumLaneElts = NumElts/NumLanes;
3412
3413 // Do not handle 64-bit element shuffles with palignr.
3414 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003415 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003416
Craig Topper0e2037b2012-01-20 05:53:00 +00003417 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3418 unsigned i;
3419 for (i = 0; i != NumLaneElts; ++i) {
3420 if (Mask[i+l] >= 0)
3421 break;
3422 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003423
Craig Topper0e2037b2012-01-20 05:53:00 +00003424 // Lane is all undef, go to next lane
3425 if (i == NumLaneElts)
3426 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003427
Craig Topper0e2037b2012-01-20 05:53:00 +00003428 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003429
Craig Topper0e2037b2012-01-20 05:53:00 +00003430 // Make sure its in this lane in one of the sources
3431 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3432 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003433 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003434
3435 // If not lane 0, then we must match lane 0
3436 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3437 return false;
3438
3439 // Correct second source to be contiguous with first source
3440 if (Start >= (int)NumElts)
3441 Start -= NumElts - NumLaneElts;
3442
3443 // Make sure we're shifting in the right direction.
3444 if (Start <= (int)(i+l))
3445 return false;
3446
3447 Start -= i;
3448
3449 // Check the rest of the elements to see if they are consecutive.
3450 for (++i; i != NumLaneElts; ++i) {
3451 int Idx = Mask[i+l];
3452
3453 // Make sure its in this lane
3454 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3455 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3456 return false;
3457
3458 // If not lane 0, then we must match lane 0
3459 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3460 return false;
3461
3462 if (Idx >= (int)NumElts)
3463 Idx -= NumElts - NumLaneElts;
3464
3465 if (!isUndefOrEqual(Idx, Start+i))
3466 return false;
3467
3468 }
Nate Begemana09008b2009-10-19 02:17:23 +00003469 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003470
Nate Begemana09008b2009-10-19 02:17:23 +00003471 return true;
3472}
3473
Craig Topper1a7700a2012-01-19 08:19:12 +00003474/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3475/// the two vector operands have swapped position.
3476static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3477 unsigned NumElems) {
3478 for (unsigned i = 0; i != NumElems; ++i) {
3479 int idx = Mask[i];
3480 if (idx < 0)
3481 continue;
3482 else if (idx < (int)NumElems)
3483 Mask[i] = idx + NumElems;
3484 else
3485 Mask[i] = idx - NumElems;
3486 }
3487}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003488
Craig Topper1a7700a2012-01-19 08:19:12 +00003489/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3490/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3491/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3492/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003493static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003494 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003495 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003496 return false;
3497
Craig Topper1a7700a2012-01-19 08:19:12 +00003498 unsigned NumElems = VT.getVectorNumElements();
3499 unsigned NumLanes = VT.getSizeInBits()/128;
3500 unsigned NumLaneElems = NumElems/NumLanes;
3501
3502 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003503 return false;
3504
3505 // VSHUFPSY divides the resulting vector into 4 chunks.
3506 // The sources are also splitted into 4 chunks, and each destination
3507 // chunk must come from a different source chunk.
3508 //
3509 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3510 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3511 //
3512 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3513 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3514 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003515 // VSHUFPDY divides the resulting vector into 4 chunks.
3516 // The sources are also splitted into 4 chunks, and each destination
3517 // chunk must come from a different source chunk.
3518 //
3519 // SRC1 => X3 X2 X1 X0
3520 // SRC2 => Y3 Y2 Y1 Y0
3521 //
3522 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3523 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003524 unsigned HalfLaneElems = NumLaneElems/2;
3525 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3526 for (unsigned i = 0; i != NumLaneElems; ++i) {
3527 int Idx = Mask[i+l];
3528 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3529 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3530 return false;
3531 // For VSHUFPSY, the mask of the second half must be the same as the
3532 // first but with the appropriate offsets. This works in the same way as
3533 // VPERMILPS works with masks.
3534 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3535 continue;
3536 if (!isUndefOrEqual(Idx, Mask[i]+l))
3537 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003538 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003539 }
3540
3541 return true;
3542}
3543
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003544/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3545/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003546static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003547 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003548 return false;
3549
Craig Topper7a9a28b2012-08-12 02:23:29 +00003550 unsigned NumElems = VT.getVectorNumElements();
3551
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003552 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003553 return false;
3554
Evan Cheng2064a2b2006-03-28 06:50:32 +00003555 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003556 return isUndefOrEqual(Mask[0], 6) &&
3557 isUndefOrEqual(Mask[1], 7) &&
3558 isUndefOrEqual(Mask[2], 2) &&
3559 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003560}
3561
Nate Begeman0b10b912009-11-07 23:17:15 +00003562/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3563/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3564/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003565static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003566 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003567 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003568
Craig Topper7a9a28b2012-08-12 02:23:29 +00003569 unsigned NumElems = VT.getVectorNumElements();
3570
Nate Begeman0b10b912009-11-07 23:17:15 +00003571 if (NumElems != 4)
3572 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003573
Craig Topperdd637ae2012-02-19 05:41:45 +00003574 return isUndefOrEqual(Mask[0], 2) &&
3575 isUndefOrEqual(Mask[1], 3) &&
3576 isUndefOrEqual(Mask[2], 2) &&
3577 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003578}
3579
Evan Cheng5ced1d82006-04-06 23:23:56 +00003580/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3581/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003582static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003583 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003584 return false;
3585
Craig Topperdd637ae2012-02-19 05:41:45 +00003586 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003587
Evan Cheng5ced1d82006-04-06 23:23:56 +00003588 if (NumElems != 2 && NumElems != 4)
3589 return false;
3590
Chad Rosier238ae312012-04-30 17:47:15 +00003591 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003592 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003593 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003594
Chad Rosier238ae312012-04-30 17:47:15 +00003595 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003596 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003597 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003598
3599 return true;
3600}
3601
Nate Begeman0b10b912009-11-07 23:17:15 +00003602/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3603/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003604static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003605 if (!VT.is128BitVector())
3606 return false;
3607
Craig Topperdd637ae2012-02-19 05:41:45 +00003608 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003609
Craig Topper7a9a28b2012-08-12 02:23:29 +00003610 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003611 return false;
3612
Chad Rosier238ae312012-04-30 17:47:15 +00003613 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003614 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003615 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003616
Chad Rosier238ae312012-04-30 17:47:15 +00003617 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3618 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003619 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003620
3621 return true;
3622}
3623
Elena Demikhovsky15963732012-06-26 08:04:10 +00003624//
3625// Some special combinations that can be optimized.
3626//
3627static
3628SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3629 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003630 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003631 DebugLoc dl = SVOp->getDebugLoc();
3632
3633 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3634 return SDValue();
3635
3636 ArrayRef<int> Mask = SVOp->getMask();
3637
3638 // These are the special masks that may be optimized.
3639 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3640 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3641 bool MatchEvenMask = true;
3642 bool MatchOddMask = true;
3643 for (int i=0; i<8; ++i) {
3644 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3645 MatchEvenMask = false;
3646 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3647 MatchOddMask = false;
3648 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003649
Elena Demikhovsky32510202012-09-04 12:49:02 +00003650 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003651 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003652
Elena Demikhovsky15963732012-06-26 08:04:10 +00003653 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3654
Elena Demikhovsky32510202012-09-04 12:49:02 +00003655 SDValue Op0 = SVOp->getOperand(0);
3656 SDValue Op1 = SVOp->getOperand(1);
3657
3658 if (MatchEvenMask) {
3659 // Shift the second operand right to 32 bits.
3660 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3661 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3662 } else {
3663 // Shift the first operand left to 32 bits.
3664 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3665 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3666 }
3667 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3668 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003669}
3670
Evan Cheng0038e592006-03-28 00:39:58 +00003671/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3672/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003673static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003674 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003675 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003676
3677 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3678 "Unsupported vector type for unpckh");
3679
Craig Topper5a529e42013-01-18 06:44:29 +00003680 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003681 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003682 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003683
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003684 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3685 // independently on 128-bit lanes.
3686 unsigned NumLanes = VT.getSizeInBits()/128;
3687 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003688
Craig Topper94438ba2011-12-16 08:06:31 +00003689 for (unsigned l = 0; l != NumLanes; ++l) {
3690 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3691 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003692 i += 2, ++j) {
3693 int BitI = Mask[i];
3694 int BitI1 = Mask[i+1];
3695 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003696 return false;
David Greenea20244d2011-03-02 17:23:43 +00003697 if (V2IsSplat) {
3698 if (!isUndefOrEqual(BitI1, NumElts))
3699 return false;
3700 } else {
3701 if (!isUndefOrEqual(BitI1, j + NumElts))
3702 return false;
3703 }
Evan Cheng39623da2006-04-20 08:58:49 +00003704 }
Evan Cheng0038e592006-03-28 00:39:58 +00003705 }
David Greenea20244d2011-03-02 17:23:43 +00003706
Evan Cheng0038e592006-03-28 00:39:58 +00003707 return true;
3708}
3709
Evan Cheng4fcb9222006-03-28 02:43:26 +00003710/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3711/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003712static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003713 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003714 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003715
3716 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3717 "Unsupported vector type for unpckh");
3718
Craig Topper5a529e42013-01-18 06:44:29 +00003719 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003720 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003721 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003722
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003723 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3724 // independently on 128-bit lanes.
3725 unsigned NumLanes = VT.getSizeInBits()/128;
3726 unsigned NumLaneElts = NumElts/NumLanes;
3727
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003728 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003729 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3730 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003731 int BitI = Mask[i];
3732 int BitI1 = Mask[i+1];
3733 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003734 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003735 if (V2IsSplat) {
3736 if (isUndefOrEqual(BitI1, NumElts))
3737 return false;
3738 } else {
3739 if (!isUndefOrEqual(BitI1, j+NumElts))
3740 return false;
3741 }
Evan Cheng39623da2006-04-20 08:58:49 +00003742 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003743 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003744 return true;
3745}
3746
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003747/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3748/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3749/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003750static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003751 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003752 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003753
3754 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3755 "Unsupported vector type for unpckh");
3756
Craig Topper5a529e42013-01-18 06:44:29 +00003757 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003758 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003759 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003760
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003761 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3762 // FIXME: Need a better way to get rid of this, there's no latency difference
3763 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3764 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003765 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003766 return false;
3767
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003768 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3769 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003770 unsigned NumLanes = VT.getSizeInBits()/128;
3771 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003772
Craig Topper94438ba2011-12-16 08:06:31 +00003773 for (unsigned l = 0; l != NumLanes; ++l) {
3774 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3775 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003776 i += 2, ++j) {
3777 int BitI = Mask[i];
3778 int BitI1 = Mask[i+1];
3779
3780 if (!isUndefOrEqual(BitI, j))
3781 return false;
3782 if (!isUndefOrEqual(BitI1, j))
3783 return false;
3784 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003785 }
David Greenea20244d2011-03-02 17:23:43 +00003786
Rafael Espindola15684b22009-04-24 12:40:33 +00003787 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003788}
3789
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003790/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3791/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3792/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003793static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003794 unsigned NumElts = VT.getVectorNumElements();
3795
3796 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3797 "Unsupported vector type for unpckh");
3798
Craig Topper5a529e42013-01-18 06:44:29 +00003799 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003800 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003801 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003802
Craig Topper94438ba2011-12-16 08:06:31 +00003803 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3804 // independently on 128-bit lanes.
3805 unsigned NumLanes = VT.getSizeInBits()/128;
3806 unsigned NumLaneElts = NumElts/NumLanes;
3807
3808 for (unsigned l = 0; l != NumLanes; ++l) {
3809 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3810 i != (l+1)*NumLaneElts; i += 2, ++j) {
3811 int BitI = Mask[i];
3812 int BitI1 = Mask[i+1];
3813 if (!isUndefOrEqual(BitI, j))
3814 return false;
3815 if (!isUndefOrEqual(BitI1, j))
3816 return false;
3817 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003818 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003819 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003820}
3821
Evan Cheng017dcc62006-04-21 01:05:10 +00003822/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3823/// specifies a shuffle of elements that is suitable for input to MOVSS,
3824/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003825static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003826 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003827 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003828 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003829 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003830
Craig Topperc612d792012-01-02 09:17:37 +00003831 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003832
Nate Begeman9008ca62009-04-27 18:41:29 +00003833 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003834 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003835
Craig Topperc612d792012-01-02 09:17:37 +00003836 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003838 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003839
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003840 return true;
3841}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003842
Craig Topper70b883b2011-11-28 10:14:51 +00003843/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003844/// as permutations between 128-bit chunks or halves. As an example: this
3845/// shuffle bellow:
3846/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3847/// The first half comes from the second half of V1 and the second half from the
3848/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003849static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3850 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003851 return false;
3852
3853 // The shuffle result is divided into half A and half B. In total the two
3854 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3855 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003856 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003857 bool MatchA = false, MatchB = false;
3858
3859 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003860 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003861 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3862 MatchA = true;
3863 break;
3864 }
3865 }
3866
3867 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003868 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003869 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3870 MatchB = true;
3871 break;
3872 }
3873 }
3874
3875 return MatchA && MatchB;
3876}
3877
Craig Topper70b883b2011-11-28 10:14:51 +00003878/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3879/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003880static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003881 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003882
Craig Topperc612d792012-01-02 09:17:37 +00003883 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003884
Craig Topperc612d792012-01-02 09:17:37 +00003885 unsigned FstHalf = 0, SndHalf = 0;
3886 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003887 if (SVOp->getMaskElt(i) > 0) {
3888 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3889 break;
3890 }
3891 }
Craig Topperc612d792012-01-02 09:17:37 +00003892 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003893 if (SVOp->getMaskElt(i) > 0) {
3894 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3895 break;
3896 }
3897 }
3898
3899 return (FstHalf | (SndHalf << 4));
3900}
3901
Craig Topper70b883b2011-11-28 10:14:51 +00003902/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003903/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3904/// Note that VPERMIL mask matching is different depending whether theunderlying
3905/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3906/// to the same elements of the low, but to the higher half of the source.
3907/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003908/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003909static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3910 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003911 return false;
3912
Craig Topperc612d792012-01-02 09:17:37 +00003913 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003914 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003915 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003916 return false;
3917
Craig Topperc612d792012-01-02 09:17:37 +00003918 unsigned NumLanes = VT.getSizeInBits()/128;
3919 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003920 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003921 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003922 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003923 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003924 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003925 continue;
3926 // VPERMILPS handling
3927 if (Mask[i] < 0)
3928 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003929 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003930 return false;
3931 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003932 }
3933
3934 return true;
3935}
3936
Craig Topper5aaffa82012-02-19 02:53:47 +00003937/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003938/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003939/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003940static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003942 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003943 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003944
3945 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003946 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003947 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003948
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003950 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003951
Craig Topperc612d792012-01-02 09:17:37 +00003952 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3954 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3955 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003956 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003957
Evan Cheng39623da2006-04-20 08:58:49 +00003958 return true;
3959}
3960
Evan Chengd9539472006-04-14 21:59:03 +00003961/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3962/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003963/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003964static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003965 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003966 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003967 return false;
3968
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003969 unsigned NumElems = VT.getVectorNumElements();
3970
Craig Topper5a529e42013-01-18 06:44:29 +00003971 if ((VT.is128BitVector() && NumElems != 4) ||
3972 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003973 return false;
3974
3975 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003976 for (unsigned i = 0; i != NumElems; i += 2)
3977 if (!isUndefOrEqual(Mask[i], i+1) ||
3978 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003979 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003980
3981 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003982}
3983
3984/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3985/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003986/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003987static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003988 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003989 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003990 return false;
3991
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003992 unsigned NumElems = VT.getVectorNumElements();
3993
Craig Topper5a529e42013-01-18 06:44:29 +00003994 if ((VT.is128BitVector() && NumElems != 4) ||
3995 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003996 return false;
3997
3998 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003999 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00004000 if (!isUndefOrEqual(Mask[i], i) ||
4001 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004002 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004003
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004004 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004005}
4006
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004007/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4008/// specifies a shuffle of elements that is suitable for input to 256-bit
4009/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004010static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4011 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00004012 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004013
Craig Topper7a9a28b2012-08-12 02:23:29 +00004014 unsigned NumElts = VT.getVectorNumElements();
4015 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004016 return false;
4017
Craig Topperc612d792012-01-02 09:17:37 +00004018 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004019 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004020 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004021 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004022 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004023 return false;
4024 return true;
4025}
4026
Evan Cheng0b457f02008-09-25 20:50:48 +00004027/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004028/// specifies a shuffle of elements that is suitable for input to 128-bit
4029/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004030static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004031 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004032 return false;
4033
Craig Topperc612d792012-01-02 09:17:37 +00004034 unsigned e = VT.getVectorNumElements() / 2;
4035 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004036 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004037 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004038 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004039 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004040 return false;
4041 return true;
4042}
4043
David Greenec38a03e2011-02-03 15:50:00 +00004044/// isVEXTRACTF128Index - Return true if the specified
4045/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4046/// suitable for input to VEXTRACTF128.
4047bool X86::isVEXTRACTF128Index(SDNode *N) {
4048 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4049 return false;
4050
4051 // The index should be aligned on a 128-bit boundary.
4052 uint64_t Index =
4053 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4054
Craig Topper5141d972013-01-18 08:41:28 +00004055 MVT VT = N->getValueType(0).getSimpleVT();
4056 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004057 bool Result = (Index * ElSize) % 128 == 0;
4058
4059 return Result;
4060}
4061
David Greeneccacdc12011-02-04 16:08:29 +00004062/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4063/// operand specifies a subvector insert that is suitable for input to
4064/// VINSERTF128.
4065bool X86::isVINSERTF128Index(SDNode *N) {
4066 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4067 return false;
4068
4069 // The index should be aligned on a 128-bit boundary.
4070 uint64_t Index =
4071 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4072
Craig Topper5141d972013-01-18 08:41:28 +00004073 MVT VT = N->getValueType(0).getSimpleVT();
4074 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004075 bool Result = (Index * ElSize) % 128 == 0;
4076
4077 return Result;
4078}
4079
Evan Cheng63d33002006-03-22 08:01:21 +00004080/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004081/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004082/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004083static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004084 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004085
Craig Topper1a7700a2012-01-19 08:19:12 +00004086 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4087 "Unsupported vector type for PSHUF/SHUFP");
4088
4089 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4090 // independently on 128-bit lanes.
4091 unsigned NumElts = VT.getVectorNumElements();
4092 unsigned NumLanes = VT.getSizeInBits()/128;
4093 unsigned NumLaneElts = NumElts/NumLanes;
4094
4095 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4096 "Only supports 2 or 4 elements per lane");
4097
4098 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004099 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004100 for (unsigned i = 0; i != NumElts; ++i) {
4101 int Elt = N->getMaskElt(i);
4102 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004103 Elt &= NumLaneElts - 1;
4104 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004105 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004106 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004107
Evan Cheng63d33002006-03-22 08:01:21 +00004108 return Mask;
4109}
4110
Evan Cheng506d3df2006-03-29 23:07:14 +00004111/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004112/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004113static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004114 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004115
4116 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4117 "Unsupported vector type for PSHUFHW");
4118
4119 unsigned NumElts = VT.getVectorNumElements();
4120
Evan Cheng506d3df2006-03-29 23:07:14 +00004121 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004122 for (unsigned l = 0; l != NumElts; l += 8) {
4123 // 8 nodes per lane, but we only care about the last 4.
4124 for (unsigned i = 0; i < 4; ++i) {
4125 int Elt = N->getMaskElt(l+i+4);
4126 if (Elt < 0) continue;
4127 Elt &= 0x3; // only 2-bits.
4128 Mask |= Elt << (i * 2);
4129 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004130 }
Craig Topper6b28d352012-05-03 07:12:59 +00004131
Evan Cheng506d3df2006-03-29 23:07:14 +00004132 return Mask;
4133}
4134
4135/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004136/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004137static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004138 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004139
4140 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4141 "Unsupported vector type for PSHUFHW");
4142
4143 unsigned NumElts = VT.getVectorNumElements();
4144
Evan Cheng506d3df2006-03-29 23:07:14 +00004145 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004146 for (unsigned l = 0; l != NumElts; l += 8) {
4147 // 8 nodes per lane, but we only care about the first 4.
4148 for (unsigned i = 0; i < 4; ++i) {
4149 int Elt = N->getMaskElt(l+i);
4150 if (Elt < 0) continue;
4151 Elt &= 0x3; // only 2-bits
4152 Mask |= Elt << (i * 2);
4153 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004154 }
Craig Topper6b28d352012-05-03 07:12:59 +00004155
Evan Cheng506d3df2006-03-29 23:07:14 +00004156 return Mask;
4157}
4158
Nate Begemana09008b2009-10-19 02:17:23 +00004159/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4160/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004161static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004162 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004163 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004164
Craig Topper0e2037b2012-01-20 05:53:00 +00004165 unsigned NumElts = VT.getVectorNumElements();
4166 unsigned NumLanes = VT.getSizeInBits()/128;
4167 unsigned NumLaneElts = NumElts/NumLanes;
4168
4169 int Val = 0;
4170 unsigned i;
4171 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004172 Val = SVOp->getMaskElt(i);
4173 if (Val >= 0)
4174 break;
4175 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004176 if (Val >= (int)NumElts)
4177 Val -= NumElts - NumLaneElts;
4178
Eli Friedman63f8dde2011-07-25 21:36:45 +00004179 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004180 return (Val - i) * EltSize;
4181}
4182
David Greenec38a03e2011-02-03 15:50:00 +00004183/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4184/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4185/// instructions.
4186unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4187 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4188 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4189
4190 uint64_t Index =
4191 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4192
Craig Toppercfcab212013-01-19 08:27:45 +00004193 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4194 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004195
4196 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004197 return Index / NumElemsPerChunk;
4198}
4199
David Greeneccacdc12011-02-04 16:08:29 +00004200/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4201/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4202/// instructions.
4203unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4204 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4205 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4206
4207 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004208 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004209
Craig Toppercfcab212013-01-19 08:27:45 +00004210 MVT VecVT = N->getValueType(0).getSimpleVT();
4211 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004212
4213 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004214 return Index / NumElemsPerChunk;
4215}
4216
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004217/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4218/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4219/// Handles 256-bit.
4220static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004221 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004222
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004223 unsigned NumElts = VT.getVectorNumElements();
4224
Craig Topper095c5282012-04-15 23:48:57 +00004225 assert((VT.is256BitVector() && NumElts == 4) &&
4226 "Unsupported vector type for VPERMQ/VPERMPD");
4227
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004228 unsigned Mask = 0;
4229 for (unsigned i = 0; i != NumElts; ++i) {
4230 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004231 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004232 continue;
4233 Mask |= Elt << (i*2);
4234 }
4235
4236 return Mask;
4237}
Evan Cheng37b73872009-07-30 08:33:02 +00004238/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4239/// constant +0.0.
4240bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004241 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4242 return CN->isNullValue();
4243 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4244 return CFP->getValueAPF().isPosZero();
4245 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004246}
4247
Nate Begeman9008ca62009-04-27 18:41:29 +00004248/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4249/// their permute mask.
4250static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4251 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004252 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004253 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004254 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004255
Nate Begeman5a5ca152009-04-29 05:20:52 +00004256 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004257 int Idx = SVOp->getMaskElt(i);
4258 if (Idx >= 0) {
4259 if (Idx < (int)NumElems)
4260 Idx += NumElems;
4261 else
4262 Idx -= NumElems;
4263 }
4264 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004265 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004266 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4267 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004268}
4269
Evan Cheng533a0aa2006-04-19 20:35:22 +00004270/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4271/// match movhlps. The lower half elements should come from upper half of
4272/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004273/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004274static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004275 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004276 return false;
4277 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004278 return false;
4279 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004280 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004281 return false;
4282 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004283 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004284 return false;
4285 return true;
4286}
4287
Evan Cheng5ced1d82006-04-06 23:23:56 +00004288/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004289/// is promoted to a vector. It also returns the LoadSDNode by reference if
4290/// required.
4291static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004292 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4293 return false;
4294 N = N->getOperand(0).getNode();
4295 if (!ISD::isNON_EXTLoad(N))
4296 return false;
4297 if (LD)
4298 *LD = cast<LoadSDNode>(N);
4299 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004300}
4301
Dan Gohman65fd6562011-11-03 21:49:52 +00004302// Test whether the given value is a vector value which will be legalized
4303// into a load.
4304static bool WillBeConstantPoolLoad(SDNode *N) {
4305 if (N->getOpcode() != ISD::BUILD_VECTOR)
4306 return false;
4307
4308 // Check for any non-constant elements.
4309 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4310 switch (N->getOperand(i).getNode()->getOpcode()) {
4311 case ISD::UNDEF:
4312 case ISD::ConstantFP:
4313 case ISD::Constant:
4314 break;
4315 default:
4316 return false;
4317 }
4318
4319 // Vectors of all-zeros and all-ones are materialized with special
4320 // instructions rather than being loaded.
4321 return !ISD::isBuildVectorAllZeros(N) &&
4322 !ISD::isBuildVectorAllOnes(N);
4323}
4324
Evan Cheng533a0aa2006-04-19 20:35:22 +00004325/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4326/// match movlp{s|d}. The lower half elements should come from lower half of
4327/// V1 (and in order), and the upper half elements should come from the upper
4328/// half of V2 (and in order). And since V1 will become the source of the
4329/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004330static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004331 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004332 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004333 return false;
4334
Evan Cheng466685d2006-10-09 20:57:25 +00004335 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004336 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004337 // Is V2 is a vector load, don't do this transformation. We will try to use
4338 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004339 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004340 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004341
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004342 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004343
Evan Cheng533a0aa2006-04-19 20:35:22 +00004344 if (NumElems != 2 && NumElems != 4)
4345 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004346 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004347 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004348 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004349 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004350 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004351 return false;
4352 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004353}
4354
Evan Cheng39623da2006-04-20 08:58:49 +00004355/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4356/// all the same.
4357static bool isSplatVector(SDNode *N) {
4358 if (N->getOpcode() != ISD::BUILD_VECTOR)
4359 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004360
Dan Gohman475871a2008-07-27 21:46:04 +00004361 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004362 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4363 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004364 return false;
4365 return true;
4366}
4367
Evan Cheng213d2cf2007-05-17 18:45:50 +00004368/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004369/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004370/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004371static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004372 SDValue V1 = N->getOperand(0);
4373 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004374 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4375 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004377 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004378 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004379 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4380 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004381 if (Opc != ISD::BUILD_VECTOR ||
4382 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 return false;
4384 } else if (Idx >= 0) {
4385 unsigned Opc = V1.getOpcode();
4386 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4387 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004388 if (Opc != ISD::BUILD_VECTOR ||
4389 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004390 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004391 }
4392 }
4393 return true;
4394}
4395
4396/// getZeroVector - Returns a vector of specified type with all zero elements.
4397///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004398static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004399 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004400 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004401
Dale Johannesen0488fb62010-09-30 23:57:10 +00004402 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004403 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004404 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004405 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004406 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004407 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4408 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4409 } else { // SSE1
4410 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4411 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4412 }
Craig Topper5a529e42013-01-18 06:44:29 +00004413 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004414 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004415 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4416 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004417 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops,
4418 array_lengthof(Ops));
Craig Topper12216172012-01-13 08:12:35 +00004419 } else {
4420 // 256-bit logic and arithmetic instructions in AVX are all
4421 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4422 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4423 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004424 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops,
4425 array_lengthof(Ops));
Craig Topper12216172012-01-13 08:12:35 +00004426 }
Craig Topper9d352402012-04-23 07:24:41 +00004427 } else
4428 llvm_unreachable("Unexpected vector type");
4429
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004430 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004431}
4432
Chris Lattner8a594482007-11-25 00:24:49 +00004433/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004434/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4435/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4436/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004437static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004438 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004439 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004440
Owen Anderson825b72b2009-08-11 20:47:22 +00004441 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004442 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004443 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004444 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004445 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004446 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops,
4447 array_lengthof(Ops));
Craig Topper745a86b2011-11-19 22:34:59 +00004448 } else { // AVX
4449 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004450 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004451 }
Craig Topper5a529e42013-01-18 06:44:29 +00004452 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004453 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004454 } else
4455 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004456
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004457 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004458}
4459
Evan Cheng39623da2006-04-20 08:58:49 +00004460/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4461/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004462static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004463 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004464 if (Mask[i] > (int)NumElems) {
4465 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004466 }
Evan Cheng39623da2006-04-20 08:58:49 +00004467 }
Evan Cheng39623da2006-04-20 08:58:49 +00004468}
4469
Evan Cheng017dcc62006-04-21 01:05:10 +00004470/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4471/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004472static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 SDValue V2) {
4474 unsigned NumElems = VT.getVectorNumElements();
4475 SmallVector<int, 8> Mask;
4476 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004477 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 Mask.push_back(i);
4479 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004480}
4481
Nate Begeman9008ca62009-04-27 18:41:29 +00004482/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004483static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 SDValue V2) {
4485 unsigned NumElems = VT.getVectorNumElements();
4486 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004487 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004488 Mask.push_back(i);
4489 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004490 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004492}
4493
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004494/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004495static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 SDValue V2) {
4497 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004499 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 Mask.push_back(i + Half);
4501 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004502 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004503 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004504}
4505
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004506// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507// a generic shuffle instruction because the target has no such instructions.
4508// Generate shuffles which repeat i16 and i8 several times until they can be
4509// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004510static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004511 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004512 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004513 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004514
Nate Begeman9008ca62009-04-27 18:41:29 +00004515 while (NumElems > 4) {
4516 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004518 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004519 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004520 EltNo -= NumElems/2;
4521 }
4522 NumElems >>= 1;
4523 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004524 return V;
4525}
Eric Christopherfd179292009-08-27 18:07:15 +00004526
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004527/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4528static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4529 EVT VT = V.getValueType();
4530 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004531
Craig Topper5a529e42013-01-18 06:44:29 +00004532 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004533 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004534 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004535 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4536 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004537 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004538 // To use VPERMILPS to splat scalars, the second half of indicies must
4539 // refer to the higher part, which is a duplication of the lower one,
4540 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004541 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4542 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004543
4544 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4545 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4546 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004547 } else
4548 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004549
4550 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4551}
4552
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004553/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004554static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4555 EVT SrcVT = SV->getValueType(0);
4556 SDValue V1 = SV->getOperand(0);
4557 DebugLoc dl = SV->getDebugLoc();
4558
4559 int EltNo = SV->getSplatIndex();
4560 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004561 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004562
Craig Topper5a529e42013-01-18 06:44:29 +00004563 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4564 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004565
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004566 // Extract the 128-bit part containing the splat element and update
4567 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004568 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004569 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4570 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004571 EltNo -= NumElems/2;
4572 }
4573
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004574 // All i16 and i8 vector types can't be used directly by a generic shuffle
4575 // instruction because the target has no such instruction. Generate shuffles
4576 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004577 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004578 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004579 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004580 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004581
4582 // Recreate the 256-bit vector and place the same 128-bit vector
4583 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004584 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004585 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004586 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004587 }
4588
4589 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004590}
4591
Evan Chengba05f722006-04-21 23:03:30 +00004592/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004593/// vector of zero or undef vector. This produces a shuffle where the low
4594/// element of V2 is swizzled into the zero/undef vector, landing at element
4595/// 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 +00004596static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004597 bool IsZero,
4598 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004599 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004600 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004601 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004602 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004603 unsigned NumElems = VT.getVectorNumElements();
4604 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004605 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004606 // If this is the insertion idx, put the low elt of V2 here.
4607 MaskVec.push_back(i == Idx ? NumElems : i);
4608 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004609}
4610
Craig Toppera1ffc682012-03-20 06:42:26 +00004611/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4612/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004613/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004614static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004615 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004616 unsigned NumElems = VT.getVectorNumElements();
4617 SDValue ImmN;
4618
Craig Topper89f4e662012-03-20 07:17:59 +00004619 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004620 switch(N->getOpcode()) {
4621 case X86ISD::SHUFP:
4622 ImmN = N->getOperand(N->getNumOperands()-1);
4623 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4624 break;
4625 case X86ISD::UNPCKH:
4626 DecodeUNPCKHMask(VT, Mask);
4627 break;
4628 case X86ISD::UNPCKL:
4629 DecodeUNPCKLMask(VT, Mask);
4630 break;
4631 case X86ISD::MOVHLPS:
4632 DecodeMOVHLPSMask(NumElems, Mask);
4633 break;
4634 case X86ISD::MOVLHPS:
4635 DecodeMOVLHPSMask(NumElems, Mask);
4636 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004637 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004638 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004639 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004640 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004641 case X86ISD::PSHUFD:
4642 case X86ISD::VPERMILP:
4643 ImmN = N->getOperand(N->getNumOperands()-1);
4644 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004645 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004646 break;
4647 case X86ISD::PSHUFHW:
4648 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004649 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004650 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004651 break;
4652 case X86ISD::PSHUFLW:
4653 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004654 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004655 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004656 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004657 case X86ISD::VPERMI:
4658 ImmN = N->getOperand(N->getNumOperands()-1);
4659 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4660 IsUnary = true;
4661 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004662 case X86ISD::MOVSS:
4663 case X86ISD::MOVSD: {
4664 // The index 0 always comes from the first element of the second source,
4665 // this is why MOVSS and MOVSD are used in the first place. The other
4666 // elements come from the other positions of the first source vector
4667 Mask.push_back(NumElems);
4668 for (unsigned i = 1; i != NumElems; ++i) {
4669 Mask.push_back(i);
4670 }
4671 break;
4672 }
4673 case X86ISD::VPERM2X128:
4674 ImmN = N->getOperand(N->getNumOperands()-1);
4675 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004676 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004677 break;
4678 case X86ISD::MOVDDUP:
4679 case X86ISD::MOVLHPD:
4680 case X86ISD::MOVLPD:
4681 case X86ISD::MOVLPS:
4682 case X86ISD::MOVSHDUP:
4683 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004684 // Not yet implemented
4685 return false;
4686 default: llvm_unreachable("unknown target shuffle node");
4687 }
4688
4689 return true;
4690}
4691
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4693/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004694static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004695 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004696 if (Depth == 6)
4697 return SDValue(); // Limit search depth.
4698
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004699 SDValue V = SDValue(N, 0);
4700 EVT VT = V.getValueType();
4701 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004702
4703 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4704 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004705 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004706
Craig Topper3d092db2012-03-21 02:14:01 +00004707 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708 return DAG.getUNDEF(VT.getVectorElementType());
4709
Craig Topperd156dc12012-02-06 07:17:51 +00004710 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004711 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4712 : SV->getOperand(1);
4713 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004714 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004715
4716 // Recurse into target specific vector shuffles to find scalars.
4717 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004718 MVT ShufVT = V.getValueType().getSimpleVT();
4719 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004720 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004721 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004722
Craig Topperd978c542012-05-06 19:46:21 +00004723 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004724 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004725
Craig Topper3d092db2012-03-21 02:14:01 +00004726 int Elt = ShuffleMask[Index];
4727 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004728 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004729
Craig Topper3d092db2012-03-21 02:14:01 +00004730 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004731 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004732 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004733 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004734 }
4735
4736 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004737 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004738 V = V.getOperand(0);
4739 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004740 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004741
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004742 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743 return SDValue();
4744 }
4745
4746 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4747 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004748 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004749
4750 if (V.getOpcode() == ISD::BUILD_VECTOR)
4751 return V.getOperand(Index);
4752
4753 return SDValue();
4754}
4755
4756/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4757/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004758/// search can start in two different directions, from left or right.
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004759/// We count undefs as zeros until PreferredNum is reached.
4760static unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp,
4761 unsigned NumElems, bool ZerosFromLeft,
4762 SelectionDAG &DAG,
4763 unsigned PreferredNum = -1U) {
4764 unsigned NumZeros = 0;
4765 for (unsigned i = 0; i != NumElems; ++i) {
4766 unsigned Index = ZerosFromLeft ? i : NumElems - i - 1;
Craig Topper3d092db2012-03-21 02:14:01 +00004767 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004768 if (!Elt.getNode())
4769 break;
4770
4771 if (X86::isZeroNode(Elt))
4772 ++NumZeros;
4773 else if (Elt.getOpcode() == ISD::UNDEF) // Undef as zero up to PreferredNum.
4774 NumZeros = std::min(NumZeros + 1, PreferredNum);
4775 else
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004776 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004777 }
4778
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004779 return NumZeros;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004780}
4781
Craig Topper3d092db2012-03-21 02:14:01 +00004782/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4783/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004784/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4785static
Craig Topper3d092db2012-03-21 02:14:01 +00004786bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4787 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4788 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004789 bool SeenV1 = false;
4790 bool SeenV2 = false;
4791
Craig Topper3d092db2012-03-21 02:14:01 +00004792 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004793 int Idx = SVOp->getMaskElt(i);
4794 // Ignore undef indicies
4795 if (Idx < 0)
4796 continue;
4797
Craig Topper3d092db2012-03-21 02:14:01 +00004798 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004799 SeenV1 = true;
4800 else
4801 SeenV2 = true;
4802
4803 // Only accept consecutive elements from the same vector
4804 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4805 return false;
4806 }
4807
4808 OpNum = SeenV1 ? 0 : 1;
4809 return true;
4810}
4811
4812/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4813/// logical left shift of a vector.
4814static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4815 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4816 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004817 unsigned NumZeros = getNumOfConsecutiveZeros(
4818 SVOp, NumElems, false /* check zeros from right */, DAG,
4819 SVOp->getMaskElt(0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004820 unsigned OpSrc;
4821
4822 if (!NumZeros)
4823 return false;
4824
4825 // Considering the elements in the mask that are not consecutive zeros,
4826 // check if they consecutively come from only one of the source vectors.
4827 //
4828 // V1 = {X, A, B, C} 0
4829 // \ \ \ /
4830 // vector_shuffle V1, V2 <1, 2, 3, X>
4831 //
4832 if (!isShuffleMaskConsecutive(SVOp,
4833 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004834 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004835 NumZeros, // Where to start looking in the src vector
4836 NumElems, // Number of elements in vector
4837 OpSrc)) // Which source operand ?
4838 return false;
4839
4840 isLeft = false;
4841 ShAmt = NumZeros;
4842 ShVal = SVOp->getOperand(OpSrc);
4843 return true;
4844}
4845
4846/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4847/// logical left shift of a vector.
4848static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4849 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4850 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004851 unsigned NumZeros = getNumOfConsecutiveZeros(
4852 SVOp, NumElems, true /* check zeros from left */, DAG,
4853 NumElems - SVOp->getMaskElt(NumElems - 1) - 1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004854 unsigned OpSrc;
4855
4856 if (!NumZeros)
4857 return false;
4858
4859 // Considering the elements in the mask that are not consecutive zeros,
4860 // check if they consecutively come from only one of the source vectors.
4861 //
4862 // 0 { A, B, X, X } = V2
4863 // / \ / /
4864 // vector_shuffle V1, V2 <X, X, 4, 5>
4865 //
4866 if (!isShuffleMaskConsecutive(SVOp,
4867 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004868 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004869 0, // Where to start looking in the src vector
4870 NumElems, // Number of elements in vector
4871 OpSrc)) // Which source operand ?
4872 return false;
4873
4874 isLeft = true;
4875 ShAmt = NumZeros;
4876 ShVal = SVOp->getOperand(OpSrc);
4877 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004878}
4879
4880/// isVectorShift - Returns true if the shuffle can be implemented as a
4881/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004882static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004883 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004884 // Although the logic below support any bitwidth size, there are no
4885 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004886 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004887 return false;
4888
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004889 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4890 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4891 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004892
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004893 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004894}
4895
Evan Chengc78d3b42006-04-24 18:01:45 +00004896/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4897///
Dan Gohman475871a2008-07-27 21:46:04 +00004898static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004899 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004900 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004901 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004902 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004903 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004904 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004905
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004906 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004907 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004908 bool First = true;
4909 for (unsigned i = 0; i < 16; ++i) {
4910 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4911 if (ThisIsNonZero && First) {
4912 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004913 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004916 First = false;
4917 }
4918
4919 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004921 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4922 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004923 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004924 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004925 }
4926 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4928 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4929 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004930 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004931 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004932 } else
4933 ThisElt = LastElt;
4934
Gabor Greifba36cb52008-08-28 21:40:38 +00004935 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004936 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004937 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004938 }
4939 }
4940
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004941 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004942}
4943
Bill Wendlinga348c562007-03-22 18:42:45 +00004944/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004945///
Dan Gohman475871a2008-07-27 21:46:04 +00004946static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004947 unsigned NumNonZero, unsigned NumZero,
4948 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004949 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004950 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004951 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004952 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004953
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004954 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004955 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004956 bool First = true;
4957 for (unsigned i = 0; i < 8; ++i) {
4958 bool isNonZero = (NonZeros & (1 << i)) != 0;
4959 if (isNonZero) {
4960 if (First) {
4961 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004962 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004963 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004964 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004965 First = false;
4966 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004967 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004968 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004969 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004970 }
4971 }
4972
4973 return V;
4974}
4975
Evan Chengf26ffe92008-05-29 08:22:04 +00004976/// getVShift - Return a vector logical shift node.
4977///
Owen Andersone50ed302009-08-10 22:56:29 +00004978static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004979 unsigned NumBits, SelectionDAG &DAG,
4980 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004981 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004982 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004983 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004984 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4985 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004986 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004987 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004988 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004989}
4990
Dan Gohman475871a2008-07-27 21:46:04 +00004991SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004992X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004993 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004994
Evan Chengc3630942009-12-09 21:00:30 +00004995 // Check if the scalar load can be widened into a vector load. And if
4996 // the address is "base + cst" see if the cst can be "absorbed" into
4997 // the shuffle mask.
4998 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4999 SDValue Ptr = LD->getBasePtr();
5000 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5001 return SDValue();
5002 EVT PVT = LD->getValueType(0);
5003 if (PVT != MVT::i32 && PVT != MVT::f32)
5004 return SDValue();
5005
5006 int FI = -1;
5007 int64_t Offset = 0;
5008 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5009 FI = FINode->getIndex();
5010 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00005011 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00005012 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5013 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5014 Offset = Ptr.getConstantOperandVal(1);
5015 Ptr = Ptr.getOperand(0);
5016 } else {
5017 return SDValue();
5018 }
5019
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005020 // FIXME: 256-bit vector instructions don't require a strict alignment,
5021 // improve this code to support it better.
5022 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005023 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005024 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005025 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005026 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005027 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005028 // Can't change the alignment. FIXME: It's possible to compute
5029 // the exact stack offset and reference FI + adjust offset instead.
5030 // If someone *really* cares about this. That's the way to implement it.
5031 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005032 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005033 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005034 }
5035 }
5036
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005037 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005038 // Ptr + (Offset & ~15).
5039 if (Offset < 0)
5040 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005041 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005042 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005043 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005044 if (StartOffset)
5045 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5046 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5047
5048 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005049 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005050
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005051 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5052 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005053 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005054 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005055
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005056 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005057 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005058 Mask.push_back(EltNo);
5059
Craig Toppercc3000632012-01-30 07:50:31 +00005060 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005061 }
5062
5063 return SDValue();
5064}
5065
Michael J. Spencerec38de22010-10-10 22:04:20 +00005066/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5067/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005068/// load which has the same value as a build_vector whose operands are 'elts'.
5069///
5070/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005071///
Nate Begeman1449f292010-03-24 22:19:06 +00005072/// FIXME: we'd also like to handle the case where the last elements are zero
5073/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5074/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005075static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005076 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005077 EVT EltVT = VT.getVectorElementType();
5078 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005079
Nate Begemanfdea31a2010-03-24 20:49:50 +00005080 LoadSDNode *LDBase = NULL;
5081 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005082
Nate Begeman1449f292010-03-24 22:19:06 +00005083 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005084 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005085 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005086 for (unsigned i = 0; i < NumElems; ++i) {
5087 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005088
Nate Begemanfdea31a2010-03-24 20:49:50 +00005089 if (!Elt.getNode() ||
5090 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5091 return SDValue();
5092 if (!LDBase) {
5093 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5094 return SDValue();
5095 LDBase = cast<LoadSDNode>(Elt.getNode());
5096 LastLoadedElt = i;
5097 continue;
5098 }
5099 if (Elt.getOpcode() == ISD::UNDEF)
5100 continue;
5101
5102 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5103 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5104 return SDValue();
5105 LastLoadedElt = i;
5106 }
Nate Begeman1449f292010-03-24 22:19:06 +00005107
5108 // If we have found an entire vector of loads and undefs, then return a large
5109 // load of the entire vector width starting at the base pointer. If we found
5110 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005111 if (LastLoadedElt == NumElems - 1) {
5112 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005113 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005114 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005115 LDBase->isVolatile(), LDBase->isNonTemporal(),
5116 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005117 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005118 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005119 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005120 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005121 }
5122 if (NumElems == 4 && LastLoadedElt == 1 &&
5123 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005124 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5125 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005126 SDValue ResNode =
Michael Liao0ee17002013-04-19 04:03:37 +00005127 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops,
5128 array_lengthof(Ops), MVT::i64,
Eli Friedman322ea082011-09-14 23:42:45 +00005129 LDBase->getPointerInfo(),
5130 LDBase->getAlignment(),
5131 false/*isVolatile*/, true/*ReadMem*/,
5132 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005133
5134 // Make sure the newly-created LOAD is in the same position as LDBase in
5135 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5136 // update uses of LDBase's output chain to use the TokenFactor.
5137 if (LDBase->hasAnyUseOfValue(1)) {
5138 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5139 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5140 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5141 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5142 SDValue(ResNode.getNode(), 1));
5143 }
5144
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005145 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005146 }
5147 return SDValue();
5148}
5149
Nadav Rotem9d68b062012-04-08 12:54:54 +00005150/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5151/// to generate a splat value for the following cases:
5152/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005153/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005154/// a scalar load, or a constant.
5155/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005156/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005157SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005158X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005159 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005160 return SDValue();
5161
Craig Topper45e1c752013-01-20 00:38:18 +00005162 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005163 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005164
Craig Topper5da8a802012-05-04 05:49:51 +00005165 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5166 "Unsupported vector type for broadcast.");
5167
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005168 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005169 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005170
Nadav Rotem9d68b062012-04-08 12:54:54 +00005171 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005172 default:
5173 // Unknown pattern found.
5174 return SDValue();
5175
5176 case ISD::BUILD_VECTOR: {
5177 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005178 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005179 return SDValue();
5180
Nadav Rotem9d68b062012-04-08 12:54:54 +00005181 Ld = Op.getOperand(0);
5182 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5183 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005184
5185 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005186 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005187 // Constants may have multiple users.
5188 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005189 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005190 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005191 }
5192
5193 case ISD::VECTOR_SHUFFLE: {
5194 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5195
5196 // Shuffles must have a splat mask where the first element is
5197 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005198 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005199 return SDValue();
5200
5201 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005202 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005203 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5204
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005205 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005206 return SDValue();
5207
5208 // Use the register form of the broadcast instruction available on AVX2.
5209 if (VT.is256BitVector())
5210 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5211 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5212 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005213
5214 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005215 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005216 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005217
5218 // The scalar_to_vector node and the suspected
5219 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005220 // Constants may have multiple users.
5221 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005222 return SDValue();
5223 break;
5224 }
5225 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005226
Craig Topper7a9a28b2012-08-12 02:23:29 +00005227 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005228
5229 // Handle the broadcasting a single constant scalar from the constant pool
5230 // into a vector. On Sandybridge it is still better to load a constant vector
5231 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005232 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005233 EVT CVT = Ld.getValueType();
5234 assert(!CVT.isVector() && "Must not broadcast a vector type");
5235 unsigned ScalarSize = CVT.getSizeInBits();
5236
Craig Topper5da8a802012-05-04 05:49:51 +00005237 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005238 const Constant *C = 0;
5239 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5240 C = CI->getConstantIntValue();
5241 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5242 C = CF->getConstantFPValue();
5243
5244 assert(C && "Invalid constant type");
5245
Nadav Rotem154819d2012-04-09 07:45:58 +00005246 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005247 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005248 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005249 MachinePointerInfo::getConstantPool(),
5250 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005251
Nadav Rotem9d68b062012-04-08 12:54:54 +00005252 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5253 }
5254 }
5255
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005256 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005257 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5258
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005259 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005260 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005261 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5262 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5263
5264 // The scalar source must be a normal load.
5265 if (!IsLoad)
5266 return SDValue();
5267
Craig Topper5da8a802012-05-04 05:49:51 +00005268 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005269 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005270
Craig Toppera9376332012-01-10 08:23:59 +00005271 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005272 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005273 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005274 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005275 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005276 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005277
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005278 // Unsupported broadcast.
5279 return SDValue();
5280}
5281
Evan Chengc3630942009-12-09 21:00:30 +00005282SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005283X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5284 EVT VT = Op.getValueType();
5285
5286 // Skip if insert_vec_elt is not supported.
5287 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5288 return SDValue();
5289
5290 DebugLoc DL = Op.getDebugLoc();
5291 unsigned NumElems = Op.getNumOperands();
5292
5293 SDValue VecIn1;
5294 SDValue VecIn2;
5295 SmallVector<unsigned, 4> InsertIndices;
5296 SmallVector<int, 8> Mask(NumElems, -1);
5297
5298 for (unsigned i = 0; i != NumElems; ++i) {
5299 unsigned Opc = Op.getOperand(i).getOpcode();
5300
5301 if (Opc == ISD::UNDEF)
5302 continue;
5303
5304 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5305 // Quit if more than 1 elements need inserting.
5306 if (InsertIndices.size() > 1)
5307 return SDValue();
5308
5309 InsertIndices.push_back(i);
5310 continue;
5311 }
5312
5313 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5314 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5315
5316 // Quit if extracted from vector of different type.
5317 if (ExtractedFromVec.getValueType() != VT)
5318 return SDValue();
5319
5320 // Quit if non-constant index.
5321 if (!isa<ConstantSDNode>(ExtIdx))
5322 return SDValue();
5323
5324 if (VecIn1.getNode() == 0)
5325 VecIn1 = ExtractedFromVec;
5326 else if (VecIn1 != ExtractedFromVec) {
5327 if (VecIn2.getNode() == 0)
5328 VecIn2 = ExtractedFromVec;
5329 else if (VecIn2 != ExtractedFromVec)
5330 // Quit if more than 2 vectors to shuffle
5331 return SDValue();
5332 }
5333
5334 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5335
5336 if (ExtractedFromVec == VecIn1)
5337 Mask[i] = Idx;
5338 else if (ExtractedFromVec == VecIn2)
5339 Mask[i] = Idx + NumElems;
5340 }
5341
5342 if (VecIn1.getNode() == 0)
5343 return SDValue();
5344
5345 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5346 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5347 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5348 unsigned Idx = InsertIndices[i];
5349 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5350 DAG.getIntPtrConstant(Idx));
5351 }
5352
5353 return NV;
5354}
5355
5356SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005357X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005358 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005359
Craig Topper45e1c752013-01-20 00:38:18 +00005360 MVT VT = Op.getValueType().getSimpleVT();
5361 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005362 unsigned NumElems = Op.getNumOperands();
5363
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005364 // Vectors containing all zeros can be matched by pxor and xorps later
5365 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5366 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5367 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005368 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005369 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005370
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005371 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005372 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005374 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005375 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5376 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005377 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005378 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005379 return Op;
5380
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005381 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005382 }
5383
Nadav Rotem154819d2012-04-09 07:45:58 +00005384 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005385 if (Broadcast.getNode())
5386 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005387
Owen Andersone50ed302009-08-10 22:56:29 +00005388 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005389
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390 unsigned NumZero = 0;
5391 unsigned NumNonZero = 0;
5392 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005393 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005394 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005395 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005396 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005397 if (Elt.getOpcode() == ISD::UNDEF)
5398 continue;
5399 Values.insert(Elt);
5400 if (Elt.getOpcode() != ISD::Constant &&
5401 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005402 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005403 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005404 NumZero++;
5405 else {
5406 NonZeros |= (1 << i);
5407 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005408 }
5409 }
5410
Chris Lattner97a2a562010-08-26 05:24:29 +00005411 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5412 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005413 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005414
Chris Lattner67f453a2008-03-09 05:42:06 +00005415 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005416 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005417 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005419
Chris Lattner62098042008-03-09 01:05:04 +00005420 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5421 // the value are obviously zero, truncate the value to i32 and do the
5422 // insertion that way. Only do this if the value is non-constant or if the
5423 // value is a constant being inserted into element 0. It is cheaper to do
5424 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005425 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005426 (!IsAllConstants || Idx == 0)) {
5427 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005428 // Handle SSE only.
5429 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5430 EVT VecVT = MVT::v4i32;
5431 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005432
Chris Lattner62098042008-03-09 01:05:04 +00005433 // Truncate the value (which may itself be a constant) to i32, and
5434 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005435 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005436 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005437 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005438
Chris Lattner62098042008-03-09 01:05:04 +00005439 // Now we have our 32-bit value zero extended in the low element of
5440 // a vector. If Idx != 0, swizzle it into place.
5441 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005442 SmallVector<int, 4> Mask;
5443 Mask.push_back(Idx);
5444 for (unsigned i = 1; i != VecElts; ++i)
5445 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005446 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005447 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005448 }
Craig Topper07a27622012-01-22 03:07:48 +00005449 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005450 }
5451 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005452
Chris Lattner19f79692008-03-08 22:59:52 +00005453 // If we have a constant or non-constant insertion into the low element of
5454 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5455 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005456 // depending on what the source datatype is.
5457 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005458 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005459 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005460
5461 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005462 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005463 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005464 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005465 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5466 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005467 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005468 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005469 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5470 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005471 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005472 }
5473
5474 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005475 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005476 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005477 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005478 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005479 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005480 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005481 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005482 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005483 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005484 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005485 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005486 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005487
5488 // Is it a vector logical left shift?
5489 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005490 X86::isZeroNode(Op.getOperand(0)) &&
5491 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005492 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005493 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005494 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005495 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005496 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005497 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005498
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005499 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005500 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005501
Chris Lattner19f79692008-03-08 22:59:52 +00005502 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5503 // is a non-constant being inserted into an element other than the low one,
5504 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5505 // movd/movss) to move this into the low element, then shuffle it into
5506 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005507 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005508 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005509
Evan Cheng0db9fe62006-04-25 20:13:52 +00005510 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005511 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005512 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005513 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005514 MaskVec.push_back(i == Idx ? 0 : 1);
5515 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005516 }
5517 }
5518
Chris Lattner67f453a2008-03-09 05:42:06 +00005519 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005520 if (Values.size() == 1) {
5521 if (EVTBits == 32) {
5522 // Instead of a shuffle like this:
5523 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5524 // Check if it's possible to issue this instead.
5525 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5526 unsigned Idx = CountTrailingZeros_32(NonZeros);
5527 SDValue Item = Op.getOperand(Idx);
5528 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5529 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5530 }
Dan Gohman475871a2008-07-27 21:46:04 +00005531 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005532 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005533
Dan Gohmana3941172007-07-24 22:55:08 +00005534 // A vector full of immediates; various special cases are already
5535 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005536 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005537 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005538
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005539 // For AVX-length vectors, build the individual 128-bit pieces and use
5540 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005541 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005542 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005543 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005544 V.push_back(Op.getOperand(i));
5545
5546 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5547
5548 // Build both the lower and upper subvector.
5549 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5550 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5551 NumElems/2);
5552
5553 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005554 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005555 }
5556
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005557 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005558 if (EVTBits == 64) {
5559 if (NumNonZero == 1) {
5560 // One half is zero or undef.
5561 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005562 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005563 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005564 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005565 }
Dan Gohman475871a2008-07-27 21:46:04 +00005566 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005567 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005568
5569 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005570 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005571 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005572 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005573 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005574 }
5575
Bill Wendling826f36f2007-03-28 00:57:11 +00005576 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005578 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005579 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005580 }
5581
5582 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005583 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005584 if (NumElems == 4 && NumZero > 0) {
5585 for (unsigned i = 0; i < 4; ++i) {
5586 bool isZero = !(NonZeros & (1 << i));
5587 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005588 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005589 else
Dale Johannesenace16102009-02-03 19:33:06 +00005590 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005591 }
5592
5593 for (unsigned i = 0; i < 2; ++i) {
5594 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5595 default: break;
5596 case 0:
5597 V[i] = V[i*2]; // Must be a zero vector.
5598 break;
5599 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005600 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005601 break;
5602 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005603 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005604 break;
5605 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005606 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005607 break;
5608 }
5609 }
5610
Benjamin Kramer9c683542012-01-30 15:16:21 +00005611 bool Reverse1 = (NonZeros & 0x3) == 2;
5612 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5613 int MaskVec[] = {
5614 Reverse1 ? 1 : 0,
5615 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005616 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5617 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005618 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005620 }
5621
Craig Topper7a9a28b2012-08-12 02:23:29 +00005622 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005623 // Check for a build vector of consecutive loads.
5624 for (unsigned i = 0; i < NumElems; ++i)
5625 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005626
Nate Begemanfdea31a2010-03-24 20:49:50 +00005627 // Check for elements which are consecutive loads.
5628 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5629 if (LD.getNode())
5630 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005631
Michael Liaofacace82012-10-19 17:15:18 +00005632 // Check for a build vector from mostly shuffle plus few inserting.
5633 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5634 if (Sh.getNode())
5635 return Sh;
5636
Michael J. Spencerec38de22010-10-10 22:04:20 +00005637 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005638 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005639 SDValue Result;
5640 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5641 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5642 else
5643 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005644
Chris Lattner24faf612010-08-28 17:59:08 +00005645 for (unsigned i = 1; i < NumElems; ++i) {
5646 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5647 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005648 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005649 }
5650 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005651 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005652
Chris Lattner6e80e442010-08-28 17:15:43 +00005653 // Otherwise, expand into a number of unpckl*, start by extending each of
5654 // our (non-undef) elements to the full vector width with the element in the
5655 // bottom slot of the vector (which generates no code for SSE).
5656 for (unsigned i = 0; i < NumElems; ++i) {
5657 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5658 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5659 else
5660 V[i] = DAG.getUNDEF(VT);
5661 }
5662
5663 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005664 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5665 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5666 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005667 unsigned EltStride = NumElems >> 1;
5668 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005669 for (unsigned i = 0; i < EltStride; ++i) {
5670 // If V[i+EltStride] is undef and this is the first round of mixing,
5671 // then it is safe to just drop this shuffle: V[i] is already in the
5672 // right place, the one element (since it's the first round) being
5673 // inserted as undef can be dropped. This isn't safe for successive
5674 // rounds because they will permute elements within both vectors.
5675 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5676 EltStride == NumElems/2)
5677 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005678
Chris Lattner6e80e442010-08-28 17:15:43 +00005679 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005680 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005681 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005682 }
5683 return V[0];
5684 }
Dan Gohman475871a2008-07-27 21:46:04 +00005685 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005686}
5687
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005688// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5689// to create 256-bit vectors from two other 128-bit ones.
5690static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5691 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005692 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005693
Craig Topper7a9a28b2012-08-12 02:23:29 +00005694 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005695
5696 SDValue V1 = Op.getOperand(0);
5697 SDValue V2 = Op.getOperand(1);
5698 unsigned NumElems = ResVT.getVectorNumElements();
5699
Craig Topper4c7972d2012-04-22 18:15:59 +00005700 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005701}
5702
Craig Topper55b24052012-09-11 06:15:32 +00005703static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005704 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005705
5706 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5707 // from two other 128-bit ones.
5708 return LowerAVXCONCAT_VECTORS(Op, DAG);
5709}
5710
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005711// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005712static SDValue
5713LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5714 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005715 SDValue V1 = SVOp->getOperand(0);
5716 SDValue V2 = SVOp->getOperand(1);
5717 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005718 MVT VT = SVOp->getValueType(0).getSimpleVT();
5719 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005720 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005721
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005722 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5723 return SDValue();
5724 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005725 return SDValue();
5726
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005727 // Check the mask for BLEND and build the value.
5728 unsigned MaskValue = 0;
5729 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005730 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005731 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005732
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005733 // Blend for v16i16 should be symetric for the both lanes.
5734 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005735
Craig Topper9b33ef72013-01-21 06:57:59 +00005736 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005737 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005738 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005739
Craig Topper04f74a12013-01-21 07:25:16 +00005740 if ((EltIdx < 0 || EltIdx == (int)i) &&
5741 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005742 continue;
5743
Craig Topper9b33ef72013-01-21 06:57:59 +00005744 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005745 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005746 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5747 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005748 else
Craig Topper1842ba02012-04-23 06:38:28 +00005749 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005750 }
5751
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005752 // Convert i32 vectors to floating point if it is not AVX2.
5753 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005754 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005755 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005756 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5757 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005758 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5759 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5760 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005761
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005762 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5763 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005764 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005765}
5766
Nate Begemanb9a47b82009-02-23 08:49:38 +00005767// v8i16 shuffles - Prefer shuffles in the following order:
5768// 1. [all] pshuflw, pshufhw, optional move
5769// 2. [ssse3] 1 x pshufb
5770// 3. [ssse3] 2 x pshufb + 1 x por
5771// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005772static SDValue
5773LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5774 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005775 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005776 SDValue V1 = SVOp->getOperand(0);
5777 SDValue V2 = SVOp->getOperand(1);
5778 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005780
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // Determine if more than 1 of the words in each of the low and high quadwords
5782 // of the result come from the same quadword of one of the two inputs. Undef
5783 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005784 unsigned LoQuad[] = { 0, 0, 0, 0 };
5785 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005786 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005788 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005789 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 MaskVals.push_back(EltIdx);
5791 if (EltIdx < 0) {
5792 ++Quad[0];
5793 ++Quad[1];
5794 ++Quad[2];
5795 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005796 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 }
5798 ++Quad[EltIdx / 4];
5799 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005800 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005801
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005803 unsigned MaxQuad = 1;
5804 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 if (LoQuad[i] > MaxQuad) {
5806 BestLoQuad = i;
5807 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005808 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005809 }
5810
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005812 MaxQuad = 1;
5813 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 if (HiQuad[i] > MaxQuad) {
5815 BestHiQuad = i;
5816 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005817 }
5818 }
5819
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005821 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 // single pshufb instruction is necessary. If There are more than 2 input
5823 // quads, disable the next transformation since it does not help SSSE3.
5824 bool V1Used = InputQuads[0] || InputQuads[1];
5825 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005826 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005828 BestLoQuad = InputQuads[0] ? 0 : 1;
5829 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005830 }
5831 if (InputQuads.count() > 2) {
5832 BestLoQuad = -1;
5833 BestHiQuad = -1;
5834 }
5835 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005836
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5838 // the shuffle mask. If a quad is scored as -1, that means that it contains
5839 // words from all 4 input quadwords.
5840 SDValue NewV;
5841 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005842 int MaskV[] = {
5843 BestLoQuad < 0 ? 0 : BestLoQuad,
5844 BestHiQuad < 0 ? 1 : BestHiQuad
5845 };
Eric Christopherfd179292009-08-27 18:07:15 +00005846 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005847 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5848 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5849 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005850
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5852 // source words for the shuffle, to aid later transformations.
5853 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005854 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005855 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005857 if (idx != (int)i)
5858 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005860 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 AllWordsInNewV = false;
5862 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005863 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005864
Nate Begemanb9a47b82009-02-23 08:49:38 +00005865 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5866 if (AllWordsInNewV) {
5867 for (int i = 0; i != 8; ++i) {
5868 int idx = MaskVals[i];
5869 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005870 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005871 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 if ((idx != i) && idx < 4)
5873 pshufhw = false;
5874 if ((idx != i) && idx > 3)
5875 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005876 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005877 V1 = NewV;
5878 V2Used = false;
5879 BestLoQuad = 0;
5880 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005881 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005882
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5884 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005885 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005886 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5887 unsigned TargetMask = 0;
5888 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005890 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5891 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5892 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005893 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005894 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005895 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005896 }
Eric Christopherfd179292009-08-27 18:07:15 +00005897
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005898 // Promote splats to a larger type which usually leads to more efficient code.
5899 // FIXME: Is this true if pshufb is available?
5900 if (SVOp->isSplat())
5901 return PromoteSplat(SVOp, DAG);
5902
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // If we have SSSE3, and all words of the result are from 1 input vector,
5904 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5905 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005906 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005908
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005910 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 // mask, and elements that come from V1 in the V2 mask, so that the two
5912 // results can be OR'd together.
5913 bool TwoInputs = V1Used && V2Used;
5914 for (unsigned i = 0; i != 8; ++i) {
5915 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005916 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5917 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005918 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005919 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005921 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005922 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005923 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005926 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005927
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 // Calculate the shuffle mask for the second input, shuffle it, and
5929 // OR it with the first shuffled input.
5930 pshufbMask.clear();
5931 for (unsigned i = 0; i != 8; ++i) {
5932 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005933 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5934 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5935 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5936 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005937 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005938 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005939 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005940 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005941 MVT::v16i8, &pshufbMask[0], 16));
5942 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005943 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 }
5945
5946 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5947 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005948 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005949 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005950 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 for (int i = 0; i != 4; ++i) {
5952 int idx = MaskVals[i];
5953 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 InOrder.set(i);
5955 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005956 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 }
5959 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005960 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005961 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005962
Craig Topperdd637ae2012-02-19 05:41:45 +00005963 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5964 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005965 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005966 NewV.getOperand(0),
5967 getShufflePSHUFLWImmediate(SVOp), DAG);
5968 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 }
Eric Christopherfd179292009-08-27 18:07:15 +00005970
Nate Begemanb9a47b82009-02-23 08:49:38 +00005971 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5972 // and update MaskVals with the new element order.
5973 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005974 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 for (unsigned i = 4; i != 8; ++i) {
5976 int idx = MaskVals[i];
5977 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 InOrder.set(i);
5979 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005980 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005981 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 }
5983 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005985 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005986
Craig Topperdd637ae2012-02-19 05:41:45 +00005987 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5988 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005989 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005990 NewV.getOperand(0),
5991 getShufflePSHUFHWImmediate(SVOp), DAG);
5992 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 }
Eric Christopherfd179292009-08-27 18:07:15 +00005994
Nate Begemanb9a47b82009-02-23 08:49:38 +00005995 // In case BestHi & BestLo were both -1, which means each quadword has a word
5996 // from each of the four input quadwords, calculate the InOrder bitvector now
5997 // before falling through to the insert/extract cleanup.
5998 if (BestLoQuad == -1 && BestHiQuad == -1) {
5999 NewV = V1;
6000 for (int i = 0; i != 8; ++i)
6001 if (MaskVals[i] < 0 || MaskVals[i] == i)
6002 InOrder.set(i);
6003 }
Eric Christopherfd179292009-08-27 18:07:15 +00006004
Nate Begemanb9a47b82009-02-23 08:49:38 +00006005 // The other elements are put in the right place using pextrw and pinsrw.
6006 for (unsigned i = 0; i != 8; ++i) {
6007 if (InOrder[i])
6008 continue;
6009 int EltIdx = MaskVals[i];
6010 if (EltIdx < 0)
6011 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00006012 SDValue ExtOp = (EltIdx < 8) ?
6013 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
6014 DAG.getIntPtrConstant(EltIdx)) :
6015 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00006017 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 DAG.getIntPtrConstant(i));
6019 }
6020 return NewV;
6021}
6022
6023// v16i8 shuffles - Prefer shuffles in the following order:
6024// 1. [ssse3] 1 x pshufb
6025// 2. [ssse3] 2 x pshufb + 1 x por
6026// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6027static
Nate Begeman9008ca62009-04-27 18:41:29 +00006028SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006029 SelectionDAG &DAG,
6030 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006031 SDValue V1 = SVOp->getOperand(0);
6032 SDValue V2 = SVOp->getOperand(1);
6033 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006034 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006035
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006036 // Promote splats to a larger type which usually leads to more efficient code.
6037 // FIXME: Is this true if pshufb is available?
6038 if (SVOp->isSplat())
6039 return PromoteSplat(SVOp, DAG);
6040
Nate Begemanb9a47b82009-02-23 08:49:38 +00006041 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006042 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006043 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006044
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006046 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006048
Nate Begemanb9a47b82009-02-23 08:49:38 +00006049 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006050 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006051 //
6052 // Otherwise, we have elements from both input vectors, and must zero out
6053 // elements that come from V2 in the first mask, and V1 in the second mask
6054 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006055 for (unsigned i = 0; i != 16; ++i) {
6056 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006057 if (EltIdx < 0 || EltIdx >= 16)
6058 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006059 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006060 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006061 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006062 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006063 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006064
6065 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6066 // the 2nd operand if it's undefined or zero.
6067 if (V2.getOpcode() == ISD::UNDEF ||
6068 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006069 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006070
Nate Begemanb9a47b82009-02-23 08:49:38 +00006071 // Calculate the shuffle mask for the second input, shuffle it, and
6072 // OR it with the first shuffled input.
6073 pshufbMask.clear();
6074 for (unsigned i = 0; i != 16; ++i) {
6075 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006076 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006077 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006078 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006079 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006080 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 MVT::v16i8, &pshufbMask[0], 16));
6082 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006083 }
Eric Christopherfd179292009-08-27 18:07:15 +00006084
Nate Begemanb9a47b82009-02-23 08:49:38 +00006085 // No SSSE3 - Calculate in place words and then fix all out of place words
6086 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6087 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006088 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6089 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006090 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006091 for (int i = 0; i != 8; ++i) {
6092 int Elt0 = MaskVals[i*2];
6093 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006094
Nate Begemanb9a47b82009-02-23 08:49:38 +00006095 // This word of the result is all undef, skip it.
6096 if (Elt0 < 0 && Elt1 < 0)
6097 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006098
Nate Begemanb9a47b82009-02-23 08:49:38 +00006099 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006100 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006101 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006102
Nate Begemanb9a47b82009-02-23 08:49:38 +00006103 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6104 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6105 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006106
6107 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6108 // using a single extract together, load it and store it.
6109 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006110 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006111 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006112 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006113 DAG.getIntPtrConstant(i));
6114 continue;
6115 }
6116
Nate Begemanb9a47b82009-02-23 08:49:38 +00006117 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006118 // source byte is not also odd, shift the extracted word left 8 bits
6119 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006120 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006121 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006122 DAG.getIntPtrConstant(Elt1 / 2));
6123 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006124 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006125 DAG.getConstant(8,
6126 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006127 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006128 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6129 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006130 }
6131 // If Elt0 is defined, extract it from the appropriate source. If the
6132 // source byte is not also even, shift the extracted word right 8 bits. If
6133 // Elt1 was also defined, OR the extracted values together before
6134 // inserting them in the result.
6135 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006136 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006137 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6138 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006139 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006140 DAG.getConstant(8,
6141 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006142 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006143 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6144 DAG.getConstant(0x00FF, MVT::i16));
6145 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006146 : InsElt0;
6147 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006148 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006149 DAG.getIntPtrConstant(i));
6150 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006151 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006152}
6153
Elena Demikhovsky41789462012-09-06 12:42:01 +00006154// v32i8 shuffles - Translate to VPSHUFB if possible.
6155static
6156SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006157 const X86Subtarget *Subtarget,
6158 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006159 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006160 SDValue V1 = SVOp->getOperand(0);
6161 SDValue V2 = SVOp->getOperand(1);
6162 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006163 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006164
6165 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006166 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6167 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006168
Michael Liao471b9172012-10-03 23:43:52 +00006169 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006170 // (1) one of input vector is undefined or zeroinitializer.
6171 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6172 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006173 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006174 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006175 return SDValue();
6176
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006177 if (V1IsAllZero && !V2IsAllZero) {
6178 CommuteVectorShuffleMask(MaskVals, 32);
6179 V1 = V2;
6180 }
6181 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006182 for (unsigned i = 0; i != 32; i++) {
6183 int EltIdx = MaskVals[i];
6184 if (EltIdx < 0 || EltIdx >= 32)
6185 EltIdx = 0x80;
6186 else {
6187 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6188 // Cross lane is not allowed.
6189 return SDValue();
6190 EltIdx &= 0xf;
6191 }
6192 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6193 }
6194 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6195 DAG.getNode(ISD::BUILD_VECTOR, dl,
6196 MVT::v32i8, &pshufbMask[0], 32));
6197}
6198
Evan Cheng7a831ce2007-12-15 03:00:47 +00006199/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006200/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006201/// done when every pair / quad of shuffle mask elements point to elements in
6202/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006203/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006204static
Nate Begeman9008ca62009-04-27 18:41:29 +00006205SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006206 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006207 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006208 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006209 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006210 MVT NewVT;
6211 unsigned Scale;
6212 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006213 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006214 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6215 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6216 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6217 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6218 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6219 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006220 }
6221
Nate Begeman9008ca62009-04-27 18:41:29 +00006222 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006223 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006224 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006225 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006226 int EltIdx = SVOp->getMaskElt(i+j);
6227 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006228 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006229 if (StartIdx < 0)
6230 StartIdx = (EltIdx / Scale);
6231 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006232 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006233 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006234 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006235 }
6236
Craig Topper11ac1f82012-05-04 04:08:44 +00006237 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6238 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006239 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006240}
6241
Evan Chengd880b972008-05-09 21:53:03 +00006242/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006243///
Craig Topperf84b7502013-01-20 00:50:58 +00006244static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006245 SDValue SrcOp, SelectionDAG &DAG,
6246 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006247 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006248 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006249 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006250 LD = dyn_cast<LoadSDNode>(SrcOp);
6251 if (!LD) {
6252 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6253 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006254 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006255 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006256 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006257 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006258 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006259 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006260 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006261 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006262 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6263 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6264 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006265 SrcOp.getOperand(0)
6266 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006267 }
6268 }
6269 }
6270
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006271 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006272 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006273 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006274 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006275}
6276
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006277/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6278/// which could not be matched by any known target speficic shuffle
6279static SDValue
6280LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006281
6282 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6283 if (NewOp.getNode())
6284 return NewOp;
6285
Craig Topper657a99c2013-01-19 23:36:09 +00006286 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006287
Craig Topper8f35c132012-01-20 09:29:03 +00006288 unsigned NumElems = VT.getVectorNumElements();
6289 unsigned NumLaneElems = NumElems / 2;
6290
Craig Topper8f35c132012-01-20 09:29:03 +00006291 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006292 MVT EltVT = VT.getVectorElementType();
6293 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006294 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006295
Craig Topper9a2b6e12012-04-06 07:45:23 +00006296 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006297 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006298 // Build a shuffle mask for the output, discovering on the fly which
6299 // input vectors to use as shuffle operands (recorded in InputUsed).
6300 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006301 // out with UseBuildVector set.
6302 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006303 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006304 unsigned LaneStart = l * NumLaneElems;
6305 for (unsigned i = 0; i != NumLaneElems; ++i) {
6306 // The mask element. This indexes into the input.
6307 int Idx = SVOp->getMaskElt(i+LaneStart);
6308 if (Idx < 0) {
6309 // the mask element does not index into any input vector.
6310 Mask.push_back(-1);
6311 continue;
6312 }
Craig Topper8f35c132012-01-20 09:29:03 +00006313
Craig Topper9a2b6e12012-04-06 07:45:23 +00006314 // The input vector this mask element indexes into.
6315 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006316
Craig Topper9a2b6e12012-04-06 07:45:23 +00006317 // Turn the index into an offset from the start of the input vector.
6318 Idx -= Input * NumLaneElems;
6319
6320 // Find or create a shuffle vector operand to hold this input.
6321 unsigned OpNo;
6322 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6323 if (InputUsed[OpNo] == Input)
6324 // This input vector is already an operand.
6325 break;
6326 if (InputUsed[OpNo] < 0) {
6327 // Create a new operand for this input vector.
6328 InputUsed[OpNo] = Input;
6329 break;
6330 }
6331 }
6332
6333 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006334 // More than two input vectors used! Give up on trying to create a
6335 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6336 UseBuildVector = true;
6337 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006338 }
6339
6340 // Add the mask index for the new shuffle vector.
6341 Mask.push_back(Idx + OpNo * NumLaneElems);
6342 }
6343
Craig Topper8ae97ba2012-05-21 06:40:16 +00006344 if (UseBuildVector) {
6345 SmallVector<SDValue, 16> SVOps;
6346 for (unsigned i = 0; i != NumLaneElems; ++i) {
6347 // The mask element. This indexes into the input.
6348 int Idx = SVOp->getMaskElt(i+LaneStart);
6349 if (Idx < 0) {
6350 SVOps.push_back(DAG.getUNDEF(EltVT));
6351 continue;
6352 }
6353
6354 // The input vector this mask element indexes into.
6355 int Input = Idx / NumElems;
6356
6357 // Turn the index into an offset from the start of the input vector.
6358 Idx -= Input * NumElems;
6359
6360 // Extract the vector element by hand.
6361 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6362 SVOp->getOperand(Input),
6363 DAG.getIntPtrConstant(Idx)));
6364 }
6365
6366 // Construct the output using a BUILD_VECTOR.
6367 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6368 SVOps.size());
6369 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006370 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006371 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006372 } else {
6373 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006374 (InputUsed[0] % 2) * NumLaneElems,
6375 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006376 // If only one input was used, use an undefined vector for the other.
6377 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6378 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006379 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006380 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006381 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006382 }
6383
6384 Mask.clear();
6385 }
Craig Topper8f35c132012-01-20 09:29:03 +00006386
6387 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006388 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006389}
6390
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006391/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6392/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006393static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006394LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006395 SDValue V1 = SVOp->getOperand(0);
6396 SDValue V2 = SVOp->getOperand(1);
6397 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006398 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006399
Craig Topper7a9a28b2012-08-12 02:23:29 +00006400 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006401
Benjamin Kramer9c683542012-01-30 15:16:21 +00006402 std::pair<int, int> Locs[4];
6403 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006404 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006405
Evan Chengace3c172008-07-22 21:13:36 +00006406 unsigned NumHi = 0;
6407 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006408 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006409 int Idx = PermMask[i];
6410 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006411 Locs[i] = std::make_pair(-1, -1);
6412 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006413 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6414 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006415 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006417 NumLo++;
6418 } else {
6419 Locs[i] = std::make_pair(1, NumHi);
6420 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006421 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006422 NumHi++;
6423 }
6424 }
6425 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006426
Evan Chengace3c172008-07-22 21:13:36 +00006427 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006428 // If no more than two elements come from either vector. This can be
6429 // implemented with two shuffles. First shuffle gather the elements.
6430 // The second shuffle, which takes the first shuffle as both of its
6431 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006432 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006433
Benjamin Kramer9c683542012-01-30 15:16:21 +00006434 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006435
Benjamin Kramer9c683542012-01-30 15:16:21 +00006436 for (unsigned i = 0; i != 4; ++i)
6437 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006438 unsigned Idx = (i < 2) ? 0 : 4;
6439 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006440 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006441 }
Evan Chengace3c172008-07-22 21:13:36 +00006442
Nate Begeman9008ca62009-04-27 18:41:29 +00006443 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006444 }
6445
6446 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006447 // Otherwise, we must have three elements from one vector, call it X, and
6448 // one element from the other, call it Y. First, use a shufps to build an
6449 // intermediate vector with the one element from Y and the element from X
6450 // that will be in the same half in the final destination (the indexes don't
6451 // matter). Then, use a shufps to build the final vector, taking the half
6452 // containing the element from Y from the intermediate, and the other half
6453 // from X.
6454 if (NumHi == 3) {
6455 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006456 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006457 std::swap(V1, V2);
6458 }
6459
6460 // Find the element from V2.
6461 unsigned HiIndex;
6462 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006463 int Val = PermMask[HiIndex];
6464 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006465 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006466 if (Val >= 4)
6467 break;
6468 }
6469
Nate Begeman9008ca62009-04-27 18:41:29 +00006470 Mask1[0] = PermMask[HiIndex];
6471 Mask1[1] = -1;
6472 Mask1[2] = PermMask[HiIndex^1];
6473 Mask1[3] = -1;
6474 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006475
6476 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006477 Mask1[0] = PermMask[0];
6478 Mask1[1] = PermMask[1];
6479 Mask1[2] = HiIndex & 1 ? 6 : 4;
6480 Mask1[3] = HiIndex & 1 ? 4 : 6;
6481 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006482 }
Craig Topper69947b92012-04-23 06:57:04 +00006483
6484 Mask1[0] = HiIndex & 1 ? 2 : 0;
6485 Mask1[1] = HiIndex & 1 ? 0 : 2;
6486 Mask1[2] = PermMask[2];
6487 Mask1[3] = PermMask[3];
6488 if (Mask1[2] >= 0)
6489 Mask1[2] += 4;
6490 if (Mask1[3] >= 0)
6491 Mask1[3] += 4;
6492 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006493 }
6494
6495 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006496 int LoMask[] = { -1, -1, -1, -1 };
6497 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006498
Benjamin Kramer9c683542012-01-30 15:16:21 +00006499 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006500 unsigned MaskIdx = 0;
6501 unsigned LoIdx = 0;
6502 unsigned HiIdx = 2;
6503 for (unsigned i = 0; i != 4; ++i) {
6504 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006505 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006506 MaskIdx = 1;
6507 LoIdx = 0;
6508 HiIdx = 2;
6509 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006510 int Idx = PermMask[i];
6511 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006512 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006513 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006514 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006515 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006516 LoIdx++;
6517 } else {
6518 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006519 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006520 HiIdx++;
6521 }
6522 }
6523
Nate Begeman9008ca62009-04-27 18:41:29 +00006524 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6525 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006526 int MaskOps[] = { -1, -1, -1, -1 };
6527 for (unsigned i = 0; i != 4; ++i)
6528 if (Locs[i].first != -1)
6529 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006530 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006531}
6532
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006533static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006534 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006535 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006536
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006537 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6538 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006539 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6540 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6541 // BUILD_VECTOR (load), undef
6542 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006543
6544 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006545}
6546
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006547static
Evan Cheng835580f2010-10-07 20:50:20 +00006548SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6549 EVT VT = Op.getValueType();
6550
6551 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006552 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6553 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006554 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6555 V1, DAG));
6556}
6557
6558static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006559SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006560 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006561 SDValue V1 = Op.getOperand(0);
6562 SDValue V2 = Op.getOperand(1);
6563 EVT VT = Op.getValueType();
6564
6565 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6566
Craig Topper1accb7e2012-01-10 06:54:16 +00006567 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006568 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6569
Evan Cheng0899f5c2011-08-31 02:05:24 +00006570 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6571 return DAG.getNode(ISD::BITCAST, dl, VT,
6572 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6573 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6574 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006575}
6576
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006577static
6578SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6579 SDValue V1 = Op.getOperand(0);
6580 SDValue V2 = Op.getOperand(1);
6581 EVT VT = Op.getValueType();
6582
6583 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6584 "unsupported shuffle type");
6585
6586 if (V2.getOpcode() == ISD::UNDEF)
6587 V2 = V1;
6588
6589 // v4i32 or v4f32
6590 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6591}
6592
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006593static
Craig Topper1accb7e2012-01-10 06:54:16 +00006594SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006595 SDValue V1 = Op.getOperand(0);
6596 SDValue V2 = Op.getOperand(1);
6597 EVT VT = Op.getValueType();
6598 unsigned NumElems = VT.getVectorNumElements();
6599
6600 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6601 // operand of these instructions is only memory, so check if there's a
6602 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6603 // same masks.
6604 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006605
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006606 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006607 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006608 CanFoldLoad = true;
6609
6610 // When V1 is a load, it can be folded later into a store in isel, example:
6611 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6612 // turns into:
6613 // (MOVLPSmr addr:$src1, VR128:$src2)
6614 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006615 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006616 CanFoldLoad = true;
6617
Dan Gohman65fd6562011-11-03 21:49:52 +00006618 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006619 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006620 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006621 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6622
6623 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006624 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006625 if (SVOp->getMaskElt(1) != -1)
6626 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006627 }
6628
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006629 // movl and movlp will both match v2i64, but v2i64 is never matched by
6630 // movl earlier because we make it strict to avoid messing with the movlp load
6631 // folding logic (see the code above getMOVLP call). Match it here then,
6632 // this is horrible, but will stay like this until we move all shuffle
6633 // matching to x86 specific nodes. Note that for the 1st condition all
6634 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006635 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006636 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6637 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006638 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006639 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006640 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006641 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006642
6643 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6644
6645 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006646 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006647 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006648}
6649
Michael Liaod9d09602012-10-23 17:34:00 +00006650// Reduce a vector shuffle to zext.
6651SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006652X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006653 // PMOVZX is only available from SSE41.
6654 if (!Subtarget->hasSSE41())
6655 return SDValue();
6656
6657 EVT VT = Op.getValueType();
6658
6659 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006660 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006661 return SDValue();
6662
6663 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6664 DebugLoc DL = Op.getDebugLoc();
6665 SDValue V1 = Op.getOperand(0);
6666 SDValue V2 = Op.getOperand(1);
6667 unsigned NumElems = VT.getVectorNumElements();
6668
6669 // Extending is an unary operation and the element type of the source vector
6670 // won't be equal to or larger than i64.
6671 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6672 VT.getVectorElementType() == MVT::i64)
6673 return SDValue();
6674
6675 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6676 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006677 while ((1U << Shift) < NumElems) {
6678 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006679 break;
6680 Shift += 1;
6681 // The maximal ratio is 8, i.e. from i8 to i64.
6682 if (Shift > 3)
6683 return SDValue();
6684 }
6685
6686 // Check the shuffle mask.
6687 unsigned Mask = (1U << Shift) - 1;
6688 for (unsigned i = 0; i != NumElems; ++i) {
6689 int EltIdx = SVOp->getMaskElt(i);
6690 if ((i & Mask) != 0 && EltIdx != -1)
6691 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006692 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006693 return SDValue();
6694 }
6695
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006696 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006697 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006698 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6699 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006700
6701 if (!isTypeLegal(NVT))
6702 return SDValue();
6703
6704 // Simplify the operand as it's prepared to be fed into shuffle.
6705 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6706 if (V1.getOpcode() == ISD::BITCAST &&
6707 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6708 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6709 V1.getOperand(0)
6710 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6711 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6712 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006713 ConstantSDNode *CIdx =
6714 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006715 // If it's foldable, i.e. normal load with single use, we will let code
6716 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006717 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006718 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6719 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6720 // The "ext_vec_elt" node is wider than the result node.
6721 // In this case we should extract subvector from V.
6722 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6723 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6724 EVT FullVT = V.getValueType();
6725 EVT SubVecVT = EVT::getVectorVT(*Context,
6726 FullVT.getVectorElementType(),
6727 FullVT.getVectorNumElements()/Ratio);
6728 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
6729 DAG.getIntPtrConstant(0));
6730 }
Michael Liaod9d09602012-10-23 17:34:00 +00006731 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006732 }
Michael Liaod9d09602012-10-23 17:34:00 +00006733 }
6734
6735 return DAG.getNode(ISD::BITCAST, DL, VT,
6736 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6737}
6738
Nadav Rotem154819d2012-04-09 07:45:58 +00006739SDValue
6740X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006741 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006742 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006743 DebugLoc dl = Op.getDebugLoc();
6744 SDValue V1 = Op.getOperand(0);
6745 SDValue V2 = Op.getOperand(1);
6746
6747 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006748 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006749
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006750 // Handle splat operations
6751 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006752 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006753 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006754 if (Broadcast.getNode())
6755 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006756 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006757
Michael Liaod9d09602012-10-23 17:34:00 +00006758 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006759 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006760 if (NewOp.getNode())
6761 return NewOp;
6762
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006763 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6764 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006765 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6766 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006767 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006768 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006769 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006770 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006771 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006772 // FIXME: Figure out a cleaner way to do this.
6773 // Try to make use of movq to zero out the top part.
6774 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006775 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006776 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006777 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006778 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6779 NewVT, true, false))
6780 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006781 DAG, Subtarget, dl);
6782 }
6783 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006784 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006785 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006786 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006787 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6788 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6789 DAG, Subtarget, dl);
6790 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006791 }
6792 }
6793 return SDValue();
6794}
6795
Dan Gohman475871a2008-07-27 21:46:04 +00006796SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006797X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006798 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006799 SDValue V1 = Op.getOperand(0);
6800 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006801 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006802 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006803 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006804 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006805 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006806 bool V1IsSplat = false;
6807 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006808 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006809 bool HasFp256 = Subtarget->hasFp256();
6810 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006811 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006812 bool OptForSize = MF.getFunction()->getAttributes().
6813 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006814
Craig Topper3426a3e2011-11-14 06:46:21 +00006815 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006816
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006817 if (V1IsUndef && V2IsUndef)
6818 return DAG.getUNDEF(VT);
6819
6820 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006821
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006822 // Vector shuffle lowering takes 3 steps:
6823 //
6824 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6825 // narrowing and commutation of operands should be handled.
6826 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6827 // shuffle nodes.
6828 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6829 // so the shuffle can be broken into other shuffles and the legalizer can
6830 // try the lowering again.
6831 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006832 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006833 // be matched during isel, all of them must be converted to a target specific
6834 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006835
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006836 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6837 // narrowing and commutation of operands should be handled. The actual code
6838 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006839 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006840 if (NewOp.getNode())
6841 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006842
Craig Topper5aaffa82012-02-19 02:53:47 +00006843 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6844
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006845 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6846 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006847 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006848 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006849 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006850 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006851
Craig Topperdd637ae2012-02-19 05:41:45 +00006852 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006853 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006854 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006855
Craig Topperdd637ae2012-02-19 05:41:45 +00006856 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006857 return getMOVHighToLow(Op, dl, DAG);
6858
6859 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006860 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006861 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006862 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006863
Craig Topper5aaffa82012-02-19 02:53:47 +00006864 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006865 // The actual implementation will match the mask in the if above and then
6866 // during isel it can match several different instructions, not only pshufd
6867 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006868 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6869 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006870
Craig Topper5aaffa82012-02-19 02:53:47 +00006871 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006872
Craig Topper1accb7e2012-01-10 06:54:16 +00006873 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006874 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6875
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006876 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6877 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6878 DAG);
6879
Craig Topperb3982da2011-12-31 23:50:21 +00006880 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006881 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006882 }
Eric Christopherfd179292009-08-27 18:07:15 +00006883
Benjamin Kramera0de26c2013-05-17 14:48:34 +00006884 if (isPALIGNRMask(M, VT, Subtarget))
6885 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
6886 getShufflePALIGNRImmediate(SVOp),
6887 DAG);
6888
Evan Chengf26ffe92008-05-29 08:22:04 +00006889 // Check if this can be converted into a logical shift.
6890 bool isLeft = false;
6891 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006892 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006893 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006894 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006895 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006896 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006897 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006898 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006899 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006900 }
Eric Christopherfd179292009-08-27 18:07:15 +00006901
Craig Topper5aaffa82012-02-19 02:53:47 +00006902 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006903 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006904 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006905 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006906 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006907 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6908
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006909 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006910 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6911 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006912 }
Eric Christopherfd179292009-08-27 18:07:15 +00006913
Nate Begeman9008ca62009-04-27 18:41:29 +00006914 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006915 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006916 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006917
Craig Topperdd637ae2012-02-19 05:41:45 +00006918 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006919 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006920
Craig Topperdd637ae2012-02-19 05:41:45 +00006921 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006922 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006923
Craig Topperdd637ae2012-02-19 05:41:45 +00006924 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006925 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006926
Craig Topperdd637ae2012-02-19 05:41:45 +00006927 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006928 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929
Craig Topperdd637ae2012-02-19 05:41:45 +00006930 if (ShouldXformToMOVHLPS(M, VT) ||
6931 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006932 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006933
Evan Chengf26ffe92008-05-29 08:22:04 +00006934 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006935 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006936 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006937 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006938 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006939 }
Eric Christopherfd179292009-08-27 18:07:15 +00006940
Evan Cheng9eca5e82006-10-25 21:49:50 +00006941 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006942 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6943 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006944 V1IsSplat = isSplatVector(V1.getNode());
6945 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006946
Chris Lattner8a594482007-11-25 00:24:49 +00006947 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006948 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6949 CommuteVectorShuffleMask(M, NumElems);
6950 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006951 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006952 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006953 }
6954
Craig Topperbeabc6c2011-12-05 06:56:46 +00006955 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006956 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006957 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006958 return V1;
6959 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6960 // the instruction selector will not match, so get a canonical MOVL with
6961 // swapped operands to undo the commute.
6962 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006963 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006965 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006966 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006967
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006968 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006969 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006970
Evan Cheng9bbbb982006-10-25 20:48:19 +00006971 if (V2IsSplat) {
6972 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006973 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006974 // new vector_shuffle with the corrected mask.p
6975 SmallVector<int, 8> NewMask(M.begin(), M.end());
6976 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006977 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006978 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006979 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006980 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006981 }
6982
Evan Cheng9eca5e82006-10-25 21:49:50 +00006983 if (Commuted) {
6984 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006985 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006986 CommuteVectorShuffleMask(M, NumElems);
6987 std::swap(V1, V2);
6988 std::swap(V1IsSplat, V2IsSplat);
6989 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006990
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006991 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006992 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006993
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006994 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006995 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006996 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006997
Nate Begeman9008ca62009-04-27 18:41:29 +00006998 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006999 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00007000 return CommuteVectorShuffle(SVOp, DAG);
7001
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00007002 // The checks below are all present in isShuffleMaskLegal, but they are
7003 // inlined here right now to enable us to directly emit target specific
7004 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00007005
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00007006 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
7007 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00007008 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00007009 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00007010 }
7011
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007012 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007013 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007014 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007015 DAG);
7016
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007017 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007018 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007019 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007020 DAG);
7021
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007022 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00007023 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00007024 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00007025
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007026 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007027 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007028 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007029 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007030
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007031 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007032 // Generate target specific nodes for 128 or 256-bit shuffles only
7033 // supported in the AVX instruction set.
7034 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007035
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007036 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007037 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007038 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7039
Craig Topper70b883b2011-11-28 10:14:51 +00007040 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007041 if (isVPERMILPMask(M, VT, HasFp256)) {
7042 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007043 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007044 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007045 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007046 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007047 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007048
Craig Topper70b883b2011-11-28 10:14:51 +00007049 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007050 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007051 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007052 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007053
Craig Topper1842ba02012-04-23 06:38:28 +00007054 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007055 if (BlendOp.getNode())
7056 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007057
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007058 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007059 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007060 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007061 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007062 }
Craig Topper92040742012-04-16 06:43:40 +00007063 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7064 &permclMask[0], 8);
7065 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007066 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007067 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007068 }
Craig Topper095c5282012-04-15 23:48:57 +00007069
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007070 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007071 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007072 getShuffleCLImmediate(SVOp), DAG);
7073
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007074 //===--------------------------------------------------------------------===//
7075 // Since no target specific shuffle was selected for this generic one,
7076 // lower it into other known shuffles. FIXME: this isn't true yet, but
7077 // this is the plan.
7078 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007079
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007080 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7081 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007082 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007083 if (NewOp.getNode())
7084 return NewOp;
7085 }
7086
7087 if (VT == MVT::v16i8) {
7088 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7089 if (NewOp.getNode())
7090 return NewOp;
7091 }
7092
Elena Demikhovsky41789462012-09-06 12:42:01 +00007093 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007094 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007095 if (NewOp.getNode())
7096 return NewOp;
7097 }
7098
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007099 // Handle all 128-bit wide vectors with 4 elements, and match them with
7100 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007101 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007102 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7103
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007104 // Handle general 256-bit shuffles
7105 if (VT.is256BitVector())
7106 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7107
Dan Gohman475871a2008-07-27 21:46:04 +00007108 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007109}
7110
Craig Topperf84b7502013-01-20 00:50:58 +00007111static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007112 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007113 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007114
Craig Topper45e1c752013-01-20 00:38:18 +00007115 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007116 return SDValue();
7117
Duncan Sands83ec4b62008-06-06 12:08:01 +00007118 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007120 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007121 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007122 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007123 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007124 }
7125
7126 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007127 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7128 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7129 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7131 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007132 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007134 Op.getOperand(0)),
7135 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007136 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007137 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007139 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007140 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007141 }
7142
7143 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007144 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7145 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007146 // result has a single use which is a store or a bitcast to i32. And in
7147 // the case of a store, it's not worth it if the index is a constant 0,
7148 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007149 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007150 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007151 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007152 if ((User->getOpcode() != ISD::STORE ||
7153 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7154 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007155 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007156 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007157 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007158 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007159 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007160 Op.getOperand(0)),
7161 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007162 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007163 }
7164
7165 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007166 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007167 if (isa<ConstantSDNode>(Op.getOperand(1)))
7168 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007169 }
Dan Gohman475871a2008-07-27 21:46:04 +00007170 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007171}
7172
Dan Gohman475871a2008-07-27 21:46:04 +00007173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007174X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7175 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007176 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007177 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007178
David Greene74a579d2011-02-10 16:57:36 +00007179 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007180 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007181
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007182 // If this is a 256-bit vector result, first extract the 128-bit vector and
7183 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007184 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007185 DebugLoc dl = Op.getNode()->getDebugLoc();
7186 unsigned NumElems = VecVT.getVectorNumElements();
7187 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007188 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7189
7190 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007191 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007192
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007193 if (IdxVal >= NumElems/2)
7194 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007195 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007196 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007197 }
7198
Craig Topper7a9a28b2012-08-12 02:23:29 +00007199 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007200
Craig Topperd0a31172012-01-10 06:37:29 +00007201 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007203 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007204 return Res;
7205 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007206
Craig Topper45e1c752013-01-20 00:38:18 +00007207 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007208 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007209 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007210 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007211 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007212 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007213 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007214 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7215 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007216 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007217 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007218 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007219 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007220 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007221 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007222 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007223 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007224 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007225 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007226 }
7227
7228 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007229 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007230 if (Idx == 0)
7231 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007232
Evan Cheng0db9fe62006-04-25 20:13:52 +00007233 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007234 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007235 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007236 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007237 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007238 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007239 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007240 }
7241
7242 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7244 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7245 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007246 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007247 if (Idx == 0)
7248 return Op;
7249
7250 // UNPCKHPD the element to the lowest double word, then movsd.
7251 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7252 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007253 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007254 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007255 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007256 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007257 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007258 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007259 }
7260
Dan Gohman475871a2008-07-27 21:46:04 +00007261 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007262}
7263
Craig Topperf84b7502013-01-20 00:50:58 +00007264static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007265 MVT VT = Op.getValueType().getSimpleVT();
7266 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007267 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007268
Dan Gohman475871a2008-07-27 21:46:04 +00007269 SDValue N0 = Op.getOperand(0);
7270 SDValue N1 = Op.getOperand(1);
7271 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007272
Craig Topper7a9a28b2012-08-12 02:23:29 +00007273 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007274 return SDValue();
7275
Dan Gohman8a55ce42009-09-23 21:02:20 +00007276 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007277 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007278 unsigned Opc;
7279 if (VT == MVT::v8i16)
7280 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007281 else if (VT == MVT::v16i8)
7282 Opc = X86ISD::PINSRB;
7283 else
7284 Opc = X86ISD::PINSRB;
7285
Nate Begeman14d12ca2008-02-11 04:19:36 +00007286 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7287 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007288 if (N1.getValueType() != MVT::i32)
7289 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7290 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007291 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007292 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007293 }
7294
7295 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007296 // Bits [7:6] of the constant are the source select. This will always be
7297 // zero here. The DAG Combiner may combine an extract_elt index into these
7298 // bits. For example (insert (extract, 3), 2) could be matched by putting
7299 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007300 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007301 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007302 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007303 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007304 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007305 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007306 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007307 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007308 }
7309
7310 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007311 // PINSR* works with constant index.
7312 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007313 }
Dan Gohman475871a2008-07-27 21:46:04 +00007314 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007315}
7316
Dan Gohman475871a2008-07-27 21:46:04 +00007317SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007318X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007319 MVT VT = Op.getValueType().getSimpleVT();
7320 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007321
David Greene6b381262011-02-09 15:32:06 +00007322 DebugLoc dl = Op.getDebugLoc();
7323 SDValue N0 = Op.getOperand(0);
7324 SDValue N1 = Op.getOperand(1);
7325 SDValue N2 = Op.getOperand(2);
7326
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007327 // If this is a 256-bit vector result, first extract the 128-bit vector,
7328 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007329 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007330 if (!isa<ConstantSDNode>(N2))
7331 return SDValue();
7332
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007333 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007334 unsigned NumElems = VT.getVectorNumElements();
7335 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007336 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007337
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007338 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007339 bool Upper = IdxVal >= NumElems/2;
7340 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7341 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007342
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007343 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007344 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007345 }
7346
Craig Topperd0a31172012-01-10 06:37:29 +00007347 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007348 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7349
Dan Gohman8a55ce42009-09-23 21:02:20 +00007350 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007351 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007352
Dan Gohman8a55ce42009-09-23 21:02:20 +00007353 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007354 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7355 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007356 if (N1.getValueType() != MVT::i32)
7357 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7358 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007359 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007360 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007361 }
Dan Gohman475871a2008-07-27 21:46:04 +00007362 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007363}
7364
Craig Topper55b24052012-09-11 06:15:32 +00007365static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007366 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007367 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007368 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007369
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007370 // If this is a 256-bit vector result, first insert into a 128-bit
7371 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007372 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007373 // Insert into a 128-bit vector.
7374 EVT VT128 = EVT::getVectorVT(*Context,
7375 OpVT.getVectorElementType(),
7376 OpVT.getVectorNumElements() / 2);
7377
7378 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7379
7380 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007381 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007382 }
7383
Craig Topperd77d2fe2012-04-29 20:22:05 +00007384 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007385 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007386 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007387
Owen Anderson825b72b2009-08-11 20:47:22 +00007388 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007389 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007390 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007391 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007392}
7393
David Greene91585092011-01-26 15:38:49 +00007394// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7395// a simple subregister reference or explicit instructions to grab
7396// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007397static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7398 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007399 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007400 DebugLoc dl = Op.getNode()->getDebugLoc();
7401 SDValue Vec = Op.getNode()->getOperand(0);
7402 SDValue Idx = Op.getNode()->getOperand(1);
7403
Craig Topper7a9a28b2012-08-12 02:23:29 +00007404 if (Op.getNode()->getValueType(0).is128BitVector() &&
7405 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007406 isa<ConstantSDNode>(Idx)) {
7407 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7408 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007409 }
David Greene91585092011-01-26 15:38:49 +00007410 }
7411 return SDValue();
7412}
7413
David Greenecfe33c42011-01-26 19:13:22 +00007414// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7415// simple superregister reference or explicit instructions to insert
7416// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007417static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7418 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007419 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007420 DebugLoc dl = Op.getNode()->getDebugLoc();
7421 SDValue Vec = Op.getNode()->getOperand(0);
7422 SDValue SubVec = Op.getNode()->getOperand(1);
7423 SDValue Idx = Op.getNode()->getOperand(2);
7424
Craig Topper7a9a28b2012-08-12 02:23:29 +00007425 if (Op.getNode()->getValueType(0).is256BitVector() &&
7426 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007427 isa<ConstantSDNode>(Idx)) {
7428 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7429 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007430 }
7431 }
7432 return SDValue();
7433}
7434
Bill Wendling056292f2008-09-16 21:48:12 +00007435// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7436// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7437// one of the above mentioned nodes. It has to be wrapped because otherwise
7438// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7439// be used to form addressing mode. These wrapped nodes will be selected
7440// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007441SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007442X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007443 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007444
Chris Lattner41621a22009-06-26 19:22:52 +00007445 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7446 // global base reg.
7447 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007448 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007449 CodeModel::Model M = getTargetMachine().getCodeModel();
7450
Chris Lattner4f066492009-07-11 20:29:19 +00007451 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007452 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007453 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007454 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007455 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007456 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007457 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007458
Evan Cheng1606e8e2009-03-13 07:51:59 +00007459 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007460 CP->getAlignment(),
7461 CP->getOffset(), OpFlag);
7462 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007463 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007464 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007465 if (OpFlag) {
7466 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007467 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007468 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007469 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007470 }
7471
7472 return Result;
7473}
7474
Dan Gohmand858e902010-04-17 15:26:15 +00007475SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007476 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007477
Chris Lattner18c59872009-06-27 04:16:01 +00007478 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7479 // global base reg.
7480 unsigned char OpFlag = 0;
7481 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007482 CodeModel::Model M = getTargetMachine().getCodeModel();
7483
Chris Lattner4f066492009-07-11 20:29:19 +00007484 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007485 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007486 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007487 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007488 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007489 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007490 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007491
Chris Lattner18c59872009-06-27 04:16:01 +00007492 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7493 OpFlag);
7494 DebugLoc DL = JT->getDebugLoc();
7495 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007496
Chris Lattner18c59872009-06-27 04:16:01 +00007497 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007498 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007499 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7500 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007501 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007502 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007503
Chris Lattner18c59872009-06-27 04:16:01 +00007504 return Result;
7505}
7506
7507SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007508X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007509 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007510
Chris Lattner18c59872009-06-27 04:16:01 +00007511 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7512 // global base reg.
7513 unsigned char OpFlag = 0;
7514 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007515 CodeModel::Model M = getTargetMachine().getCodeModel();
7516
Chris Lattner4f066492009-07-11 20:29:19 +00007517 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007518 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7519 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7520 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007521 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007522 } else if (Subtarget->isPICStyleGOT()) {
7523 OpFlag = X86II::MO_GOT;
7524 } else if (Subtarget->isPICStyleStubPIC()) {
7525 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7526 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7527 OpFlag = X86II::MO_DARWIN_NONLAZY;
7528 }
Eric Christopherfd179292009-08-27 18:07:15 +00007529
Chris Lattner18c59872009-06-27 04:16:01 +00007530 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007531
Chris Lattner18c59872009-06-27 04:16:01 +00007532 DebugLoc DL = Op.getDebugLoc();
7533 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007534
Chris Lattner18c59872009-06-27 04:16:01 +00007535 // With PIC, the address is actually $g + Offset.
7536 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007537 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007538 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7539 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007540 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007541 Result);
7542 }
Eric Christopherfd179292009-08-27 18:07:15 +00007543
Eli Friedman586272d2011-08-11 01:48:05 +00007544 // For symbols that require a load from a stub to get the address, emit the
7545 // load.
7546 if (isGlobalStubReference(OpFlag))
7547 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007548 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007549
Chris Lattner18c59872009-06-27 04:16:01 +00007550 return Result;
7551}
7552
Dan Gohman475871a2008-07-27 21:46:04 +00007553SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007554X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007555 // Create the TargetBlockAddressAddress node.
7556 unsigned char OpFlags =
7557 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007558 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007559 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007560 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007561 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007562 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7563 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007564
Dan Gohmanf705adb2009-10-30 01:28:02 +00007565 if (Subtarget->isPICStyleRIPRel() &&
7566 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007567 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7568 else
7569 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007570
Dan Gohman29cbade2009-11-20 23:18:13 +00007571 // With PIC, the address is actually $g + Offset.
7572 if (isGlobalRelativeToPICBase(OpFlags)) {
7573 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7574 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7575 Result);
7576 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007577
7578 return Result;
7579}
7580
7581SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007582X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007583 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007584 // Create the TargetGlobalAddress node, folding in the constant
7585 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007586 unsigned char OpFlags =
7587 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007588 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007589 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007590 if (OpFlags == X86II::MO_NO_FLAG &&
7591 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007592 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007593 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007594 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007595 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007596 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007597 }
Eric Christopherfd179292009-08-27 18:07:15 +00007598
Chris Lattner4f066492009-07-11 20:29:19 +00007599 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007600 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007601 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7602 else
7603 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007604
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007605 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007606 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007607 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7608 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007609 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007610 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007611
Chris Lattner36c25012009-07-10 07:34:39 +00007612 // For globals that require a load from a stub to get the address, emit the
7613 // load.
7614 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007615 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007616 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007617
Dan Gohman6520e202008-10-18 02:06:02 +00007618 // If there was a non-zero offset that we didn't fold, create an explicit
7619 // addition for it.
7620 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007621 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007622 DAG.getConstant(Offset, getPointerTy()));
7623
Evan Cheng0db9fe62006-04-25 20:13:52 +00007624 return Result;
7625}
7626
Evan Chengda43bcf2008-09-24 00:05:32 +00007627SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007628X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007629 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007630 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007631 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007632}
7633
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007634static SDValue
7635GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007636 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007637 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007638 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007639 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007640 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007641 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007642 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007643 GA->getOffset(),
7644 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007645
7646 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7647 : X86ISD::TLSADDR;
7648
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007649 if (InFlag) {
7650 SDValue Ops[] = { Chain, TGA, *InFlag };
Michael Liao0ee17002013-04-19 04:03:37 +00007651 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops));
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007652 } else {
7653 SDValue Ops[] = { Chain, TGA };
Michael Liao0ee17002013-04-19 04:03:37 +00007654 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops));
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007655 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007656
7657 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007658 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007659
Rafael Espindola15f1b662009-04-24 12:59:40 +00007660 SDValue Flag = Chain.getValue(1);
7661 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007662}
7663
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007664// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007665static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007666LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007667 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007668 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007669 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7670 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007671 DAG.getNode(X86ISD::GlobalBaseReg,
7672 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007673 InFlag = Chain.getValue(1);
7674
Chris Lattnerb903bed2009-06-26 21:20:29 +00007675 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007676}
7677
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007678// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007679static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007680LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007681 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007682 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7683 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007684}
7685
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007686static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7687 SelectionDAG &DAG,
7688 const EVT PtrVT,
7689 bool is64Bit) {
7690 DebugLoc dl = GA->getDebugLoc();
7691
7692 // Get the start address of the TLS block for this module.
7693 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7694 .getInfo<X86MachineFunctionInfo>();
7695 MFI->incNumLocalDynamicTLSAccesses();
7696
7697 SDValue Base;
7698 if (is64Bit) {
7699 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7700 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7701 } else {
7702 SDValue InFlag;
7703 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7704 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7705 InFlag = Chain.getValue(1);
7706 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7707 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7708 }
7709
7710 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7711 // of Base.
7712
7713 // Build x@dtpoff.
7714 unsigned char OperandFlags = X86II::MO_DTPOFF;
7715 unsigned WrapperKind = X86ISD::Wrapper;
7716 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7717 GA->getValueType(0),
7718 GA->getOffset(), OperandFlags);
7719 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7720
7721 // Add x@dtpoff with the base.
7722 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7723}
7724
Hans Wennborg228756c2012-05-11 10:11:01 +00007725// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007726static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007727 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007728 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007729 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007730
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007731 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7732 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7733 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007734
Michael J. Spencerec38de22010-10-10 22:04:20 +00007735 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007736 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007737 MachinePointerInfo(Ptr),
7738 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007739
Chris Lattnerb903bed2009-06-26 21:20:29 +00007740 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007741 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7742 // initialexec.
7743 unsigned WrapperKind = X86ISD::Wrapper;
7744 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007745 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007746 } else if (model == TLSModel::InitialExec) {
7747 if (is64Bit) {
7748 OperandFlags = X86II::MO_GOTTPOFF;
7749 WrapperKind = X86ISD::WrapperRIP;
7750 } else {
7751 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7752 }
Chris Lattner18c59872009-06-27 04:16:01 +00007753 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007754 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007755 }
Eric Christopherfd179292009-08-27 18:07:15 +00007756
Hans Wennborg228756c2012-05-11 10:11:01 +00007757 // emit "addl x@ntpoff,%eax" (local exec)
7758 // or "addl x@indntpoff,%eax" (initial exec)
7759 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007760 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007761 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007762 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007763 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007764
Hans Wennborg228756c2012-05-11 10:11:01 +00007765 if (model == TLSModel::InitialExec) {
7766 if (isPIC && !is64Bit) {
7767 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7768 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7769 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007770 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007771
7772 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7773 MachinePointerInfo::getGOT(), false, false, false,
7774 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007775 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007776
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007777 // The address of the thread local variable is the add of the thread
7778 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007779 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007780}
7781
Dan Gohman475871a2008-07-27 21:46:04 +00007782SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007783X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007784
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007785 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007786 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007787
Eric Christopher30ef0e52010-06-03 04:07:48 +00007788 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007789 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007790
Eric Christopher30ef0e52010-06-03 04:07:48 +00007791 switch (model) {
7792 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007793 if (Subtarget->is64Bit())
7794 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7795 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007796 case TLSModel::LocalDynamic:
7797 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7798 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007799 case TLSModel::InitialExec:
7800 case TLSModel::LocalExec:
7801 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007802 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007803 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007804 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007805 llvm_unreachable("Unknown TLS model.");
7806 }
7807
7808 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007809 // Darwin only has one model of TLS. Lower to that.
7810 unsigned char OpFlag = 0;
7811 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7812 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007813
Eric Christopher30ef0e52010-06-03 04:07:48 +00007814 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7815 // global base reg.
7816 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7817 !Subtarget->is64Bit();
7818 if (PIC32)
7819 OpFlag = X86II::MO_TLVP_PIC_BASE;
7820 else
7821 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007822 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007823 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007824 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007825 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007826 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007827
Eric Christopher30ef0e52010-06-03 04:07:48 +00007828 // With PIC32, the address is actually $g + Offset.
7829 if (PIC32)
7830 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7831 DAG.getNode(X86ISD::GlobalBaseReg,
7832 DebugLoc(), getPointerTy()),
7833 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007834
Eric Christopher30ef0e52010-06-03 04:07:48 +00007835 // Lowering the machine isd will make sure everything is in the right
7836 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007837 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007838 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007839 SDValue Args[] = { Chain, Offset };
7840 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007841
Eric Christopher30ef0e52010-06-03 04:07:48 +00007842 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7843 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7844 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007845
Eric Christopher30ef0e52010-06-03 04:07:48 +00007846 // And our return value (tls address) is in the standard call return value
7847 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007848 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007849 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7850 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007851 }
7852
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007853 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007854 // Just use the implicit TLS architecture
7855 // Need to generate someting similar to:
7856 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7857 // ; from TEB
7858 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7859 // mov rcx, qword [rdx+rcx*8]
7860 // mov eax, .tls$:tlsvar
7861 // [rax+rcx] contains the address
7862 // Windows 64bit: gs:0x58
7863 // Windows 32bit: fs:__tls_array
7864
7865 // If GV is an alias then use the aliasee for determining
7866 // thread-localness.
7867 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7868 GV = GA->resolveAliasedGlobal(false);
7869 DebugLoc dl = GA->getDebugLoc();
7870 SDValue Chain = DAG.getEntryNode();
7871
7872 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007873 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
7874 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007875 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7876 ? Type::getInt8PtrTy(*DAG.getContext(),
7877 256)
7878 : Type::getInt32PtrTy(*DAG.getContext(),
7879 257));
7880
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007881 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
7882 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
7883 DAG.getExternalSymbol("_tls_array", getPointerTy()));
7884
7885 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007886 MachinePointerInfo(Ptr),
7887 false, false, false, 0);
7888
7889 // Load the _tls_index variable
7890 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7891 if (Subtarget->is64Bit())
7892 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7893 IDX, MachinePointerInfo(), MVT::i32,
7894 false, false, 0);
7895 else
7896 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7897 false, false, false, 0);
7898
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007899 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007900 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007901 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7902
7903 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7904 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7905 false, false, false, 0);
7906
7907 // Get the offset of start of .tls section
7908 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7909 GA->getValueType(0),
7910 GA->getOffset(), X86II::MO_SECREL);
7911 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7912
7913 // The address of the thread local variable is the add of the thread
7914 // pointer with the offset of the variable.
7915 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007916 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007917
David Blaikie4d6ccb52012-01-20 21:51:11 +00007918 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007919}
7920
Chad Rosierb90d2a92012-01-03 23:19:12 +00007921/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7922/// and take a 2 x i32 value to shift plus a shift amount.
7923SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007924 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007925 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007926 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007927 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007928 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007929 SDValue ShOpLo = Op.getOperand(0);
7930 SDValue ShOpHi = Op.getOperand(1);
7931 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007932 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007934 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007935
Dan Gohman475871a2008-07-27 21:46:04 +00007936 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007937 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007938 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7939 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007940 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007941 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7942 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007943 }
Evan Chenge3413162006-01-09 18:33:28 +00007944
Owen Anderson825b72b2009-08-11 20:47:22 +00007945 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7946 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007947 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007948 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007949
Dan Gohman475871a2008-07-27 21:46:04 +00007950 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007952 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7953 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007954
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007955 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007956 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7957 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007958 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007959 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7960 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007961 }
7962
Dan Gohman475871a2008-07-27 21:46:04 +00007963 SDValue Ops[2] = { Lo, Hi };
Michael Liao0ee17002013-04-19 04:03:37 +00007964 return DAG.getMergeValues(Ops, array_lengthof(Ops), dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007965}
Evan Chenga3195e82006-01-12 22:54:21 +00007966
Dan Gohmand858e902010-04-17 15:26:15 +00007967SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7968 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007969 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007970
Dale Johannesen0488fb62010-09-30 23:57:10 +00007971 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007972 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007973
Owen Anderson825b72b2009-08-11 20:47:22 +00007974 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007975 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007976
Eli Friedman36df4992009-05-27 00:47:34 +00007977 // These are really Legal; return the operand so the caller accepts it as
7978 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007979 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007980 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007981 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007982 Subtarget->is64Bit()) {
7983 return Op;
7984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007985
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007986 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007987 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007988 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007989 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007990 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007991 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007992 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007993 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007994 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007995 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7996}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007997
Owen Andersone50ed302009-08-10 22:56:29 +00007998SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007999 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00008000 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008001 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00008002 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00008003 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00008004 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008005 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008006 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00008007 else
Owen Anderson825b72b2009-08-11 20:47:22 +00008008 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008009
Chris Lattner492a43e2010-09-22 01:28:21 +00008010 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00008011
Stuart Hastings84be9582011-06-02 15:57:11 +00008012 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
8013 MachineMemOperand *MMO;
8014 if (FI) {
8015 int SSFI = FI->getIndex();
8016 MMO =
8017 DAG.getMachineFunction()
8018 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8019 MachineMemOperand::MOLoad, ByteSize, ByteSize);
8020 } else {
8021 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
8022 StackSlot = StackSlot.getOperand(1);
8023 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008024 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008025 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
8026 X86ISD::FILD, DL,
8027 Tys, Ops, array_lengthof(Ops),
8028 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008029
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008030 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008031 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008032 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008033
8034 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8035 // shouldn't be necessary except that RFP cannot be live across
8036 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008037 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008038 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8039 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008040 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008041 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008042 SDValue Ops[] = {
8043 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8044 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008045 MachineMemOperand *MMO =
8046 DAG.getMachineFunction()
8047 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008048 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008049
Chris Lattner492a43e2010-09-22 01:28:21 +00008050 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8051 Ops, array_lengthof(Ops),
8052 Op.getValueType(), MMO);
8053 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008054 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008055 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008056 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008057
Evan Cheng0db9fe62006-04-25 20:13:52 +00008058 return Result;
8059}
8060
Bill Wendling8b8a6362009-01-17 03:56:04 +00008061// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008062SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8063 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008064 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008065 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008066 movq %rax, %xmm0
8067 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8068 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8069 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008070 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008071 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008072 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008073 addpd %xmm1, %xmm0
8074 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008075 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008076
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008077 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008078 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008079
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008080 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008081 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8082 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008083 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008084
Chris Lattner97484792012-01-25 09:56:22 +00008085 SmallVector<Constant*,2> CV1;
8086 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008087 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8088 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008089 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008090 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8091 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008092 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008093 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008094
Bill Wendling397ae212012-01-05 02:13:20 +00008095 // Load the 64-bit value into an XMM register.
8096 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8097 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008098 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008099 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008100 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008101 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8102 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8103 CLod0);
8104
Owen Anderson825b72b2009-08-11 20:47:22 +00008105 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008106 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008107 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008108 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008109 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008110 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008111
Craig Topperd0a31172012-01-10 06:37:29 +00008112 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008113 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8114 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8115 } else {
8116 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8117 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8118 S2F, 0x4E, DAG);
8119 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8120 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8121 Sub);
8122 }
8123
8124 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008125 DAG.getIntPtrConstant(0));
8126}
8127
Bill Wendling8b8a6362009-01-17 03:56:04 +00008128// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008129SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8130 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008131 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008132 // FP constant to bias correct the final result.
8133 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008134 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008135
8136 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008137 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008138 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008139
Eli Friedmanf3704762011-08-29 21:15:46 +00008140 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008141 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008142
Owen Anderson825b72b2009-08-11 20:47:22 +00008143 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008144 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008145 DAG.getIntPtrConstant(0));
8146
8147 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008148 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008149 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008150 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008151 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008152 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008153 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008154 MVT::v2f64, Bias)));
8155 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008156 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008157 DAG.getIntPtrConstant(0));
8158
8159 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008160 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008161
8162 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008163 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008164
Craig Topper69947b92012-04-23 06:57:04 +00008165 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008166 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008167 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008168 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008169 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008170
8171 // Handle final rounding.
8172 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008173}
8174
Michael Liaoa7554632012-10-23 17:36:08 +00008175SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8176 SelectionDAG &DAG) const {
8177 SDValue N0 = Op.getOperand(0);
8178 EVT SVT = N0.getValueType();
8179 DebugLoc dl = Op.getDebugLoc();
8180
8181 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8182 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8183 "Custom UINT_TO_FP is not supported!");
8184
Craig Topperb99bafe2013-01-21 06:21:54 +00008185 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8186 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008187 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8188 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8189}
8190
Dan Gohmand858e902010-04-17 15:26:15 +00008191SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8192 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008193 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008194 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008195
Michael Liaoa7554632012-10-23 17:36:08 +00008196 if (Op.getValueType().isVector())
8197 return lowerUINT_TO_FP_vec(Op, DAG);
8198
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008199 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008200 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8201 // the optimization here.
8202 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008203 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008204
Owen Andersone50ed302009-08-10 22:56:29 +00008205 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008206 EVT DstVT = Op.getValueType();
8207 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008208 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008209 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008210 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008211 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008212 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008213
8214 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008215 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008216 if (SrcVT == MVT::i32) {
8217 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8218 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8219 getPointerTy(), StackSlot, WordOff);
8220 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008221 StackSlot, MachinePointerInfo(),
8222 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008223 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008224 OffsetSlot, MachinePointerInfo(),
8225 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008226 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8227 return Fild;
8228 }
8229
8230 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8231 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008232 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008233 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008234 // For i64 source, we need to add the appropriate power of 2 if the input
8235 // was negative. This is the same as the optimization in
8236 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8237 // we must be careful to do the computation in x87 extended precision, not
8238 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008239 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8240 MachineMemOperand *MMO =
8241 DAG.getMachineFunction()
8242 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8243 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008244
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008245 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8246 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Michael Liao0ee17002013-04-19 04:03:37 +00008247 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops,
8248 array_lengthof(Ops), MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008249
8250 APInt FF(32, 0x5F800000ULL);
8251
8252 // Check whether the sign bit is set.
8253 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8254 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8255 ISD::SETLT);
8256
8257 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8258 SDValue FudgePtr = DAG.getConstantPool(
8259 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8260 getPointerTy());
8261
8262 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8263 SDValue Zero = DAG.getIntPtrConstant(0);
8264 SDValue Four = DAG.getIntPtrConstant(4);
8265 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8266 Zero, Four);
8267 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8268
8269 // Load the value out, extending it from f32 to f80.
8270 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008271 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008272 FudgePtr, MachinePointerInfo::getConstantPool(),
8273 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008274 // Extend everything to 80 bits to force it to be done on x87.
8275 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8276 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008277}
8278
Craig Topperb99bafe2013-01-21 06:21:54 +00008279std::pair<SDValue,SDValue>
8280X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8281 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008282 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008283
Owen Andersone50ed302009-08-10 22:56:29 +00008284 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008285
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008286 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008287 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8288 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008289 }
8290
Owen Anderson825b72b2009-08-11 20:47:22 +00008291 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8292 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008293 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008294
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008295 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008296 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008297 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008298 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008299 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008300 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008301 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008302 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008303
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008304 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8305 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008306 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008307 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008308 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008309 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008310
Evan Cheng0db9fe62006-04-25 20:13:52 +00008311 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008312 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8313 Opc = X86ISD::WIN_FTOL;
8314 else
8315 switch (DstTy.getSimpleVT().SimpleTy) {
8316 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8317 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8318 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8319 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008321
Dan Gohman475871a2008-07-27 21:46:04 +00008322 SDValue Chain = DAG.getEntryNode();
8323 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008324 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008325 // FIXME This causes a redundant load/store if the SSE-class value is already
8326 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008327 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008328 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008329 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008330 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008331 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008332 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008333 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008334 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008335 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008336
Chris Lattner492a43e2010-09-22 01:28:21 +00008337 MachineMemOperand *MMO =
8338 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8339 MachineMemOperand::MOLoad, MemSize, MemSize);
Michael Liao0ee17002013-04-19 04:03:37 +00008340 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops,
8341 array_lengthof(Ops), DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008342 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008343 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008344 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8345 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008346
Chris Lattner07290932010-09-22 01:05:16 +00008347 MachineMemOperand *MMO =
8348 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8349 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008350
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008351 if (Opc != X86ISD::WIN_FTOL) {
8352 // Build the FP_TO_INT*_IN_MEM
8353 SDValue Ops[] = { Chain, Value, StackSlot };
8354 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
Michael Liao0ee17002013-04-19 04:03:37 +00008355 Ops, array_lengthof(Ops), DstTy,
8356 MMO);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008357 return std::make_pair(FIST, StackSlot);
8358 } else {
8359 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8360 DAG.getVTList(MVT::Other, MVT::Glue),
8361 Chain, Value);
8362 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8363 MVT::i32, ftol.getValue(1));
8364 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8365 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008366 SDValue Ops[] = { eax, edx };
8367 SDValue pair = IsReplace
Michael Liao0ee17002013-04-19 04:03:37 +00008368 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, array_lengthof(Ops))
8369 : DAG.getMergeValues(Ops, array_lengthof(Ops), DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008370 return std::make_pair(pair, SDValue());
8371 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008372}
8373
Nadav Rotem0509db22012-12-28 05:45:24 +00008374static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8375 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008376 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008377 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008378 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008379 DebugLoc dl = Op->getDebugLoc();
8380
8381 // Optimize vectors in AVX mode:
8382 //
8383 // v8i16 -> v8i32
8384 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8385 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8386 // Concat upper and lower parts.
8387 //
8388 // v4i32 -> v4i64
8389 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8390 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8391 // Concat upper and lower parts.
8392 //
8393
8394 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8395 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8396 return SDValue();
8397
8398 if (Subtarget->hasInt256())
8399 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8400
8401 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8402 SDValue Undef = DAG.getUNDEF(InVT);
8403 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8404 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8405 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8406
Craig Toppera080daf2013-01-20 21:50:27 +00008407 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008408 VT.getVectorNumElements()/2);
8409
8410 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8411 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8412
8413 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8414}
8415
8416SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8417 SelectionDAG &DAG) const {
8418 if (Subtarget->hasFp256()) {
8419 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8420 if (Res.getNode())
8421 return Res;
8422 }
8423
8424 return SDValue();
8425}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008426SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8427 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008428 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008429 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008430 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008431 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008432
Nadav Rotem0509db22012-12-28 05:45:24 +00008433 if (Subtarget->hasFp256()) {
8434 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8435 if (Res.getNode())
8436 return Res;
8437 }
8438
Michael Liaoa7554632012-10-23 17:36:08 +00008439 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8440 VT.getVectorNumElements() != SVT.getVectorNumElements())
8441 return SDValue();
8442
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008443 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008444
8445 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008446 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008447 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8448
8449 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8450 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8451 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008452 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8453 DAG.getUNDEF(MVT::v8i16),
8454 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008455
8456 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8457}
8458
Craig Topperd713c0f2013-01-20 21:34:37 +00008459SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008460 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008461 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008462 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008463 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008464
Nadav Rotem3c22a442012-12-27 07:45:10 +00008465 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8466 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8467 if (Subtarget->hasInt256()) {
8468 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8469 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8470 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8471 ShufMask);
8472 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8473 DAG.getIntPtrConstant(0));
8474 }
8475
8476 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8477 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8478 DAG.getIntPtrConstant(0));
8479 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8480 DAG.getIntPtrConstant(2));
8481
8482 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8483 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8484
8485 // The PSHUFD mask:
8486 static const int ShufMask1[] = {0, 2, 0, 0};
8487 SDValue Undef = DAG.getUNDEF(VT);
8488 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8489 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8490
8491 // The MOVLHPS mask:
8492 static const int ShufMask2[] = {0, 1, 4, 5};
8493 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8494 }
8495
8496 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8497 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8498 if (Subtarget->hasInt256()) {
8499 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8500
8501 SmallVector<SDValue,32> pshufbMask;
8502 for (unsigned i = 0; i < 2; ++i) {
8503 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8504 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8505 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8506 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8507 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8508 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8509 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8510 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8511 for (unsigned j = 0; j < 8; ++j)
8512 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8513 }
8514 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8515 &pshufbMask[0], 32);
8516 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8517 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8518
8519 static const int ShufMask[] = {0, 2, -1, -1};
8520 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8521 &ShufMask[0]);
8522 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8523 DAG.getIntPtrConstant(0));
8524 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8525 }
8526
8527 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8528 DAG.getIntPtrConstant(0));
8529
8530 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8531 DAG.getIntPtrConstant(4));
8532
8533 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8534 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8535
8536 // The PSHUFB mask:
8537 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8538 -1, -1, -1, -1, -1, -1, -1, -1};
8539
8540 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8541 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8542 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8543
8544 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8545 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8546
8547 // The MOVLHPS Mask:
8548 static const int ShufMask2[] = {0, 1, 4, 5};
8549 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8550 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8551 }
8552
8553 // Handle truncation of V256 to V128 using shuffles.
8554 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008555 return SDValue();
8556
Nadav Rotem3c22a442012-12-27 07:45:10 +00008557 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8558 "Invalid op");
8559 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008560
8561 unsigned NumElems = VT.getVectorNumElements();
8562 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8563 NumElems * 2);
8564
Michael Liaobedcbd42012-10-16 18:14:11 +00008565 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8566 // Prepare truncation shuffle mask
8567 for (unsigned i = 0; i != NumElems; ++i)
8568 MaskVec[i] = i * 2;
8569 SDValue V = DAG.getVectorShuffle(NVT, DL,
8570 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8571 DAG.getUNDEF(NVT), &MaskVec[0]);
8572 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8573 DAG.getIntPtrConstant(0));
8574}
8575
Dan Gohmand858e902010-04-17 15:26:15 +00008576SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8577 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008578 MVT VT = Op.getValueType().getSimpleVT();
8579 if (VT.isVector()) {
8580 if (VT == MVT::v8i16)
8581 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008582 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8583 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008584 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008585 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008586
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008587 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8588 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008589 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008590 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8591 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008592
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008593 if (StackSlot.getNode())
8594 // Load the result.
8595 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8596 FIST, StackSlot, MachinePointerInfo(),
8597 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008598
8599 // The node is the result.
8600 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008601}
8602
Dan Gohmand858e902010-04-17 15:26:15 +00008603SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8604 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008605 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8606 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008607 SDValue FIST = Vals.first, StackSlot = Vals.second;
8608 assert(FIST.getNode() && "Unexpected failure");
8609
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008610 if (StackSlot.getNode())
8611 // Load the result.
8612 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8613 FIST, StackSlot, MachinePointerInfo(),
8614 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008615
8616 // The node is the result.
8617 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008618}
8619
Craig Topperb84b4232013-01-21 06:13:28 +00008620static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008621 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008622 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008623 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008624 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008625
8626 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8627
8628 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8629 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8630 In, DAG.getUNDEF(SVT)));
8631}
8632
Craig Topper43620672012-09-08 07:31:51 +00008633SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008634 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008635 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008636 MVT VT = Op.getValueType().getSimpleVT();
8637 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008638 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8639 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008640 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008641 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008642 }
Craig Topper43620672012-09-08 07:31:51 +00008643 Constant *C;
8644 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008645 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8646 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008647 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008648 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8649 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008650 C = ConstantVector::getSplat(NumElts, C);
8651 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8652 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008653 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008654 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008655 false, false, false, Alignment);
8656 if (VT.isVector()) {
8657 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8658 return DAG.getNode(ISD::BITCAST, dl, VT,
8659 DAG.getNode(ISD::AND, dl, ANDVT,
8660 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8661 Op.getOperand(0)),
8662 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8663 }
Dale Johannesenace16102009-02-03 19:33:06 +00008664 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008665}
8666
Dan Gohmand858e902010-04-17 15:26:15 +00008667SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008668 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008669 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008670 MVT VT = Op.getValueType().getSimpleVT();
8671 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008672 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8673 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008674 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008675 NumElts = VT.getVectorNumElements();
8676 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008677 Constant *C;
8678 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008679 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8680 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008681 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008682 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8683 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008684 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008685 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8686 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008687 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008688 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008689 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008690 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008691 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008692 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008693 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008694 DAG.getNode(ISD::BITCAST, dl, XORVT,
8695 Op.getOperand(0)),
8696 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008697 }
Craig Topper69947b92012-04-23 06:57:04 +00008698
8699 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008700}
8701
Dan Gohmand858e902010-04-17 15:26:15 +00008702SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008703 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008704 SDValue Op0 = Op.getOperand(0);
8705 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008706 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008707 MVT VT = Op.getValueType().getSimpleVT();
8708 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008709
8710 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008711 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008712 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008713 SrcVT = VT;
8714 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008715 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008716 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008717 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008718 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008719 }
8720
8721 // At this point the operands and the result should have the same
8722 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008723
Evan Cheng68c47cb2007-01-05 07:55:56 +00008724 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008725 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008726 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008727 const fltSemantics &Sem = APFloat::IEEEdouble;
8728 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8729 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008730 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008731 const fltSemantics &Sem = APFloat::IEEEsingle;
8732 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8733 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8734 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8735 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008736 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008737 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008738 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008739 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008740 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008741 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008742 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008743
8744 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008745 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008746 // Op0 is MVT::f32, Op1 is MVT::f64.
8747 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8748 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8749 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008750 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008751 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008752 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008753 }
8754
Evan Cheng73d6cf12007-01-05 21:37:56 +00008755 // Clear first operand sign bit.
8756 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008757 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008758 const fltSemantics &Sem = APFloat::IEEEdouble;
8759 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8760 APInt(64, ~(1ULL << 63)))));
8761 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008762 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008763 const fltSemantics &Sem = APFloat::IEEEsingle;
8764 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8765 APInt(32, ~(1U << 31)))));
8766 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8767 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8768 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008769 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008770 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008771 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008772 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008773 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008774 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008775 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008776
8777 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008778 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008779}
8780
Craig Topper55b24052012-09-11 06:15:32 +00008781static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008782 SDValue N0 = Op.getOperand(0);
8783 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008784 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008785
8786 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8787 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8788 DAG.getConstant(1, VT));
8789 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8790}
8791
Michael Liaof966e4e2012-09-13 20:24:54 +00008792// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8793//
Craig Topperb99bafe2013-01-21 06:21:54 +00008794SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8795 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008796 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8797
8798 if (!Subtarget->hasSSE41())
8799 return SDValue();
8800
8801 if (!Op->hasOneUse())
8802 return SDValue();
8803
8804 SDNode *N = Op.getNode();
8805 DebugLoc DL = N->getDebugLoc();
8806
8807 SmallVector<SDValue, 8> Opnds;
8808 DenseMap<SDValue, unsigned> VecInMap;
8809 EVT VT = MVT::Other;
8810
8811 // Recognize a special case where a vector is casted into wide integer to
8812 // test all 0s.
8813 Opnds.push_back(N->getOperand(0));
8814 Opnds.push_back(N->getOperand(1));
8815
8816 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8817 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8818 // BFS traverse all OR'd operands.
8819 if (I->getOpcode() == ISD::OR) {
8820 Opnds.push_back(I->getOperand(0));
8821 Opnds.push_back(I->getOperand(1));
8822 // Re-evaluate the number of nodes to be traversed.
8823 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8824 continue;
8825 }
8826
8827 // Quit if a non-EXTRACT_VECTOR_ELT
8828 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8829 return SDValue();
8830
8831 // Quit if without a constant index.
8832 SDValue Idx = I->getOperand(1);
8833 if (!isa<ConstantSDNode>(Idx))
8834 return SDValue();
8835
8836 SDValue ExtractedFromVec = I->getOperand(0);
8837 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8838 if (M == VecInMap.end()) {
8839 VT = ExtractedFromVec.getValueType();
8840 // Quit if not 128/256-bit vector.
8841 if (!VT.is128BitVector() && !VT.is256BitVector())
8842 return SDValue();
8843 // Quit if not the same type.
8844 if (VecInMap.begin() != VecInMap.end() &&
8845 VT != VecInMap.begin()->first.getValueType())
8846 return SDValue();
8847 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8848 }
8849 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8850 }
8851
8852 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008853 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008854
8855 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8856 SmallVector<SDValue, 8> VecIns;
8857
8858 for (DenseMap<SDValue, unsigned>::const_iterator
8859 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8860 // Quit if not all elements are used.
8861 if (I->second != FullMask)
8862 return SDValue();
8863 VecIns.push_back(I->first);
8864 }
8865
8866 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8867
8868 // Cast all vectors into TestVT for PTEST.
8869 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8870 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8871
8872 // If more than one full vectors are evaluated, OR them first before PTEST.
8873 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8874 // Each iteration will OR 2 nodes and append the result until there is only
8875 // 1 node left, i.e. the final OR'd value of all vectors.
8876 SDValue LHS = VecIns[Slot];
8877 SDValue RHS = VecIns[Slot + 1];
8878 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8879 }
8880
8881 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8882 VecIns.back(), VecIns.back());
8883}
8884
Dan Gohman076aee32009-03-04 19:44:21 +00008885/// Emit nodes that will be selected as "test Op0,Op0", or something
8886/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008887SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008888 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008889 DebugLoc dl = Op.getDebugLoc();
8890
Dan Gohman31125812009-03-07 01:58:32 +00008891 // CF and OF aren't always set the way we want. Determine which
8892 // of these we need.
8893 bool NeedCF = false;
8894 bool NeedOF = false;
8895 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008896 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008897 case X86::COND_A: case X86::COND_AE:
8898 case X86::COND_B: case X86::COND_BE:
8899 NeedCF = true;
8900 break;
8901 case X86::COND_G: case X86::COND_GE:
8902 case X86::COND_L: case X86::COND_LE:
8903 case X86::COND_O: case X86::COND_NO:
8904 NeedOF = true;
8905 break;
Dan Gohman31125812009-03-07 01:58:32 +00008906 }
8907
Dan Gohman076aee32009-03-04 19:44:21 +00008908 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008909 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8910 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008911 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8912 // Emit a CMP with 0, which is the TEST pattern.
8913 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8914 DAG.getConstant(0, Op.getValueType()));
8915
8916 unsigned Opcode = 0;
8917 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008918
8919 // Truncate operations may prevent the merge of the SETCC instruction
8920 // and the arithmetic intruction before it. Attempt to truncate the operands
8921 // of the arithmetic instruction and use a reduced bit-width instruction.
8922 bool NeedTruncation = false;
8923 SDValue ArithOp = Op;
8924 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8925 SDValue Arith = Op->getOperand(0);
8926 // Both the trunc and the arithmetic op need to have one user each.
8927 if (Arith->hasOneUse())
8928 switch (Arith.getOpcode()) {
8929 default: break;
8930 case ISD::ADD:
8931 case ISD::SUB:
8932 case ISD::AND:
8933 case ISD::OR:
8934 case ISD::XOR: {
8935 NeedTruncation = true;
8936 ArithOp = Arith;
8937 }
8938 }
8939 }
8940
8941 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8942 // which may be the result of a CAST. We use the variable 'Op', which is the
8943 // non-casted variable when we check for possible users.
8944 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008945 case ISD::ADD:
8946 // Due to an isel shortcoming, be conservative if this add is likely to be
8947 // selected as part of a load-modify-store instruction. When the root node
8948 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8949 // uses of other nodes in the match, such as the ADD in this case. This
8950 // leads to the ADD being left around and reselected, with the result being
8951 // two adds in the output. Alas, even if none our users are stores, that
8952 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8953 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8954 // climbing the DAG back to the root, and it doesn't seem to be worth the
8955 // effort.
8956 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008957 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8958 if (UI->getOpcode() != ISD::CopyToReg &&
8959 UI->getOpcode() != ISD::SETCC &&
8960 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008961 goto default_case;
8962
8963 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008964 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008965 // An add of one will be selected as an INC.
8966 if (C->getAPIntValue() == 1) {
8967 Opcode = X86ISD::INC;
8968 NumOperands = 1;
8969 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008970 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008971
8972 // An add of negative one (subtract of one) will be selected as a DEC.
8973 if (C->getAPIntValue().isAllOnesValue()) {
8974 Opcode = X86ISD::DEC;
8975 NumOperands = 1;
8976 break;
8977 }
Dan Gohman076aee32009-03-04 19:44:21 +00008978 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008979
8980 // Otherwise use a regular EFLAGS-setting add.
8981 Opcode = X86ISD::ADD;
8982 NumOperands = 2;
8983 break;
8984 case ISD::AND: {
8985 // If the primary and result isn't used, don't bother using X86ISD::AND,
8986 // because a TEST instruction will be better.
8987 bool NonFlagUse = false;
8988 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8989 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8990 SDNode *User = *UI;
8991 unsigned UOpNo = UI.getOperandNo();
8992 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8993 // Look pass truncate.
8994 UOpNo = User->use_begin().getOperandNo();
8995 User = *User->use_begin();
8996 }
8997
8998 if (User->getOpcode() != ISD::BRCOND &&
8999 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009000 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009001 NonFlagUse = true;
9002 break;
9003 }
Dan Gohman076aee32009-03-04 19:44:21 +00009004 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009005
9006 if (!NonFlagUse)
9007 break;
9008 }
9009 // FALL THROUGH
9010 case ISD::SUB:
9011 case ISD::OR:
9012 case ISD::XOR:
9013 // Due to the ISEL shortcoming noted above, be conservative if this op is
9014 // likely to be selected as part of a load-modify-store instruction.
9015 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9016 UE = Op.getNode()->use_end(); UI != UE; ++UI)
9017 if (UI->getOpcode() == ISD::STORE)
9018 goto default_case;
9019
9020 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009021 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009022 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009023 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009024 case ISD::XOR: Opcode = X86ISD::XOR; break;
9025 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00009026 case ISD::OR: {
9027 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
9028 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9029 if (EFLAGS.getNode())
9030 return EFLAGS;
9031 }
9032 Opcode = X86ISD::OR;
9033 break;
9034 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009035 }
9036
9037 NumOperands = 2;
9038 break;
9039 case X86ISD::ADD:
9040 case X86ISD::SUB:
9041 case X86ISD::INC:
9042 case X86ISD::DEC:
9043 case X86ISD::OR:
9044 case X86ISD::XOR:
9045 case X86ISD::AND:
9046 return SDValue(Op.getNode(), 1);
9047 default:
9048 default_case:
9049 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009050 }
9051
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009052 // If we found that truncation is beneficial, perform the truncation and
9053 // update 'Op'.
9054 if (NeedTruncation) {
9055 EVT VT = Op.getValueType();
9056 SDValue WideVal = Op->getOperand(0);
9057 EVT WideVT = WideVal.getValueType();
9058 unsigned ConvertedOp = 0;
9059 // Use a target machine opcode to prevent further DAGCombine
9060 // optimizations that may separate the arithmetic operations
9061 // from the setcc node.
9062 switch (WideVal.getOpcode()) {
9063 default: break;
9064 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9065 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9066 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9067 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9068 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9069 }
9070
9071 if (ConvertedOp) {
9072 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9073 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9074 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9075 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9076 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9077 }
9078 }
9079 }
9080
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009081 if (Opcode == 0)
9082 // Emit a CMP with 0, which is the TEST pattern.
9083 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9084 DAG.getConstant(0, Op.getValueType()));
9085
9086 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9087 SmallVector<SDValue, 4> Ops;
9088 for (unsigned i = 0; i != NumOperands; ++i)
9089 Ops.push_back(Op.getOperand(i));
9090
9091 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9092 DAG.ReplaceAllUsesWith(Op, New);
9093 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009094}
9095
9096/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9097/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009098SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009099 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009100 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9101 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009102 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009103
9104 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009105 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9106 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9107 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9108 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9109 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9110 Op0, Op1);
9111 return SDValue(Sub.getNode(), 1);
9112 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009113 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009114}
9115
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009116/// Convert a comparison if required by the subtarget.
9117SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9118 SelectionDAG &DAG) const {
9119 // If the subtarget does not support the FUCOMI instruction, floating-point
9120 // comparisons have to be converted.
9121 if (Subtarget->hasCMov() ||
9122 Cmp.getOpcode() != X86ISD::CMP ||
9123 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9124 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9125 return Cmp;
9126
9127 // The instruction selector will select an FUCOM instruction instead of
9128 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9129 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9130 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9131 DebugLoc dl = Cmp.getDebugLoc();
9132 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9133 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9134 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9135 DAG.getConstant(8, MVT::i8));
9136 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9137 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9138}
9139
Evan Cheng4e544802012-12-05 00:10:38 +00009140static bool isAllOnes(SDValue V) {
9141 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9142 return C && C->isAllOnesValue();
9143}
9144
Evan Chengd40d03e2010-01-06 19:38:29 +00009145/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9146/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009147SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9148 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009149 SDValue Op0 = And.getOperand(0);
9150 SDValue Op1 = And.getOperand(1);
9151 if (Op0.getOpcode() == ISD::TRUNCATE)
9152 Op0 = Op0.getOperand(0);
9153 if (Op1.getOpcode() == ISD::TRUNCATE)
9154 Op1 = Op1.getOperand(0);
9155
Evan Chengd40d03e2010-01-06 19:38:29 +00009156 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009157 if (Op1.getOpcode() == ISD::SHL)
9158 std::swap(Op0, Op1);
9159 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009160 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9161 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009162 // If we looked past a truncate, check that it's only truncating away
9163 // known zeros.
9164 unsigned BitWidth = Op0.getValueSizeInBits();
9165 unsigned AndBitWidth = And.getValueSizeInBits();
9166 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009167 APInt Zeros, Ones;
9168 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009169 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9170 return SDValue();
9171 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009172 LHS = Op1;
9173 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009174 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009175 } else if (Op1.getOpcode() == ISD::Constant) {
9176 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009177 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009178 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009179
9180 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009181 LHS = AndLHS.getOperand(0);
9182 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009183 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009184
9185 // Use BT if the immediate can't be encoded in a TEST instruction.
9186 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9187 LHS = AndLHS;
9188 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9189 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009190 }
Evan Cheng0488db92007-09-25 01:57:46 +00009191
Evan Chengd40d03e2010-01-06 19:38:29 +00009192 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009193 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009194 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009195 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009196 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009197 // Also promote i16 to i32 for performance / code size reason.
9198 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009199 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009200 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009201
Evan Chengd40d03e2010-01-06 19:38:29 +00009202 // If the operand types disagree, extend the shift amount to match. Since
9203 // BT ignores high bits (like shifts) we can use anyextend.
9204 if (LHS.getValueType() != RHS.getValueType())
9205 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009206
Evan Chengd40d03e2010-01-06 19:38:29 +00009207 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009208 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Evan Chengd40d03e2010-01-06 19:38:29 +00009209 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9210 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009211 }
9212
Evan Cheng54de3ea2010-01-05 06:52:31 +00009213 return SDValue();
9214}
9215
Craig Topper89af15e2011-09-18 08:03:58 +00009216// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009217// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009218static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009219 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009220
Craig Topper7a9a28b2012-08-12 02:23:29 +00009221 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009222 "Unsupported value type for operation");
9223
Craig Topper66ddd152012-04-27 22:54:43 +00009224 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009225 DebugLoc dl = Op.getDebugLoc();
9226 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009227
9228 // Extract the LHS vectors
9229 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009230 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9231 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009232
9233 // Extract the RHS vectors
9234 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009235 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9236 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009237
9238 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009239 MVT EltVT = VT.getVectorElementType();
9240 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009241 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9242 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9243 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9244}
9245
Craig Topper26827f32013-01-20 09:02:22 +00009246static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9247 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009248 SDValue Cond;
9249 SDValue Op0 = Op.getOperand(0);
9250 SDValue Op1 = Op.getOperand(1);
9251 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009252 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009253 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009254 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009255 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009256
9257 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009258#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009259 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009260 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9261#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009262
Craig Topper523908d2012-08-13 02:34:03 +00009263 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009264 bool Swap = false;
9265
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009266 // SSE Condition code mapping:
9267 // 0 - EQ
9268 // 1 - LT
9269 // 2 - LE
9270 // 3 - UNORD
9271 // 4 - NEQ
9272 // 5 - NLT
9273 // 6 - NLE
9274 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009275 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009276 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009277 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009278 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009279 case ISD::SETOGT:
9280 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009281 case ISD::SETLT:
9282 case ISD::SETOLT: SSECC = 1; break;
9283 case ISD::SETOGE:
9284 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009285 case ISD::SETLE:
9286 case ISD::SETOLE: SSECC = 2; break;
9287 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009288 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009289 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009290 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009291 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009292 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009293 case ISD::SETUGT: SSECC = 6; break;
9294 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009295 case ISD::SETUEQ:
9296 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009297 }
9298 if (Swap)
9299 std::swap(Op0, Op1);
9300
Nate Begemanfb8ead02008-07-25 19:05:58 +00009301 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009302 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009303 unsigned CC0, CC1;
9304 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009305 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009306 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9307 } else {
9308 assert(SetCCOpcode == ISD::SETONE);
9309 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009310 }
Craig Topper523908d2012-08-13 02:34:03 +00009311
9312 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9313 DAG.getConstant(CC0, MVT::i8));
9314 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9315 DAG.getConstant(CC1, MVT::i8));
9316 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009317 }
9318 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009319 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9320 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009322
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009323 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009324 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009325 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009326
Nate Begeman30a0de92008-07-17 16:51:19 +00009327 // We are handling one of the integer comparisons here. Since SSE only has
9328 // GT and EQ comparisons for integer, swapping operands and multiple
9329 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009330 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009331 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009332
Nate Begeman30a0de92008-07-17 16:51:19 +00009333 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009334 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009335 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009336 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009337 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009338 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009339 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009340 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009341 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009342 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009343 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009344 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009345 }
9346 if (Swap)
9347 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009348
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009349 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9350 // bits of the inputs before performing those operations.
9351 if (FlipSigns) {
9352 EVT EltVT = VT.getVectorElementType();
9353 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9354 EltVT);
9355 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
9356 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9357 SignBits.size());
9358 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9359 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
9360 }
9361
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009362 // Check that the operation in question is available (most are plain SSE2,
9363 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009364 if (VT == MVT::v2i64) {
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009365 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42()) {
9366 assert(Subtarget->hasSSE2() && "Don't know how to lower!");
9367
9368 // First cast everything to the right type,
9369 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9370 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9371
9372 // Emulate PCMPGTQ with (hi1 > hi2) | ((hi1 == hi2) & (lo1 > lo2))
9373 SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1);
9374 SDValue EQ = DAG.getNode(X86ISD::PCMPEQ, dl, MVT::v4i32, Op0, Op1);
9375
9376 // Create masks for only the low parts/high parts of the 64 bit integers.
9377 const int MaskHi[] = { 1, 1, 3, 3 };
9378 const int MaskLo[] = { 0, 0, 2, 2 };
9379 SDValue EQHi = DAG.getVectorShuffle(MVT::v4i32, dl, EQ, EQ, MaskHi);
9380 SDValue GTLo = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskLo);
9381 SDValue GTHi = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi);
9382
9383 SDValue Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, EQHi, GTLo);
9384 Result = DAG.getNode(ISD::OR, dl, MVT::v4i32, Result, GTHi);
9385
9386 if (Invert)
9387 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9388
9389 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9390 }
9391
Benjamin Kramer382ed782012-12-25 12:54:19 +00009392 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9393 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009394 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009395 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9396
9397 // First cast everything to the right type,
9398 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9399 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9400
9401 // Do the compare.
9402 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9403
9404 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009405 const int Mask[] = { 1, 0, 3, 2 };
9406 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9407 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009408
9409 if (Invert)
9410 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9411
9412 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9413 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009414 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009415
Dale Johannesenace16102009-02-03 19:33:06 +00009416 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009417
9418 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009419 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009420 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009421
Nate Begeman30a0de92008-07-17 16:51:19 +00009422 return Result;
9423}
Evan Cheng0488db92007-09-25 01:57:46 +00009424
Craig Topper26827f32013-01-20 09:02:22 +00009425SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9426
9427 MVT VT = Op.getValueType().getSimpleVT();
9428
9429 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9430
9431 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9432 SDValue Op0 = Op.getOperand(0);
9433 SDValue Op1 = Op.getOperand(1);
9434 DebugLoc dl = Op.getDebugLoc();
9435 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9436
9437 // Optimize to BT if possible.
9438 // Lower (X & (1 << N)) == 0 to BT(X, N).
9439 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9440 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9441 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9442 Op1.getOpcode() == ISD::Constant &&
9443 cast<ConstantSDNode>(Op1)->isNullValue() &&
9444 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9445 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9446 if (NewSetCC.getNode())
9447 return NewSetCC;
9448 }
9449
9450 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9451 // these.
9452 if (Op1.getOpcode() == ISD::Constant &&
9453 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9454 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9455 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9456
9457 // If the input is a setcc, then reuse the input setcc or use a new one with
9458 // the inverted condition.
9459 if (Op0.getOpcode() == X86ISD::SETCC) {
9460 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9461 bool Invert = (CC == ISD::SETNE) ^
9462 cast<ConstantSDNode>(Op1)->isNullValue();
9463 if (!Invert) return Op0;
9464
9465 CCode = X86::GetOppositeBranchCondition(CCode);
9466 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9467 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9468 }
9469 }
9470
9471 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9472 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9473 if (X86CC == X86::COND_INVALID)
9474 return SDValue();
9475
9476 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9477 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9478 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9479 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9480}
9481
Evan Cheng370e5342008-12-03 08:38:43 +00009482// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009483static bool isX86LogicalCmp(SDValue Op) {
9484 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009485 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9486 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009487 return true;
9488 if (Op.getResNo() == 1 &&
9489 (Opc == X86ISD::ADD ||
9490 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009491 Opc == X86ISD::ADC ||
9492 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009493 Opc == X86ISD::SMUL ||
9494 Opc == X86ISD::UMUL ||
9495 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009496 Opc == X86ISD::DEC ||
9497 Opc == X86ISD::OR ||
9498 Opc == X86ISD::XOR ||
9499 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009500 return true;
9501
Chris Lattner9637d5b2010-12-05 07:49:54 +00009502 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9503 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009504
Dan Gohman076aee32009-03-04 19:44:21 +00009505 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009506}
9507
Chris Lattnera2b56002010-12-05 01:23:24 +00009508static bool isZero(SDValue V) {
9509 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9510 return C && C->isNullValue();
9511}
9512
Evan Chengb64dd5f2012-08-07 22:21:00 +00009513static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9514 if (V.getOpcode() != ISD::TRUNCATE)
9515 return false;
9516
9517 SDValue VOp0 = V.getOperand(0);
9518 unsigned InBits = VOp0.getValueSizeInBits();
9519 unsigned Bits = V.getValueSizeInBits();
9520 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9521}
9522
Dan Gohmand858e902010-04-17 15:26:15 +00009523SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009524 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009525 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009526 SDValue Op1 = Op.getOperand(1);
9527 SDValue Op2 = Op.getOperand(2);
9528 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009529 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009530
Dan Gohman1a492952009-10-20 16:22:37 +00009531 if (Cond.getOpcode() == ISD::SETCC) {
9532 SDValue NewCond = LowerSETCC(Cond, DAG);
9533 if (NewCond.getNode())
9534 Cond = NewCond;
9535 }
Evan Cheng734503b2006-09-11 02:19:56 +00009536
Chris Lattnera2b56002010-12-05 01:23:24 +00009537 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009538 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009539 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009540 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009541 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009542 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9543 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009544 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009545
Chris Lattnera2b56002010-12-05 01:23:24 +00009546 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009547
9548 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009549 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9550 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009551
9552 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009553 // Apply further optimizations for special cases
9554 // (select (x != 0), -1, 0) -> neg & sbb
9555 // (select (x == 0), 0, -1) -> neg & sbb
9556 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009557 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009558 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9559 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009560 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9561 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009562 CmpOp0);
9563 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9564 DAG.getConstant(X86::COND_B, MVT::i8),
9565 SDValue(Neg.getNode(), 1));
9566 return Res;
9567 }
9568
Chris Lattnera2b56002010-12-05 01:23:24 +00009569 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9570 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009571 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009572
Chris Lattner96908b12010-12-05 02:00:51 +00009573 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009574 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9575 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009576
Chris Lattner96908b12010-12-05 02:00:51 +00009577 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9578 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009579
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009580 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009581 if (N2C == 0 || !N2C->isNullValue())
9582 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9583 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009584 }
9585 }
9586
Chris Lattnera2b56002010-12-05 01:23:24 +00009587 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009588 if (Cond.getOpcode() == ISD::AND &&
9589 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9590 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009591 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009592 Cond = Cond.getOperand(0);
9593 }
9594
Evan Cheng3f41d662007-10-08 22:16:29 +00009595 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9596 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009597 unsigned CondOpcode = Cond.getOpcode();
9598 if (CondOpcode == X86ISD::SETCC ||
9599 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009600 CC = Cond.getOperand(0);
9601
Dan Gohman475871a2008-07-27 21:46:04 +00009602 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009603 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009604 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009605
Evan Cheng3f41d662007-10-08 22:16:29 +00009606 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009607 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009608 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009609 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009610
Chris Lattnerd1980a52009-03-12 06:52:53 +00009611 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9612 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009613 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009614 addTest = false;
9615 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009616 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9617 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9618 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9619 Cond.getOperand(0).getValueType() != MVT::i8)) {
9620 SDValue LHS = Cond.getOperand(0);
9621 SDValue RHS = Cond.getOperand(1);
9622 unsigned X86Opcode;
9623 unsigned X86Cond;
9624 SDVTList VTs;
9625 switch (CondOpcode) {
9626 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9627 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9628 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9629 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9630 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9631 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9632 default: llvm_unreachable("unexpected overflowing operator");
9633 }
9634 if (CondOpcode == ISD::UMULO)
9635 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9636 MVT::i32);
9637 else
9638 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9639
9640 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9641
9642 if (CondOpcode == ISD::UMULO)
9643 Cond = X86Op.getValue(2);
9644 else
9645 Cond = X86Op.getValue(1);
9646
9647 CC = DAG.getConstant(X86Cond, MVT::i8);
9648 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009649 }
9650
9651 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009652 // Look pass the truncate if the high bits are known zero.
9653 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9654 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009655
9656 // We know the result of AND is compared against zero. Try to match
9657 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009658 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009659 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009660 if (NewSetCC.getNode()) {
9661 CC = NewSetCC.getOperand(0);
9662 Cond = NewSetCC.getOperand(1);
9663 addTest = false;
9664 }
9665 }
9666 }
9667
9668 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009669 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009670 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009671 }
9672
Benjamin Kramere915ff32010-12-22 23:09:28 +00009673 // a < b ? -1 : 0 -> RES = ~setcc_carry
9674 // a < b ? 0 : -1 -> RES = setcc_carry
9675 // a >= b ? -1 : 0 -> RES = setcc_carry
9676 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009677 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009678 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009679 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9680
9681 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9682 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9683 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9684 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9685 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9686 return DAG.getNOT(DL, Res, Res.getValueType());
9687 return Res;
9688 }
9689 }
9690
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009691 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9692 // widen the cmov and push the truncate through. This avoids introducing a new
9693 // branch during isel and doesn't add any extensions.
9694 if (Op.getValueType() == MVT::i8 &&
9695 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9696 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9697 if (T1.getValueType() == T2.getValueType() &&
9698 // Blacklist CopyFromReg to avoid partial register stalls.
9699 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9700 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009701 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009702 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9703 }
9704 }
9705
Evan Cheng0488db92007-09-25 01:57:46 +00009706 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9707 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009708 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009709 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009710 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009711}
9712
Nadav Rotem1a330af2012-12-27 22:47:16 +00009713SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9714 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009715 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009716 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009717 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009718 DebugLoc dl = Op->getDebugLoc();
9719
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009720 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9721 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9722 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009723
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009724 if (Subtarget->hasInt256())
9725 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009726
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009727 // Optimize vectors in AVX mode
9728 // Sign extend v8i16 to v8i32 and
9729 // v4i32 to v4i64
9730 //
9731 // Divide input vector into two parts
9732 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9733 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9734 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009735
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009736 unsigned NumElems = InVT.getVectorNumElements();
9737 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009738
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009739 SmallVector<int,8> ShufMask1(NumElems, -1);
9740 for (unsigned i = 0; i != NumElems/2; ++i)
9741 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009742
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009743 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009744
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009745 SmallVector<int,8> ShufMask2(NumElems, -1);
9746 for (unsigned i = 0; i != NumElems/2; ++i)
9747 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009748
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009749 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009750
Craig Toppera080daf2013-01-20 21:50:27 +00009751 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009752 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009753
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009754 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9755 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009756
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009757 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009758}
9759
Evan Cheng370e5342008-12-03 08:38:43 +00009760// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9761// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9762// from the AND / OR.
9763static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9764 Opc = Op.getOpcode();
9765 if (Opc != ISD::OR && Opc != ISD::AND)
9766 return false;
9767 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9768 Op.getOperand(0).hasOneUse() &&
9769 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9770 Op.getOperand(1).hasOneUse());
9771}
9772
Evan Cheng961d6d42009-02-02 08:19:07 +00009773// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9774// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009775static bool isXor1OfSetCC(SDValue Op) {
9776 if (Op.getOpcode() != ISD::XOR)
9777 return false;
9778 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9779 if (N1C && N1C->getAPIntValue() == 1) {
9780 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9781 Op.getOperand(0).hasOneUse();
9782 }
9783 return false;
9784}
9785
Dan Gohmand858e902010-04-17 15:26:15 +00009786SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009787 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009788 SDValue Chain = Op.getOperand(0);
9789 SDValue Cond = Op.getOperand(1);
9790 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009791 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009792 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009793 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009794
Dan Gohman1a492952009-10-20 16:22:37 +00009795 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009796 // Check for setcc([su]{add,sub,mul}o == 0).
9797 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9798 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9799 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9800 Cond.getOperand(0).getResNo() == 1 &&
9801 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9802 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9803 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9804 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9805 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9806 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9807 Inverted = true;
9808 Cond = Cond.getOperand(0);
9809 } else {
9810 SDValue NewCond = LowerSETCC(Cond, DAG);
9811 if (NewCond.getNode())
9812 Cond = NewCond;
9813 }
Dan Gohman1a492952009-10-20 16:22:37 +00009814 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009815#if 0
9816 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009817 else if (Cond.getOpcode() == X86ISD::ADD ||
9818 Cond.getOpcode() == X86ISD::SUB ||
9819 Cond.getOpcode() == X86ISD::SMUL ||
9820 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009821 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009822#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009823
Evan Chengad9c0a32009-12-15 00:53:42 +00009824 // Look pass (and (setcc_carry (cmp ...)), 1).
9825 if (Cond.getOpcode() == ISD::AND &&
9826 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9827 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009828 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009829 Cond = Cond.getOperand(0);
9830 }
9831
Evan Cheng3f41d662007-10-08 22:16:29 +00009832 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9833 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009834 unsigned CondOpcode = Cond.getOpcode();
9835 if (CondOpcode == X86ISD::SETCC ||
9836 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009837 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009838
Dan Gohman475871a2008-07-27 21:46:04 +00009839 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009840 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009841 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009842 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009843 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009844 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009845 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009846 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009847 default: break;
9848 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009849 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009850 // These can only come from an arithmetic instruction with overflow,
9851 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009852 Cond = Cond.getNode()->getOperand(1);
9853 addTest = false;
9854 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009855 }
Evan Cheng0488db92007-09-25 01:57:46 +00009856 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009857 }
9858 CondOpcode = Cond.getOpcode();
9859 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9860 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9861 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9862 Cond.getOperand(0).getValueType() != MVT::i8)) {
9863 SDValue LHS = Cond.getOperand(0);
9864 SDValue RHS = Cond.getOperand(1);
9865 unsigned X86Opcode;
9866 unsigned X86Cond;
9867 SDVTList VTs;
9868 switch (CondOpcode) {
9869 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9870 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9871 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9872 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9873 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9874 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9875 default: llvm_unreachable("unexpected overflowing operator");
9876 }
9877 if (Inverted)
9878 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9879 if (CondOpcode == ISD::UMULO)
9880 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9881 MVT::i32);
9882 else
9883 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9884
9885 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9886
9887 if (CondOpcode == ISD::UMULO)
9888 Cond = X86Op.getValue(2);
9889 else
9890 Cond = X86Op.getValue(1);
9891
9892 CC = DAG.getConstant(X86Cond, MVT::i8);
9893 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009894 } else {
9895 unsigned CondOpc;
9896 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9897 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009898 if (CondOpc == ISD::OR) {
9899 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9900 // two branches instead of an explicit OR instruction with a
9901 // separate test.
9902 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009903 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009904 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009905 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009906 Chain, Dest, CC, Cmp);
9907 CC = Cond.getOperand(1).getOperand(0);
9908 Cond = Cmp;
9909 addTest = false;
9910 }
9911 } else { // ISD::AND
9912 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9913 // two branches instead of an explicit AND instruction with a
9914 // separate test. However, we only do this if this block doesn't
9915 // have a fall-through edge, because this requires an explicit
9916 // jmp when the condition is false.
9917 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009918 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009919 Op.getNode()->hasOneUse()) {
9920 X86::CondCode CCode =
9921 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9922 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009923 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009924 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009925 // Look for an unconditional branch following this conditional branch.
9926 // We need this because we need to reverse the successors in order
9927 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009928 if (User->getOpcode() == ISD::BR) {
9929 SDValue FalseBB = User->getOperand(1);
9930 SDNode *NewBR =
9931 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009932 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009933 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009934 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009935
Dale Johannesene4d209d2009-02-03 20:21:25 +00009936 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009937 Chain, Dest, CC, Cmp);
9938 X86::CondCode CCode =
9939 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9940 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009941 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009942 Cond = Cmp;
9943 addTest = false;
9944 }
9945 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009946 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009947 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9948 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9949 // It should be transformed during dag combiner except when the condition
9950 // is set by a arithmetics with overflow node.
9951 X86::CondCode CCode =
9952 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9953 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009954 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009955 Cond = Cond.getOperand(0).getOperand(1);
9956 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009957 } else if (Cond.getOpcode() == ISD::SETCC &&
9958 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9959 // For FCMP_OEQ, we can emit
9960 // two branches instead of an explicit AND instruction with a
9961 // separate test. However, we only do this if this block doesn't
9962 // have a fall-through edge, because this requires an explicit
9963 // jmp when the condition is false.
9964 if (Op.getNode()->hasOneUse()) {
9965 SDNode *User = *Op.getNode()->use_begin();
9966 // Look for an unconditional branch following this conditional branch.
9967 // We need this because we need to reverse the successors in order
9968 // to implement FCMP_OEQ.
9969 if (User->getOpcode() == ISD::BR) {
9970 SDValue FalseBB = User->getOperand(1);
9971 SDNode *NewBR =
9972 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9973 assert(NewBR == User);
9974 (void)NewBR;
9975 Dest = FalseBB;
9976
9977 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9978 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009979 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009980 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9981 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9982 Chain, Dest, CC, Cmp);
9983 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9984 Cond = Cmp;
9985 addTest = false;
9986 }
9987 }
9988 } else if (Cond.getOpcode() == ISD::SETCC &&
9989 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9990 // For FCMP_UNE, we can emit
9991 // two branches instead of an explicit AND instruction with a
9992 // separate test. However, we only do this if this block doesn't
9993 // have a fall-through edge, because this requires an explicit
9994 // jmp when the condition is false.
9995 if (Op.getNode()->hasOneUse()) {
9996 SDNode *User = *Op.getNode()->use_begin();
9997 // Look for an unconditional branch following this conditional branch.
9998 // We need this because we need to reverse the successors in order
9999 // to implement FCMP_UNE.
10000 if (User->getOpcode() == ISD::BR) {
10001 SDValue FalseBB = User->getOperand(1);
10002 SDNode *NewBR =
10003 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
10004 assert(NewBR == User);
10005 (void)NewBR;
10006
10007 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
10008 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010009 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +000010010 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
10011 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
10012 Chain, Dest, CC, Cmp);
10013 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
10014 Cond = Cmp;
10015 addTest = false;
10016 Dest = FalseBB;
10017 }
10018 }
Dan Gohman279c22e2008-10-21 03:29:32 +000010019 }
Evan Cheng0488db92007-09-25 01:57:46 +000010020 }
10021
10022 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +000010023 // Look pass the truncate if the high bits are known zero.
10024 if (isTruncWithZeroHighBitsInput(Cond, DAG))
10025 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +000010026
10027 // We know the result of AND is compared against zero. Try to match
10028 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010029 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +000010030 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
10031 if (NewSetCC.getNode()) {
10032 CC = NewSetCC.getOperand(0);
10033 Cond = NewSetCC.getOperand(1);
10034 addTest = false;
10035 }
10036 }
10037 }
10038
10039 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010040 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +000010041 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +000010042 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010043 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010044 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010045 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010046}
10047
Anton Korobeynikove060b532007-04-17 19:34:00 +000010048// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10049// Calls to _alloca is needed to probe the stack when allocating more than 4k
10050// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10051// that the guard pages used by the OS virtual memory manager are allocated in
10052// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010053SDValue
10054X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010055 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010056 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010057 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010058 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010059 "are being used");
10060 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010061 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010062
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010063 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010064 SDValue Chain = Op.getOperand(0);
10065 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010066 // FIXME: Ensure alignment here
10067
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010068 bool Is64Bit = Subtarget->is64Bit();
10069 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010070
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010071 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010072 MachineFunction &MF = DAG.getMachineFunction();
10073 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010074
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010075 if (Is64Bit) {
10076 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010077 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010078 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010079
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010080 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010081 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010082 if (I->hasNestAttr())
10083 report_fatal_error("Cannot use segmented stacks with functions that "
10084 "have nested arguments.");
10085 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010086
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010087 const TargetRegisterClass *AddrRegClass =
10088 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10089 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10090 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10091 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10092 DAG.getRegister(Vreg, SPTy));
10093 SDValue Ops1[2] = { Value, Chain };
10094 return DAG.getMergeValues(Ops1, 2, dl);
10095 } else {
10096 SDValue Flag;
10097 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010098
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010099 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10100 Flag = Chain.getValue(1);
10101 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010102
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010103 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10104 Flag = Chain.getValue(1);
10105
Michael Liaoc5c970e2012-10-31 04:14:09 +000010106 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10107 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010108
10109 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10110 return DAG.getMergeValues(Ops1, 2, dl);
10111 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010112}
10113
Dan Gohmand858e902010-04-17 15:26:15 +000010114SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010115 MachineFunction &MF = DAG.getMachineFunction();
10116 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10117
Dan Gohman69de1932008-02-06 22:27:42 +000010118 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010119 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010120
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010121 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010122 // vastart just stores the address of the VarArgsFrameIndex slot into the
10123 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010124 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10125 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010126 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10127 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010128 }
10129
10130 // __va_list_tag:
10131 // gp_offset (0 - 6 * 8)
10132 // fp_offset (48 - 48 + 8 * 16)
10133 // overflow_arg_area (point to parameters coming in memory).
10134 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010135 SmallVector<SDValue, 8> MemOps;
10136 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010137 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010138 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010139 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10140 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010141 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010142 MemOps.push_back(Store);
10143
10144 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010145 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010146 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010147 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010148 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10149 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010150 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010151 MemOps.push_back(Store);
10152
10153 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010154 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010155 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010156 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10157 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010158 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10159 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010160 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010161 MemOps.push_back(Store);
10162
10163 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010164 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010165 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010166 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10167 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010168 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10169 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010170 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010171 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010172 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010173}
10174
Dan Gohmand858e902010-04-17 15:26:15 +000010175SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010176 assert(Subtarget->is64Bit() &&
10177 "LowerVAARG only handles 64-bit va_arg!");
10178 assert((Subtarget->isTargetLinux() ||
10179 Subtarget->isTargetDarwin()) &&
10180 "Unhandled target in LowerVAARG");
10181 assert(Op.getNode()->getNumOperands() == 4);
10182 SDValue Chain = Op.getOperand(0);
10183 SDValue SrcPtr = Op.getOperand(1);
10184 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10185 unsigned Align = Op.getConstantOperandVal(3);
10186 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010187
Dan Gohman320afb82010-10-12 18:00:49 +000010188 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010189 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010190 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010191 uint8_t ArgMode;
10192
10193 // Decide which area this value should be read from.
10194 // TODO: Implement the AMD64 ABI in its entirety. This simple
10195 // selection mechanism works only for the basic types.
10196 if (ArgVT == MVT::f80) {
10197 llvm_unreachable("va_arg for f80 not yet implemented");
10198 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10199 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10200 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10201 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10202 } else {
10203 llvm_unreachable("Unhandled argument type in LowerVAARG");
10204 }
10205
10206 if (ArgMode == 2) {
10207 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010208 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010209 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010210 .getFunction()->getAttributes()
10211 .hasAttribute(AttributeSet::FunctionIndex,
10212 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010213 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010214 }
10215
10216 // Insert VAARG_64 node into the DAG
10217 // VAARG_64 returns two values: Variable Argument Address, Chain
10218 SmallVector<SDValue, 11> InstOps;
10219 InstOps.push_back(Chain);
10220 InstOps.push_back(SrcPtr);
10221 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10222 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10223 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10224 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10225 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10226 VTs, &InstOps[0], InstOps.size(),
10227 MVT::i64,
10228 MachinePointerInfo(SV),
10229 /*Align=*/0,
10230 /*Volatile=*/false,
10231 /*ReadMem=*/true,
10232 /*WriteMem=*/true);
10233 Chain = VAARG.getValue(1);
10234
10235 // Load the next argument and return it
10236 return DAG.getLoad(ArgVT, dl,
10237 Chain,
10238 VAARG,
10239 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010240 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010241}
10242
Craig Topper55b24052012-09-11 06:15:32 +000010243static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10244 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010245 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010246 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010247 SDValue Chain = Op.getOperand(0);
10248 SDValue DstPtr = Op.getOperand(1);
10249 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010250 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10251 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010252 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010253
Chris Lattnere72f2022010-09-21 05:40:29 +000010254 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010255 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010256 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010257 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010258}
10259
Craig Topperff3139f2013-02-19 07:43:59 +000010260// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010261// may or may not be a constant. Takes immediate version of shift as input.
10262static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10263 SDValue SrcOp, SDValue ShAmt,
10264 SelectionDAG &DAG) {
10265 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10266
10267 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010268 // Constant may be a TargetConstant. Use a regular constant.
10269 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010270 switch (Opc) {
10271 default: llvm_unreachable("Unknown target vector shift node");
10272 case X86ISD::VSHLI:
10273 case X86ISD::VSRLI:
10274 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010275 return DAG.getNode(Opc, dl, VT, SrcOp,
10276 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010277 }
10278 }
10279
10280 // Change opcode to non-immediate version
10281 switch (Opc) {
10282 default: llvm_unreachable("Unknown target vector shift node");
10283 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10284 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10285 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10286 }
10287
10288 // Need to build a vector containing shift amount
10289 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10290 SDValue ShOps[4];
10291 ShOps[0] = ShAmt;
10292 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010293 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010294 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010295
10296 // The return type has to be a 128-bit type with the same element
10297 // type as the input type.
10298 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10299 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10300
10301 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010302 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10303}
10304
Craig Topper55b24052012-09-11 06:15:32 +000010305static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010306 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010307 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010308 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010309 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010310 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010311 case Intrinsic::x86_sse_comieq_ss:
10312 case Intrinsic::x86_sse_comilt_ss:
10313 case Intrinsic::x86_sse_comile_ss:
10314 case Intrinsic::x86_sse_comigt_ss:
10315 case Intrinsic::x86_sse_comige_ss:
10316 case Intrinsic::x86_sse_comineq_ss:
10317 case Intrinsic::x86_sse_ucomieq_ss:
10318 case Intrinsic::x86_sse_ucomilt_ss:
10319 case Intrinsic::x86_sse_ucomile_ss:
10320 case Intrinsic::x86_sse_ucomigt_ss:
10321 case Intrinsic::x86_sse_ucomige_ss:
10322 case Intrinsic::x86_sse_ucomineq_ss:
10323 case Intrinsic::x86_sse2_comieq_sd:
10324 case Intrinsic::x86_sse2_comilt_sd:
10325 case Intrinsic::x86_sse2_comile_sd:
10326 case Intrinsic::x86_sse2_comigt_sd:
10327 case Intrinsic::x86_sse2_comige_sd:
10328 case Intrinsic::x86_sse2_comineq_sd:
10329 case Intrinsic::x86_sse2_ucomieq_sd:
10330 case Intrinsic::x86_sse2_ucomilt_sd:
10331 case Intrinsic::x86_sse2_ucomile_sd:
10332 case Intrinsic::x86_sse2_ucomigt_sd:
10333 case Intrinsic::x86_sse2_ucomige_sd:
10334 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010335 unsigned Opc;
10336 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010337 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010338 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010339 case Intrinsic::x86_sse_comieq_ss:
10340 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010341 Opc = X86ISD::COMI;
10342 CC = ISD::SETEQ;
10343 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010344 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010345 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010346 Opc = X86ISD::COMI;
10347 CC = ISD::SETLT;
10348 break;
10349 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010350 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010351 Opc = X86ISD::COMI;
10352 CC = ISD::SETLE;
10353 break;
10354 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010355 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010356 Opc = X86ISD::COMI;
10357 CC = ISD::SETGT;
10358 break;
10359 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010360 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010361 Opc = X86ISD::COMI;
10362 CC = ISD::SETGE;
10363 break;
10364 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010365 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010366 Opc = X86ISD::COMI;
10367 CC = ISD::SETNE;
10368 break;
10369 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010370 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010371 Opc = X86ISD::UCOMI;
10372 CC = ISD::SETEQ;
10373 break;
10374 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010375 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010376 Opc = X86ISD::UCOMI;
10377 CC = ISD::SETLT;
10378 break;
10379 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010380 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010381 Opc = X86ISD::UCOMI;
10382 CC = ISD::SETLE;
10383 break;
10384 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010385 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010386 Opc = X86ISD::UCOMI;
10387 CC = ISD::SETGT;
10388 break;
10389 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010390 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010391 Opc = X86ISD::UCOMI;
10392 CC = ISD::SETGE;
10393 break;
10394 case Intrinsic::x86_sse_ucomineq_ss:
10395 case Intrinsic::x86_sse2_ucomineq_sd:
10396 Opc = X86ISD::UCOMI;
10397 CC = ISD::SETNE;
10398 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010399 }
Evan Cheng734503b2006-09-11 02:19:56 +000010400
Dan Gohman475871a2008-07-27 21:46:04 +000010401 SDValue LHS = Op.getOperand(1);
10402 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010403 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010404 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010405 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10406 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10407 DAG.getConstant(X86CC, MVT::i8), Cond);
10408 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010409 }
Craig Topper6d688152012-08-14 07:43:25 +000010410
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010411 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010412 case Intrinsic::x86_sse2_pmulu_dq:
10413 case Intrinsic::x86_avx2_pmulu_dq:
10414 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10415 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010416
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010417 // SSE2/AVX2 sub with unsigned saturation intrinsics
10418 case Intrinsic::x86_sse2_psubus_b:
10419 case Intrinsic::x86_sse2_psubus_w:
10420 case Intrinsic::x86_avx2_psubus_b:
10421 case Intrinsic::x86_avx2_psubus_w:
10422 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10423 Op.getOperand(1), Op.getOperand(2));
10424
Craig Topper6d688152012-08-14 07:43:25 +000010425 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010426 case Intrinsic::x86_sse3_hadd_ps:
10427 case Intrinsic::x86_sse3_hadd_pd:
10428 case Intrinsic::x86_avx_hadd_ps_256:
10429 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010430 case Intrinsic::x86_sse3_hsub_ps:
10431 case Intrinsic::x86_sse3_hsub_pd:
10432 case Intrinsic::x86_avx_hsub_ps_256:
10433 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010434 case Intrinsic::x86_ssse3_phadd_w_128:
10435 case Intrinsic::x86_ssse3_phadd_d_128:
10436 case Intrinsic::x86_avx2_phadd_w:
10437 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010438 case Intrinsic::x86_ssse3_phsub_w_128:
10439 case Intrinsic::x86_ssse3_phsub_d_128:
10440 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010441 case Intrinsic::x86_avx2_phsub_d: {
10442 unsigned Opcode;
10443 switch (IntNo) {
10444 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10445 case Intrinsic::x86_sse3_hadd_ps:
10446 case Intrinsic::x86_sse3_hadd_pd:
10447 case Intrinsic::x86_avx_hadd_ps_256:
10448 case Intrinsic::x86_avx_hadd_pd_256:
10449 Opcode = X86ISD::FHADD;
10450 break;
10451 case Intrinsic::x86_sse3_hsub_ps:
10452 case Intrinsic::x86_sse3_hsub_pd:
10453 case Intrinsic::x86_avx_hsub_ps_256:
10454 case Intrinsic::x86_avx_hsub_pd_256:
10455 Opcode = X86ISD::FHSUB;
10456 break;
10457 case Intrinsic::x86_ssse3_phadd_w_128:
10458 case Intrinsic::x86_ssse3_phadd_d_128:
10459 case Intrinsic::x86_avx2_phadd_w:
10460 case Intrinsic::x86_avx2_phadd_d:
10461 Opcode = X86ISD::HADD;
10462 break;
10463 case Intrinsic::x86_ssse3_phsub_w_128:
10464 case Intrinsic::x86_ssse3_phsub_d_128:
10465 case Intrinsic::x86_avx2_phsub_w:
10466 case Intrinsic::x86_avx2_phsub_d:
10467 Opcode = X86ISD::HSUB;
10468 break;
10469 }
10470 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010471 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010472 }
10473
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010474 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10475 case Intrinsic::x86_sse2_pmaxu_b:
10476 case Intrinsic::x86_sse41_pmaxuw:
10477 case Intrinsic::x86_sse41_pmaxud:
10478 case Intrinsic::x86_avx2_pmaxu_b:
10479 case Intrinsic::x86_avx2_pmaxu_w:
10480 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010481 case Intrinsic::x86_sse2_pminu_b:
10482 case Intrinsic::x86_sse41_pminuw:
10483 case Intrinsic::x86_sse41_pminud:
10484 case Intrinsic::x86_avx2_pminu_b:
10485 case Intrinsic::x86_avx2_pminu_w:
10486 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010487 case Intrinsic::x86_sse41_pmaxsb:
10488 case Intrinsic::x86_sse2_pmaxs_w:
10489 case Intrinsic::x86_sse41_pmaxsd:
10490 case Intrinsic::x86_avx2_pmaxs_b:
10491 case Intrinsic::x86_avx2_pmaxs_w:
10492 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010493 case Intrinsic::x86_sse41_pminsb:
10494 case Intrinsic::x86_sse2_pmins_w:
10495 case Intrinsic::x86_sse41_pminsd:
10496 case Intrinsic::x86_avx2_pmins_b:
10497 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010498 case Intrinsic::x86_avx2_pmins_d: {
10499 unsigned Opcode;
10500 switch (IntNo) {
10501 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10502 case Intrinsic::x86_sse2_pmaxu_b:
10503 case Intrinsic::x86_sse41_pmaxuw:
10504 case Intrinsic::x86_sse41_pmaxud:
10505 case Intrinsic::x86_avx2_pmaxu_b:
10506 case Intrinsic::x86_avx2_pmaxu_w:
10507 case Intrinsic::x86_avx2_pmaxu_d:
10508 Opcode = X86ISD::UMAX;
10509 break;
10510 case Intrinsic::x86_sse2_pminu_b:
10511 case Intrinsic::x86_sse41_pminuw:
10512 case Intrinsic::x86_sse41_pminud:
10513 case Intrinsic::x86_avx2_pminu_b:
10514 case Intrinsic::x86_avx2_pminu_w:
10515 case Intrinsic::x86_avx2_pminu_d:
10516 Opcode = X86ISD::UMIN;
10517 break;
10518 case Intrinsic::x86_sse41_pmaxsb:
10519 case Intrinsic::x86_sse2_pmaxs_w:
10520 case Intrinsic::x86_sse41_pmaxsd:
10521 case Intrinsic::x86_avx2_pmaxs_b:
10522 case Intrinsic::x86_avx2_pmaxs_w:
10523 case Intrinsic::x86_avx2_pmaxs_d:
10524 Opcode = X86ISD::SMAX;
10525 break;
10526 case Intrinsic::x86_sse41_pminsb:
10527 case Intrinsic::x86_sse2_pmins_w:
10528 case Intrinsic::x86_sse41_pminsd:
10529 case Intrinsic::x86_avx2_pmins_b:
10530 case Intrinsic::x86_avx2_pmins_w:
10531 case Intrinsic::x86_avx2_pmins_d:
10532 Opcode = X86ISD::SMIN;
10533 break;
10534 }
10535 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010536 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010537 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010538
Craig Topper6d183e42012-12-29 16:44:25 +000010539 // SSE/SSE2/AVX floating point max/min intrinsics.
10540 case Intrinsic::x86_sse_max_ps:
10541 case Intrinsic::x86_sse2_max_pd:
10542 case Intrinsic::x86_avx_max_ps_256:
10543 case Intrinsic::x86_avx_max_pd_256:
10544 case Intrinsic::x86_sse_min_ps:
10545 case Intrinsic::x86_sse2_min_pd:
10546 case Intrinsic::x86_avx_min_ps_256:
10547 case Intrinsic::x86_avx_min_pd_256: {
10548 unsigned Opcode;
10549 switch (IntNo) {
10550 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10551 case Intrinsic::x86_sse_max_ps:
10552 case Intrinsic::x86_sse2_max_pd:
10553 case Intrinsic::x86_avx_max_ps_256:
10554 case Intrinsic::x86_avx_max_pd_256:
10555 Opcode = X86ISD::FMAX;
10556 break;
10557 case Intrinsic::x86_sse_min_ps:
10558 case Intrinsic::x86_sse2_min_pd:
10559 case Intrinsic::x86_avx_min_ps_256:
10560 case Intrinsic::x86_avx_min_pd_256:
10561 Opcode = X86ISD::FMIN;
10562 break;
10563 }
10564 return DAG.getNode(Opcode, dl, Op.getValueType(),
10565 Op.getOperand(1), Op.getOperand(2));
10566 }
10567
Craig Topper6d688152012-08-14 07:43:25 +000010568 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010569 case Intrinsic::x86_avx2_psllv_d:
10570 case Intrinsic::x86_avx2_psllv_q:
10571 case Intrinsic::x86_avx2_psllv_d_256:
10572 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010573 case Intrinsic::x86_avx2_psrlv_d:
10574 case Intrinsic::x86_avx2_psrlv_q:
10575 case Intrinsic::x86_avx2_psrlv_d_256:
10576 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010577 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010578 case Intrinsic::x86_avx2_psrav_d_256: {
10579 unsigned Opcode;
10580 switch (IntNo) {
10581 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10582 case Intrinsic::x86_avx2_psllv_d:
10583 case Intrinsic::x86_avx2_psllv_q:
10584 case Intrinsic::x86_avx2_psllv_d_256:
10585 case Intrinsic::x86_avx2_psllv_q_256:
10586 Opcode = ISD::SHL;
10587 break;
10588 case Intrinsic::x86_avx2_psrlv_d:
10589 case Intrinsic::x86_avx2_psrlv_q:
10590 case Intrinsic::x86_avx2_psrlv_d_256:
10591 case Intrinsic::x86_avx2_psrlv_q_256:
10592 Opcode = ISD::SRL;
10593 break;
10594 case Intrinsic::x86_avx2_psrav_d:
10595 case Intrinsic::x86_avx2_psrav_d_256:
10596 Opcode = ISD::SRA;
10597 break;
10598 }
10599 return DAG.getNode(Opcode, dl, Op.getValueType(),
10600 Op.getOperand(1), Op.getOperand(2));
10601 }
10602
Craig Topper969ba282012-01-25 06:43:11 +000010603 case Intrinsic::x86_ssse3_pshuf_b_128:
10604 case Intrinsic::x86_avx2_pshuf_b:
10605 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10606 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010607
Craig Topper969ba282012-01-25 06:43:11 +000010608 case Intrinsic::x86_ssse3_psign_b_128:
10609 case Intrinsic::x86_ssse3_psign_w_128:
10610 case Intrinsic::x86_ssse3_psign_d_128:
10611 case Intrinsic::x86_avx2_psign_b:
10612 case Intrinsic::x86_avx2_psign_w:
10613 case Intrinsic::x86_avx2_psign_d:
10614 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10615 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010616
Craig Toppere566cd02012-01-26 07:18:03 +000010617 case Intrinsic::x86_sse41_insertps:
10618 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10619 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010620
Craig Toppere566cd02012-01-26 07:18:03 +000010621 case Intrinsic::x86_avx_vperm2f128_ps_256:
10622 case Intrinsic::x86_avx_vperm2f128_pd_256:
10623 case Intrinsic::x86_avx_vperm2f128_si_256:
10624 case Intrinsic::x86_avx2_vperm2i128:
10625 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10626 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010627
Craig Topperffa6c402012-04-16 07:13:00 +000010628 case Intrinsic::x86_avx2_permd:
10629 case Intrinsic::x86_avx2_permps:
10630 // Operands intentionally swapped. Mask is last operand to intrinsic,
10631 // but second operand for node/intruction.
10632 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10633 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010634
Craig Topper22d8f0d2012-12-29 18:18:20 +000010635 case Intrinsic::x86_sse_sqrt_ps:
10636 case Intrinsic::x86_sse2_sqrt_pd:
10637 case Intrinsic::x86_avx_sqrt_ps_256:
10638 case Intrinsic::x86_avx_sqrt_pd_256:
10639 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10640
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010641 // ptest and testp intrinsics. The intrinsic these come from are designed to
10642 // return an integer value, not just an instruction so lower it to the ptest
10643 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010644 case Intrinsic::x86_sse41_ptestz:
10645 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010646 case Intrinsic::x86_sse41_ptestnzc:
10647 case Intrinsic::x86_avx_ptestz_256:
10648 case Intrinsic::x86_avx_ptestc_256:
10649 case Intrinsic::x86_avx_ptestnzc_256:
10650 case Intrinsic::x86_avx_vtestz_ps:
10651 case Intrinsic::x86_avx_vtestc_ps:
10652 case Intrinsic::x86_avx_vtestnzc_ps:
10653 case Intrinsic::x86_avx_vtestz_pd:
10654 case Intrinsic::x86_avx_vtestc_pd:
10655 case Intrinsic::x86_avx_vtestnzc_pd:
10656 case Intrinsic::x86_avx_vtestz_ps_256:
10657 case Intrinsic::x86_avx_vtestc_ps_256:
10658 case Intrinsic::x86_avx_vtestnzc_ps_256:
10659 case Intrinsic::x86_avx_vtestz_pd_256:
10660 case Intrinsic::x86_avx_vtestc_pd_256:
10661 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10662 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010663 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010664 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010665 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010666 case Intrinsic::x86_avx_vtestz_ps:
10667 case Intrinsic::x86_avx_vtestz_pd:
10668 case Intrinsic::x86_avx_vtestz_ps_256:
10669 case Intrinsic::x86_avx_vtestz_pd_256:
10670 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010671 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010672 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010673 // ZF = 1
10674 X86CC = X86::COND_E;
10675 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010676 case Intrinsic::x86_avx_vtestc_ps:
10677 case Intrinsic::x86_avx_vtestc_pd:
10678 case Intrinsic::x86_avx_vtestc_ps_256:
10679 case Intrinsic::x86_avx_vtestc_pd_256:
10680 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010681 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010682 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010683 // CF = 1
10684 X86CC = X86::COND_B;
10685 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010686 case Intrinsic::x86_avx_vtestnzc_ps:
10687 case Intrinsic::x86_avx_vtestnzc_pd:
10688 case Intrinsic::x86_avx_vtestnzc_ps_256:
10689 case Intrinsic::x86_avx_vtestnzc_pd_256:
10690 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010691 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010692 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010693 // ZF and CF = 0
10694 X86CC = X86::COND_A;
10695 break;
10696 }
Eric Christopherfd179292009-08-27 18:07:15 +000010697
Eric Christopher71c67532009-07-29 00:28:05 +000010698 SDValue LHS = Op.getOperand(1);
10699 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010700 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10701 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010702 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10703 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10704 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010705 }
Evan Cheng5759f972008-05-04 09:15:50 +000010706
Craig Topper80e46362012-01-23 06:16:53 +000010707 // SSE/AVX shift intrinsics
10708 case Intrinsic::x86_sse2_psll_w:
10709 case Intrinsic::x86_sse2_psll_d:
10710 case Intrinsic::x86_sse2_psll_q:
10711 case Intrinsic::x86_avx2_psll_w:
10712 case Intrinsic::x86_avx2_psll_d:
10713 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010714 case Intrinsic::x86_sse2_psrl_w:
10715 case Intrinsic::x86_sse2_psrl_d:
10716 case Intrinsic::x86_sse2_psrl_q:
10717 case Intrinsic::x86_avx2_psrl_w:
10718 case Intrinsic::x86_avx2_psrl_d:
10719 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010720 case Intrinsic::x86_sse2_psra_w:
10721 case Intrinsic::x86_sse2_psra_d:
10722 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010723 case Intrinsic::x86_avx2_psra_d: {
10724 unsigned Opcode;
10725 switch (IntNo) {
10726 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10727 case Intrinsic::x86_sse2_psll_w:
10728 case Intrinsic::x86_sse2_psll_d:
10729 case Intrinsic::x86_sse2_psll_q:
10730 case Intrinsic::x86_avx2_psll_w:
10731 case Intrinsic::x86_avx2_psll_d:
10732 case Intrinsic::x86_avx2_psll_q:
10733 Opcode = X86ISD::VSHL;
10734 break;
10735 case Intrinsic::x86_sse2_psrl_w:
10736 case Intrinsic::x86_sse2_psrl_d:
10737 case Intrinsic::x86_sse2_psrl_q:
10738 case Intrinsic::x86_avx2_psrl_w:
10739 case Intrinsic::x86_avx2_psrl_d:
10740 case Intrinsic::x86_avx2_psrl_q:
10741 Opcode = X86ISD::VSRL;
10742 break;
10743 case Intrinsic::x86_sse2_psra_w:
10744 case Intrinsic::x86_sse2_psra_d:
10745 case Intrinsic::x86_avx2_psra_w:
10746 case Intrinsic::x86_avx2_psra_d:
10747 Opcode = X86ISD::VSRA;
10748 break;
10749 }
10750 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010751 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010752 }
10753
10754 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010755 case Intrinsic::x86_sse2_pslli_w:
10756 case Intrinsic::x86_sse2_pslli_d:
10757 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010758 case Intrinsic::x86_avx2_pslli_w:
10759 case Intrinsic::x86_avx2_pslli_d:
10760 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010761 case Intrinsic::x86_sse2_psrli_w:
10762 case Intrinsic::x86_sse2_psrli_d:
10763 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010764 case Intrinsic::x86_avx2_psrli_w:
10765 case Intrinsic::x86_avx2_psrli_d:
10766 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010767 case Intrinsic::x86_sse2_psrai_w:
10768 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010769 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010770 case Intrinsic::x86_avx2_psrai_d: {
10771 unsigned Opcode;
10772 switch (IntNo) {
10773 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10774 case Intrinsic::x86_sse2_pslli_w:
10775 case Intrinsic::x86_sse2_pslli_d:
10776 case Intrinsic::x86_sse2_pslli_q:
10777 case Intrinsic::x86_avx2_pslli_w:
10778 case Intrinsic::x86_avx2_pslli_d:
10779 case Intrinsic::x86_avx2_pslli_q:
10780 Opcode = X86ISD::VSHLI;
10781 break;
10782 case Intrinsic::x86_sse2_psrli_w:
10783 case Intrinsic::x86_sse2_psrli_d:
10784 case Intrinsic::x86_sse2_psrli_q:
10785 case Intrinsic::x86_avx2_psrli_w:
10786 case Intrinsic::x86_avx2_psrli_d:
10787 case Intrinsic::x86_avx2_psrli_q:
10788 Opcode = X86ISD::VSRLI;
10789 break;
10790 case Intrinsic::x86_sse2_psrai_w:
10791 case Intrinsic::x86_sse2_psrai_d:
10792 case Intrinsic::x86_avx2_psrai_w:
10793 case Intrinsic::x86_avx2_psrai_d:
10794 Opcode = X86ISD::VSRAI;
10795 break;
10796 }
10797 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010798 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010799 }
10800
Craig Topper4feb6472012-08-06 06:22:36 +000010801 case Intrinsic::x86_sse42_pcmpistria128:
10802 case Intrinsic::x86_sse42_pcmpestria128:
10803 case Intrinsic::x86_sse42_pcmpistric128:
10804 case Intrinsic::x86_sse42_pcmpestric128:
10805 case Intrinsic::x86_sse42_pcmpistrio128:
10806 case Intrinsic::x86_sse42_pcmpestrio128:
10807 case Intrinsic::x86_sse42_pcmpistris128:
10808 case Intrinsic::x86_sse42_pcmpestris128:
10809 case Intrinsic::x86_sse42_pcmpistriz128:
10810 case Intrinsic::x86_sse42_pcmpestriz128: {
10811 unsigned Opcode;
10812 unsigned X86CC;
10813 switch (IntNo) {
10814 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10815 case Intrinsic::x86_sse42_pcmpistria128:
10816 Opcode = X86ISD::PCMPISTRI;
10817 X86CC = X86::COND_A;
10818 break;
10819 case Intrinsic::x86_sse42_pcmpestria128:
10820 Opcode = X86ISD::PCMPESTRI;
10821 X86CC = X86::COND_A;
10822 break;
10823 case Intrinsic::x86_sse42_pcmpistric128:
10824 Opcode = X86ISD::PCMPISTRI;
10825 X86CC = X86::COND_B;
10826 break;
10827 case Intrinsic::x86_sse42_pcmpestric128:
10828 Opcode = X86ISD::PCMPESTRI;
10829 X86CC = X86::COND_B;
10830 break;
10831 case Intrinsic::x86_sse42_pcmpistrio128:
10832 Opcode = X86ISD::PCMPISTRI;
10833 X86CC = X86::COND_O;
10834 break;
10835 case Intrinsic::x86_sse42_pcmpestrio128:
10836 Opcode = X86ISD::PCMPESTRI;
10837 X86CC = X86::COND_O;
10838 break;
10839 case Intrinsic::x86_sse42_pcmpistris128:
10840 Opcode = X86ISD::PCMPISTRI;
10841 X86CC = X86::COND_S;
10842 break;
10843 case Intrinsic::x86_sse42_pcmpestris128:
10844 Opcode = X86ISD::PCMPESTRI;
10845 X86CC = X86::COND_S;
10846 break;
10847 case Intrinsic::x86_sse42_pcmpistriz128:
10848 Opcode = X86ISD::PCMPISTRI;
10849 X86CC = X86::COND_E;
10850 break;
10851 case Intrinsic::x86_sse42_pcmpestriz128:
10852 Opcode = X86ISD::PCMPESTRI;
10853 X86CC = X86::COND_E;
10854 break;
10855 }
10856 SmallVector<SDValue, 5> NewOps;
10857 NewOps.append(Op->op_begin()+1, Op->op_end());
10858 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10859 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10860 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10861 DAG.getConstant(X86CC, MVT::i8),
10862 SDValue(PCMP.getNode(), 1));
10863 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10864 }
Craig Topper6d688152012-08-14 07:43:25 +000010865
Craig Topper4feb6472012-08-06 06:22:36 +000010866 case Intrinsic::x86_sse42_pcmpistri128:
10867 case Intrinsic::x86_sse42_pcmpestri128: {
10868 unsigned Opcode;
10869 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10870 Opcode = X86ISD::PCMPISTRI;
10871 else
10872 Opcode = X86ISD::PCMPESTRI;
10873
10874 SmallVector<SDValue, 5> NewOps;
10875 NewOps.append(Op->op_begin()+1, Op->op_end());
10876 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10877 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10878 }
Craig Topper0e292372012-08-24 04:03:22 +000010879 case Intrinsic::x86_fma_vfmadd_ps:
10880 case Intrinsic::x86_fma_vfmadd_pd:
10881 case Intrinsic::x86_fma_vfmsub_ps:
10882 case Intrinsic::x86_fma_vfmsub_pd:
10883 case Intrinsic::x86_fma_vfnmadd_ps:
10884 case Intrinsic::x86_fma_vfnmadd_pd:
10885 case Intrinsic::x86_fma_vfnmsub_ps:
10886 case Intrinsic::x86_fma_vfnmsub_pd:
10887 case Intrinsic::x86_fma_vfmaddsub_ps:
10888 case Intrinsic::x86_fma_vfmaddsub_pd:
10889 case Intrinsic::x86_fma_vfmsubadd_ps:
10890 case Intrinsic::x86_fma_vfmsubadd_pd:
10891 case Intrinsic::x86_fma_vfmadd_ps_256:
10892 case Intrinsic::x86_fma_vfmadd_pd_256:
10893 case Intrinsic::x86_fma_vfmsub_ps_256:
10894 case Intrinsic::x86_fma_vfmsub_pd_256:
10895 case Intrinsic::x86_fma_vfnmadd_ps_256:
10896 case Intrinsic::x86_fma_vfnmadd_pd_256:
10897 case Intrinsic::x86_fma_vfnmsub_ps_256:
10898 case Intrinsic::x86_fma_vfnmsub_pd_256:
10899 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10900 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10901 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10902 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010903 unsigned Opc;
10904 switch (IntNo) {
10905 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10906 case Intrinsic::x86_fma_vfmadd_ps:
10907 case Intrinsic::x86_fma_vfmadd_pd:
10908 case Intrinsic::x86_fma_vfmadd_ps_256:
10909 case Intrinsic::x86_fma_vfmadd_pd_256:
10910 Opc = X86ISD::FMADD;
10911 break;
10912 case Intrinsic::x86_fma_vfmsub_ps:
10913 case Intrinsic::x86_fma_vfmsub_pd:
10914 case Intrinsic::x86_fma_vfmsub_ps_256:
10915 case Intrinsic::x86_fma_vfmsub_pd_256:
10916 Opc = X86ISD::FMSUB;
10917 break;
10918 case Intrinsic::x86_fma_vfnmadd_ps:
10919 case Intrinsic::x86_fma_vfnmadd_pd:
10920 case Intrinsic::x86_fma_vfnmadd_ps_256:
10921 case Intrinsic::x86_fma_vfnmadd_pd_256:
10922 Opc = X86ISD::FNMADD;
10923 break;
10924 case Intrinsic::x86_fma_vfnmsub_ps:
10925 case Intrinsic::x86_fma_vfnmsub_pd:
10926 case Intrinsic::x86_fma_vfnmsub_ps_256:
10927 case Intrinsic::x86_fma_vfnmsub_pd_256:
10928 Opc = X86ISD::FNMSUB;
10929 break;
10930 case Intrinsic::x86_fma_vfmaddsub_ps:
10931 case Intrinsic::x86_fma_vfmaddsub_pd:
10932 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10933 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10934 Opc = X86ISD::FMADDSUB;
10935 break;
10936 case Intrinsic::x86_fma_vfmsubadd_ps:
10937 case Intrinsic::x86_fma_vfmsubadd_pd:
10938 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10939 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10940 Opc = X86ISD::FMSUBADD;
10941 break;
10942 }
10943
10944 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10945 Op.getOperand(2), Op.getOperand(3));
10946 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010947 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010948}
Evan Cheng72261582005-12-20 06:22:03 +000010949
Craig Topper55b24052012-09-11 06:15:32 +000010950static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010951 DebugLoc dl = Op.getDebugLoc();
10952 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10953 switch (IntNo) {
10954 default: return SDValue(); // Don't custom lower most intrinsics.
10955
Michael Liaoc26392a2013-03-28 23:41:26 +000010956 // RDRAND/RDSEED intrinsics.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010957 case Intrinsic::x86_rdrand_16:
10958 case Intrinsic::x86_rdrand_32:
Michael Liaoc26392a2013-03-28 23:41:26 +000010959 case Intrinsic::x86_rdrand_64:
10960 case Intrinsic::x86_rdseed_16:
10961 case Intrinsic::x86_rdseed_32:
10962 case Intrinsic::x86_rdseed_64: {
10963 unsigned Opcode = (IntNo == Intrinsic::x86_rdseed_16 ||
10964 IntNo == Intrinsic::x86_rdseed_32 ||
10965 IntNo == Intrinsic::x86_rdseed_64) ? X86ISD::RDSEED :
10966 X86ISD::RDRAND;
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010967 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010968 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
Michael Liaoc26392a2013-03-28 23:41:26 +000010969 SDValue Result = DAG.getNode(Opcode, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010970
Michael Liaoc26392a2013-03-28 23:41:26 +000010971 // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1.
10972 // Otherwise return the value from Rand, which is always 0, casted to i32.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010973 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10974 DAG.getConstant(1, Op->getValueType(1)),
10975 DAG.getConstant(X86::COND_B, MVT::i32),
10976 SDValue(Result.getNode(), 1) };
10977 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10978 DAG.getVTList(Op->getValueType(1), MVT::Glue),
Michael Liao0ee17002013-04-19 04:03:37 +000010979 Ops, array_lengthof(Ops));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010980
10981 // Return { result, isValid, chain }.
10982 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010983 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010984 }
Michael Liaof8fd8832013-03-26 22:47:01 +000010985
10986 // XTEST intrinsics.
10987 case Intrinsic::x86_xtest: {
10988 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
10989 SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0));
10990 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10991 DAG.getConstant(X86::COND_NE, MVT::i8),
10992 InTrans);
10993 SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
10994 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
10995 Ret, SDValue(InTrans.getNode(), 1));
10996 }
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010997 }
10998}
10999
Dan Gohmand858e902010-04-17 15:26:15 +000011000SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
11001 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000011002 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11003 MFI->setReturnAddressIsTaken(true);
11004
Bill Wendling64e87322009-01-16 19:25:27 +000011005 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011006 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011007 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000011008
11009 if (Depth > 0) {
11010 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
11011 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011012 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
11013 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
11014 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000011015 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011016 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000011017 }
11018
11019 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000011020 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011021 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011022 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011023}
11024
Dan Gohmand858e902010-04-17 15:26:15 +000011025SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000011026 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11027 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000011028
Owen Andersone50ed302009-08-10 22:56:29 +000011029 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011030 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000011031 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Michael Liaob9cca132013-05-02 08:21:56 +000011032 unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
11033 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
Michael Liao299eb2e2013-05-02 09:22:04 +000011034 (FrameReg == X86::EBP && VT == MVT::i32)) &&
11035 "Invalid Frame Register!");
Dale Johannesendd64c412009-02-04 00:33:20 +000011036 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000011037 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000011038 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
11039 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011040 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000011041 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000011042}
11043
Dan Gohman475871a2008-07-27 21:46:04 +000011044SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011045 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011046 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011047}
11048
Dan Gohmand858e902010-04-17 15:26:15 +000011049SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011050 SDValue Chain = Op.getOperand(0);
11051 SDValue Offset = Op.getOperand(1);
11052 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011053 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011054
Michael Liaodb7da202013-05-02 09:18:38 +000011055 EVT PtrVT = getPointerTy();
11056 unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
11057 assert(((FrameReg == X86::RBP && PtrVT == MVT::i64) ||
11058 (FrameReg == X86::EBP && PtrVT == MVT::i32)) &&
11059 "Invalid Frame Register!");
11060 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, PtrVT);
11061 unsigned StoreAddrReg = (PtrVT == MVT::i64) ? X86::RCX : X86::ECX;
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011062
Michael Liaodb7da202013-05-02 09:18:38 +000011063 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, Frame,
Michael Liao299eb2e2013-05-02 09:22:04 +000011064 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Michael Liaodb7da202013-05-02 09:18:38 +000011065 StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011066 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11067 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011068 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011069
Michael Liaodb7da202013-05-02 09:18:38 +000011070 return DAG.getNode(X86ISD::EH_RETURN, dl, MVT::Other, Chain,
11071 DAG.getRegister(StoreAddrReg, PtrVT));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011072}
11073
Michael Liao6c0e04c2012-10-15 22:39:43 +000011074SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11075 SelectionDAG &DAG) const {
11076 DebugLoc DL = Op.getDebugLoc();
11077 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11078 DAG.getVTList(MVT::i32, MVT::Other),
11079 Op.getOperand(0), Op.getOperand(1));
11080}
11081
11082SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11083 SelectionDAG &DAG) const {
11084 DebugLoc DL = Op.getDebugLoc();
11085 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11086 Op.getOperand(0), Op.getOperand(1));
11087}
11088
Craig Topper55b24052012-09-11 06:15:32 +000011089static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011090 return Op.getOperand(0);
11091}
11092
11093SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11094 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011095 SDValue Root = Op.getOperand(0);
11096 SDValue Trmp = Op.getOperand(1); // trampoline
11097 SDValue FPtr = Op.getOperand(2); // nested function
11098 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011099 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011100
Dan Gohman69de1932008-02-06 22:27:42 +000011101 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011102 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011103
11104 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011105 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011106
11107 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011108 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11109 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011110
Michael Liao7abf67a2012-10-04 19:50:43 +000011111 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11112 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011113
11114 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11115
11116 // Load the pointer to the nested function into R11.
11117 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011118 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011119 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011120 Addr, MachinePointerInfo(TrmpAddr),
11121 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011122
Owen Anderson825b72b2009-08-11 20:47:22 +000011123 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11124 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011125 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11126 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011127 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011128
11129 // Load the 'nest' parameter value into R10.
11130 // R10 is specified in X86CallingConv.td
11131 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011132 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11133 DAG.getConstant(10, MVT::i64));
11134 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011135 Addr, MachinePointerInfo(TrmpAddr, 10),
11136 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011137
Owen Anderson825b72b2009-08-11 20:47:22 +000011138 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11139 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011140 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11141 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011142 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011143
11144 // Jump to the nested function.
11145 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011146 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11147 DAG.getConstant(20, MVT::i64));
11148 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011149 Addr, MachinePointerInfo(TrmpAddr, 20),
11150 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011151
11152 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011153 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11154 DAG.getConstant(22, MVT::i64));
11155 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011156 MachinePointerInfo(TrmpAddr, 22),
11157 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011158
Duncan Sands4a544a72011-09-06 13:37:06 +000011159 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011160 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011161 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011162 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011163 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011164 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011165
11166 switch (CC) {
11167 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011168 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011169 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011170 case CallingConv::X86_StdCall: {
11171 // Pass 'nest' parameter in ECX.
11172 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011173 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011174
11175 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011176 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011177 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011178
Chris Lattner58d74912008-03-12 17:45:29 +000011179 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011180 unsigned InRegCount = 0;
11181 unsigned Idx = 1;
11182
11183 for (FunctionType::param_iterator I = FTy->param_begin(),
11184 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011185 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011186 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011187 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011188
11189 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011190 report_fatal_error("Nest register in use - reduce number of inreg"
11191 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011192 }
11193 }
11194 break;
11195 }
11196 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011197 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011198 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011199 // Pass 'nest' parameter in EAX.
11200 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011201 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011202 break;
11203 }
11204
Dan Gohman475871a2008-07-27 21:46:04 +000011205 SDValue OutChains[4];
11206 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011207
Owen Anderson825b72b2009-08-11 20:47:22 +000011208 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11209 DAG.getConstant(10, MVT::i32));
11210 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011211
Chris Lattnera62fe662010-02-05 19:20:30 +000011212 // This is storing the opcode for MOV32ri.
11213 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011214 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011215 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011216 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011217 Trmp, MachinePointerInfo(TrmpAddr),
11218 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011219
Owen Anderson825b72b2009-08-11 20:47:22 +000011220 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11221 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011222 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11223 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011224 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011225
Chris Lattnera62fe662010-02-05 19:20:30 +000011226 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011227 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11228 DAG.getConstant(5, MVT::i32));
11229 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011230 MachinePointerInfo(TrmpAddr, 5),
11231 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011232
Owen Anderson825b72b2009-08-11 20:47:22 +000011233 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11234 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011235 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11236 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011237 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011238
Duncan Sands4a544a72011-09-06 13:37:06 +000011239 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011240 }
11241}
11242
Dan Gohmand858e902010-04-17 15:26:15 +000011243SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11244 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011245 /*
11246 The rounding mode is in bits 11:10 of FPSR, and has the following
11247 settings:
11248 00 Round to nearest
11249 01 Round to -inf
11250 10 Round to +inf
11251 11 Round to 0
11252
11253 FLT_ROUNDS, on the other hand, expects the following:
11254 -1 Undefined
11255 0 Round to 0
11256 1 Round to nearest
11257 2 Round to +inf
11258 3 Round to -inf
11259
11260 To perform the conversion, we do:
11261 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11262 */
11263
11264 MachineFunction &MF = DAG.getMachineFunction();
11265 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011266 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011267 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011268 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011269 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011270
11271 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011272 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011273 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011274
Chris Lattner2156b792010-09-22 01:11:26 +000011275 MachineMemOperand *MMO =
11276 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11277 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011278
Chris Lattner2156b792010-09-22 01:11:26 +000011279 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11280 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11281 DAG.getVTList(MVT::Other),
Michael Liao0ee17002013-04-19 04:03:37 +000011282 Ops, array_lengthof(Ops), MVT::i16,
11283 MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011284
11285 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011286 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011287 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011288
11289 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011290 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011291 DAG.getNode(ISD::SRL, DL, MVT::i16,
11292 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011293 CWD, DAG.getConstant(0x800, MVT::i16)),
11294 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011295 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011296 DAG.getNode(ISD::SRL, DL, MVT::i16,
11297 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011298 CWD, DAG.getConstant(0x400, MVT::i16)),
11299 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011300
Dan Gohman475871a2008-07-27 21:46:04 +000011301 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011302 DAG.getNode(ISD::AND, DL, MVT::i16,
11303 DAG.getNode(ISD::ADD, DL, MVT::i16,
11304 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011305 DAG.getConstant(1, MVT::i16)),
11306 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011307
Duncan Sands83ec4b62008-06-06 12:08:01 +000011308 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011309 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011310}
11311
Craig Topper55b24052012-09-11 06:15:32 +000011312static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011313 EVT VT = Op.getValueType();
11314 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011315 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011316 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011317
11318 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011319 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011320 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011321 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011322 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011323 }
Evan Cheng18efe262007-12-14 02:13:44 +000011324
Evan Cheng152804e2007-12-14 08:30:15 +000011325 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011326 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011327 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011328
11329 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011330 SDValue Ops[] = {
11331 Op,
11332 DAG.getConstant(NumBits+NumBits-1, OpVT),
11333 DAG.getConstant(X86::COND_E, MVT::i8),
11334 Op.getValue(1)
11335 };
11336 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011337
11338 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011339 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011340
Owen Anderson825b72b2009-08-11 20:47:22 +000011341 if (VT == MVT::i8)
11342 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011343 return Op;
11344}
11345
Craig Topper55b24052012-09-11 06:15:32 +000011346static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011347 EVT VT = Op.getValueType();
11348 EVT OpVT = VT;
11349 unsigned NumBits = VT.getSizeInBits();
11350 DebugLoc dl = Op.getDebugLoc();
11351
11352 Op = Op.getOperand(0);
11353 if (VT == MVT::i8) {
11354 // Zero extend to i32 since there is not an i8 bsr.
11355 OpVT = MVT::i32;
11356 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11357 }
11358
11359 // Issue a bsr (scan bits in reverse).
11360 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11361 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11362
11363 // And xor with NumBits-1.
11364 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11365
11366 if (VT == MVT::i8)
11367 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11368 return Op;
11369}
11370
Craig Topper55b24052012-09-11 06:15:32 +000011371static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011372 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011373 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011374 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011375 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011376
11377 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011378 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011379 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011380
11381 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011382 SDValue Ops[] = {
11383 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011384 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011385 DAG.getConstant(X86::COND_E, MVT::i8),
11386 Op.getValue(1)
11387 };
Chandler Carruth77821022011-12-24 12:12:34 +000011388 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011389}
11390
Craig Topper13894fa2011-08-24 06:14:18 +000011391// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11392// ones, and then concatenate the result back.
11393static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011394 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011395
Craig Topper7a9a28b2012-08-12 02:23:29 +000011396 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011397 "Unsupported value type for operation");
11398
Craig Topper66ddd152012-04-27 22:54:43 +000011399 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011400 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011401
11402 // Extract the LHS vectors
11403 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011404 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11405 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011406
11407 // Extract the RHS vectors
11408 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011409 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11410 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011411
11412 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11413 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11414
11415 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11416 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11417 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11418}
11419
Craig Topper55b24052012-09-11 06:15:32 +000011420static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011421 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011422 Op.getValueType().isInteger() &&
11423 "Only handle AVX 256-bit vector integer operation");
11424 return Lower256IntArith(Op, DAG);
11425}
11426
Craig Topper55b24052012-09-11 06:15:32 +000011427static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011428 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011429 Op.getValueType().isInteger() &&
11430 "Only handle AVX 256-bit vector integer operation");
11431 return Lower256IntArith(Op, DAG);
11432}
11433
Craig Topper55b24052012-09-11 06:15:32 +000011434static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11435 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011436 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011437 EVT VT = Op.getValueType();
11438
11439 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011440 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011441 return Lower256IntArith(Op, DAG);
11442
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011443 SDValue A = Op.getOperand(0);
11444 SDValue B = Op.getOperand(1);
11445
11446 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11447 if (VT == MVT::v4i32) {
11448 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11449 "Should not custom lower when pmuldq is available!");
11450
11451 // Extract the odd parts.
11452 const int UnpackMask[] = { 1, -1, 3, -1 };
11453 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11454 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11455
11456 // Multiply the even parts.
11457 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11458 // Now multiply odd parts.
11459 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11460
11461 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11462 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11463
11464 // Merge the two vectors back together with a shuffle. This expands into 2
11465 // shuffles.
11466 const int ShufMask[] = { 0, 4, 2, 6 };
11467 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11468 }
11469
Craig Topper5b209e82012-02-05 03:14:49 +000011470 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11471 "Only know how to lower V2I64/V4I64 multiply");
11472
Craig Topper5b209e82012-02-05 03:14:49 +000011473 // Ahi = psrlqi(a, 32);
11474 // Bhi = psrlqi(b, 32);
11475 //
11476 // AloBlo = pmuludq(a, b);
11477 // AloBhi = pmuludq(a, Bhi);
11478 // AhiBlo = pmuludq(Ahi, b);
11479
11480 // AloBhi = psllqi(AloBhi, 32);
11481 // AhiBlo = psllqi(AhiBlo, 32);
11482 // return AloBlo + AloBhi + AhiBlo;
11483
Craig Topper5b209e82012-02-05 03:14:49 +000011484 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011485
Craig Topper5b209e82012-02-05 03:14:49 +000011486 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11487 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011488
Craig Topper5b209e82012-02-05 03:14:49 +000011489 // Bit cast to 32-bit vectors for MULUDQ
11490 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11491 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11492 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11493 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11494 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011495
Craig Topper5b209e82012-02-05 03:14:49 +000011496 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11497 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11498 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011499
Craig Topper5b209e82012-02-05 03:14:49 +000011500 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11501 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011502
Dale Johannesene4d209d2009-02-03 20:21:25 +000011503 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011504 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011505}
11506
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011507SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11508 EVT VT = Op.getValueType();
11509 EVT EltTy = VT.getVectorElementType();
11510 unsigned NumElts = VT.getVectorNumElements();
11511 SDValue N0 = Op.getOperand(0);
11512 DebugLoc dl = Op.getDebugLoc();
11513
11514 // Lower sdiv X, pow2-const.
11515 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11516 if (!C)
11517 return SDValue();
11518
11519 APInt SplatValue, SplatUndef;
11520 unsigned MinSplatBits;
11521 bool HasAnyUndefs;
11522 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11523 return SDValue();
11524
11525 if ((SplatValue != 0) &&
11526 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11527 unsigned lg2 = SplatValue.countTrailingZeros();
11528 // Splat the sign bit.
11529 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11530 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11531 // Add (N0 < 0) ? abs2 - 1 : 0;
11532 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11533 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11534 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11535 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11536 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11537
11538 // If we're dividing by a positive value, we're done. Otherwise, we must
11539 // negate the result.
11540 if (SplatValue.isNonNegative())
11541 return SRA;
11542
11543 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11544 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11545 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11546 }
11547 return SDValue();
11548}
11549
Michael Liao4b7ab122013-03-20 02:20:36 +000011550static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11551 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011552 EVT VT = Op.getValueType();
11553 DebugLoc dl = Op.getDebugLoc();
11554 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011555 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011556
Nadav Rotem43012222011-05-11 08:12:09 +000011557 // Optimize shl/srl/sra with constant shift amount.
11558 if (isSplatVector(Amt.getNode())) {
11559 SDValue SclrAmt = Amt->getOperand(0);
11560 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11561 uint64_t ShiftAmt = C->getZExtValue();
11562
Craig Toppered2e13d2012-01-22 19:15:14 +000011563 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011564 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011565 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11566 if (Op.getOpcode() == ISD::SHL)
11567 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11568 DAG.getConstant(ShiftAmt, MVT::i32));
11569 if (Op.getOpcode() == ISD::SRL)
11570 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11571 DAG.getConstant(ShiftAmt, MVT::i32));
11572 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11573 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11574 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011575 }
11576
Craig Toppered2e13d2012-01-22 19:15:14 +000011577 if (VT == MVT::v16i8) {
11578 if (Op.getOpcode() == ISD::SHL) {
11579 // Make a large shift.
11580 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11581 DAG.getConstant(ShiftAmt, MVT::i32));
11582 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11583 // Zero out the rightmost bits.
11584 SmallVector<SDValue, 16> V(16,
11585 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11586 MVT::i8));
11587 return DAG.getNode(ISD::AND, dl, VT, SHL,
11588 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011589 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011590 if (Op.getOpcode() == ISD::SRL) {
11591 // Make a large shift.
11592 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11593 DAG.getConstant(ShiftAmt, MVT::i32));
11594 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11595 // Zero out the leftmost bits.
11596 SmallVector<SDValue, 16> V(16,
11597 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11598 MVT::i8));
11599 return DAG.getNode(ISD::AND, dl, VT, SRL,
11600 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11601 }
11602 if (Op.getOpcode() == ISD::SRA) {
11603 if (ShiftAmt == 7) {
11604 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011605 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011606 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011607 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011608
Craig Toppered2e13d2012-01-22 19:15:14 +000011609 // R s>> a === ((R u>> a) ^ m) - m
11610 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11611 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11612 MVT::i8));
11613 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11614 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11615 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11616 return Res;
11617 }
Craig Topper731dfd02012-04-23 03:42:40 +000011618 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011619 }
Craig Topper46154eb2011-11-11 07:39:23 +000011620
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011621 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011622 if (Op.getOpcode() == ISD::SHL) {
11623 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011624 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11625 DAG.getConstant(ShiftAmt, MVT::i32));
11626 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011627 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011628 SmallVector<SDValue, 32> V(32,
11629 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11630 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011631 return DAG.getNode(ISD::AND, dl, VT, SHL,
11632 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011633 }
Craig Topper0d86d462011-11-20 00:12:05 +000011634 if (Op.getOpcode() == ISD::SRL) {
11635 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011636 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11637 DAG.getConstant(ShiftAmt, MVT::i32));
11638 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011639 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011640 SmallVector<SDValue, 32> V(32,
11641 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11642 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011643 return DAG.getNode(ISD::AND, dl, VT, SRL,
11644 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11645 }
11646 if (Op.getOpcode() == ISD::SRA) {
11647 if (ShiftAmt == 7) {
11648 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011649 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011650 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011651 }
11652
11653 // R s>> a === ((R u>> a) ^ m) - m
11654 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11655 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11656 MVT::i8));
11657 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11658 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11659 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11660 return Res;
11661 }
Craig Topper731dfd02012-04-23 03:42:40 +000011662 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011663 }
Nadav Rotem43012222011-05-11 08:12:09 +000011664 }
11665 }
11666
Michael Liao42317cc2013-03-20 02:33:21 +000011667 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11668 if (!Subtarget->is64Bit() &&
11669 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11670 Amt.getOpcode() == ISD::BITCAST &&
11671 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11672 Amt = Amt.getOperand(0);
11673 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11674 VT.getVectorNumElements();
11675 unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
11676 uint64_t ShiftAmt = 0;
11677 for (unsigned i = 0; i != Ratio; ++i) {
11678 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i));
11679 if (C == 0)
11680 return SDValue();
11681 // 6 == Log2(64)
11682 ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
11683 }
11684 // Check remaining shift amounts.
11685 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11686 uint64_t ShAmt = 0;
11687 for (unsigned j = 0; j != Ratio; ++j) {
11688 ConstantSDNode *C =
11689 dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
11690 if (C == 0)
11691 return SDValue();
11692 // 6 == Log2(64)
11693 ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
11694 }
11695 if (ShAmt != ShiftAmt)
11696 return SDValue();
11697 }
11698 switch (Op.getOpcode()) {
11699 default:
11700 llvm_unreachable("Unknown shift opcode!");
11701 case ISD::SHL:
11702 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11703 DAG.getConstant(ShiftAmt, MVT::i32));
11704 case ISD::SRL:
11705 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11706 DAG.getConstant(ShiftAmt, MVT::i32));
11707 case ISD::SRA:
11708 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11709 DAG.getConstant(ShiftAmt, MVT::i32));
11710 }
11711 }
11712
11713 return SDValue();
11714}
11715
11716static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
11717 const X86Subtarget* Subtarget) {
11718 EVT VT = Op.getValueType();
11719 DebugLoc dl = Op.getDebugLoc();
11720 SDValue R = Op.getOperand(0);
11721 SDValue Amt = Op.getOperand(1);
11722
11723 if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) ||
11724 VT == MVT::v4i32 || VT == MVT::v8i16 ||
11725 (Subtarget->hasInt256() &&
11726 ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) ||
11727 VT == MVT::v8i32 || VT == MVT::v16i16))) {
11728 SDValue BaseShAmt;
11729 EVT EltVT = VT.getVectorElementType();
11730
11731 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11732 unsigned NumElts = VT.getVectorNumElements();
11733 unsigned i, j;
11734 for (i = 0; i != NumElts; ++i) {
11735 if (Amt.getOperand(i).getOpcode() == ISD::UNDEF)
11736 continue;
11737 break;
11738 }
11739 for (j = i; j != NumElts; ++j) {
11740 SDValue Arg = Amt.getOperand(j);
11741 if (Arg.getOpcode() == ISD::UNDEF) continue;
11742 if (Arg != Amt.getOperand(i))
11743 break;
11744 }
11745 if (i != NumElts && j == NumElts)
11746 BaseShAmt = Amt.getOperand(i);
11747 } else {
11748 if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11749 Amt = Amt.getOperand(0);
11750 if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE &&
11751 cast<ShuffleVectorSDNode>(Amt)->isSplat()) {
11752 SDValue InVec = Amt.getOperand(0);
11753 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11754 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11755 unsigned i = 0;
11756 for (; i != NumElts; ++i) {
11757 SDValue Arg = InVec.getOperand(i);
11758 if (Arg.getOpcode() == ISD::UNDEF) continue;
11759 BaseShAmt = Arg;
11760 break;
11761 }
11762 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11763 if (ConstantSDNode *C =
11764 dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
11765 unsigned SplatIdx =
11766 cast<ShuffleVectorSDNode>(Amt)->getSplatIndex();
11767 if (C->getZExtValue() == SplatIdx)
11768 BaseShAmt = InVec.getOperand(1);
11769 }
11770 }
11771 if (BaseShAmt.getNode() == 0)
11772 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt,
11773 DAG.getIntPtrConstant(0));
11774 }
11775 }
11776
11777 if (BaseShAmt.getNode()) {
11778 if (EltVT.bitsGT(MVT::i32))
11779 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
11780 else if (EltVT.bitsLT(MVT::i32))
11781 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
11782
11783 switch (Op.getOpcode()) {
11784 default:
11785 llvm_unreachable("Unknown shift opcode!");
11786 case ISD::SHL:
11787 switch (VT.getSimpleVT().SimpleTy) {
11788 default: return SDValue();
11789 case MVT::v2i64:
11790 case MVT::v4i32:
11791 case MVT::v8i16:
11792 case MVT::v4i64:
11793 case MVT::v8i32:
11794 case MVT::v16i16:
11795 return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG);
11796 }
11797 case ISD::SRA:
11798 switch (VT.getSimpleVT().SimpleTy) {
11799 default: return SDValue();
11800 case MVT::v4i32:
11801 case MVT::v8i16:
11802 case MVT::v8i32:
11803 case MVT::v16i16:
11804 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG);
11805 }
11806 case ISD::SRL:
11807 switch (VT.getSimpleVT().SimpleTy) {
11808 default: return SDValue();
11809 case MVT::v2i64:
11810 case MVT::v4i32:
11811 case MVT::v8i16:
11812 case MVT::v4i64:
11813 case MVT::v8i32:
11814 case MVT::v16i16:
11815 return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG);
11816 }
11817 }
11818 }
11819 }
11820
11821 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11822 if (!Subtarget->is64Bit() &&
11823 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11824 Amt.getOpcode() == ISD::BITCAST &&
11825 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11826 Amt = Amt.getOperand(0);
11827 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11828 VT.getVectorNumElements();
11829 std::vector<SDValue> Vals(Ratio);
11830 for (unsigned i = 0; i != Ratio; ++i)
11831 Vals[i] = Amt.getOperand(i);
11832 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11833 for (unsigned j = 0; j != Ratio; ++j)
11834 if (Vals[j] != Amt.getOperand(i + j))
11835 return SDValue();
11836 }
11837 switch (Op.getOpcode()) {
11838 default:
11839 llvm_unreachable("Unknown shift opcode!");
11840 case ISD::SHL:
11841 return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1));
11842 case ISD::SRL:
11843 return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1));
11844 case ISD::SRA:
11845 return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1));
11846 }
11847 }
11848
Michael Liao4b7ab122013-03-20 02:20:36 +000011849 return SDValue();
11850}
11851
11852SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11853
11854 EVT VT = Op.getValueType();
11855 DebugLoc dl = Op.getDebugLoc();
11856 SDValue R = Op.getOperand(0);
11857 SDValue Amt = Op.getOperand(1);
11858 SDValue V;
11859
11860 if (!Subtarget->hasSSE2())
11861 return SDValue();
11862
11863 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
11864 if (V.getNode())
11865 return V;
11866
Michael Liao42317cc2013-03-20 02:33:21 +000011867 V = LowerScalarVariableShift(Op, DAG, Subtarget);
11868 if (V.getNode())
11869 return V;
11870
Michael Liao5c5f1902013-03-20 02:28:20 +000011871 // AVX2 has VPSLLV/VPSRAV/VPSRLV.
11872 if (Subtarget->hasInt256()) {
11873 if (Op.getOpcode() == ISD::SRL &&
11874 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11875 VT == MVT::v4i64 || VT == MVT::v8i32))
11876 return Op;
11877 if (Op.getOpcode() == ISD::SHL &&
11878 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11879 VT == MVT::v4i64 || VT == MVT::v8i32))
11880 return Op;
11881 if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32))
11882 return Op;
11883 }
11884
Nadav Rotem43012222011-05-11 08:12:09 +000011885 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011886 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011887 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011888
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011889 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011890 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011891 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11892 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11893 }
Nadav Rotem43012222011-05-11 08:12:09 +000011894 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011895 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011896
Nate Begeman51409212010-07-28 00:21:48 +000011897 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011898 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011899 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011900
Lang Hames8b99c1e2011-12-17 01:08:46 +000011901 // Turn 'a' into a mask suitable for VSELECT
11902 SDValue VSelM = DAG.getConstant(0x80, VT);
11903 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011904 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011905
Lang Hames8b99c1e2011-12-17 01:08:46 +000011906 SDValue CM1 = DAG.getConstant(0x0f, VT);
11907 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011908
Lang Hames8b99c1e2011-12-17 01:08:46 +000011909 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11910 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011911 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11912 DAG.getConstant(4, MVT::i32), DAG);
11913 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011914 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11915
Nate Begeman51409212010-07-28 00:21:48 +000011916 // a += a
11917 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011918 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011919 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011920
Lang Hames8b99c1e2011-12-17 01:08:46 +000011921 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11922 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011923 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11924 DAG.getConstant(2, MVT::i32), DAG);
11925 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011926 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11927
Nate Begeman51409212010-07-28 00:21:48 +000011928 // a += a
11929 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011930 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011931 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011932
Lang Hames8b99c1e2011-12-17 01:08:46 +000011933 // return VSELECT(r, r+r, a);
11934 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011935 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011936 return R;
11937 }
Craig Topper46154eb2011-11-11 07:39:23 +000011938
11939 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011940 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011941 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011942 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11943 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11944
11945 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011946 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11947 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011948
11949 // Recreate the shift amount vectors
11950 SDValue Amt1, Amt2;
11951 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11952 // Constant shift amount
11953 SmallVector<SDValue, 4> Amt1Csts;
11954 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011955 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011956 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011957 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011958 Amt2Csts.push_back(Amt->getOperand(i));
11959
11960 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11961 &Amt1Csts[0], NumElems/2);
11962 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11963 &Amt2Csts[0], NumElems/2);
11964 } else {
11965 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011966 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11967 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011968 }
11969
11970 // Issue new vector shifts for the smaller types
11971 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11972 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11973
11974 // Concatenate the result back
11975 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11976 }
11977
Nate Begeman51409212010-07-28 00:21:48 +000011978 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011979}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011980
Craig Topper55b24052012-09-11 06:15:32 +000011981static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011982 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11983 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011984 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11985 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011986 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011987 SDValue LHS = N->getOperand(0);
11988 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011989 unsigned BaseOp = 0;
11990 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011991 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011992 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011993 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011994 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011995 // A subtract of one will be selected as a INC. Note that INC doesn't
11996 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011997 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11998 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011999 BaseOp = X86ISD::INC;
12000 Cond = X86::COND_O;
12001 break;
12002 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012003 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000012004 Cond = X86::COND_O;
12005 break;
12006 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012007 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000012008 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000012009 break;
12010 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000012011 // A subtract of one will be selected as a DEC. Note that DEC doesn't
12012 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000012013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
12014 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000012015 BaseOp = X86ISD::DEC;
12016 Cond = X86::COND_O;
12017 break;
12018 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012019 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000012020 Cond = X86::COND_O;
12021 break;
12022 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012023 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000012024 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000012025 break;
12026 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000012027 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000012028 Cond = X86::COND_O;
12029 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012030 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
12031 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
12032 MVT::i32);
12033 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012034
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012035 SDValue SetCC =
12036 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12037 DAG.getConstant(X86::COND_O, MVT::i32),
12038 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012039
Dan Gohman6e5fda22011-07-22 18:45:15 +000012040 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012041 }
Bill Wendling74c37652008-12-09 22:08:41 +000012042 }
Bill Wendling3fafd932008-11-26 22:37:40 +000012043
Bill Wendling61edeb52008-12-02 01:06:39 +000012044 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000012045 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012046 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000012047
Bill Wendling61edeb52008-12-02 01:06:39 +000012048 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012049 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
12050 DAG.getConstant(Cond, MVT::i32),
12051 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000012052
Dan Gohman6e5fda22011-07-22 18:45:15 +000012053 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000012054}
12055
Chad Rosier30450e82011-12-22 22:35:21 +000012056SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
12057 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012058 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000012059 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
12060 EVT VT = Op.getValueType();
12061
Craig Toppered2e13d2012-01-22 19:15:14 +000012062 if (!Subtarget->hasSSE2() || !VT.isVector())
12063 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012064
Craig Toppered2e13d2012-01-22 19:15:14 +000012065 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
12066 ExtraVT.getScalarType().getSizeInBits();
12067 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
12068
12069 switch (VT.getSimpleVT().SimpleTy) {
12070 default: return SDValue();
12071 case MVT::v8i32:
12072 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012073 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012074 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012075 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012076 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000012077 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000012078
Craig Toppered2e13d2012-01-22 19:15:14 +000012079 // Extract the LHS vectors
12080 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000012081 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
12082 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000012083
Craig Toppered2e13d2012-01-22 19:15:14 +000012084 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12085 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000012086
Craig Toppered2e13d2012-01-22 19:15:14 +000012087 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000012088 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000012089 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
12090 ExtraNumElems/2);
12091 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000012092
Craig Toppered2e13d2012-01-22 19:15:14 +000012093 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
12094 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000012095
Dmitri Gribenko2de05722012-09-10 21:26:47 +000012096 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012097 }
12098 // fall through
12099 case MVT::v4i32:
12100 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000012101 // (sext (vzext x)) -> (vsext x)
12102 SDValue Op0 = Op.getOperand(0);
12103 SDValue Op00 = Op0.getOperand(0);
12104 SDValue Tmp1;
12105 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
12106 if (Op0.getOpcode() == ISD::BITCAST &&
12107 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
12108 Tmp1 = LowerVectorIntExtend(Op00, DAG);
12109 if (Tmp1.getNode()) {
12110 SDValue Tmp1Op0 = Tmp1.getOperand(0);
12111 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
12112 "This optimization is invalid without a VZEXT.");
12113 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
12114 }
12115
12116 // If the above didn't work, then just use Shift-Left + Shift-Right.
12117 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000012118 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012119 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012120 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012121}
12122
Craig Topper55b24052012-09-11 06:15:32 +000012123static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
12124 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000012125 DebugLoc dl = Op.getDebugLoc();
12126 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
12127 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
12128 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
12129 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
12130
12131 // The only fence that needs an instruction is a sequentially-consistent
12132 // cross-thread fence.
12133 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
12134 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
12135 // no-sse2). There isn't any reason to disable it if the target processor
12136 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012137 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000012138 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
12139
12140 SDValue Chain = Op.getOperand(0);
12141 SDValue Zero = DAG.getConstant(0, MVT::i32);
12142 SDValue Ops[] = {
12143 DAG.getRegister(X86::ESP, MVT::i32), // Base
12144 DAG.getTargetConstant(1, MVT::i8), // Scale
12145 DAG.getRegister(0, MVT::i32), // Index
12146 DAG.getTargetConstant(0, MVT::i32), // Disp
12147 DAG.getRegister(0, MVT::i32), // Segment.
12148 Zero,
12149 Chain
12150 };
Michael Liao2a8bea72013-04-19 22:22:57 +000012151 SDNode *Res = DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops);
Eli Friedman14648462011-07-27 22:21:52 +000012152 return SDValue(Res, 0);
12153 }
12154
12155 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
12156 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
12157}
12158
Craig Topper55b24052012-09-11 06:15:32 +000012159static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
12160 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000012161 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012162 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000012163 unsigned Reg = 0;
12164 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000012165 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000012166 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000012167 case MVT::i8: Reg = X86::AL; size = 1; break;
12168 case MVT::i16: Reg = X86::AX; size = 2; break;
12169 case MVT::i32: Reg = X86::EAX; size = 4; break;
12170 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000012171 assert(Subtarget->is64Bit() && "Node not type legal!");
12172 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000012173 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000012174 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012175 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000012176 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000012177 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012178 Op.getOperand(1),
12179 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000012180 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012181 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012182 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012183 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
12184 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
Michael Liao0ee17002013-04-19 04:03:37 +000012185 Ops, array_lengthof(Ops), T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000012186 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012187 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000012188 return cpOut;
12189}
12190
Craig Topper55b24052012-09-11 06:15:32 +000012191static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
12192 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000012193 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012194 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012195 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000012196 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012197 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012198 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
12199 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000012200 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000012201 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
12202 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000012203 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000012204 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000012205 rdx.getValue(1)
12206 };
Michael Liao0ee17002013-04-19 04:03:37 +000012207 return DAG.getMergeValues(Ops, array_lengthof(Ops), dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012208}
12209
Craig Topper55b24052012-09-11 06:15:32 +000012210SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012211 EVT SrcVT = Op.getOperand(0).getValueType();
12212 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012213 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012214 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012215 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012216 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012217 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012218 // i64 <=> MMX conversions are Legal.
12219 if (SrcVT==MVT::i64 && DstVT.isVector())
12220 return Op;
12221 if (DstVT==MVT::i64 && SrcVT.isVector())
12222 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012223 // MMX <=> MMX conversions are Legal.
12224 if (SrcVT.isVector() && DstVT.isVector())
12225 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012226 // All other conversions need to be expanded.
12227 return SDValue();
12228}
Chris Lattner5b856542010-12-20 00:59:46 +000012229
Craig Topper55b24052012-09-11 06:15:32 +000012230static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012231 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012232 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012233 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012234 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012235 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012236 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012237 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012238 Node->getOperand(0),
12239 Node->getOperand(1), negOp,
12240 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012241 cast<AtomicSDNode>(Node)->getAlignment(),
12242 cast<AtomicSDNode>(Node)->getOrdering(),
12243 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012244}
12245
Eli Friedman327236c2011-08-24 20:50:09 +000012246static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12247 SDNode *Node = Op.getNode();
12248 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012249 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012250
12251 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012252 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12253 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12254 // (The only way to get a 16-byte store is cmpxchg16b)
12255 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12256 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12257 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012258 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12259 cast<AtomicSDNode>(Node)->getMemoryVT(),
12260 Node->getOperand(0),
12261 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012262 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012263 cast<AtomicSDNode>(Node)->getOrdering(),
12264 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012265 return Swap.getValue(1);
12266 }
12267 // Other atomic stores have a simple pattern.
12268 return Op;
12269}
12270
Chris Lattner5b856542010-12-20 00:59:46 +000012271static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12272 EVT VT = Op.getNode()->getValueType(0);
12273
12274 // Let legalize expand this if it isn't a legal type yet.
12275 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12276 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012277
Chris Lattner5b856542010-12-20 00:59:46 +000012278 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012279
Chris Lattner5b856542010-12-20 00:59:46 +000012280 unsigned Opc;
12281 bool ExtraOp = false;
12282 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012283 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012284 case ISD::ADDC: Opc = X86ISD::ADD; break;
12285 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12286 case ISD::SUBC: Opc = X86ISD::SUB; break;
12287 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12288 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012289
Chris Lattner5b856542010-12-20 00:59:46 +000012290 if (!ExtraOp)
12291 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12292 Op.getOperand(1));
12293 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12294 Op.getOperand(1), Op.getOperand(2));
12295}
12296
Evan Cheng8688a582013-01-29 02:32:37 +000012297SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012298 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012299
Evan Cheng8688a582013-01-29 02:32:37 +000012300 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012301 // which returns the values as { float, float } (in XMM0) or
12302 // { double, double } (which is returned in XMM0, XMM1).
Evan Cheng8688a582013-01-29 02:32:37 +000012303 DebugLoc dl = Op.getDebugLoc();
12304 SDValue Arg = Op.getOperand(0);
12305 EVT ArgVT = Arg.getValueType();
12306 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012307
Evan Cheng8688a582013-01-29 02:32:37 +000012308 ArgListTy Args;
12309 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012310
Evan Cheng8688a582013-01-29 02:32:37 +000012311 Entry.Node = Arg;
12312 Entry.Ty = ArgTy;
12313 Entry.isSExt = false;
12314 Entry.isZExt = false;
12315 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012316
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012317 bool isF64 = ArgVT == MVT::f64;
Evan Chenga66f40a2013-01-30 22:56:35 +000012318 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12319 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12320 // the results are returned via SRet in memory.
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012321 const char *LibcallName = isF64 ? "__sincos_stret" : "__sincosf_stret";
Evan Cheng8688a582013-01-29 02:32:37 +000012322 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012323
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012324 Type *RetTy = isF64
12325 ? (Type*)StructType::get(ArgTy, ArgTy, NULL)
12326 : (Type*)VectorType::get(ArgTy, 4);
Evan Cheng8688a582013-01-29 02:32:37 +000012327 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012328 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12329 false, false, false, false, 0,
12330 CallingConv::C, /*isTaillCall=*/false,
12331 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12332 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012333 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012334
12335 if (isF64)
12336 // Returned in xmm0 and xmm1.
12337 return CallResult.first;
12338
12339 // Returned in bits 0:31 and 32:64 xmm0.
12340 SDValue SinVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
12341 CallResult.first, DAG.getIntPtrConstant(0));
12342 SDValue CosVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
12343 CallResult.first, DAG.getIntPtrConstant(1));
12344 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
12345 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal);
Evan Cheng8688a582013-01-29 02:32:37 +000012346}
12347
Evan Cheng0db9fe62006-04-25 20:13:52 +000012348/// LowerOperation - Provide custom lowering hooks for some operations.
12349///
Dan Gohmand858e902010-04-17 15:26:15 +000012350SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012351 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012352 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012353 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012354 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12355 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012356 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012357 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012358 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012359 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012360 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12361 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12362 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012363 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12364 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012365 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12366 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12367 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012368 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012369 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012370 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012371 case ISD::SHL_PARTS:
12372 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012373 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012374 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012375 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012376 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012377 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12378 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12379 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012380 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012381 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012382 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012383 case ISD::FABS: return LowerFABS(Op, DAG);
12384 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012385 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012386 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012387 case ISD::SETCC: return LowerSETCC(Op, DAG);
12388 case ISD::SELECT: return LowerSELECT(Op, DAG);
12389 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012390 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012391 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012392 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012393 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012394 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012395 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012396 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12397 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012398 case ISD::FRAME_TO_ARGS_OFFSET:
12399 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012400 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012401 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012402 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12403 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012404 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12405 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012406 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012407 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012408 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012409 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012410 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012411 case ISD::SRA:
12412 case ISD::SRL:
12413 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012414 case ISD::SADDO:
12415 case ISD::UADDO:
12416 case ISD::SSUBO:
12417 case ISD::USUBO:
12418 case ISD::SMULO:
12419 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012420 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012421 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012422 case ISD::ADDC:
12423 case ISD::ADDE:
12424 case ISD::SUBC:
12425 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012426 case ISD::ADD: return LowerADD(Op, DAG);
12427 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012428 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012429 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012430 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012431}
12432
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012433static void ReplaceATOMIC_LOAD(SDNode *Node,
12434 SmallVectorImpl<SDValue> &Results,
12435 SelectionDAG &DAG) {
12436 DebugLoc dl = Node->getDebugLoc();
12437 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12438
12439 // Convert wide load -> cmpxchg8b/cmpxchg16b
12440 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12441 // (The only way to get a 16-byte load is cmpxchg16b)
12442 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012443 SDValue Zero = DAG.getConstant(0, VT);
12444 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012445 Node->getOperand(0),
12446 Node->getOperand(1), Zero, Zero,
12447 cast<AtomicSDNode>(Node)->getMemOperand(),
12448 cast<AtomicSDNode>(Node)->getOrdering(),
12449 cast<AtomicSDNode>(Node)->getSynchScope());
12450 Results.push_back(Swap.getValue(0));
12451 Results.push_back(Swap.getValue(1));
12452}
12453
Craig Topperc0878702012-08-17 06:55:11 +000012454static void
Duncan Sands1607f052008-12-01 11:39:25 +000012455ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012456 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012457 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012458 assert (Node->getValueType(0) == MVT::i64 &&
12459 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012460
12461 SDValue Chain = Node->getOperand(0);
12462 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012463 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012464 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012465 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012466 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012467 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012468 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012469 SDValue Result =
Michael Liao0ee17002013-04-19 04:03:37 +000012470 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, array_lengthof(Ops), MVT::i64,
Dan Gohmanc76909a2009-09-25 20:36:54 +000012471 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012472 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012473 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012474 Results.push_back(Result.getValue(2));
12475}
12476
Duncan Sands126d9072008-07-04 11:47:58 +000012477/// ReplaceNodeResults - Replace a node with an illegal result type
12478/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012479void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12480 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012481 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012482 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012483 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012484 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012485 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012486 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012487 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012488 case ISD::ADDC:
12489 case ISD::ADDE:
12490 case ISD::SUBC:
12491 case ISD::SUBE:
12492 // We don't want to expand or promote these.
12493 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012494 case ISD::FP_TO_SINT:
12495 case ISD::FP_TO_UINT: {
12496 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12497
12498 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12499 return;
12500
Eli Friedman948e95a2009-05-23 09:59:16 +000012501 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012502 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012503 SDValue FIST = Vals.first, StackSlot = Vals.second;
12504 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012505 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012506 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012507 if (StackSlot.getNode() != 0)
12508 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12509 MachinePointerInfo(),
12510 false, false, false, 0));
12511 else
12512 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012513 }
12514 return;
12515 }
Michael Liao991b6a22012-10-24 04:09:32 +000012516 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012517 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12518 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012519 N->getValueType(0) != MVT::v2f32)
12520 return;
12521 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12522 N->getOperand(0));
12523 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12524 MVT::f64);
12525 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12526 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12527 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12528 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12529 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12530 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12531 return;
12532 }
Michael Liao44c2d612012-10-10 16:53:28 +000012533 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012534 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12535 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012536 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12537 Results.push_back(V);
12538 return;
12539 }
Duncan Sands1607f052008-12-01 11:39:25 +000012540 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012541 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012542 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012543 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012544 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012545 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012546 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012547 eax.getValue(2));
12548 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12549 SDValue Ops[] = { eax, edx };
Michael Liao0ee17002013-04-19 04:03:37 +000012550 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops,
12551 array_lengthof(Ops)));
Duncan Sands1607f052008-12-01 11:39:25 +000012552 Results.push_back(edx.getValue(1));
12553 return;
12554 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012555 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012556 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012557 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012558 bool Regs64bit = T == MVT::i128;
12559 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012560 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012561 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12562 DAG.getConstant(0, HalfT));
12563 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12564 DAG.getConstant(1, HalfT));
12565 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12566 Regs64bit ? X86::RAX : X86::EAX,
12567 cpInL, SDValue());
12568 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12569 Regs64bit ? X86::RDX : X86::EDX,
12570 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012571 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012572 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12573 DAG.getConstant(0, HalfT));
12574 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12575 DAG.getConstant(1, HalfT));
12576 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12577 Regs64bit ? X86::RBX : X86::EBX,
12578 swapInL, cpInH.getValue(1));
12579 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012580 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012581 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012582 SDValue Ops[] = { swapInH.getValue(0),
12583 N->getOperand(1),
12584 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012585 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012586 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012587 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12588 X86ISD::LCMPXCHG8_DAG;
12589 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Michael Liao0ee17002013-04-19 04:03:37 +000012590 Ops, array_lengthof(Ops), T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012591 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12592 Regs64bit ? X86::RAX : X86::EAX,
12593 HalfT, Result.getValue(1));
12594 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12595 Regs64bit ? X86::RDX : X86::EDX,
12596 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012597 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012598 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012599 Results.push_back(cpOutH.getValue(1));
12600 return;
12601 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012602 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012603 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012604 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012605 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012606 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012607 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012608 case ISD::ATOMIC_LOAD_MAX:
12609 case ISD::ATOMIC_LOAD_MIN:
12610 case ISD::ATOMIC_LOAD_UMAX:
12611 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012612 case ISD::ATOMIC_SWAP: {
12613 unsigned Opc;
12614 switch (N->getOpcode()) {
12615 default: llvm_unreachable("Unexpected opcode");
12616 case ISD::ATOMIC_LOAD_ADD:
12617 Opc = X86ISD::ATOMADD64_DAG;
12618 break;
12619 case ISD::ATOMIC_LOAD_AND:
12620 Opc = X86ISD::ATOMAND64_DAG;
12621 break;
12622 case ISD::ATOMIC_LOAD_NAND:
12623 Opc = X86ISD::ATOMNAND64_DAG;
12624 break;
12625 case ISD::ATOMIC_LOAD_OR:
12626 Opc = X86ISD::ATOMOR64_DAG;
12627 break;
12628 case ISD::ATOMIC_LOAD_SUB:
12629 Opc = X86ISD::ATOMSUB64_DAG;
12630 break;
12631 case ISD::ATOMIC_LOAD_XOR:
12632 Opc = X86ISD::ATOMXOR64_DAG;
12633 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012634 case ISD::ATOMIC_LOAD_MAX:
12635 Opc = X86ISD::ATOMMAX64_DAG;
12636 break;
12637 case ISD::ATOMIC_LOAD_MIN:
12638 Opc = X86ISD::ATOMMIN64_DAG;
12639 break;
12640 case ISD::ATOMIC_LOAD_UMAX:
12641 Opc = X86ISD::ATOMUMAX64_DAG;
12642 break;
12643 case ISD::ATOMIC_LOAD_UMIN:
12644 Opc = X86ISD::ATOMUMIN64_DAG;
12645 break;
Craig Topperc0878702012-08-17 06:55:11 +000012646 case ISD::ATOMIC_SWAP:
12647 Opc = X86ISD::ATOMSWAP64_DAG;
12648 break;
12649 }
12650 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012651 return;
Craig Topperc0878702012-08-17 06:55:11 +000012652 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012653 case ISD::ATOMIC_LOAD:
12654 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012655 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012656}
12657
Evan Cheng72261582005-12-20 06:22:03 +000012658const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12659 switch (Opcode) {
12660 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012661 case X86ISD::BSF: return "X86ISD::BSF";
12662 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012663 case X86ISD::SHLD: return "X86ISD::SHLD";
12664 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012665 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012666 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012667 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012668 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012669 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012670 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012671 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12672 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12673 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012674 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012675 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012676 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012677 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012678 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012679 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012680 case X86ISD::COMI: return "X86ISD::COMI";
12681 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012682 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012683 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012684 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12685 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012686 case X86ISD::CMOV: return "X86ISD::CMOV";
12687 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012688 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012689 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12690 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012691 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012692 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012693 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012694 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012695 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012696 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12697 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012698 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012699 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012700 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012701 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012702 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012703 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012704 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012705 case X86ISD::HADD: return "X86ISD::HADD";
12706 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012707 case X86ISD::FHADD: return "X86ISD::FHADD";
12708 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012709 case X86ISD::UMAX: return "X86ISD::UMAX";
12710 case X86ISD::UMIN: return "X86ISD::UMIN";
12711 case X86ISD::SMAX: return "X86ISD::SMAX";
12712 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012713 case X86ISD::FMAX: return "X86ISD::FMAX";
12714 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012715 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12716 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012717 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12718 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012719 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012720 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012721 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012722 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12723 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012724 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012725 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012726 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012727 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012728 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12729 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012730 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12731 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12732 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12733 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12734 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12735 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012736 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012737 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012738 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012739 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12740 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012741 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012742 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012743 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12744 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012745 case X86ISD::VSHL: return "X86ISD::VSHL";
12746 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012747 case X86ISD::VSRA: return "X86ISD::VSRA";
12748 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12749 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12750 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012751 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012752 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12753 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012754 case X86ISD::ADD: return "X86ISD::ADD";
12755 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012756 case X86ISD::ADC: return "X86ISD::ADC";
12757 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012758 case X86ISD::SMUL: return "X86ISD::SMUL";
12759 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012760 case X86ISD::INC: return "X86ISD::INC";
12761 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012762 case X86ISD::OR: return "X86ISD::OR";
12763 case X86ISD::XOR: return "X86ISD::XOR";
12764 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012765 case X86ISD::BLSI: return "X86ISD::BLSI";
12766 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12767 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012768 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012769 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012770 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012771 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012772 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12773 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012774 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012775 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012776 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012777 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012778 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012779 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12780 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012781 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12782 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12783 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012784 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12785 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012786 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12787 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012788 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012789 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012790 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012791 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12792 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012793 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012794 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012795 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012796 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012797 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012798 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012799 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012800 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012801 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Michael Liaoc26392a2013-03-28 23:41:26 +000012802 case X86ISD::RDSEED: return "X86ISD::RDSEED";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012803 case X86ISD::FMADD: return "X86ISD::FMADD";
12804 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12805 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12806 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12807 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12808 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012809 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12810 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Michael Liaof8fd8832013-03-26 22:47:01 +000012811 case X86ISD::XTEST: return "X86ISD::XTEST";
Evan Cheng72261582005-12-20 06:22:03 +000012812 }
12813}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012814
Chris Lattnerc9addb72007-03-30 23:15:24 +000012815// isLegalAddressingMode - Return true if the addressing mode represented
12816// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012817bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012818 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012819 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012820 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012821 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012822
Chris Lattnerc9addb72007-03-30 23:15:24 +000012823 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012824 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012825 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012826
Chris Lattnerc9addb72007-03-30 23:15:24 +000012827 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012828 unsigned GVFlags =
12829 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012830
Chris Lattnerdfed4132009-07-10 07:38:24 +000012831 // If a reference to this global requires an extra load, we can't fold it.
12832 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012833 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012834
Chris Lattnerdfed4132009-07-10 07:38:24 +000012835 // If BaseGV requires a register for the PIC base, we cannot also have a
12836 // BaseReg specified.
12837 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012838 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012839
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012840 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012841 if ((M != CodeModel::Small || R != Reloc::Static) &&
12842 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012843 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012844 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012845
Chris Lattnerc9addb72007-03-30 23:15:24 +000012846 switch (AM.Scale) {
12847 case 0:
12848 case 1:
12849 case 2:
12850 case 4:
12851 case 8:
12852 // These scales always work.
12853 break;
12854 case 3:
12855 case 5:
12856 case 9:
12857 // These scales are formed with basereg+scalereg. Only accept if there is
12858 // no basereg yet.
12859 if (AM.HasBaseReg)
12860 return false;
12861 break;
12862 default: // Other stuff never works.
12863 return false;
12864 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012865
Chris Lattnerc9addb72007-03-30 23:15:24 +000012866 return true;
12867}
12868
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012869bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012870 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012871 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012872 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12873 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012874 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012875}
12876
Evan Cheng70e10d32012-07-17 06:53:39 +000012877bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012878 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012879}
12880
12881bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012882 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012883 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012884}
12885
Owen Andersone50ed302009-08-10 22:56:29 +000012886bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012887 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012888 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012889 unsigned NumBits1 = VT1.getSizeInBits();
12890 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012891 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012892}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012893
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012894bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012895 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012896 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012897}
12898
Owen Andersone50ed302009-08-10 22:56:29 +000012899bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012900 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012901 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012902}
12903
Evan Cheng2766a472012-12-06 19:13:27 +000012904bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12905 EVT VT1 = Val.getValueType();
12906 if (isZExtFree(VT1, VT2))
12907 return true;
12908
12909 if (Val.getOpcode() != ISD::LOAD)
12910 return false;
12911
12912 if (!VT1.isSimple() || !VT1.isInteger() ||
12913 !VT2.isSimple() || !VT2.isInteger())
12914 return false;
12915
12916 switch (VT1.getSimpleVT().SimpleTy) {
12917 default: break;
12918 case MVT::i8:
12919 case MVT::i16:
12920 case MVT::i32:
12921 // X86 has 8, 16, and 32-bit zero-extending loads.
12922 return true;
12923 }
12924
12925 return false;
12926}
12927
Owen Andersone50ed302009-08-10 22:56:29 +000012928bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012929 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012930 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012931}
12932
Evan Cheng60c07e12006-07-05 22:17:51 +000012933/// isShuffleMaskLegal - Targets can use this to indicate that they only
12934/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12935/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12936/// are assumed to be legal.
12937bool
Eric Christopherfd179292009-08-27 18:07:15 +000012938X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012939 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012940 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012941 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012942 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012943
Nate Begemana09008b2009-10-19 02:17:23 +000012944 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012945 return (VT.getVectorNumElements() == 2 ||
12946 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12947 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012948 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012949 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012950 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12951 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012952 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012953 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12954 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12955 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12956 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012957}
12958
Dan Gohman7d8143f2008-04-09 20:09:42 +000012959bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012960X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012961 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012962 unsigned NumElts = VT.getVectorNumElements();
12963 // FIXME: This collection of masks seems suspect.
12964 if (NumElts == 2)
12965 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012966 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012967 return (isMOVLMask(Mask, VT) ||
12968 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012969 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12970 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012971 }
12972 return false;
12973}
12974
12975//===----------------------------------------------------------------------===//
12976// X86 Scheduler Hooks
12977//===----------------------------------------------------------------------===//
12978
Michael Liaobe02a902012-11-08 07:28:54 +000012979/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012980static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12981 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012982 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012983
12984 const BasicBlock *BB = MBB->getBasicBlock();
12985 MachineFunction::iterator I = MBB;
12986 ++I;
12987
12988 // For the v = xbegin(), we generate
12989 //
12990 // thisMBB:
12991 // xbegin sinkMBB
12992 //
12993 // mainMBB:
12994 // eax = -1
12995 //
12996 // sinkMBB:
12997 // v = eax
12998
12999 MachineBasicBlock *thisMBB = MBB;
13000 MachineFunction *MF = MBB->getParent();
13001 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13002 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13003 MF->insert(I, mainMBB);
13004 MF->insert(I, sinkMBB);
13005
13006 // Transfer the remainder of BB and its successor edges to sinkMBB.
13007 sinkMBB->splice(sinkMBB->begin(), MBB,
13008 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13009 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13010
13011 // thisMBB:
13012 // xbegin sinkMBB
13013 // # fallthrough to mainMBB
13014 // # abortion to sinkMBB
13015 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
13016 thisMBB->addSuccessor(mainMBB);
13017 thisMBB->addSuccessor(sinkMBB);
13018
13019 // mainMBB:
13020 // EAX = -1
13021 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
13022 mainMBB->addSuccessor(sinkMBB);
13023
13024 // sinkMBB:
13025 // EAX is live into the sinkMBB
13026 sinkMBB->addLiveIn(X86::EAX);
13027 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13028 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13029 .addReg(X86::EAX);
13030
13031 MI->eraseFromParent();
13032 return sinkMBB;
13033}
13034
Michael Liaob118a072012-09-20 03:06:15 +000013035// Get CMPXCHG opcode for the specified data type.
13036static unsigned getCmpXChgOpcode(EVT VT) {
13037 switch (VT.getSimpleVT().SimpleTy) {
13038 case MVT::i8: return X86::LCMPXCHG8;
13039 case MVT::i16: return X86::LCMPXCHG16;
13040 case MVT::i32: return X86::LCMPXCHG32;
13041 case MVT::i64: return X86::LCMPXCHG64;
13042 default:
13043 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000013044 }
Michael Liaob118a072012-09-20 03:06:15 +000013045 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000013046}
13047
Michael Liaob118a072012-09-20 03:06:15 +000013048// Get LOAD opcode for the specified data type.
13049static unsigned getLoadOpcode(EVT VT) {
13050 switch (VT.getSimpleVT().SimpleTy) {
13051 case MVT::i8: return X86::MOV8rm;
13052 case MVT::i16: return X86::MOV16rm;
13053 case MVT::i32: return X86::MOV32rm;
13054 case MVT::i64: return X86::MOV64rm;
13055 default:
13056 break;
13057 }
13058 llvm_unreachable("Invalid operand size!");
13059}
13060
13061// Get opcode of the non-atomic one from the specified atomic instruction.
13062static unsigned getNonAtomicOpcode(unsigned Opc) {
13063 switch (Opc) {
13064 case X86::ATOMAND8: return X86::AND8rr;
13065 case X86::ATOMAND16: return X86::AND16rr;
13066 case X86::ATOMAND32: return X86::AND32rr;
13067 case X86::ATOMAND64: return X86::AND64rr;
13068 case X86::ATOMOR8: return X86::OR8rr;
13069 case X86::ATOMOR16: return X86::OR16rr;
13070 case X86::ATOMOR32: return X86::OR32rr;
13071 case X86::ATOMOR64: return X86::OR64rr;
13072 case X86::ATOMXOR8: return X86::XOR8rr;
13073 case X86::ATOMXOR16: return X86::XOR16rr;
13074 case X86::ATOMXOR32: return X86::XOR32rr;
13075 case X86::ATOMXOR64: return X86::XOR64rr;
13076 }
13077 llvm_unreachable("Unhandled atomic-load-op opcode!");
13078}
13079
13080// Get opcode of the non-atomic one from the specified atomic instruction with
13081// extra opcode.
13082static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
13083 unsigned &ExtraOpc) {
13084 switch (Opc) {
13085 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
13086 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
13087 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
13088 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013089 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013090 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
13091 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
13092 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013093 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013094 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
13095 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
13096 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013097 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013098 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
13099 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
13100 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013101 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013102 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
13103 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
13104 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
13105 }
13106 llvm_unreachable("Unhandled atomic-load-op opcode!");
13107}
13108
13109// Get opcode of the non-atomic one from the specified atomic instruction for
13110// 64-bit data type on 32-bit target.
13111static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
13112 switch (Opc) {
13113 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
13114 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
13115 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
13116 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
13117 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
13118 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013119 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
13120 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
13121 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
13122 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000013123 }
13124 llvm_unreachable("Unhandled atomic-load-op opcode!");
13125}
13126
13127// Get opcode of the non-atomic one from the specified atomic instruction for
13128// 64-bit data type on 32-bit target with extra opcode.
13129static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
13130 unsigned &HiOpc,
13131 unsigned &ExtraOpc) {
13132 switch (Opc) {
13133 case X86::ATOMNAND6432:
13134 ExtraOpc = X86::NOT32r;
13135 HiOpc = X86::AND32rr;
13136 return X86::AND32rr;
13137 }
13138 llvm_unreachable("Unhandled atomic-load-op opcode!");
13139}
13140
13141// Get pseudo CMOV opcode from the specified data type.
13142static unsigned getPseudoCMOVOpc(EVT VT) {
13143 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000013144 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000013145 case MVT::i16: return X86::CMOV_GR16;
13146 case MVT::i32: return X86::CMOV_GR32;
13147 default:
13148 break;
13149 }
13150 llvm_unreachable("Unknown CMOV opcode!");
13151}
13152
13153// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
13154// They will be translated into a spin-loop or compare-exchange loop from
13155//
13156// ...
13157// dst = atomic-fetch-op MI.addr, MI.val
13158// ...
13159//
13160// to
13161//
13162// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013163// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000013164// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013165// t4 = phi(t1, t3 / loop)
13166// t2 = OP MI.val, t4
13167// EAX = t4
13168// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
13169// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013170// JNE loop
13171// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013172// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013173// ...
Mon P Wang63307c32008-05-05 19:05:59 +000013174MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000013175X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
13176 MachineBasicBlock *MBB) const {
13177 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13178 DebugLoc DL = MI->getDebugLoc();
13179
13180 MachineFunction *MF = MBB->getParent();
13181 MachineRegisterInfo &MRI = MF->getRegInfo();
13182
13183 const BasicBlock *BB = MBB->getBasicBlock();
13184 MachineFunction::iterator I = MBB;
13185 ++I;
13186
Michael Liao13d08bf2013-01-22 21:47:38 +000013187 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000013188 "Unexpected number of operands");
13189
13190 assert(MI->hasOneMemOperand() &&
13191 "Expected atomic-load-op to have one memoperand");
13192
13193 // Memory Reference
13194 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13195 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13196
13197 unsigned DstReg, SrcReg;
13198 unsigned MemOpndSlot;
13199
13200 unsigned CurOp = 0;
13201
13202 DstReg = MI->getOperand(CurOp++).getReg();
13203 MemOpndSlot = CurOp;
13204 CurOp += X86::AddrNumOperands;
13205 SrcReg = MI->getOperand(CurOp++).getReg();
13206
13207 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000013208 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000013209 unsigned t1 = MRI.createVirtualRegister(RC);
13210 unsigned t2 = MRI.createVirtualRegister(RC);
13211 unsigned t3 = MRI.createVirtualRegister(RC);
13212 unsigned t4 = MRI.createVirtualRegister(RC);
13213 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000013214
13215 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
13216 unsigned LOADOpc = getLoadOpcode(VT);
13217
13218 // For the atomic load-arith operator, we generate
13219 //
13220 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013221 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000013222 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013223 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013224 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013225 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013226 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013227 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013228 // JNE mainMBB
13229 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013230 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013231
13232 MachineBasicBlock *thisMBB = MBB;
13233 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13234 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13235 MF->insert(I, mainMBB);
13236 MF->insert(I, sinkMBB);
13237
13238 MachineInstrBuilder MIB;
13239
13240 // Transfer the remainder of BB and its successor edges to sinkMBB.
13241 sinkMBB->splice(sinkMBB->begin(), MBB,
13242 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13243 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13244
13245 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013246 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13247 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13248 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13249 if (NewMO.isReg())
13250 NewMO.setIsKill(false);
13251 MIB.addOperand(NewMO);
13252 }
13253 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13254 unsigned flags = (*MMOI)->getFlags();
13255 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13256 MachineMemOperand *MMO =
13257 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13258 (*MMOI)->getSize(),
13259 (*MMOI)->getBaseAlignment(),
13260 (*MMOI)->getTBAAInfo(),
13261 (*MMOI)->getRanges());
13262 MIB.addMemOperand(MMO);
13263 }
Michael Liaob118a072012-09-20 03:06:15 +000013264
13265 thisMBB->addSuccessor(mainMBB);
13266
13267 // mainMBB:
13268 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013269
Michael Liaoc537f792013-03-06 00:17:04 +000013270 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013271 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13272 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013273
Michael Liaob118a072012-09-20 03:06:15 +000013274 unsigned Opc = MI->getOpcode();
13275 switch (Opc) {
13276 default:
13277 llvm_unreachable("Unhandled atomic-load-op opcode!");
13278 case X86::ATOMAND8:
13279 case X86::ATOMAND16:
13280 case X86::ATOMAND32:
13281 case X86::ATOMAND64:
13282 case X86::ATOMOR8:
13283 case X86::ATOMOR16:
13284 case X86::ATOMOR32:
13285 case X86::ATOMOR64:
13286 case X86::ATOMXOR8:
13287 case X86::ATOMXOR16:
13288 case X86::ATOMXOR32:
13289 case X86::ATOMXOR64: {
13290 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013291 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13292 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013293 break;
13294 }
13295 case X86::ATOMNAND8:
13296 case X86::ATOMNAND16:
13297 case X86::ATOMNAND32:
13298 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013299 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013300 unsigned NOTOpc;
13301 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013302 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13303 .addReg(t4);
13304 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013305 break;
13306 }
Michael Liao08382492012-09-21 03:00:17 +000013307 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013308 case X86::ATOMMAX16:
13309 case X86::ATOMMAX32:
13310 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013311 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013312 case X86::ATOMMIN16:
13313 case X86::ATOMMIN32:
13314 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013315 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013316 case X86::ATOMUMAX16:
13317 case X86::ATOMUMAX32:
13318 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013319 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013320 case X86::ATOMUMIN16:
13321 case X86::ATOMUMIN32:
13322 case X86::ATOMUMIN64: {
13323 unsigned CMPOpc;
13324 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13325
13326 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13327 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013328 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013329
13330 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013331 if (VT != MVT::i8) {
13332 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013333 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013334 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013335 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013336 } else {
13337 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013338 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13339 const TargetRegisterClass *RC32 =
13340 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013341 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13342 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013343 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013344
13345 unsigned Undef = MRI.createVirtualRegister(RC32);
13346 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13347
13348 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13349 .addReg(Undef)
13350 .addReg(SrcReg)
13351 .addImm(X86::sub_8bit);
13352 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13353 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013354 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013355 .addImm(X86::sub_8bit);
13356
Michael Liaoc537f792013-03-06 00:17:04 +000013357 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013358 .addReg(SrcReg32)
13359 .addReg(AccReg32);
13360
Michael Liaoc537f792013-03-06 00:17:04 +000013361 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13362 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013363 }
Michael Liaob118a072012-09-20 03:06:15 +000013364 } else {
13365 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013366 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013367 "Invalid atomic-load-op transformation!");
13368 unsigned SelOpc = getPseudoCMOVOpc(VT);
13369 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13370 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013371 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13372 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013373 .addImm(CC);
13374 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013375 // Replace the original PHI node as mainMBB is changed after CMOV
13376 // lowering.
13377 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13378 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13379 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013380 }
13381 break;
13382 }
13383 }
13384
Michael Liaoc537f792013-03-06 00:17:04 +000013385 // Copy PhyReg back from virtual register.
13386 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13387 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013388
13389 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013390 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13391 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13392 if (NewMO.isReg())
13393 NewMO.setIsKill(false);
13394 MIB.addOperand(NewMO);
13395 }
13396 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013397 MIB.setMemRefs(MMOBegin, MMOEnd);
13398
Michael Liaoc537f792013-03-06 00:17:04 +000013399 // Copy PhyReg back to virtual register.
13400 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13401 .addReg(PhyReg);
13402
Michael Liaob118a072012-09-20 03:06:15 +000013403 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13404
13405 mainMBB->addSuccessor(origMainMBB);
13406 mainMBB->addSuccessor(sinkMBB);
13407
13408 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013409 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13410 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013411 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013412
13413 MI->eraseFromParent();
13414 return sinkMBB;
13415}
13416
13417// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13418// instructions. They will be translated into a spin-loop or compare-exchange
13419// loop from
13420//
13421// ...
13422// dst = atomic-fetch-op MI.addr, MI.val
13423// ...
13424//
13425// to
13426//
13427// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013428// t1L = LOAD [MI.addr + 0]
13429// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013430// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013431// t4L = phi(t1L, t3L / loop)
13432// t4H = phi(t1H, t3H / loop)
13433// t2L = OP MI.val.lo, t4L
13434// t2H = OP MI.val.hi, t4H
13435// EAX = t4L
13436// EDX = t4H
13437// EBX = t2L
13438// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013439// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013440// t3L = EAX
13441// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013442// JNE loop
13443// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013444// dstL = t3L
13445// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013446// ...
13447MachineBasicBlock *
13448X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13449 MachineBasicBlock *MBB) const {
13450 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13451 DebugLoc DL = MI->getDebugLoc();
13452
13453 MachineFunction *MF = MBB->getParent();
13454 MachineRegisterInfo &MRI = MF->getRegInfo();
13455
13456 const BasicBlock *BB = MBB->getBasicBlock();
13457 MachineFunction::iterator I = MBB;
13458 ++I;
13459
Michael Liao13d08bf2013-01-22 21:47:38 +000013460 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013461 "Unexpected number of operands");
13462
13463 assert(MI->hasOneMemOperand() &&
13464 "Expected atomic-load-op32 to have one memoperand");
13465
13466 // Memory Reference
13467 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13468 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13469
13470 unsigned DstLoReg, DstHiReg;
13471 unsigned SrcLoReg, SrcHiReg;
13472 unsigned MemOpndSlot;
13473
13474 unsigned CurOp = 0;
13475
13476 DstLoReg = MI->getOperand(CurOp++).getReg();
13477 DstHiReg = MI->getOperand(CurOp++).getReg();
13478 MemOpndSlot = CurOp;
13479 CurOp += X86::AddrNumOperands;
13480 SrcLoReg = MI->getOperand(CurOp++).getReg();
13481 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013482
Craig Topperc9099502012-04-20 06:31:50 +000013483 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013484 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013485
Michael Liaoc537f792013-03-06 00:17:04 +000013486 unsigned t1L = MRI.createVirtualRegister(RC);
13487 unsigned t1H = MRI.createVirtualRegister(RC);
13488 unsigned t2L = MRI.createVirtualRegister(RC);
13489 unsigned t2H = MRI.createVirtualRegister(RC);
13490 unsigned t3L = MRI.createVirtualRegister(RC);
13491 unsigned t3H = MRI.createVirtualRegister(RC);
13492 unsigned t4L = MRI.createVirtualRegister(RC);
13493 unsigned t4H = MRI.createVirtualRegister(RC);
13494
Michael Liaob118a072012-09-20 03:06:15 +000013495 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13496 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013497
Michael Liaob118a072012-09-20 03:06:15 +000013498 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013499 //
Michael Liaob118a072012-09-20 03:06:15 +000013500 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013501 // t1L = LOAD [MI.addr + 0]
13502 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013503 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013504 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13505 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13506 // t2L = OP MI.val.lo, t4L
13507 // t2H = OP MI.val.hi, t4H
13508 // EBX = t2L
13509 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013510 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013511 // t3L = EAX
13512 // t3H = EDX
13513 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013514 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013515 // dstL = t3L
13516 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013517
Mon P Wang63307c32008-05-05 19:05:59 +000013518 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013519 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13520 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13521 MF->insert(I, mainMBB);
13522 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013523
Michael Liaob118a072012-09-20 03:06:15 +000013524 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013525
Michael Liaob118a072012-09-20 03:06:15 +000013526 // Transfer the remainder of BB and its successor edges to sinkMBB.
13527 sinkMBB->splice(sinkMBB->begin(), MBB,
13528 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13529 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013530
Michael Liaob118a072012-09-20 03:06:15 +000013531 // thisMBB:
13532 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013533 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013534 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013535 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13536 if (NewMO.isReg())
13537 NewMO.setIsKill(false);
13538 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013539 }
Michael Liaoc537f792013-03-06 00:17:04 +000013540 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13541 unsigned flags = (*MMOI)->getFlags();
13542 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13543 MachineMemOperand *MMO =
13544 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13545 (*MMOI)->getSize(),
13546 (*MMOI)->getBaseAlignment(),
13547 (*MMOI)->getTBAAInfo(),
13548 (*MMOI)->getRanges());
13549 MIB.addMemOperand(MMO);
13550 };
13551 MachineInstr *LowMI = MIB;
13552
13553 // Hi
13554 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13555 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13556 if (i == X86::AddrDisp) {
13557 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13558 } else {
13559 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13560 if (NewMO.isReg())
13561 NewMO.setIsKill(false);
13562 MIB.addOperand(NewMO);
13563 }
13564 }
13565 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013566
Michael Liaob118a072012-09-20 03:06:15 +000013567 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013568
Michael Liaob118a072012-09-20 03:06:15 +000013569 // mainMBB:
13570 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013571
Michael Liaoc537f792013-03-06 00:17:04 +000013572 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013573 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13574 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13575 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13576 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013577
Michael Liaob118a072012-09-20 03:06:15 +000013578 unsigned Opc = MI->getOpcode();
13579 switch (Opc) {
13580 default:
13581 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13582 case X86::ATOMAND6432:
13583 case X86::ATOMOR6432:
13584 case X86::ATOMXOR6432:
13585 case X86::ATOMADD6432:
13586 case X86::ATOMSUB6432: {
13587 unsigned HiOpc;
13588 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013589 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13590 .addReg(SrcLoReg);
13591 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13592 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013593 break;
13594 }
13595 case X86::ATOMNAND6432: {
13596 unsigned HiOpc, NOTOpc;
13597 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013598 unsigned TmpL = MRI.createVirtualRegister(RC);
13599 unsigned TmpH = MRI.createVirtualRegister(RC);
13600 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13601 .addReg(t4L);
13602 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13603 .addReg(t4H);
13604 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13605 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013606 break;
13607 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013608 case X86::ATOMMAX6432:
13609 case X86::ATOMMIN6432:
13610 case X86::ATOMUMAX6432:
13611 case X86::ATOMUMIN6432: {
13612 unsigned HiOpc;
13613 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13614 unsigned cL = MRI.createVirtualRegister(RC8);
13615 unsigned cH = MRI.createVirtualRegister(RC8);
13616 unsigned cL32 = MRI.createVirtualRegister(RC);
13617 unsigned cH32 = MRI.createVirtualRegister(RC);
13618 unsigned cc = MRI.createVirtualRegister(RC);
13619 // cl := cmp src_lo, lo
13620 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013621 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013622 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13623 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13624 // ch := cmp src_hi, hi
13625 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013626 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013627 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13628 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13629 // cc := if (src_hi == hi) ? cl : ch;
13630 if (Subtarget->hasCMov()) {
13631 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13632 .addReg(cH32).addReg(cL32);
13633 } else {
13634 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13635 .addReg(cH32).addReg(cL32)
13636 .addImm(X86::COND_E);
13637 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13638 }
13639 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13640 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013641 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13642 .addReg(SrcLoReg).addReg(t4L);
13643 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13644 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013645 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013646 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13647 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013648 .addImm(X86::COND_NE);
13649 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013650 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13651 // 2nd CMOV lowering.
13652 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013653 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13654 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013655 .addImm(X86::COND_NE);
13656 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013657 // Replace the original PHI node as mainMBB is changed after CMOV
13658 // lowering.
13659 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13660 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13661 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13662 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13663 PhiL->eraseFromParent();
13664 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013665 }
13666 break;
13667 }
Michael Liaob118a072012-09-20 03:06:15 +000013668 case X86::ATOMSWAP6432: {
13669 unsigned HiOpc;
13670 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013671 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13672 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013673 break;
13674 }
13675 }
Mon P Wang63307c32008-05-05 19:05:59 +000013676
Michael Liaob118a072012-09-20 03:06:15 +000013677 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013678 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13679 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013680 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013681 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13682 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013683
Michael Liaob118a072012-09-20 03:06:15 +000013684 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013685 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13686 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13687 if (NewMO.isReg())
13688 NewMO.setIsKill(false);
13689 MIB.addOperand(NewMO);
13690 }
Michael Liaob118a072012-09-20 03:06:15 +000013691 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013692
Michael Liaoc537f792013-03-06 00:17:04 +000013693 // Copy EDX:EAX back to t3H:t3L
13694 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13695 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13696
Michael Liaob118a072012-09-20 03:06:15 +000013697 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013698
Michael Liaob118a072012-09-20 03:06:15 +000013699 mainMBB->addSuccessor(origMainMBB);
13700 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013701
Michael Liaob118a072012-09-20 03:06:15 +000013702 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013703 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13704 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013705 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013706 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13707 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013708 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013709
Michael Liaob118a072012-09-20 03:06:15 +000013710 MI->eraseFromParent();
13711 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013712}
13713
Eric Christopherf83a5de2009-08-27 18:08:16 +000013714// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013715// or XMM0_V32I8 in AVX all of this code can be replaced with that
13716// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013717static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13718 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013719 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013720 switch (MI->getOpcode()) {
13721 default: llvm_unreachable("illegal opcode!");
13722 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13723 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13724 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13725 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13726 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13727 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13728 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13729 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013730 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013731
Craig Topper8aae8dd2012-11-10 08:57:41 +000013732 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013733 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013734
Craig Topper52ea2452012-11-10 09:25:36 +000013735 unsigned NumArgs = MI->getNumOperands();
13736 for (unsigned i = 1; i < NumArgs; ++i) {
13737 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013738 if (!(Op.isReg() && Op.isImplicit()))
13739 MIB.addOperand(Op);
13740 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013741 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013742 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13743
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013744 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013745 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013746 .addReg(X86::XMM0);
13747
Dan Gohman14152b42010-07-06 20:24:04 +000013748 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013749 return BB;
13750}
13751
Craig Topper9c7ae012012-11-10 01:23:36 +000013752// FIXME: Custom handling because TableGen doesn't support multiple implicit
13753// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013754static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13755 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013756 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013757 switch (MI->getOpcode()) {
13758 default: llvm_unreachable("illegal opcode!");
13759 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13760 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13761 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13762 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13763 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13764 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13765 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13766 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013767 }
13768
Craig Topper8aae8dd2012-11-10 08:57:41 +000013769 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013770 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013771
Craig Topper52ea2452012-11-10 09:25:36 +000013772 unsigned NumArgs = MI->getNumOperands(); // remove the results
13773 for (unsigned i = 1; i < NumArgs; ++i) {
13774 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013775 if (!(Op.isReg() && Op.isImplicit()))
13776 MIB.addOperand(Op);
13777 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013778 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013779 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13780
13781 BuildMI(*BB, MI, dl,
13782 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13783 .addReg(X86::ECX);
13784
13785 MI->eraseFromParent();
13786 return BB;
13787}
13788
Craig Topper2da36912012-11-11 22:45:02 +000013789static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13790 const TargetInstrInfo *TII,
13791 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013792 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013793
Eric Christopher228232b2010-11-30 07:20:12 +000013794 // Address into RAX/EAX, other two args into ECX, EDX.
13795 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13796 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13797 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13798 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013799 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013800
Eric Christopher228232b2010-11-30 07:20:12 +000013801 unsigned ValOps = X86::AddrNumOperands;
13802 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13803 .addReg(MI->getOperand(ValOps).getReg());
13804 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13805 .addReg(MI->getOperand(ValOps+1).getReg());
13806
13807 // The instruction doesn't actually take any operands though.
13808 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013809
Eric Christopher228232b2010-11-30 07:20:12 +000013810 MI->eraseFromParent(); // The pseudo is gone now.
13811 return BB;
13812}
13813
13814MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013815X86TargetLowering::EmitVAARG64WithCustomInserter(
13816 MachineInstr *MI,
13817 MachineBasicBlock *MBB) const {
13818 // Emit va_arg instruction on X86-64.
13819
13820 // Operands to this pseudo-instruction:
13821 // 0 ) Output : destination address (reg)
13822 // 1-5) Input : va_list address (addr, i64mem)
13823 // 6 ) ArgSize : Size (in bytes) of vararg type
13824 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13825 // 8 ) Align : Alignment of type
13826 // 9 ) EFLAGS (implicit-def)
13827
13828 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13829 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13830
13831 unsigned DestReg = MI->getOperand(0).getReg();
13832 MachineOperand &Base = MI->getOperand(1);
13833 MachineOperand &Scale = MI->getOperand(2);
13834 MachineOperand &Index = MI->getOperand(3);
13835 MachineOperand &Disp = MI->getOperand(4);
13836 MachineOperand &Segment = MI->getOperand(5);
13837 unsigned ArgSize = MI->getOperand(6).getImm();
13838 unsigned ArgMode = MI->getOperand(7).getImm();
13839 unsigned Align = MI->getOperand(8).getImm();
13840
13841 // Memory Reference
13842 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13843 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13844 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13845
13846 // Machine Information
13847 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13848 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13849 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13850 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13851 DebugLoc DL = MI->getDebugLoc();
13852
13853 // struct va_list {
13854 // i32 gp_offset
13855 // i32 fp_offset
13856 // i64 overflow_area (address)
13857 // i64 reg_save_area (address)
13858 // }
13859 // sizeof(va_list) = 24
13860 // alignment(va_list) = 8
13861
13862 unsigned TotalNumIntRegs = 6;
13863 unsigned TotalNumXMMRegs = 8;
13864 bool UseGPOffset = (ArgMode == 1);
13865 bool UseFPOffset = (ArgMode == 2);
13866 unsigned MaxOffset = TotalNumIntRegs * 8 +
13867 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13868
13869 /* Align ArgSize to a multiple of 8 */
13870 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13871 bool NeedsAlign = (Align > 8);
13872
13873 MachineBasicBlock *thisMBB = MBB;
13874 MachineBasicBlock *overflowMBB;
13875 MachineBasicBlock *offsetMBB;
13876 MachineBasicBlock *endMBB;
13877
13878 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13879 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13880 unsigned OffsetReg = 0;
13881
13882 if (!UseGPOffset && !UseFPOffset) {
13883 // If we only pull from the overflow region, we don't create a branch.
13884 // We don't need to alter control flow.
13885 OffsetDestReg = 0; // unused
13886 OverflowDestReg = DestReg;
13887
13888 offsetMBB = NULL;
13889 overflowMBB = thisMBB;
13890 endMBB = thisMBB;
13891 } else {
13892 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13893 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13894 // If not, pull from overflow_area. (branch to overflowMBB)
13895 //
13896 // thisMBB
13897 // | .
13898 // | .
13899 // offsetMBB overflowMBB
13900 // | .
13901 // | .
13902 // endMBB
13903
13904 // Registers for the PHI in endMBB
13905 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13906 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13907
13908 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13909 MachineFunction *MF = MBB->getParent();
13910 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13911 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13912 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13913
13914 MachineFunction::iterator MBBIter = MBB;
13915 ++MBBIter;
13916
13917 // Insert the new basic blocks
13918 MF->insert(MBBIter, offsetMBB);
13919 MF->insert(MBBIter, overflowMBB);
13920 MF->insert(MBBIter, endMBB);
13921
13922 // Transfer the remainder of MBB and its successor edges to endMBB.
13923 endMBB->splice(endMBB->begin(), thisMBB,
13924 llvm::next(MachineBasicBlock::iterator(MI)),
13925 thisMBB->end());
13926 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13927
13928 // Make offsetMBB and overflowMBB successors of thisMBB
13929 thisMBB->addSuccessor(offsetMBB);
13930 thisMBB->addSuccessor(overflowMBB);
13931
13932 // endMBB is a successor of both offsetMBB and overflowMBB
13933 offsetMBB->addSuccessor(endMBB);
13934 overflowMBB->addSuccessor(endMBB);
13935
13936 // Load the offset value into a register
13937 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13938 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13939 .addOperand(Base)
13940 .addOperand(Scale)
13941 .addOperand(Index)
13942 .addDisp(Disp, UseFPOffset ? 4 : 0)
13943 .addOperand(Segment)
13944 .setMemRefs(MMOBegin, MMOEnd);
13945
13946 // Check if there is enough room left to pull this argument.
13947 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13948 .addReg(OffsetReg)
13949 .addImm(MaxOffset + 8 - ArgSizeA8);
13950
13951 // Branch to "overflowMBB" if offset >= max
13952 // Fall through to "offsetMBB" otherwise
13953 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13954 .addMBB(overflowMBB);
13955 }
13956
13957 // In offsetMBB, emit code to use the reg_save_area.
13958 if (offsetMBB) {
13959 assert(OffsetReg != 0);
13960
13961 // Read the reg_save_area address.
13962 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13963 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13964 .addOperand(Base)
13965 .addOperand(Scale)
13966 .addOperand(Index)
13967 .addDisp(Disp, 16)
13968 .addOperand(Segment)
13969 .setMemRefs(MMOBegin, MMOEnd);
13970
13971 // Zero-extend the offset
13972 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13973 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13974 .addImm(0)
13975 .addReg(OffsetReg)
13976 .addImm(X86::sub_32bit);
13977
13978 // Add the offset to the reg_save_area to get the final address.
13979 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13980 .addReg(OffsetReg64)
13981 .addReg(RegSaveReg);
13982
13983 // Compute the offset for the next argument
13984 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13985 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13986 .addReg(OffsetReg)
13987 .addImm(UseFPOffset ? 16 : 8);
13988
13989 // Store it back into the va_list.
13990 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13991 .addOperand(Base)
13992 .addOperand(Scale)
13993 .addOperand(Index)
13994 .addDisp(Disp, UseFPOffset ? 4 : 0)
13995 .addOperand(Segment)
13996 .addReg(NextOffsetReg)
13997 .setMemRefs(MMOBegin, MMOEnd);
13998
13999 // Jump to endMBB
14000 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
14001 .addMBB(endMBB);
14002 }
14003
14004 //
14005 // Emit code to use overflow area
14006 //
14007
14008 // Load the overflow_area address into a register.
14009 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
14010 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
14011 .addOperand(Base)
14012 .addOperand(Scale)
14013 .addOperand(Index)
14014 .addDisp(Disp, 8)
14015 .addOperand(Segment)
14016 .setMemRefs(MMOBegin, MMOEnd);
14017
14018 // If we need to align it, do so. Otherwise, just copy the address
14019 // to OverflowDestReg.
14020 if (NeedsAlign) {
14021 // Align the overflow address
14022 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
14023 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
14024
14025 // aligned_addr = (addr + (align-1)) & ~(align-1)
14026 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
14027 .addReg(OverflowAddrReg)
14028 .addImm(Align-1);
14029
14030 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
14031 .addReg(TmpReg)
14032 .addImm(~(uint64_t)(Align-1));
14033 } else {
14034 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
14035 .addReg(OverflowAddrReg);
14036 }
14037
14038 // Compute the next overflow address after this argument.
14039 // (the overflow address should be kept 8-byte aligned)
14040 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
14041 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
14042 .addReg(OverflowDestReg)
14043 .addImm(ArgSizeA8);
14044
14045 // Store the new overflow address.
14046 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
14047 .addOperand(Base)
14048 .addOperand(Scale)
14049 .addOperand(Index)
14050 .addDisp(Disp, 8)
14051 .addOperand(Segment)
14052 .addReg(NextAddrReg)
14053 .setMemRefs(MMOBegin, MMOEnd);
14054
14055 // If we branched, emit the PHI to the front of endMBB.
14056 if (offsetMBB) {
14057 BuildMI(*endMBB, endMBB->begin(), DL,
14058 TII->get(X86::PHI), DestReg)
14059 .addReg(OffsetDestReg).addMBB(offsetMBB)
14060 .addReg(OverflowDestReg).addMBB(overflowMBB);
14061 }
14062
14063 // Erase the pseudo instruction
14064 MI->eraseFromParent();
14065
14066 return endMBB;
14067}
14068
14069MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000014070X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
14071 MachineInstr *MI,
14072 MachineBasicBlock *MBB) const {
14073 // Emit code to save XMM registers to the stack. The ABI says that the
14074 // number of registers to save is given in %al, so it's theoretically
14075 // possible to do an indirect jump trick to avoid saving all of them,
14076 // however this code takes a simpler approach and just executes all
14077 // of the stores if %al is non-zero. It's less code, and it's probably
14078 // easier on the hardware branch predictor, and stores aren't all that
14079 // expensive anyway.
14080
14081 // Create the new basic blocks. One block contains all the XMM stores,
14082 // and one block is the final destination regardless of whether any
14083 // stores were performed.
14084 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14085 MachineFunction *F = MBB->getParent();
14086 MachineFunction::iterator MBBIter = MBB;
14087 ++MBBIter;
14088 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
14089 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
14090 F->insert(MBBIter, XMMSaveMBB);
14091 F->insert(MBBIter, EndMBB);
14092
Dan Gohman14152b42010-07-06 20:24:04 +000014093 // Transfer the remainder of MBB and its successor edges to EndMBB.
14094 EndMBB->splice(EndMBB->begin(), MBB,
14095 llvm::next(MachineBasicBlock::iterator(MI)),
14096 MBB->end());
14097 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
14098
Dan Gohmand6708ea2009-08-15 01:38:56 +000014099 // The original block will now fall through to the XMM save block.
14100 MBB->addSuccessor(XMMSaveMBB);
14101 // The XMMSaveMBB will fall through to the end block.
14102 XMMSaveMBB->addSuccessor(EndMBB);
14103
14104 // Now add the instructions.
14105 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14106 DebugLoc DL = MI->getDebugLoc();
14107
14108 unsigned CountReg = MI->getOperand(0).getReg();
14109 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
14110 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
14111
14112 if (!Subtarget->isTargetWin64()) {
14113 // If %al is 0, branch around the XMM save block.
14114 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000014115 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014116 MBB->addSuccessor(EndMBB);
14117 }
14118
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014119 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000014120 // In the XMM save block, save all the XMM argument registers.
14121 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
14122 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000014123 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000014124 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000014125 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000014126 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000014127 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014128 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000014129 .addFrameIndex(RegSaveFrameIndex)
14130 .addImm(/*Scale=*/1)
14131 .addReg(/*IndexReg=*/0)
14132 .addImm(/*Disp=*/Offset)
14133 .addReg(/*Segment=*/0)
14134 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000014135 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014136 }
14137
Dan Gohman14152b42010-07-06 20:24:04 +000014138 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000014139
14140 return EndMBB;
14141}
Mon P Wang63307c32008-05-05 19:05:59 +000014142
Lang Hames6e3f7e42012-02-03 01:13:49 +000014143// The EFLAGS operand of SelectItr might be missing a kill marker
14144// because there were multiple uses of EFLAGS, and ISel didn't know
14145// which to mark. Figure out whether SelectItr should have had a
14146// kill marker, and set it if it should. Returns the correct kill
14147// marker value.
14148static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
14149 MachineBasicBlock* BB,
14150 const TargetRegisterInfo* TRI) {
14151 // Scan forward through BB for a use/def of EFLAGS.
14152 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
14153 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000014154 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014155 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000014156 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014157 if (mi.definesRegister(X86::EFLAGS))
14158 break; // Should have kill-flag - update below.
14159 }
14160
14161 // If we hit the end of the block, check whether EFLAGS is live into a
14162 // successor.
14163 if (miI == BB->end()) {
14164 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
14165 sEnd = BB->succ_end();
14166 sItr != sEnd; ++sItr) {
14167 MachineBasicBlock* succ = *sItr;
14168 if (succ->isLiveIn(X86::EFLAGS))
14169 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000014170 }
14171 }
14172
Lang Hames6e3f7e42012-02-03 01:13:49 +000014173 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
14174 // out. SelectMI should have a kill flag on EFLAGS.
14175 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000014176 return true;
14177}
14178
Evan Cheng60c07e12006-07-05 22:17:51 +000014179MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000014180X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014181 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000014182 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14183 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000014184
Chris Lattner52600972009-09-02 05:57:00 +000014185 // To "insert" a SELECT_CC instruction, we actually have to insert the
14186 // diamond control-flow pattern. The incoming instruction knows the
14187 // destination vreg to set, the condition code register to branch on, the
14188 // true/false values to select between, and a branch opcode to use.
14189 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14190 MachineFunction::iterator It = BB;
14191 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000014192
Chris Lattner52600972009-09-02 05:57:00 +000014193 // thisMBB:
14194 // ...
14195 // TrueVal = ...
14196 // cmpTY ccX, r1, r2
14197 // bCC copy1MBB
14198 // fallthrough --> copy0MBB
14199 MachineBasicBlock *thisMBB = BB;
14200 MachineFunction *F = BB->getParent();
14201 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
14202 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000014203 F->insert(It, copy0MBB);
14204 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000014205
Bill Wendling730c07e2010-06-25 20:48:10 +000014206 // If the EFLAGS register isn't dead in the terminator, then claim that it's
14207 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000014208 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
14209 if (!MI->killsRegister(X86::EFLAGS) &&
14210 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
14211 copy0MBB->addLiveIn(X86::EFLAGS);
14212 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000014213 }
14214
Dan Gohman14152b42010-07-06 20:24:04 +000014215 // Transfer the remainder of BB and its successor edges to sinkMBB.
14216 sinkMBB->splice(sinkMBB->begin(), BB,
14217 llvm::next(MachineBasicBlock::iterator(MI)),
14218 BB->end());
14219 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
14220
14221 // Add the true and fallthrough blocks as its successors.
14222 BB->addSuccessor(copy0MBB);
14223 BB->addSuccessor(sinkMBB);
14224
14225 // Create the conditional branch instruction.
14226 unsigned Opc =
14227 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14228 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14229
Chris Lattner52600972009-09-02 05:57:00 +000014230 // copy0MBB:
14231 // %FalseValue = ...
14232 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014233 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014234
Chris Lattner52600972009-09-02 05:57:00 +000014235 // sinkMBB:
14236 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14237 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014238 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14239 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014240 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14241 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14242
Dan Gohman14152b42010-07-06 20:24:04 +000014243 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014244 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014245}
14246
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014247MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014248X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14249 bool Is64Bit) const {
14250 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14251 DebugLoc DL = MI->getDebugLoc();
14252 MachineFunction *MF = BB->getParent();
14253 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14254
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014255 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014256
14257 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14258 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14259
14260 // BB:
14261 // ... [Till the alloca]
14262 // If stacklet is not large enough, jump to mallocMBB
14263 //
14264 // bumpMBB:
14265 // Allocate by subtracting from RSP
14266 // Jump to continueMBB
14267 //
14268 // mallocMBB:
14269 // Allocate by call to runtime
14270 //
14271 // continueMBB:
14272 // ...
14273 // [rest of original BB]
14274 //
14275
14276 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14277 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14278 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14279
14280 MachineRegisterInfo &MRI = MF->getRegInfo();
14281 const TargetRegisterClass *AddrRegClass =
14282 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14283
14284 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14285 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14286 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014287 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014288 sizeVReg = MI->getOperand(1).getReg(),
14289 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14290
14291 MachineFunction::iterator MBBIter = BB;
14292 ++MBBIter;
14293
14294 MF->insert(MBBIter, bumpMBB);
14295 MF->insert(MBBIter, mallocMBB);
14296 MF->insert(MBBIter, continueMBB);
14297
14298 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14299 (MachineBasicBlock::iterator(MI)), BB->end());
14300 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14301
14302 // Add code to the main basic block to check if the stack limit has been hit,
14303 // and if so, jump to mallocMBB otherwise to bumpMBB.
14304 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014305 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014306 .addReg(tmpSPVReg).addReg(sizeVReg);
14307 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014308 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014309 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014310 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14311
14312 // bumpMBB simply decreases the stack pointer, since we know the current
14313 // stacklet has enough space.
14314 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014315 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014316 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014317 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014318 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14319
14320 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014321 const uint32_t *RegMask =
14322 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014323 if (Is64Bit) {
14324 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14325 .addReg(sizeVReg);
14326 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014327 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014328 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014329 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014330 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014331 } else {
14332 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14333 .addImm(12);
14334 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14335 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014336 .addExternalSymbol("__morestack_allocate_stack_space")
14337 .addRegMask(RegMask)
14338 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014339 }
14340
14341 if (!Is64Bit)
14342 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14343 .addImm(16);
14344
14345 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14346 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14347 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14348
14349 // Set up the CFG correctly.
14350 BB->addSuccessor(bumpMBB);
14351 BB->addSuccessor(mallocMBB);
14352 mallocMBB->addSuccessor(continueMBB);
14353 bumpMBB->addSuccessor(continueMBB);
14354
14355 // Take care of the PHI nodes.
14356 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14357 MI->getOperand(0).getReg())
14358 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14359 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14360
14361 // Delete the original pseudo instruction.
14362 MI->eraseFromParent();
14363
14364 // And we're done.
14365 return continueMBB;
14366}
14367
14368MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014369X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014370 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014371 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14372 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014373
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014374 assert(!Subtarget->isTargetEnvMacho());
14375
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014376 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14377 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014378
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014379 if (Subtarget->isTargetWin64()) {
14380 if (Subtarget->isTargetCygMing()) {
14381 // ___chkstk(Mingw64):
14382 // Clobbers R10, R11, RAX and EFLAGS.
14383 // Updates RSP.
14384 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14385 .addExternalSymbol("___chkstk")
14386 .addReg(X86::RAX, RegState::Implicit)
14387 .addReg(X86::RSP, RegState::Implicit)
14388 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14389 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14390 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14391 } else {
14392 // __chkstk(MSVCRT): does not update stack pointer.
14393 // Clobbers R10, R11 and EFLAGS.
14394 // FIXME: RAX(allocated size) might be reused and not killed.
14395 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14396 .addExternalSymbol("__chkstk")
14397 .addReg(X86::RAX, RegState::Implicit)
14398 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14399 // RAX has the offset to subtracted from RSP.
14400 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14401 .addReg(X86::RSP)
14402 .addReg(X86::RAX);
14403 }
14404 } else {
14405 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014406 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14407
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014408 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14409 .addExternalSymbol(StackProbeSymbol)
14410 .addReg(X86::EAX, RegState::Implicit)
14411 .addReg(X86::ESP, RegState::Implicit)
14412 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14413 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14414 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14415 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014416
Dan Gohman14152b42010-07-06 20:24:04 +000014417 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014418 return BB;
14419}
Chris Lattner52600972009-09-02 05:57:00 +000014420
14421MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014422X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14423 MachineBasicBlock *BB) const {
14424 // This is pretty easy. We're taking the value that we received from
14425 // our load from the relocation, sticking it in either RDI (x86-64)
14426 // or EAX and doing an indirect call. The return value will then
14427 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014428 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014429 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014430 DebugLoc DL = MI->getDebugLoc();
14431 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014432
14433 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014434 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014435
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014436 // Get a register mask for the lowered call.
14437 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14438 // proper register mask.
14439 const uint32_t *RegMask =
14440 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014441 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014442 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14443 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014444 .addReg(X86::RIP)
14445 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014446 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014447 MI->getOperand(3).getTargetFlags())
14448 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014449 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014450 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014451 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014452 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014453 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14454 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014455 .addReg(0)
14456 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014457 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014458 MI->getOperand(3).getTargetFlags())
14459 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014460 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014461 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014462 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014463 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014464 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14465 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014466 .addReg(TII->getGlobalBaseReg(F))
14467 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014468 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014469 MI->getOperand(3).getTargetFlags())
14470 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014471 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014472 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014473 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014474 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014475
Dan Gohman14152b42010-07-06 20:24:04 +000014476 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014477 return BB;
14478}
14479
14480MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014481X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14482 MachineBasicBlock *MBB) const {
14483 DebugLoc DL = MI->getDebugLoc();
14484 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14485
14486 MachineFunction *MF = MBB->getParent();
14487 MachineRegisterInfo &MRI = MF->getRegInfo();
14488
14489 const BasicBlock *BB = MBB->getBasicBlock();
14490 MachineFunction::iterator I = MBB;
14491 ++I;
14492
14493 // Memory Reference
14494 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14495 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14496
14497 unsigned DstReg;
14498 unsigned MemOpndSlot = 0;
14499
14500 unsigned CurOp = 0;
14501
14502 DstReg = MI->getOperand(CurOp++).getReg();
14503 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14504 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14505 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14506 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14507
14508 MemOpndSlot = CurOp;
14509
14510 MVT PVT = getPointerTy();
14511 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14512 "Invalid Pointer Size!");
14513
14514 // For v = setjmp(buf), we generate
14515 //
14516 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014517 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014518 // SjLjSetup restoreMBB
14519 //
14520 // mainMBB:
14521 // v_main = 0
14522 //
14523 // sinkMBB:
14524 // v = phi(main, restore)
14525 //
14526 // restoreMBB:
14527 // v_restore = 1
14528
14529 MachineBasicBlock *thisMBB = MBB;
14530 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14531 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14532 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14533 MF->insert(I, mainMBB);
14534 MF->insert(I, sinkMBB);
14535 MF->push_back(restoreMBB);
14536
14537 MachineInstrBuilder MIB;
14538
14539 // Transfer the remainder of BB and its successor edges to sinkMBB.
14540 sinkMBB->splice(sinkMBB->begin(), MBB,
14541 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14542 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14543
14544 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014545 unsigned PtrStoreOpc = 0;
14546 unsigned LabelReg = 0;
14547 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14548 Reloc::Model RM = getTargetMachine().getRelocationModel();
14549 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14550 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014551
Michael Liao281ae5a2012-10-17 02:22:27 +000014552 // Prepare IP either in reg or imm.
14553 if (!UseImmLabel) {
14554 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14555 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14556 LabelReg = MRI.createVirtualRegister(PtrRC);
14557 if (Subtarget->is64Bit()) {
14558 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14559 .addReg(X86::RIP)
14560 .addImm(0)
14561 .addReg(0)
14562 .addMBB(restoreMBB)
14563 .addReg(0);
14564 } else {
14565 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14566 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14567 .addReg(XII->getGlobalBaseReg(MF))
14568 .addImm(0)
14569 .addReg(0)
14570 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14571 .addReg(0);
14572 }
14573 } else
14574 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014575 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014576 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014577 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14578 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014579 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014580 else
14581 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14582 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014583 if (!UseImmLabel)
14584 MIB.addReg(LabelReg);
14585 else
14586 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014587 MIB.setMemRefs(MMOBegin, MMOEnd);
14588 // Setup
14589 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14590 .addMBB(restoreMBB);
14591 MIB.addRegMask(RegInfo->getNoPreservedMask());
14592 thisMBB->addSuccessor(mainMBB);
14593 thisMBB->addSuccessor(restoreMBB);
14594
14595 // mainMBB:
14596 // EAX = 0
14597 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14598 mainMBB->addSuccessor(sinkMBB);
14599
14600 // sinkMBB:
14601 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14602 TII->get(X86::PHI), DstReg)
14603 .addReg(mainDstReg).addMBB(mainMBB)
14604 .addReg(restoreDstReg).addMBB(restoreMBB);
14605
14606 // restoreMBB:
14607 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14608 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14609 restoreMBB->addSuccessor(sinkMBB);
14610
14611 MI->eraseFromParent();
14612 return sinkMBB;
14613}
14614
14615MachineBasicBlock *
14616X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14617 MachineBasicBlock *MBB) const {
14618 DebugLoc DL = MI->getDebugLoc();
14619 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14620
14621 MachineFunction *MF = MBB->getParent();
14622 MachineRegisterInfo &MRI = MF->getRegInfo();
14623
14624 // Memory Reference
14625 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14626 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14627
14628 MVT PVT = getPointerTy();
14629 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14630 "Invalid Pointer Size!");
14631
14632 const TargetRegisterClass *RC =
14633 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14634 unsigned Tmp = MRI.createVirtualRegister(RC);
14635 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14636 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14637 unsigned SP = RegInfo->getStackRegister();
14638
14639 MachineInstrBuilder MIB;
14640
Michael Liao281ae5a2012-10-17 02:22:27 +000014641 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14642 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014643
14644 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14645 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14646
14647 // Reload FP
14648 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14649 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14650 MIB.addOperand(MI->getOperand(i));
14651 MIB.setMemRefs(MMOBegin, MMOEnd);
14652 // Reload IP
14653 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14654 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14655 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014656 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014657 else
14658 MIB.addOperand(MI->getOperand(i));
14659 }
14660 MIB.setMemRefs(MMOBegin, MMOEnd);
14661 // Reload SP
14662 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14663 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14664 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014665 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014666 else
14667 MIB.addOperand(MI->getOperand(i));
14668 }
14669 MIB.setMemRefs(MMOBegin, MMOEnd);
14670 // Jump
14671 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14672
14673 MI->eraseFromParent();
14674 return MBB;
14675}
14676
14677MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014678X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014679 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014680 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014681 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014682 case X86::TAILJMPd64:
14683 case X86::TAILJMPr64:
14684 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014685 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014686 case X86::TCRETURNdi64:
14687 case X86::TCRETURNri64:
14688 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014689 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014690 case X86::WIN_ALLOCA:
14691 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014692 case X86::SEG_ALLOCA_32:
14693 return EmitLoweredSegAlloca(MI, BB, false);
14694 case X86::SEG_ALLOCA_64:
14695 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014696 case X86::TLSCall_32:
14697 case X86::TLSCall_64:
14698 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014699 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014700 case X86::CMOV_FR32:
14701 case X86::CMOV_FR64:
14702 case X86::CMOV_V4F32:
14703 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014704 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014705 case X86::CMOV_V8F32:
14706 case X86::CMOV_V4F64:
14707 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014708 case X86::CMOV_GR16:
14709 case X86::CMOV_GR32:
14710 case X86::CMOV_RFP32:
14711 case X86::CMOV_RFP64:
14712 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014713 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014714
Dale Johannesen849f2142007-07-03 00:53:03 +000014715 case X86::FP32_TO_INT16_IN_MEM:
14716 case X86::FP32_TO_INT32_IN_MEM:
14717 case X86::FP32_TO_INT64_IN_MEM:
14718 case X86::FP64_TO_INT16_IN_MEM:
14719 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014720 case X86::FP64_TO_INT64_IN_MEM:
14721 case X86::FP80_TO_INT16_IN_MEM:
14722 case X86::FP80_TO_INT32_IN_MEM:
14723 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014724 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14725 DebugLoc DL = MI->getDebugLoc();
14726
Evan Cheng60c07e12006-07-05 22:17:51 +000014727 // Change the floating point control register to use "round towards zero"
14728 // mode when truncating to an integer value.
14729 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014730 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014731 addFrameReference(BuildMI(*BB, MI, DL,
14732 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014733
14734 // Load the old value of the high byte of the control word...
14735 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014736 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014737 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014738 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014739
14740 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014741 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014742 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014743
14744 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014745 addFrameReference(BuildMI(*BB, MI, DL,
14746 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014747
14748 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014749 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014750 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014751
14752 // Get the X86 opcode to use.
14753 unsigned Opc;
14754 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014755 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014756 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14757 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14758 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14759 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14760 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14761 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014762 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14763 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14764 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014765 }
14766
14767 X86AddressMode AM;
14768 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014769 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014770 AM.BaseType = X86AddressMode::RegBase;
14771 AM.Base.Reg = Op.getReg();
14772 } else {
14773 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014774 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014775 }
14776 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014777 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014778 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014779 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014780 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014781 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014782 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014783 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014784 AM.GV = Op.getGlobal();
14785 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014786 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014787 }
Dan Gohman14152b42010-07-06 20:24:04 +000014788 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014789 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014790
14791 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014792 addFrameReference(BuildMI(*BB, MI, DL,
14793 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014794
Dan Gohman14152b42010-07-06 20:24:04 +000014795 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014796 return BB;
14797 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014798 // String/text processing lowering.
14799 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014800 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014801 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014802 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014803 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014804 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014805 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014806 case X86::VPCMPESTRM128MEM:
14807 assert(Subtarget->hasSSE42() &&
14808 "Target must have SSE4.2 or AVX features enabled");
14809 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014810
14811 // String/text processing lowering.
14812 case X86::PCMPISTRIREG:
14813 case X86::VPCMPISTRIREG:
14814 case X86::PCMPISTRIMEM:
14815 case X86::VPCMPISTRIMEM:
14816 case X86::PCMPESTRIREG:
14817 case X86::VPCMPESTRIREG:
14818 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014819 case X86::VPCMPESTRIMEM:
14820 assert(Subtarget->hasSSE42() &&
14821 "Target must have SSE4.2 or AVX features enabled");
14822 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014823
Craig Topper8aae8dd2012-11-10 08:57:41 +000014824 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014825 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014826 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014827
Michael Liaobe02a902012-11-08 07:28:54 +000014828 // xbegin
14829 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014830 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014831
Craig Topper8aae8dd2012-11-10 08:57:41 +000014832 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014833 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014834 case X86::ATOMAND16:
14835 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014836 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014837 // Fall through
14838 case X86::ATOMOR8:
14839 case X86::ATOMOR16:
14840 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014841 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014842 // Fall through
14843 case X86::ATOMXOR16:
14844 case X86::ATOMXOR8:
14845 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014846 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014847 // Fall through
14848 case X86::ATOMNAND8:
14849 case X86::ATOMNAND16:
14850 case X86::ATOMNAND32:
14851 case X86::ATOMNAND64:
14852 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014853 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014854 case X86::ATOMMAX16:
14855 case X86::ATOMMAX32:
14856 case X86::ATOMMAX64:
14857 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014858 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014859 case X86::ATOMMIN16:
14860 case X86::ATOMMIN32:
14861 case X86::ATOMMIN64:
14862 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014863 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014864 case X86::ATOMUMAX16:
14865 case X86::ATOMUMAX32:
14866 case X86::ATOMUMAX64:
14867 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014868 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014869 case X86::ATOMUMIN16:
14870 case X86::ATOMUMIN32:
14871 case X86::ATOMUMIN64:
14872 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014873
14874 // This group does 64-bit operations on a 32-bit host.
14875 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014876 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014877 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014878 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014879 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014880 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014881 case X86::ATOMMAX6432:
14882 case X86::ATOMMIN6432:
14883 case X86::ATOMUMAX6432:
14884 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014885 case X86::ATOMSWAP6432:
14886 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014887
Dan Gohmand6708ea2009-08-15 01:38:56 +000014888 case X86::VASTART_SAVE_XMM_REGS:
14889 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014890
14891 case X86::VAARG_64:
14892 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014893
14894 case X86::EH_SjLj_SetJmp32:
14895 case X86::EH_SjLj_SetJmp64:
14896 return emitEHSjLjSetJmp(MI, BB);
14897
14898 case X86::EH_SjLj_LongJmp32:
14899 case X86::EH_SjLj_LongJmp64:
14900 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014901 }
14902}
14903
14904//===----------------------------------------------------------------------===//
14905// X86 Optimization Hooks
14906//===----------------------------------------------------------------------===//
14907
Dan Gohman475871a2008-07-27 21:46:04 +000014908void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014909 APInt &KnownZero,
14910 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014911 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014912 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014913 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014914 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014915 assert((Opc >= ISD::BUILTIN_OP_END ||
14916 Opc == ISD::INTRINSIC_WO_CHAIN ||
14917 Opc == ISD::INTRINSIC_W_CHAIN ||
14918 Opc == ISD::INTRINSIC_VOID) &&
14919 "Should use MaskedValueIsZero if you don't know whether Op"
14920 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014921
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014922 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014923 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014924 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014925 case X86ISD::ADD:
14926 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014927 case X86ISD::ADC:
14928 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014929 case X86ISD::SMUL:
14930 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014931 case X86ISD::INC:
14932 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014933 case X86ISD::OR:
14934 case X86ISD::XOR:
14935 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014936 // These nodes' second result is a boolean.
14937 if (Op.getResNo() == 0)
14938 break;
14939 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014940 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014941 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014942 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014943 case ISD::INTRINSIC_WO_CHAIN: {
14944 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14945 unsigned NumLoBits = 0;
14946 switch (IntId) {
14947 default: break;
14948 case Intrinsic::x86_sse_movmsk_ps:
14949 case Intrinsic::x86_avx_movmsk_ps_256:
14950 case Intrinsic::x86_sse2_movmsk_pd:
14951 case Intrinsic::x86_avx_movmsk_pd_256:
14952 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014953 case Intrinsic::x86_sse2_pmovmskb_128:
14954 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014955 // High bits of movmskp{s|d}, pmovmskb are known zero.
14956 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014957 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014958 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14959 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14960 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14961 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14962 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14963 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014964 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014965 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014966 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014967 break;
14968 }
14969 }
14970 break;
14971 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014972 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014973}
Chris Lattner259e97c2006-01-31 19:43:35 +000014974
Owen Andersonbc146b02010-09-21 20:42:50 +000014975unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14976 unsigned Depth) const {
14977 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14978 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14979 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014980
Owen Andersonbc146b02010-09-21 20:42:50 +000014981 // Fallback case.
14982 return 1;
14983}
14984
Evan Cheng206ee9d2006-07-07 08:33:52 +000014985/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014986/// node is a GlobalAddress + offset.
14987bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014988 const GlobalValue* &GA,
14989 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014990 if (N->getOpcode() == X86ISD::Wrapper) {
14991 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014992 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014993 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014994 return true;
14995 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014996 }
Evan Chengad4196b2008-05-12 19:56:52 +000014997 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014998}
14999
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015000/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
15001/// same as extracting the high 128-bit part of 256-bit vector and then
15002/// inserting the result into the low part of a new 256-bit vector
15003static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
15004 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015005 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015006
15007 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000015008 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015009 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15010 SVOp->getMaskElt(j) >= 0)
15011 return false;
15012
15013 return true;
15014}
15015
15016/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
15017/// same as extracting the low 128-bit part of 256-bit vector and then
15018/// inserting the result into the high part of a new 256-bit vector
15019static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
15020 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015021 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015022
15023 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000015024 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015025 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15026 SVOp->getMaskElt(j) >= 0)
15027 return false;
15028
15029 return true;
15030}
15031
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015032/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
15033static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000015034 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015035 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015036 DebugLoc dl = N->getDebugLoc();
15037 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
15038 SDValue V1 = SVOp->getOperand(0);
15039 SDValue V2 = SVOp->getOperand(1);
15040 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015041 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015042
15043 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
15044 V2.getOpcode() == ISD::CONCAT_VECTORS) {
15045 //
15046 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000015047 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015048 // V UNDEF BUILD_VECTOR UNDEF
15049 // \ / \ /
15050 // CONCAT_VECTOR CONCAT_VECTOR
15051 // \ /
15052 // \ /
15053 // RESULT: V + zero extended
15054 //
15055 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
15056 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
15057 V1.getOperand(1).getOpcode() != ISD::UNDEF)
15058 return SDValue();
15059
15060 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
15061 return SDValue();
15062
15063 // To match the shuffle mask, the first half of the mask should
15064 // be exactly the first vector, and all the rest a splat with the
15065 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000015066 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015067 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
15068 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
15069 return SDValue();
15070
Chad Rosier3d1161e2012-01-03 21:05:52 +000015071 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
15072 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000015073 if (Ld->hasNUsesOfValue(1, 0)) {
15074 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
15075 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
15076 SDValue ResNode =
Michael Liao0ee17002013-04-19 04:03:37 +000015077 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops,
15078 array_lengthof(Ops),
Chad Rosier42726832012-05-07 18:47:44 +000015079 Ld->getMemoryVT(),
15080 Ld->getPointerInfo(),
15081 Ld->getAlignment(),
15082 false/*isVolatile*/, true/*ReadMem*/,
15083 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000015084
15085 // Make sure the newly-created LOAD is in the same position as Ld in
15086 // terms of dependency. We create a TokenFactor for Ld and ResNode,
15087 // and update uses of Ld's output chain to use the TokenFactor.
15088 if (Ld->hasAnyUseOfValue(1)) {
15089 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
15090 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
15091 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
15092 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
15093 SDValue(ResNode.getNode(), 1));
15094 }
15095
Chad Rosier42726832012-05-07 18:47:44 +000015096 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
15097 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000015098 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000015099
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015100 // Emit a zeroed vector and insert the desired subvector on its
15101 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015102 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000015103 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015104 return DCI.CombineTo(N, InsV);
15105 }
15106
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015107 //===--------------------------------------------------------------------===//
15108 // Combine some shuffles into subvector extracts and inserts:
15109 //
15110
15111 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
15112 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015113 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
15114 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015115 return DCI.CombineTo(N, InsV);
15116 }
15117
15118 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
15119 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015120 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
15121 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015122 return DCI.CombineTo(N, InsV);
15123 }
15124
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015125 return SDValue();
15126}
15127
15128/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000015129static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015130 TargetLowering::DAGCombinerInfo &DCI,
15131 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000015132 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000015133 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000015134
Mon P Wanga0fd0d52010-12-19 23:55:53 +000015135 // Don't create instructions with illegal types after legalize types has run.
15136 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15137 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
15138 return SDValue();
15139
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015140 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015141 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015142 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015143 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015144
15145 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015146 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015147 return SDValue();
15148
15149 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
15150 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
15151 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000015152 SmallVector<SDValue, 16> Elts;
15153 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015154 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000015155
Nate Begemanfdea31a2010-03-24 20:49:50 +000015156 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000015157}
Evan Chengd880b972008-05-09 21:53:03 +000015158
Nadav Roteme12bf182013-01-04 17:35:21 +000015159/// PerformTruncateCombine - Converts truncate operation to
15160/// a sequence of vector shuffle operations.
15161/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000015162static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
15163 TargetLowering::DAGCombinerInfo &DCI,
15164 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015165 return SDValue();
15166}
15167
Craig Topper89f4e662012-03-20 07:17:59 +000015168/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
15169/// specific shuffle of a load can be folded into a single element load.
15170/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
15171/// shuffles have been customed lowered so we need to handle those here.
15172static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
15173 TargetLowering::DAGCombinerInfo &DCI) {
15174 if (DCI.isBeforeLegalizeOps())
15175 return SDValue();
15176
15177 SDValue InVec = N->getOperand(0);
15178 SDValue EltNo = N->getOperand(1);
15179
15180 if (!isa<ConstantSDNode>(EltNo))
15181 return SDValue();
15182
15183 EVT VT = InVec.getValueType();
15184
15185 bool HasShuffleIntoBitcast = false;
15186 if (InVec.getOpcode() == ISD::BITCAST) {
15187 // Don't duplicate a load with other uses.
15188 if (!InVec.hasOneUse())
15189 return SDValue();
15190 EVT BCVT = InVec.getOperand(0).getValueType();
15191 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
15192 return SDValue();
15193 InVec = InVec.getOperand(0);
15194 HasShuffleIntoBitcast = true;
15195 }
15196
15197 if (!isTargetShuffle(InVec.getOpcode()))
15198 return SDValue();
15199
15200 // Don't duplicate a load with other uses.
15201 if (!InVec.hasOneUse())
15202 return SDValue();
15203
15204 SmallVector<int, 16> ShuffleMask;
15205 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000015206 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
15207 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000015208 return SDValue();
15209
15210 // Select the input vector, guarding against out of range extract vector.
15211 unsigned NumElems = VT.getVectorNumElements();
15212 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
15213 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
15214 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
15215 : InVec.getOperand(1);
15216
15217 // If inputs to shuffle are the same for both ops, then allow 2 uses
15218 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
15219
15220 if (LdNode.getOpcode() == ISD::BITCAST) {
15221 // Don't duplicate a load with other uses.
15222 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
15223 return SDValue();
15224
15225 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15226 LdNode = LdNode.getOperand(0);
15227 }
15228
15229 if (!ISD::isNormalLoad(LdNode.getNode()))
15230 return SDValue();
15231
15232 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15233
15234 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15235 return SDValue();
15236
15237 if (HasShuffleIntoBitcast) {
15238 // If there's a bitcast before the shuffle, check if the load type and
15239 // alignment is valid.
15240 unsigned Align = LN0->getAlignment();
15241 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015242 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015243 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15244
15245 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15246 return SDValue();
15247 }
15248
15249 // All checks match so transform back to vector_shuffle so that DAG combiner
15250 // can finish the job
15251 DebugLoc dl = N->getDebugLoc();
15252
15253 // Create shuffle node taking into account the case that its a unary shuffle
15254 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15255 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15256 InVec.getOperand(0), Shuffle,
15257 &ShuffleMask[0]);
15258 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15259 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15260 EltNo);
15261}
15262
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015263/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15264/// generation and convert it from being a bunch of shuffles and extracts
15265/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015266static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015267 TargetLowering::DAGCombinerInfo &DCI) {
15268 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15269 if (NewOp.getNode())
15270 return NewOp;
15271
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015272 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015273 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15274 // from mmx to v2i32 has a single usage.
15275 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15276 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15277 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
15278 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
15279 N->getValueType(0),
15280 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015281
15282 // Only operate on vectors of 4 elements, where the alternative shuffling
15283 // gets to be more expensive.
15284 if (InputVector.getValueType() != MVT::v4i32)
15285 return SDValue();
15286
15287 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15288 // single use which is a sign-extend or zero-extend, and all elements are
15289 // used.
15290 SmallVector<SDNode *, 4> Uses;
15291 unsigned ExtractedElements = 0;
15292 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15293 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15294 if (UI.getUse().getResNo() != InputVector.getResNo())
15295 return SDValue();
15296
15297 SDNode *Extract = *UI;
15298 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15299 return SDValue();
15300
15301 if (Extract->getValueType(0) != MVT::i32)
15302 return SDValue();
15303 if (!Extract->hasOneUse())
15304 return SDValue();
15305 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15306 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15307 return SDValue();
15308 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15309 return SDValue();
15310
15311 // Record which element was extracted.
15312 ExtractedElements |=
15313 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15314
15315 Uses.push_back(Extract);
15316 }
15317
15318 // If not all the elements were used, this may not be worthwhile.
15319 if (ExtractedElements != 15)
15320 return SDValue();
15321
15322 // Ok, we've now decided to do the transformation.
15323 DebugLoc dl = InputVector.getDebugLoc();
15324
15325 // Store the value to a temporary stack slot.
15326 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015327 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15328 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015329
15330 // Replace each use (extract) with a load of the appropriate element.
15331 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15332 UE = Uses.end(); UI != UE; ++UI) {
15333 SDNode *Extract = *UI;
15334
Nadav Rotem86694292011-05-17 08:31:57 +000015335 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015336 SDValue Idx = Extract->getOperand(1);
15337 unsigned EltSize =
15338 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15339 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015340 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015341 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15342
Nadav Rotem86694292011-05-17 08:31:57 +000015343 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015344 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015345
15346 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015347 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015348 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015349 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015350
15351 // Replace the exact with the load.
15352 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15353 }
15354
15355 // The replacement was made in place; don't return anything.
15356 return SDValue();
15357}
15358
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015359/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15360static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15361 SDValue RHS, SelectionDAG &DAG,
15362 const X86Subtarget *Subtarget) {
15363 if (!VT.isVector())
15364 return 0;
15365
15366 switch (VT.getSimpleVT().SimpleTy) {
15367 default: return 0;
15368 case MVT::v32i8:
15369 case MVT::v16i16:
15370 case MVT::v8i32:
15371 if (!Subtarget->hasAVX2())
15372 return 0;
15373 case MVT::v16i8:
15374 case MVT::v8i16:
15375 case MVT::v4i32:
15376 if (!Subtarget->hasSSE2())
15377 return 0;
15378 }
15379
15380 // SSE2 has only a small subset of the operations.
15381 bool hasUnsigned = Subtarget->hasSSE41() ||
15382 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15383 bool hasSigned = Subtarget->hasSSE41() ||
15384 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15385
15386 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15387
15388 // Check for x CC y ? x : y.
15389 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15390 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15391 switch (CC) {
15392 default: break;
15393 case ISD::SETULT:
15394 case ISD::SETULE:
15395 return hasUnsigned ? X86ISD::UMIN : 0;
15396 case ISD::SETUGT:
15397 case ISD::SETUGE:
15398 return hasUnsigned ? X86ISD::UMAX : 0;
15399 case ISD::SETLT:
15400 case ISD::SETLE:
15401 return hasSigned ? X86ISD::SMIN : 0;
15402 case ISD::SETGT:
15403 case ISD::SETGE:
15404 return hasSigned ? X86ISD::SMAX : 0;
15405 }
15406 // Check for x CC y ? y : x -- a min/max with reversed arms.
15407 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15408 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15409 switch (CC) {
15410 default: break;
15411 case ISD::SETULT:
15412 case ISD::SETULE:
15413 return hasUnsigned ? X86ISD::UMAX : 0;
15414 case ISD::SETUGT:
15415 case ISD::SETUGE:
15416 return hasUnsigned ? X86ISD::UMIN : 0;
15417 case ISD::SETLT:
15418 case ISD::SETLE:
15419 return hasSigned ? X86ISD::SMAX : 0;
15420 case ISD::SETGT:
15421 case ISD::SETGE:
15422 return hasSigned ? X86ISD::SMIN : 0;
15423 }
15424 }
15425
15426 return 0;
15427}
15428
Duncan Sands6bcd2192011-09-17 16:49:39 +000015429/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15430/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015431static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015432 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015433 const X86Subtarget *Subtarget) {
15434 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015435 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015436 // Get the LHS/RHS of the select.
15437 SDValue LHS = N->getOperand(1);
15438 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015439 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015440
Dan Gohman670e5392009-09-21 18:03:22 +000015441 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015442 // instructions match the semantics of the common C idiom x<y?x:y but not
15443 // x<=y?x:y, because of how they handle negative zero (which can be
15444 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015445 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15446 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015447 (Subtarget->hasSSE2() ||
15448 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015449 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015450
Chris Lattner47b4ce82009-03-11 05:48:52 +000015451 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015452 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015453 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15454 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015455 switch (CC) {
15456 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015457 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015458 // Converting this to a min would handle NaNs incorrectly, and swapping
15459 // the operands would cause it to handle comparisons between positive
15460 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015461 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015462 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015463 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15464 break;
15465 std::swap(LHS, RHS);
15466 }
Dan Gohman670e5392009-09-21 18:03:22 +000015467 Opcode = X86ISD::FMIN;
15468 break;
15469 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015470 // Converting this to a min would handle comparisons between positive
15471 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015472 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015473 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15474 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015475 Opcode = X86ISD::FMIN;
15476 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015477 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015478 // Converting this to a min would handle both negative zeros and NaNs
15479 // incorrectly, but we can swap the operands to fix both.
15480 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015481 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015482 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015483 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015484 Opcode = X86ISD::FMIN;
15485 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015486
Dan Gohman670e5392009-09-21 18:03:22 +000015487 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015488 // Converting this to a max would handle comparisons between positive
15489 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015490 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015491 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015492 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015493 Opcode = X86ISD::FMAX;
15494 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015495 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015496 // Converting this to a max would handle NaNs incorrectly, and swapping
15497 // the operands would cause it to handle comparisons between positive
15498 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015499 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015500 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015501 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15502 break;
15503 std::swap(LHS, RHS);
15504 }
Dan Gohman670e5392009-09-21 18:03:22 +000015505 Opcode = X86ISD::FMAX;
15506 break;
15507 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015508 // Converting this to a max would handle both negative zeros and NaNs
15509 // incorrectly, but we can swap the operands to fix both.
15510 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015511 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015512 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015513 case ISD::SETGE:
15514 Opcode = X86ISD::FMAX;
15515 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015516 }
Dan Gohman670e5392009-09-21 18:03:22 +000015517 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015518 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15519 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015520 switch (CC) {
15521 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015522 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015523 // Converting this to a min would handle comparisons between positive
15524 // and negative zero incorrectly, and swapping the operands would
15525 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015526 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015527 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015528 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015529 break;
15530 std::swap(LHS, RHS);
15531 }
Dan Gohman670e5392009-09-21 18:03:22 +000015532 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015533 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015534 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015535 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015536 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015537 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15538 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015539 Opcode = X86ISD::FMIN;
15540 break;
15541 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015542 // Converting this to a min would handle both negative zeros and NaNs
15543 // incorrectly, but we can swap the operands to fix both.
15544 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015545 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015546 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015547 case ISD::SETGE:
15548 Opcode = X86ISD::FMIN;
15549 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015550
Dan Gohman670e5392009-09-21 18:03:22 +000015551 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015552 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015553 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015554 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015555 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015556 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015557 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015558 // Converting this to a max would handle comparisons between positive
15559 // and negative zero incorrectly, and swapping the operands would
15560 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015561 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015562 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015563 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015564 break;
15565 std::swap(LHS, RHS);
15566 }
Dan Gohman670e5392009-09-21 18:03:22 +000015567 Opcode = X86ISD::FMAX;
15568 break;
15569 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015570 // Converting this to a max would handle both negative zeros and NaNs
15571 // incorrectly, but we can swap the operands to fix both.
15572 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015573 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015574 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015575 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015576 Opcode = X86ISD::FMAX;
15577 break;
15578 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015579 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015580
Chris Lattner47b4ce82009-03-11 05:48:52 +000015581 if (Opcode)
15582 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015583 }
Eric Christopherfd179292009-08-27 18:07:15 +000015584
Chris Lattnerd1980a52009-03-12 06:52:53 +000015585 // If this is a select between two integer constants, try to do some
15586 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015587 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15588 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015589 // Don't do this for crazy integer types.
15590 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15591 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015592 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015593 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015594
Chris Lattnercee56e72009-03-13 05:53:31 +000015595 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015596 // Efficiently invertible.
15597 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15598 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15599 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15600 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015601 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015602 }
Eric Christopherfd179292009-08-27 18:07:15 +000015603
Chris Lattnerd1980a52009-03-12 06:52:53 +000015604 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015605 if (FalseC->getAPIntValue() == 0 &&
15606 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015607 if (NeedsCondInvert) // Invert the condition if needed.
15608 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15609 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015610
Chris Lattnerd1980a52009-03-12 06:52:53 +000015611 // Zero extend the condition if needed.
15612 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015613
Chris Lattnercee56e72009-03-13 05:53:31 +000015614 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015615 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015616 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015617 }
Eric Christopherfd179292009-08-27 18:07:15 +000015618
Chris Lattner97a29a52009-03-13 05:22:11 +000015619 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015620 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015621 if (NeedsCondInvert) // Invert the condition if needed.
15622 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15623 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015624
Chris Lattner97a29a52009-03-13 05:22:11 +000015625 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015626 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15627 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015628 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015629 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015630 }
Eric Christopherfd179292009-08-27 18:07:15 +000015631
Chris Lattnercee56e72009-03-13 05:53:31 +000015632 // Optimize cases that will turn into an LEA instruction. This requires
15633 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015634 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015635 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015636 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015637
Chris Lattnercee56e72009-03-13 05:53:31 +000015638 bool isFastMultiplier = false;
15639 if (Diff < 10) {
15640 switch ((unsigned char)Diff) {
15641 default: break;
15642 case 1: // result = add base, cond
15643 case 2: // result = lea base( , cond*2)
15644 case 3: // result = lea base(cond, cond*2)
15645 case 4: // result = lea base( , cond*4)
15646 case 5: // result = lea base(cond, cond*4)
15647 case 8: // result = lea base( , cond*8)
15648 case 9: // result = lea base(cond, cond*8)
15649 isFastMultiplier = true;
15650 break;
15651 }
15652 }
Eric Christopherfd179292009-08-27 18:07:15 +000015653
Chris Lattnercee56e72009-03-13 05:53:31 +000015654 if (isFastMultiplier) {
15655 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15656 if (NeedsCondInvert) // Invert the condition if needed.
15657 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15658 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015659
Chris Lattnercee56e72009-03-13 05:53:31 +000015660 // Zero extend the condition if needed.
15661 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15662 Cond);
15663 // Scale the condition by the difference.
15664 if (Diff != 1)
15665 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15666 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015667
Chris Lattnercee56e72009-03-13 05:53:31 +000015668 // Add the base if non-zero.
15669 if (FalseC->getAPIntValue() != 0)
15670 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15671 SDValue(FalseC, 0));
15672 return Cond;
15673 }
Eric Christopherfd179292009-08-27 18:07:15 +000015674 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015675 }
15676 }
Eric Christopherfd179292009-08-27 18:07:15 +000015677
Evan Cheng56f582d2012-01-04 01:41:39 +000015678 // Canonicalize max and min:
15679 // (x > y) ? x : y -> (x >= y) ? x : y
15680 // (x < y) ? x : y -> (x <= y) ? x : y
15681 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15682 // the need for an extra compare
15683 // against zero. e.g.
15684 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15685 // subl %esi, %edi
15686 // testl %edi, %edi
15687 // movl $0, %eax
15688 // cmovgl %edi, %eax
15689 // =>
15690 // xorl %eax, %eax
15691 // subl %esi, $edi
15692 // cmovsl %eax, %edi
15693 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15694 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15695 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15696 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15697 switch (CC) {
15698 default: break;
15699 case ISD::SETLT:
15700 case ISD::SETGT: {
15701 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15702 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15703 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15704 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15705 }
15706 }
15707 }
15708
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015709 // Match VSELECTs into subs with unsigned saturation.
15710 if (!DCI.isBeforeLegalize() &&
15711 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15712 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15713 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15714 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15715 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15716
15717 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15718 // left side invert the predicate to simplify logic below.
15719 SDValue Other;
15720 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15721 Other = RHS;
15722 CC = ISD::getSetCCInverse(CC, true);
15723 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15724 Other = LHS;
15725 }
15726
15727 if (Other.getNode() && Other->getNumOperands() == 2 &&
15728 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15729 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15730 SDValue CondRHS = Cond->getOperand(1);
15731
15732 // Look for a general sub with unsigned saturation first.
15733 // x >= y ? x-y : 0 --> subus x, y
15734 // x > y ? x-y : 0 --> subus x, y
15735 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15736 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15737 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15738
15739 // If the RHS is a constant we have to reverse the const canonicalization.
15740 // x > C-1 ? x+-C : 0 --> subus x, C
15741 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15742 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15743 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015744 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015745 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015746 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015747 }
15748
15749 // Another special case: If C was a sign bit, the sub has been
15750 // canonicalized into a xor.
15751 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15752 // it's safe to decanonicalize the xor?
15753 // x s< 0 ? x^C : 0 --> subus x, C
15754 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15755 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15756 isSplatVector(OpRHS.getNode())) {
15757 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15758 if (A.isSignBit())
15759 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15760 }
15761 }
15762 }
15763
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015764 // Try to match a min/max vector operation.
15765 if (!DCI.isBeforeLegalize() &&
15766 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15767 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15768 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15769
Michael Liaobf538412013-04-11 05:15:54 +000015770 // Simplify vector selection if the selector will be produced by CMPP*/PCMP*.
15771 if (!DCI.isBeforeLegalize() && N->getOpcode() == ISD::VSELECT &&
15772 Cond.getOpcode() == ISD::SETCC) {
15773
15774 assert(Cond.getValueType().isVector() &&
15775 "vector select expects a vector selector!");
15776
15777 EVT IntVT = Cond.getValueType();
15778 bool TValIsAllOnes = ISD::isBuildVectorAllOnes(LHS.getNode());
15779 bool FValIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
15780
15781 if (!TValIsAllOnes && !FValIsAllZeros) {
15782 // Try invert the condition if true value is not all 1s and false value
15783 // is not all 0s.
15784 bool TValIsAllZeros = ISD::isBuildVectorAllZeros(LHS.getNode());
15785 bool FValIsAllOnes = ISD::isBuildVectorAllOnes(RHS.getNode());
15786
15787 if (TValIsAllZeros || FValIsAllOnes) {
15788 SDValue CC = Cond.getOperand(2);
15789 ISD::CondCode NewCC =
15790 ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
15791 Cond.getOperand(0).getValueType().isInteger());
15792 Cond = DAG.getSetCC(DL, IntVT, Cond.getOperand(0), Cond.getOperand(1), NewCC);
15793 std::swap(LHS, RHS);
15794 TValIsAllOnes = FValIsAllOnes;
15795 FValIsAllZeros = TValIsAllZeros;
15796 }
15797 }
15798
15799 if (TValIsAllOnes || FValIsAllZeros) {
15800 SDValue Ret;
15801
15802 if (TValIsAllOnes && FValIsAllZeros)
15803 Ret = Cond;
15804 else if (TValIsAllOnes)
15805 Ret = DAG.getNode(ISD::OR, DL, IntVT, Cond,
15806 DAG.getNode(ISD::BITCAST, DL, IntVT, RHS));
15807 else if (FValIsAllZeros)
15808 Ret = DAG.getNode(ISD::AND, DL, IntVT, Cond,
15809 DAG.getNode(ISD::BITCAST, DL, IntVT, LHS));
15810
15811 return DAG.getNode(ISD::BITCAST, DL, VT, Ret);
15812 }
15813 }
15814
Nadav Rotemcc616562012-01-15 19:27:55 +000015815 // If we know that this node is legal then we know that it is going to be
15816 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15817 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15818 // to simplify previous instructions.
15819 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15820 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015821 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015822 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015823
15824 // Don't optimize vector selects that map to mask-registers.
15825 if (BitWidth == 1)
15826 return SDValue();
15827
Nadav Rotemcc616562012-01-15 19:27:55 +000015828 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15829 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15830
15831 APInt KnownZero, KnownOne;
15832 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15833 DCI.isBeforeLegalizeOps());
15834 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15835 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15836 DCI.CommitTargetLoweringOpt(TLO);
15837 }
15838
Dan Gohman475871a2008-07-27 21:46:04 +000015839 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015840}
15841
Michael Liao2a33cec2012-08-10 19:58:13 +000015842// Check whether a boolean test is testing a boolean value generated by
15843// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15844// code.
15845//
15846// Simplify the following patterns:
15847// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15848// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15849// to (Op EFLAGS Cond)
15850//
15851// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15852// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15853// to (Op EFLAGS !Cond)
15854//
15855// where Op could be BRCOND or CMOV.
15856//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015857static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015858 // Quit if not CMP and SUB with its value result used.
15859 if (Cmp.getOpcode() != X86ISD::CMP &&
15860 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15861 return SDValue();
15862
15863 // Quit if not used as a boolean value.
15864 if (CC != X86::COND_E && CC != X86::COND_NE)
15865 return SDValue();
15866
15867 // Check CMP operands. One of them should be 0 or 1 and the other should be
15868 // an SetCC or extended from it.
15869 SDValue Op1 = Cmp.getOperand(0);
15870 SDValue Op2 = Cmp.getOperand(1);
15871
15872 SDValue SetCC;
15873 const ConstantSDNode* C = 0;
15874 bool needOppositeCond = (CC == X86::COND_E);
Michael Liao959ddbb2013-04-11 04:43:09 +000015875 bool checkAgainstTrue = false; // Is it a comparison against 1?
Michael Liao2a33cec2012-08-10 19:58:13 +000015876
15877 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15878 SetCC = Op2;
15879 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15880 SetCC = Op1;
15881 else // Quit if all operands are not constants.
15882 return SDValue();
15883
Michael Liao959ddbb2013-04-11 04:43:09 +000015884 if (C->getZExtValue() == 1) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015885 needOppositeCond = !needOppositeCond;
Michael Liao959ddbb2013-04-11 04:43:09 +000015886 checkAgainstTrue = true;
15887 } else if (C->getZExtValue() != 0)
Michael Liao2a33cec2012-08-10 19:58:13 +000015888 // Quit if the constant is neither 0 or 1.
15889 return SDValue();
15890
Michael Liao959ddbb2013-04-11 04:43:09 +000015891 bool truncatedToBoolWithAnd = false;
15892 // Skip (zext $x), (trunc $x), or (and $x, 1) node.
15893 while (SetCC.getOpcode() == ISD::ZERO_EXTEND ||
15894 SetCC.getOpcode() == ISD::TRUNCATE ||
15895 SetCC.getOpcode() == ISD::AND) {
15896 if (SetCC.getOpcode() == ISD::AND) {
15897 int OpIdx = -1;
15898 ConstantSDNode *CS;
15899 if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(0))) &&
15900 CS->getZExtValue() == 1)
15901 OpIdx = 1;
15902 if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(1))) &&
15903 CS->getZExtValue() == 1)
15904 OpIdx = 0;
15905 if (OpIdx == -1)
15906 break;
15907 SetCC = SetCC.getOperand(OpIdx);
15908 truncatedToBoolWithAnd = true;
15909 } else
15910 SetCC = SetCC.getOperand(0);
15911 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015912
Michael Liao7fdc66b2012-09-10 16:36:16 +000015913 switch (SetCC.getOpcode()) {
Michael Liao959ddbb2013-04-11 04:43:09 +000015914 case X86ISD::SETCC_CARRY:
15915 // Since SETCC_CARRY gives output based on R = CF ? ~0 : 0, it's unsafe to
15916 // simplify it if the result of SETCC_CARRY is not canonicalized to 0 or 1,
15917 // i.e. it's a comparison against true but the result of SETCC_CARRY is not
15918 // truncated to i1 using 'and'.
15919 if (checkAgainstTrue && !truncatedToBoolWithAnd)
15920 break;
15921 assert(X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B &&
15922 "Invalid use of SETCC_CARRY!");
15923 // FALL THROUGH
Michael Liao7fdc66b2012-09-10 16:36:16 +000015924 case X86ISD::SETCC:
15925 // Set the condition code or opposite one if necessary.
15926 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15927 if (needOppositeCond)
15928 CC = X86::GetOppositeBranchCondition(CC);
15929 return SetCC.getOperand(1);
15930 case X86ISD::CMOV: {
15931 // Check whether false/true value has canonical one, i.e. 0 or 1.
15932 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15933 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15934 // Quit if true value is not a constant.
15935 if (!TVal)
15936 return SDValue();
15937 // Quit if false value is not a constant.
15938 if (!FVal) {
Michael Liao7fdc66b2012-09-10 16:36:16 +000015939 SDValue Op = SetCC.getOperand(0);
Michael Liao258d9b72013-03-28 23:38:52 +000015940 // Skip 'zext' or 'trunc' node.
15941 if (Op.getOpcode() == ISD::ZERO_EXTEND ||
15942 Op.getOpcode() == ISD::TRUNCATE)
15943 Op = Op.getOperand(0);
Michael Liaoc26392a2013-03-28 23:41:26 +000015944 // A special case for rdrand/rdseed, where 0 is set if false cond is
15945 // found.
15946 if ((Op.getOpcode() != X86ISD::RDRAND &&
15947 Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0)
Michael Liao7fdc66b2012-09-10 16:36:16 +000015948 return SDValue();
15949 }
15950 // Quit if false value is not the constant 0 or 1.
15951 bool FValIsFalse = true;
15952 if (FVal && FVal->getZExtValue() != 0) {
15953 if (FVal->getZExtValue() != 1)
15954 return SDValue();
15955 // If FVal is 1, opposite cond is needed.
15956 needOppositeCond = !needOppositeCond;
15957 FValIsFalse = false;
15958 }
15959 // Quit if TVal is not the constant opposite of FVal.
15960 if (FValIsFalse && TVal->getZExtValue() != 1)
15961 return SDValue();
15962 if (!FValIsFalse && TVal->getZExtValue() != 0)
15963 return SDValue();
15964 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15965 if (needOppositeCond)
15966 CC = X86::GetOppositeBranchCondition(CC);
15967 return SetCC.getOperand(3);
15968 }
15969 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015970
Michael Liao7fdc66b2012-09-10 16:36:16 +000015971 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015972}
15973
Chris Lattnerd1980a52009-03-12 06:52:53 +000015974/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15975static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015976 TargetLowering::DAGCombinerInfo &DCI,
15977 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015978 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015979
Chris Lattnerd1980a52009-03-12 06:52:53 +000015980 // If the flag operand isn't dead, don't touch this CMOV.
15981 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15982 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015983
Evan Chengb5a55d92011-05-24 01:48:22 +000015984 SDValue FalseOp = N->getOperand(0);
15985 SDValue TrueOp = N->getOperand(1);
15986 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15987 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015988
Evan Chengb5a55d92011-05-24 01:48:22 +000015989 if (CC == X86::COND_E || CC == X86::COND_NE) {
15990 switch (Cond.getOpcode()) {
15991 default: break;
15992 case X86ISD::BSR:
15993 case X86ISD::BSF:
15994 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15995 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15996 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15997 }
15998 }
15999
Michael Liao2a33cec2012-08-10 19:58:13 +000016000 SDValue Flags;
16001
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016002 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000016003 if (Flags.getNode() &&
16004 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000016005 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016006 SDValue Ops[] = { FalseOp, TrueOp,
16007 DAG.getConstant(CC, MVT::i8), Flags };
16008 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
16009 Ops, array_lengthof(Ops));
16010 }
16011
Chris Lattnerd1980a52009-03-12 06:52:53 +000016012 // If this is a select between two integer constants, try to do some
16013 // optimizations. Note that the operands are ordered the opposite of SELECT
16014 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000016015 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
16016 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000016017 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
16018 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000016019 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
16020 CC = X86::GetOppositeBranchCondition(CC);
16021 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016022 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000016023 }
Eric Christopherfd179292009-08-27 18:07:15 +000016024
Chris Lattnerd1980a52009-03-12 06:52:53 +000016025 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000016026 // This is efficient for any integer data type (including i8/i16) and
16027 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000016028 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016029 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16030 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016031
Chris Lattnerd1980a52009-03-12 06:52:53 +000016032 // Zero extend the condition if needed.
16033 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016034
Chris Lattnerd1980a52009-03-12 06:52:53 +000016035 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
16036 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000016037 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000016038 if (N->getNumValues() == 2) // Dead flag value?
16039 return DCI.CombineTo(N, Cond, SDValue());
16040 return Cond;
16041 }
Eric Christopherfd179292009-08-27 18:07:15 +000016042
Chris Lattnercee56e72009-03-13 05:53:31 +000016043 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
16044 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000016045 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016046 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16047 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016048
Chris Lattner97a29a52009-03-13 05:22:11 +000016049 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000016050 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
16051 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000016052 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16053 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000016054
Chris Lattner97a29a52009-03-13 05:22:11 +000016055 if (N->getNumValues() == 2) // Dead flag value?
16056 return DCI.CombineTo(N, Cond, SDValue());
16057 return Cond;
16058 }
Eric Christopherfd179292009-08-27 18:07:15 +000016059
Chris Lattnercee56e72009-03-13 05:53:31 +000016060 // Optimize cases that will turn into an LEA instruction. This requires
16061 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000016062 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000016063 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000016064 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000016065
Chris Lattnercee56e72009-03-13 05:53:31 +000016066 bool isFastMultiplier = false;
16067 if (Diff < 10) {
16068 switch ((unsigned char)Diff) {
16069 default: break;
16070 case 1: // result = add base, cond
16071 case 2: // result = lea base( , cond*2)
16072 case 3: // result = lea base(cond, cond*2)
16073 case 4: // result = lea base( , cond*4)
16074 case 5: // result = lea base(cond, cond*4)
16075 case 8: // result = lea base( , cond*8)
16076 case 9: // result = lea base(cond, cond*8)
16077 isFastMultiplier = true;
16078 break;
16079 }
16080 }
Eric Christopherfd179292009-08-27 18:07:15 +000016081
Chris Lattnercee56e72009-03-13 05:53:31 +000016082 if (isFastMultiplier) {
16083 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000016084 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16085 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000016086 // Zero extend the condition if needed.
16087 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
16088 Cond);
16089 // Scale the condition by the difference.
16090 if (Diff != 1)
16091 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
16092 DAG.getConstant(Diff, Cond.getValueType()));
16093
16094 // Add the base if non-zero.
16095 if (FalseC->getAPIntValue() != 0)
16096 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16097 SDValue(FalseC, 0));
16098 if (N->getNumValues() == 2) // Dead flag value?
16099 return DCI.CombineTo(N, Cond, SDValue());
16100 return Cond;
16101 }
Eric Christopherfd179292009-08-27 18:07:15 +000016102 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000016103 }
16104 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016105
16106 // Handle these cases:
16107 // (select (x != c), e, c) -> select (x != c), e, x),
16108 // (select (x == c), c, e) -> select (x == c), x, e)
16109 // where the c is an integer constant, and the "select" is the combination
16110 // of CMOV and CMP.
16111 //
16112 // The rationale for this change is that the conditional-move from a constant
16113 // needs two instructions, however, conditional-move from a register needs
16114 // only one instruction.
16115 //
16116 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
16117 // some instruction-combining opportunities. This opt needs to be
16118 // postponed as late as possible.
16119 //
16120 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
16121 // the DCI.xxxx conditions are provided to postpone the optimization as
16122 // late as possible.
16123
16124 ConstantSDNode *CmpAgainst = 0;
16125 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
16126 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016127 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016128
16129 if (CC == X86::COND_NE &&
16130 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
16131 CC = X86::GetOppositeBranchCondition(CC);
16132 std::swap(TrueOp, FalseOp);
16133 }
16134
16135 if (CC == X86::COND_E &&
16136 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
16137 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
16138 DAG.getConstant(CC, MVT::i8), Cond };
16139 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
16140 array_lengthof(Ops));
16141 }
16142 }
16143 }
16144
Chris Lattnerd1980a52009-03-12 06:52:53 +000016145 return SDValue();
16146}
16147
Evan Cheng0b0cd912009-03-28 05:57:29 +000016148/// PerformMulCombine - Optimize a single multiply with constant into two
16149/// in order to implement it with two cheaper instructions, e.g.
16150/// LEA + SHL, LEA + LEA.
16151static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
16152 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000016153 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
16154 return SDValue();
16155
Owen Andersone50ed302009-08-10 22:56:29 +000016156 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000016157 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000016158 return SDValue();
16159
16160 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
16161 if (!C)
16162 return SDValue();
16163 uint64_t MulAmt = C->getZExtValue();
16164 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
16165 return SDValue();
16166
16167 uint64_t MulAmt1 = 0;
16168 uint64_t MulAmt2 = 0;
16169 if ((MulAmt % 9) == 0) {
16170 MulAmt1 = 9;
16171 MulAmt2 = MulAmt / 9;
16172 } else if ((MulAmt % 5) == 0) {
16173 MulAmt1 = 5;
16174 MulAmt2 = MulAmt / 5;
16175 } else if ((MulAmt % 3) == 0) {
16176 MulAmt1 = 3;
16177 MulAmt2 = MulAmt / 3;
16178 }
16179 if (MulAmt2 &&
16180 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
16181 DebugLoc DL = N->getDebugLoc();
16182
16183 if (isPowerOf2_64(MulAmt2) &&
16184 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
16185 // If second multiplifer is pow2, issue it first. We want the multiply by
16186 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
16187 // is an add.
16188 std::swap(MulAmt1, MulAmt2);
16189
16190 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000016191 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016192 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000016193 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000016194 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016195 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000016196 DAG.getConstant(MulAmt1, VT));
16197
Eric Christopherfd179292009-08-27 18:07:15 +000016198 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016199 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000016200 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000016201 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016202 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000016203 DAG.getConstant(MulAmt2, VT));
16204
16205 // Do not add new nodes to DAG combiner worklist.
16206 DCI.CombineTo(N, NewMul, false);
16207 }
16208 return SDValue();
16209}
16210
Evan Chengad9c0a32009-12-15 00:53:42 +000016211static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
16212 SDValue N0 = N->getOperand(0);
16213 SDValue N1 = N->getOperand(1);
16214 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
16215 EVT VT = N0.getValueType();
16216
16217 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
16218 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016219 if (VT.isInteger() && !VT.isVector() &&
16220 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000016221 N0.getOperand(1).getOpcode() == ISD::Constant) {
16222 SDValue N00 = N0.getOperand(0);
16223 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
16224 ((N00.getOpcode() == ISD::ANY_EXTEND ||
16225 N00.getOpcode() == ISD::ZERO_EXTEND) &&
16226 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
16227 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
16228 APInt ShAmt = N1C->getAPIntValue();
16229 Mask = Mask.shl(ShAmt);
16230 if (Mask != 0)
16231 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
16232 N00, DAG.getConstant(Mask, VT));
16233 }
16234 }
16235
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016236 // Hardware support for vector shifts is sparse which makes us scalarize the
16237 // vector operations in many cases. Also, on sandybridge ADD is faster than
16238 // shl.
16239 // (shl V, 1) -> add V,V
16240 if (isSplatVector(N1.getNode())) {
16241 assert(N0.getValueType().isVector() && "Invalid vector shift type");
16242 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
16243 // We shift all of the values by one. In many cases we do not have
16244 // hardware support for this operation. This is better expressed as an ADD
16245 // of two values.
16246 if (N1C && (1 == N1C->getZExtValue())) {
16247 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
16248 }
16249 }
16250
Evan Chengad9c0a32009-12-15 00:53:42 +000016251 return SDValue();
16252}
Evan Cheng0b0cd912009-03-28 05:57:29 +000016253
Nadav Rotem0fb65232013-05-04 23:24:56 +000016254/// PerformShiftCombine - Combine shifts.
Nate Begeman740ab032009-01-26 00:52:55 +000016255static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000016256 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000016257 const X86Subtarget *Subtarget) {
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016258 if (N->getOpcode() == ISD::SHL) {
16259 SDValue V = PerformSHLCombine(N, DAG);
16260 if (V.getNode()) return V;
16261 }
Evan Chengad9c0a32009-12-15 00:53:42 +000016262
Michael Liao42317cc2013-03-20 02:33:21 +000016263 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016264}
16265
Stuart Hastings865f0932011-06-03 23:53:54 +000016266// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16267// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16268// and friends. Likewise for OR -> CMPNEQSS.
16269static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16270 TargetLowering::DAGCombinerInfo &DCI,
16271 const X86Subtarget *Subtarget) {
16272 unsigned opcode;
16273
16274 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16275 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016276 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016277 SDValue N0 = N->getOperand(0);
16278 SDValue N1 = N->getOperand(1);
16279 SDValue CMP0 = N0->getOperand(1);
16280 SDValue CMP1 = N1->getOperand(1);
16281 DebugLoc DL = N->getDebugLoc();
16282
16283 // The SETCCs should both refer to the same CMP.
16284 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16285 return SDValue();
16286
16287 SDValue CMP00 = CMP0->getOperand(0);
16288 SDValue CMP01 = CMP0->getOperand(1);
16289 EVT VT = CMP00.getValueType();
16290
16291 if (VT == MVT::f32 || VT == MVT::f64) {
16292 bool ExpectingFlags = false;
16293 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016294 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016295 !ExpectingFlags && UI != UE; ++UI)
16296 switch (UI->getOpcode()) {
16297 default:
16298 case ISD::BR_CC:
16299 case ISD::BRCOND:
16300 case ISD::SELECT:
16301 ExpectingFlags = true;
16302 break;
16303 case ISD::CopyToReg:
16304 case ISD::SIGN_EXTEND:
16305 case ISD::ZERO_EXTEND:
16306 case ISD::ANY_EXTEND:
16307 break;
16308 }
16309
16310 if (!ExpectingFlags) {
16311 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16312 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16313
16314 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16315 X86::CondCode tmp = cc0;
16316 cc0 = cc1;
16317 cc1 = tmp;
16318 }
16319
16320 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16321 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16322 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16323 X86ISD::NodeType NTOperator = is64BitFP ?
16324 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16325 // FIXME: need symbolic constants for these magic numbers.
16326 // See X86ATTInstPrinter.cpp:printSSECC().
16327 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16328 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16329 DAG.getConstant(x86cc, MVT::i8));
16330 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16331 OnesOrZeroesF);
16332 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16333 DAG.getConstant(1, MVT::i32));
16334 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16335 return OneBitOfTruth;
16336 }
16337 }
16338 }
16339 }
16340 return SDValue();
16341}
16342
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016343/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16344/// so it can be folded inside ANDNP.
16345static bool CanFoldXORWithAllOnes(const SDNode *N) {
16346 EVT VT = N->getValueType(0);
16347
16348 // Match direct AllOnes for 128 and 256-bit vectors
16349 if (ISD::isBuildVectorAllOnes(N))
16350 return true;
16351
16352 // Look through a bit convert.
16353 if (N->getOpcode() == ISD::BITCAST)
16354 N = N->getOperand(0).getNode();
16355
16356 // Sometimes the operand may come from a insert_subvector building a 256-bit
16357 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016358 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016359 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16360 SDValue V1 = N->getOperand(0);
16361 SDValue V2 = N->getOperand(1);
16362
16363 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16364 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16365 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16366 ISD::isBuildVectorAllOnes(V2.getNode()))
16367 return true;
16368 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016369
16370 return false;
16371}
16372
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016373// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16374// register. In most cases we actually compare or select YMM-sized registers
16375// and mixing the two types creates horrible code. This method optimizes
16376// some of the transition sequences.
16377static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16378 TargetLowering::DAGCombinerInfo &DCI,
16379 const X86Subtarget *Subtarget) {
16380 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016381 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016382 return SDValue();
16383
16384 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16385 N->getOpcode() == ISD::ZERO_EXTEND ||
16386 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16387
16388 SDValue Narrow = N->getOperand(0);
16389 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016390 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016391 return SDValue();
16392
16393 if (Narrow->getOpcode() != ISD::XOR &&
16394 Narrow->getOpcode() != ISD::AND &&
16395 Narrow->getOpcode() != ISD::OR)
16396 return SDValue();
16397
16398 SDValue N0 = Narrow->getOperand(0);
16399 SDValue N1 = Narrow->getOperand(1);
16400 DebugLoc DL = Narrow->getDebugLoc();
16401
16402 // The Left side has to be a trunc.
16403 if (N0.getOpcode() != ISD::TRUNCATE)
16404 return SDValue();
16405
16406 // The type of the truncated inputs.
16407 EVT WideVT = N0->getOperand(0)->getValueType(0);
16408 if (WideVT != VT)
16409 return SDValue();
16410
16411 // The right side has to be a 'trunc' or a constant vector.
16412 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16413 bool RHSConst = (isSplatVector(N1.getNode()) &&
16414 isa<ConstantSDNode>(N1->getOperand(0)));
16415 if (!RHSTrunc && !RHSConst)
16416 return SDValue();
16417
16418 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16419
16420 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16421 return SDValue();
16422
16423 // Set N0 and N1 to hold the inputs to the new wide operation.
16424 N0 = N0->getOperand(0);
16425 if (RHSConst) {
16426 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16427 N1->getOperand(0));
16428 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16429 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16430 } else if (RHSTrunc) {
16431 N1 = N1->getOperand(0);
16432 }
16433
16434 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016435 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016436 unsigned Opcode = N->getOpcode();
16437 switch (Opcode) {
16438 case ISD::ANY_EXTEND:
16439 return Op;
16440 case ISD::ZERO_EXTEND: {
16441 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16442 APInt Mask = APInt::getAllOnesValue(InBits);
16443 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16444 return DAG.getNode(ISD::AND, DL, VT,
16445 Op, DAG.getConstant(Mask, VT));
16446 }
16447 case ISD::SIGN_EXTEND:
16448 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16449 Op, DAG.getValueType(NarrowVT));
16450 default:
16451 llvm_unreachable("Unexpected opcode");
16452 }
16453}
16454
Nate Begemanb65c1752010-12-17 22:55:37 +000016455static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16456 TargetLowering::DAGCombinerInfo &DCI,
16457 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016458 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016459 if (DCI.isBeforeLegalizeOps())
16460 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016461
Stuart Hastings865f0932011-06-03 23:53:54 +000016462 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16463 if (R.getNode())
16464 return R;
16465
Craig Topperb926afc2012-12-17 05:12:30 +000016466 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016467 // BLSI is X & (-X)
16468 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016469 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16470 SDValue N0 = N->getOperand(0);
16471 SDValue N1 = N->getOperand(1);
16472 DebugLoc DL = N->getDebugLoc();
16473
Craig Topperb4c94572011-10-21 06:55:01 +000016474 // Check LHS for neg
16475 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16476 isZero(N0.getOperand(0)))
16477 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16478
16479 // Check RHS for neg
16480 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16481 isZero(N1.getOperand(0)))
16482 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16483
16484 // Check LHS for X-1
16485 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16486 isAllOnes(N0.getOperand(1)))
16487 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16488
16489 // Check RHS for X-1
16490 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16491 isAllOnes(N1.getOperand(1)))
16492 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16493
Craig Topper54a11172011-10-14 07:06:56 +000016494 return SDValue();
16495 }
16496
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016497 // Want to form ANDNP nodes:
16498 // 1) In the hopes of then easily combining them with OR and AND nodes
16499 // to form PBLEND/PSIGN.
16500 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016501 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016502 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016503
Nate Begemanb65c1752010-12-17 22:55:37 +000016504 SDValue N0 = N->getOperand(0);
16505 SDValue N1 = N->getOperand(1);
16506 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016507
Nate Begemanb65c1752010-12-17 22:55:37 +000016508 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016509 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016510 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16511 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016512 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016513
16514 // Check RHS for vnot
16515 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016516 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16517 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016518 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016519
Nate Begemanb65c1752010-12-17 22:55:37 +000016520 return SDValue();
16521}
16522
Evan Cheng760d1942010-01-04 21:22:48 +000016523static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016524 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016525 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016526 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016527 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016528 return SDValue();
16529
Stuart Hastings865f0932011-06-03 23:53:54 +000016530 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16531 if (R.getNode())
16532 return R;
16533
Evan Cheng760d1942010-01-04 21:22:48 +000016534 SDValue N0 = N->getOperand(0);
16535 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016536
Nate Begemanb65c1752010-12-17 22:55:37 +000016537 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016538 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016539 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016540 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016541 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016542
Craig Topper1666cb62011-11-19 07:07:26 +000016543 // Canonicalize pandn to RHS
16544 if (N0.getOpcode() == X86ISD::ANDNP)
16545 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016546 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016547 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16548 SDValue Mask = N1.getOperand(0);
16549 SDValue X = N1.getOperand(1);
16550 SDValue Y;
16551 if (N0.getOperand(0) == Mask)
16552 Y = N0.getOperand(1);
16553 if (N0.getOperand(1) == Mask)
16554 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016555
Craig Topper1666cb62011-11-19 07:07:26 +000016556 // Check to see if the mask appeared in both the AND and ANDNP and
16557 if (!Y.getNode())
16558 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016559
Craig Topper1666cb62011-11-19 07:07:26 +000016560 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016561 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016562 if (Mask.getOpcode() == ISD::BITCAST)
16563 Mask = Mask.getOperand(0);
16564 if (X.getOpcode() == ISD::BITCAST)
16565 X = X.getOperand(0);
16566 if (Y.getOpcode() == ISD::BITCAST)
16567 Y = Y.getOperand(0);
16568
Craig Topper1666cb62011-11-19 07:07:26 +000016569 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016570
Craig Toppered2e13d2012-01-22 19:15:14 +000016571 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016572 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16573 // there is no psrai.b
Craig Topper1666cb62011-11-19 07:07:26 +000016574 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
Michael Liao42317cc2013-03-20 02:33:21 +000016575 unsigned SraAmt = ~0;
16576 if (Mask.getOpcode() == ISD::SRA) {
16577 SDValue Amt = Mask.getOperand(1);
16578 if (isSplatVector(Amt.getNode())) {
16579 SDValue SclrAmt = Amt->getOperand(0);
16580 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt))
16581 SraAmt = C->getZExtValue();
16582 }
16583 } else if (Mask.getOpcode() == X86ISD::VSRAI) {
16584 SDValue SraC = Mask.getOperand(1);
16585 SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16586 }
Craig Topper1666cb62011-11-19 07:07:26 +000016587 if ((SraAmt + 1) != EltBits)
16588 return SDValue();
16589
16590 DebugLoc DL = N->getDebugLoc();
16591
16592 // Now we know we at least have a plendvb with the mask val. See if
16593 // we can form a psignb/w/d.
16594 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016595 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16596 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016597 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16598 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16599 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016600 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016601 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016602 }
16603 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016604 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016605 return SDValue();
16606
16607 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16608
16609 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16610 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16611 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016612 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016613 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016614 }
16615 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016616
Craig Topper1666cb62011-11-19 07:07:26 +000016617 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16618 return SDValue();
16619
Nate Begemanb65c1752010-12-17 22:55:37 +000016620 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016621 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16622 std::swap(N0, N1);
16623 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16624 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016625 if (!N0.hasOneUse() || !N1.hasOneUse())
16626 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016627
16628 SDValue ShAmt0 = N0.getOperand(1);
16629 if (ShAmt0.getValueType() != MVT::i8)
16630 return SDValue();
16631 SDValue ShAmt1 = N1.getOperand(1);
16632 if (ShAmt1.getValueType() != MVT::i8)
16633 return SDValue();
16634 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16635 ShAmt0 = ShAmt0.getOperand(0);
16636 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16637 ShAmt1 = ShAmt1.getOperand(0);
16638
16639 DebugLoc DL = N->getDebugLoc();
16640 unsigned Opc = X86ISD::SHLD;
16641 SDValue Op0 = N0.getOperand(0);
16642 SDValue Op1 = N1.getOperand(0);
16643 if (ShAmt0.getOpcode() == ISD::SUB) {
16644 Opc = X86ISD::SHRD;
16645 std::swap(Op0, Op1);
16646 std::swap(ShAmt0, ShAmt1);
16647 }
16648
Evan Cheng8b1190a2010-04-28 01:18:01 +000016649 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016650 if (ShAmt1.getOpcode() == ISD::SUB) {
16651 SDValue Sum = ShAmt1.getOperand(0);
16652 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016653 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16654 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16655 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16656 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016657 return DAG.getNode(Opc, DL, VT,
16658 Op0, Op1,
16659 DAG.getNode(ISD::TRUNCATE, DL,
16660 MVT::i8, ShAmt0));
16661 }
16662 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16663 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16664 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016665 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016666 return DAG.getNode(Opc, DL, VT,
16667 N0.getOperand(0), N1.getOperand(0),
16668 DAG.getNode(ISD::TRUNCATE, DL,
16669 MVT::i8, ShAmt0));
16670 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016671
Evan Cheng760d1942010-01-04 21:22:48 +000016672 return SDValue();
16673}
16674
Manman Ren92363622012-06-07 22:39:10 +000016675// Generate NEG and CMOV for integer abs.
16676static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16677 EVT VT = N->getValueType(0);
16678
16679 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16680 // 8-bit integer abs to NEG and CMOV.
16681 if (VT.isInteger() && VT.getSizeInBits() == 8)
16682 return SDValue();
16683
16684 SDValue N0 = N->getOperand(0);
16685 SDValue N1 = N->getOperand(1);
16686 DebugLoc DL = N->getDebugLoc();
16687
16688 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16689 // and change it to SUB and CMOV.
16690 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16691 N0.getOpcode() == ISD::ADD &&
16692 N0.getOperand(1) == N1 &&
16693 N1.getOpcode() == ISD::SRA &&
16694 N1.getOperand(0) == N0.getOperand(0))
16695 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16696 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16697 // Generate SUB & CMOV.
16698 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16699 DAG.getConstant(0, VT), N0.getOperand(0));
16700
16701 SDValue Ops[] = { N0.getOperand(0), Neg,
16702 DAG.getConstant(X86::COND_GE, MVT::i8),
16703 SDValue(Neg.getNode(), 1) };
16704 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16705 Ops, array_lengthof(Ops));
16706 }
16707 return SDValue();
16708}
16709
Craig Topper3738ccd2011-12-27 06:27:23 +000016710// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016711static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16712 TargetLowering::DAGCombinerInfo &DCI,
16713 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016714 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016715 if (DCI.isBeforeLegalizeOps())
16716 return SDValue();
16717
Manman Ren45d53b82012-06-08 18:58:26 +000016718 if (Subtarget->hasCMov()) {
16719 SDValue RV = performIntegerAbsCombine(N, DAG);
16720 if (RV.getNode())
16721 return RV;
16722 }
Manman Ren92363622012-06-07 22:39:10 +000016723
16724 // Try forming BMI if it is available.
16725 if (!Subtarget->hasBMI())
16726 return SDValue();
16727
Craig Topperb4c94572011-10-21 06:55:01 +000016728 if (VT != MVT::i32 && VT != MVT::i64)
16729 return SDValue();
16730
Craig Topper3738ccd2011-12-27 06:27:23 +000016731 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16732
Craig Topperb4c94572011-10-21 06:55:01 +000016733 // Create BLSMSK instructions by finding X ^ (X-1)
16734 SDValue N0 = N->getOperand(0);
16735 SDValue N1 = N->getOperand(1);
16736 DebugLoc DL = N->getDebugLoc();
16737
16738 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16739 isAllOnes(N0.getOperand(1)))
16740 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16741
16742 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16743 isAllOnes(N1.getOperand(1)))
16744 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16745
16746 return SDValue();
16747}
16748
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016749/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16750static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016751 TargetLowering::DAGCombinerInfo &DCI,
16752 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016753 LoadSDNode *Ld = cast<LoadSDNode>(N);
16754 EVT RegVT = Ld->getValueType(0);
16755 EVT MemVT = Ld->getMemoryVT();
16756 DebugLoc dl = Ld->getDebugLoc();
16757 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016758 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016759
Michael Liaod4584c92013-03-25 23:50:10 +000016760 // On Sandybridge unaligned 256bit loads are inefficient.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016761 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016762 unsigned Alignment = Ld->getAlignment();
Michael Liaod4584c92013-03-25 23:50:10 +000016763 bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016764 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016765 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016766 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016767 if (NumElems < 2)
16768 return SDValue();
16769
Nadav Rotem48177ac2013-01-18 23:10:30 +000016770 SDValue Ptr = Ld->getBasePtr();
16771 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16772
16773 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16774 NumElems/2);
16775 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16776 Ld->getPointerInfo(), Ld->isVolatile(),
16777 Ld->isNonTemporal(), Ld->isInvariant(),
16778 Alignment);
16779 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16780 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16781 Ld->getPointerInfo(), Ld->isVolatile(),
16782 Ld->isNonTemporal(), Ld->isInvariant(),
Michael Liaod4584c92013-03-25 23:50:10 +000016783 std::min(16U, Alignment));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016784 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16785 Load1.getValue(1),
16786 Load2.getValue(1));
16787
16788 SDValue NewVec = DAG.getUNDEF(RegVT);
16789 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16790 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16791 return DCI.CombineTo(N, NewVec, TF, true);
16792 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016793
Nadav Rotemca6f2962011-09-18 19:00:23 +000016794 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016795 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16796 // expansion is still better than scalar code.
16797 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16798 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016799 // TODO: It is possible to support ZExt by zeroing the undef values
16800 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016801 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16802 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016803 assert(MemVT != RegVT && "Cannot extend to the same type");
16804 assert(MemVT.isVector() && "Must load a vector from memory");
16805
16806 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016807 unsigned MemSz = MemVT.getSizeInBits();
16808 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016809
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016810 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16811 return SDValue();
16812
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016813 // All sizes must be a power of two.
16814 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16815 return SDValue();
16816
16817 // Attempt to load the original value using scalar loads.
16818 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016819 MVT SclrLoadTy = MVT::i8;
16820 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16821 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16822 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016823 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016824 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016825 }
16826 }
16827
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016828 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16829 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16830 (64 <= MemSz))
16831 SclrLoadTy = MVT::f64;
16832
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016833 // Calculate the number of scalar loads that we need to perform
16834 // in order to load our vector from memory.
16835 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016836 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16837 return SDValue();
16838
16839 unsigned loadRegZize = RegSz;
16840 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16841 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016842
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016843 // Represent our vector as a sequence of elements which are the
16844 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016845 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016846 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016847
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016848 // Represent the data using the same element type that is stored in
16849 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016850 EVT WideVecVT =
16851 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016852 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016853
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016854 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16855 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016856
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016857 // We can't shuffle using an illegal type.
16858 if (!TLI.isTypeLegal(WideVecVT))
16859 return SDValue();
16860
16861 SmallVector<SDValue, 8> Chains;
16862 SDValue Ptr = Ld->getBasePtr();
16863 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16864 TLI.getPointerTy());
16865 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16866
16867 for (unsigned i = 0; i < NumLoads; ++i) {
16868 // Perform a single load.
16869 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16870 Ptr, Ld->getPointerInfo(),
16871 Ld->isVolatile(), Ld->isNonTemporal(),
16872 Ld->isInvariant(), Ld->getAlignment());
16873 Chains.push_back(ScalarLoad.getValue(1));
16874 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16875 // another round of DAGCombining.
16876 if (i == 0)
16877 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16878 else
16879 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16880 ScalarLoad, DAG.getIntPtrConstant(i));
16881
16882 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16883 }
16884
16885 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16886 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016887
16888 // Bitcast the loaded value to a vector of the original element type, in
16889 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016890 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016891 unsigned SizeRatio = RegSz/MemSz;
16892
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016893 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016894 // If we have SSE4.1 we can directly emit a VSEXT node.
16895 if (Subtarget->hasSSE41()) {
16896 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16897 return DCI.CombineTo(N, Sext, TF, true);
16898 }
16899
16900 // Otherwise we'll shuffle the small elements in the high bits of the
16901 // larger type and perform an arithmetic shift. If the shift is not legal
16902 // it's better to scalarize.
16903 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16904 return SDValue();
16905
16906 // Redistribute the loaded elements into the different locations.
16907 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16908 for (unsigned i = 0; i != NumElems; ++i)
16909 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16910
16911 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16912 DAG.getUNDEF(WideVecVT),
16913 &ShuffleVec[0]);
16914
16915 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16916
16917 // Build the arithmetic shift.
16918 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16919 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016920 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16921 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016922
16923 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016924 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016925
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016926 // Redistribute the loaded elements into the different locations.
16927 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016928 for (unsigned i = 0; i != NumElems; ++i)
16929 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016930
16931 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016932 DAG.getUNDEF(WideVecVT),
16933 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016934
16935 // Bitcast to the requested type.
16936 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16937 // Replace the original load with the new sequence
16938 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016939 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016940 }
16941
16942 return SDValue();
16943}
16944
Chris Lattner149a4e52008-02-22 02:09:43 +000016945/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016946static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016947 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016948 StoreSDNode *St = cast<StoreSDNode>(N);
16949 EVT VT = St->getValue().getValueType();
16950 EVT StVT = St->getMemoryVT();
16951 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016952 SDValue StoredVal = St->getOperand(1);
16953 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16954
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016955 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016956 // On Sandy Bridge, 256-bit memory operations are executed by two
16957 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16958 // memory operation.
Michael Liaod4584c92013-03-25 23:50:10 +000016959 unsigned Alignment = St->getAlignment();
16960 bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016961 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016962 StVT == VT && !IsAligned) {
16963 unsigned NumElems = VT.getVectorNumElements();
16964 if (NumElems < 2)
16965 return SDValue();
16966
16967 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16968 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016969
16970 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16971 SDValue Ptr0 = St->getBasePtr();
16972 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16973
16974 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16975 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016976 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016977 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16978 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016979 St->isNonTemporal(),
Michael Liaod4584c92013-03-25 23:50:10 +000016980 std::min(16U, Alignment));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016981 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16982 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016983
16984 // Optimize trunc store (of multiple scalars) to shuffle and store.
16985 // First, pack all of the elements in one place. Next, store to memory
16986 // in fewer chunks.
16987 if (St->isTruncatingStore() && VT.isVector()) {
16988 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16989 unsigned NumElems = VT.getVectorNumElements();
16990 assert(StVT != VT && "Cannot truncate to the same type");
16991 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16992 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16993
16994 // From, To sizes and ElemCount must be pow of two
16995 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016996 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016997 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016998 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016999
Nadav Rotem614061b2011-08-10 19:30:14 +000017000 unsigned SizeRatio = FromSz / ToSz;
17001
17002 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
17003
17004 // Create a type on which we perform the shuffle
17005 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
17006 StVT.getScalarType(), NumElems*SizeRatio);
17007
17008 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
17009
17010 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
17011 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000017012 for (unsigned i = 0; i != NumElems; ++i)
17013 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000017014
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017015 // Can't shuffle using an illegal type.
17016 if (!TLI.isTypeLegal(WideVecVT))
17017 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000017018
17019 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000017020 DAG.getUNDEF(WideVecVT),
17021 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000017022 // At this point all of the data is stored at the bottom of the
17023 // register. We now need to save it to mem.
17024
17025 // Find the largest store unit
17026 MVT StoreType = MVT::i8;
17027 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
17028 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
17029 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017030 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000017031 StoreType = Tp;
17032 }
17033
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017034 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
17035 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
17036 (64 <= NumElems * ToSz))
17037 StoreType = MVT::f64;
17038
Nadav Rotem614061b2011-08-10 19:30:14 +000017039 // Bitcast the original vector into a vector of store-size units
17040 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017041 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000017042 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
17043 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
17044 SmallVector<SDValue, 8> Chains;
17045 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
17046 TLI.getPointerTy());
17047 SDValue Ptr = St->getBasePtr();
17048
17049 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000017050 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000017051 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
17052 StoreType, ShuffWide,
17053 DAG.getIntPtrConstant(i));
17054 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
17055 St->getPointerInfo(), St->isVolatile(),
17056 St->isNonTemporal(), St->getAlignment());
17057 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
17058 Chains.push_back(Ch);
17059 }
17060
17061 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
17062 Chains.size());
17063 }
17064
Chris Lattner149a4e52008-02-22 02:09:43 +000017065 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
17066 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000017067 // A preferable solution to the general problem is to figure out the right
17068 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000017069
17070 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000017071 if (VT.getSizeInBits() != 64)
17072 return SDValue();
17073
Devang Patel578efa92009-06-05 21:57:13 +000017074 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000017075 bool NoImplicitFloatOps = F->getAttributes().
17076 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000017077 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000017078 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000017079 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000017080 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000017081 isa<LoadSDNode>(St->getValue()) &&
17082 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
17083 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017084 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017085 LoadSDNode *Ld = 0;
17086 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000017087 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000017088 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017089 // Must be a store of a load. We currently handle two cases: the load
17090 // is a direct child, and it's under an intervening TokenFactor. It is
17091 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000017092 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000017093 Ld = cast<LoadSDNode>(St->getChain());
17094 else if (St->getValue().hasOneUse() &&
17095 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000017096 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017097 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000017098 TokenFactorIndex = i;
17099 Ld = cast<LoadSDNode>(St->getValue());
17100 } else
17101 Ops.push_back(ChainVal->getOperand(i));
17102 }
17103 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000017104
Evan Cheng536e6672009-03-12 05:59:15 +000017105 if (!Ld || !ISD::isNormalLoad(Ld))
17106 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017107
Evan Cheng536e6672009-03-12 05:59:15 +000017108 // If this is not the MMX case, i.e. we are just turning i64 load/store
17109 // into f64 load/store, avoid the transformation if there are multiple
17110 // uses of the loaded value.
17111 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
17112 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017113
Evan Cheng536e6672009-03-12 05:59:15 +000017114 DebugLoc LdDL = Ld->getDebugLoc();
17115 DebugLoc StDL = N->getDebugLoc();
17116 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
17117 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
17118 // pair instead.
17119 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017120 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000017121 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
17122 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017123 Ld->isNonTemporal(), Ld->isInvariant(),
17124 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017125 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000017126 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000017127 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000017128 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000017129 Ops.size());
17130 }
Evan Cheng536e6672009-03-12 05:59:15 +000017131 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000017132 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017133 St->isVolatile(), St->isNonTemporal(),
17134 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000017135 }
Evan Cheng536e6672009-03-12 05:59:15 +000017136
17137 // Otherwise, lower to two pairs of 32-bit loads / stores.
17138 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017139 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
17140 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017141
Owen Anderson825b72b2009-08-11 20:47:22 +000017142 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017143 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017144 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017145 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000017146 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017147 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000017148 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017149 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000017150 MinAlign(Ld->getAlignment(), 4));
17151
17152 SDValue NewChain = LoLd.getValue(1);
17153 if (TokenFactorIndex != -1) {
17154 Ops.push_back(LoLd);
17155 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000017156 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000017157 Ops.size());
17158 }
17159
17160 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017161 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
17162 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017163
17164 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017165 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017166 St->isVolatile(), St->isNonTemporal(),
17167 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017168 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017169 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000017170 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000017171 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000017172 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000017173 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000017174 }
Dan Gohman475871a2008-07-27 21:46:04 +000017175 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000017176}
17177
Duncan Sands17470be2011-09-22 20:15:48 +000017178/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
17179/// and return the operands for the horizontal operation in LHS and RHS. A
17180/// horizontal operation performs the binary operation on successive elements
17181/// of its first operand, then on successive elements of its second operand,
17182/// returning the resulting values in a vector. For example, if
17183/// A = < float a0, float a1, float a2, float a3 >
17184/// and
17185/// B = < float b0, float b1, float b2, float b3 >
17186/// then the result of doing a horizontal operation on A and B is
17187/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
17188/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
17189/// A horizontal-op B, for some already available A and B, and if so then LHS is
17190/// set to A, RHS to B, and the routine returns 'true'.
17191/// Note that the binary operation should have the property that if one of the
17192/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017193static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000017194 // Look for the following pattern: if
17195 // A = < float a0, float a1, float a2, float a3 >
17196 // B = < float b0, float b1, float b2, float b3 >
17197 // and
17198 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17199 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17200 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17201 // which is A horizontal-op B.
17202
17203 // At least one of the operands should be a vector shuffle.
17204 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17205 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17206 return false;
17207
17208 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017209
17210 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17211 "Unsupported vector type for horizontal add/sub");
17212
17213 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17214 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017215 unsigned NumElts = VT.getVectorNumElements();
17216 unsigned NumLanes = VT.getSizeInBits()/128;
17217 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017218 assert((NumLaneElts % 2 == 0) &&
17219 "Vector type should have an even number of elements in each lane");
17220 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017221
17222 // View LHS in the form
17223 // LHS = VECTOR_SHUFFLE A, B, LMask
17224 // If LHS is not a shuffle then pretend it is the shuffle
17225 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17226 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17227 // type VT.
17228 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017229 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017230 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17231 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17232 A = LHS.getOperand(0);
17233 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17234 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017235 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17236 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017237 } else {
17238 if (LHS.getOpcode() != ISD::UNDEF)
17239 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017240 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017241 LMask[i] = i;
17242 }
17243
17244 // Likewise, view RHS in the form
17245 // RHS = VECTOR_SHUFFLE C, D, RMask
17246 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017247 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017248 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17249 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17250 C = RHS.getOperand(0);
17251 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17252 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017253 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17254 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017255 } else {
17256 if (RHS.getOpcode() != ISD::UNDEF)
17257 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017258 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017259 RMask[i] = i;
17260 }
17261
17262 // Check that the shuffles are both shuffling the same vectors.
17263 if (!(A == C && B == D) && !(A == D && B == C))
17264 return false;
17265
17266 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17267 if (!A.getNode() && !B.getNode())
17268 return false;
17269
17270 // If A and B occur in reverse order in RHS, then "swap" them (which means
17271 // rewriting the mask).
17272 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017273 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017274
17275 // At this point LHS and RHS are equivalent to
17276 // LHS = VECTOR_SHUFFLE A, B, LMask
17277 // RHS = VECTOR_SHUFFLE A, B, RMask
17278 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017279 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017280 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017281
Craig Topperf8363302011-12-02 08:18:41 +000017282 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017283 if (LIdx < 0 || RIdx < 0 ||
17284 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17285 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017286 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017287
Craig Topperf8363302011-12-02 08:18:41 +000017288 // Check that successive elements are being operated on. If not, this is
17289 // not a horizontal operation.
17290 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17291 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017292 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017293 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017294 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017295 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017296 }
17297
17298 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17299 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17300 return true;
17301}
17302
17303/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17304static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17305 const X86Subtarget *Subtarget) {
17306 EVT VT = N->getValueType(0);
17307 SDValue LHS = N->getOperand(0);
17308 SDValue RHS = N->getOperand(1);
17309
17310 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017311 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017312 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017313 isHorizontalBinOp(LHS, RHS, true))
17314 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
17315 return SDValue();
17316}
17317
17318/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17319static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17320 const X86Subtarget *Subtarget) {
17321 EVT VT = N->getValueType(0);
17322 SDValue LHS = N->getOperand(0);
17323 SDValue RHS = N->getOperand(1);
17324
17325 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017326 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017327 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017328 isHorizontalBinOp(LHS, RHS, false))
17329 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
17330 return SDValue();
17331}
17332
Chris Lattner6cf73262008-01-25 06:14:17 +000017333/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17334/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017335static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017336 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17337 // F[X]OR(0.0, x) -> x
17338 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017339 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17340 if (C->getValueAPF().isPosZero())
17341 return N->getOperand(1);
17342 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17343 if (C->getValueAPF().isPosZero())
17344 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017345 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017346}
17347
Nadav Rotemd60cb112012-08-19 13:06:16 +000017348/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17349/// X86ISD::FMAX nodes.
17350static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17351 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17352
17353 // Only perform optimizations if UnsafeMath is used.
17354 if (!DAG.getTarget().Options.UnsafeFPMath)
17355 return SDValue();
17356
17357 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017358 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017359 unsigned NewOp = 0;
17360 switch (N->getOpcode()) {
17361 default: llvm_unreachable("unknown opcode");
17362 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17363 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17364 }
17365
17366 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17367 N->getOperand(0), N->getOperand(1));
17368}
17369
Chris Lattneraf723b92008-01-25 05:46:26 +000017370/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017371static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017372 // FAND(0.0, x) -> 0.0
17373 // FAND(x, 0.0) -> 0.0
17374 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17375 if (C->getValueAPF().isPosZero())
17376 return N->getOperand(0);
17377 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17378 if (C->getValueAPF().isPosZero())
17379 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017380 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017381}
17382
Dan Gohmane5af2d32009-01-29 01:59:02 +000017383static SDValue PerformBTCombine(SDNode *N,
17384 SelectionDAG &DAG,
17385 TargetLowering::DAGCombinerInfo &DCI) {
17386 // BT ignores high bits in the bit index operand.
17387 SDValue Op1 = N->getOperand(1);
17388 if (Op1.hasOneUse()) {
17389 unsigned BitWidth = Op1.getValueSizeInBits();
17390 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17391 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017392 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17393 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017394 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017395 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17396 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17397 DCI.CommitTargetLoweringOpt(TLO);
17398 }
17399 return SDValue();
17400}
Chris Lattner83e6c992006-10-04 06:57:07 +000017401
Eli Friedman7a5e5552009-06-07 06:52:44 +000017402static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17403 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017404 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017405 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017406 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017407 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017408 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017409 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017410 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017411 }
17412 return SDValue();
17413}
17414
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017415static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
17416 const X86Subtarget *Subtarget) {
17417 EVT VT = N->getValueType(0);
17418 if (!VT.isVector())
17419 return SDValue();
17420
17421 SDValue N0 = N->getOperand(0);
17422 SDValue N1 = N->getOperand(1);
17423 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
17424 DebugLoc dl = N->getDebugLoc();
17425
17426 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17427 // both SSE and AVX2 since there is no sign-extended shift right
17428 // operation on a vector with 64-bit elements.
17429 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17430 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17431 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17432 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17433 SDValue N00 = N0.getOperand(0);
17434
17435 // EXTLOAD has a better solution on AVX2,
17436 // it may be replaced with X86ISD::VSEXT node.
17437 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17438 if (!ISD::isNormalLoad(N00.getNode()))
17439 return SDValue();
17440
17441 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
17442 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
17443 N00, N1);
17444 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17445 }
17446 }
17447 return SDValue();
17448}
17449
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017450static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17451 TargetLowering::DAGCombinerInfo &DCI,
17452 const X86Subtarget *Subtarget) {
17453 if (!DCI.isBeforeLegalizeOps())
17454 return SDValue();
17455
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017456 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017457 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017458
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017459 EVT VT = N->getValueType(0);
17460 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017461 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17462 if (R.getNode())
17463 return R;
17464 }
17465
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017466 return SDValue();
17467}
17468
Michael Liaof6c24ee2012-08-10 14:39:24 +000017469static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017470 const X86Subtarget* Subtarget) {
17471 DebugLoc dl = N->getDebugLoc();
17472 EVT VT = N->getValueType(0);
17473
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017474 // Let legalize expand this if it isn't a legal type yet.
17475 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17476 return SDValue();
17477
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017478 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017479 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17480 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017481 return SDValue();
17482
17483 SDValue A = N->getOperand(0);
17484 SDValue B = N->getOperand(1);
17485 SDValue C = N->getOperand(2);
17486
17487 bool NegA = (A.getOpcode() == ISD::FNEG);
17488 bool NegB = (B.getOpcode() == ISD::FNEG);
17489 bool NegC = (C.getOpcode() == ISD::FNEG);
17490
Michael Liaof6c24ee2012-08-10 14:39:24 +000017491 // Negative multiplication when NegA xor NegB
17492 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017493 if (NegA)
17494 A = A.getOperand(0);
17495 if (NegB)
17496 B = B.getOperand(0);
17497 if (NegC)
17498 C = C.getOperand(0);
17499
17500 unsigned Opcode;
17501 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017502 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017503 else
Craig Topperbf404372012-08-31 15:40:30 +000017504 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17505
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017506 return DAG.getNode(Opcode, dl, VT, A, B, C);
17507}
17508
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017509static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017510 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017511 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017512 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17513 // (and (i32 x86isd::setcc_carry), 1)
17514 // This eliminates the zext. This transformation is necessary because
17515 // ISD::SETCC is always legalized to i8.
17516 DebugLoc dl = N->getDebugLoc();
17517 SDValue N0 = N->getOperand(0);
17518 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017519
Evan Cheng2e489c42009-12-16 00:53:11 +000017520 if (N0.getOpcode() == ISD::AND &&
17521 N0.hasOneUse() &&
17522 N0.getOperand(0).hasOneUse()) {
17523 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017524 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17525 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17526 if (!C || C->getZExtValue() != 1)
17527 return SDValue();
17528 return DAG.getNode(ISD::AND, dl, VT,
17529 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17530 N00.getOperand(0), N00.getOperand(1)),
17531 DAG.getConstant(1, VT));
17532 }
17533 }
17534
Craig Topper5a529e42013-01-18 06:44:29 +000017535 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017536 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17537 if (R.getNode())
17538 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017539 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017540
Evan Cheng2e489c42009-12-16 00:53:11 +000017541 return SDValue();
17542}
17543
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017544// Optimize x == -y --> x+y == 0
17545// x != -y --> x+y != 0
17546static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17547 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17548 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017549 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017550
17551 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17552 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17553 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17554 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17555 LHS.getValueType(), RHS, LHS.getOperand(1));
17556 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17557 addV, DAG.getConstant(0, addV.getValueType()), CC);
17558 }
17559 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17560 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17561 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17562 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17563 RHS.getValueType(), LHS, RHS.getOperand(1));
17564 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17565 addV, DAG.getConstant(0, addV.getValueType()), CC);
17566 }
17567 return SDValue();
17568}
17569
Eric Christophere187e252013-01-31 00:50:48 +000017570// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17571// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017572// an all-ones bit which is more useful than 0/1 in some cases.
17573static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17574 return DAG.getNode(ISD::AND, DL, MVT::i8,
17575 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17576 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17577 DAG.getConstant(1, MVT::i8));
17578}
17579
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017580// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017581static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17582 TargetLowering::DAGCombinerInfo &DCI,
17583 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017584 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017585 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17586 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017587
Shuxin Yanga5526a92012-10-31 23:11:48 +000017588 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017589 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017590 // materializing "setb reg".
17591 //
17592 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17593 // cannot take an immediate as its first operand.
17594 //
Eric Christophere187e252013-01-31 00:50:48 +000017595 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017596 EFLAGS.getValueType().isInteger() &&
17597 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17598 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17599 EFLAGS.getNode()->getVTList(),
17600 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17601 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17602 return MaterializeSETB(DL, NewEFLAGS, DAG);
17603 }
17604 }
17605
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017606 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17607 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17608 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017609 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017610 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017611
Michael Liao2a33cec2012-08-10 19:58:13 +000017612 SDValue Flags;
17613
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017614 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17615 if (Flags.getNode()) {
17616 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17617 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17618 }
17619
Michael Liao2a33cec2012-08-10 19:58:13 +000017620 return SDValue();
17621}
17622
17623// Optimize branch condition evaluation.
17624//
17625static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17626 TargetLowering::DAGCombinerInfo &DCI,
17627 const X86Subtarget *Subtarget) {
17628 DebugLoc DL = N->getDebugLoc();
17629 SDValue Chain = N->getOperand(0);
17630 SDValue Dest = N->getOperand(1);
17631 SDValue EFLAGS = N->getOperand(3);
17632 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17633
17634 SDValue Flags;
17635
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017636 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17637 if (Flags.getNode()) {
17638 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17639 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17640 Flags);
17641 }
17642
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017643 return SDValue();
17644}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017645
Benjamin Kramer1396c402011-06-18 11:09:41 +000017646static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17647 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017648 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017649 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017650
17651 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017652 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017653 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017654 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017655 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17656 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17657 }
17658
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017659 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17660 // a 32-bit target where SSE doesn't support i64->FP operations.
17661 if (Op0.getOpcode() == ISD::LOAD) {
17662 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17663 EVT VT = Ld->getValueType(0);
17664 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17665 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17666 !XTLI->getSubtarget()->is64Bit() &&
17667 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017668 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17669 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017670 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17671 return FILDChain;
17672 }
17673 }
17674 return SDValue();
17675}
17676
Chris Lattner23a01992010-12-20 01:37:09 +000017677// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17678static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17679 X86TargetLowering::DAGCombinerInfo &DCI) {
17680 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17681 // the result is either zero or one (depending on the input carry bit).
17682 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17683 if (X86::isZeroNode(N->getOperand(0)) &&
17684 X86::isZeroNode(N->getOperand(1)) &&
17685 // We don't have a good way to replace an EFLAGS use, so only do this when
17686 // dead right now.
17687 SDValue(N, 1).use_empty()) {
17688 DebugLoc DL = N->getDebugLoc();
17689 EVT VT = N->getValueType(0);
17690 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17691 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17692 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17693 DAG.getConstant(X86::COND_B,MVT::i8),
17694 N->getOperand(2)),
17695 DAG.getConstant(1, VT));
17696 return DCI.CombineTo(N, Res1, CarryOut);
17697 }
17698
17699 return SDValue();
17700}
17701
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017702// fold (add Y, (sete X, 0)) -> adc 0, Y
17703// (add Y, (setne X, 0)) -> sbb -1, Y
17704// (sub (sete X, 0), Y) -> sbb 0, Y
17705// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017706static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017707 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017708
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017709 // Look through ZExts.
17710 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17711 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17712 return SDValue();
17713
17714 SDValue SetCC = Ext.getOperand(0);
17715 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17716 return SDValue();
17717
17718 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17719 if (CC != X86::COND_E && CC != X86::COND_NE)
17720 return SDValue();
17721
17722 SDValue Cmp = SetCC.getOperand(1);
17723 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017724 !X86::isZeroNode(Cmp.getOperand(1)) ||
17725 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017726 return SDValue();
17727
17728 SDValue CmpOp0 = Cmp.getOperand(0);
17729 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17730 DAG.getConstant(1, CmpOp0.getValueType()));
17731
17732 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17733 if (CC == X86::COND_NE)
17734 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17735 DL, OtherVal.getValueType(), OtherVal,
17736 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17737 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17738 DL, OtherVal.getValueType(), OtherVal,
17739 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17740}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017741
Craig Topper54f952a2011-11-19 09:02:40 +000017742/// PerformADDCombine - Do target-specific dag combines on integer adds.
17743static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17744 const X86Subtarget *Subtarget) {
17745 EVT VT = N->getValueType(0);
17746 SDValue Op0 = N->getOperand(0);
17747 SDValue Op1 = N->getOperand(1);
17748
17749 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017750 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017751 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017752 isHorizontalBinOp(Op0, Op1, true))
17753 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17754
17755 return OptimizeConditionalInDecrement(N, DAG);
17756}
17757
17758static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17759 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017760 SDValue Op0 = N->getOperand(0);
17761 SDValue Op1 = N->getOperand(1);
17762
17763 // X86 can't encode an immediate LHS of a sub. See if we can push the
17764 // negation into a preceding instruction.
17765 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017766 // If the RHS of the sub is a XOR with one use and a constant, invert the
17767 // immediate. Then add one to the LHS of the sub so we can turn
17768 // X-Y -> X+~Y+1, saving one register.
17769 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17770 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017771 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017772 EVT VT = Op0.getValueType();
17773 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17774 Op1.getOperand(0),
17775 DAG.getConstant(~XorC, VT));
17776 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017777 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017778 }
17779 }
17780
Craig Topper54f952a2011-11-19 09:02:40 +000017781 // Try to synthesize horizontal adds from adds of shuffles.
17782 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017783 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017784 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017785 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017786 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17787
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017788 return OptimizeConditionalInDecrement(N, DAG);
17789}
17790
Michael Liaod9d09602012-10-23 17:34:00 +000017791/// performVZEXTCombine - Performs build vector combines
17792static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17793 TargetLowering::DAGCombinerInfo &DCI,
17794 const X86Subtarget *Subtarget) {
17795 // (vzext (bitcast (vzext (x)) -> (vzext x)
17796 SDValue In = N->getOperand(0);
17797 while (In.getOpcode() == ISD::BITCAST)
17798 In = In.getOperand(0);
17799
17800 if (In.getOpcode() != X86ISD::VZEXT)
17801 return SDValue();
17802
Nadav Rotemb39a5522013-02-14 18:20:48 +000017803 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0),
17804 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017805}
17806
Dan Gohman475871a2008-07-27 21:46:04 +000017807SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017808 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017809 SelectionDAG &DAG = DCI.DAG;
17810 switch (N->getOpcode()) {
17811 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017812 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017813 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017814 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017815 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017816 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017817 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17818 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017819 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017820 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017821 case ISD::SHL:
17822 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017823 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017824 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017825 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017826 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017827 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017828 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017829 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017830 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17831 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017832 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017833 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017834 case X86ISD::FMIN:
17835 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017836 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017837 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017838 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017839 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017840 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017841 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017842 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017843 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017844 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017845 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017846 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017847 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017848 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017849 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017850 case X86ISD::UNPCKH:
17851 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017852 case X86ISD::MOVHLPS:
17853 case X86ISD::MOVLHPS:
17854 case X86ISD::PSHUFD:
17855 case X86ISD::PSHUFHW:
17856 case X86ISD::PSHUFLW:
17857 case X86ISD::MOVSS:
17858 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017859 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017860 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017861 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017862 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017863 }
17864
Dan Gohman475871a2008-07-27 21:46:04 +000017865 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017866}
17867
Evan Chenge5b51ac2010-04-17 06:13:15 +000017868/// isTypeDesirableForOp - Return true if the target has native support for
17869/// the specified value type and it is 'desirable' to use the type for the
17870/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17871/// instruction encodings are longer and some i16 instructions are slow.
17872bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17873 if (!isTypeLegal(VT))
17874 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017875 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017876 return true;
17877
17878 switch (Opc) {
17879 default:
17880 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017881 case ISD::LOAD:
17882 case ISD::SIGN_EXTEND:
17883 case ISD::ZERO_EXTEND:
17884 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017885 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017886 case ISD::SRL:
17887 case ISD::SUB:
17888 case ISD::ADD:
17889 case ISD::MUL:
17890 case ISD::AND:
17891 case ISD::OR:
17892 case ISD::XOR:
17893 return false;
17894 }
17895}
17896
17897/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017898/// beneficial for dag combiner to promote the specified node. If true, it
17899/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017900bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017901 EVT VT = Op.getValueType();
17902 if (VT != MVT::i16)
17903 return false;
17904
Evan Cheng4c26e932010-04-19 19:29:22 +000017905 bool Promote = false;
17906 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017907 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017908 default: break;
17909 case ISD::LOAD: {
17910 LoadSDNode *LD = cast<LoadSDNode>(Op);
17911 // If the non-extending load has a single use and it's not live out, then it
17912 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017913 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17914 Op.hasOneUse()*/) {
17915 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17916 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17917 // The only case where we'd want to promote LOAD (rather then it being
17918 // promoted as an operand is when it's only use is liveout.
17919 if (UI->getOpcode() != ISD::CopyToReg)
17920 return false;
17921 }
17922 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017923 Promote = true;
17924 break;
17925 }
17926 case ISD::SIGN_EXTEND:
17927 case ISD::ZERO_EXTEND:
17928 case ISD::ANY_EXTEND:
17929 Promote = true;
17930 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017931 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017932 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017933 SDValue N0 = Op.getOperand(0);
17934 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017935 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017936 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017937 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017938 break;
17939 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017940 case ISD::ADD:
17941 case ISD::MUL:
17942 case ISD::AND:
17943 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017944 case ISD::XOR:
17945 Commute = true;
17946 // fallthrough
17947 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017948 SDValue N0 = Op.getOperand(0);
17949 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017950 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017951 return false;
17952 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017953 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017954 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017955 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017956 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017957 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017958 }
17959 }
17960
17961 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017962 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017963}
17964
Evan Cheng60c07e12006-07-05 22:17:51 +000017965//===----------------------------------------------------------------------===//
17966// X86 Inline Assembly Support
17967//===----------------------------------------------------------------------===//
17968
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017969namespace {
17970 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017971 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017972 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017973
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017974 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017975 StringRef piece(*args[i]);
17976 if (!s.startswith(piece)) // Check if the piece matches.
17977 return false;
17978
17979 s = s.substr(piece.size());
17980 StringRef::size_type pos = s.find_first_not_of(" \t");
17981 if (pos == 0) // We matched a prefix.
17982 return false;
17983
17984 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017985 }
17986
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017987 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017988 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017989 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017990}
17991
Chris Lattnerb8105652009-07-20 17:51:36 +000017992bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17993 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017994
17995 std::string AsmStr = IA->getAsmString();
17996
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017997 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17998 if (!Ty || Ty->getBitWidth() % 16 != 0)
17999 return false;
18000
Chris Lattnerb8105652009-07-20 17:51:36 +000018001 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000018002 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000018003 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000018004
18005 switch (AsmPieces.size()) {
18006 default: return false;
18007 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000018008 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018009 // we will turn this bswap into something that will be lowered to logical
18010 // ops instead of emitting the bswap asm. For now, we don't support 486 or
18011 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000018012 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018013 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
18014 matchAsm(AsmPieces[0], "bswapl", "$0") ||
18015 matchAsm(AsmPieces[0], "bswapq", "$0") ||
18016 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
18017 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
18018 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000018019 // No need to check constraints, nothing other than the equivalent of
18020 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000018021 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018022 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018023
Chris Lattnerb8105652009-07-20 17:51:36 +000018024 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000018025 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018026 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018027 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
18028 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000018029 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000018030 const std::string &ConstraintsStr = IA->getConstraintString();
18031 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000018032 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000018033 if (AsmPieces.size() == 4 &&
18034 AsmPieces[0] == "~{cc}" &&
18035 AsmPieces[1] == "~{dirflag}" &&
18036 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018037 AsmPieces[3] == "~{fpsr}")
18038 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018039 }
18040 break;
18041 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000018042 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018043 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018044 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
18045 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
18046 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018047 AsmPieces.clear();
18048 const std::string &ConstraintsStr = IA->getConstraintString();
18049 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000018050 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018051 if (AsmPieces.size() == 4 &&
18052 AsmPieces[0] == "~{cc}" &&
18053 AsmPieces[1] == "~{dirflag}" &&
18054 AsmPieces[2] == "~{flags}" &&
18055 AsmPieces[3] == "~{fpsr}")
18056 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000018057 }
Evan Cheng55d42002011-01-08 01:24:27 +000018058
18059 if (CI->getType()->isIntegerTy(64)) {
18060 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
18061 if (Constraints.size() >= 2 &&
18062 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
18063 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
18064 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018065 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
18066 matchAsm(AsmPieces[1], "bswap", "%edx") &&
18067 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018068 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018069 }
18070 }
18071 break;
18072 }
18073 return false;
18074}
18075
Chris Lattnerf4dff842006-07-11 02:54:03 +000018076/// getConstraintType - Given a constraint letter, return the type of
18077/// constraint it is for this target.
18078X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000018079X86TargetLowering::getConstraintType(const std::string &Constraint) const {
18080 if (Constraint.size() == 1) {
18081 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000018082 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000018083 case 'q':
18084 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000018085 case 'f':
18086 case 't':
18087 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000018088 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000018089 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000018090 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000018091 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000018092 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000018093 case 'a':
18094 case 'b':
18095 case 'c':
18096 case 'd':
18097 case 'S':
18098 case 'D':
18099 case 'A':
18100 return C_Register;
18101 case 'I':
18102 case 'J':
18103 case 'K':
18104 case 'L':
18105 case 'M':
18106 case 'N':
18107 case 'G':
18108 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000018109 case 'e':
18110 case 'Z':
18111 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000018112 default:
18113 break;
18114 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000018115 }
Chris Lattner4234f572007-03-25 02:14:49 +000018116 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000018117}
18118
John Thompson44ab89e2010-10-29 17:29:13 +000018119/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000018120/// This object must already have been set up with the operand type
18121/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000018122TargetLowering::ConstraintWeight
18123 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000018124 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000018125 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018126 Value *CallOperandVal = info.CallOperandVal;
18127 // If we don't have a value, we can't do a match,
18128 // but allow it at the lowest weight.
18129 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000018130 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000018131 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000018132 // Look at the constraint type.
18133 switch (*constraint) {
18134 default:
John Thompson44ab89e2010-10-29 17:29:13 +000018135 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
18136 case 'R':
18137 case 'q':
18138 case 'Q':
18139 case 'a':
18140 case 'b':
18141 case 'c':
18142 case 'd':
18143 case 'S':
18144 case 'D':
18145 case 'A':
18146 if (CallOperandVal->getType()->isIntegerTy())
18147 weight = CW_SpecificReg;
18148 break;
18149 case 'f':
18150 case 't':
18151 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018152 if (type->isFloatingPointTy())
18153 weight = CW_SpecificReg;
18154 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018155 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018156 if (type->isX86_MMXTy() && Subtarget->hasMMX())
18157 weight = CW_SpecificReg;
18158 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018159 case 'x':
18160 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000018161 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018162 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000018163 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018164 break;
18165 case 'I':
18166 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
18167 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000018168 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018169 }
18170 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018171 case 'J':
18172 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18173 if (C->getZExtValue() <= 63)
18174 weight = CW_Constant;
18175 }
18176 break;
18177 case 'K':
18178 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18179 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
18180 weight = CW_Constant;
18181 }
18182 break;
18183 case 'L':
18184 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18185 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
18186 weight = CW_Constant;
18187 }
18188 break;
18189 case 'M':
18190 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18191 if (C->getZExtValue() <= 3)
18192 weight = CW_Constant;
18193 }
18194 break;
18195 case 'N':
18196 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18197 if (C->getZExtValue() <= 0xff)
18198 weight = CW_Constant;
18199 }
18200 break;
18201 case 'G':
18202 case 'C':
18203 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18204 weight = CW_Constant;
18205 }
18206 break;
18207 case 'e':
18208 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18209 if ((C->getSExtValue() >= -0x80000000LL) &&
18210 (C->getSExtValue() <= 0x7fffffffLL))
18211 weight = CW_Constant;
18212 }
18213 break;
18214 case 'Z':
18215 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18216 if (C->getZExtValue() <= 0xffffffff)
18217 weight = CW_Constant;
18218 }
18219 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018220 }
18221 return weight;
18222}
18223
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018224/// LowerXConstraint - try to replace an X constraint, which matches anything,
18225/// with another that has more specific requirements based on the type of the
18226/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018227const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018228LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018229 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18230 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018231 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018232 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018233 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018234 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018235 return "x";
18236 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018237
Chris Lattner5e764232008-04-26 23:02:14 +000018238 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018239}
18240
Chris Lattner48884cd2007-08-25 00:47:38 +000018241/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18242/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018243void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018244 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018245 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018246 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018247 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018248
Eric Christopher100c8332011-06-02 23:16:42 +000018249 // Only support length 1 constraints for now.
18250 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018251
Eric Christopher100c8332011-06-02 23:16:42 +000018252 char ConstraintLetter = Constraint[0];
18253 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018254 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018255 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018256 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018257 if (C->getZExtValue() <= 31) {
18258 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018259 break;
18260 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018261 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018262 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018263 case 'J':
18264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018265 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018266 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18267 break;
18268 }
18269 }
18270 return;
18271 case 'K':
18272 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018273 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018274 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18275 break;
18276 }
18277 }
18278 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018279 case 'N':
18280 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018281 if (C->getZExtValue() <= 255) {
18282 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018283 break;
18284 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018285 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018286 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018287 case 'e': {
18288 // 32-bit signed value
18289 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018290 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18291 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018292 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018293 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018294 break;
18295 }
18296 // FIXME gcc accepts some relocatable values here too, but only in certain
18297 // memory models; it's complicated.
18298 }
18299 return;
18300 }
18301 case 'Z': {
18302 // 32-bit unsigned value
18303 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018304 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18305 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018306 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18307 break;
18308 }
18309 }
18310 // FIXME gcc accepts some relocatable values here too, but only in certain
18311 // memory models; it's complicated.
18312 return;
18313 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018314 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018315 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018316 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018317 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018318 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018319 break;
18320 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018321
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018322 // In any sort of PIC mode addresses need to be computed at runtime by
18323 // adding in a register or some sort of table lookup. These can't
18324 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018325 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018326 return;
18327
Chris Lattnerdc43a882007-05-03 16:52:29 +000018328 // If we are in non-pic codegen mode, we allow the address of a global (with
18329 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018330 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018331 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018332
Chris Lattner49921962009-05-08 18:23:14 +000018333 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18334 while (1) {
18335 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18336 Offset += GA->getOffset();
18337 break;
18338 } else if (Op.getOpcode() == ISD::ADD) {
18339 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18340 Offset += C->getZExtValue();
18341 Op = Op.getOperand(0);
18342 continue;
18343 }
18344 } else if (Op.getOpcode() == ISD::SUB) {
18345 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18346 Offset += -C->getZExtValue();
18347 Op = Op.getOperand(0);
18348 continue;
18349 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018350 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018351
Chris Lattner49921962009-05-08 18:23:14 +000018352 // Otherwise, this isn't something we can handle, reject it.
18353 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018354 }
Eric Christopherfd179292009-08-27 18:07:15 +000018355
Dan Gohman46510a72010-04-15 01:51:59 +000018356 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018357 // If we require an extra load to get this address, as in PIC mode, we
18358 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018359 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18360 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018361 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018362
Devang Patel0d881da2010-07-06 22:08:15 +000018363 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
18364 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018365 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018366 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018367 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018368
Gabor Greifba36cb52008-08-28 21:40:38 +000018369 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018370 Ops.push_back(Result);
18371 return;
18372 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018373 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018374}
18375
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018376std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018377X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000018378 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018379 // First, see if this is a constraint that directly corresponds to an LLVM
18380 // register class.
18381 if (Constraint.size() == 1) {
18382 // GCC Constraint Letters
18383 switch (Constraint[0]) {
18384 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018385 // TODO: Slight differences here in allocation order and leaving
18386 // RIP in the class. Do they matter any more here than they do
18387 // in the normal allocation?
18388 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18389 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018390 if (VT == MVT::i32 || VT == MVT::f32)
18391 return std::make_pair(0U, &X86::GR32RegClass);
18392 if (VT == MVT::i16)
18393 return std::make_pair(0U, &X86::GR16RegClass);
18394 if (VT == MVT::i8 || VT == MVT::i1)
18395 return std::make_pair(0U, &X86::GR8RegClass);
18396 if (VT == MVT::i64 || VT == MVT::f64)
18397 return std::make_pair(0U, &X86::GR64RegClass);
18398 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018399 }
18400 // 32-bit fallthrough
18401 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018402 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018403 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18404 if (VT == MVT::i16)
18405 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18406 if (VT == MVT::i8 || VT == MVT::i1)
18407 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18408 if (VT == MVT::i64)
18409 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018410 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018411 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018412 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018413 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018414 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018415 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018416 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018417 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018418 return std::make_pair(0U, &X86::GR32RegClass);
18419 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018420 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018421 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018422 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018423 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018424 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018425 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018426 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18427 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018428 case 'f': // FP Stack registers.
18429 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18430 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018431 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018432 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018433 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018434 return std::make_pair(0U, &X86::RFP64RegClass);
18435 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018436 case 'y': // MMX_REGS if MMX allowed.
18437 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018438 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018439 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018440 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018441 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018442 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018443 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018444
Owen Anderson825b72b2009-08-11 20:47:22 +000018445 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018446 default: break;
18447 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018448 case MVT::f32:
18449 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018450 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018451 case MVT::f64:
18452 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018453 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018454 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018455 case MVT::v16i8:
18456 case MVT::v8i16:
18457 case MVT::v4i32:
18458 case MVT::v2i64:
18459 case MVT::v4f32:
18460 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018461 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018462 // AVX types.
18463 case MVT::v32i8:
18464 case MVT::v16i16:
18465 case MVT::v8i32:
18466 case MVT::v4i64:
18467 case MVT::v8f32:
18468 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018469 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018470 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018471 break;
18472 }
18473 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018474
Chris Lattnerf76d1802006-07-31 23:26:50 +000018475 // Use the default implementation in TargetLowering to convert the register
18476 // constraint into a member of a register class.
18477 std::pair<unsigned, const TargetRegisterClass*> Res;
18478 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018479
18480 // Not found as a standard register?
18481 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018482 // Map st(0) -> st(7) -> ST0
18483 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18484 tolower(Constraint[1]) == 's' &&
18485 tolower(Constraint[2]) == 't' &&
18486 Constraint[3] == '(' &&
18487 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18488 Constraint[5] == ')' &&
18489 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018490
Chris Lattner56d77c72009-09-13 22:41:48 +000018491 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018492 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018493 return Res;
18494 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018495
Chris Lattner56d77c72009-09-13 22:41:48 +000018496 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018497 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018498 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018499 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018500 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018501 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018502
18503 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018504 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018505 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018506 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018507 return Res;
18508 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018509
Dale Johannesen330169f2008-11-13 21:52:36 +000018510 // 'A' means EAX + EDX.
18511 if (Constraint == "A") {
18512 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018513 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018514 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018515 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018516 return Res;
18517 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018518
Chris Lattnerf76d1802006-07-31 23:26:50 +000018519 // Otherwise, check to see if this is a register class of the wrong value
18520 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18521 // turn into {ax},{dx}.
18522 if (Res.second->hasType(VT))
18523 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018524
Chris Lattnerf76d1802006-07-31 23:26:50 +000018525 // All of the single-register GCC register classes map their values onto
18526 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18527 // really want an 8-bit or 32-bit register, map to the appropriate register
18528 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018529 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018530 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018531 unsigned DestReg = 0;
18532 switch (Res.first) {
18533 default: break;
18534 case X86::AX: DestReg = X86::AL; break;
18535 case X86::DX: DestReg = X86::DL; break;
18536 case X86::CX: DestReg = X86::CL; break;
18537 case X86::BX: DestReg = X86::BL; break;
18538 }
18539 if (DestReg) {
18540 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018541 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018542 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018543 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018544 unsigned DestReg = 0;
18545 switch (Res.first) {
18546 default: break;
18547 case X86::AX: DestReg = X86::EAX; break;
18548 case X86::DX: DestReg = X86::EDX; break;
18549 case X86::CX: DestReg = X86::ECX; break;
18550 case X86::BX: DestReg = X86::EBX; break;
18551 case X86::SI: DestReg = X86::ESI; break;
18552 case X86::DI: DestReg = X86::EDI; break;
18553 case X86::BP: DestReg = X86::EBP; break;
18554 case X86::SP: DestReg = X86::ESP; break;
18555 }
18556 if (DestReg) {
18557 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018558 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018559 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018560 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018561 unsigned DestReg = 0;
18562 switch (Res.first) {
18563 default: break;
18564 case X86::AX: DestReg = X86::RAX; break;
18565 case X86::DX: DestReg = X86::RDX; break;
18566 case X86::CX: DestReg = X86::RCX; break;
18567 case X86::BX: DestReg = X86::RBX; break;
18568 case X86::SI: DestReg = X86::RSI; break;
18569 case X86::DI: DestReg = X86::RDI; break;
18570 case X86::BP: DestReg = X86::RBP; break;
18571 case X86::SP: DestReg = X86::RSP; break;
18572 }
18573 if (DestReg) {
18574 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018575 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018576 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018577 }
Craig Topperc9099502012-04-20 06:31:50 +000018578 } else if (Res.second == &X86::FR32RegClass ||
18579 Res.second == &X86::FR64RegClass ||
18580 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018581 // Handle references to XMM physical registers that got mapped into the
18582 // wrong class. This can happen with constraints like {xmm0} where the
18583 // target independent register mapper will just pick the first match it can
18584 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018585
18586 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018587 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018588 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018589 Res.second = &X86::FR64RegClass;
18590 else if (X86::VR128RegClass.hasType(VT))
18591 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018592 else if (X86::VR256RegClass.hasType(VT))
18593 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018594 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018595
Chris Lattnerf76d1802006-07-31 23:26:50 +000018596 return Res;
18597}