blob: 75928b70ec498c672b65280b590f26e49f702517 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000047#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000053#include "llvm/Target/TargetOptions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
David Greenea5f26012011-02-07 19:36:54 +000063static SDValue Insert128BitVector(SDValue Result,
64 SDValue Vec,
65 SDValue Idx,
66 SelectionDAG &DAG,
67 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000068
David Greenea5f26012011-02-07 19:36:54 +000069static SDValue Extract128BitVector(SDValue Vec,
70 SDValue Idx,
71 SelectionDAG &DAG,
72 DebugLoc dl);
73
74/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
75/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000076/// simple subregister reference. Idx is an index in the 128 bits we
77/// want. It need not be aligned to a 128-bit bounday. That makes
78/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000079static SDValue Extract128BitVector(SDValue Vec,
80 SDValue Idx,
81 SelectionDAG &DAG,
82 DebugLoc dl) {
83 EVT VT = Vec.getValueType();
84 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000085 EVT ElVT = VT.getVectorElementType();
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000086 int Factor = VT.getSizeInBits()/128;
87 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
88 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000089
90 // Extract from UNDEF is UNDEF.
91 if (Vec.getOpcode() == ISD::UNDEF)
92 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
93
94 if (isa<ConstantSDNode>(Idx)) {
95 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
96
97 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
98 // we can match to VEXTRACTF128.
99 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
100
101 // This is the index of the first element of the 128-bit chunk
102 // we want.
103 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
104 * ElemsPerChunk);
105
106 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000107 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
108 VecIdx);
109
110 return Result;
111 }
112
113 return SDValue();
114}
115
116/// Generate a DAG to put 128-bits into a vector > 128 bits. This
117/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000118/// simple superregister reference. Idx is an index in the 128 bits
119/// we want. It need not be aligned to a 128-bit bounday. That makes
120/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000121static SDValue Insert128BitVector(SDValue Result,
122 SDValue Vec,
123 SDValue Idx,
124 SelectionDAG &DAG,
125 DebugLoc dl) {
126 if (isa<ConstantSDNode>(Idx)) {
127 EVT VT = Vec.getValueType();
128 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
129
130 EVT ElVT = VT.getVectorElementType();
David Greenea5f26012011-02-07 19:36:54 +0000131 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
David Greenea5f26012011-02-07 19:36:54 +0000132 EVT ResultVT = Result.getValueType();
133
134 // Insert the relevant 128 bits.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000135 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000136
137 // This is the index of the first element of the 128-bit chunk
138 // we want.
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +0000139 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
David Greenea5f26012011-02-07 19:36:54 +0000140 * ElemsPerChunk);
141
142 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
David Greenea5f26012011-02-07 19:36:54 +0000143 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
144 VecIdx);
145 return Result;
146 }
147
148 return SDValue();
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000152 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
153 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000154
Evan Cheng2bffee22011-02-01 01:14:13 +0000155 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000156 if (is64Bit)
157 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000158 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000159 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000160
Evan Cheng203576a2011-07-20 19:50:42 +0000161 if (Subtarget->isTargetELF())
162 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000163 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000164 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000165 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000168X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000170 Subtarget = &TM.getSubtarget<X86Subtarget>();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000171 X86ScalarSSEf64 = Subtarget->hasXMMInt();
172 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000174
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000175 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000176 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000177
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000179 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
181 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000182 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000183 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000185
Eric Christopherde5e1012011-03-11 01:05:58 +0000186 // For 64-bit since we have so many registers use the ILP scheduler, for
187 // 32-bit code use the register pressure specific scheduling.
188 if (Subtarget->is64Bit())
189 setSchedulingPreference(Sched::ILP);
190 else
191 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000193
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000194 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000195 // Setup Windows compiler runtime calls.
196 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallName(RTLIB::SREM_I64, "_allrem");
199 setLibcallName(RTLIB::UREM_I64, "_aullrem");
200 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000201 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000202 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000208 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
209 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000210 }
211
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000213 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000214 setUseUnderscoreSetJmp(false);
215 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000216 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 // MS runtime is weird: it exports _setjmp, but longjmp!
218 setUseUnderscoreSetJmp(true);
219 setUseUnderscoreLongJmp(false);
220 } else {
221 setUseUnderscoreSetJmp(true);
222 setUseUnderscoreLongJmp(true);
223 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000227 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000233
Scott Michelfdc40a02009-02-17 22:15:04 +0000234 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000238 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
240 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000241
242 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
247 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
248 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000249
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
251 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
253 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
254 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000255
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000259 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000260 // We have an algorithm for SSE2->double, and we turn this into a
261 // 64-bit FILD followed by conditional FADD for other targets.
262 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000263 // We have an algorithm for SSE2, and we turn this into a 64-bit
264 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267
268 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
269 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
271 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000272
Devang Patel6a784892009-06-05 18:48:29 +0000273 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000274 // SSE has no i16 to fp conversion, only i32
275 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
281 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000283 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
285 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Dale Johannesen73328d12007-09-19 23:55:34 +0000288 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
289 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
291 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000292
Evan Cheng02568ff2006-01-30 22:13:22 +0000293 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
294 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
296 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000297
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000298 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000300 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
304 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000305 }
306
307 // Handle FP_TO_UINT by promoting the destination to a larger signed
308 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
310 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
311 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000316 } else if (!UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000317 // Since AVX is a superset of SSE3, only check for SSE here.
318 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 // Expand FP_TO_UINT into a select.
320 // FIXME: We would like to use a Custom expander here eventually to do
321 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000324 // With SSE3 we can use fisttpll to convert to a signed i64; without
325 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328
Chris Lattner399610a2006-12-05 18:22:22 +0000329 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000330 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
332 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000333 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000335 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000336 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000337 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000338 }
Chris Lattner21f66852005-12-23 05:15:23 +0000339
Dan Gohmanb00ee212008-02-18 19:34:53 +0000340 // Scalar integer divide and remainder are lowered to use operations that
341 // produce two results, to match the available instructions. This exposes
342 // the two-result form to trivial CSE, which is able to combine x/y and x%y
343 // into a single instruction.
344 //
345 // Scalar integer multiply-high is also lowered to use two-result
346 // operations, to match the available instructions. However, plain multiply
347 // (low) operations are left as Legal, as there are single-result
348 // instructions for this in x86. Using the two-result multiply instructions
349 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000350 for (unsigned i = 0, e = 4; i != e; ++i) {
351 MVT VT = IntVTs[i];
352 setOperationAction(ISD::MULHS, VT, Expand);
353 setOperationAction(ISD::MULHU, VT, Expand);
354 setOperationAction(ISD::SDIV, VT, Expand);
355 setOperationAction(ISD::UDIV, VT, Expand);
356 setOperationAction(ISD::SREM, VT, Expand);
357 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000358
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000359 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000360 setOperationAction(ISD::ADDC, VT, Custom);
361 setOperationAction(ISD::ADDE, VT, Custom);
362 setOperationAction(ISD::SUBC, VT, Custom);
363 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000364 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
367 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
368 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
369 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000370 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
372 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
373 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
375 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
376 setOperationAction(ISD::FREM , MVT::f32 , Expand);
377 setOperationAction(ISD::FREM , MVT::f64 , Expand);
378 setOperationAction(ISD::FREM , MVT::f80 , Expand);
379 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Craig Topper909652f2011-10-14 03:21:46 +0000381 if (Subtarget->hasBMI()) {
382 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
383 } else {
384 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
385 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
386 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
387 if (Subtarget->is64Bit())
388 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
389 }
Craig Topper37f21672011-10-11 06:44:02 +0000390
391 if (Subtarget->hasLZCNT()) {
392 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
393 } else {
394 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
395 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
396 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
397 if (Subtarget->is64Bit())
398 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 }
400
Benjamin Kramer1292c222010-12-04 20:32:23 +0000401 if (Subtarget->hasPOPCNT()) {
402 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
403 } else {
404 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
405 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
406 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
409 }
410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
412 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000413
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000415 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000416 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000417 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000418 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
420 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
421 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
422 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
423 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000424 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
426 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
427 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
428 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000431 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000434
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000435 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
437 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
438 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
439 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000440 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
442 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000443 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000444 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
446 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
447 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
448 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000449 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000451 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
453 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
454 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
457 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
458 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000459 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000461 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000463
Eric Christopher9a9d2752010-07-22 02:48:34 +0000464 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000465 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000466
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000467 // On X86 and X86-64, atomic operations are lowered to locked instructions.
468 // Locked instructions, in turn, have implicit fence semantics (all memory
469 // operations are flushed before issuing the locked instruction, and they
470 // are not buffered), so we can fold away the common pattern of
471 // fence-atomic-fence.
472 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000473
Mon P Wang63307c32008-05-05 19:05:59 +0000474 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000475 for (unsigned i = 0, e = 4; i != e; ++i) {
476 MVT VT = IntVTs[i];
477 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
478 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000479 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000480 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000481
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000482 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000483 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
485 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
486 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
487 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
488 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
489 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000491 }
492
Eli Friedman43f51ae2011-08-26 21:21:21 +0000493 if (Subtarget->hasCmpxchg16b()) {
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
495 }
496
Evan Cheng3c992d22006-03-07 02:02:57 +0000497 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000498 if (!Subtarget->isTargetDarwin() &&
499 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000500 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000502 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
505 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
506 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
507 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000508 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000509 setExceptionPointerRegister(X86::RAX);
510 setExceptionSelectorRegister(X86::RDX);
511 } else {
512 setExceptionPointerRegister(X86::EAX);
513 setExceptionSelectorRegister(X86::EDX);
514 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
516 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000517
Duncan Sands4a544a72011-09-06 13:37:06 +0000518 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
519 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000522
Nate Begemanacc398c2006-01-25 18:21:52 +0000523 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::VASTART , MVT::Other, Custom);
525 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000526 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
528 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000529 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::VAARG , MVT::Other, Expand);
531 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000532 }
Evan Chengae642192007-03-02 23:16:35 +0000533
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
535 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000536
537 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
538 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
539 MVT::i64 : MVT::i32, Custom);
540 else if (EnableSegmentedStacks)
541 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
542 MVT::i64 : MVT::i32, Custom);
543 else
544 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
545 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000546
Evan Chengc7ce29b2009-02-13 22:36:38 +0000547 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000548 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
551 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552
Evan Cheng223547a2006-01-31 22:28:30 +0000553 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::FABS , MVT::f64, Custom);
555 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000556
557 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::FNEG , MVT::f64, Custom);
559 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000560
Evan Cheng68c47cb2007-01-05 07:55:56 +0000561 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
563 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000564
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000565 // Lower this to FGETSIGNx86 plus an AND.
566 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
567 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
568
Evan Chengd25e9e82006-02-02 00:28:23 +0000569 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FSIN , MVT::f64, Expand);
571 setOperationAction(ISD::FCOS , MVT::f64, Expand);
572 setOperationAction(ISD::FSIN , MVT::f32, Expand);
573 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000574
Chris Lattnera54aa942006-01-29 06:26:08 +0000575 // Expand FP immediates into loads from the stack, except for the special
576 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000577 addLegalFPImmediate(APFloat(+0.0)); // xorpd
578 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000579 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 // Use SSE for f32, x87 for f64.
581 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
583 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000584
585 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
595 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
Nate Begemane1795842008-02-14 08:57:00 +0000601 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602 addLegalFPImmediate(APFloat(+0.0f)); // xorps
603 addLegalFPImmediate(APFloat(+0.0)); // FLD0
604 addLegalFPImmediate(APFloat(+1.0)); // FLD1
605 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
606 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
607
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
610 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000612 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
616 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000617
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
619 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000622
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000623 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
625 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000627 addLegalFPImmediate(APFloat(+0.0)); // FLD0
628 addLegalFPImmediate(APFloat(+1.0)); // FLD1
629 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
630 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
632 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
633 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
634 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000636
Cameron Zwarich33390842011-07-08 21:39:21 +0000637 // We don't support FMA.
638 setOperationAction(ISD::FMA, MVT::f64, Expand);
639 setOperationAction(ISD::FMA, MVT::f32, Expand);
640
Dale Johannesen59a58732007-08-05 18:49:15 +0000641 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000642 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
644 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
645 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000647 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000648 addLegalFPImmediate(TmpFlt); // FLD0
649 TmpFlt.changeSign();
650 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000651
652 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000653 APFloat TmpFlt2(+1.0);
654 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
655 &ignored);
656 addLegalFPImmediate(TmpFlt2); // FLD1
657 TmpFlt2.changeSign();
658 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
659 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
663 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000664 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000665
666 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000667 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000668
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
671 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
672 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::FLOG, MVT::f80, Expand);
675 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
676 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
677 setOperationAction(ISD::FEXP, MVT::f80, Expand);
678 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000679
Mon P Wangf007a8b2008-11-06 05:31:54 +0000680 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000681 // (for widening) or expand (for scalarization). Then we will selectively
682 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
684 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
685 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000701 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
702 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000724 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000734 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000735 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000739 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000740 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
741 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
742 setTruncStoreAction((MVT::SimpleValueType)VT,
743 (MVT::SimpleValueType)InnerVT, Expand);
744 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
745 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000747 }
748
Evan Chengc7ce29b2009-02-13 22:36:38 +0000749 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
750 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000751 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000752 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000753 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000754 }
755
Dale Johannesen0488fb62010-09-30 23:57:10 +0000756 // MMX-sized vectors (other than x86mmx) are expected to be expanded
757 // into smaller operations.
758 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
759 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
760 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
761 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
762 setOperationAction(ISD::AND, MVT::v8i8, Expand);
763 setOperationAction(ISD::AND, MVT::v4i16, Expand);
764 setOperationAction(ISD::AND, MVT::v2i32, Expand);
765 setOperationAction(ISD::AND, MVT::v1i64, Expand);
766 setOperationAction(ISD::OR, MVT::v8i8, Expand);
767 setOperationAction(ISD::OR, MVT::v4i16, Expand);
768 setOperationAction(ISD::OR, MVT::v2i32, Expand);
769 setOperationAction(ISD::OR, MVT::v1i64, Expand);
770 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
771 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
772 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
773 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
774 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
779 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
780 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
781 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
782 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
784 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000787
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000788 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
792 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
793 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
794 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
795 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
796 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
797 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
798 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
799 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
800 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
801 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000802 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000803 }
804
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000805 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000807
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000808 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
809 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
811 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
816 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
817 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
818 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
819 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
820 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
821 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
822 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
823 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
825 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
826 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
827 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
828 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
829 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
830 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000831
Nadav Rotem354efd82011-09-18 14:57:03 +0000832 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000833 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
834 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
835 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
839 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000842
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
848
Evan Cheng2c3ae372006-04-12 21:21:57 +0000849 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
851 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000852 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000854 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000855 // Do not attempt to custom lower non-128-bit vectors
856 if (!VT.is128BitVector())
857 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::BUILD_VECTOR,
859 VT.getSimpleVT().SimpleTy, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE,
861 VT.getSimpleVT().SimpleTy, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
863 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
868 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000872
Nate Begemancdd1eec2008-02-12 22:51:28 +0000873 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
875 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000876 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000877
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000878 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
880 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000881 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000882
883 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000884 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000885 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000886
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000889 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000891 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000893 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000895 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000896 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000897 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000900
Evan Cheng2c3ae372006-04-12 21:21:57 +0000901 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
903 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
904 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
905 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
908 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000909 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000910
Craig Topperc0d82852011-11-22 00:44:41 +0000911 if (Subtarget->hasSSE41orAVX()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000912 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
913 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
914 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
915 setOperationAction(ISD::FRINT, MVT::f32, Legal);
916 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
917 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
920 setOperationAction(ISD::FRINT, MVT::f64, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
922
Nate Begeman14d12ca2008-02-11 04:19:36 +0000923 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000926 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
927 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
928 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
929 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
930 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000931
Nate Begeman14d12ca2008-02-11 04:19:36 +0000932 // i8 and i16 vectors are custom , because the source register and source
933 // source memory operand types are not the same width. f32 vectors are
934 // custom since the immediate controlling the insert encodes additional
935 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000940
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000945
Pete Coopera77214a2011-11-14 19:38:42 +0000946 // FIXME: these should be Legal but thats only for the case where
947 // the index is constant. For now custom expand to deal with that
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000949 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
950 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000951 }
952 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000953
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000954 if (Subtarget->hasXMMInt()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000955 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000956 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000957
Nadav Rotem43012222011-05-11 08:12:09 +0000958 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000959 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000960
Nadav Rotem43012222011-05-11 08:12:09 +0000961 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000962 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000963
964 if (Subtarget->hasAVX2()) {
965 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
966 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
967
968 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
969 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
970
971 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
972 } else {
973 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
974 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
975
976 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
977 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
978
979 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
980 }
Nadav Rotem43012222011-05-11 08:12:09 +0000981 }
982
Craig Topperc0d82852011-11-22 00:44:41 +0000983 if (Subtarget->hasSSE42orAVX())
Duncan Sands28b77e92011-09-06 19:07:46 +0000984 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000985
David Greene9b9838d2009-06-29 16:47:10 +0000986 if (!UseSoftFloat && Subtarget->hasAVX()) {
Bruno Cardoso Lopesdbd4fe22011-07-21 02:24:08 +0000987 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
988 addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
989 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
990 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
991 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
992 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000993
Owen Anderson825b72b2009-08-11 20:47:22 +0000994 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
996 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000997
Owen Anderson825b72b2009-08-11 20:47:22 +0000998 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
999 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1000 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1001 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1002 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1003 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001004
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1006 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1007 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1008 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1009 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1010 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001012 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1013 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001014 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001015
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00001016 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f64, Custom);
1017 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i64, Custom);
1018 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
1019 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
1020 setOperationAction(ISD::CONCAT_VECTORS, MVT::v32i8, Custom);
1021 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i16, Custom);
1022
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001023 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1024 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1025
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001026 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1027 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1028
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001029 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001030 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001031
Duncan Sands28b77e92011-09-06 19:07:46 +00001032 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1033 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1034 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1035 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001036
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001037 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1038 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1039 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1040
Craig Topperaaa643c2011-11-09 07:28:55 +00001041 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1042 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1043 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1044 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001045
Craig Topperaaa643c2011-11-09 07:28:55 +00001046 if (Subtarget->hasAVX2()) {
1047 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1048 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1049 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1050 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001051
Craig Topperaaa643c2011-11-09 07:28:55 +00001052 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1053 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1054 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1055 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001056
Craig Topperaaa643c2011-11-09 07:28:55 +00001057 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1058 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1059 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001060 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001061
1062 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063
1064 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1065 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1066
1067 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1068 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1069
1070 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001071 } else {
1072 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1073 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1074 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1075 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1076
1077 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1078 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1079 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1080 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1081
1082 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1083 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1084 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1085 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001086
1087 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1088 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1089
1090 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1091 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1092
1093 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001094 }
Craig Topper13894fa2011-08-24 06:14:18 +00001095
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001096 // Custom lower several nodes for 256-bit types.
David Greene54d8eba2011-01-27 22:38:56 +00001097 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001098 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1099 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1100 EVT VT = SVT;
1101
1102 // Extract subvector is special because the value type
1103 // (result) is 128-bit but the source is 256-bit wide.
1104 if (VT.is128BitVector())
1105 setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1106
1107 // Do not attempt to custom lower other non-256-bit vectors
1108 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001109 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001110
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001111 setOperationAction(ISD::BUILD_VECTOR, SVT, Custom);
1112 setOperationAction(ISD::VECTOR_SHUFFLE, SVT, Custom);
1113 setOperationAction(ISD::INSERT_VECTOR_ELT, SVT, Custom);
1114 setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00001115 setOperationAction(ISD::SCALAR_TO_VECTOR, SVT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001116 setOperationAction(ISD::INSERT_SUBVECTOR, SVT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001117 }
1118
David Greene54d8eba2011-01-27 22:38:56 +00001119 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001120 for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
1121 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
1122 EVT VT = SVT;
David Greene54d8eba2011-01-27 22:38:56 +00001123
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001124 // Do not attempt to promote non-256-bit vectors
1125 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001126 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 setOperationAction(ISD::AND, SVT, Promote);
1129 AddPromotedToType (ISD::AND, SVT, MVT::v4i64);
1130 setOperationAction(ISD::OR, SVT, Promote);
1131 AddPromotedToType (ISD::OR, SVT, MVT::v4i64);
1132 setOperationAction(ISD::XOR, SVT, Promote);
1133 AddPromotedToType (ISD::XOR, SVT, MVT::v4i64);
1134 setOperationAction(ISD::LOAD, SVT, Promote);
1135 AddPromotedToType (ISD::LOAD, SVT, MVT::v4i64);
1136 setOperationAction(ISD::SELECT, SVT, Promote);
1137 AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001138 }
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001141 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1142 // of this type with custom code.
1143 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1144 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
1145 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1146 }
1147
Evan Cheng6be2c582006-04-05 23:38:46 +00001148 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001150
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001151
Eli Friedman962f5492010-06-02 19:35:46 +00001152 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1153 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001154 //
Eli Friedman962f5492010-06-02 19:35:46 +00001155 // FIXME: We really should do custom legalization for addition and
1156 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1157 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001158 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1159 // Add/Sub/Mul with overflow operations are custom lowered.
1160 MVT VT = IntVTs[i];
1161 setOperationAction(ISD::SADDO, VT, Custom);
1162 setOperationAction(ISD::UADDO, VT, Custom);
1163 setOperationAction(ISD::SSUBO, VT, Custom);
1164 setOperationAction(ISD::USUBO, VT, Custom);
1165 setOperationAction(ISD::SMULO, VT, Custom);
1166 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001167 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001168
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001169 // There are no 8-bit 3-address imul/mul instructions
1170 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1171 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001172
Evan Chengd54f2d52009-03-31 19:38:51 +00001173 if (!Subtarget->is64Bit()) {
1174 // These libcalls are not available in 32-bit.
1175 setLibcallName(RTLIB::SHL_I128, 0);
1176 setLibcallName(RTLIB::SRL_I128, 0);
1177 setLibcallName(RTLIB::SRA_I128, 0);
1178 }
1179
Evan Cheng206ee9d2006-07-07 08:33:52 +00001180 // We have target-specific dag combine patterns for the following nodes:
1181 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001182 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001183 setTargetDAGCombine(ISD::BUILD_VECTOR);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001184 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001185 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001186 setTargetDAGCombine(ISD::SHL);
1187 setTargetDAGCombine(ISD::SRA);
1188 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001189 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001190 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001191 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001192 setTargetDAGCombine(ISD::FADD);
1193 setTargetDAGCombine(ISD::FSUB);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001194 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001195 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001196 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001197 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001198 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001199 if (Subtarget->is64Bit())
1200 setTargetDAGCombine(ISD::MUL);
Craig Topperb4c94572011-10-21 06:55:01 +00001201 if (Subtarget->hasBMI())
1202 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001204 computeRegisterProperties();
1205
Evan Cheng05219282011-01-06 06:52:41 +00001206 // On Darwin, -Os means optimize for size without hurting performance,
1207 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001208 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001209 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001211 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1212 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1213 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001214 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001215 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001216
1217 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001218}
1219
Scott Michel5b8f82e2008-03-10 15:42:14 +00001220
Duncan Sands28b77e92011-09-06 19:07:46 +00001221EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1222 if (!VT.isVector()) return MVT::i8;
1223 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001224}
1225
1226
Evan Cheng29286502008-01-23 23:17:41 +00001227/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1228/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001229static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001230 if (MaxAlign == 16)
1231 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001232 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001233 if (VTy->getBitWidth() == 128)
1234 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001235 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001236 unsigned EltAlign = 0;
1237 getMaxByValAlign(ATy->getElementType(), EltAlign);
1238 if (EltAlign > MaxAlign)
1239 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001240 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001241 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1242 unsigned EltAlign = 0;
1243 getMaxByValAlign(STy->getElementType(i), EltAlign);
1244 if (EltAlign > MaxAlign)
1245 MaxAlign = EltAlign;
1246 if (MaxAlign == 16)
1247 break;
1248 }
1249 }
1250 return;
1251}
1252
1253/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1254/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001255/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1256/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001257unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001258 if (Subtarget->is64Bit()) {
1259 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001260 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001261 if (TyAlign > 8)
1262 return TyAlign;
1263 return 8;
1264 }
1265
Evan Cheng29286502008-01-23 23:17:41 +00001266 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001267 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001268 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001269 return Align;
1270}
Chris Lattner2b02a442007-02-25 08:29:00 +00001271
Evan Chengf0df0312008-05-15 08:39:06 +00001272/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001273/// and store operations as a result of memset, memcpy, and memmove
1274/// lowering. If DstAlign is zero that means it's safe to destination
1275/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1276/// means there isn't a need to check it against alignment requirement,
1277/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001278/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001279/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1280/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1281/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001282/// It returns EVT::Other if the type should be determined using generic
1283/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001284EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001285X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1286 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001287 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001288 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001289 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001290 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1291 // linux. This is because the stack realignment code can't handle certain
1292 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001293 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001294 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001295 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001296 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001297 (Subtarget->isUnalignedMemAccessFast() ||
1298 ((DstAlign == 0 || DstAlign >= 16) &&
1299 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001300 Subtarget->getStackAlignment() >= 16) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001301 if (Subtarget->hasAVX() &&
1302 Subtarget->getStackAlignment() >= 32)
1303 return MVT::v8f32;
1304 if (Subtarget->hasXMMInt())
Evan Cheng255f20f2010-04-01 06:04:33 +00001305 return MVT::v4i32;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001306 if (Subtarget->hasXMM())
Evan Cheng255f20f2010-04-01 06:04:33 +00001307 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001308 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001309 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001310 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001311 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001312 // Do not use f64 to lower memcpy if source is string constant. It's
1313 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001314 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001315 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001316 }
Evan Chengf0df0312008-05-15 08:39:06 +00001317 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 return MVT::i64;
1319 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001320}
1321
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001322/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1323/// current function. The returned value is a member of the
1324/// MachineJumpTableInfo::JTEntryKind enum.
1325unsigned X86TargetLowering::getJumpTableEncoding() const {
1326 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1327 // symbol.
1328 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1329 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001330 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001331
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001332 // Otherwise, use the normal jump table encoding heuristics.
1333 return TargetLowering::getJumpTableEncoding();
1334}
1335
Chris Lattnerc64daab2010-01-26 05:02:42 +00001336const MCExpr *
1337X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1338 const MachineBasicBlock *MBB,
1339 unsigned uid,MCContext &Ctx) const{
1340 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1341 Subtarget->isPICStyleGOT());
1342 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1343 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001344 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1345 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001346}
1347
Evan Chengcc415862007-11-09 01:32:10 +00001348/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1349/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001350SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001351 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001352 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001353 // This doesn't have DebugLoc associated with it, but is not really the
1354 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001355 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001356 return Table;
1357}
1358
Chris Lattner589c6f62010-01-26 06:28:43 +00001359/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1360/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1361/// MCExpr.
1362const MCExpr *X86TargetLowering::
1363getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1364 MCContext &Ctx) const {
1365 // X86-64 uses RIP relative addressing based on the jump table label.
1366 if (Subtarget->isPICStyleRIPRel())
1367 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1368
1369 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001370 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001371}
1372
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001373// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001374std::pair<const TargetRegisterClass*, uint8_t>
1375X86TargetLowering::findRepresentativeClass(EVT VT) const{
1376 const TargetRegisterClass *RRC = 0;
1377 uint8_t Cost = 1;
1378 switch (VT.getSimpleVT().SimpleTy) {
1379 default:
1380 return TargetLowering::findRepresentativeClass(VT);
1381 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1382 RRC = (Subtarget->is64Bit()
1383 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1384 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001385 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001386 RRC = X86::VR64RegisterClass;
1387 break;
1388 case MVT::f32: case MVT::f64:
1389 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1390 case MVT::v4f32: case MVT::v2f64:
1391 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1392 case MVT::v4f64:
1393 RRC = X86::VR128RegisterClass;
1394 break;
1395 }
1396 return std::make_pair(RRC, Cost);
1397}
1398
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001399bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1400 unsigned &Offset) const {
1401 if (!Subtarget->isTargetLinux())
1402 return false;
1403
1404 if (Subtarget->is64Bit()) {
1405 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1406 Offset = 0x28;
1407 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1408 AddressSpace = 256;
1409 else
1410 AddressSpace = 257;
1411 } else {
1412 // %gs:0x14 on i386
1413 Offset = 0x14;
1414 AddressSpace = 256;
1415 }
1416 return true;
1417}
1418
1419
Chris Lattner2b02a442007-02-25 08:29:00 +00001420//===----------------------------------------------------------------------===//
1421// Return Value Calling Convention Implementation
1422//===----------------------------------------------------------------------===//
1423
Chris Lattner59ed56b2007-02-28 04:55:35 +00001424#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001425
Michael J. Spencerec38de22010-10-10 22:04:20 +00001426bool
Eric Christopher471e4222011-06-08 23:55:35 +00001427X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1428 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001429 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001430 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001431 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001432 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001433 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001434 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001435}
1436
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437SDValue
1438X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001439 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001441 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001442 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001443 MachineFunction &MF = DAG.getMachineFunction();
1444 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001445
Chris Lattner9774c912007-02-27 05:28:59 +00001446 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001447 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 RVLocs, *DAG.getContext());
1449 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Evan Chengdcea1632010-02-04 02:40:39 +00001451 // Add the regs to the liveout set for the function.
1452 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1453 for (unsigned i = 0; i != RVLocs.size(); ++i)
1454 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1455 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001458
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001460 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1461 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001462 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1463 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001464
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001465 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001466 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1467 CCValAssign &VA = RVLocs[i];
1468 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001469 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001470 EVT ValVT = ValToCopy.getValueType();
1471
Dale Johannesenc4510512010-09-24 19:05:48 +00001472 // If this is x86-64, and we disabled SSE, we can't return FP values,
1473 // or SSE or MMX vectors.
1474 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1475 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001476 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001477 report_fatal_error("SSE register return with SSE disabled");
1478 }
1479 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1480 // llvm-gcc has never done it right and no one has noticed, so this
1481 // should be OK for now.
1482 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001483 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001484 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001485
Chris Lattner447ff682008-03-11 03:23:40 +00001486 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1487 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001488 if (VA.getLocReg() == X86::ST0 ||
1489 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001490 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1491 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001492 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001493 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps.push_back(ValToCopy);
1495 // Don't emit a copytoreg.
1496 continue;
1497 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001498
Evan Cheng242b38b2009-02-23 09:03:22 +00001499 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1500 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001501 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001502 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001503 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001504 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001505 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1506 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001507 // If we don't have SSE2 available, convert to v4f32 so the generated
1508 // register is legal.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00001509 if (!Subtarget->hasXMMInt())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001511 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001512 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001513 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001514
Dale Johannesendd64c412009-02-04 00:33:20 +00001515 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001516 Flag = Chain.getValue(1);
1517 }
Dan Gohman61a92132008-04-21 23:59:07 +00001518
1519 // The x86-64 ABI for returning structs by value requires that we copy
1520 // the sret argument into %rax for the return. We saved the argument into
1521 // a virtual register in the entry block, so now we copy the value out
1522 // and into %rax.
1523 if (Subtarget->is64Bit() &&
1524 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1525 MachineFunction &MF = DAG.getMachineFunction();
1526 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1527 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001528 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001529 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001530 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001531
Dale Johannesendd64c412009-02-04 00:33:20 +00001532 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001533 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001534
1535 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001536 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Chris Lattner447ff682008-03-11 03:23:40 +00001539 RetOps[0] = Chain; // Update chain.
1540
1541 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001542 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001543 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
1545 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001547}
1548
Evan Cheng3d2125c2010-11-30 23:55:39 +00001549bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1550 if (N->getNumValues() != 1)
1551 return false;
1552 if (!N->hasNUsesOfValue(1, 0))
1553 return false;
1554
1555 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001556 if (Copy->getOpcode() != ISD::CopyToReg &&
1557 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001558 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001559
1560 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001561 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001562 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001563 if (UI->getOpcode() != X86ISD::RET_FLAG)
1564 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001565 HasRet = true;
1566 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001567
Evan Cheng1bf891a2010-12-01 22:59:46 +00001568 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001569}
1570
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001571EVT
1572X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001573 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001574 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001575 // TODO: Is this also valid on 32-bit?
1576 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001577 ReturnMVT = MVT::i8;
1578 else
1579 ReturnMVT = MVT::i32;
1580
1581 EVT MinVT = getRegisterType(Context, ReturnMVT);
1582 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001583}
1584
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585/// LowerCallResult - Lower the result values of a call into the
1586/// appropriate copies out of appropriate physical registers.
1587///
1588SDValue
1589X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 const SmallVectorImpl<ISD::InputArg> &Ins,
1592 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001593 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001594
Chris Lattnere32bbf62007-02-28 07:09:55 +00001595 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001596 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001597 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001598 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1599 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner3085e152007-02-25 08:59:22 +00001602 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001603 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001604 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001605 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Torok Edwin3f142c32009-02-01 18:15:56 +00001607 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001609 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001610 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001611 }
1612
Evan Cheng79fb3b42009-02-20 20:43:02 +00001613 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001614
1615 // If this is a call to a function that returns an fp value on the floating
1616 // point stack, we must guarantee the the value is popped from the stack, so
1617 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001618 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001619 // instead.
1620 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1621 // If we prefer to use the value in xmm registers, copy it out as f80 and
1622 // use a truncate to move it from fp stack reg to xmm reg.
1623 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001624 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001625 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1626 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001627 Val = Chain.getValue(0);
1628
1629 // Round the f80 to the right size, which also moves it to the appropriate
1630 // xmm register.
1631 if (CopyVT != VA.getValVT())
1632 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1633 // This truncation won't change the value.
1634 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001635 } else {
1636 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1637 CopyVT, InFlag).getValue(1);
1638 Val = Chain.getValue(0);
1639 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001640 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001642 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001643
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001645}
1646
1647
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001648//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001650//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001651// StdCall calling convention seems to be standard for many Windows' API
1652// routines and around. It differs from C calling convention just a little:
1653// callee should clean up the stack, not caller. Symbols should be also
1654// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001655// For info on fast calling convention see Fast Calling Convention (tail call)
1656// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001659/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1661 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001663
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001665}
1666
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001667/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001668/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669static bool
1670ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1671 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001675}
1676
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001677/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1678/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001679/// the specific parameter attribute. The copy will be passed as a byval
1680/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001681static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001682CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001683 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1684 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001685 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001686
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001688 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001689 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001690}
1691
Chris Lattner29689432010-03-11 00:22:57 +00001692/// IsTailCallConvention - Return true if the calling convention is one that
1693/// supports tail call optimization.
1694static bool IsTailCallConvention(CallingConv::ID CC) {
1695 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1696}
1697
Evan Cheng485fafc2011-03-21 01:19:09 +00001698bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1699 if (!CI->isTailCall())
1700 return false;
1701
1702 CallSite CS(CI);
1703 CallingConv::ID CalleeCC = CS.getCallingConv();
1704 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1705 return false;
1706
1707 return true;
1708}
1709
Evan Cheng0c439eb2010-01-27 00:07:07 +00001710/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1711/// a tailcall target by changing its ABI.
1712static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001713 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001714}
1715
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716SDValue
1717X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001718 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 const SmallVectorImpl<ISD::InputArg> &Ins,
1720 DebugLoc dl, SelectionDAG &DAG,
1721 const CCValAssign &VA,
1722 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001723 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001724 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001726 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001727 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001728 EVT ValVT;
1729
1730 // If value is passed by pointer we have address passed instead of the value
1731 // itself.
1732 if (VA.getLocInfo() == CCValAssign::Indirect)
1733 ValVT = VA.getLocVT();
1734 else
1735 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001736
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001737 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001738 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001739 // In case of tail call optimization mark all arguments mutable. Since they
1740 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001741 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001742 unsigned Bytes = Flags.getByValSize();
1743 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1744 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001745 return DAG.getFrameIndex(FI, getPointerTy());
1746 } else {
1747 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001748 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001749 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1750 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001751 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001752 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001753 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001754}
1755
Dan Gohman475871a2008-07-27 21:46:04 +00001756SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001758 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001759 bool isVarArg,
1760 const SmallVectorImpl<ISD::InputArg> &Ins,
1761 DebugLoc dl,
1762 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001763 SmallVectorImpl<SDValue> &InVals)
1764 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001765 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 const Function* Fn = MF.getFunction();
1769 if (Fn->hasExternalLinkage() &&
1770 Subtarget->isTargetCygMing() &&
1771 Fn->getName() == "main")
1772 FuncInfo->setForceFramePointer(true);
1773
Evan Cheng1bc78042006-04-26 01:20:17 +00001774 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001776 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777
Chris Lattner29689432010-03-11 00:22:57 +00001778 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1779 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001780
Chris Lattner638402b2007-02-28 07:00:42 +00001781 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001782 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001783 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001785
1786 // Allocate shadow area for Win64
1787 if (IsWin64) {
1788 CCInfo.AllocateStack(32, 8);
1789 }
1790
Duncan Sands45907662010-10-31 13:21:44 +00001791 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001792
Chris Lattnerf39f7712007-02-28 05:46:49 +00001793 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001794 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001795 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1796 CCValAssign &VA = ArgLocs[i];
1797 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1798 // places.
1799 assert(VA.getValNo() != LastVal &&
1800 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001801 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001802 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001803
Chris Lattnerf39f7712007-02-28 05:46:49 +00001804 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001805 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001806 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001808 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001815 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1816 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001817 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001818 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001819 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001820 RC = X86::VR64RegisterClass;
1821 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001822 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001823
Devang Patel68e6bee2011-02-21 23:21:26 +00001824 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001826
Chris Lattnerf39f7712007-02-28 05:46:49 +00001827 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1828 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1829 // right size.
1830 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001831 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001832 DAG.getValueType(VA.getValVT()));
1833 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001834 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001835 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001836 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001837 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001838
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001839 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001840 // Handle MMX values passed in XMM regs.
1841 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001842 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1843 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001844 } else
1845 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001846 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001847 } else {
1848 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001850 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001851
1852 // If value is passed via pointer - do a load.
1853 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001854 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001855 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001856
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001858 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001859
Dan Gohman61a92132008-04-21 23:59:07 +00001860 // The x86-64 ABI for returning structs by value requires that we copy
1861 // the sret argument into %rax for the return. Save the argument into
1862 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001863 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001864 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1865 unsigned Reg = FuncInfo->getSRetReturnReg();
1866 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001867 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001868 FuncInfo->setSRetReturnReg(Reg);
1869 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001870 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001872 }
1873
Chris Lattnerf39f7712007-02-28 05:46:49 +00001874 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001875 // Align stack specially for tail calls.
1876 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001877 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001878
Evan Cheng1bc78042006-04-26 01:20:17 +00001879 // If the function takes variable number of arguments, make a frame index for
1880 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001881 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001882 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1883 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001884 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 }
1886 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001887 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1888
1889 // FIXME: We should really autogenerate these arrays
1890 static const unsigned GPR64ArgRegsWin64[] = {
1891 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001893 static const unsigned GPR64ArgRegs64Bit[] = {
1894 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1895 };
1896 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001897 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1898 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1899 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001900 const unsigned *GPR64ArgRegs;
1901 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001902
1903 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001904 // The XMM registers which might contain var arg parameters are shadowed
1905 // in their paired GPR. So we only need to save the GPR to their home
1906 // slots.
1907 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001908 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001909 } else {
1910 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1911 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001912
1913 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001914 }
1915 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1916 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917
Devang Patel578efa92009-06-05 21:57:13 +00001918 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001919 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001920 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001921 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001922 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001923 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001924 // Kernel mode asks for SSE to be disabled, so don't push them
1925 // on the stack.
1926 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001927
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001928 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001929 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001930 // Get to the caller-allocated home save location. Add 8 to account
1931 // for the return address.
1932 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001933 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001934 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001935 // Fixup to set vararg frame on shadow area (4 x i64).
1936 if (NumIntRegs < 4)
1937 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001938 } else {
1939 // For X86-64, if there are vararg parameters that are passed via
1940 // registers, then we must store them to their spots on the stack so they
1941 // may be loaded by deferencing the result of va_next.
1942 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1943 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1944 FuncInfo->setRegSaveFrameIndex(
1945 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001946 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001947 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001948
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001951 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1952 getPointerTy());
1953 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001954 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001955 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1956 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001957 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001958 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001961 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001962 MachinePointerInfo::getFixedStack(
1963 FuncInfo->getRegSaveFrameIndex(), Offset),
1964 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001966 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001968
Dan Gohmanface41a2009-08-16 21:24:25 +00001969 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1970 // Now store the XMM (fp + vector) parameter registers.
1971 SmallVector<SDValue, 11> SaveXMMOps;
1972 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001973
Devang Patel68e6bee2011-02-21 23:21:26 +00001974 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001975 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1976 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001977
Dan Gohman1e93df62010-04-17 14:41:14 +00001978 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1979 FuncInfo->getRegSaveFrameIndex()));
1980 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1981 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001982
Dan Gohmanface41a2009-08-16 21:24:25 +00001983 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001984 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001985 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001986 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1987 SaveXMMOps.push_back(Val);
1988 }
1989 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1990 MVT::Other,
1991 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001992 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001993
1994 if (!MemOps.empty())
1995 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1996 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Gordon Henriksen86737662008-01-05 16:56:59 +00002000 // Some CCs need callee pop.
Evan Chengef41ff62011-06-23 17:54:54 +00002001 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002002 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002003 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002004 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002005 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00002006 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00002007 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002008 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002009
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002011 // RegSaveFrameIndex is X86-64 only.
2012 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002013 if (CallConv == CallingConv::X86_FastCall ||
2014 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002015 // fastcc functions can't have varargs.
2016 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 }
Evan Cheng25caf632006-05-23 21:06:34 +00002018
Rafael Espindola76927d752011-08-30 19:39:58 +00002019 FuncInfo->setArgumentStackSize(StackSize);
2020
Dan Gohman98ca4f22009-08-05 01:29:28 +00002021 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002022}
2023
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2026 SDValue StackPtr, SDValue Arg,
2027 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002028 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002030 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002032 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002033 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002034 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002035
2036 return DAG.getStore(Chain, dl, Arg, PtrOff,
2037 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002038 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002039}
2040
Bill Wendling64e87322009-01-16 19:25:27 +00002041/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002042/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002043SDValue
2044X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002045 SDValue &OutRetAddr, SDValue Chain,
2046 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002047 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002048 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002049 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002050 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002051
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002052 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002053 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002054 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002056}
2057
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002058/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002059/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002060static SDValue
2061EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002063 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002064 // Store the return address to the appropriate stack slot.
2065 if (!FPDiff) return Chain;
2066 // Calculate the new stack slot for the return address.
2067 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002068 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002069 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002071 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002072 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002073 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002074 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002075 return Chain;
2076}
2077
Dan Gohman98ca4f22009-08-05 01:29:28 +00002078SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002079X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002080 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002081 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002082 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002083 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002084 const SmallVectorImpl<ISD::InputArg> &Ins,
2085 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002086 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002087 MachineFunction &MF = DAG.getMachineFunction();
2088 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002089 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002090 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002091 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092
Evan Cheng5f941932010-02-05 02:21:12 +00002093 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002094 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002095 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2096 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002097 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002098
2099 // Sibcalls are automatically detected tailcalls which do not require
2100 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00002101 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002102 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002103
2104 if (isTailCall)
2105 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002106 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002107
Chris Lattner29689432010-03-11 00:22:57 +00002108 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2109 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002110
Chris Lattner638402b2007-02-28 07:00:42 +00002111 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002112 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002113 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002115
2116 // Allocate shadow area for Win64
2117 if (IsWin64) {
2118 CCInfo.AllocateStack(32, 8);
2119 }
2120
Duncan Sands45907662010-10-31 13:21:44 +00002121 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Chris Lattner423c5f42007-02-28 05:31:48 +00002123 // Get a count of how many bytes are to be pushed on the stack.
2124 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002126 // This is a sibcall. The memory operands are available in caller's
2127 // own caller's stack.
2128 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002129 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002130 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002131
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002133 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002134 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002135 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002136 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2137 FPDiff = NumBytesCallerPushed - NumBytes;
2138
2139 // Set the delta of movement of the returnaddr stackslot.
2140 // But only set if delta is greater than previous delta.
2141 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2142 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2143 }
2144
Evan Chengf22f9b32010-02-06 03:28:46 +00002145 if (!IsSibcall)
2146 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002147
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002149 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002150 if (isTailCall && FPDiff)
2151 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2152 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002153
Dan Gohman475871a2008-07-27 21:46:04 +00002154 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2155 SmallVector<SDValue, 8> MemOpChains;
2156 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002157
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002158 // Walk the register/memloc assignments, inserting copies/loads. In the case
2159 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002160 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2161 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002162 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002163 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002165 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002166
Chris Lattner423c5f42007-02-28 05:31:48 +00002167 // Promote the value if needed.
2168 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002169 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002170 case CCValAssign::Full: break;
2171 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002172 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002173 break;
2174 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002175 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002176 break;
2177 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002178 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2179 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002180 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002181 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2182 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002183 } else
2184 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2185 break;
2186 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002187 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002188 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002189 case CCValAssign::Indirect: {
2190 // Store the argument.
2191 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002192 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002193 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002194 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002195 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002196 Arg = SpillSlot;
2197 break;
2198 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002200
Chris Lattner423c5f42007-02-28 05:31:48 +00002201 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002202 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2203 if (isVarArg && IsWin64) {
2204 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2205 // shadow reg if callee is a varargs function.
2206 unsigned ShadowReg = 0;
2207 switch (VA.getLocReg()) {
2208 case X86::XMM0: ShadowReg = X86::RCX; break;
2209 case X86::XMM1: ShadowReg = X86::RDX; break;
2210 case X86::XMM2: ShadowReg = X86::R8; break;
2211 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002212 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002213 if (ShadowReg)
2214 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002215 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002216 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002217 assert(VA.isMemLoc());
2218 if (StackPtr.getNode() == 0)
2219 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2220 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2221 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002222 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002224
Evan Cheng32fe1032006-05-25 00:59:30 +00002225 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002227 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002228
Evan Cheng347d5f72006-04-28 21:29:37 +00002229 // Build a sequence of copy-to-reg nodes chained together with token chain
2230 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002231 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002232 // Tail call byval lowering might overwrite argument registers so in case of
2233 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002236 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002237 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002238 InFlag = Chain.getValue(1);
2239 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002240
Chris Lattner88e1fd52009-07-09 04:24:46 +00002241 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002242 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2243 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002245 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2246 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002247 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002248 InFlag);
2249 InFlag = Chain.getValue(1);
2250 } else {
2251 // If we are tail calling and generating PIC/GOT style code load the
2252 // address of the callee into ECX. The value in ecx is used as target of
2253 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2254 // for tail calls on PIC/GOT architectures. Normally we would just put the
2255 // address of GOT into ebx and then call target@PLT. But for tail calls
2256 // ebx would be restored (since ebx is callee saved) before jumping to the
2257 // target@PLT.
2258
2259 // Note: The actual moving to ECX is done further down.
2260 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2261 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2262 !G->getGlobal()->hasProtectedVisibility())
2263 Callee = LowerGlobalAddress(Callee, DAG);
2264 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002265 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002266 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002267 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002268
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002269 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002270 // From AMD64 ABI document:
2271 // For calls that may call functions that use varargs or stdargs
2272 // (prototype-less calls or calls to functions containing ellipsis (...) in
2273 // the declaration) %al is used as hidden argument to specify the number
2274 // of SSE registers used. The contents of %al do not need to match exactly
2275 // the number of registers, but must be an ubound on the number of SSE
2276 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002277
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 // Count the number of XMM registers allocated.
2279 static const unsigned XMMArgRegs[] = {
2280 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2281 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2282 };
2283 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002284 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002285 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dale Johannesendd64c412009-02-04 00:33:20 +00002287 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002289 InFlag = Chain.getValue(1);
2290 }
2291
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002292
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002293 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294 if (isTailCall) {
2295 // Force all the incoming stack arguments to be loaded from the stack
2296 // before any new outgoing arguments are stored to the stack, because the
2297 // outgoing stack slots may alias the incoming argument stack slots, and
2298 // the alias isn't otherwise explicit. This is slightly more conservative
2299 // than necessary, because it means that each store effectively depends
2300 // on every argument instead of just those arguments it would clobber.
2301 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2302
Dan Gohman475871a2008-07-27 21:46:04 +00002303 SmallVector<SDValue, 8> MemOpChains2;
2304 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002305 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002306 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002307 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002308 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2310 CCValAssign &VA = ArgLocs[i];
2311 if (VA.isRegLoc())
2312 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002313 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002314 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002315 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002316 // Create frame index.
2317 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002318 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002319 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002320 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002321
Duncan Sands276dcbd2008-03-21 09:14:45 +00002322 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002323 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002324 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002326 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002327 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002328 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002329
Dan Gohman98ca4f22009-08-05 01:29:28 +00002330 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2331 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002332 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002334 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002335 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002337 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002338 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002339 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 }
2341 }
2342
2343 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002345 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002346
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002347 // Copy arguments to their registers.
2348 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002349 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002350 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002351 InFlag = Chain.getValue(1);
2352 }
Dan Gohman475871a2008-07-27 21:46:04 +00002353 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002354
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002356 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002357 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002358 }
2359
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002360 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2361 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2362 // In the 64-bit large code model, we have to make all calls
2363 // through a register, since the call instruction's 32-bit
2364 // pc-relative offset may not be large enough to hold the whole
2365 // address.
2366 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002367 // If the callee is a GlobalAddress node (quite common, every direct call
2368 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2369 // it.
2370
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002371 // We should use extra load for direct calls to dllimported functions in
2372 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002373 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002374 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002375 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002376 bool ExtraLoad = false;
2377 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002378
Chris Lattner48a7d022009-07-09 05:02:21 +00002379 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2380 // external symbols most go through the PLT in PIC mode. If the symbol
2381 // has hidden or protected visibility, or if it is static or local, then
2382 // we don't need to use the PLT - we can directly call it.
2383 if (Subtarget->isTargetELF() &&
2384 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002385 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002386 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002387 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002388 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002389 (!Subtarget->getTargetTriple().isMacOSX() ||
2390 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002391 // PC-relative references to external symbols should go through $stub,
2392 // unless we're building with the leopard linker or later, which
2393 // automatically synthesizes these stubs.
2394 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002395 } else if (Subtarget->isPICStyleRIPRel() &&
2396 isa<Function>(GV) &&
2397 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2398 // If the function is marked as non-lazy, generate an indirect call
2399 // which loads from the GOT directly. This avoids runtime overhead
2400 // at the cost of eager binding (and one extra byte of encoding).
2401 OpFlags = X86II::MO_GOTPCREL;
2402 WrapperKind = X86ISD::WrapperRIP;
2403 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002404 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002405
Devang Patel0d881da2010-07-06 22:08:15 +00002406 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002407 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002408
2409 // Add a wrapper if needed.
2410 if (WrapperKind != ISD::DELETED_NODE)
2411 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2412 // Add extra indirection if needed.
2413 if (ExtraLoad)
2414 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2415 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002416 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002417 }
Bill Wendling056292f2008-09-16 21:48:12 +00002418 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002419 unsigned char OpFlags = 0;
2420
Evan Cheng1bf891a2010-12-01 22:59:46 +00002421 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2422 // external symbols should go through the PLT.
2423 if (Subtarget->isTargetELF() &&
2424 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2425 OpFlags = X86II::MO_PLT;
2426 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002427 (!Subtarget->getTargetTriple().isMacOSX() ||
2428 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002429 // PC-relative references to external symbols should go through $stub,
2430 // unless we're building with the leopard linker or later, which
2431 // automatically synthesizes these stubs.
2432 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002433 }
Eric Christopherfd179292009-08-27 18:07:15 +00002434
Chris Lattner48a7d022009-07-09 05:02:21 +00002435 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2436 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002437 }
2438
Chris Lattnerd96d0722007-02-25 06:40:16 +00002439 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002440 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002441 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002442
Evan Chengf22f9b32010-02-06 03:28:46 +00002443 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002444 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2445 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002446 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002447 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002448
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002449 Ops.push_back(Chain);
2450 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002451
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002453 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002454
Gordon Henriksen86737662008-01-05 16:56:59 +00002455 // Add argument registers to the end of the list so that they are known live
2456 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002457 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2458 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2459 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Evan Cheng586ccac2008-03-18 23:36:35 +00002461 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002463 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2464
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002465 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002466 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002468
Gabor Greifba36cb52008-08-28 21:40:38 +00002469 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002470 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002471
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002473 // We used to do:
2474 //// If this is the first return lowered for this function, add the regs
2475 //// to the liveout set for the function.
2476 // This isn't right, although it's probably harmless on x86; liveouts
2477 // should be computed from returns not tail calls. Consider a void
2478 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479 return DAG.getNode(X86ISD::TC_RETURN, dl,
2480 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002481 }
2482
Dale Johannesenace16102009-02-03 19:33:06 +00002483 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002484 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002485
Chris Lattner2d297092006-05-23 18:50:38 +00002486 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002487 unsigned NumBytesForCalleeToPush;
Evan Chengef41ff62011-06-23 17:54:54 +00002488 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002489 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002490 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002491 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002492 // pops the hidden struct pointer, so we have to push it back.
2493 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002494 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002495 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002496 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002497
Gordon Henriksenae636f82008-01-03 16:47:34 +00002498 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002499 if (!IsSibcall) {
2500 Chain = DAG.getCALLSEQ_END(Chain,
2501 DAG.getIntPtrConstant(NumBytes, true),
2502 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2503 true),
2504 InFlag);
2505 InFlag = Chain.getValue(1);
2506 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002507
Chris Lattner3085e152007-02-25 08:59:22 +00002508 // Handle result values, copying them out of physregs into vregs that we
2509 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002510 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2511 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002512}
2513
Evan Cheng25ab6902006-09-08 06:48:29 +00002514
2515//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002516// Fast Calling Convention (tail call) implementation
2517//===----------------------------------------------------------------------===//
2518
2519// Like std call, callee cleans arguments, convention except that ECX is
2520// reserved for storing the tail called function address. Only 2 registers are
2521// free for argument passing (inreg). Tail call optimization is performed
2522// provided:
2523// * tailcallopt is enabled
2524// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002525// On X86_64 architecture with GOT-style position independent code only local
2526// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002527// To keep the stack aligned according to platform abi the function
2528// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2529// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002530// If a tail called function callee has more arguments than the caller the
2531// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002532// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002533// original REtADDR, but before the saved framepointer or the spilled registers
2534// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2535// stack layout:
2536// arg1
2537// arg2
2538// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002539// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002540// move area ]
2541// (possible EBP)
2542// ESI
2543// EDI
2544// local1 ..
2545
2546/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2547/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002548unsigned
2549X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2550 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002551 MachineFunction &MF = DAG.getMachineFunction();
2552 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002553 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002554 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002555 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002556 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002557 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002558 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2559 // Number smaller than 12 so just add the difference.
2560 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2561 } else {
2562 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002563 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002564 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002565 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002566 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002567}
2568
Evan Cheng5f941932010-02-05 02:21:12 +00002569/// MatchingStackOffset - Return true if the given stack call argument is
2570/// already available in the same position (relatively) of the caller's
2571/// incoming argument stack.
2572static
2573bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2574 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2575 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002576 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2577 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002578 if (Arg.getOpcode() == ISD::CopyFromReg) {
2579 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002580 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002581 return false;
2582 MachineInstr *Def = MRI->getVRegDef(VR);
2583 if (!Def)
2584 return false;
2585 if (!Flags.isByVal()) {
2586 if (!TII->isLoadFromStackSlot(Def, FI))
2587 return false;
2588 } else {
2589 unsigned Opcode = Def->getOpcode();
2590 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2591 Def->getOperand(1).isFI()) {
2592 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002593 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002594 } else
2595 return false;
2596 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002597 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2598 if (Flags.isByVal())
2599 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002600 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002601 // define @foo(%struct.X* %A) {
2602 // tail call @bar(%struct.X* byval %A)
2603 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002604 return false;
2605 SDValue Ptr = Ld->getBasePtr();
2606 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2607 if (!FINode)
2608 return false;
2609 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002610 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002611 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002612 FI = FINode->getIndex();
2613 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002614 } else
2615 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002616
Evan Cheng4cae1332010-03-05 08:38:04 +00002617 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002618 if (!MFI->isFixedObjectIndex(FI))
2619 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002620 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002621}
2622
Dan Gohman98ca4f22009-08-05 01:29:28 +00002623/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2624/// for tail call optimization. Targets which want to do tail call
2625/// optimization should implement this function.
2626bool
2627X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002628 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002629 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002630 bool isCalleeStructRet,
2631 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002632 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002633 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002634 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002636 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002637 CalleeCC != CallingConv::C)
2638 return false;
2639
Evan Cheng7096ae42010-01-29 06:45:59 +00002640 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002641 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002642 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002643 CallingConv::ID CallerCC = CallerF->getCallingConv();
2644 bool CCMatch = CallerCC == CalleeCC;
2645
Dan Gohman1797ed52010-02-08 20:27:50 +00002646 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002647 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002648 return true;
2649 return false;
2650 }
2651
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002652 // Look for obvious safe cases to perform tail call optimization that do not
2653 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002654
Evan Cheng2c12cb42010-03-26 16:26:03 +00002655 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2656 // emit a special epilogue.
2657 if (RegInfo->needsStackRealignment(MF))
2658 return false;
2659
Evan Chenga375d472010-03-15 18:54:48 +00002660 // Also avoid sibcall optimization if either caller or callee uses struct
2661 // return semantics.
2662 if (isCalleeStructRet || isCallerStructRet)
2663 return false;
2664
Chad Rosier2416da32011-06-24 21:15:36 +00002665 // An stdcall caller is expected to clean up its arguments; the callee
2666 // isn't going to do that.
2667 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2668 return false;
2669
Chad Rosier871f6642011-05-18 19:59:50 +00002670 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002671 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002672 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002673
2674 // Optimizing for varargs on Win64 is unlikely to be safe without
2675 // additional testing.
2676 if (Subtarget->isTargetWin64())
2677 return false;
2678
Chad Rosier871f6642011-05-18 19:59:50 +00002679 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002680 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2681 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002682
Chad Rosier871f6642011-05-18 19:59:50 +00002683 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2684 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2685 if (!ArgLocs[i].isRegLoc())
2686 return false;
2687 }
2688
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002689 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2690 // Therefore if it's not used by the call it is not safe to optimize this into
2691 // a sibcall.
2692 bool Unused = false;
2693 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2694 if (!Ins[i].Used) {
2695 Unused = true;
2696 break;
2697 }
2698 }
2699 if (Unused) {
2700 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002701 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
2702 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002703 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002704 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002705 CCValAssign &VA = RVLocs[i];
2706 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2707 return false;
2708 }
2709 }
2710
Evan Cheng13617962010-04-30 01:12:32 +00002711 // If the calling conventions do not match, then we'd better make sure the
2712 // results are returned in the same way as what the caller expects.
2713 if (!CCMatch) {
2714 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002715 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
2716 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002717 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2718
2719 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002720 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
2721 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002722 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2723
2724 if (RVLocs1.size() != RVLocs2.size())
2725 return false;
2726 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2727 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2728 return false;
2729 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2730 return false;
2731 if (RVLocs1[i].isRegLoc()) {
2732 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2733 return false;
2734 } else {
2735 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2736 return false;
2737 }
2738 }
2739 }
2740
Evan Chenga6bff982010-01-30 01:22:00 +00002741 // If the callee takes no arguments then go on to check the results of the
2742 // call.
2743 if (!Outs.empty()) {
2744 // Check if stack adjustment is needed. For now, do not do this if any
2745 // argument is passed on the stack.
2746 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002747 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2748 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002749
2750 // Allocate shadow area for Win64
2751 if (Subtarget->isTargetWin64()) {
2752 CCInfo.AllocateStack(32, 8);
2753 }
2754
Duncan Sands45907662010-10-31 13:21:44 +00002755 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002756 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002757 MachineFunction &MF = DAG.getMachineFunction();
2758 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2759 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002760
2761 // Check if the arguments are already laid out in the right way as
2762 // the caller's fixed stack objects.
2763 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002764 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2765 const X86InstrInfo *TII =
2766 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002767 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2768 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002769 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002770 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002771 if (VA.getLocInfo() == CCValAssign::Indirect)
2772 return false;
2773 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002774 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2775 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002776 return false;
2777 }
2778 }
2779 }
Evan Cheng9c044672010-05-29 01:35:22 +00002780
2781 // If the tailcall address may be in a register, then make sure it's
2782 // possible to register allocate for it. In 32-bit, the call address can
2783 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002784 // callee-saved registers are restored. These happen to be the same
2785 // registers used to pass 'inreg' arguments so watch out for those.
2786 if (!Subtarget->is64Bit() &&
2787 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002788 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002789 unsigned NumInRegs = 0;
2790 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2791 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002792 if (!VA.isRegLoc())
2793 continue;
2794 unsigned Reg = VA.getLocReg();
2795 switch (Reg) {
2796 default: break;
2797 case X86::EAX: case X86::EDX: case X86::ECX:
2798 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002799 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002800 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002801 }
2802 }
2803 }
Evan Chenga6bff982010-01-30 01:22:00 +00002804 }
Evan Chengb1712452010-01-27 06:25:16 +00002805
Evan Cheng86809cc2010-02-03 03:28:02 +00002806 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002807}
2808
Dan Gohman3df24e62008-09-03 23:12:08 +00002809FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002810X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2811 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002812}
2813
2814
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002815//===----------------------------------------------------------------------===//
2816// Other Lowering Hooks
2817//===----------------------------------------------------------------------===//
2818
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002819static bool MayFoldLoad(SDValue Op) {
2820 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2821}
2822
2823static bool MayFoldIntoStore(SDValue Op) {
2824 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2825}
2826
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002827static bool isTargetShuffle(unsigned Opcode) {
2828 switch(Opcode) {
2829 default: return false;
2830 case X86ISD::PSHUFD:
2831 case X86ISD::PSHUFHW:
2832 case X86ISD::PSHUFLW:
2833 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002834 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002835 case X86ISD::SHUFPS:
2836 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002837 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002838 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002839 case X86ISD::MOVLPS:
2840 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002841 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002842 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002843 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002844 case X86ISD::MOVSS:
2845 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002846 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002847 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002848 case X86ISD::VUNPCKLPSY:
2849 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002850 case X86ISD::PUNPCKLWD:
2851 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002852 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002853 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002854 case X86ISD::VPUNPCKLWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002855 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002856 case X86ISD::VPUNPCKLDQY:
2857 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002858 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002859 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002860 case X86ISD::VUNPCKHPSY:
2861 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002862 case X86ISD::PUNPCKHWD:
2863 case X86ISD::PUNPCKHBW:
2864 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002865 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002866 case X86ISD::VPUNPCKHWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002867 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002868 case X86ISD::VPUNPCKHDQY:
2869 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002870 case X86ISD::VPERMILPS:
2871 case X86ISD::VPERMILPSY:
2872 case X86ISD::VPERMILPD:
2873 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002874 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002875 return true;
2876 }
2877 return false;
2878}
2879
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002880static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002881 SDValue V1, SelectionDAG &DAG) {
2882 switch(Opc) {
2883 default: llvm_unreachable("Unknown x86 shuffle node");
2884 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002885 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002886 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002887 return DAG.getNode(Opc, dl, VT, V1);
2888 }
2889
2890 return SDValue();
2891}
2892
2893static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002894 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002895 switch(Opc) {
2896 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002897 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002898 case X86ISD::PSHUFHW:
2899 case X86ISD::PSHUFLW:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00002900 case X86ISD::VPERMILPS:
2901 case X86ISD::VPERMILPSY:
2902 case X86ISD::VPERMILPD:
2903 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002904 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2905 }
2906
2907 return SDValue();
2908}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002909
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002910static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2911 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2912 switch(Opc) {
2913 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002914 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002915 case X86ISD::SHUFPD:
2916 case X86ISD::SHUFPS:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00002917 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002918 return DAG.getNode(Opc, dl, VT, V1, V2,
2919 DAG.getConstant(TargetMask, MVT::i8));
2920 }
2921 return SDValue();
2922}
2923
2924static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2925 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2926 switch(Opc) {
2927 default: llvm_unreachable("Unknown x86 shuffle node");
2928 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002929 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002930 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002931 case X86ISD::MOVLPS:
2932 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002933 case X86ISD::MOVSS:
2934 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002935 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002936 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002937 case X86ISD::VUNPCKLPSY:
2938 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002939 case X86ISD::PUNPCKLWD:
2940 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002941 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002942 case X86ISD::PUNPCKLQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002943 case X86ISD::VPUNPCKLWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002944 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002945 case X86ISD::VPUNPCKLDQY:
2946 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002947 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002948 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00002949 case X86ISD::VUNPCKHPSY:
2950 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002951 case X86ISD::PUNPCKHWD:
2952 case X86ISD::PUNPCKHBW:
2953 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002954 case X86ISD::PUNPCKHQDQ:
Craig Topper6347e862011-11-21 06:57:39 +00002955 case X86ISD::VPUNPCKHWDY:
Craig Topper6fa583d2011-11-21 08:26:50 +00002956 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +00002957 case X86ISD::VPUNPCKHDQY:
2958 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002959 return DAG.getNode(Opc, dl, VT, V1, V2);
2960 }
2961 return SDValue();
2962}
2963
Dan Gohmand858e902010-04-17 15:26:15 +00002964SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002965 MachineFunction &MF = DAG.getMachineFunction();
2966 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2967 int ReturnAddrIndex = FuncInfo->getRAIndex();
2968
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002969 if (ReturnAddrIndex == 0) {
2970 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002971 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002972 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002973 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002974 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002975 }
2976
Evan Cheng25ab6902006-09-08 06:48:29 +00002977 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002978}
2979
2980
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002981bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2982 bool hasSymbolicDisplacement) {
2983 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002984 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002985 return false;
2986
2987 // If we don't have a symbolic displacement - we don't have any extra
2988 // restrictions.
2989 if (!hasSymbolicDisplacement)
2990 return true;
2991
2992 // FIXME: Some tweaks might be needed for medium code model.
2993 if (M != CodeModel::Small && M != CodeModel::Kernel)
2994 return false;
2995
2996 // For small code model we assume that latest object is 16MB before end of 31
2997 // bits boundary. We may also accept pretty large negative constants knowing
2998 // that all objects are in the positive half of address space.
2999 if (M == CodeModel::Small && Offset < 16*1024*1024)
3000 return true;
3001
3002 // For kernel code model we know that all object resist in the negative half
3003 // of 32bits address space. We may not accept negative offsets, since they may
3004 // be just off and we may accept pretty large positive ones.
3005 if (M == CodeModel::Kernel && Offset > 0)
3006 return true;
3007
3008 return false;
3009}
3010
Evan Chengef41ff62011-06-23 17:54:54 +00003011/// isCalleePop - Determines whether the callee is required to pop its
3012/// own arguments. Callee pop is necessary to support tail calls.
3013bool X86::isCalleePop(CallingConv::ID CallingConv,
3014 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3015 if (IsVarArg)
3016 return false;
3017
3018 switch (CallingConv) {
3019 default:
3020 return false;
3021 case CallingConv::X86_StdCall:
3022 return !is64Bit;
3023 case CallingConv::X86_FastCall:
3024 return !is64Bit;
3025 case CallingConv::X86_ThisCall:
3026 return !is64Bit;
3027 case CallingConv::Fast:
3028 return TailCallOpt;
3029 case CallingConv::GHC:
3030 return TailCallOpt;
3031 }
3032}
3033
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003034/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3035/// specific condition code, returning the condition code and the LHS/RHS of the
3036/// comparison to make.
3037static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3038 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003039 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003040 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3041 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3042 // X > -1 -> X == 0, jump !sign.
3043 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003044 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003045 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
3046 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003047 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00003048 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003049 // X < 1 -> X <= 0
3050 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003051 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003052 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003053 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003054
Evan Chengd9558e02006-01-06 00:43:03 +00003055 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003056 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003057 case ISD::SETEQ: return X86::COND_E;
3058 case ISD::SETGT: return X86::COND_G;
3059 case ISD::SETGE: return X86::COND_GE;
3060 case ISD::SETLT: return X86::COND_L;
3061 case ISD::SETLE: return X86::COND_LE;
3062 case ISD::SETNE: return X86::COND_NE;
3063 case ISD::SETULT: return X86::COND_B;
3064 case ISD::SETUGT: return X86::COND_A;
3065 case ISD::SETULE: return X86::COND_BE;
3066 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003067 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003068 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003069
Chris Lattner4c78e022008-12-23 23:42:27 +00003070 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003071
Chris Lattner4c78e022008-12-23 23:42:27 +00003072 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003073 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3074 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003075 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3076 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003077 }
3078
Chris Lattner4c78e022008-12-23 23:42:27 +00003079 switch (SetCCOpcode) {
3080 default: break;
3081 case ISD::SETOLT:
3082 case ISD::SETOLE:
3083 case ISD::SETUGT:
3084 case ISD::SETUGE:
3085 std::swap(LHS, RHS);
3086 break;
3087 }
3088
3089 // On a floating point condition, the flags are set as follows:
3090 // ZF PF CF op
3091 // 0 | 0 | 0 | X > Y
3092 // 0 | 0 | 1 | X < Y
3093 // 1 | 0 | 0 | X == Y
3094 // 1 | 1 | 1 | unordered
3095 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003096 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003097 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003098 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003099 case ISD::SETOLT: // flipped
3100 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003101 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003102 case ISD::SETOLE: // flipped
3103 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003104 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003105 case ISD::SETUGT: // flipped
3106 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003107 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003108 case ISD::SETUGE: // flipped
3109 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003110 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003111 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003112 case ISD::SETNE: return X86::COND_NE;
3113 case ISD::SETUO: return X86::COND_P;
3114 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003115 case ISD::SETOEQ:
3116 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003117 }
Evan Chengd9558e02006-01-06 00:43:03 +00003118}
3119
Evan Cheng4a460802006-01-11 00:33:36 +00003120/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3121/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003122/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003123static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003124 switch (X86CC) {
3125 default:
3126 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003127 case X86::COND_B:
3128 case X86::COND_BE:
3129 case X86::COND_E:
3130 case X86::COND_P:
3131 case X86::COND_A:
3132 case X86::COND_AE:
3133 case X86::COND_NE:
3134 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003135 return true;
3136 }
3137}
3138
Evan Chengeb2f9692009-10-27 19:56:55 +00003139/// isFPImmLegal - Returns true if the target can instruction select the
3140/// specified FP immediate natively. If false, the legalizer will
3141/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003142bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003143 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3144 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3145 return true;
3146 }
3147 return false;
3148}
3149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3151/// the specified range (L, H].
3152static bool isUndefOrInRange(int Val, int Low, int Hi) {
3153 return (Val < 0) || (Val >= Low && Val < Hi);
3154}
3155
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00003156/// isUndefOrInRange - Return true if every element in Mask, begining
3157/// from position Pos and ending in Pos+Size, falls within the specified
3158/// range (L, L+Pos]. or is undef.
3159static bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
3160 int Pos, int Size, int Low, int Hi) {
3161 for (int i = Pos, e = Pos+Size; i != e; ++i)
3162 if (!isUndefOrInRange(Mask[i], Low, Hi))
3163 return false;
3164 return true;
3165}
3166
Nate Begeman9008ca62009-04-27 18:41:29 +00003167/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3168/// specified value.
3169static bool isUndefOrEqual(int Val, int CmpVal) {
3170 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003171 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003173}
3174
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003175/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
3176/// from position Pos and ending in Pos+Size, falls within the specified
3177/// sequential range (L, L+Pos]. or is undef.
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003178static bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
3179 int Pos, int Size, int Low) {
3180 for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
3181 if (!isUndefOrEqual(Mask[i], Low))
3182 return false;
3183 return true;
3184}
3185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3187/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3188/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003189static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003190 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003191 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 return (Mask[0] < 2 && Mask[1] < 2);
3194 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003195}
3196
Nate Begeman9008ca62009-04-27 18:41:29 +00003197bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003198 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 N->getMask(M);
3200 return ::isPSHUFDMask(M, N->getValueType(0));
3201}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3204/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003205static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003207 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003208
Nate Begeman9008ca62009-04-27 18:41:29 +00003209 // Lower quadword copied in order or undef.
3210 for (int i = 0; i != 4; ++i)
3211 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003212 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003213
Evan Cheng506d3df2006-03-29 23:07:14 +00003214 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 for (int i = 4; i != 8; ++i)
3216 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003217 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003218
Evan Cheng506d3df2006-03-29 23:07:14 +00003219 return true;
3220}
3221
Nate Begeman9008ca62009-04-27 18:41:29 +00003222bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003223 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 N->getMask(M);
3225 return ::isPSHUFHWMask(M, N->getValueType(0));
3226}
Evan Cheng506d3df2006-03-29 23:07:14 +00003227
Nate Begeman9008ca62009-04-27 18:41:29 +00003228/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3229/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003230static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003232 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003233
Rafael Espindola15684b22009-04-24 12:40:33 +00003234 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 for (int i = 4; i != 8; ++i)
3236 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003237 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003238
Rafael Espindola15684b22009-04-24 12:40:33 +00003239 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003240 for (int i = 0; i != 4; ++i)
3241 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003243
Rafael Espindola15684b22009-04-24 12:40:33 +00003244 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003245}
3246
Nate Begeman9008ca62009-04-27 18:41:29 +00003247bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003248 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 N->getMask(M);
3250 return ::isPSHUFLWMask(M, N->getValueType(0));
3251}
3252
Nate Begemana09008b2009-10-19 02:17:23 +00003253/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3254/// is suitable for input to PALIGNR.
3255static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003256 bool hasSSSE3OrAVX) {
Nate Begemana09008b2009-10-19 02:17:23 +00003257 int i, e = VT.getVectorNumElements();
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003258 if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
3259 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003260
Nate Begemana09008b2009-10-19 02:17:23 +00003261 // Do not handle v2i64 / v2f64 shuffles with palignr.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00003262 if (e < 4 || !hasSSSE3OrAVX)
Nate Begemana09008b2009-10-19 02:17:23 +00003263 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003264
Nate Begemana09008b2009-10-19 02:17:23 +00003265 for (i = 0; i != e; ++i)
3266 if (Mask[i] >= 0)
3267 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003268
Nate Begemana09008b2009-10-19 02:17:23 +00003269 // All undef, not a palignr.
3270 if (i == e)
3271 return false;
3272
Eli Friedman63f8dde2011-07-25 21:36:45 +00003273 // Make sure we're shifting in the right direction.
3274 if (Mask[i] <= i)
3275 return false;
Nate Begemana09008b2009-10-19 02:17:23 +00003276
3277 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003278
Nate Begemana09008b2009-10-19 02:17:23 +00003279 // Check the rest of the elements to see if they are consecutive.
3280 for (++i; i != e; ++i) {
3281 int m = Mask[i];
Eli Friedman63f8dde2011-07-25 21:36:45 +00003282 if (m >= 0 && m != s+i)
Nate Begemana09008b2009-10-19 02:17:23 +00003283 return false;
3284 }
3285 return true;
3286}
3287
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003288/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
3289/// specifies a shuffle of elements that is suitable for input to 256-bit
3290/// VSHUFPSY.
3291static bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3292 const X86Subtarget *Subtarget) {
3293 int NumElems = VT.getVectorNumElements();
3294
3295 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3296 return false;
3297
3298 if (NumElems != 8)
3299 return false;
3300
3301 // VSHUFPSY divides the resulting vector into 4 chunks.
3302 // The sources are also splitted into 4 chunks, and each destination
3303 // chunk must come from a different source chunk.
3304 //
3305 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3306 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3307 //
3308 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3309 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3310 //
3311 int QuarterSize = NumElems/4;
3312 int HalfSize = QuarterSize*2;
3313 for (int i = 0; i < QuarterSize; ++i)
3314 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3315 return false;
3316 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3317 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3318 return false;
3319
3320 // The mask of the second half must be the same as the first but with
3321 // the appropriate offsets. This works in the same way as VPERMILPS
3322 // works with masks.
3323 for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
3324 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3325 return false;
3326 int FstHalfIdx = i-HalfSize;
3327 if (Mask[FstHalfIdx] < 0)
3328 continue;
3329 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3330 return false;
3331 }
3332 for (int i = QuarterSize*3; i < NumElems; ++i) {
3333 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3334 return false;
3335 int FstHalfIdx = i-HalfSize;
3336 if (Mask[FstHalfIdx] < 0)
3337 continue;
3338 if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
3339 return false;
3340
3341 }
3342
3343 return true;
3344}
3345
3346/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
3347/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
3348static unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
3349 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3350 EVT VT = SVOp->getValueType(0);
3351 int NumElems = VT.getVectorNumElements();
3352
3353 assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
3354 "Only supports v8i32 and v8f32 types");
3355
3356 int HalfSize = NumElems/2;
3357 unsigned Mask = 0;
3358 for (int i = 0; i != NumElems ; ++i) {
3359 if (SVOp->getMaskElt(i) < 0)
3360 continue;
3361 // The mask of the first half must be equal to the second one.
3362 unsigned Shamt = (i%HalfSize)*2;
3363 unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
3364 Mask |= Elt << Shamt;
3365 }
3366
3367 return Mask;
3368}
3369
3370/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
3371/// specifies a shuffle of elements that is suitable for input to 256-bit
3372/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
3373/// version and the mask of the second half isn't binded with the first
3374/// one.
3375static bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
3376 const X86Subtarget *Subtarget) {
3377 int NumElems = VT.getVectorNumElements();
3378
3379 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3380 return false;
3381
3382 if (NumElems != 4)
3383 return false;
3384
3385 // VSHUFPSY divides the resulting vector into 4 chunks.
3386 // The sources are also splitted into 4 chunks, and each destination
3387 // chunk must come from a different source chunk.
3388 //
3389 // SRC1 => X3 X2 X1 X0
3390 // SRC2 => Y3 Y2 Y1 Y0
3391 //
3392 // DST => Y2..Y3, X2..X3, Y1..Y0, X1..X0
3393 //
3394 int QuarterSize = NumElems/4;
3395 int HalfSize = QuarterSize*2;
3396 for (int i = 0; i < QuarterSize; ++i)
3397 if (!isUndefOrInRange(Mask[i], 0, HalfSize))
3398 return false;
3399 for (int i = QuarterSize; i < QuarterSize*2; ++i)
3400 if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
3401 return false;
3402 for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
3403 if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
3404 return false;
3405 for (int i = QuarterSize*3; i < NumElems; ++i)
3406 if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
3407 return false;
3408
3409 return true;
3410}
3411
3412/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
3413/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
3414static unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
3415 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3416 EVT VT = SVOp->getValueType(0);
3417 int NumElems = VT.getVectorNumElements();
3418
3419 assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
3420 "Only supports v4i64 and v4f64 types");
3421
3422 int HalfSize = NumElems/2;
3423 unsigned Mask = 0;
3424 for (int i = 0; i != NumElems ; ++i) {
3425 if (SVOp->getMaskElt(i) < 0)
3426 continue;
3427 int Elt = SVOp->getMaskElt(i) % HalfSize;
3428 Mask |= Elt << i;
3429 }
3430
3431 return Mask;
3432}
3433
Evan Cheng14aed5e2006-03-24 01:18:28 +00003434/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003435/// specifies a shuffle of elements that is suitable for input to 128-bit
3436/// SHUFPS and SHUFPD.
Owen Andersone50ed302009-08-10 22:56:29 +00003437static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesaf002d82011-08-24 23:17:55 +00003439
3440 if (VT.getSizeInBits() != 128)
3441 return false;
3442
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 if (NumElems != 2 && NumElems != 4)
3444 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003445
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 int Half = NumElems / 2;
3447 for (int i = 0; i < Half; ++i)
3448 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003449 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 for (int i = Half; i < NumElems; ++i)
3451 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003452 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003453
Evan Cheng14aed5e2006-03-24 01:18:28 +00003454 return true;
3455}
3456
Nate Begeman9008ca62009-04-27 18:41:29 +00003457bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3458 SmallVector<int, 8> M;
3459 N->getMask(M);
3460 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003461}
3462
Evan Cheng213d2cf2007-05-17 18:45:50 +00003463/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003464/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3465/// half elements to come from vector 1 (which would equal the dest.) and
3466/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003467static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003468 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003469
3470 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003472
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 int Half = NumElems / 2;
3474 for (int i = 0; i < Half; ++i)
3475 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003476 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 for (int i = Half; i < NumElems; ++i)
3478 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003479 return false;
3480 return true;
3481}
3482
Nate Begeman9008ca62009-04-27 18:41:29 +00003483static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3484 SmallVector<int, 8> M;
3485 N->getMask(M);
3486 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003487}
3488
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003489/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3490/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003491bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003492 EVT VT = N->getValueType(0);
3493 unsigned NumElems = VT.getVectorNumElements();
3494
3495 if (VT.getSizeInBits() != 128)
3496 return false;
3497
3498 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003499 return false;
3500
Evan Cheng2064a2b2006-03-28 06:50:32 +00003501 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3503 isUndefOrEqual(N->getMaskElt(1), 7) &&
3504 isUndefOrEqual(N->getMaskElt(2), 2) &&
3505 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003506}
3507
Nate Begeman0b10b912009-11-07 23:17:15 +00003508/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3509/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3510/// <2, 3, 2, 3>
3511bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003512 EVT VT = N->getValueType(0);
3513 unsigned NumElems = VT.getVectorNumElements();
3514
3515 if (VT.getSizeInBits() != 128)
3516 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003517
Nate Begeman0b10b912009-11-07 23:17:15 +00003518 if (NumElems != 4)
3519 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003520
Nate Begeman0b10b912009-11-07 23:17:15 +00003521 return isUndefOrEqual(N->getMaskElt(0), 2) &&
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003522 isUndefOrEqual(N->getMaskElt(1), 3) &&
3523 isUndefOrEqual(N->getMaskElt(2), 2) &&
3524 isUndefOrEqual(N->getMaskElt(3), 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003525}
3526
Evan Cheng5ced1d82006-04-06 23:23:56 +00003527/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3528/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003529bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3530 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003531
Evan Cheng5ced1d82006-04-06 23:23:56 +00003532 if (NumElems != 2 && NumElems != 4)
3533 return false;
3534
Evan Chengc5cdff22006-04-07 21:53:05 +00003535 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003536 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003537 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003538
Evan Chengc5cdff22006-04-07 21:53:05 +00003539 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003540 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003541 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003542
3543 return true;
3544}
3545
Nate Begeman0b10b912009-11-07 23:17:15 +00003546/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3547/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3548bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003550
David Greenea20244d2011-03-02 17:23:43 +00003551 if ((NumElems != 2 && NumElems != 4)
3552 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003553 return false;
3554
Evan Chengc5cdff22006-04-07 21:53:05 +00003555 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003556 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003557 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003558
Nate Begeman9008ca62009-04-27 18:41:29 +00003559 for (unsigned i = 0; i < NumElems/2; ++i)
3560 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003561 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003562
3563 return true;
3564}
3565
Evan Cheng0038e592006-03-28 00:39:58 +00003566/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3567/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003568static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003569 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003571
3572 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3573 "Unsupported vector type for unpckh");
3574
Craig Topper6347e862011-11-21 06:57:39 +00003575 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003576 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003577 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003578
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003579 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3580 // independently on 128-bit lanes.
3581 unsigned NumLanes = VT.getSizeInBits()/128;
3582 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003583
3584 unsigned Start = 0;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003585 unsigned End = NumLaneElts;
3586 for (unsigned s = 0; s < NumLanes; ++s) {
3587 for (unsigned i = Start, j = s * NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003588 i != End;
3589 i += 2, ++j) {
3590 int BitI = Mask[i];
3591 int BitI1 = Mask[i+1];
3592 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003593 return false;
David Greenea20244d2011-03-02 17:23:43 +00003594 if (V2IsSplat) {
3595 if (!isUndefOrEqual(BitI1, NumElts))
3596 return false;
3597 } else {
3598 if (!isUndefOrEqual(BitI1, j + NumElts))
3599 return false;
3600 }
Evan Cheng39623da2006-04-20 08:58:49 +00003601 }
David Greenea20244d2011-03-02 17:23:43 +00003602 // Process the next 128 bits.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603 Start += NumLaneElts;
3604 End += NumLaneElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003605 }
David Greenea20244d2011-03-02 17:23:43 +00003606
Evan Cheng0038e592006-03-28 00:39:58 +00003607 return true;
3608}
3609
Craig Topper6347e862011-11-21 06:57:39 +00003610bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 SmallVector<int, 8> M;
3612 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003613 return ::isUNPCKLMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003614}
3615
Evan Cheng4fcb9222006-03-28 02:43:26 +00003616/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3617/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003618static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003619 bool HasAVX2, bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 int NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003621
3622 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3623 "Unsupported vector type for unpckh");
3624
Craig Topper6347e862011-11-21 06:57:39 +00003625 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003626 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003627 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003628
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003629 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3630 // independently on 128-bit lanes.
3631 unsigned NumLanes = VT.getSizeInBits()/128;
3632 unsigned NumLaneElts = NumElts/NumLanes;
3633
3634 unsigned Start = 0;
3635 unsigned End = NumLaneElts;
3636 for (unsigned l = 0; l != NumLanes; ++l) {
3637 for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
3638 i != End; i += 2, ++j) {
3639 int BitI = Mask[i];
3640 int BitI1 = Mask[i+1];
3641 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003642 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003643 if (V2IsSplat) {
3644 if (isUndefOrEqual(BitI1, NumElts))
3645 return false;
3646 } else {
3647 if (!isUndefOrEqual(BitI1, j+NumElts))
3648 return false;
3649 }
Evan Cheng39623da2006-04-20 08:58:49 +00003650 }
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003651 // Process the next 128 bits.
3652 Start += NumLaneElts;
3653 End += NumLaneElts;
Evan Cheng4fcb9222006-03-28 02:43:26 +00003654 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003655 return true;
3656}
3657
Craig Topper6347e862011-11-21 06:57:39 +00003658bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool HasAVX2, bool V2IsSplat) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 SmallVector<int, 8> M;
3660 N->getMask(M);
Craig Topper6347e862011-11-21 06:57:39 +00003661 return ::isUNPCKHMask(M, N->getValueType(0), HasAVX2, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003662}
3663
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003664/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3665/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3666/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003667static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003669 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003670 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003671
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003672 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3673 // FIXME: Need a better way to get rid of this, there's no latency difference
3674 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3675 // the former later. We should also remove the "_undef" special mask.
3676 if (NumElems == 4 && VT.getSizeInBits() == 256)
3677 return false;
3678
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003679 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3680 // independently on 128-bit lanes.
3681 unsigned NumLanes = VT.getSizeInBits() / 128;
3682 unsigned NumLaneElts = NumElems / NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003683
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003684 for (unsigned s = 0; s < NumLanes; ++s) {
3685 for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
3686 i != NumLaneElts * (s + 1);
David Greenea20244d2011-03-02 17:23:43 +00003687 i += 2, ++j) {
3688 int BitI = Mask[i];
3689 int BitI1 = Mask[i+1];
3690
3691 if (!isUndefOrEqual(BitI, j))
3692 return false;
3693 if (!isUndefOrEqual(BitI1, j))
3694 return false;
3695 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003696 }
David Greenea20244d2011-03-02 17:23:43 +00003697
Rafael Espindola15684b22009-04-24 12:40:33 +00003698 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003699}
3700
Nate Begeman9008ca62009-04-27 18:41:29 +00003701bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3702 SmallVector<int, 8> M;
3703 N->getMask(M);
3704 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3705}
3706
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003707/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3708/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3709/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003710static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003711 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003712 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3713 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003714
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3716 int BitI = Mask[i];
3717 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003718 if (!isUndefOrEqual(BitI, j))
3719 return false;
3720 if (!isUndefOrEqual(BitI1, j))
3721 return false;
3722 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003723 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003724}
3725
Nate Begeman9008ca62009-04-27 18:41:29 +00003726bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3727 SmallVector<int, 8> M;
3728 N->getMask(M);
3729 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3730}
3731
Evan Cheng017dcc62006-04-21 01:05:10 +00003732/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3733/// specifies a shuffle of elements that is suitable for input to MOVSS,
3734/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003735static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003736 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003737 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003738
3739 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003740
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003742 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003743
Nate Begeman9008ca62009-04-27 18:41:29 +00003744 for (int i = 1; i < NumElts; ++i)
3745 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003746 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003747
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003748 return true;
3749}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003750
Nate Begeman9008ca62009-04-27 18:41:29 +00003751bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3752 SmallVector<int, 8> M;
3753 N->getMask(M);
3754 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003755}
3756
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
3758/// as permutations between 128-bit chunks or halves. As an example: this
3759/// shuffle bellow:
3760/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3761/// The first half comes from the second half of V1 and the second half from the
3762/// the second half of V2.
3763static bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
3764 const X86Subtarget *Subtarget) {
3765 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3766 return false;
3767
3768 // The shuffle result is divided into half A and half B. In total the two
3769 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3770 // B must come from C, D, E or F.
3771 int HalfSize = VT.getVectorNumElements()/2;
3772 bool MatchA = false, MatchB = false;
3773
3774 // Check if A comes from one of C, D, E, F.
3775 for (int Half = 0; Half < 4; ++Half) {
3776 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3777 MatchA = true;
3778 break;
3779 }
3780 }
3781
3782 // Check if B comes from one of C, D, E, F.
3783 for (int Half = 0; Half < 4; ++Half) {
3784 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3785 MatchB = true;
3786 break;
3787 }
3788 }
3789
3790 return MatchA && MatchB;
3791}
3792
3793/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
3794/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
3795static unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
3796 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3797 EVT VT = SVOp->getValueType(0);
3798
3799 int HalfSize = VT.getVectorNumElements()/2;
3800
3801 int FstHalf = 0, SndHalf = 0;
3802 for (int i = 0; i < HalfSize; ++i) {
3803 if (SVOp->getMaskElt(i) > 0) {
3804 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3805 break;
3806 }
3807 }
3808 for (int i = HalfSize; i < HalfSize*2; ++i) {
3809 if (SVOp->getMaskElt(i) > 0) {
3810 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3811 break;
3812 }
3813 }
3814
3815 return (FstHalf | (SndHalf << 4));
3816}
3817
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003818/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
3819/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3820/// Note that VPERMIL mask matching is different depending whether theunderlying
3821/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3822/// to the same elements of the low, but to the higher half of the source.
3823/// In VPERMILPD the two lanes could be shuffled independently of each other
3824/// with the same restriction that lanes can't be crossed.
3825static bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
3826 const X86Subtarget *Subtarget) {
3827 int NumElts = VT.getVectorNumElements();
3828 int NumLanes = VT.getSizeInBits()/128;
3829
3830 if (!Subtarget->hasAVX())
3831 return false;
3832
Eli Friedmandca62d52011-10-10 22:28:47 +00003833 // Only match 256-bit with 64-bit types
3834 if (VT.getSizeInBits() != 256 || NumElts != 4)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003835 return false;
3836
3837 // The mask on the high lane is independent of the low. Both can match
3838 // any element in inside its own lane, but can't cross.
3839 int LaneSize = NumElts/NumLanes;
3840 for (int l = 0; l < NumLanes; ++l)
3841 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3842 int LaneStart = l*LaneSize;
3843 if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
3844 return false;
3845 }
3846
3847 return true;
3848}
3849
3850/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
3851/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
3852/// Note that VPERMIL mask matching is different depending whether theunderlying
3853/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3854/// to the same elements of the low, but to the higher half of the source.
3855/// In VPERMILPD the two lanes could be shuffled independently of each other
3856/// with the same restriction that lanes can't be crossed.
3857static bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
3858 const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003859 unsigned NumElts = VT.getVectorNumElements();
3860 unsigned NumLanes = VT.getSizeInBits()/128;
3861
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003862 if (!Subtarget->hasAVX())
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003863 return false;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003864
Eli Friedmandca62d52011-10-10 22:28:47 +00003865 // Only match 256-bit with 32-bit types
3866 if (VT.getSizeInBits() != 256 || NumElts != 8)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003867 return false;
3868
3869 // The mask on the high lane should be the same as the low. Actually,
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003870 // they can differ if any of the corresponding index in a lane is undef
3871 // and the other stays in range.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003872 int LaneSize = NumElts/NumLanes;
3873 for (int i = 0; i < LaneSize; ++i) {
3874 int HighElt = i+LaneSize;
Bruno Cardoso Lopes155a92a2011-08-10 01:54:17 +00003875 bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
3876 bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
3877
3878 if (!HighValid || !LowValid)
3879 return false;
3880 if (Mask[i] < 0 || Mask[HighElt] < 0)
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003881 continue;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003882 if (Mask[HighElt]-Mask[i] != LaneSize)
3883 return false;
3884 }
3885
3886 return true;
3887}
3888
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003889/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
3890/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
3891static unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003892 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3893 EVT VT = SVOp->getValueType(0);
3894
3895 int NumElts = VT.getVectorNumElements();
3896 int NumLanes = VT.getSizeInBits()/128;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003897 int LaneSize = NumElts/NumLanes;
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003898
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003899 // Although the mask is equal for both lanes do it twice to get the cases
3900 // where a mask will match because the same mask element is undef on the
3901 // first half but valid on the second. This would get pathological cases
3902 // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003903 unsigned Mask = 0;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003904 for (int l = 0; l < NumLanes; ++l) {
3905 for (int i = 0; i < LaneSize; ++i) {
3906 int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
3907 if (MaskElt < 0)
3908 continue;
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003909 if (MaskElt >= LaneSize)
3910 MaskElt -= LaneSize;
Bruno Cardoso Lopesdd635302011-07-29 01:31:15 +00003911 Mask |= MaskElt << (i*2);
3912 }
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003913 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003914
3915 return Mask;
3916}
3917
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003918/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
3919/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
3920static unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
3921 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3922 EVT VT = SVOp->getValueType(0);
3923
3924 int NumElts = VT.getVectorNumElements();
3925 int NumLanes = VT.getSizeInBits()/128;
3926
3927 unsigned Mask = 0;
3928 int LaneSize = NumElts/NumLanes;
3929 for (int l = 0; l < NumLanes; ++l)
Bruno Cardoso Lopes377baa52011-07-29 01:31:04 +00003930 for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
3931 int MaskElt = SVOp->getMaskElt(i);
3932 if (MaskElt < 0)
3933 continue;
3934 Mask |= (MaskElt-l*LaneSize) << i;
3935 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003936
3937 return Mask;
3938}
3939
Evan Cheng017dcc62006-04-21 01:05:10 +00003940/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3941/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003942/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003943static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 bool V2IsSplat = false, bool V2IsUndef = false) {
3945 int 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
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 for (int i = 1; i < NumOps; ++i)
3953 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
Nate Begeman9008ca62009-04-27 18:41:29 +00003961static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003962 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 SmallVector<int, 8> M;
3964 N->getMask(M);
3965 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003966}
3967
Evan Chengd9539472006-04-14 21:59:03 +00003968/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3969/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003970/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
3971bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
3972 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00003973 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00003974 return false;
3975
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003976 // The second vector must be undef
3977 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3978 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003979
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003980 EVT VT = N->getValueType(0);
3981 unsigned NumElems = VT.getVectorNumElements();
3982
3983 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3984 (VT.getSizeInBits() == 256 && NumElems != 8))
3985 return false;
3986
3987 // "i+1" is the value the indexed mask element must have
3988 for (unsigned i = 0; i < NumElems; i += 2)
3989 if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
3990 !isUndefOrEqual(N->getMaskElt(i+1), i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003991 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003992
3993 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003994}
3995
3996/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3997/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003998/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
3999bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
4000 const X86Subtarget *Subtarget) {
Craig Topperc0d82852011-11-22 00:44:41 +00004001 if (!Subtarget->hasSSE3orAVX())
Evan Chengd9539472006-04-14 21:59:03 +00004002 return false;
4003
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004004 // The second vector must be undef
4005 if (N->getOperand(1).getOpcode() != ISD::UNDEF)
4006 return false;
4007
4008 EVT VT = N->getValueType(0);
4009 unsigned NumElems = VT.getVectorNumElements();
4010
4011 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
4012 (VT.getSizeInBits() == 256 && NumElems != 8))
4013 return false;
4014
4015 // "i" is the value the indexed mask element must have
4016 for (unsigned i = 0; i < NumElems; i += 2)
4017 if (!isUndefOrEqual(N->getMaskElt(i), i) ||
4018 !isUndefOrEqual(N->getMaskElt(i+1), i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004019 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004020
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004021 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004022}
4023
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004024/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4025/// specifies a shuffle of elements that is suitable for input to 256-bit
4026/// version of MOVDDUP.
4027static bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
4028 const X86Subtarget *Subtarget) {
4029 EVT VT = N->getValueType(0);
4030 int NumElts = VT.getVectorNumElements();
4031 bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
4032
4033 if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
4034 !V2IsUndef || NumElts != 4)
4035 return false;
4036
4037 for (int i = 0; i != NumElts/2; ++i)
4038 if (!isUndefOrEqual(N->getMaskElt(i), 0))
4039 return false;
4040 for (int i = NumElts/2; i != NumElts; ++i)
4041 if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
4042 return false;
4043 return true;
4044}
4045
Evan Cheng0b457f02008-09-25 20:50:48 +00004046/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004047/// specifies a shuffle of elements that is suitable for input to 128-bit
4048/// version of MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00004049bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004050 EVT VT = N->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004051
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004052 if (VT.getSizeInBits() != 128)
4053 return false;
4054
4055 int e = VT.getVectorNumElements() / 2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 for (int i = 0; i < e; ++i)
4057 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004058 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004059 for (int i = 0; i < e; ++i)
4060 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004061 return false;
4062 return true;
4063}
4064
David Greenec38a03e2011-02-03 15:50:00 +00004065/// isVEXTRACTF128Index - Return true if the specified
4066/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4067/// suitable for input to VEXTRACTF128.
4068bool X86::isVEXTRACTF128Index(SDNode *N) {
4069 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4070 return false;
4071
4072 // The index should be aligned on a 128-bit boundary.
4073 uint64_t Index =
4074 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4075
4076 unsigned VL = N->getValueType(0).getVectorNumElements();
4077 unsigned VBits = N->getValueType(0).getSizeInBits();
4078 unsigned ElSize = VBits / VL;
4079 bool Result = (Index * ElSize) % 128 == 0;
4080
4081 return Result;
4082}
4083
David Greeneccacdc12011-02-04 16:08:29 +00004084/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4085/// operand specifies a subvector insert that is suitable for input to
4086/// VINSERTF128.
4087bool X86::isVINSERTF128Index(SDNode *N) {
4088 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4089 return false;
4090
4091 // The index should be aligned on a 128-bit boundary.
4092 uint64_t Index =
4093 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4094
4095 unsigned VL = N->getValueType(0).getVectorNumElements();
4096 unsigned VBits = N->getValueType(0).getSizeInBits();
4097 unsigned ElSize = VBits / VL;
4098 bool Result = (Index * ElSize) % 128 == 0;
4099
4100 return Result;
4101}
4102
Evan Cheng63d33002006-03-22 08:01:21 +00004103/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004104/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00004105unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4107 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4108
Evan Chengb9df0ca2006-03-22 02:53:00 +00004109 unsigned Shift = (NumOperands == 4) ? 2 : 1;
4110 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00004111 for (int i = 0; i < NumOperands; ++i) {
4112 int Val = SVOp->getMaskElt(NumOperands-i-1);
4113 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00004114 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00004115 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00004116 if (i != NumOperands - 1)
4117 Mask <<= Shift;
4118 }
Evan Cheng63d33002006-03-22 08:01:21 +00004119 return Mask;
4120}
4121
Evan Cheng506d3df2006-03-29 23:07:14 +00004122/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004123/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004124unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004125 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004126 unsigned Mask = 0;
4127 // 8 nodes, but we only care about the last 4.
4128 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004129 int Val = SVOp->getMaskElt(i);
4130 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004131 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00004132 if (i != 4)
4133 Mask <<= 2;
4134 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004135 return Mask;
4136}
4137
4138/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004139/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00004140unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00004142 unsigned Mask = 0;
4143 // 8 nodes, but we only care about the first 4.
4144 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004145 int Val = SVOp->getMaskElt(i);
4146 if (Val >= 0)
4147 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00004148 if (i != 0)
4149 Mask <<= 2;
4150 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004151 return Mask;
4152}
4153
Nate Begemana09008b2009-10-19 02:17:23 +00004154/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4155/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4156unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4157 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4158 EVT VVT = N->getValueType(0);
4159 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4160 int Val = 0;
4161
4162 unsigned i, e;
4163 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4164 Val = SVOp->getMaskElt(i);
4165 if (Val >= 0)
4166 break;
4167 }
Eli Friedman63f8dde2011-07-25 21:36:45 +00004168 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004169 return (Val - i) * EltSize;
4170}
4171
David Greenec38a03e2011-02-03 15:50:00 +00004172/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4173/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4174/// instructions.
4175unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4176 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4177 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4178
4179 uint64_t Index =
4180 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4181
4182 EVT VecVT = N->getOperand(0).getValueType();
4183 EVT ElVT = VecVT.getVectorElementType();
4184
4185 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004186 return Index / NumElemsPerChunk;
4187}
4188
David Greeneccacdc12011-02-04 16:08:29 +00004189/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4190/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4191/// instructions.
4192unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4193 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4194 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4195
4196 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004197 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004198
4199 EVT VecVT = N->getValueType(0);
4200 EVT ElVT = VecVT.getVectorElementType();
4201
4202 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004203 return Index / NumElemsPerChunk;
4204}
4205
Evan Cheng37b73872009-07-30 08:33:02 +00004206/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4207/// constant +0.0.
4208bool X86::isZeroNode(SDValue Elt) {
4209 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004210 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004211 (isa<ConstantFPSDNode>(Elt) &&
4212 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4213}
4214
Nate Begeman9008ca62009-04-27 18:41:29 +00004215/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4216/// their permute mask.
4217static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4218 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004219 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004220 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004221 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004222
Nate Begeman5a5ca152009-04-29 05:20:52 +00004223 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 int idx = SVOp->getMaskElt(i);
4225 if (idx < 0)
4226 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004227 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004229 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004230 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004231 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004232 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4233 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234}
4235
Evan Cheng779ccea2007-12-07 21:30:01 +00004236/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4237/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00004238static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 unsigned NumElems = VT.getVectorNumElements();
4240 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 int idx = Mask[i];
4242 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004243 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004244 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00004245 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004246 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004247 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004248 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004249}
4250
Evan Cheng533a0aa2006-04-19 20:35:22 +00004251/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4252/// match movhlps. The lower half elements should come from upper half of
4253/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004254/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00004255static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004256 EVT VT = Op->getValueType(0);
4257 if (VT.getSizeInBits() != 128)
4258 return false;
4259 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004260 return false;
4261 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004263 return false;
4264 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004266 return false;
4267 return true;
4268}
4269
Evan Cheng5ced1d82006-04-06 23:23:56 +00004270/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004271/// is promoted to a vector. It also returns the LoadSDNode by reference if
4272/// required.
4273static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004274 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4275 return false;
4276 N = N->getOperand(0).getNode();
4277 if (!ISD::isNON_EXTLoad(N))
4278 return false;
4279 if (LD)
4280 *LD = cast<LoadSDNode>(N);
4281 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004282}
4283
Dan Gohman65fd6562011-11-03 21:49:52 +00004284// Test whether the given value is a vector value which will be legalized
4285// into a load.
4286static bool WillBeConstantPoolLoad(SDNode *N) {
4287 if (N->getOpcode() != ISD::BUILD_VECTOR)
4288 return false;
4289
4290 // Check for any non-constant elements.
4291 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4292 switch (N->getOperand(i).getNode()->getOpcode()) {
4293 case ISD::UNDEF:
4294 case ISD::ConstantFP:
4295 case ISD::Constant:
4296 break;
4297 default:
4298 return false;
4299 }
4300
4301 // Vectors of all-zeros and all-ones are materialized with special
4302 // instructions rather than being loaded.
4303 return !ISD::isBuildVectorAllZeros(N) &&
4304 !ISD::isBuildVectorAllOnes(N);
4305}
4306
Evan Cheng533a0aa2006-04-19 20:35:22 +00004307/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4308/// match movlp{s|d}. The lower half elements should come from lower half of
4309/// V1 (and in order), and the upper half elements should come from the upper
4310/// half of V2 (and in order). And since V1 will become the source of the
4311/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004312static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4313 ShuffleVectorSDNode *Op) {
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004314 EVT VT = Op->getValueType(0);
4315 if (VT.getSizeInBits() != 128)
4316 return false;
4317
Evan Cheng466685d2006-10-09 20:57:25 +00004318 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004319 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004320 // Is V2 is a vector load, don't do this transformation. We will try to use
4321 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004322 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004323 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004324
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004325 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004326
Evan Cheng533a0aa2006-04-19 20:35:22 +00004327 if (NumElems != 2 && NumElems != 4)
4328 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004329 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004331 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004332 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004334 return false;
4335 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004336}
4337
Evan Cheng39623da2006-04-20 08:58:49 +00004338/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4339/// all the same.
4340static bool isSplatVector(SDNode *N) {
4341 if (N->getOpcode() != ISD::BUILD_VECTOR)
4342 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004343
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004345 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4346 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004347 return false;
4348 return true;
4349}
4350
Evan Cheng213d2cf2007-05-17 18:45:50 +00004351/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004352/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004353/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004354static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004355 SDValue V1 = N->getOperand(0);
4356 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004357 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4358 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004360 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004362 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4363 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004364 if (Opc != ISD::BUILD_VECTOR ||
4365 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 return false;
4367 } else if (Idx >= 0) {
4368 unsigned Opc = V1.getOpcode();
4369 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4370 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004371 if (Opc != ISD::BUILD_VECTOR ||
4372 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004373 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004374 }
4375 }
4376 return true;
4377}
4378
4379/// getZeroVector - Returns a vector of specified type with all zero elements.
4380///
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004381static SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00004382 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004383 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004384
Dale Johannesen0488fb62010-09-30 23:57:10 +00004385 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004386 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004387 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00004388 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004389 if (HasXMMInt) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004390 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4391 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4392 } else { // SSE1
4393 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4394 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4395 }
4396 } else if (VT.getSizeInBits() == 256) { // AVX
4397 // 256-bit logic and arithmetic instructions in AVX are
4398 // all floating-point, no support for integer ops. Default
4399 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00004400 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004401 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4402 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00004403 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004404 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004405}
4406
Chris Lattner8a594482007-11-25 00:24:49 +00004407/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004408/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4409/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4410/// Then bitcast to their original type, ensuring they get CSE'd.
4411static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4412 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004413 assert(VT.isVector() && "Expected a vector type");
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00004414 assert((VT.is128BitVector() || VT.is256BitVector())
4415 && "Expected a 128-bit or 256-bit vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004416
Owen Anderson825b72b2009-08-11 20:47:22 +00004417 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004418 SDValue Vec;
4419 if (VT.getSizeInBits() == 256) {
4420 if (HasAVX2) { // AVX2
4421 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4422 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4423 } else { // AVX
4424 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4425 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
4426 Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
4427 Vec = Insert128BitVector(InsV, Vec,
4428 DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
4429 }
4430 } else {
4431 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004432 }
4433
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004434 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004435}
4436
Evan Cheng39623da2006-04-20 08:58:49 +00004437/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4438/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00004439static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004440 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004441 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004442
Evan Cheng39623da2006-04-20 08:58:49 +00004443 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 SmallVector<int, 8> MaskVec;
4445 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00004446
Nate Begeman5a5ca152009-04-29 05:20:52 +00004447 for (unsigned i = 0; i != NumElems; ++i) {
4448 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 MaskVec[i] = NumElems;
4450 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00004451 }
Evan Cheng39623da2006-04-20 08:58:49 +00004452 }
Evan Cheng39623da2006-04-20 08:58:49 +00004453 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4455 SVOp->getOperand(1), &MaskVec[0]);
4456 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00004457}
4458
Evan Cheng017dcc62006-04-21 01:05:10 +00004459/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4460/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004461static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 SDValue V2) {
4463 unsigned NumElems = VT.getVectorNumElements();
4464 SmallVector<int, 8> Mask;
4465 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004466 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 Mask.push_back(i);
4468 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004469}
4470
Nate Begeman9008ca62009-04-27 18:41:29 +00004471/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004472static SDValue getUnpackl(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;
Evan Chengc575ca22006-04-17 20:43:08 +00004476 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 Mask.push_back(i);
4478 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004479 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004481}
4482
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004483/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004484static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 SDValue V2) {
4486 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00004487 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00004488 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00004489 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 Mask.push_back(i + Half);
4491 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004492 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004494}
4495
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004496// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497// a generic shuffle instruction because the target has no such instructions.
4498// Generate shuffles which repeat i16 and i8 several times until they can be
4499// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004500static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004501 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004504
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 while (NumElems > 4) {
4506 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004507 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004508 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004509 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 EltNo -= NumElems/2;
4511 }
4512 NumElems >>= 1;
4513 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004514 return V;
4515}
Eric Christopherfd179292009-08-27 18:07:15 +00004516
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4518static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4519 EVT VT = V.getValueType();
4520 DebugLoc dl = V.getDebugLoc();
4521 assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
4522 && "Vector size not supported");
4523
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004524 if (VT.getSizeInBits() == 128) {
4525 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004526 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004527 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4528 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529 } else {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004530 // To use VPERMILPS to splat scalars, the second half of indicies must
4531 // refer to the higher part, which is a duplication of the lower one,
4532 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004533 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4534 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004535
4536 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4537 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4538 &SplatMask[0]);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004539 }
4540
4541 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4542}
4543
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004544/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004545static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4546 EVT SrcVT = SV->getValueType(0);
4547 SDValue V1 = SV->getOperand(0);
4548 DebugLoc dl = SV->getDebugLoc();
4549
4550 int EltNo = SV->getSplatIndex();
4551 int NumElems = SrcVT.getVectorNumElements();
4552 unsigned Size = SrcVT.getSizeInBits();
4553
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004554 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4555 "Unknown how to promote splat for type");
4556
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004557 // Extract the 128-bit part containing the splat element and update
4558 // the splat element index when it refers to the higher register.
4559 if (Size == 256) {
4560 unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
4561 V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
4562 if (Idx > 0)
4563 EltNo -= NumElems/2;
4564 }
4565
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004566 // All i16 and i8 vector types can't be used directly by a generic shuffle
4567 // instruction because the target has no such instruction. Generate shuffles
4568 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004569 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004570 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004571 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004572 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004573
4574 // Recreate the 256-bit vector and place the same 128-bit vector
4575 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004576 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004577 if (Size == 256) {
4578 SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
4579 DAG.getConstant(0, MVT::i32), DAG, dl);
4580 V1 = Insert128BitVector(InsV, V1,
4581 DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
4582 }
4583
4584 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004585}
4586
Evan Chengba05f722006-04-21 23:03:30 +00004587/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004588/// vector of zero or undef vector. This produces a shuffle where the low
4589/// element of V2 is swizzled into the zero/undef vector, landing at element
4590/// 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 +00004591static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004592 bool isZero, bool HasXMMInt,
4593 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004594 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue V1 = isZero
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004596 ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004597 unsigned NumElems = VT.getVectorNumElements();
4598 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004599 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004600 // If this is the insertion idx, put the low elt of V2 here.
4601 MaskVec.push_back(i == Idx ? NumElems : i);
4602 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004603}
4604
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004605/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4606/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00004607static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
4608 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004609 if (Depth == 6)
4610 return SDValue(); // Limit search depth.
4611
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004612 SDValue V = SDValue(N, 0);
4613 EVT VT = V.getValueType();
4614 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004615
4616 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4617 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
4618 Index = SV->getMaskElt(Index);
4619
4620 if (Index < 0)
4621 return DAG.getUNDEF(VT.getVectorElementType());
4622
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004623 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004624 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004625 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004626 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627
4628 // Recurse into target specific vector shuffles to find scalars.
4629 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004630 int NumElems = VT.getVectorNumElements();
4631 SmallVector<unsigned, 16> ShuffleMask;
4632 SDValue ImmN;
4633
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004635 case X86ISD::SHUFPS:
4636 case X86ISD::SHUFPD:
4637 ImmN = N->getOperand(N->getNumOperands()-1);
4638 DecodeSHUFPSMask(NumElems,
4639 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4640 ShuffleMask);
4641 break;
4642 case X86ISD::PUNPCKHBW:
4643 case X86ISD::PUNPCKHWD:
4644 case X86ISD::PUNPCKHDQ:
4645 case X86ISD::PUNPCKHQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +00004646 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +00004647 case X86ISD::VPUNPCKHWDY:
4648 case X86ISD::VPUNPCKHDQY:
4649 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004650 DecodePUNPCKHMask(NumElems, ShuffleMask);
4651 break;
4652 case X86ISD::UNPCKHPS:
4653 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00004654 case X86ISD::VUNPCKHPSY:
4655 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004656 DecodeUNPCKHPMask(NumElems, ShuffleMask);
4657 break;
4658 case X86ISD::PUNPCKLBW:
4659 case X86ISD::PUNPCKLWD:
4660 case X86ISD::PUNPCKLDQ:
4661 case X86ISD::PUNPCKLQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +00004662 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +00004663 case X86ISD::VPUNPCKLWDY:
4664 case X86ISD::VPUNPCKLDQY:
4665 case X86ISD::VPUNPCKLQDQY:
David Greenec4db4e52011-02-28 19:06:56 +00004666 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004667 break;
4668 case X86ISD::UNPCKLPS:
4669 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004670 case X86ISD::VUNPCKLPSY:
4671 case X86ISD::VUNPCKLPDY:
4672 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004673 break;
4674 case X86ISD::MOVHLPS:
4675 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4676 break;
4677 case X86ISD::MOVLHPS:
4678 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4679 break;
4680 case X86ISD::PSHUFD:
4681 ImmN = N->getOperand(N->getNumOperands()-1);
4682 DecodePSHUFMask(NumElems,
4683 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4684 ShuffleMask);
4685 break;
4686 case X86ISD::PSHUFHW:
4687 ImmN = N->getOperand(N->getNumOperands()-1);
4688 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4689 ShuffleMask);
4690 break;
4691 case X86ISD::PSHUFLW:
4692 ImmN = N->getOperand(N->getNumOperands()-1);
4693 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4694 ShuffleMask);
4695 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004696 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004697 case X86ISD::MOVSD: {
4698 // The index 0 always comes from the first element of the second source,
4699 // this is why MOVSS and MOVSD are used in the first place. The other
4700 // elements come from the other positions of the first source vector.
4701 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004702 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4703 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004704 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004705 case X86ISD::VPERMILPS:
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004706 ImmN = N->getOperand(N->getNumOperands()-1);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004707 DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004708 ShuffleMask);
Bruno Cardoso Lopes2eb4c2b2011-07-29 01:31:11 +00004709 break;
4710 case X86ISD::VPERMILPSY:
4711 ImmN = N->getOperand(N->getNumOperands()-1);
4712 DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4713 ShuffleMask);
4714 break;
4715 case X86ISD::VPERMILPD:
4716 ImmN = N->getOperand(N->getNumOperands()-1);
4717 DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4718 ShuffleMask);
4719 break;
4720 case X86ISD::VPERMILPDY:
4721 ImmN = N->getOperand(N->getNumOperands()-1);
4722 DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4723 ShuffleMask);
4724 break;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004725 case X86ISD::VPERM2F128:
4726 ImmN = N->getOperand(N->getNumOperands()-1);
4727 DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
4728 ShuffleMask);
4729 break;
Eli Friedman106f6e72011-09-10 02:01:42 +00004730 case X86ISD::MOVDDUP:
4731 case X86ISD::MOVLHPD:
4732 case X86ISD::MOVLPD:
4733 case X86ISD::MOVLPS:
4734 case X86ISD::MOVSHDUP:
4735 case X86ISD::MOVSLDUP:
4736 case X86ISD::PALIGN:
4737 return SDValue(); // Not yet implemented.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004738 default:
Eli Friedman106f6e72011-09-10 02:01:42 +00004739 assert(0 && "unknown target shuffle node");
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004740 return SDValue();
4741 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004742
4743 Index = ShuffleMask[Index];
4744 if (Index < 0)
4745 return DAG.getUNDEF(VT.getVectorElementType());
4746
4747 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4748 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4749 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004750 }
4751
4752 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004753 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004754 V = V.getOperand(0);
4755 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004756 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004757
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004758 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004759 return SDValue();
4760 }
4761
4762 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4763 return (Index == 0) ? V.getOperand(0)
4764 : DAG.getUNDEF(VT.getVectorElementType());
4765
4766 if (V.getOpcode() == ISD::BUILD_VECTOR)
4767 return V.getOperand(Index);
4768
4769 return SDValue();
4770}
4771
4772/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4773/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004774/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004775static
4776unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4777 bool ZerosFromLeft, SelectionDAG &DAG) {
4778 int i = 0;
4779
4780 while (i < NumElems) {
4781 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004782 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004783 if (!(Elt.getNode() &&
4784 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4785 break;
4786 ++i;
4787 }
4788
4789 return i;
4790}
4791
4792/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4793/// MaskE correspond consecutively to elements from one of the vector operands,
4794/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4795static
4796bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4797 int OpIdx, int NumElems, unsigned &OpNum) {
4798 bool SeenV1 = false;
4799 bool SeenV2 = false;
4800
4801 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4802 int Idx = SVOp->getMaskElt(i);
4803 // Ignore undef indicies
4804 if (Idx < 0)
4805 continue;
4806
4807 if (Idx < NumElems)
4808 SeenV1 = true;
4809 else
4810 SeenV2 = true;
4811
4812 // Only accept consecutive elements from the same vector
4813 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4814 return false;
4815 }
4816
4817 OpNum = SeenV1 ? 0 : 1;
4818 return true;
4819}
4820
4821/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4822/// logical left shift of a vector.
4823static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4824 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4825 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4826 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4827 false /* check zeros from right */, DAG);
4828 unsigned OpSrc;
4829
4830 if (!NumZeros)
4831 return false;
4832
4833 // Considering the elements in the mask that are not consecutive zeros,
4834 // check if they consecutively come from only one of the source vectors.
4835 //
4836 // V1 = {X, A, B, C} 0
4837 // \ \ \ /
4838 // vector_shuffle V1, V2 <1, 2, 3, X>
4839 //
4840 if (!isShuffleMaskConsecutive(SVOp,
4841 0, // Mask Start Index
4842 NumElems-NumZeros-1, // Mask End Index
4843 NumZeros, // Where to start looking in the src vector
4844 NumElems, // Number of elements in vector
4845 OpSrc)) // Which source operand ?
4846 return false;
4847
4848 isLeft = false;
4849 ShAmt = NumZeros;
4850 ShVal = SVOp->getOperand(OpSrc);
4851 return true;
4852}
4853
4854/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4855/// logical left shift of a vector.
4856static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4857 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4858 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4859 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4860 true /* check zeros from left */, DAG);
4861 unsigned OpSrc;
4862
4863 if (!NumZeros)
4864 return false;
4865
4866 // Considering the elements in the mask that are not consecutive zeros,
4867 // check if they consecutively come from only one of the source vectors.
4868 //
4869 // 0 { A, B, X, X } = V2
4870 // / \ / /
4871 // vector_shuffle V1, V2 <X, X, 4, 5>
4872 //
4873 if (!isShuffleMaskConsecutive(SVOp,
4874 NumZeros, // Mask Start Index
4875 NumElems-1, // Mask End Index
4876 0, // Where to start looking in the src vector
4877 NumElems, // Number of elements in vector
4878 OpSrc)) // Which source operand ?
4879 return false;
4880
4881 isLeft = true;
4882 ShAmt = NumZeros;
4883 ShVal = SVOp->getOperand(OpSrc);
4884 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004885}
4886
4887/// isVectorShift - Returns true if the shuffle can be implemented as a
4888/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004889static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004890 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004891 // Although the logic below support any bitwidth size, there are no
4892 // shift instructions which handle more than 128-bit vectors.
4893 if (SVOp->getValueType(0).getSizeInBits() > 128)
4894 return false;
4895
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004896 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4897 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4898 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004899
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004900 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004901}
4902
Evan Chengc78d3b42006-04-24 18:01:45 +00004903/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4904///
Dan Gohman475871a2008-07-27 21:46:04 +00004905static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004906 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004907 SelectionDAG &DAG,
4908 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004909 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004910 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004911
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004912 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 bool First = true;
4915 for (unsigned i = 0; i < 16; ++i) {
4916 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4917 if (ThisIsNonZero && First) {
4918 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004919 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004920 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004921 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004922 First = false;
4923 }
4924
4925 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004926 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004927 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4928 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004929 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004930 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004931 }
4932 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4934 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4935 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004936 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004938 } else
4939 ThisElt = LastElt;
4940
Gabor Greifba36cb52008-08-28 21:40:38 +00004941 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004943 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004944 }
4945 }
4946
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004947 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004948}
4949
Bill Wendlinga348c562007-03-22 18:42:45 +00004950/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004951///
Dan Gohman475871a2008-07-27 21:46:04 +00004952static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004953 unsigned NumNonZero, unsigned NumZero,
4954 SelectionDAG &DAG,
4955 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004956 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004958
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004959 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004960 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004961 bool First = true;
4962 for (unsigned i = 0; i < 8; ++i) {
4963 bool isNonZero = (NonZeros & (1 << i)) != 0;
4964 if (isNonZero) {
4965 if (First) {
4966 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004967 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004968 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004969 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004970 First = false;
4971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004972 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004973 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004974 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004975 }
4976 }
4977
4978 return V;
4979}
4980
Evan Chengf26ffe92008-05-29 08:22:04 +00004981/// getVShift - Return a vector logical shift node.
4982///
Owen Andersone50ed302009-08-10 22:56:29 +00004983static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004984 unsigned NumBits, SelectionDAG &DAG,
4985 const TargetLowering &TLI, DebugLoc dl) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004986 assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004987 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004988 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004989 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4990 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004991 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004992 DAG.getConstant(NumBits,
4993 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004994}
4995
Dan Gohman475871a2008-07-27 21:46:04 +00004996SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004997X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004998 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004999
Evan Chengc3630942009-12-09 21:00:30 +00005000 // Check if the scalar load can be widened into a vector load. And if
5001 // the address is "base + cst" see if the cst can be "absorbed" into
5002 // the shuffle mask.
5003 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
5004 SDValue Ptr = LD->getBasePtr();
5005 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5006 return SDValue();
5007 EVT PVT = LD->getValueType(0);
5008 if (PVT != MVT::i32 && PVT != MVT::f32)
5009 return SDValue();
5010
5011 int FI = -1;
5012 int64_t Offset = 0;
5013 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5014 FI = FINode->getIndex();
5015 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00005016 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00005017 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5018 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5019 Offset = Ptr.getConstantOperandVal(1);
5020 Ptr = Ptr.getOperand(0);
5021 } else {
5022 return SDValue();
5023 }
5024
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005025 // FIXME: 256-bit vector instructions don't require a strict alignment,
5026 // improve this code to support it better.
5027 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005028 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005029 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005030 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005031 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005032 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005033 // Can't change the alignment. FIXME: It's possible to compute
5034 // the exact stack offset and reference FI + adjust offset instead.
5035 // If someone *really* cares about this. That's the way to implement it.
5036 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005037 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005038 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005039 }
5040 }
5041
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005042 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005043 // Ptr + (Offset & ~15).
5044 if (Offset < 0)
5045 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005046 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005047 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005048 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005049 if (StartOffset)
5050 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5051 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5052
5053 int EltNo = (Offset - StartOffset) >> 2;
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005054 int NumElems = VT.getVectorNumElements();
5055
5056 EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
5057 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5058 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005059 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005060 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005061
5062 // Canonicalize it to a v4i32 or v8i32 shuffle.
5063 SmallVector<int, 8> Mask;
5064 for (int i = 0; i < NumElems; ++i)
5065 Mask.push_back(EltNo);
5066
5067 V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
5068 return DAG.getNode(ISD::BITCAST, dl, NVT,
5069 DAG.getVectorShuffle(CanonVT, dl, V1,
5070 DAG.getUNDEF(CanonVT),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00005071 }
5072
5073 return SDValue();
5074}
5075
Michael J. Spencerec38de22010-10-10 22:04:20 +00005076/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5077/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005078/// load which has the same value as a build_vector whose operands are 'elts'.
5079///
5080/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005081///
Nate Begeman1449f292010-03-24 22:19:06 +00005082/// FIXME: we'd also like to handle the case where the last elements are zero
5083/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5084/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005085static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005086 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005087 EVT EltVT = VT.getVectorElementType();
5088 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005089
Nate Begemanfdea31a2010-03-24 20:49:50 +00005090 LoadSDNode *LDBase = NULL;
5091 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005092
Nate Begeman1449f292010-03-24 22:19:06 +00005093 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005094 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005095 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005096 for (unsigned i = 0; i < NumElems; ++i) {
5097 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005098
Nate Begemanfdea31a2010-03-24 20:49:50 +00005099 if (!Elt.getNode() ||
5100 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5101 return SDValue();
5102 if (!LDBase) {
5103 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5104 return SDValue();
5105 LDBase = cast<LoadSDNode>(Elt.getNode());
5106 LastLoadedElt = i;
5107 continue;
5108 }
5109 if (Elt.getOpcode() == ISD::UNDEF)
5110 continue;
5111
5112 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5113 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5114 return SDValue();
5115 LastLoadedElt = i;
5116 }
Nate Begeman1449f292010-03-24 22:19:06 +00005117
5118 // If we have found an entire vector of loads and undefs, then return a large
5119 // load of the entire vector width starting at the base pointer. If we found
5120 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005121 if (LastLoadedElt == NumElems - 1) {
5122 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005123 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005124 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005125 LDBase->isVolatile(), LDBase->isNonTemporal(),
5126 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005127 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005128 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005129 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005130 LDBase->isInvariant(), LDBase->getAlignment());
Eli Friedman61cc47e2011-07-26 21:02:58 +00005131 } else if (NumElems == 4 && LastLoadedElt == 1 &&
5132 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005133 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5134 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005135 SDValue ResNode =
5136 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5137 LDBase->getPointerInfo(),
5138 LDBase->getAlignment(),
5139 false/*isVolatile*/, true/*ReadMem*/,
5140 false/*WriteMem*/);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005141 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005142 }
5143 return SDValue();
5144}
5145
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005146/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
5147/// a vbroadcast node. We support two patterns:
5148/// 1. A splat BUILD_VECTOR which uses a single scalar load.
5149/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
5150/// a scalar load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005151/// The scalar load node is returned when a pattern is found,
5152/// or SDValue() otherwise.
5153static SDValue isVectorBroadcast(SDValue &Op, bool hasAVX2) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 EVT VT = Op.getValueType();
5155 SDValue V = Op;
5156
5157 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
5158 V = V.getOperand(0);
5159
5160 //A suspected load to be broadcasted.
5161 SDValue Ld;
5162
5163 switch (V.getOpcode()) {
5164 default:
5165 // Unknown pattern found.
5166 return SDValue();
5167
5168 case ISD::BUILD_VECTOR: {
5169 // The BUILD_VECTOR node must be a splat.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005170 if (!isSplatVector(V.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005171 return SDValue();
5172
5173 Ld = V.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005174
5175 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005176 // of its users are from the BUILD_VECTOR node.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005177 if (!Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005178 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005179 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005180 }
5181
5182 case ISD::VECTOR_SHUFFLE: {
5183 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5184
5185 // Shuffles must have a splat mask where the first element is
5186 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005187 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005188 return SDValue();
5189
5190 SDValue Sc = Op.getOperand(0);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005191 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005192 return SDValue();
5193
5194 Ld = Sc.getOperand(0);
5195
5196 // The scalar_to_vector node and the suspected
5197 // load node must have exactly one user.
5198 if (!Sc.hasOneUse() || !Ld.hasOneUse())
5199 return SDValue();
5200 break;
5201 }
5202 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005203
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005204 // The scalar source must be a normal load.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005205 if (!ISD::isNormalLoad(Ld.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005206 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005207
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005208 bool Is256 = VT.getSizeInBits() == 256;
5209 bool Is128 = VT.getSizeInBits() == 128;
5210 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5211
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005212 if (hasAVX2) {
5213 // VBroadcast to YMM
5214 if (Is256 && (ScalarSize == 8 || ScalarSize == 16 ||
5215 ScalarSize == 32 || ScalarSize == 64 ))
5216 return Ld;
5217
5218 // VBroadcast to XMM
5219 if (Is128 && (ScalarSize == 8 || ScalarSize == 32 ||
5220 ScalarSize == 16 || ScalarSize == 64 ))
5221 return Ld;
5222 }
5223
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005224 // VBroadcast to YMM
5225 if (Is256 && (ScalarSize == 32 || ScalarSize == 64))
5226 return Ld;
5227
5228 // VBroadcast to XMM
5229 if (Is128 && (ScalarSize == 32))
5230 return Ld;
5231
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005232
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005233 // Unsupported broadcast.
5234 return SDValue();
5235}
5236
Evan Chengc3630942009-12-09 21:00:30 +00005237SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005238X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005239 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005240
David Greenef125a292011-02-08 19:04:41 +00005241 EVT VT = Op.getValueType();
5242 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005243 unsigned NumElems = Op.getNumOperands();
5244
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005245 // Vectors containing all zeros can be matched by pxor and xorps later
5246 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5247 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5248 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00005249 if (Op.getValueType() == MVT::v4i32 ||
5250 Op.getValueType() == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005251 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005253 return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005254 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005255
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005256 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005257 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5258 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005259 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper745a86b2011-11-19 22:34:59 +00005260 if (Op.getValueType() == MVT::v4i32 ||
5261 (Op.getValueType() == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005262 return Op;
5263
Craig Topper745a86b2011-11-19 22:34:59 +00005264 return getOnesVector(Op.getValueType(), Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005265 }
5266
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005267 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005268 if (Subtarget->hasAVX() && LD.getNode())
5269 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
5270
Owen Andersone50ed302009-08-10 22:56:29 +00005271 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005272
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273 unsigned NumZero = 0;
5274 unsigned NumNonZero = 0;
5275 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005276 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005277 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005278 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005279 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005280 if (Elt.getOpcode() == ISD::UNDEF)
5281 continue;
5282 Values.insert(Elt);
5283 if (Elt.getOpcode() != ISD::Constant &&
5284 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005285 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005286 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005287 NumZero++;
5288 else {
5289 NonZeros |= (1 << i);
5290 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005291 }
5292 }
5293
Chris Lattner97a2a562010-08-26 05:24:29 +00005294 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5295 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005296 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005297
Chris Lattner67f453a2008-03-09 05:42:06 +00005298 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005299 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005300 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005302
Chris Lattner62098042008-03-09 01:05:04 +00005303 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5304 // the value are obviously zero, truncate the value to i32 and do the
5305 // insertion that way. Only do this if the value is non-constant or if the
5306 // value is a constant being inserted into element 0. It is cheaper to do
5307 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005308 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005309 (!IsAllConstants || Idx == 0)) {
5310 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005311 // Handle SSE only.
5312 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5313 EVT VecVT = MVT::v4i32;
5314 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005315
Chris Lattner62098042008-03-09 01:05:04 +00005316 // Truncate the value (which may itself be a constant) to i32, and
5317 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005318 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005319 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00005320 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005321 Subtarget->hasXMMInt(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005322
Chris Lattner62098042008-03-09 01:05:04 +00005323 // Now we have our 32-bit value zero extended in the low element of
5324 // a vector. If Idx != 0, swizzle it into place.
5325 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005326 SmallVector<int, 4> Mask;
5327 Mask.push_back(Idx);
5328 for (unsigned i = 1; i != VecElts; ++i)
5329 Mask.push_back(i);
5330 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00005331 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00005332 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005333 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005334 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00005335 }
5336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005337
Chris Lattner19f79692008-03-08 22:59:52 +00005338 // If we have a constant or non-constant insertion into the low element of
5339 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5340 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005341 // depending on what the source datatype is.
5342 if (Idx == 0) {
5343 if (NumZero == 0) {
5344 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5346 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00005347 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5348 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005349 return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
Eli Friedman10415532009-06-06 06:05:10 +00005350 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5352 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005353 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
5354 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00005355 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5356 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005357 Subtarget->hasXMMInt(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005358 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005359 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005360 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005361
5362 // Is it a vector logical left shift?
5363 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005364 X86::isZeroNode(Op.getOperand(0)) &&
5365 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005366 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005367 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005368 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005369 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005370 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005372
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005373 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005374 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375
Chris Lattner19f79692008-03-08 22:59:52 +00005376 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5377 // is a non-constant being inserted into an element other than the low one,
5378 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5379 // movd/movss) to move this into the low element, then shuffle it into
5380 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005382 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005383
Evan Cheng0db9fe62006-04-25 20:13:52 +00005384 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00005385 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005386 Subtarget->hasXMMInt(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005387 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00005389 MaskVec.push_back(i == Idx ? 0 : 1);
5390 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391 }
5392 }
5393
Chris Lattner67f453a2008-03-09 05:42:06 +00005394 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005395 if (Values.size() == 1) {
5396 if (EVTBits == 32) {
5397 // Instead of a shuffle like this:
5398 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5399 // Check if it's possible to issue this instead.
5400 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5401 unsigned Idx = CountTrailingZeros_32(NonZeros);
5402 SDValue Item = Op.getOperand(Idx);
5403 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5404 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5405 }
Dan Gohman475871a2008-07-27 21:46:04 +00005406 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005407 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005408
Dan Gohmana3941172007-07-24 22:55:08 +00005409 // A vector full of immediates; various special cases are already
5410 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005411 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005412 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005413
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005414 // For AVX-length vectors, build the individual 128-bit pieces and use
5415 // shuffles to put them in place.
5416 if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
5417 SmallVector<SDValue, 32> V;
5418 for (unsigned i = 0; i < NumElems; ++i)
5419 V.push_back(Op.getOperand(i));
5420
5421 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5422
5423 // Build both the lower and upper subvector.
5424 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5425 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5426 NumElems/2);
5427
5428 // Recreate the wider vector with the lower and upper part.
Bruno Cardoso Lopes15d03fb2011-07-28 01:26:53 +00005429 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
5430 DAG.getConstant(0, MVT::i32), DAG, dl);
5431 return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005432 DAG, dl);
5433 }
5434
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005435 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005436 if (EVTBits == 64) {
5437 if (NumNonZero == 1) {
5438 // One half is zero or undef.
5439 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005440 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005441 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00005442 return getShuffleVectorZeroOrUndef(V2, Idx, true,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005443 Subtarget->hasXMMInt(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005444 }
Dan Gohman475871a2008-07-27 21:46:04 +00005445 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005446 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447
5448 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005449 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005450 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00005451 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005452 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005453 }
5454
Bill Wendling826f36f2007-03-28 00:57:11 +00005455 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00005457 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005458 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005459 }
5460
5461 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005462 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00005463 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464 if (NumElems == 4 && NumZero > 0) {
5465 for (unsigned i = 0; i < 4; ++i) {
5466 bool isZero = !(NonZeros & (1 << i));
5467 if (isZero)
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005468 V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 else
Dale Johannesenace16102009-02-03 19:33:06 +00005470 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005471 }
5472
5473 for (unsigned i = 0; i < 2; ++i) {
5474 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5475 default: break;
5476 case 0:
5477 V[i] = V[i*2]; // Must be a zero vector.
5478 break;
5479 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005480 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 break;
5482 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005483 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005484 break;
5485 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005486 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 break;
5488 }
5489 }
5490
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 bool Reverse = (NonZeros & 0x3) == 2;
5493 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005494 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005495 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5496 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005497 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5498 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499 }
5500
Nate Begemanfdea31a2010-03-24 20:49:50 +00005501 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5502 // Check for a build vector of consecutive loads.
5503 for (unsigned i = 0; i < NumElems; ++i)
5504 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005505
Nate Begemanfdea31a2010-03-24 20:49:50 +00005506 // Check for elements which are consecutive loads.
5507 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5508 if (LD.getNode())
5509 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005510
5511 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperc0d82852011-11-22 00:44:41 +00005512 if (getSubtarget()->hasSSE41orAVX()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005513 SDValue Result;
5514 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5515 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5516 else
5517 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005518
Chris Lattner24faf612010-08-28 17:59:08 +00005519 for (unsigned i = 1; i < NumElems; ++i) {
5520 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5521 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005522 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005523 }
5524 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005525 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005526
Chris Lattner6e80e442010-08-28 17:15:43 +00005527 // Otherwise, expand into a number of unpckl*, start by extending each of
5528 // our (non-undef) elements to the full vector width with the element in the
5529 // bottom slot of the vector (which generates no code for SSE).
5530 for (unsigned i = 0; i < NumElems; ++i) {
5531 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5532 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5533 else
5534 V[i] = DAG.getUNDEF(VT);
5535 }
5536
5537 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005538 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5539 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5540 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005541 unsigned EltStride = NumElems >> 1;
5542 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005543 for (unsigned i = 0; i < EltStride; ++i) {
5544 // If V[i+EltStride] is undef and this is the first round of mixing,
5545 // then it is safe to just drop this shuffle: V[i] is already in the
5546 // right place, the one element (since it's the first round) being
5547 // inserted as undef can be dropped. This isn't safe for successive
5548 // rounds because they will permute elements within both vectors.
5549 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5550 EltStride == NumElems/2)
5551 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005552
Chris Lattner6e80e442010-08-28 17:15:43 +00005553 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005554 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005555 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005556 }
5557 return V[0];
5558 }
Dan Gohman475871a2008-07-27 21:46:04 +00005559 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005560}
5561
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005562// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
5563// them in a MMX register. This is better than doing a stack convert.
5564static SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005565 DebugLoc dl = Op.getDebugLoc();
5566 EVT ResVT = Op.getValueType();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005567
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005568 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5569 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5570 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005571 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005572 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5573 InVec = Op.getOperand(1);
5574 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5575 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005576 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005577 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5578 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5579 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005580 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005581 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5582 Mask[0] = 0; Mask[1] = 2;
5583 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5584 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005585 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00005586}
5587
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005588// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5589// to create 256-bit vectors from two other 128-bit ones.
5590static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5591 DebugLoc dl = Op.getDebugLoc();
5592 EVT ResVT = Op.getValueType();
5593
5594 assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
5595
5596 SDValue V1 = Op.getOperand(0);
5597 SDValue V2 = Op.getOperand(1);
5598 unsigned NumElems = ResVT.getVectorNumElements();
5599
5600 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
5601 DAG.getConstant(0, MVT::i32), DAG, dl);
5602 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
5603 DAG, dl);
5604}
5605
5606SDValue
5607X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005608 EVT ResVT = Op.getValueType();
5609
5610 assert(Op.getNumOperands() == 2);
5611 assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
5612 "Unsupported CONCAT_VECTORS for value type");
5613
5614 // We support concatenate two MMX registers and place them in a MMX register.
5615 // This is better than doing a stack convert.
5616 if (ResVT.is128BitVector())
5617 return LowerMMXCONCAT_VECTORS(Op, DAG);
5618
5619 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5620 // from two other 128-bit ones.
5621 return LowerAVXCONCAT_VECTORS(Op, DAG);
5622}
5623
Nate Begemanb9a47b82009-02-23 08:49:38 +00005624// v8i16 shuffles - Prefer shuffles in the following order:
5625// 1. [all] pshuflw, pshufhw, optional move
5626// 2. [ssse3] 1 x pshufb
5627// 3. [ssse3] 2 x pshufb + 1 x por
5628// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005629SDValue
5630X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5631 SelectionDAG &DAG) const {
5632 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005633 SDValue V1 = SVOp->getOperand(0);
5634 SDValue V2 = SVOp->getOperand(1);
5635 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005636 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005637
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 // Determine if more than 1 of the words in each of the low and high quadwords
5639 // of the result come from the same quadword of one of the two inputs. Undef
5640 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005641 unsigned LoQuad[] = { 0, 0, 0, 0 };
5642 unsigned HiQuad[] = { 0, 0, 0, 0 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005643 BitVector InputQuads(4);
5644 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005645 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647 MaskVals.push_back(EltIdx);
5648 if (EltIdx < 0) {
5649 ++Quad[0];
5650 ++Quad[1];
5651 ++Quad[2];
5652 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005653 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 }
5655 ++Quad[EltIdx / 4];
5656 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005657 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005658
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005660 unsigned MaxQuad = 1;
5661 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 if (LoQuad[i] > MaxQuad) {
5663 BestLoQuad = i;
5664 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005665 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005666 }
5667
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005669 MaxQuad = 1;
5670 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005671 if (HiQuad[i] > MaxQuad) {
5672 BestHiQuad = i;
5673 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005674 }
5675 }
5676
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005678 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005679 // single pshufb instruction is necessary. If There are more than 2 input
5680 // quads, disable the next transformation since it does not help SSSE3.
5681 bool V1Used = InputQuads[0] || InputQuads[1];
5682 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperc0d82852011-11-22 00:44:41 +00005683 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 if (InputQuads.count() == 2 && V1Used && V2Used) {
5685 BestLoQuad = InputQuads.find_first();
5686 BestHiQuad = InputQuads.find_next(BestLoQuad);
5687 }
5688 if (InputQuads.count() > 2) {
5689 BestLoQuad = -1;
5690 BestHiQuad = -1;
5691 }
5692 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005693
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5695 // the shuffle mask. If a quad is scored as -1, that means that it contains
5696 // words from all 4 input quadwords.
5697 SDValue NewV;
5698 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005699 SmallVector<int, 8> MaskV;
5700 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5701 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00005702 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005703 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5704 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5705 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005706
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5708 // source words for the shuffle, to aid later transformations.
5709 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005710 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005711 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005713 if (idx != (int)i)
5714 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005716 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 AllWordsInNewV = false;
5718 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005719 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5722 if (AllWordsInNewV) {
5723 for (int i = 0; i != 8; ++i) {
5724 int idx = MaskVals[i];
5725 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005726 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005727 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005728 if ((idx != i) && idx < 4)
5729 pshufhw = false;
5730 if ((idx != i) && idx > 3)
5731 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005732 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005733 V1 = NewV;
5734 V2Used = false;
5735 BestLoQuad = 0;
5736 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005737 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005738
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5740 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005741 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005742 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5743 unsigned TargetMask = 0;
5744 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005746 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
5747 X86::getShufflePSHUFLWImmediate(NewV.getNode());
5748 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005749 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005750 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005751 }
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // If we have SSSE3, and all words of the result are from 1 input vector,
5754 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5755 // is present, fall back to case 4.
Craig Topperc0d82852011-11-22 00:44:41 +00005756 if (Subtarget->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005758
Nate Begemanb9a47b82009-02-23 08:49:38 +00005759 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005760 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005761 // mask, and elements that come from V1 in the V2 mask, so that the two
5762 // results can be OR'd together.
5763 bool TwoInputs = V1Used && V2Used;
5764 for (unsigned i = 0; i != 8; ++i) {
5765 int EltIdx = MaskVals[i] * 2;
5766 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5768 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 continue;
5770 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5772 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005774 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005775 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005776 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005777 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005779 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005780
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // Calculate the shuffle mask for the second input, shuffle it, and
5782 // OR it with the first shuffled input.
5783 pshufbMask.clear();
5784 for (unsigned i = 0; i != 8; ++i) {
5785 int EltIdx = MaskVals[i] * 2;
5786 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005787 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5788 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 continue;
5790 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005791 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5792 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005794 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005795 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005796 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 MVT::v16i8, &pshufbMask[0], 16));
5798 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005799 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 }
5801
5802 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5803 // and update MaskVals with new element order.
5804 BitVector InOrder(8);
5805 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005806 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 for (int i = 0; i != 4; ++i) {
5808 int idx = MaskVals[i];
5809 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005810 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 InOrder.set(i);
5812 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005813 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 InOrder.set(i);
5815 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 }
5818 }
5819 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005820 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005822 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005823
Craig Topperc0d82852011-11-22 00:44:41 +00005824 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005825 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
5826 NewV.getOperand(0),
5827 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
5828 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 }
Eric Christopherfd179292009-08-27 18:07:15 +00005830
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5832 // and update MaskVals with the new element order.
5833 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005834 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005836 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 for (unsigned i = 4; i != 8; ++i) {
5838 int idx = MaskVals[i];
5839 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 InOrder.set(i);
5842 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005843 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 InOrder.set(i);
5845 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 }
5848 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005850 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005851
Craig Topperc0d82852011-11-22 00:44:41 +00005852 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3orAVX())
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005853 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5854 NewV.getOperand(0),
5855 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5856 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 }
Eric Christopherfd179292009-08-27 18:07:15 +00005858
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 // In case BestHi & BestLo were both -1, which means each quadword has a word
5860 // from each of the four input quadwords, calculate the InOrder bitvector now
5861 // before falling through to the insert/extract cleanup.
5862 if (BestLoQuad == -1 && BestHiQuad == -1) {
5863 NewV = V1;
5864 for (int i = 0; i != 8; ++i)
5865 if (MaskVals[i] < 0 || MaskVals[i] == i)
5866 InOrder.set(i);
5867 }
Eric Christopherfd179292009-08-27 18:07:15 +00005868
Nate Begemanb9a47b82009-02-23 08:49:38 +00005869 // The other elements are put in the right place using pextrw and pinsrw.
5870 for (unsigned i = 0; i != 8; ++i) {
5871 if (InOrder[i])
5872 continue;
5873 int EltIdx = MaskVals[i];
5874 if (EltIdx < 0)
5875 continue;
5876 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005877 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005879 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005881 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 DAG.getIntPtrConstant(i));
5883 }
5884 return NewV;
5885}
5886
5887// v16i8 shuffles - Prefer shuffles in the following order:
5888// 1. [ssse3] 1 x pshufb
5889// 2. [ssse3] 2 x pshufb + 1 x por
5890// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5891static
Nate Begeman9008ca62009-04-27 18:41:29 +00005892SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005893 SelectionDAG &DAG,
5894 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005895 SDValue V1 = SVOp->getOperand(0);
5896 SDValue V2 = SVOp->getOperand(1);
5897 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005898 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005899 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005900
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005902 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // present, fall back to case 3.
5904 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5905 bool V1Only = true;
5906 bool V2Only = true;
5907 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005908 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 if (EltIdx < 0)
5910 continue;
5911 if (EltIdx < 16)
5912 V2Only = false;
5913 else
5914 V1Only = false;
5915 }
Eric Christopherfd179292009-08-27 18:07:15 +00005916
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperc0d82852011-11-22 00:44:41 +00005918 if (TLI.getSubtarget()->hasSSSE3orAVX()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005920
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005922 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005923 //
5924 // Otherwise, we have elements from both input vectors, and must zero out
5925 // elements that come from V2 in the first mask, and V1 in the second mask
5926 // so that we can OR them together.
5927 bool TwoInputs = !(V1Only || V2Only);
5928 for (unsigned i = 0; i != 16; ++i) {
5929 int EltIdx = MaskVals[i];
5930 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 continue;
5933 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005935 }
5936 // If all the elements are from V2, assign it to V1 and return after
5937 // building the first pshufb.
5938 if (V2Only)
5939 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005940 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005941 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 if (!TwoInputs)
5944 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005945
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 // Calculate the shuffle mask for the second input, shuffle it, and
5947 // OR it with the first shuffled input.
5948 pshufbMask.clear();
5949 for (unsigned i = 0; i != 16; ++i) {
5950 int EltIdx = MaskVals[i];
5951 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005952 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 continue;
5954 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005957 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005958 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005959 MVT::v16i8, &pshufbMask[0], 16));
5960 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005961 }
Eric Christopherfd179292009-08-27 18:07:15 +00005962
Nate Begemanb9a47b82009-02-23 08:49:38 +00005963 // No SSSE3 - Calculate in place words and then fix all out of place words
5964 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5965 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005966 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5967 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 SDValue NewV = V2Only ? V2 : V1;
5969 for (int i = 0; i != 8; ++i) {
5970 int Elt0 = MaskVals[i*2];
5971 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005972
Nate Begemanb9a47b82009-02-23 08:49:38 +00005973 // This word of the result is all undef, skip it.
5974 if (Elt0 < 0 && Elt1 < 0)
5975 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005976
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 // This word of the result is already in the correct place, skip it.
5978 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5979 continue;
5980 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5981 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005982
Nate Begemanb9a47b82009-02-23 08:49:38 +00005983 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5984 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5985 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005986
5987 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5988 // using a single extract together, load it and store it.
5989 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005990 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005991 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005993 DAG.getIntPtrConstant(i));
5994 continue;
5995 }
5996
Nate Begemanb9a47b82009-02-23 08:49:38 +00005997 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005998 // source byte is not also odd, shift the extracted word left 8 bits
5999 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006002 DAG.getIntPtrConstant(Elt1 / 2));
6003 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006005 DAG.getConstant(8,
6006 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006007 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6009 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 }
6011 // If Elt0 is defined, extract it from the appropriate source. If the
6012 // source byte is not also even, shift the extracted word right 8 bits. If
6013 // Elt1 was also defined, OR the extracted values together before
6014 // inserting them in the result.
6015 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006017 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6018 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006020 DAG.getConstant(8,
6021 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006022 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6024 DAG.getConstant(0x00FF, MVT::i16));
6025 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 : InsElt0;
6027 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006028 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 DAG.getIntPtrConstant(i));
6030 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006031 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006032}
6033
Evan Cheng7a831ce2007-12-15 03:00:47 +00006034/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006035/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006036/// done when every pair / quad of shuffle mask elements point to elements in
6037/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006038/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006039static
Nate Begeman9008ca62009-04-27 18:41:29 +00006040SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006041 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00006042 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00006043 SDValue V1 = SVOp->getOperand(0);
6044 SDValue V2 = SVOp->getOperand(1);
6045 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00006046 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006047 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00006048 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006049 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 case MVT::v4f32: NewVT = MVT::v2f64; break;
6051 case MVT::v4i32: NewVT = MVT::v2i64; break;
6052 case MVT::v8i16: NewVT = MVT::v4i32; break;
6053 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006054 }
6055
Nate Begeman9008ca62009-04-27 18:41:29 +00006056 int Scale = NumElems / NewWidth;
6057 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00006058 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006059 int StartIdx = -1;
6060 for (int j = 0; j < Scale; ++j) {
6061 int EltIdx = SVOp->getMaskElt(i+j);
6062 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006063 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00006064 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00006065 StartIdx = EltIdx - (EltIdx % Scale);
6066 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00006067 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006068 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006069 if (StartIdx == -1)
6070 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00006071 else
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006073 }
6074
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006075 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
6076 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00006077 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006078}
6079
Evan Chengd880b972008-05-09 21:53:03 +00006080/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006081///
Owen Andersone50ed302009-08-10 22:56:29 +00006082static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006083 SDValue SrcOp, SelectionDAG &DAG,
6084 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006085 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006086 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006087 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006088 LD = dyn_cast<LoadSDNode>(SrcOp);
6089 if (!LD) {
6090 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6091 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006092 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006093 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006094 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006095 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006096 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006097 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006098 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006099 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006100 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6101 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6102 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006103 SrcOp.getOperand(0)
6104 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006105 }
6106 }
6107 }
6108
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006109 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006110 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006111 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006112 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006113}
6114
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006115/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
6116/// shuffle node referes to only one lane in the sources.
6117static bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
6118 EVT VT = SVOp->getValueType(0);
6119 int NumElems = VT.getVectorNumElements();
6120 int HalfSize = NumElems/2;
6121 SmallVector<int, 16> M;
6122 SVOp->getMask(M);
6123 bool MatchA = false, MatchB = false;
6124
6125 for (int l = 0; l < NumElems*2; l += HalfSize) {
6126 if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
6127 MatchA = true;
6128 break;
6129 }
6130 }
6131
6132 for (int l = 0; l < NumElems*2; l += HalfSize) {
6133 if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
6134 MatchB = true;
6135 break;
6136 }
6137 }
6138
6139 return MatchA && MatchB;
6140}
6141
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006142/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6143/// which could not be matched by any known target speficic shuffle
6144static SDValue
6145LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006146 if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
6147 // If each half of a vector shuffle node referes to only one lane in the
6148 // source vectors, extract each used 128-bit lane and shuffle them using
6149 // 128-bit shuffles. Then, concatenate the results. Otherwise leave
6150 // the work to the legalizer.
6151 DebugLoc dl = SVOp->getDebugLoc();
6152 EVT VT = SVOp->getValueType(0);
6153 int NumElems = VT.getVectorNumElements();
6154 int HalfSize = NumElems/2;
6155
6156 // Extract the reference for each half
6157 int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
6158 int FstVecOpNum = 0, SndVecOpNum = 0;
6159 for (int i = 0; i < HalfSize; ++i) {
6160 int Elt = SVOp->getMaskElt(i);
6161 if (SVOp->getMaskElt(i) < 0)
6162 continue;
6163 FstVecOpNum = Elt/NumElems;
6164 FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6165 break;
6166 }
6167 for (int i = HalfSize; i < NumElems; ++i) {
6168 int Elt = SVOp->getMaskElt(i);
6169 if (SVOp->getMaskElt(i) < 0)
6170 continue;
6171 SndVecOpNum = Elt/NumElems;
6172 SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
6173 break;
6174 }
6175
6176 // Extract the subvectors
6177 SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
6178 DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
6179 SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
6180 DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
6181
6182 // Generate 128-bit shuffles
6183 SmallVector<int, 16> MaskV1, MaskV2;
6184 for (int i = 0; i < HalfSize; ++i) {
6185 int Elt = SVOp->getMaskElt(i);
6186 MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6187 }
6188 for (int i = HalfSize; i < NumElems; ++i) {
6189 int Elt = SVOp->getMaskElt(i);
6190 MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
6191 }
6192
6193 EVT NVT = V1.getValueType();
6194 V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
6195 V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
6196
6197 // Concatenate the result back
6198 SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
6199 DAG.getConstant(0, MVT::i32), DAG, dl);
6200 return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
6201 DAG, dl);
6202 }
6203
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006204 return SDValue();
6205}
6206
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006207/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6208/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006209static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006210LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006211 SDValue V1 = SVOp->getOperand(0);
6212 SDValue V2 = SVOp->getOperand(1);
6213 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006214 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006215
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006216 assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
6217
Evan Chengace3c172008-07-22 21:13:36 +00006218 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00006219 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006220 SmallVector<int, 8> Mask1(4U, -1);
6221 SmallVector<int, 8> PermMask;
6222 SVOp->getMask(PermMask);
6223
Evan Chengace3c172008-07-22 21:13:36 +00006224 unsigned NumHi = 0;
6225 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006226 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 int Idx = PermMask[i];
6228 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006229 Locs[i] = std::make_pair(-1, -1);
6230 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006231 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6232 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006233 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006234 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006235 NumLo++;
6236 } else {
6237 Locs[i] = std::make_pair(1, NumHi);
6238 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006239 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006240 NumHi++;
6241 }
6242 }
6243 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006244
Evan Chengace3c172008-07-22 21:13:36 +00006245 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006246 // If no more than two elements come from either vector. This can be
6247 // implemented with two shuffles. First shuffle gather the elements.
6248 // The second shuffle, which takes the first shuffle as both of its
6249 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006250 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006251
Nate Begeman9008ca62009-04-27 18:41:29 +00006252 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00006253
Evan Chengace3c172008-07-22 21:13:36 +00006254 for (unsigned i = 0; i != 4; ++i) {
6255 if (Locs[i].first == -1)
6256 continue;
6257 else {
6258 unsigned Idx = (i < 2) ? 0 : 4;
6259 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006260 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006261 }
6262 }
6263
Nate Begeman9008ca62009-04-27 18:41:29 +00006264 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006265 } else if (NumLo == 3 || NumHi == 3) {
6266 // Otherwise, we must have three elements from one vector, call it X, and
6267 // one element from the other, call it Y. First, use a shufps to build an
6268 // intermediate vector with the one element from Y and the element from X
6269 // that will be in the same half in the final destination (the indexes don't
6270 // matter). Then, use a shufps to build the final vector, taking the half
6271 // containing the element from Y from the intermediate, and the other half
6272 // from X.
6273 if (NumHi == 3) {
6274 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00006275 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006276 std::swap(V1, V2);
6277 }
6278
6279 // Find the element from V2.
6280 unsigned HiIndex;
6281 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006282 int Val = PermMask[HiIndex];
6283 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006284 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006285 if (Val >= 4)
6286 break;
6287 }
6288
Nate Begeman9008ca62009-04-27 18:41:29 +00006289 Mask1[0] = PermMask[HiIndex];
6290 Mask1[1] = -1;
6291 Mask1[2] = PermMask[HiIndex^1];
6292 Mask1[3] = -1;
6293 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006294
6295 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006296 Mask1[0] = PermMask[0];
6297 Mask1[1] = PermMask[1];
6298 Mask1[2] = HiIndex & 1 ? 6 : 4;
6299 Mask1[3] = HiIndex & 1 ? 4 : 6;
6300 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006301 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006302 Mask1[0] = HiIndex & 1 ? 2 : 0;
6303 Mask1[1] = HiIndex & 1 ? 0 : 2;
6304 Mask1[2] = PermMask[2];
6305 Mask1[3] = PermMask[3];
6306 if (Mask1[2] >= 0)
6307 Mask1[2] += 4;
6308 if (Mask1[3] >= 0)
6309 Mask1[3] += 4;
6310 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006311 }
Evan Chengace3c172008-07-22 21:13:36 +00006312 }
6313
6314 // Break it into (shuffle shuffle_hi, shuffle_lo).
6315 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00006316 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00006317 SmallVector<int,8> LoMask(4U, -1);
6318 SmallVector<int,8> HiMask(4U, -1);
6319
6320 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006321 unsigned MaskIdx = 0;
6322 unsigned LoIdx = 0;
6323 unsigned HiIdx = 2;
6324 for (unsigned i = 0; i != 4; ++i) {
6325 if (i == 2) {
6326 MaskPtr = &HiMask;
6327 MaskIdx = 1;
6328 LoIdx = 0;
6329 HiIdx = 2;
6330 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006331 int Idx = PermMask[i];
6332 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006333 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006334 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006335 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006336 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006337 LoIdx++;
6338 } else {
6339 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00006340 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006341 HiIdx++;
6342 }
6343 }
6344
Nate Begeman9008ca62009-04-27 18:41:29 +00006345 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6346 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6347 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00006348 for (unsigned i = 0; i != 4; ++i) {
6349 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006350 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00006351 } else {
6352 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006353 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00006354 }
6355 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006356 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006357}
6358
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006359static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006360 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006361 V = V.getOperand(0);
6362 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6363 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006364 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6365 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6366 // BUILD_VECTOR (load), undef
6367 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006368 if (MayFoldLoad(V))
6369 return true;
6370 return false;
6371}
6372
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006373// FIXME: the version above should always be used. Since there's
6374// a bug where several vector shuffles can't be folded because the
6375// DAG is not updated during lowering and a node claims to have two
6376// uses while it only has one, use this version, and let isel match
6377// another instruction if the load really happens to have more than
6378// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006379// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006380static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006381 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006382 V = V.getOperand(0);
6383 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6384 V = V.getOperand(0);
6385 if (ISD::isNormalLoad(V.getNode()))
6386 return true;
6387 return false;
6388}
6389
6390/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
6391/// a vector extract, and if both can be later optimized into a single load.
6392/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
6393/// here because otherwise a target specific shuffle node is going to be
6394/// emitted for this shuffle, and the optimization not done.
6395/// FIXME: This is probably not the best approach, but fix the problem
6396/// until the right path is decided.
6397static
6398bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
6399 const TargetLowering &TLI) {
6400 EVT VT = V.getValueType();
6401 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
6402
6403 // Be sure that the vector shuffle is present in a pattern like this:
6404 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
6405 if (!V.hasOneUse())
6406 return false;
6407
6408 SDNode *N = *V.getNode()->use_begin();
6409 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6410 return false;
6411
6412 SDValue EltNo = N->getOperand(1);
6413 if (!isa<ConstantSDNode>(EltNo))
6414 return false;
6415
6416 // If the bit convert changed the number of elements, it is unsafe
6417 // to examine the mask.
6418 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006419 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006420 EVT SrcVT = V.getOperand(0).getValueType();
6421 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
6422 return false;
6423 V = V.getOperand(0);
6424 HasShuffleIntoBitcast = true;
6425 }
6426
6427 // Select the input vector, guarding against out of range extract vector.
6428 unsigned NumElems = VT.getVectorNumElements();
6429 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
6430 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
6431 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
6432
6433 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006434 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006435 V = V.getOperand(0);
6436
6437 if (ISD::isNormalLoad(V.getNode())) {
6438 // Is the original load suitable?
6439 LoadSDNode *LN0 = cast<LoadSDNode>(V);
6440
6441 // FIXME: avoid the multi-use bug that is preventing lots of
6442 // of foldings to be detected, this is still wrong of course, but
6443 // give the temporary desired behavior, and if it happens that
6444 // the load has real more uses, during isel it will not fold, and
6445 // will generate poor code.
6446 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
6447 return false;
6448
6449 if (!HasShuffleIntoBitcast)
6450 return true;
6451
6452 // If there's a bitcast before the shuffle, check if the load type and
6453 // alignment is valid.
6454 unsigned Align = LN0->getAlignment();
6455 unsigned NewAlign =
6456 TLI.getTargetData()->getABITypeAlignment(
6457 VT.getTypeForEVT(*DAG.getContext()));
6458
6459 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
6460 return false;
6461 }
6462
6463 return true;
6464}
6465
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006466static
Evan Cheng835580f2010-10-07 20:50:20 +00006467SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6468 EVT VT = Op.getValueType();
6469
6470 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006471 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6472 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006473 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6474 V1, DAG));
6475}
6476
6477static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006478SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006479 bool HasXMMInt) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006480 SDValue V1 = Op.getOperand(0);
6481 SDValue V2 = Op.getOperand(1);
6482 EVT VT = Op.getValueType();
6483
6484 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6485
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006486 if (HasXMMInt && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006487 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6488
Evan Cheng0899f5c2011-08-31 02:05:24 +00006489 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6490 return DAG.getNode(ISD::BITCAST, dl, VT,
6491 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6492 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6493 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006494}
6495
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006496static
6497SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6498 SDValue V1 = Op.getOperand(0);
6499 SDValue V2 = Op.getOperand(1);
6500 EVT VT = Op.getValueType();
6501
6502 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6503 "unsupported shuffle type");
6504
6505 if (V2.getOpcode() == ISD::UNDEF)
6506 V2 = V1;
6507
6508 // v4i32 or v4f32
6509 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6510}
6511
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006512static inline unsigned getSHUFPOpcode(EVT VT) {
6513 switch(VT.getSimpleVT().SimpleTy) {
6514 case MVT::v8i32: // Use fp unit for int unpack.
6515 case MVT::v8f32:
6516 case MVT::v4i32: // Use fp unit for int unpack.
6517 case MVT::v4f32: return X86ISD::SHUFPS;
6518 case MVT::v4i64: // Use fp unit for int unpack.
6519 case MVT::v4f64:
6520 case MVT::v2i64: // Use fp unit for int unpack.
6521 case MVT::v2f64: return X86ISD::SHUFPD;
6522 default:
6523 llvm_unreachable("Unknown type for shufp*");
6524 }
6525 return 0;
6526}
6527
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006528static
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006529SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006530 SDValue V1 = Op.getOperand(0);
6531 SDValue V2 = Op.getOperand(1);
6532 EVT VT = Op.getValueType();
6533 unsigned NumElems = VT.getVectorNumElements();
6534
6535 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6536 // operand of these instructions is only memory, so check if there's a
6537 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6538 // same masks.
6539 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006540
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006541 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006542 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006543 CanFoldLoad = true;
6544
6545 // When V1 is a load, it can be folded later into a store in isel, example:
6546 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6547 // turns into:
6548 // (MOVLPSmr addr:$src1, VR128:$src2)
6549 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006550 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006551 CanFoldLoad = true;
6552
Dan Gohman65fd6562011-11-03 21:49:52 +00006553 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006554 if (CanFoldLoad) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006555 if (HasXMMInt && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006556 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6557
6558 if (NumElems == 4)
Dan Gohman65fd6562011-11-03 21:49:52 +00006559 // If we don't care about the second element, procede to use movss.
6560 if (SVOp->getMaskElt(1) != -1)
6561 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006562 }
6563
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006564 // movl and movlp will both match v2i64, but v2i64 is never matched by
6565 // movl earlier because we make it strict to avoid messing with the movlp load
6566 // folding logic (see the code above getMOVLP call). Match it here then,
6567 // this is horrible, but will stay like this until we move all shuffle
6568 // matching to x86 specific nodes. Note that for the 1st condition all
6569 // types are matched with movsd.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006570 if (HasXMMInt) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006571 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6572 // as to remove this logic from here, as much as possible
6573 if (NumElems == 2 || !X86::isMOVLMask(SVOp))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006574 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006575 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006576 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006577
6578 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6579
6580 // Invert the operand order and use SHUFPS to match it.
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006581 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006582 X86::getShuffleSHUFImmediate(SVOp), DAG);
6583}
6584
Craig Topper6347e862011-11-21 06:57:39 +00006585static inline unsigned getUNPCKLOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006586 switch(VT.getSimpleVT().SimpleTy) {
6587 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
6588 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006589 case MVT::v4f32: return X86ISD::UNPCKLPS;
6590 case MVT::v2f64: return X86ISD::UNPCKLPD;
Craig Topper6347e862011-11-21 06:57:39 +00006591 case MVT::v8i32:
6592 if (HasAVX2) return X86ISD::VPUNPCKLDQY;
6593 // else use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006594 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
Craig Topper6347e862011-11-21 06:57:39 +00006595 case MVT::v4i64:
6596 if (HasAVX2) return X86ISD::VPUNPCKLQDQY;
6597 // else use fp unit for int unpack.
David Greenec4db4e52011-02-28 19:06:56 +00006598 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006599 case MVT::v16i8: return X86ISD::PUNPCKLBW;
6600 case MVT::v8i16: return X86ISD::PUNPCKLWD;
Craig Topper6347e862011-11-21 06:57:39 +00006601 case MVT::v16i16: return X86ISD::VPUNPCKLWDY;
Craig Topper6fa583d2011-11-21 08:26:50 +00006602 case MVT::v32i8: return X86ISD::VPUNPCKLBWY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006603 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006604 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006605 }
6606 return 0;
6607}
6608
Craig Topper6347e862011-11-21 06:57:39 +00006609static inline unsigned getUNPCKHOpcode(EVT VT, bool HasAVX2) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006610 switch(VT.getSimpleVT().SimpleTy) {
6611 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
6612 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
6613 case MVT::v4f32: return X86ISD::UNPCKHPS;
6614 case MVT::v2f64: return X86ISD::UNPCKHPD;
Craig Topper6347e862011-11-21 06:57:39 +00006615 case MVT::v8i32:
6616 if (HasAVX2) return X86ISD::VPUNPCKHDQY;
6617 // else use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006618 case MVT::v8f32: return X86ISD::VUNPCKHPSY;
Craig Topper6347e862011-11-21 06:57:39 +00006619 case MVT::v4i64:
6620 if (HasAVX2) return X86ISD::VPUNPCKHQDQY;
6621 // else use fp unit for int unpack.
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00006622 case MVT::v4f64: return X86ISD::VUNPCKHPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006623 case MVT::v16i8: return X86ISD::PUNPCKHBW;
6624 case MVT::v8i16: return X86ISD::PUNPCKHWD;
Craig Topper6347e862011-11-21 06:57:39 +00006625 case MVT::v16i16: return X86ISD::VPUNPCKHWDY;
Craig Topper6fa583d2011-11-21 08:26:50 +00006626 case MVT::v32i8: return X86ISD::VPUNPCKHBWY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006627 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00006628 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00006629 }
6630 return 0;
6631}
6632
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006633static inline unsigned getVPERMILOpcode(EVT VT) {
6634 switch(VT.getSimpleVT().SimpleTy) {
6635 case MVT::v4i32:
6636 case MVT::v4f32: return X86ISD::VPERMILPS;
6637 case MVT::v2i64:
6638 case MVT::v2f64: return X86ISD::VPERMILPD;
6639 case MVT::v8i32:
6640 case MVT::v8f32: return X86ISD::VPERMILPSY;
6641 case MVT::v4i64:
6642 case MVT::v4f64: return X86ISD::VPERMILPDY;
6643 default:
6644 llvm_unreachable("Unknown type for vpermil");
6645 }
6646 return 0;
6647}
6648
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006649static
6650SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006651 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006652 const X86Subtarget *Subtarget) {
6653 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6654 EVT VT = Op.getValueType();
6655 DebugLoc dl = Op.getDebugLoc();
6656 SDValue V1 = Op.getOperand(0);
6657 SDValue V2 = Op.getOperand(1);
6658
6659 if (isZeroShuffle(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006660 return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006661
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006662 // Handle splat operations
6663 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006664 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006665 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006666 // Special case, this is the only place now where it's allowed to return
6667 // a vector_shuffle operation without using a target specific node, because
6668 // *hopefully* it will be optimized away by the dag combiner. FIXME: should
6669 // this be moved to DAGCombine instead?
6670 if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006671 return Op;
6672
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006673 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00006674 SDValue LD = isVectorBroadcast(Op, Subtarget->hasAVX2());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00006675 if (Subtarget->hasAVX() && LD.getNode())
6676 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, LD);
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006677
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006678 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006679 if ((Size == 128 && NumElem <= 4) ||
6680 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006681 return SDValue();
6682
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006683 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006684 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006685 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006686
6687 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6688 // do it!
6689 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
6690 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6691 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006692 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006693 } else if ((VT == MVT::v4i32 ||
6694 (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006695 // FIXME: Figure out a cleaner way to do this.
6696 // Try to make use of movq to zero out the top part.
6697 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6698 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6699 if (NewOp.getNode()) {
6700 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6701 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6702 DAG, Subtarget, dl);
6703 }
6704 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6705 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6706 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6707 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6708 DAG, Subtarget, dl);
6709 }
6710 }
6711 return SDValue();
6712}
6713
Dan Gohman475871a2008-07-27 21:46:04 +00006714SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006715X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006716 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SDValue V1 = Op.getOperand(0);
6718 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006719 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006720 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006721 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006722 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
6723 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006724 bool V1IsSplat = false;
6725 bool V2IsSplat = false;
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006726 bool HasXMMInt = Subtarget->hasXMMInt();
Craig Topper6347e862011-11-21 06:57:39 +00006727 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006728 MachineFunction &MF = DAG.getMachineFunction();
6729 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006730
Craig Topper3426a3e2011-11-14 06:46:21 +00006731 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006732
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006733 // Vector shuffle lowering takes 3 steps:
6734 //
6735 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6736 // narrowing and commutation of operands should be handled.
6737 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6738 // shuffle nodes.
6739 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6740 // so the shuffle can be broken into other shuffles and the legalizer can
6741 // try the lowering again.
6742 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006743 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006744 // be matched during isel, all of them must be converted to a target specific
6745 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006746
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006747 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6748 // narrowing and commutation of operands should be handled. The actual code
6749 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006750 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006751 if (NewOp.getNode())
6752 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006753
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006754 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6755 // unpckh_undef). Only use pshufd if speed is more important than size.
6756 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006757 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6758 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006759 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006760 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6761 DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006762
Craig Topperc0d82852011-11-22 00:44:41 +00006763 if (X86::isMOVDDUPMask(SVOp) && Subtarget->hasSSE3orAVX() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006764 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006765 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006766
Dale Johannesen0488fb62010-09-30 23:57:10 +00006767 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006768 return getMOVHighToLow(Op, dl, DAG);
6769
6770 // Use to match splats
Craig Topperc0d82852011-11-22 00:44:41 +00006771 if (HasXMMInt && X86::isUNPCKHMask(SVOp, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006772 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper6347e862011-11-21 06:57:39 +00006773 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6774 DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006775
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006776 if (X86::isPSHUFDMask(SVOp)) {
6777 // The actual implementation will match the mask in the if above and then
6778 // during isel it can match several different instructions, not only pshufd
6779 // as its name says, sad but true, emulate the behavior for now...
6780 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6781 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
6782
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006783 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
6784
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006785 if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006786 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6787
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006788 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
6789 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006790 }
Eric Christopherfd179292009-08-27 18:07:15 +00006791
Evan Chengf26ffe92008-05-29 08:22:04 +00006792 // Check if this can be converted into a logical shift.
6793 bool isLeft = false;
6794 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006795 SDValue ShVal;
Craig Topperc0d82852011-11-22 00:44:41 +00006796 bool isShift = HasXMMInt && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006797 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006798 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006799 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006800 EVT EltVT = VT.getVectorElementType();
6801 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006802 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006803 }
Eric Christopherfd179292009-08-27 18:07:15 +00006804
Nate Begeman9008ca62009-04-27 18:41:29 +00006805 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006806 if (V1IsUndef)
6807 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00006808 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006809 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00006810 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006811 if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006812 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6813
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006814 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006815 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6816 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006817 }
Eric Christopherfd179292009-08-27 18:07:15 +00006818
Nate Begeman9008ca62009-04-27 18:41:29 +00006819 // FIXME: fold these into legal mask.
Craig Topperc0d82852011-11-22 00:44:41 +00006820 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp, HasAVX2))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006821 return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006822
Dale Johannesen0488fb62010-09-30 23:57:10 +00006823 if (X86::isMOVHLPSMask(SVOp))
6824 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006825
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006826 if (X86::isMOVSHDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006827 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006828
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00006829 if (X86::isMOVSLDUPMask(SVOp, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006830 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006831
Dale Johannesen0488fb62010-09-30 23:57:10 +00006832 if (X86::isMOVLPMask(SVOp))
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006833 return getMOVLP(Op, dl, DAG, HasXMMInt);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006834
Nate Begeman9008ca62009-04-27 18:41:29 +00006835 if (ShouldXformToMOVHLPS(SVOp) ||
6836 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6837 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006838
Evan Chengf26ffe92008-05-29 08:22:04 +00006839 if (isShift) {
6840 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006841 EVT EltVT = VT.getVectorElementType();
6842 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006843 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006844 }
Eric Christopherfd179292009-08-27 18:07:15 +00006845
Evan Cheng9eca5e82006-10-25 21:49:50 +00006846 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006847 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6848 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006849 V1IsSplat = isSplatVector(V1.getNode());
6850 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006851
Chris Lattner8a594482007-11-25 00:24:49 +00006852 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00006853 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006854 Op = CommuteVectorShuffle(SVOp, DAG);
6855 SVOp = cast<ShuffleVectorSDNode>(Op);
6856 V1 = SVOp->getOperand(0);
6857 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006858 std::swap(V1IsSplat, V2IsSplat);
6859 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006860 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006861 }
6862
Nate Begeman9008ca62009-04-27 18:41:29 +00006863 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6864 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006865 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006866 return V1;
6867 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6868 // the instruction selector will not match, so get a canonical MOVL with
6869 // swapped operands to undo the commute.
6870 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006871 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006872
Craig Topperc0d82852011-11-22 00:44:41 +00006873 if (X86::isUNPCKLMask(SVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006874 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V2,
6875 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006876
Craig Topperc0d82852011-11-22 00:44:41 +00006877 if (X86::isUNPCKHMask(SVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006878 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V2,
6879 DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006880
Evan Cheng9bbbb982006-10-25 20:48:19 +00006881 if (V2IsSplat) {
6882 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006883 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00006884 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00006885 SDValue NewMask = NormalizeMask(SVOp, DAG);
6886 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6887 if (NSVOp != SVOp) {
Craig Topperc0d82852011-11-22 00:44:41 +00006888 if (X86::isUNPCKLMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006889 return NewMask;
Craig Topperc0d82852011-11-22 00:44:41 +00006890 } else if (X86::isUNPCKHMask(NSVOp, HasAVX2, true)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006891 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006892 }
6893 }
6894 }
6895
Evan Cheng9eca5e82006-10-25 21:49:50 +00006896 if (Commuted) {
6897 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006898 // FIXME: this seems wrong.
6899 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6900 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006901
Craig Topperc0d82852011-11-22 00:44:41 +00006902 if (X86::isUNPCKLMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006903 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V2, V1,
6904 DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006905
Craig Topperc0d82852011-11-22 00:44:41 +00006906 if (X86::isUNPCKHMask(NewSVOp, HasAVX2))
Craig Topper6347e862011-11-21 06:57:39 +00006907 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V2, V1,
6908 DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006909 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006910
Nate Begeman9008ca62009-04-27 18:41:29 +00006911 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00006912 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00006913 return CommuteVectorShuffle(SVOp, DAG);
6914
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006915 // The checks below are all present in isShuffleMaskLegal, but they are
6916 // inlined here right now to enable us to directly emit target specific
6917 // nodes, and remove one by one until they don't return Op anymore.
6918 SmallVector<int, 16> M;
6919 SVOp->getMask(M);
6920
Craig Topperc0d82852011-11-22 00:44:41 +00006921 if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006922 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
6923 X86::getShufflePALIGNRImmediate(SVOp),
6924 DAG);
6925
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006926 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6927 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00006928 if (VT == MVT::v2f64)
6929 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006930 if (VT == MVT::v2i64)
6931 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
6932 }
6933
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006934 if (isPSHUFHWMask(M, VT))
6935 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
6936 X86::getShufflePSHUFHWImmediate(SVOp),
6937 DAG);
6938
6939 if (isPSHUFLWMask(M, VT))
6940 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
6941 X86::getShufflePSHUFLWImmediate(SVOp),
6942 DAG);
6943
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006944 if (isSHUFPMask(M, VT))
6945 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6946 X86::getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006947
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006948 if (X86::isUNPCKL_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006949 return getTargetShuffleNode(getUNPCKLOpcode(VT, HasAVX2), dl, VT, V1, V1,
6950 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006951 if (X86::isUNPCKH_v_undef_Mask(SVOp))
Craig Topper6347e862011-11-21 06:57:39 +00006952 return getTargetShuffleNode(getUNPCKHOpcode(VT, HasAVX2), dl, VT, V1, V1,
6953 DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006954
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006955 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006956 // Generate target specific nodes for 128 or 256-bit shuffles only
6957 // supported in the AVX instruction set.
6958 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006959
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006960 // Handle VMOVDDUPY permutations
6961 if (isMOVDDUPYMask(SVOp, Subtarget))
6962 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6963
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006964 // Handle VPERMILPS* permutations
6965 if (isVPERMILPSMask(M, VT, Subtarget))
6966 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6967 getShuffleVPERMILPSImmediate(SVOp), DAG);
6968
6969 // Handle VPERMILPD* permutations
6970 if (isVPERMILPDMask(M, VT, Subtarget))
6971 return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
6972 getShuffleVPERMILPDImmediate(SVOp), DAG);
6973
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00006974 // Handle VPERM2F128 permutations
6975 if (isVPERM2F128Mask(M, VT, Subtarget))
6976 return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
6977 getShuffleVPERM2F128Immediate(SVOp), DAG);
6978
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006979 // Handle VSHUFPSY permutations
6980 if (isVSHUFPSYMask(M, VT, Subtarget))
6981 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6982 getShuffleVSHUFPSYImmediate(SVOp), DAG);
6983
6984 // Handle VSHUFPDY permutations
6985 if (isVSHUFPDYMask(M, VT, Subtarget))
6986 return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
6987 getShuffleVSHUFPDYImmediate(SVOp), DAG);
6988
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006989 //===--------------------------------------------------------------------===//
6990 // Since no target specific shuffle was selected for this generic one,
6991 // lower it into other known shuffles. FIXME: this isn't true yet, but
6992 // this is the plan.
6993 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006994
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006995 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6996 if (VT == MVT::v8i16) {
6997 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6998 if (NewOp.getNode())
6999 return NewOp;
7000 }
7001
7002 if (VT == MVT::v16i8) {
7003 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7004 if (NewOp.getNode())
7005 return NewOp;
7006 }
7007
7008 // Handle all 128-bit wide vectors with 4 elements, and match them with
7009 // several different shuffle types.
7010 if (NumElems == 4 && VT.getSizeInBits() == 128)
7011 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7012
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007013 // Handle general 256-bit shuffles
7014 if (VT.is256BitVector())
7015 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7016
Dan Gohman475871a2008-07-27 21:46:04 +00007017 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007018}
7019
Dan Gohman475871a2008-07-27 21:46:04 +00007020SDValue
7021X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007022 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007023 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007024 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007025
7026 if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
7027 return SDValue();
7028
Duncan Sands83ec4b62008-06-06 12:08:01 +00007029 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007030 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007031 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007032 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007033 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007034 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007035 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007036 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7037 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7038 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007039 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7040 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007041 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007042 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007043 Op.getOperand(0)),
7044 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007046 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007047 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00007048 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007049 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007051 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7052 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007053 // result has a single use which is a store or a bitcast to i32. And in
7054 // the case of a store, it's not worth it if the index is a constant 0,
7055 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007056 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007057 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007058 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007059 if ((User->getOpcode() != ISD::STORE ||
7060 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7061 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007062 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007064 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007066 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007067 Op.getOperand(0)),
7068 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007069 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Pete Coopera77214a2011-11-14 19:38:42 +00007070 } else if (VT == MVT::i32 || VT == MVT::i64) {
7071 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007072 if (isa<ConstantSDNode>(Op.getOperand(1)))
7073 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007074 }
Dan Gohman475871a2008-07-27 21:46:04 +00007075 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007076}
7077
7078
Dan Gohman475871a2008-07-27 21:46:04 +00007079SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007080X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7081 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007082 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007083 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007084
David Greene74a579d2011-02-10 16:57:36 +00007085 SDValue Vec = Op.getOperand(0);
7086 EVT VecVT = Vec.getValueType();
7087
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007088 // If this is a 256-bit vector result, first extract the 128-bit vector and
7089 // then extract the element from the 128-bit vector.
7090 if (VecVT.getSizeInBits() == 256) {
David Greene74a579d2011-02-10 16:57:36 +00007091 DebugLoc dl = Op.getNode()->getDebugLoc();
7092 unsigned NumElems = VecVT.getVectorNumElements();
7093 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007094 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7095
7096 // Get the 128-bit vector.
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007097 bool Upper = IdxVal >= NumElems/2;
7098 Vec = Extract128BitVector(Vec,
7099 DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007100
David Greene74a579d2011-02-10 16:57:36 +00007101 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007102 Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
David Greene74a579d2011-02-10 16:57:36 +00007103 }
7104
7105 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
7106
Craig Topperc0d82852011-11-22 00:44:41 +00007107 if (Subtarget->hasSSE41orAVX()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007109 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007110 return Res;
7111 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007112
Owen Andersone50ed302009-08-10 22:56:29 +00007113 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007114 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007115 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007116 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007117 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007118 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007119 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007120 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7121 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007122 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007123 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007124 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007125 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007126 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007127 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007128 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007129 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007130 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007131 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007132 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007133 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007134 if (Idx == 0)
7135 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007136
Evan Cheng0db9fe62006-04-25 20:13:52 +00007137 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007138 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007139 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007140 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007141 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007142 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007143 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00007144 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007145 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7146 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7147 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007148 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149 if (Idx == 0)
7150 return Op;
7151
7152 // UNPCKHPD the element to the lowest double word, then movsd.
7153 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7154 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007155 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007156 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007157 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007158 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007159 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007160 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007161 }
7162
Dan Gohman475871a2008-07-27 21:46:04 +00007163 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007164}
7165
Dan Gohman475871a2008-07-27 21:46:04 +00007166SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007167X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7168 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007169 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007170 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007171 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007172
Dan Gohman475871a2008-07-27 21:46:04 +00007173 SDValue N0 = Op.getOperand(0);
7174 SDValue N1 = Op.getOperand(1);
7175 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007176
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007177 if (VT.getSizeInBits() == 256)
7178 return SDValue();
7179
Dan Gohman8a55ce42009-09-23 21:02:20 +00007180 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007181 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007182 unsigned Opc;
7183 if (VT == MVT::v8i16)
7184 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007185 else if (VT == MVT::v16i8)
7186 Opc = X86ISD::PINSRB;
7187 else
7188 Opc = X86ISD::PINSRB;
7189
Nate Begeman14d12ca2008-02-11 04:19:36 +00007190 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7191 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007192 if (N1.getValueType() != MVT::i32)
7193 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7194 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007195 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007196 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00007197 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007198 // Bits [7:6] of the constant are the source select. This will always be
7199 // zero here. The DAG Combiner may combine an extract_elt index into these
7200 // bits. For example (insert (extract, 3), 2) could be matched by putting
7201 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007202 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007203 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007204 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007205 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007206 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007207 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007208 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007209 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Pete Coopera77214a2011-11-14 19:38:42 +00007210 } else if ((EltVT == MVT::i32 || EltVT == MVT::i64) &&
7211 isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007212 // PINSR* works with constant index.
7213 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007214 }
Dan Gohman475871a2008-07-27 21:46:04 +00007215 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007216}
7217
Dan Gohman475871a2008-07-27 21:46:04 +00007218SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007219X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007220 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007221 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007222
David Greene6b381262011-02-09 15:32:06 +00007223 DebugLoc dl = Op.getDebugLoc();
7224 SDValue N0 = Op.getOperand(0);
7225 SDValue N1 = Op.getOperand(1);
7226 SDValue N2 = Op.getOperand(2);
7227
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007228 // If this is a 256-bit vector result, first extract the 128-bit vector,
7229 // insert the element into the extracted half and then place it back.
7230 if (VT.getSizeInBits() == 256) {
David Greene6b381262011-02-09 15:32:06 +00007231 if (!isa<ConstantSDNode>(N2))
7232 return SDValue();
7233
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007234 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007235 unsigned NumElems = VT.getVectorNumElements();
7236 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007237 bool Upper = IdxVal >= NumElems/2;
7238 SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
7239 SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007240
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007241 // Insert the element into the desired half.
7242 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
7243 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
David Greene6b381262011-02-09 15:32:06 +00007244
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007245 // Insert the changed part back to the 256-bit vector
7246 return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007247 }
7248
Craig Topperc0d82852011-11-22 00:44:41 +00007249 if (Subtarget->hasSSE41orAVX())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007250 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7251
Dan Gohman8a55ce42009-09-23 21:02:20 +00007252 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007253 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007254
Dan Gohman8a55ce42009-09-23 21:02:20 +00007255 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007256 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7257 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007258 if (N1.getValueType() != MVT::i32)
7259 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7260 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007261 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007262 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007263 }
Dan Gohman475871a2008-07-27 21:46:04 +00007264 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007265}
7266
Dan Gohman475871a2008-07-27 21:46:04 +00007267SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007268X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007269 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007270 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007271 EVT OpVT = Op.getValueType();
7272
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007273 // If this is a 256-bit vector result, first insert into a 128-bit
7274 // vector and then insert into the 256-bit vector.
7275 if (OpVT.getSizeInBits() > 128) {
7276 // Insert into a 128-bit vector.
7277 EVT VT128 = EVT::getVectorVT(*Context,
7278 OpVT.getVectorElementType(),
7279 OpVT.getVectorNumElements() / 2);
7280
7281 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7282
7283 // Insert the 128-bit vector.
7284 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
7285 DAG.getConstant(0, MVT::i32),
7286 DAG, dl);
7287 }
7288
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007289 if (Op.getValueType() == MVT::v1i64 &&
7290 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007291 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007292
Owen Anderson825b72b2009-08-11 20:47:22 +00007293 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00007294 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
7295 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007296 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00007297 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007298}
7299
David Greene91585092011-01-26 15:38:49 +00007300// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7301// a simple subregister reference or explicit instructions to grab
7302// upper bits of a vector.
7303SDValue
7304X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7305 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007306 DebugLoc dl = Op.getNode()->getDebugLoc();
7307 SDValue Vec = Op.getNode()->getOperand(0);
7308 SDValue Idx = Op.getNode()->getOperand(1);
7309
7310 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
7311 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
7312 return Extract128BitVector(Vec, Idx, DAG, dl);
7313 }
David Greene91585092011-01-26 15:38:49 +00007314 }
7315 return SDValue();
7316}
7317
David Greenecfe33c42011-01-26 19:13:22 +00007318// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7319// simple superregister reference or explicit instructions to insert
7320// the upper bits of a vector.
7321SDValue
7322X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7323 if (Subtarget->hasAVX()) {
7324 DebugLoc dl = Op.getNode()->getDebugLoc();
7325 SDValue Vec = Op.getNode()->getOperand(0);
7326 SDValue SubVec = Op.getNode()->getOperand(1);
7327 SDValue Idx = Op.getNode()->getOperand(2);
7328
7329 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
7330 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00007331 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007332 }
7333 }
7334 return SDValue();
7335}
7336
Bill Wendling056292f2008-09-16 21:48:12 +00007337// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7338// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7339// one of the above mentioned nodes. It has to be wrapped because otherwise
7340// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7341// be used to form addressing mode. These wrapped nodes will be selected
7342// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007343SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007344X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007345 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007346
Chris Lattner41621a22009-06-26 19:22:52 +00007347 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7348 // global base reg.
7349 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007350 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007351 CodeModel::Model M = getTargetMachine().getCodeModel();
7352
Chris Lattner4f066492009-07-11 20:29:19 +00007353 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007354 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007355 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007356 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007357 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007358 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007359 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007360
Evan Cheng1606e8e2009-03-13 07:51:59 +00007361 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007362 CP->getAlignment(),
7363 CP->getOffset(), OpFlag);
7364 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007365 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007366 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007367 if (OpFlag) {
7368 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007369 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007370 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007371 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007372 }
7373
7374 return Result;
7375}
7376
Dan Gohmand858e902010-04-17 15:26:15 +00007377SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007378 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007379
Chris Lattner18c59872009-06-27 04:16:01 +00007380 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7381 // global base reg.
7382 unsigned char OpFlag = 0;
7383 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007384 CodeModel::Model M = getTargetMachine().getCodeModel();
7385
Chris Lattner4f066492009-07-11 20:29:19 +00007386 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007387 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007388 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007389 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007390 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007391 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007392 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007393
Chris Lattner18c59872009-06-27 04:16:01 +00007394 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7395 OpFlag);
7396 DebugLoc DL = JT->getDebugLoc();
7397 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007398
Chris Lattner18c59872009-06-27 04:16:01 +00007399 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007400 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007401 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7402 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007403 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007404 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007405
Chris Lattner18c59872009-06-27 04:16:01 +00007406 return Result;
7407}
7408
7409SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007410X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007411 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007412
Chris Lattner18c59872009-06-27 04:16:01 +00007413 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7414 // global base reg.
7415 unsigned char OpFlag = 0;
7416 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007417 CodeModel::Model M = getTargetMachine().getCodeModel();
7418
Chris Lattner4f066492009-07-11 20:29:19 +00007419 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007420 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7421 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7422 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007423 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007424 } else if (Subtarget->isPICStyleGOT()) {
7425 OpFlag = X86II::MO_GOT;
7426 } else if (Subtarget->isPICStyleStubPIC()) {
7427 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7428 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7429 OpFlag = X86II::MO_DARWIN_NONLAZY;
7430 }
Eric Christopherfd179292009-08-27 18:07:15 +00007431
Chris Lattner18c59872009-06-27 04:16:01 +00007432 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007433
Chris Lattner18c59872009-06-27 04:16:01 +00007434 DebugLoc DL = Op.getDebugLoc();
7435 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007436
7437
Chris Lattner18c59872009-06-27 04:16:01 +00007438 // With PIC, the address is actually $g + Offset.
7439 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007440 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007441 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7442 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007443 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007444 Result);
7445 }
Eric Christopherfd179292009-08-27 18:07:15 +00007446
Eli Friedman586272d2011-08-11 01:48:05 +00007447 // For symbols that require a load from a stub to get the address, emit the
7448 // load.
7449 if (isGlobalStubReference(OpFlag))
7450 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007451 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007452
Chris Lattner18c59872009-06-27 04:16:01 +00007453 return Result;
7454}
7455
Dan Gohman475871a2008-07-27 21:46:04 +00007456SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007457X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007458 // Create the TargetBlockAddressAddress node.
7459 unsigned char OpFlags =
7460 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007461 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007462 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007463 DebugLoc dl = Op.getDebugLoc();
7464 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7465 /*isTarget=*/true, OpFlags);
7466
Dan Gohmanf705adb2009-10-30 01:28:02 +00007467 if (Subtarget->isPICStyleRIPRel() &&
7468 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007469 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7470 else
7471 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007472
Dan Gohman29cbade2009-11-20 23:18:13 +00007473 // With PIC, the address is actually $g + Offset.
7474 if (isGlobalRelativeToPICBase(OpFlags)) {
7475 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7476 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7477 Result);
7478 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007479
7480 return Result;
7481}
7482
7483SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007484X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007485 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007486 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007487 // Create the TargetGlobalAddress node, folding in the constant
7488 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007489 unsigned char OpFlags =
7490 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007491 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007492 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007493 if (OpFlags == X86II::MO_NO_FLAG &&
7494 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007495 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007496 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007497 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007498 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007499 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007500 }
Eric Christopherfd179292009-08-27 18:07:15 +00007501
Chris Lattner4f066492009-07-11 20:29:19 +00007502 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007503 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007504 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7505 else
7506 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007507
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007508 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007509 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007510 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7511 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007512 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007513 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007514
Chris Lattner36c25012009-07-10 07:34:39 +00007515 // For globals that require a load from a stub to get the address, emit the
7516 // load.
7517 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007518 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007519 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007520
Dan Gohman6520e202008-10-18 02:06:02 +00007521 // If there was a non-zero offset that we didn't fold, create an explicit
7522 // addition for it.
7523 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007524 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007525 DAG.getConstant(Offset, getPointerTy()));
7526
Evan Cheng0db9fe62006-04-25 20:13:52 +00007527 return Result;
7528}
7529
Evan Chengda43bcf2008-09-24 00:05:32 +00007530SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007531X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007532 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007533 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007534 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007535}
7536
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007537static SDValue
7538GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007539 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00007540 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007541 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007542 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007543 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007544 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007545 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007546 GA->getOffset(),
7547 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007548 if (InFlag) {
7549 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007550 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007551 } else {
7552 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00007553 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007554 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007555
7556 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007557 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007558
Rafael Espindola15f1b662009-04-24 12:59:40 +00007559 SDValue Flag = Chain.getValue(1);
7560 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007561}
7562
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007563// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007564static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007565LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007566 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007567 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007568 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7569 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007570 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007571 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007572 InFlag = Chain.getValue(1);
7573
Chris Lattnerb903bed2009-06-26 21:20:29 +00007574 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007575}
7576
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007577// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007578static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007579LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007580 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007581 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7582 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007583}
7584
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007585// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7586// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007587static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007588 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00007589 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007590 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007591
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007592 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7593 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7594 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007595
Michael J. Spencerec38de22010-10-10 22:04:20 +00007596 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007597 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007598 MachinePointerInfo(Ptr),
7599 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007600
Chris Lattnerb903bed2009-06-26 21:20:29 +00007601 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007602 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7603 // initialexec.
7604 unsigned WrapperKind = X86ISD::Wrapper;
7605 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007606 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00007607 } else if (is64Bit) {
7608 assert(model == TLSModel::InitialExec);
7609 OperandFlags = X86II::MO_GOTTPOFF;
7610 WrapperKind = X86ISD::WrapperRIP;
7611 } else {
7612 assert(model == TLSModel::InitialExec);
7613 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00007614 }
Eric Christopherfd179292009-08-27 18:07:15 +00007615
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007616 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7617 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007618 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007619 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007620 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007621 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007622
Rafael Espindola9a580232009-02-27 13:37:18 +00007623 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007624 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007625 MachinePointerInfo::getGOT(), false, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007626
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007627 // The address of the thread local variable is the add of the thread
7628 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007629 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007630}
7631
Dan Gohman475871a2008-07-27 21:46:04 +00007632SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007633X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007634
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007635 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007636 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007637
Eric Christopher30ef0e52010-06-03 04:07:48 +00007638 if (Subtarget->isTargetELF()) {
7639 // TODO: implement the "local dynamic" model
7640 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00007641
Eric Christopher30ef0e52010-06-03 04:07:48 +00007642 // If GV is an alias then use the aliasee for determining
7643 // thread-localness.
7644 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7645 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007646
7647 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00007648 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007649
Eric Christopher30ef0e52010-06-03 04:07:48 +00007650 switch (model) {
7651 case TLSModel::GeneralDynamic:
7652 case TLSModel::LocalDynamic: // not implemented
7653 if (Subtarget->is64Bit())
7654 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7655 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00007656
Eric Christopher30ef0e52010-06-03 04:07:48 +00007657 case TLSModel::InitialExec:
7658 case TLSModel::LocalExec:
7659 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
7660 Subtarget->is64Bit());
7661 }
7662 } else if (Subtarget->isTargetDarwin()) {
7663 // Darwin only has one model of TLS. Lower to that.
7664 unsigned char OpFlag = 0;
7665 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7666 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007667
Eric Christopher30ef0e52010-06-03 04:07:48 +00007668 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7669 // global base reg.
7670 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7671 !Subtarget->is64Bit();
7672 if (PIC32)
7673 OpFlag = X86II::MO_TLVP_PIC_BASE;
7674 else
7675 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007676 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007677 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007678 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007679 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007680 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007681
Eric Christopher30ef0e52010-06-03 04:07:48 +00007682 // With PIC32, the address is actually $g + Offset.
7683 if (PIC32)
7684 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7685 DAG.getNode(X86ISD::GlobalBaseReg,
7686 DebugLoc(), getPointerTy()),
7687 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007688
Eric Christopher30ef0e52010-06-03 04:07:48 +00007689 // Lowering the machine isd will make sure everything is in the right
7690 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007691 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007692 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007693 SDValue Args[] = { Chain, Offset };
7694 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007695
Eric Christopher30ef0e52010-06-03 04:07:48 +00007696 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7697 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7698 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007699
Eric Christopher30ef0e52010-06-03 04:07:48 +00007700 // And our return value (tls address) is in the standard call return value
7701 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007702 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007703 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7704 Chain.getValue(1));
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007705 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007706
Eric Christopher30ef0e52010-06-03 04:07:48 +00007707 assert(false &&
7708 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00007709
Torok Edwinc23197a2009-07-14 16:55:14 +00007710 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00007711 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007712}
7713
Evan Cheng0db9fe62006-04-25 20:13:52 +00007714
Nadav Rotem43012222011-05-11 08:12:09 +00007715/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00007716/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00007717SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00007718 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007719 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007720 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007721 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007722 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007723 SDValue ShOpLo = Op.getOperand(0);
7724 SDValue ShOpHi = Op.getOperand(1);
7725 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007726 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007728 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007729
Dan Gohman475871a2008-07-27 21:46:04 +00007730 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007731 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007732 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7733 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007734 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007735 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7736 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007737 }
Evan Chenge3413162006-01-09 18:33:28 +00007738
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7740 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007741 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007743
Dan Gohman475871a2008-07-27 21:46:04 +00007744 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007745 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007746 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7747 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007748
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007749 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007750 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7751 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007752 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007753 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7754 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007755 }
7756
Dan Gohman475871a2008-07-27 21:46:04 +00007757 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007758 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007759}
Evan Chenga3195e82006-01-12 22:54:21 +00007760
Dan Gohmand858e902010-04-17 15:26:15 +00007761SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7762 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007763 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007764
Dale Johannesen0488fb62010-09-30 23:57:10 +00007765 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007766 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007767
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007769 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007770
Eli Friedman36df4992009-05-27 00:47:34 +00007771 // These are really Legal; return the operand so the caller accepts it as
7772 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007774 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007775 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007776 Subtarget->is64Bit()) {
7777 return Op;
7778 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007779
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007780 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007781 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007782 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007783 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007784 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007785 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007786 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007787 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007788 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007789 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7790}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007791
Owen Andersone50ed302009-08-10 22:56:29 +00007792SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007793 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007794 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007795 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007796 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007797 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007798 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007799 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007800 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007801 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007802 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007803
Chris Lattner492a43e2010-09-22 01:28:21 +00007804 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007805
Stuart Hastings84be9582011-06-02 15:57:11 +00007806 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7807 MachineMemOperand *MMO;
7808 if (FI) {
7809 int SSFI = FI->getIndex();
7810 MMO =
7811 DAG.getMachineFunction()
7812 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7813 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7814 } else {
7815 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7816 StackSlot = StackSlot.getOperand(1);
7817 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007818 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007819 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7820 X86ISD::FILD, DL,
7821 Tys, Ops, array_lengthof(Ops),
7822 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007823
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007824 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007825 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007826 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007827
7828 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7829 // shouldn't be necessary except that RFP cannot be live across
7830 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007831 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007832 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7833 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007834 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007835 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007836 SDValue Ops[] = {
7837 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7838 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007839 MachineMemOperand *MMO =
7840 DAG.getMachineFunction()
7841 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007842 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007843
Chris Lattner492a43e2010-09-22 01:28:21 +00007844 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7845 Ops, array_lengthof(Ops),
7846 Op.getValueType(), MMO);
7847 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007848 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007849 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007850 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007851
Evan Cheng0db9fe62006-04-25 20:13:52 +00007852 return Result;
7853}
7854
Bill Wendling8b8a6362009-01-17 03:56:04 +00007855// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007856SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7857 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00007858 // This algorithm is not obvious. Here it is in C code, more or less:
7859 /*
7860 double uint64_to_double( uint32_t hi, uint32_t lo ) {
7861 static const __m128i exp = { 0x4330000045300000ULL, 0 };
7862 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00007863
Bill Wendling8b8a6362009-01-17 03:56:04 +00007864 // Copy ints to xmm registers.
7865 __m128i xh = _mm_cvtsi32_si128( hi );
7866 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00007867
Bill Wendling8b8a6362009-01-17 03:56:04 +00007868 // Combine into low half of a single xmm register.
7869 __m128i x = _mm_unpacklo_epi32( xh, xl );
7870 __m128d d;
7871 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00007872
Bill Wendling8b8a6362009-01-17 03:56:04 +00007873 // Merge in appropriate exponents to give the integer bits the right
7874 // magnitude.
7875 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00007876
Bill Wendling8b8a6362009-01-17 03:56:04 +00007877 // Subtract away the biases to deal with the IEEE-754 double precision
7878 // implicit 1.
7879 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00007880
Bill Wendling8b8a6362009-01-17 03:56:04 +00007881 // All conversions up to here are exact. The correctly rounded result is
7882 // calculated using the current rounding mode using the following
7883 // horizontal add.
7884 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7885 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
7886 // store doesn't really need to be here (except
7887 // maybe to zero the other double)
7888 return sd;
7889 }
7890 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007891
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007892 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007893 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007894
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007895 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00007896 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00007897 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7898 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7899 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7900 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007901 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007902 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007903
Bill Wendling8b8a6362009-01-17 03:56:04 +00007904 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00007905 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007906 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00007907 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007908 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00007909 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007910 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007911
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7913 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007914 Op.getOperand(0),
7915 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7917 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00007918 Op.getOperand(0),
7919 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007920 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7921 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007922 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007923 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007925 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007927 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007928 false, false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007930
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007931 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00007932 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00007933 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7934 DAG.getUNDEF(MVT::v2f64), ShufMask);
7935 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7936 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007937 DAG.getIntPtrConstant(0));
7938}
7939
Bill Wendling8b8a6362009-01-17 03:56:04 +00007940// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007941SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7942 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007943 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007944 // FP constant to bias correct the final result.
7945 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007947
7948 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007949 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007950 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007951
Eli Friedmanf3704762011-08-29 21:15:46 +00007952 // Zero out the upper parts of the register.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007953 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
7954 DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007955
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007957 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007958 DAG.getIntPtrConstant(0));
7959
7960 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007962 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007963 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007965 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007966 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007967 MVT::v2f64, Bias)));
7968 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007969 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007970 DAG.getIntPtrConstant(0));
7971
7972 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007973 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007974
7975 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007976 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007977
Owen Anderson825b72b2009-08-11 20:47:22 +00007978 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007979 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007980 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007981 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007982 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007983 }
7984
7985 // Handle final rounding.
7986 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007987}
7988
Dan Gohmand858e902010-04-17 15:26:15 +00007989SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7990 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007991 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007992 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007993
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007994 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007995 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7996 // the optimization here.
7997 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007998 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007999
Owen Andersone50ed302009-08-10 22:56:29 +00008000 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008001 EVT DstVT = Op.getValueType();
8002 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008003 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008004 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008005 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008006
8007 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008008 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008009 if (SrcVT == MVT::i32) {
8010 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8011 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8012 getPointerTy(), StackSlot, WordOff);
8013 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008014 StackSlot, MachinePointerInfo(),
8015 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008016 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008017 OffsetSlot, MachinePointerInfo(),
8018 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008019 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8020 return Fild;
8021 }
8022
8023 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8024 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008025 StackSlot, MachinePointerInfo(),
8026 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008027 // For i64 source, we need to add the appropriate power of 2 if the input
8028 // was negative. This is the same as the optimization in
8029 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8030 // we must be careful to do the computation in x87 extended precision, not
8031 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008032 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8033 MachineMemOperand *MMO =
8034 DAG.getMachineFunction()
8035 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8036 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008037
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008038 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8039 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008040 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8041 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008042
8043 APInt FF(32, 0x5F800000ULL);
8044
8045 // Check whether the sign bit is set.
8046 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8047 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8048 ISD::SETLT);
8049
8050 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8051 SDValue FudgePtr = DAG.getConstantPool(
8052 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8053 getPointerTy());
8054
8055 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8056 SDValue Zero = DAG.getIntPtrConstant(0);
8057 SDValue Four = DAG.getIntPtrConstant(4);
8058 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8059 Zero, Four);
8060 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8061
8062 // Load the value out, extending it from f32 to f80.
8063 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008064 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008065 FudgePtr, MachinePointerInfo::getConstantPool(),
8066 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008067 // Extend everything to 80 bits to force it to be done on x87.
8068 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8069 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008070}
8071
Dan Gohman475871a2008-07-27 21:46:04 +00008072std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00008073FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00008074 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008075
Owen Andersone50ed302009-08-10 22:56:29 +00008076 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008077
8078 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8080 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008081 }
8082
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8084 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00008085 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008086
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008087 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008089 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008090 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008091 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008092 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008093 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008094 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008095
Evan Cheng87c89352007-10-15 20:11:21 +00008096 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
8097 // stack slot.
8098 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008099 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008100 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008101 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008102
Michael J. Spencerec38de22010-10-10 22:04:20 +00008103
8104
Evan Cheng0db9fe62006-04-25 20:13:52 +00008105 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008107 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008108 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8109 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8110 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008112
Dan Gohman475871a2008-07-27 21:46:04 +00008113 SDValue Chain = DAG.getEntryNode();
8114 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008115 EVT TheVT = Op.getOperand(0).getValueType();
8116 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008117 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008118 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008119 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008120 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008121 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008122 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008123 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008124 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008125
Chris Lattner492a43e2010-09-22 01:28:21 +00008126 MachineMemOperand *MMO =
8127 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8128 MachineMemOperand::MOLoad, MemSize, MemSize);
8129 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8130 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008131 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008132 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008133 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8134 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008135
Chris Lattner07290932010-09-22 01:05:16 +00008136 MachineMemOperand *MMO =
8137 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8138 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008139
Evan Cheng0db9fe62006-04-25 20:13:52 +00008140 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00008141 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00008142 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8143 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00008144
Chris Lattner27a6c732007-11-24 07:07:01 +00008145 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008146}
8147
Dan Gohmand858e902010-04-17 15:26:15 +00008148SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8149 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008150 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008151 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008152
Eli Friedman948e95a2009-05-23 09:59:16 +00008153 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00008154 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008155 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8156 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008157
Chris Lattner27a6c732007-11-24 07:07:01 +00008158 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008159 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008160 FIST, StackSlot, MachinePointerInfo(),
8161 false, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00008162}
8163
Dan Gohmand858e902010-04-17 15:26:15 +00008164SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8165 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00008166 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
8167 SDValue FIST = Vals.first, StackSlot = Vals.second;
8168 assert(FIST.getNode() && "Unexpected failure");
8169
8170 // Load the result.
8171 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008172 FIST, StackSlot, MachinePointerInfo(),
8173 false, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008174}
8175
Dan Gohmand858e902010-04-17 15:26:15 +00008176SDValue X86TargetLowering::LowerFABS(SDValue Op,
8177 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008178 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008179 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008180 EVT VT = Op.getValueType();
8181 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008182 if (VT.isVector())
8183 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008184 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008185 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008186 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00008187 CV.push_back(C);
8188 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008189 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008190 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00008191 CV.push_back(C);
8192 CV.push_back(C);
8193 CV.push_back(C);
8194 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008195 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008196 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008197 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008198 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008199 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008200 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008201 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008202}
8203
Dan Gohmand858e902010-04-17 15:26:15 +00008204SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008205 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008206 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008207 EVT VT = Op.getValueType();
8208 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00008209 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008210 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008211 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008212 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008213 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00008214 CV.push_back(C);
8215 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008216 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008217 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00008218 CV.push_back(C);
8219 CV.push_back(C);
8220 CV.push_back(C);
8221 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008222 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008223 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008224 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008225 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008226 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008227 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008228 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008229 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008230 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008231 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008232 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008233 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008234 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008235 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00008236 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008237}
8238
Dan Gohmand858e902010-04-17 15:26:15 +00008239SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008240 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008241 SDValue Op0 = Op.getOperand(0);
8242 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008243 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008244 EVT VT = Op.getValueType();
8245 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008246
8247 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008248 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008249 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008250 SrcVT = VT;
8251 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008252 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008253 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008254 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008255 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008256 }
8257
8258 // At this point the operands and the result should have the same
8259 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008260
Evan Cheng68c47cb2007-01-05 07:55:56 +00008261 // First get the sign bit of second operand.
8262 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008264 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8265 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008266 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008267 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8268 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8269 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8270 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008271 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008272 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008273 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008274 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008275 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008276 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008277 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008278
8279 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008280 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008281 // Op0 is MVT::f32, Op1 is MVT::f64.
8282 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8283 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8284 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008285 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008287 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008288 }
8289
Evan Cheng73d6cf12007-01-05 21:37:56 +00008290 // Clear first operand sign bit.
8291 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008292 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008293 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8294 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008295 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008296 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8297 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8298 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8299 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008300 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008301 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008302 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008303 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008304 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008305 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008306 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008307
8308 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008309 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008310}
8311
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008312SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8313 SDValue N0 = Op.getOperand(0);
8314 DebugLoc dl = Op.getDebugLoc();
8315 EVT VT = Op.getValueType();
8316
8317 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8318 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8319 DAG.getConstant(1, VT));
8320 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8321}
8322
Dan Gohman076aee32009-03-04 19:44:21 +00008323/// Emit nodes that will be selected as "test Op0,Op0", or something
8324/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008325SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008326 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008327 DebugLoc dl = Op.getDebugLoc();
8328
Dan Gohman31125812009-03-07 01:58:32 +00008329 // CF and OF aren't always set the way we want. Determine which
8330 // of these we need.
8331 bool NeedCF = false;
8332 bool NeedOF = false;
8333 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008334 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008335 case X86::COND_A: case X86::COND_AE:
8336 case X86::COND_B: case X86::COND_BE:
8337 NeedCF = true;
8338 break;
8339 case X86::COND_G: case X86::COND_GE:
8340 case X86::COND_L: case X86::COND_LE:
8341 case X86::COND_O: case X86::COND_NO:
8342 NeedOF = true;
8343 break;
Dan Gohman31125812009-03-07 01:58:32 +00008344 }
8345
Dan Gohman076aee32009-03-04 19:44:21 +00008346 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008347 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8348 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008349 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8350 // Emit a CMP with 0, which is the TEST pattern.
8351 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8352 DAG.getConstant(0, Op.getValueType()));
8353
8354 unsigned Opcode = 0;
8355 unsigned NumOperands = 0;
8356 switch (Op.getNode()->getOpcode()) {
8357 case ISD::ADD:
8358 // Due to an isel shortcoming, be conservative if this add is likely to be
8359 // selected as part of a load-modify-store instruction. When the root node
8360 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8361 // uses of other nodes in the match, such as the ADD in this case. This
8362 // leads to the ADD being left around and reselected, with the result being
8363 // two adds in the output. Alas, even if none our users are stores, that
8364 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8365 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8366 // climbing the DAG back to the root, and it doesn't seem to be worth the
8367 // effort.
8368 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008369 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8370 if (UI->getOpcode() != ISD::CopyToReg &&
8371 UI->getOpcode() != ISD::SETCC &&
8372 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008373 goto default_case;
8374
8375 if (ConstantSDNode *C =
8376 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8377 // An add of one will be selected as an INC.
8378 if (C->getAPIntValue() == 1) {
8379 Opcode = X86ISD::INC;
8380 NumOperands = 1;
8381 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008382 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008383
8384 // An add of negative one (subtract of one) will be selected as a DEC.
8385 if (C->getAPIntValue().isAllOnesValue()) {
8386 Opcode = X86ISD::DEC;
8387 NumOperands = 1;
8388 break;
8389 }
Dan Gohman076aee32009-03-04 19:44:21 +00008390 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008391
8392 // Otherwise use a regular EFLAGS-setting add.
8393 Opcode = X86ISD::ADD;
8394 NumOperands = 2;
8395 break;
8396 case ISD::AND: {
8397 // If the primary and result isn't used, don't bother using X86ISD::AND,
8398 // because a TEST instruction will be better.
8399 bool NonFlagUse = false;
8400 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8401 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8402 SDNode *User = *UI;
8403 unsigned UOpNo = UI.getOperandNo();
8404 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8405 // Look pass truncate.
8406 UOpNo = User->use_begin().getOperandNo();
8407 User = *User->use_begin();
8408 }
8409
8410 if (User->getOpcode() != ISD::BRCOND &&
8411 User->getOpcode() != ISD::SETCC &&
8412 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8413 NonFlagUse = true;
8414 break;
8415 }
Dan Gohman076aee32009-03-04 19:44:21 +00008416 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008417
8418 if (!NonFlagUse)
8419 break;
8420 }
8421 // FALL THROUGH
8422 case ISD::SUB:
8423 case ISD::OR:
8424 case ISD::XOR:
8425 // Due to the ISEL shortcoming noted above, be conservative if this op is
8426 // likely to be selected as part of a load-modify-store instruction.
8427 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8428 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8429 if (UI->getOpcode() == ISD::STORE)
8430 goto default_case;
8431
8432 // Otherwise use a regular EFLAGS-setting instruction.
8433 switch (Op.getNode()->getOpcode()) {
8434 default: llvm_unreachable("unexpected operator!");
8435 case ISD::SUB: Opcode = X86ISD::SUB; break;
8436 case ISD::OR: Opcode = X86ISD::OR; break;
8437 case ISD::XOR: Opcode = X86ISD::XOR; break;
8438 case ISD::AND: Opcode = X86ISD::AND; break;
8439 }
8440
8441 NumOperands = 2;
8442 break;
8443 case X86ISD::ADD:
8444 case X86ISD::SUB:
8445 case X86ISD::INC:
8446 case X86ISD::DEC:
8447 case X86ISD::OR:
8448 case X86ISD::XOR:
8449 case X86ISD::AND:
8450 return SDValue(Op.getNode(), 1);
8451 default:
8452 default_case:
8453 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008454 }
8455
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008456 if (Opcode == 0)
8457 // Emit a CMP with 0, which is the TEST pattern.
8458 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8459 DAG.getConstant(0, Op.getValueType()));
8460
8461 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8462 SmallVector<SDValue, 4> Ops;
8463 for (unsigned i = 0; i != NumOperands; ++i)
8464 Ops.push_back(Op.getOperand(i));
8465
8466 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8467 DAG.ReplaceAllUsesWith(Op, New);
8468 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008469}
8470
8471/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8472/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008473SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008474 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008475 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8476 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008477 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008478
8479 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008480 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008481}
8482
Evan Chengd40d03e2010-01-06 19:38:29 +00008483/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8484/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008485SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8486 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008487 SDValue Op0 = And.getOperand(0);
8488 SDValue Op1 = And.getOperand(1);
8489 if (Op0.getOpcode() == ISD::TRUNCATE)
8490 Op0 = Op0.getOperand(0);
8491 if (Op1.getOpcode() == ISD::TRUNCATE)
8492 Op1 = Op1.getOperand(0);
8493
Evan Chengd40d03e2010-01-06 19:38:29 +00008494 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008495 if (Op1.getOpcode() == ISD::SHL)
8496 std::swap(Op0, Op1);
8497 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008498 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8499 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008500 // If we looked past a truncate, check that it's only truncating away
8501 // known zeros.
8502 unsigned BitWidth = Op0.getValueSizeInBits();
8503 unsigned AndBitWidth = And.getValueSizeInBits();
8504 if (BitWidth > AndBitWidth) {
8505 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8506 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8507 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8508 return SDValue();
8509 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008510 LHS = Op1;
8511 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008512 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008513 } else if (Op1.getOpcode() == ISD::Constant) {
8514 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8515 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00008516 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8517 LHS = AndLHS.getOperand(0);
8518 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008519 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008520 }
Evan Cheng0488db92007-09-25 01:57:46 +00008521
Evan Chengd40d03e2010-01-06 19:38:29 +00008522 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008523 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008524 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008525 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008526 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008527 // Also promote i16 to i32 for performance / code size reason.
8528 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008529 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008530 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008531
Evan Chengd40d03e2010-01-06 19:38:29 +00008532 // If the operand types disagree, extend the shift amount to match. Since
8533 // BT ignores high bits (like shifts) we can use anyextend.
8534 if (LHS.getValueType() != RHS.getValueType())
8535 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008536
Evan Chengd40d03e2010-01-06 19:38:29 +00008537 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8538 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8539 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8540 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008541 }
8542
Evan Cheng54de3ea2010-01-05 06:52:31 +00008543 return SDValue();
8544}
8545
Dan Gohmand858e902010-04-17 15:26:15 +00008546SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008547
8548 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8549
Evan Cheng54de3ea2010-01-05 06:52:31 +00008550 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8551 SDValue Op0 = Op.getOperand(0);
8552 SDValue Op1 = Op.getOperand(1);
8553 DebugLoc dl = Op.getDebugLoc();
8554 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8555
8556 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008557 // Lower (X & (1 << N)) == 0 to BT(X, N).
8558 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8559 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008560 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008561 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008562 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008563 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8564 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8565 if (NewSetCC.getNode())
8566 return NewSetCC;
8567 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008568
Chris Lattner481eebc2010-12-19 21:23:48 +00008569 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8570 // these.
8571 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008572 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008573 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8574 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008575
Chris Lattner481eebc2010-12-19 21:23:48 +00008576 // If the input is a setcc, then reuse the input setcc or use a new one with
8577 // the inverted condition.
8578 if (Op0.getOpcode() == X86ISD::SETCC) {
8579 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8580 bool Invert = (CC == ISD::SETNE) ^
8581 cast<ConstantSDNode>(Op1)->isNullValue();
8582 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008583
Evan Cheng2c755ba2010-02-27 07:36:59 +00008584 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008585 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8586 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8587 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008588 }
8589
Evan Chenge5b51ac2010-04-17 06:13:15 +00008590 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008591 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008592 if (X86CC == X86::COND_INVALID)
8593 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008594
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008595 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008596 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008597 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008598}
8599
Craig Topper89af15e2011-09-18 08:03:58 +00008600// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008601// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008602static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008603 EVT VT = Op.getValueType();
8604
Duncan Sands28b77e92011-09-06 19:07:46 +00008605 assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008606 "Unsupported value type for operation");
8607
8608 int NumElems = VT.getVectorNumElements();
8609 DebugLoc dl = Op.getDebugLoc();
8610 SDValue CC = Op.getOperand(2);
8611 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
8612 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
8613
8614 // Extract the LHS vectors
8615 SDValue LHS = Op.getOperand(0);
8616 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
8617 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
8618
8619 // Extract the RHS vectors
8620 SDValue RHS = Op.getOperand(1);
8621 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
8622 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
8623
8624 // Issue the operation on the smaller types and concatenate the result back
8625 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8626 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8627 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8628 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8629 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8630}
8631
8632
Dan Gohmand858e902010-04-17 15:26:15 +00008633SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008634 SDValue Cond;
8635 SDValue Op0 = Op.getOperand(0);
8636 SDValue Op1 = Op.getOperand(1);
8637 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008638 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008639 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8640 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008641 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008642
8643 if (isFP) {
8644 unsigned SSECC = 8;
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008645 EVT EltVT = Op0.getValueType().getVectorElementType();
8646 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8647
8648 unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00008649 bool Swap = false;
8650
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008651 // SSE Condition code mapping:
8652 // 0 - EQ
8653 // 1 - LT
8654 // 2 - LE
8655 // 3 - UNORD
8656 // 4 - NEQ
8657 // 5 - NLT
8658 // 6 - NLE
8659 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008660 switch (SetCCOpcode) {
8661 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008662 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008663 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008664 case ISD::SETOGT:
8665 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008666 case ISD::SETLT:
8667 case ISD::SETOLT: SSECC = 1; break;
8668 case ISD::SETOGE:
8669 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008670 case ISD::SETLE:
8671 case ISD::SETOLE: SSECC = 2; break;
8672 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008673 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008674 case ISD::SETNE: SSECC = 4; break;
8675 case ISD::SETULE: Swap = true;
8676 case ISD::SETUGE: SSECC = 5; break;
8677 case ISD::SETULT: Swap = true;
8678 case ISD::SETUGT: SSECC = 6; break;
8679 case ISD::SETO: SSECC = 7; break;
8680 }
8681 if (Swap)
8682 std::swap(Op0, Op1);
8683
Nate Begemanfb8ead02008-07-25 19:05:58 +00008684 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008685 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00008686 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00008687 SDValue UNORD, EQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008688 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8689 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008690 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Craig Topper0a150352011-11-09 08:06:13 +00008691 } else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00008692 SDValue ORD, NEQ;
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008693 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8694 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00008695 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00008696 }
Torok Edwinc23197a2009-07-14 16:55:14 +00008697 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00008698 }
8699 // Handle all other FP comparisons here.
Bruno Cardoso Lopes809f17f2011-09-13 19:33:00 +00008700 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008701 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008702
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008703 // Break 256-bit integer vector compare into smaller ones.
Craig Topper0a150352011-11-09 08:06:13 +00008704 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008705 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008706
Nate Begeman30a0de92008-07-17 16:51:19 +00008707 // We are handling one of the integer comparisons here. Since SSE only has
8708 // GT and EQ comparisons for integer, swapping operands and multiple
8709 // operations may be required for some comparisons.
8710 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8711 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008712
Craig Topper0a150352011-11-09 08:06:13 +00008713 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00008714 default: break;
Craig Topper0a150352011-11-09 08:06:13 +00008715 case MVT::i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8716 case MVT::i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8717 case MVT::i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8718 case MVT::i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008719 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008720
Nate Begeman30a0de92008-07-17 16:51:19 +00008721 switch (SetCCOpcode) {
8722 default: break;
8723 case ISD::SETNE: Invert = true;
8724 case ISD::SETEQ: Opc = EQOpc; break;
8725 case ISD::SETLT: Swap = true;
8726 case ISD::SETGT: Opc = GTOpc; break;
8727 case ISD::SETGE: Swap = true;
8728 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
8729 case ISD::SETULT: Swap = true;
8730 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8731 case ISD::SETUGE: Swap = true;
8732 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8733 }
8734 if (Swap)
8735 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008736
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008737 // Check that the operation in question is available (most are plain SSE2,
8738 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topperc0d82852011-11-22 00:44:41 +00008739 if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008740 return SDValue();
Craig Topperc0d82852011-11-22 00:44:41 +00008741 if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41orAVX())
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008742 return SDValue();
8743
Nate Begeman30a0de92008-07-17 16:51:19 +00008744 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8745 // bits of the inputs before performing those operations.
8746 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008747 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008748 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8749 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008750 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008751 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8752 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008753 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8754 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008755 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008756
Dale Johannesenace16102009-02-03 19:33:06 +00008757 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008758
8759 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008760 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008761 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008762
Nate Begeman30a0de92008-07-17 16:51:19 +00008763 return Result;
8764}
Evan Cheng0488db92007-09-25 01:57:46 +00008765
Evan Cheng370e5342008-12-03 08:38:43 +00008766// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008767static bool isX86LogicalCmp(SDValue Op) {
8768 unsigned Opc = Op.getNode()->getOpcode();
8769 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8770 return true;
8771 if (Op.getResNo() == 1 &&
8772 (Opc == X86ISD::ADD ||
8773 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008774 Opc == X86ISD::ADC ||
8775 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008776 Opc == X86ISD::SMUL ||
8777 Opc == X86ISD::UMUL ||
8778 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008779 Opc == X86ISD::DEC ||
8780 Opc == X86ISD::OR ||
8781 Opc == X86ISD::XOR ||
8782 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008783 return true;
8784
Chris Lattner9637d5b2010-12-05 07:49:54 +00008785 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8786 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008787
Dan Gohman076aee32009-03-04 19:44:21 +00008788 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008789}
8790
Chris Lattnera2b56002010-12-05 01:23:24 +00008791static bool isZero(SDValue V) {
8792 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8793 return C && C->isNullValue();
8794}
8795
Chris Lattner96908b12010-12-05 02:00:51 +00008796static bool isAllOnes(SDValue V) {
8797 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8798 return C && C->isAllOnesValue();
8799}
8800
Dan Gohmand858e902010-04-17 15:26:15 +00008801SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008802 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008803 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008804 SDValue Op1 = Op.getOperand(1);
8805 SDValue Op2 = Op.getOperand(2);
8806 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008807 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008808
Dan Gohman1a492952009-10-20 16:22:37 +00008809 if (Cond.getOpcode() == ISD::SETCC) {
8810 SDValue NewCond = LowerSETCC(Cond, DAG);
8811 if (NewCond.getNode())
8812 Cond = NewCond;
8813 }
Evan Cheng734503b2006-09-11 02:19:56 +00008814
Chris Lattnera2b56002010-12-05 01:23:24 +00008815 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008816 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008817 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008818 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008819 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008820 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8821 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008822 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008823
Chris Lattnera2b56002010-12-05 01:23:24 +00008824 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008825
8826 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008827 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8828 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008829
8830 SDValue CmpOp0 = Cmp.getOperand(0);
8831 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8832 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008833
Chris Lattner96908b12010-12-05 02:00:51 +00008834 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008835 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8836 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008837
Chris Lattner96908b12010-12-05 02:00:51 +00008838 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8839 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008840
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008841 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008842 if (N2C == 0 || !N2C->isNullValue())
8843 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8844 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008845 }
8846 }
8847
Chris Lattnera2b56002010-12-05 01:23:24 +00008848 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008849 if (Cond.getOpcode() == ISD::AND &&
8850 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8851 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008852 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008853 Cond = Cond.getOperand(0);
8854 }
8855
Evan Cheng3f41d662007-10-08 22:16:29 +00008856 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8857 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008858 unsigned CondOpcode = Cond.getOpcode();
8859 if (CondOpcode == X86ISD::SETCC ||
8860 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008861 CC = Cond.getOperand(0);
8862
Dan Gohman475871a2008-07-27 21:46:04 +00008863 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008864 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008865 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008866
Evan Cheng3f41d662007-10-08 22:16:29 +00008867 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008868 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008869 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008870 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008871
Chris Lattnerd1980a52009-03-12 06:52:53 +00008872 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8873 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008874 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008875 addTest = false;
8876 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008877 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8878 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8879 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8880 Cond.getOperand(0).getValueType() != MVT::i8)) {
8881 SDValue LHS = Cond.getOperand(0);
8882 SDValue RHS = Cond.getOperand(1);
8883 unsigned X86Opcode;
8884 unsigned X86Cond;
8885 SDVTList VTs;
8886 switch (CondOpcode) {
8887 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
8888 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
8889 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
8890 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
8891 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
8892 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
8893 default: llvm_unreachable("unexpected overflowing operator");
8894 }
8895 if (CondOpcode == ISD::UMULO)
8896 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
8897 MVT::i32);
8898 else
8899 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
8900
8901 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
8902
8903 if (CondOpcode == ISD::UMULO)
8904 Cond = X86Op.getValue(2);
8905 else
8906 Cond = X86Op.getValue(1);
8907
8908 CC = DAG.getConstant(X86Cond, MVT::i8);
8909 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00008910 }
8911
8912 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008913 // Look pass the truncate.
8914 if (Cond.getOpcode() == ISD::TRUNCATE)
8915 Cond = Cond.getOperand(0);
8916
8917 // We know the result of AND is compared against zero. Try to match
8918 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00008919 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00008920 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00008921 if (NewSetCC.getNode()) {
8922 CC = NewSetCC.getOperand(0);
8923 Cond = NewSetCC.getOperand(1);
8924 addTest = false;
8925 }
8926 }
8927 }
8928
8929 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008930 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00008931 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00008932 }
8933
Benjamin Kramere915ff32010-12-22 23:09:28 +00008934 // a < b ? -1 : 0 -> RES = ~setcc_carry
8935 // a < b ? 0 : -1 -> RES = setcc_carry
8936 // a >= b ? -1 : 0 -> RES = setcc_carry
8937 // a >= b ? 0 : -1 -> RES = ~setcc_carry
8938 if (Cond.getOpcode() == X86ISD::CMP) {
8939 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
8940
8941 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
8942 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
8943 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8944 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
8945 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
8946 return DAG.getNOT(DL, Res, Res.getValueType());
8947 return Res;
8948 }
8949 }
8950
Evan Cheng0488db92007-09-25 01:57:46 +00008951 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8952 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008953 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008954 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00008955 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00008956}
8957
Evan Cheng370e5342008-12-03 08:38:43 +00008958// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8959// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8960// from the AND / OR.
8961static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8962 Opc = Op.getOpcode();
8963 if (Opc != ISD::OR && Opc != ISD::AND)
8964 return false;
8965 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8966 Op.getOperand(0).hasOneUse() &&
8967 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8968 Op.getOperand(1).hasOneUse());
8969}
8970
Evan Cheng961d6d42009-02-02 08:19:07 +00008971// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8972// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00008973static bool isXor1OfSetCC(SDValue Op) {
8974 if (Op.getOpcode() != ISD::XOR)
8975 return false;
8976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8977 if (N1C && N1C->getAPIntValue() == 1) {
8978 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8979 Op.getOperand(0).hasOneUse();
8980 }
8981 return false;
8982}
8983
Dan Gohmand858e902010-04-17 15:26:15 +00008984SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008985 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008986 SDValue Chain = Op.getOperand(0);
8987 SDValue Cond = Op.getOperand(1);
8988 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008989 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008990 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00008991 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00008992
Dan Gohman1a492952009-10-20 16:22:37 +00008993 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00008994 // Check for setcc([su]{add,sub,mul}o == 0).
8995 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
8996 isa<ConstantSDNode>(Cond.getOperand(1)) &&
8997 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
8998 Cond.getOperand(0).getResNo() == 1 &&
8999 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9000 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9001 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9002 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9003 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9004 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9005 Inverted = true;
9006 Cond = Cond.getOperand(0);
9007 } else {
9008 SDValue NewCond = LowerSETCC(Cond, DAG);
9009 if (NewCond.getNode())
9010 Cond = NewCond;
9011 }
Dan Gohman1a492952009-10-20 16:22:37 +00009012 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009013#if 0
9014 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009015 else if (Cond.getOpcode() == X86ISD::ADD ||
9016 Cond.getOpcode() == X86ISD::SUB ||
9017 Cond.getOpcode() == X86ISD::SMUL ||
9018 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009019 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009020#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009021
Evan Chengad9c0a32009-12-15 00:53:42 +00009022 // Look pass (and (setcc_carry (cmp ...)), 1).
9023 if (Cond.getOpcode() == ISD::AND &&
9024 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9025 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009026 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009027 Cond = Cond.getOperand(0);
9028 }
9029
Evan Cheng3f41d662007-10-08 22:16:29 +00009030 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9031 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009032 unsigned CondOpcode = Cond.getOpcode();
9033 if (CondOpcode == X86ISD::SETCC ||
9034 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009035 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009036
Dan Gohman475871a2008-07-27 21:46:04 +00009037 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009038 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009039 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009040 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009041 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009042 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009043 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009044 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009045 default: break;
9046 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009047 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009048 // These can only come from an arithmetic instruction with overflow,
9049 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009050 Cond = Cond.getNode()->getOperand(1);
9051 addTest = false;
9052 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009053 }
Evan Cheng0488db92007-09-25 01:57:46 +00009054 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009055 }
9056 CondOpcode = Cond.getOpcode();
9057 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9058 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9059 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9060 Cond.getOperand(0).getValueType() != MVT::i8)) {
9061 SDValue LHS = Cond.getOperand(0);
9062 SDValue RHS = Cond.getOperand(1);
9063 unsigned X86Opcode;
9064 unsigned X86Cond;
9065 SDVTList VTs;
9066 switch (CondOpcode) {
9067 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9068 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9069 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9070 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9071 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9072 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9073 default: llvm_unreachable("unexpected overflowing operator");
9074 }
9075 if (Inverted)
9076 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9077 if (CondOpcode == ISD::UMULO)
9078 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9079 MVT::i32);
9080 else
9081 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9082
9083 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9084
9085 if (CondOpcode == ISD::UMULO)
9086 Cond = X86Op.getValue(2);
9087 else
9088 Cond = X86Op.getValue(1);
9089
9090 CC = DAG.getConstant(X86Cond, MVT::i8);
9091 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009092 } else {
9093 unsigned CondOpc;
9094 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9095 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009096 if (CondOpc == ISD::OR) {
9097 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9098 // two branches instead of an explicit OR instruction with a
9099 // separate test.
9100 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009101 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009102 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009103 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009104 Chain, Dest, CC, Cmp);
9105 CC = Cond.getOperand(1).getOperand(0);
9106 Cond = Cmp;
9107 addTest = false;
9108 }
9109 } else { // ISD::AND
9110 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9111 // two branches instead of an explicit AND instruction with a
9112 // separate test. However, we only do this if this block doesn't
9113 // have a fall-through edge, because this requires an explicit
9114 // jmp when the condition is false.
9115 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009116 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009117 Op.getNode()->hasOneUse()) {
9118 X86::CondCode CCode =
9119 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9120 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009121 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009122 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009123 // Look for an unconditional branch following this conditional branch.
9124 // We need this because we need to reverse the successors in order
9125 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009126 if (User->getOpcode() == ISD::BR) {
9127 SDValue FalseBB = User->getOperand(1);
9128 SDNode *NewBR =
9129 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009130 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009131 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009132 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009133
Dale Johannesene4d209d2009-02-03 20:21:25 +00009134 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009135 Chain, Dest, CC, Cmp);
9136 X86::CondCode CCode =
9137 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9138 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009139 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009140 Cond = Cmp;
9141 addTest = false;
9142 }
9143 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009144 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009145 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9146 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9147 // It should be transformed during dag combiner except when the condition
9148 // is set by a arithmetics with overflow node.
9149 X86::CondCode CCode =
9150 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9151 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009152 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009153 Cond = Cond.getOperand(0).getOperand(1);
9154 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009155 } else if (Cond.getOpcode() == ISD::SETCC &&
9156 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9157 // For FCMP_OEQ, we can emit
9158 // two branches instead of an explicit AND instruction with a
9159 // separate test. However, we only do this if this block doesn't
9160 // have a fall-through edge, because this requires an explicit
9161 // jmp when the condition is false.
9162 if (Op.getNode()->hasOneUse()) {
9163 SDNode *User = *Op.getNode()->use_begin();
9164 // Look for an unconditional branch following this conditional branch.
9165 // We need this because we need to reverse the successors in order
9166 // to implement FCMP_OEQ.
9167 if (User->getOpcode() == ISD::BR) {
9168 SDValue FalseBB = User->getOperand(1);
9169 SDNode *NewBR =
9170 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9171 assert(NewBR == User);
9172 (void)NewBR;
9173 Dest = FalseBB;
9174
9175 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9176 Cond.getOperand(0), Cond.getOperand(1));
9177 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9178 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9179 Chain, Dest, CC, Cmp);
9180 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9181 Cond = Cmp;
9182 addTest = false;
9183 }
9184 }
9185 } else if (Cond.getOpcode() == ISD::SETCC &&
9186 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9187 // For FCMP_UNE, we can emit
9188 // two branches instead of an explicit AND instruction with a
9189 // separate test. However, we only do this if this block doesn't
9190 // have a fall-through edge, because this requires an explicit
9191 // jmp when the condition is false.
9192 if (Op.getNode()->hasOneUse()) {
9193 SDNode *User = *Op.getNode()->use_begin();
9194 // Look for an unconditional branch following this conditional branch.
9195 // We need this because we need to reverse the successors in order
9196 // to implement FCMP_UNE.
9197 if (User->getOpcode() == ISD::BR) {
9198 SDValue FalseBB = User->getOperand(1);
9199 SDNode *NewBR =
9200 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9201 assert(NewBR == User);
9202 (void)NewBR;
9203
9204 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9205 Cond.getOperand(0), Cond.getOperand(1));
9206 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9207 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9208 Chain, Dest, CC, Cmp);
9209 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9210 Cond = Cmp;
9211 addTest = false;
9212 Dest = FalseBB;
9213 }
9214 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009215 }
Evan Cheng0488db92007-09-25 01:57:46 +00009216 }
9217
9218 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009219 // Look pass the truncate.
9220 if (Cond.getOpcode() == ISD::TRUNCATE)
9221 Cond = Cond.getOperand(0);
9222
9223 // We know the result of AND is compared against zero. Try to match
9224 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009225 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009226 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9227 if (NewSetCC.getNode()) {
9228 CC = NewSetCC.getOperand(0);
9229 Cond = NewSetCC.getOperand(1);
9230 addTest = false;
9231 }
9232 }
9233 }
9234
9235 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009236 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009237 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009238 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00009239 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009240 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009241}
9242
Anton Korobeynikove060b532007-04-17 19:34:00 +00009243
9244// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9245// Calls to _alloca is needed to probe the stack when allocating more than 4k
9246// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9247// that the guard pages used by the OS virtual memory manager are allocated in
9248// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009249SDValue
9250X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009251 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009252 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
9253 EnableSegmentedStacks) &&
9254 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009255 "are being used");
9256 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009257 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009258
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009259 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009260 SDValue Chain = Op.getOperand(0);
9261 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009262 // FIXME: Ensure alignment here
9263
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009264 bool Is64Bit = Subtarget->is64Bit();
9265 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009266
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009267 if (EnableSegmentedStacks) {
9268 MachineFunction &MF = DAG.getMachineFunction();
9269 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009270
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009271 if (Is64Bit) {
9272 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009273 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009274 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009275
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009276 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
9277 I != E; I++)
9278 if (I->hasNestAttr())
9279 report_fatal_error("Cannot use segmented stacks with functions that "
9280 "have nested arguments.");
9281 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009282
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009283 const TargetRegisterClass *AddrRegClass =
9284 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9285 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9286 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9287 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9288 DAG.getRegister(Vreg, SPTy));
9289 SDValue Ops1[2] = { Value, Chain };
9290 return DAG.getMergeValues(Ops1, 2, dl);
9291 } else {
9292 SDValue Flag;
9293 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009294
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009295 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9296 Flag = Chain.getValue(1);
9297 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009298
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009299 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9300 Flag = Chain.getValue(1);
9301
9302 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9303
9304 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9305 return DAG.getMergeValues(Ops1, 2, dl);
9306 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009307}
9308
Dan Gohmand858e902010-04-17 15:26:15 +00009309SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009310 MachineFunction &MF = DAG.getMachineFunction();
9311 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9312
Dan Gohman69de1932008-02-06 22:27:42 +00009313 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009314 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009315
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009316 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009317 // vastart just stores the address of the VarArgsFrameIndex slot into the
9318 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009319 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9320 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009321 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9322 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009323 }
9324
9325 // __va_list_tag:
9326 // gp_offset (0 - 6 * 8)
9327 // fp_offset (48 - 48 + 8 * 16)
9328 // overflow_arg_area (point to parameters coming in memory).
9329 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009330 SmallVector<SDValue, 8> MemOps;
9331 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009332 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009333 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009334 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9335 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009336 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009337 MemOps.push_back(Store);
9338
9339 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009340 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009341 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009342 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009343 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9344 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009345 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009346 MemOps.push_back(Store);
9347
9348 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009349 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009350 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009351 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9352 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009353 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9354 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009355 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009356 MemOps.push_back(Store);
9357
9358 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009359 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009360 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009361 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9362 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009363 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9364 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009365 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009366 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009367 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009368}
9369
Dan Gohmand858e902010-04-17 15:26:15 +00009370SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009371 assert(Subtarget->is64Bit() &&
9372 "LowerVAARG only handles 64-bit va_arg!");
9373 assert((Subtarget->isTargetLinux() ||
9374 Subtarget->isTargetDarwin()) &&
9375 "Unhandled target in LowerVAARG");
9376 assert(Op.getNode()->getNumOperands() == 4);
9377 SDValue Chain = Op.getOperand(0);
9378 SDValue SrcPtr = Op.getOperand(1);
9379 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9380 unsigned Align = Op.getConstantOperandVal(3);
9381 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009382
Dan Gohman320afb82010-10-12 18:00:49 +00009383 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009384 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009385 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9386 uint8_t ArgMode;
9387
9388 // Decide which area this value should be read from.
9389 // TODO: Implement the AMD64 ABI in its entirety. This simple
9390 // selection mechanism works only for the basic types.
9391 if (ArgVT == MVT::f80) {
9392 llvm_unreachable("va_arg for f80 not yet implemented");
9393 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9394 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9395 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9396 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9397 } else {
9398 llvm_unreachable("Unhandled argument type in LowerVAARG");
9399 }
9400
9401 if (ArgMode == 2) {
9402 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00009403 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009404 !(DAG.getMachineFunction()
9405 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00009406 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00009407 }
9408
9409 // Insert VAARG_64 node into the DAG
9410 // VAARG_64 returns two values: Variable Argument Address, Chain
9411 SmallVector<SDValue, 11> InstOps;
9412 InstOps.push_back(Chain);
9413 InstOps.push_back(SrcPtr);
9414 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9415 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9416 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9417 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9418 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9419 VTs, &InstOps[0], InstOps.size(),
9420 MVT::i64,
9421 MachinePointerInfo(SV),
9422 /*Align=*/0,
9423 /*Volatile=*/false,
9424 /*ReadMem=*/true,
9425 /*WriteMem=*/true);
9426 Chain = VAARG.getValue(1);
9427
9428 // Load the next argument and return it
9429 return DAG.getLoad(ArgVT, dl,
9430 Chain,
9431 VAARG,
9432 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009433 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009434}
9435
Dan Gohmand858e902010-04-17 15:26:15 +00009436SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009437 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009438 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009439 SDValue Chain = Op.getOperand(0);
9440 SDValue DstPtr = Op.getOperand(1);
9441 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009442 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9443 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009444 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009445
Chris Lattnere72f2022010-09-21 05:40:29 +00009446 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009447 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009448 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009449 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009450}
9451
Dan Gohman475871a2008-07-27 21:46:04 +00009452SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009453X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009454 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009455 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009456 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009457 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009458 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009459 case Intrinsic::x86_sse_comieq_ss:
9460 case Intrinsic::x86_sse_comilt_ss:
9461 case Intrinsic::x86_sse_comile_ss:
9462 case Intrinsic::x86_sse_comigt_ss:
9463 case Intrinsic::x86_sse_comige_ss:
9464 case Intrinsic::x86_sse_comineq_ss:
9465 case Intrinsic::x86_sse_ucomieq_ss:
9466 case Intrinsic::x86_sse_ucomilt_ss:
9467 case Intrinsic::x86_sse_ucomile_ss:
9468 case Intrinsic::x86_sse_ucomigt_ss:
9469 case Intrinsic::x86_sse_ucomige_ss:
9470 case Intrinsic::x86_sse_ucomineq_ss:
9471 case Intrinsic::x86_sse2_comieq_sd:
9472 case Intrinsic::x86_sse2_comilt_sd:
9473 case Intrinsic::x86_sse2_comile_sd:
9474 case Intrinsic::x86_sse2_comigt_sd:
9475 case Intrinsic::x86_sse2_comige_sd:
9476 case Intrinsic::x86_sse2_comineq_sd:
9477 case Intrinsic::x86_sse2_ucomieq_sd:
9478 case Intrinsic::x86_sse2_ucomilt_sd:
9479 case Intrinsic::x86_sse2_ucomile_sd:
9480 case Intrinsic::x86_sse2_ucomigt_sd:
9481 case Intrinsic::x86_sse2_ucomige_sd:
9482 case Intrinsic::x86_sse2_ucomineq_sd: {
9483 unsigned Opc = 0;
9484 ISD::CondCode CC = ISD::SETCC_INVALID;
9485 switch (IntNo) {
9486 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009487 case Intrinsic::x86_sse_comieq_ss:
9488 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009489 Opc = X86ISD::COMI;
9490 CC = ISD::SETEQ;
9491 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009492 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009493 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009494 Opc = X86ISD::COMI;
9495 CC = ISD::SETLT;
9496 break;
9497 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009498 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009499 Opc = X86ISD::COMI;
9500 CC = ISD::SETLE;
9501 break;
9502 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009503 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009504 Opc = X86ISD::COMI;
9505 CC = ISD::SETGT;
9506 break;
9507 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009508 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009509 Opc = X86ISD::COMI;
9510 CC = ISD::SETGE;
9511 break;
9512 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009513 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009514 Opc = X86ISD::COMI;
9515 CC = ISD::SETNE;
9516 break;
9517 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009518 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009519 Opc = X86ISD::UCOMI;
9520 CC = ISD::SETEQ;
9521 break;
9522 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009523 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009524 Opc = X86ISD::UCOMI;
9525 CC = ISD::SETLT;
9526 break;
9527 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009528 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009529 Opc = X86ISD::UCOMI;
9530 CC = ISD::SETLE;
9531 break;
9532 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009533 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009534 Opc = X86ISD::UCOMI;
9535 CC = ISD::SETGT;
9536 break;
9537 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009538 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009539 Opc = X86ISD::UCOMI;
9540 CC = ISD::SETGE;
9541 break;
9542 case Intrinsic::x86_sse_ucomineq_ss:
9543 case Intrinsic::x86_sse2_ucomineq_sd:
9544 Opc = X86ISD::UCOMI;
9545 CC = ISD::SETNE;
9546 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009547 }
Evan Cheng734503b2006-09-11 02:19:56 +00009548
Dan Gohman475871a2008-07-27 21:46:04 +00009549 SDValue LHS = Op.getOperand(1);
9550 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009551 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009552 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009553 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9554 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9555 DAG.getConstant(X86CC, MVT::i8), Cond);
9556 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009557 }
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009558 // Arithmetic intrinsics.
9559 case Intrinsic::x86_sse3_hadd_ps:
9560 case Intrinsic::x86_sse3_hadd_pd:
9561 case Intrinsic::x86_avx_hadd_ps_256:
9562 case Intrinsic::x86_avx_hadd_pd_256:
9563 return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
9564 Op.getOperand(1), Op.getOperand(2));
9565 case Intrinsic::x86_sse3_hsub_ps:
9566 case Intrinsic::x86_sse3_hsub_pd:
9567 case Intrinsic::x86_avx_hsub_ps_256:
9568 case Intrinsic::x86_avx_hsub_pd_256:
9569 return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
9570 Op.getOperand(1), Op.getOperand(2));
Craig Topper98fc7292011-11-19 17:46:46 +00009571 case Intrinsic::x86_avx2_psllv_d:
9572 case Intrinsic::x86_avx2_psllv_q:
9573 case Intrinsic::x86_avx2_psllv_d_256:
9574 case Intrinsic::x86_avx2_psllv_q_256:
9575 return DAG.getNode(ISD::SHL, dl, Op.getValueType(),
9576 Op.getOperand(1), Op.getOperand(2));
9577 case Intrinsic::x86_avx2_psrlv_d:
9578 case Intrinsic::x86_avx2_psrlv_q:
9579 case Intrinsic::x86_avx2_psrlv_d_256:
9580 case Intrinsic::x86_avx2_psrlv_q_256:
9581 return DAG.getNode(ISD::SRL, dl, Op.getValueType(),
9582 Op.getOperand(1), Op.getOperand(2));
9583 case Intrinsic::x86_avx2_psrav_d:
9584 case Intrinsic::x86_avx2_psrav_d_256:
9585 return DAG.getNode(ISD::SRA, dl, Op.getValueType(),
9586 Op.getOperand(1), Op.getOperand(2));
9587
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009588 // ptest and testp intrinsics. The intrinsic these come from are designed to
9589 // return an integer value, not just an instruction so lower it to the ptest
9590 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009591 case Intrinsic::x86_sse41_ptestz:
9592 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009593 case Intrinsic::x86_sse41_ptestnzc:
9594 case Intrinsic::x86_avx_ptestz_256:
9595 case Intrinsic::x86_avx_ptestc_256:
9596 case Intrinsic::x86_avx_ptestnzc_256:
9597 case Intrinsic::x86_avx_vtestz_ps:
9598 case Intrinsic::x86_avx_vtestc_ps:
9599 case Intrinsic::x86_avx_vtestnzc_ps:
9600 case Intrinsic::x86_avx_vtestz_pd:
9601 case Intrinsic::x86_avx_vtestc_pd:
9602 case Intrinsic::x86_avx_vtestnzc_pd:
9603 case Intrinsic::x86_avx_vtestz_ps_256:
9604 case Intrinsic::x86_avx_vtestc_ps_256:
9605 case Intrinsic::x86_avx_vtestnzc_ps_256:
9606 case Intrinsic::x86_avx_vtestz_pd_256:
9607 case Intrinsic::x86_avx_vtestc_pd_256:
9608 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9609 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00009610 unsigned X86CC = 0;
9611 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009612 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009613 case Intrinsic::x86_avx_vtestz_ps:
9614 case Intrinsic::x86_avx_vtestz_pd:
9615 case Intrinsic::x86_avx_vtestz_ps_256:
9616 case Intrinsic::x86_avx_vtestz_pd_256:
9617 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009618 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009619 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009620 // ZF = 1
9621 X86CC = X86::COND_E;
9622 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009623 case Intrinsic::x86_avx_vtestc_ps:
9624 case Intrinsic::x86_avx_vtestc_pd:
9625 case Intrinsic::x86_avx_vtestc_ps_256:
9626 case Intrinsic::x86_avx_vtestc_pd_256:
9627 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009628 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009629 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009630 // CF = 1
9631 X86CC = X86::COND_B;
9632 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009633 case Intrinsic::x86_avx_vtestnzc_ps:
9634 case Intrinsic::x86_avx_vtestnzc_pd:
9635 case Intrinsic::x86_avx_vtestnzc_ps_256:
9636 case Intrinsic::x86_avx_vtestnzc_pd_256:
9637 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009638 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009639 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009640 // ZF and CF = 0
9641 X86CC = X86::COND_A;
9642 break;
9643 }
Eric Christopherfd179292009-08-27 18:07:15 +00009644
Eric Christopher71c67532009-07-29 00:28:05 +00009645 SDValue LHS = Op.getOperand(1);
9646 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009647 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9648 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009649 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9650 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9651 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009652 }
Evan Cheng5759f972008-05-04 09:15:50 +00009653
9654 // Fix vector shift instructions where the last operand is a non-immediate
9655 // i32 value.
Craig Topper7be5dfd2011-11-12 09:58:49 +00009656 case Intrinsic::x86_avx2_pslli_w:
9657 case Intrinsic::x86_avx2_pslli_d:
9658 case Intrinsic::x86_avx2_pslli_q:
9659 case Intrinsic::x86_avx2_psrli_w:
9660 case Intrinsic::x86_avx2_psrli_d:
9661 case Intrinsic::x86_avx2_psrli_q:
9662 case Intrinsic::x86_avx2_psrai_w:
9663 case Intrinsic::x86_avx2_psrai_d:
Evan Cheng5759f972008-05-04 09:15:50 +00009664 case Intrinsic::x86_sse2_pslli_w:
9665 case Intrinsic::x86_sse2_pslli_d:
9666 case Intrinsic::x86_sse2_pslli_q:
9667 case Intrinsic::x86_sse2_psrli_w:
9668 case Intrinsic::x86_sse2_psrli_d:
9669 case Intrinsic::x86_sse2_psrli_q:
9670 case Intrinsic::x86_sse2_psrai_w:
9671 case Intrinsic::x86_sse2_psrai_d:
9672 case Intrinsic::x86_mmx_pslli_w:
9673 case Intrinsic::x86_mmx_pslli_d:
9674 case Intrinsic::x86_mmx_pslli_q:
9675 case Intrinsic::x86_mmx_psrli_w:
9676 case Intrinsic::x86_mmx_psrli_d:
9677 case Intrinsic::x86_mmx_psrli_q:
9678 case Intrinsic::x86_mmx_psrai_w:
9679 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00009680 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00009681 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00009682 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00009683
9684 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009685 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009686 switch (IntNo) {
9687 case Intrinsic::x86_sse2_pslli_w:
9688 NewIntNo = Intrinsic::x86_sse2_psll_w;
9689 break;
9690 case Intrinsic::x86_sse2_pslli_d:
9691 NewIntNo = Intrinsic::x86_sse2_psll_d;
9692 break;
9693 case Intrinsic::x86_sse2_pslli_q:
9694 NewIntNo = Intrinsic::x86_sse2_psll_q;
9695 break;
9696 case Intrinsic::x86_sse2_psrli_w:
9697 NewIntNo = Intrinsic::x86_sse2_psrl_w;
9698 break;
9699 case Intrinsic::x86_sse2_psrli_d:
9700 NewIntNo = Intrinsic::x86_sse2_psrl_d;
9701 break;
9702 case Intrinsic::x86_sse2_psrli_q:
9703 NewIntNo = Intrinsic::x86_sse2_psrl_q;
9704 break;
9705 case Intrinsic::x86_sse2_psrai_w:
9706 NewIntNo = Intrinsic::x86_sse2_psra_w;
9707 break;
9708 case Intrinsic::x86_sse2_psrai_d:
9709 NewIntNo = Intrinsic::x86_sse2_psra_d;
9710 break;
Craig Topper7be5dfd2011-11-12 09:58:49 +00009711 case Intrinsic::x86_avx2_pslli_w:
9712 NewIntNo = Intrinsic::x86_avx2_psll_w;
9713 break;
9714 case Intrinsic::x86_avx2_pslli_d:
9715 NewIntNo = Intrinsic::x86_avx2_psll_d;
9716 break;
9717 case Intrinsic::x86_avx2_pslli_q:
9718 NewIntNo = Intrinsic::x86_avx2_psll_q;
9719 break;
9720 case Intrinsic::x86_avx2_psrli_w:
9721 NewIntNo = Intrinsic::x86_avx2_psrl_w;
9722 break;
9723 case Intrinsic::x86_avx2_psrli_d:
9724 NewIntNo = Intrinsic::x86_avx2_psrl_d;
9725 break;
9726 case Intrinsic::x86_avx2_psrli_q:
9727 NewIntNo = Intrinsic::x86_avx2_psrl_q;
9728 break;
9729 case Intrinsic::x86_avx2_psrai_w:
9730 NewIntNo = Intrinsic::x86_avx2_psra_w;
9731 break;
9732 case Intrinsic::x86_avx2_psrai_d:
9733 NewIntNo = Intrinsic::x86_avx2_psra_d;
9734 break;
Evan Cheng5759f972008-05-04 09:15:50 +00009735 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00009736 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00009737 switch (IntNo) {
9738 case Intrinsic::x86_mmx_pslli_w:
9739 NewIntNo = Intrinsic::x86_mmx_psll_w;
9740 break;
9741 case Intrinsic::x86_mmx_pslli_d:
9742 NewIntNo = Intrinsic::x86_mmx_psll_d;
9743 break;
9744 case Intrinsic::x86_mmx_pslli_q:
9745 NewIntNo = Intrinsic::x86_mmx_psll_q;
9746 break;
9747 case Intrinsic::x86_mmx_psrli_w:
9748 NewIntNo = Intrinsic::x86_mmx_psrl_w;
9749 break;
9750 case Intrinsic::x86_mmx_psrli_d:
9751 NewIntNo = Intrinsic::x86_mmx_psrl_d;
9752 break;
9753 case Intrinsic::x86_mmx_psrli_q:
9754 NewIntNo = Intrinsic::x86_mmx_psrl_q;
9755 break;
9756 case Intrinsic::x86_mmx_psrai_w:
9757 NewIntNo = Intrinsic::x86_mmx_psra_w;
9758 break;
9759 case Intrinsic::x86_mmx_psrai_d:
9760 NewIntNo = Intrinsic::x86_mmx_psra_d;
9761 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009762 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00009763 }
9764 break;
9765 }
9766 }
Mon P Wangefa42202009-09-03 19:56:25 +00009767
9768 // The vector shift intrinsics with scalars uses 32b shift amounts but
9769 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9770 // to be zero.
9771 SDValue ShOps[4];
9772 ShOps[0] = ShAmt;
9773 ShOps[1] = DAG.getConstant(0, MVT::i32);
9774 if (ShAmtVT == MVT::v4i32) {
9775 ShOps[2] = DAG.getUNDEF(MVT::i32);
9776 ShOps[3] = DAG.getUNDEF(MVT::i32);
9777 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9778 } else {
9779 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00009780// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00009781 }
9782
Owen Andersone50ed302009-08-10 22:56:29 +00009783 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009784 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009785 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009786 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00009787 Op.getOperand(1), ShAmt);
9788 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00009789 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00009790}
Evan Cheng72261582005-12-20 06:22:03 +00009791
Dan Gohmand858e902010-04-17 15:26:15 +00009792SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9793 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00009794 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9795 MFI->setReturnAddressIsTaken(true);
9796
Bill Wendling64e87322009-01-16 19:25:27 +00009797 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009798 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00009799
9800 if (Depth > 0) {
9801 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9802 SDValue Offset =
9803 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00009804 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009805 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009806 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009807 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009808 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00009809 }
9810
9811 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00009812 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00009813 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009814 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009815}
9816
Dan Gohmand858e902010-04-17 15:26:15 +00009817SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00009818 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9819 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00009820
Owen Andersone50ed302009-08-10 22:56:29 +00009821 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009822 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00009823 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9824 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00009825 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00009826 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00009827 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
9828 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009829 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00009830 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00009831}
9832
Dan Gohman475871a2008-07-27 21:46:04 +00009833SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009834 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009835 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009836}
9837
Dan Gohmand858e902010-04-17 15:26:15 +00009838SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009839 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00009840 SDValue Chain = Op.getOperand(0);
9841 SDValue Offset = Op.getOperand(1);
9842 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009843 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009844
Dan Gohmand8816272010-08-11 18:14:00 +00009845 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
9846 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
9847 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009848 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009849
Dan Gohmand8816272010-08-11 18:14:00 +00009850 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
9851 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009852 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009853 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
9854 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00009855 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009856 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009857
Dale Johannesene4d209d2009-02-03 20:21:25 +00009858 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009859 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00009860 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009861}
9862
Duncan Sands4a544a72011-09-06 13:37:06 +00009863SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
9864 SelectionDAG &DAG) const {
9865 return Op.getOperand(0);
9866}
9867
9868SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
9869 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009870 SDValue Root = Op.getOperand(0);
9871 SDValue Trmp = Op.getOperand(1); // trampoline
9872 SDValue FPtr = Op.getOperand(2); // nested function
9873 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009874 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009875
Dan Gohman69de1932008-02-06 22:27:42 +00009876 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009877
9878 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009879 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00009880
9881 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00009882 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
9883 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00009884
Evan Cheng0e6a0522011-07-18 20:57:22 +00009885 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
9886 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00009887
9888 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9889
9890 // Load the pointer to the nested function into R11.
9891 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00009892 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00009893 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009894 Addr, MachinePointerInfo(TrmpAddr),
9895 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009896
Owen Anderson825b72b2009-08-11 20:47:22 +00009897 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9898 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009899 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
9900 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00009901 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009902
9903 // Load the 'nest' parameter value into R10.
9904 // R10 is specified in X86CallingConv.td
9905 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00009906 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9907 DAG.getConstant(10, MVT::i64));
9908 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009909 Addr, MachinePointerInfo(TrmpAddr, 10),
9910 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009911
Owen Anderson825b72b2009-08-11 20:47:22 +00009912 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9913 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009914 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
9915 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00009916 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00009917
9918 // Jump to the nested function.
9919 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00009920 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9921 DAG.getConstant(20, MVT::i64));
9922 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009923 Addr, MachinePointerInfo(TrmpAddr, 20),
9924 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009925
9926 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00009927 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9928 DAG.getConstant(22, MVT::i64));
9929 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00009930 MachinePointerInfo(TrmpAddr, 22),
9931 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00009932
Duncan Sands4a544a72011-09-06 13:37:06 +00009933 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009934 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00009935 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00009936 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00009937 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00009938 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009939
9940 switch (CC) {
9941 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009942 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009943 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009944 case CallingConv::X86_StdCall: {
9945 // Pass 'nest' parameter in ECX.
9946 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009947 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009948
9949 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009950 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00009951 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00009952
Chris Lattner58d74912008-03-12 17:45:29 +00009953 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00009954 unsigned InRegCount = 0;
9955 unsigned Idx = 1;
9956
9957 for (FunctionType::param_iterator I = FTy->param_begin(),
9958 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00009959 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00009960 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00009961 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009962
9963 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00009964 report_fatal_error("Nest register in use - reduce number of inreg"
9965 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00009966 }
9967 }
9968 break;
9969 }
9970 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00009971 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00009972 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00009973 // Pass 'nest' parameter in EAX.
9974 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00009975 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009976 break;
9977 }
9978
Dan Gohman475871a2008-07-27 21:46:04 +00009979 SDValue OutChains[4];
9980 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00009981
Owen Anderson825b72b2009-08-11 20:47:22 +00009982 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9983 DAG.getConstant(10, MVT::i32));
9984 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009985
Chris Lattnera62fe662010-02-05 19:20:30 +00009986 // This is storing the opcode for MOV32ri.
9987 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +00009988 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009989 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00009990 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009991 Trmp, MachinePointerInfo(TrmpAddr),
9992 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009993
Owen Anderson825b72b2009-08-11 20:47:22 +00009994 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9995 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009996 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
9997 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00009998 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009999
Chris Lattnera62fe662010-02-05 19:20:30 +000010000 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010001 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10002 DAG.getConstant(5, MVT::i32));
10003 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010004 MachinePointerInfo(TrmpAddr, 5),
10005 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010006
Owen Anderson825b72b2009-08-11 20:47:22 +000010007 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10008 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010009 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10010 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010011 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010012
Duncan Sands4a544a72011-09-06 13:37:06 +000010013 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010014 }
10015}
10016
Dan Gohmand858e902010-04-17 15:26:15 +000010017SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10018 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010019 /*
10020 The rounding mode is in bits 11:10 of FPSR, and has the following
10021 settings:
10022 00 Round to nearest
10023 01 Round to -inf
10024 10 Round to +inf
10025 11 Round to 0
10026
10027 FLT_ROUNDS, on the other hand, expects the following:
10028 -1 Undefined
10029 0 Round to 0
10030 1 Round to nearest
10031 2 Round to +inf
10032 3 Round to -inf
10033
10034 To perform the conversion, we do:
10035 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10036 */
10037
10038 MachineFunction &MF = DAG.getMachineFunction();
10039 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010040 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010041 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010042 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010043 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010044
10045 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010046 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010047 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010048
Michael J. Spencerec38de22010-10-10 22:04:20 +000010049
Chris Lattner2156b792010-09-22 01:11:26 +000010050 MachineMemOperand *MMO =
10051 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10052 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010053
Chris Lattner2156b792010-09-22 01:11:26 +000010054 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10055 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10056 DAG.getVTList(MVT::Other),
10057 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010058
10059 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010060 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010061 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010062
10063 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010064 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010065 DAG.getNode(ISD::SRL, DL, MVT::i16,
10066 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010067 CWD, DAG.getConstant(0x800, MVT::i16)),
10068 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010069 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010070 DAG.getNode(ISD::SRL, DL, MVT::i16,
10071 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010072 CWD, DAG.getConstant(0x400, MVT::i16)),
10073 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010074
Dan Gohman475871a2008-07-27 21:46:04 +000010075 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010076 DAG.getNode(ISD::AND, DL, MVT::i16,
10077 DAG.getNode(ISD::ADD, DL, MVT::i16,
10078 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010079 DAG.getConstant(1, MVT::i16)),
10080 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010081
10082
Duncan Sands83ec4b62008-06-06 12:08:01 +000010083 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010084 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010085}
10086
Dan Gohmand858e902010-04-17 15:26:15 +000010087SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010088 EVT VT = Op.getValueType();
10089 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010090 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010091 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010092
10093 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010094 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010095 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010096 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010097 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010098 }
Evan Cheng18efe262007-12-14 02:13:44 +000010099
Evan Cheng152804e2007-12-14 08:30:15 +000010100 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010101 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010102 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010103
10104 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010105 SDValue Ops[] = {
10106 Op,
10107 DAG.getConstant(NumBits+NumBits-1, OpVT),
10108 DAG.getConstant(X86::COND_E, MVT::i8),
10109 Op.getValue(1)
10110 };
10111 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010112
10113 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010114 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010115
Owen Anderson825b72b2009-08-11 20:47:22 +000010116 if (VT == MVT::i8)
10117 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010118 return Op;
10119}
10120
Dan Gohmand858e902010-04-17 15:26:15 +000010121SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010122 EVT VT = Op.getValueType();
10123 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010124 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010125 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010126
10127 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010128 if (VT == MVT::i8) {
10129 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010130 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010131 }
Evan Cheng152804e2007-12-14 08:30:15 +000010132
10133 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010134 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010135 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010136
10137 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010138 SDValue Ops[] = {
10139 Op,
10140 DAG.getConstant(NumBits, OpVT),
10141 DAG.getConstant(X86::COND_E, MVT::i8),
10142 Op.getValue(1)
10143 };
10144 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010145
Owen Anderson825b72b2009-08-11 20:47:22 +000010146 if (VT == MVT::i8)
10147 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010148 return Op;
10149}
10150
Craig Topper13894fa2011-08-24 06:14:18 +000010151// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10152// ones, and then concatenate the result back.
10153static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010154 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010155
10156 assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
10157 "Unsupported value type for operation");
10158
10159 int NumElems = VT.getVectorNumElements();
10160 DebugLoc dl = Op.getDebugLoc();
10161 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10162 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10163
10164 // Extract the LHS vectors
10165 SDValue LHS = Op.getOperand(0);
10166 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10167 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10168
10169 // Extract the RHS vectors
10170 SDValue RHS = Op.getOperand(1);
10171 SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
10172 SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
10173
10174 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10175 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10176
10177 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10178 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10179 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10180}
10181
10182SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
10183 assert(Op.getValueType().getSizeInBits() == 256 &&
10184 Op.getValueType().isInteger() &&
10185 "Only handle AVX 256-bit vector integer operation");
10186 return Lower256IntArith(Op, DAG);
10187}
10188
10189SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
10190 assert(Op.getValueType().getSizeInBits() == 256 &&
10191 Op.getValueType().isInteger() &&
10192 "Only handle AVX 256-bit vector integer operation");
10193 return Lower256IntArith(Op, DAG);
10194}
10195
10196SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10197 EVT VT = Op.getValueType();
10198
10199 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topperaaa643c2011-11-09 07:28:55 +000010200 if (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010201 return Lower256IntArith(Op, DAG);
10202
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010203 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010204
Craig Topperaaa643c2011-11-09 07:28:55 +000010205 SDValue A = Op.getOperand(0);
10206 SDValue B = Op.getOperand(1);
10207
10208 if (VT == MVT::v4i64) {
10209 assert(Subtarget->hasAVX2() && "Lowering v4i64 multiply requires AVX2");
10210
10211 // ulong2 Ahi = __builtin_ia32_psrlqi256( a, 32);
10212 // ulong2 Bhi = __builtin_ia32_psrlqi256( b, 32);
10213 // ulong2 AloBlo = __builtin_ia32_pmuludq256( a, b );
10214 // ulong2 AloBhi = __builtin_ia32_pmuludq256( a, Bhi );
10215 // ulong2 AhiBlo = __builtin_ia32_pmuludq256( Ahi, b );
10216 //
10217 // AloBhi = __builtin_ia32_psllqi256( AloBhi, 32 );
10218 // AhiBlo = __builtin_ia32_psllqi256( AhiBlo, 32 );
10219 // return AloBlo + AloBhi + AhiBlo;
10220
10221 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10222 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10223 A, DAG.getConstant(32, MVT::i32));
10224 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10225 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
10226 B, DAG.getConstant(32, MVT::i32));
10227 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10228 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10229 A, B);
10230 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10231 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10232 A, Bhi);
10233 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10234 DAG.getConstant(Intrinsic::x86_avx2_pmulu_dq, MVT::i32),
10235 Ahi, B);
10236 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10237 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10238 AloBhi, DAG.getConstant(32, MVT::i32));
10239 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10240 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
10241 AhiBlo, DAG.getConstant(32, MVT::i32));
10242 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10243 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
10244 return Res;
10245 }
10246
10247 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
10248
Mon P Wangaf9b9522008-12-18 21:42:19 +000010249 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
10250 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
10251 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
10252 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
10253 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
10254 //
10255 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
10256 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
10257 // return AloBlo + AloBhi + AhiBlo;
10258
Dale Johannesene4d209d2009-02-03 20:21:25 +000010259 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010260 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10261 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010262 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10264 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010265 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010266 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010267 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010268 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010269 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010270 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010271 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010272 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +000010273 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010274 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010275 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10276 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010277 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000010278 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10279 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010280 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
10281 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010282 return Res;
10283}
10284
Nadav Rotem43012222011-05-11 08:12:09 +000010285SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10286
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010287 EVT VT = Op.getValueType();
10288 DebugLoc dl = Op.getDebugLoc();
10289 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010290 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010291 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010292
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010293 if (!Subtarget->hasXMMInt())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010294 return SDValue();
10295
Nadav Rotem43012222011-05-11 08:12:09 +000010296 // Optimize shl/srl/sra with constant shift amount.
10297 if (isSplatVector(Amt.getNode())) {
10298 SDValue SclrAmt = Amt->getOperand(0);
10299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10300 uint64_t ShiftAmt = C->getZExtValue();
10301
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010302 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SHL) {
10303 // Make a large shift.
10304 SDValue SHL =
10305 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10306 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10307 R, DAG.getConstant(ShiftAmt, MVT::i32));
10308 // Zero out the rightmost bits.
10309 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10310 MVT::i8));
10311 return DAG.getNode(ISD::AND, dl, VT, SHL,
10312 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10313 }
10314
Nadav Rotem43012222011-05-11 08:12:09 +000010315 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
10316 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10317 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
10318 R, DAG.getConstant(ShiftAmt, MVT::i32));
10319
10320 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
10321 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10322 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10323 R, DAG.getConstant(ShiftAmt, MVT::i32));
10324
10325 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
10326 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10327 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10328 R, DAG.getConstant(ShiftAmt, MVT::i32));
10329
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010330 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRL) {
10331 // Make a large shift.
10332 SDValue SRL =
10333 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10334 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10335 R, DAG.getConstant(ShiftAmt, MVT::i32));
10336 // Zero out the leftmost bits.
10337 SmallVector<SDValue, 16> V(16, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10338 MVT::i8));
10339 return DAG.getNode(ISD::AND, dl, VT, SRL,
10340 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10341 }
10342
Nadav Rotem43012222011-05-11 08:12:09 +000010343 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
10344 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10345 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
10346 R, DAG.getConstant(ShiftAmt, MVT::i32));
10347
10348 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
10349 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10350 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
10351 R, DAG.getConstant(ShiftAmt, MVT::i32));
10352
10353 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
10354 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10355 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
10356 R, DAG.getConstant(ShiftAmt, MVT::i32));
10357
10358 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
10359 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10360 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
10361 R, DAG.getConstant(ShiftAmt, MVT::i32));
10362
10363 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
10364 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10365 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
10366 R, DAG.getConstant(ShiftAmt, MVT::i32));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010367
10368 if (VT == MVT::v16i8 && Op.getOpcode() == ISD::SRA) {
10369 if (ShiftAmt == 7) {
10370 // R s>> 7 === R s< 0
10371 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10372 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10373 }
10374
10375 // R s>> a === ((R u>> a) ^ m) - m
10376 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10377 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10378 MVT::i8));
10379 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10380 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10381 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10382 return Res;
10383 }
Craig Topper46154eb2011-11-11 07:39:23 +000010384
Craig Topper0d86d462011-11-20 00:12:05 +000010385 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10386 if (Op.getOpcode() == ISD::SHL) {
10387 // Make a large shift.
10388 SDValue SHL =
10389 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10390 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
10391 R, DAG.getConstant(ShiftAmt, MVT::i32));
10392 // Zero out the rightmost bits.
10393 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U << ShiftAmt),
10394 MVT::i8));
10395 return DAG.getNode(ISD::AND, dl, VT, SHL,
10396 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010397 }
Craig Topper0d86d462011-11-20 00:12:05 +000010398 if (Op.getOpcode() == ISD::SRL) {
10399 // Make a large shift.
10400 SDValue SRL =
10401 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10402 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
10403 R, DAG.getConstant(ShiftAmt, MVT::i32));
10404 // Zero out the leftmost bits.
10405 SmallVector<SDValue, 32> V(32, DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10406 MVT::i8));
10407 return DAG.getNode(ISD::AND, dl, VT, SRL,
10408 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10409 }
10410 if (Op.getOpcode() == ISD::SRA) {
10411 if (ShiftAmt == 7) {
10412 // R s>> 7 === R s< 0
10413 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
10414 return DAG.getNode(X86ISD::PCMPGTB, dl, VT, Zeros, R);
10415 }
10416
10417 // R s>> a === ((R u>> a) ^ m) - m
10418 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10419 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10420 MVT::i8));
10421 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10422 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10423 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10424 return Res;
10425 }
10426 }
Nadav Rotem43012222011-05-11 08:12:09 +000010427 }
10428 }
10429
10430 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010431 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010432 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10433 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
10434 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
10435
10436 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010437
Nate Begeman51409212010-07-28 00:21:48 +000010438 std::vector<Constant*> CV(4, CI);
10439 Constant *C = ConstantVector::get(CV);
10440 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10441 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010442 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010443 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010444
10445 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010446 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010447 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10448 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10449 }
Nadav Rotem43012222011-05-11 08:12:09 +000010450 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +000010451 // a = a << 5;
10452 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10453 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
10454 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
10455
10456 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10457 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10458
10459 std::vector<Constant*> CVM1(16, CM1);
10460 std::vector<Constant*> CVM2(16, CM2);
10461 Constant *C = ConstantVector::get(CVM1);
10462 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10463 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010464 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010465 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010466
10467 // r = pblendv(r, psllw(r & (char16)15, 4), a);
10468 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10469 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10470 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10471 DAG.getConstant(4, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010472 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010473 // a += a
10474 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010475
Nate Begeman51409212010-07-28 00:21:48 +000010476 C = ConstantVector::get(CVM2);
10477 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10478 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010479 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010480 false, false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010481
Nate Begeman51409212010-07-28 00:21:48 +000010482 // r = pblendv(r, psllw(r & (char16)63, 2), a);
10483 M = DAG.getNode(ISD::AND, dl, VT, R, M);
10484 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10485 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10486 DAG.getConstant(2, MVT::i32));
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010487 R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
Nate Begeman51409212010-07-28 00:21:48 +000010488 // a += a
10489 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010490
Nate Begeman51409212010-07-28 00:21:48 +000010491 // return pblendv(r, r+r, a);
Nadav Rotemfbad25e2011-09-11 15:02:23 +000010492 R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
10493 R, DAG.getNode(ISD::ADD, dl, VT, R, R));
Nate Begeman51409212010-07-28 00:21:48 +000010494 return R;
10495 }
Craig Topper46154eb2011-11-11 07:39:23 +000010496
10497 // Decompose 256-bit shifts into smaller 128-bit shifts.
10498 if (VT.getSizeInBits() == 256) {
10499 int NumElems = VT.getVectorNumElements();
10500 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10501 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10502
10503 // Extract the two vectors
10504 SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
10505 SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
10506 DAG, dl);
10507
10508 // Recreate the shift amount vectors
10509 SDValue Amt1, Amt2;
10510 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10511 // Constant shift amount
10512 SmallVector<SDValue, 4> Amt1Csts;
10513 SmallVector<SDValue, 4> Amt2Csts;
10514 for (int i = 0; i < NumElems/2; ++i)
10515 Amt1Csts.push_back(Amt->getOperand(i));
10516 for (int i = NumElems/2; i < NumElems; ++i)
10517 Amt2Csts.push_back(Amt->getOperand(i));
10518
10519 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10520 &Amt1Csts[0], NumElems/2);
10521 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10522 &Amt2Csts[0], NumElems/2);
10523 } else {
10524 // Variable shift amount
10525 Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
10526 Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
10527 DAG, dl);
10528 }
10529
10530 // Issue new vector shifts for the smaller types
10531 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10532 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10533
10534 // Concatenate the result back
10535 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10536 }
10537
Nate Begeman51409212010-07-28 00:21:48 +000010538 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010539}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010540
Dan Gohmand858e902010-04-17 15:26:15 +000010541SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010542 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10543 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010544 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10545 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010546 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010547 SDValue LHS = N->getOperand(0);
10548 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010549 unsigned BaseOp = 0;
10550 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010551 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010552 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010553 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010554 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010555 // A subtract of one will be selected as a INC. Note that INC doesn't
10556 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10558 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010559 BaseOp = X86ISD::INC;
10560 Cond = X86::COND_O;
10561 break;
10562 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010563 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010564 Cond = X86::COND_O;
10565 break;
10566 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010567 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010568 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010569 break;
10570 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010571 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10572 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010573 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10574 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010575 BaseOp = X86ISD::DEC;
10576 Cond = X86::COND_O;
10577 break;
10578 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010579 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010580 Cond = X86::COND_O;
10581 break;
10582 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010583 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010584 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010585 break;
10586 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010587 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010588 Cond = X86::COND_O;
10589 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010590 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10591 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10592 MVT::i32);
10593 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010594
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010595 SDValue SetCC =
10596 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10597 DAG.getConstant(X86::COND_O, MVT::i32),
10598 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010599
Dan Gohman6e5fda22011-07-22 18:45:15 +000010600 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010601 }
Bill Wendling74c37652008-12-09 22:08:41 +000010602 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010603
Bill Wendling61edeb52008-12-02 01:06:39 +000010604 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010605 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010606 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010607
Bill Wendling61edeb52008-12-02 01:06:39 +000010608 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010609 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10610 DAG.getConstant(Cond, MVT::i32),
10611 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010612
Dan Gohman6e5fda22011-07-22 18:45:15 +000010613 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010614}
10615
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010616SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
10617 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010618 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10619 EVT VT = Op.getValueType();
10620
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010621 if (Subtarget->hasXMMInt() && VT.isVector()) {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010622 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10623 ExtraVT.getScalarType().getSizeInBits();
10624 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10625
10626 unsigned SHLIntrinsicsID = 0;
10627 unsigned SRAIntrinsicsID = 0;
10628 switch (VT.getSimpleVT().SimpleTy) {
10629 default:
10630 return SDValue();
Craig Toppera124f942011-11-21 01:12:36 +000010631 case MVT::v4i32:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010632 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
10633 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
10634 break;
Craig Toppera124f942011-11-21 01:12:36 +000010635 case MVT::v8i16:
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010636 SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
10637 SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
10638 break;
Craig Toppera124f942011-11-21 01:12:36 +000010639 case MVT::v8i32:
10640 case MVT::v16i16:
10641 if (!Subtarget->hasAVX())
10642 return SDValue();
10643 if (!Subtarget->hasAVX2()) {
10644 // needs to be split
10645 int NumElems = VT.getVectorNumElements();
10646 SDValue Idx0 = DAG.getConstant(0, MVT::i32);
10647 SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
10648
10649 // Extract the LHS vectors
10650 SDValue LHS = Op.getOperand(0);
10651 SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
10652 SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
10653
10654 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10655 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10656
10657 EVT ExtraEltVT = ExtraVT.getVectorElementType();
10658 int ExtraNumElems = ExtraVT.getVectorNumElements();
10659 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10660 ExtraNumElems/2);
10661 SDValue Extra = DAG.getValueType(ExtraVT);
10662
10663 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10664 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
10665
10666 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10667 }
10668 if (VT == MVT::v8i32) {
10669 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_d;
10670 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_d;
10671 } else {
10672 SHLIntrinsicsID = Intrinsic::x86_avx2_pslli_w;
10673 SRAIntrinsicsID = Intrinsic::x86_avx2_psrai_w;
10674 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010675 }
10676
10677 SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10678 DAG.getConstant(SHLIntrinsicsID, MVT::i32),
Craig Toppera124f942011-11-21 01:12:36 +000010679 Op.getOperand(0), ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010680
Nadav Rotema7934dd2011-10-10 19:31:45 +000010681 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10682 DAG.getConstant(SRAIntrinsicsID, MVT::i32),
10683 Tmp1, ShAmt);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010684 }
10685
10686 return SDValue();
10687}
10688
10689
Eric Christopher9a9d2752010-07-22 02:48:34 +000010690SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10691 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010692
Eric Christopher77ed1352011-07-08 00:04:56 +000010693 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
10694 // There isn't any reason to disable it if the target processor supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010695 if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000010696 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000010697 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000010698 SDValue Ops[] = {
10699 DAG.getRegister(X86::ESP, MVT::i32), // Base
10700 DAG.getTargetConstant(1, MVT::i8), // Scale
10701 DAG.getRegister(0, MVT::i32), // Index
10702 DAG.getTargetConstant(0, MVT::i32), // Disp
10703 DAG.getRegister(0, MVT::i32), // Segment.
10704 Zero,
10705 Chain
10706 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000010707 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000010708 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10709 array_lengthof(Ops));
10710 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000010711 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010712
Eric Christopher9a9d2752010-07-22 02:48:34 +000010713 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000010714 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000010715 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010716
Chris Lattner132929a2010-08-14 17:26:09 +000010717 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10718 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
10719 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
10720 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010721
Chris Lattner132929a2010-08-14 17:26:09 +000010722 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
10723 if (!Op1 && !Op2 && !Op3 && Op4)
10724 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010725
Chris Lattner132929a2010-08-14 17:26:09 +000010726 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
10727 if (Op1 && !Op2 && !Op3 && !Op4)
10728 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010729
10730 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000010731 // (MFENCE)>;
10732 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000010733}
10734
Eli Friedman14648462011-07-27 22:21:52 +000010735SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
10736 SelectionDAG &DAG) const {
10737 DebugLoc dl = Op.getDebugLoc();
10738 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
10739 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
10740 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
10741 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
10742
10743 // The only fence that needs an instruction is a sequentially-consistent
10744 // cross-thread fence.
10745 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
10746 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
10747 // no-sse2). There isn't any reason to disable it if the target processor
10748 // supports it.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010749 if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000010750 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
10751
10752 SDValue Chain = Op.getOperand(0);
10753 SDValue Zero = DAG.getConstant(0, MVT::i32);
10754 SDValue Ops[] = {
10755 DAG.getRegister(X86::ESP, MVT::i32), // Base
10756 DAG.getTargetConstant(1, MVT::i8), // Scale
10757 DAG.getRegister(0, MVT::i32), // Index
10758 DAG.getTargetConstant(0, MVT::i32), // Disp
10759 DAG.getRegister(0, MVT::i32), // Segment.
10760 Zero,
10761 Chain
10762 };
10763 SDNode *Res =
10764 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
10765 array_lengthof(Ops));
10766 return SDValue(Res, 0);
10767 }
10768
10769 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
10770 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10771}
10772
10773
Dan Gohmand858e902010-04-17 15:26:15 +000010774SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010775 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010776 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000010777 unsigned Reg = 0;
10778 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000010779 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000010780 default:
10781 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010782 case MVT::i8: Reg = X86::AL; size = 1; break;
10783 case MVT::i16: Reg = X86::AX; size = 2; break;
10784 case MVT::i32: Reg = X86::EAX; size = 4; break;
10785 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000010786 assert(Subtarget->is64Bit() && "Node not type legal!");
10787 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000010788 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010789 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010790 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000010791 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000010792 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010793 Op.getOperand(1),
10794 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000010795 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000010796 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010797 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010798 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
10799 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
10800 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000010801 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000010802 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000010803 return cpOut;
10804}
10805
Duncan Sands1607f052008-12-01 11:39:25 +000010806SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010807 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000010808 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010809 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000010810 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010811 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010812 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000010813 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10814 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000010815 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000010816 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10817 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000010818 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000010819 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000010820 rdx.getValue(1)
10821 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000010822 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010823}
10824
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010825SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000010826 SelectionDAG &DAG) const {
10827 EVT SrcVT = Op.getOperand(0).getValueType();
10828 EVT DstVT = Op.getValueType();
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000010829 assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000010830 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010831 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000010832 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010833 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000010834 // i64 <=> MMX conversions are Legal.
10835 if (SrcVT==MVT::i64 && DstVT.isVector())
10836 return Op;
10837 if (DstVT==MVT::i64 && SrcVT.isVector())
10838 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000010839 // MMX <=> MMX conversions are Legal.
10840 if (SrcVT.isVector() && DstVT.isVector())
10841 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000010842 // All other conversions need to be expanded.
10843 return SDValue();
10844}
Chris Lattner5b856542010-12-20 00:59:46 +000010845
Dan Gohmand858e902010-04-17 15:26:15 +000010846SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010847 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000010848 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010849 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010850 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000010851 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010852 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010853 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000010854 Node->getOperand(0),
10855 Node->getOperand(1), negOp,
10856 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000010857 cast<AtomicSDNode>(Node)->getAlignment(),
10858 cast<AtomicSDNode>(Node)->getOrdering(),
10859 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000010860}
10861
Eli Friedman327236c2011-08-24 20:50:09 +000010862static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
10863 SDNode *Node = Op.getNode();
10864 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010865 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000010866
10867 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010868 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
10869 // FIXME: On 32-bit, store -> fist or movq would be more efficient
10870 // (The only way to get a 16-byte store is cmpxchg16b)
10871 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
10872 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
10873 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000010874 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
10875 cast<AtomicSDNode>(Node)->getMemoryVT(),
10876 Node->getOperand(0),
10877 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010878 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000010879 cast<AtomicSDNode>(Node)->getOrdering(),
10880 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000010881 return Swap.getValue(1);
10882 }
10883 // Other atomic stores have a simple pattern.
10884 return Op;
10885}
10886
Chris Lattner5b856542010-12-20 00:59:46 +000010887static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
10888 EVT VT = Op.getNode()->getValueType(0);
10889
10890 // Let legalize expand this if it isn't a legal type yet.
10891 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10892 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010893
Chris Lattner5b856542010-12-20 00:59:46 +000010894 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010895
Chris Lattner5b856542010-12-20 00:59:46 +000010896 unsigned Opc;
10897 bool ExtraOp = false;
10898 switch (Op.getOpcode()) {
10899 default: assert(0 && "Invalid code");
10900 case ISD::ADDC: Opc = X86ISD::ADD; break;
10901 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
10902 case ISD::SUBC: Opc = X86ISD::SUB; break;
10903 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
10904 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010905
Chris Lattner5b856542010-12-20 00:59:46 +000010906 if (!ExtraOp)
10907 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10908 Op.getOperand(1));
10909 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
10910 Op.getOperand(1), Op.getOperand(2));
10911}
10912
Evan Cheng0db9fe62006-04-25 20:13:52 +000010913/// LowerOperation - Provide custom lowering hooks for some operations.
10914///
Dan Gohmand858e902010-04-17 15:26:15 +000010915SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000010916 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010917 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010918 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000010919 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000010920 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000010921 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
10922 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000010923 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010924 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000010925 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010926 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
10927 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10928 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000010929 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000010930 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010931 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
10932 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
10933 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000010934 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000010935 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000010936 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010937 case ISD::SHL_PARTS:
10938 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000010939 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010940 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000010941 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010942 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000010943 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010944 case ISD::FABS: return LowerFABS(Op, DAG);
10945 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000010946 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000010947 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000010948 case ISD::SETCC: return LowerSETCC(Op, DAG);
10949 case ISD::SELECT: return LowerSELECT(Op, DAG);
10950 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010951 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010952 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000010953 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000010954 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010955 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010956 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
10957 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010958 case ISD::FRAME_TO_ARGS_OFFSET:
10959 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010960 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010961 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000010962 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
10963 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000010964 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000010965 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
10966 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010967 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000010968 case ISD::SRA:
10969 case ISD::SRL:
10970 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000010971 case ISD::SADDO:
10972 case ISD::UADDO:
10973 case ISD::SSUBO:
10974 case ISD::USUBO:
10975 case ISD::SMULO:
10976 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000010977 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010978 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000010979 case ISD::ADDC:
10980 case ISD::ADDE:
10981 case ISD::SUBC:
10982 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000010983 case ISD::ADD: return LowerADD(Op, DAG);
10984 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010985 }
Chris Lattner27a6c732007-11-24 07:07:01 +000010986}
10987
Eli Friedmanf8f90f02011-08-24 22:33:28 +000010988static void ReplaceATOMIC_LOAD(SDNode *Node,
10989 SmallVectorImpl<SDValue> &Results,
10990 SelectionDAG &DAG) {
10991 DebugLoc dl = Node->getDebugLoc();
10992 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
10993
10994 // Convert wide load -> cmpxchg8b/cmpxchg16b
10995 // FIXME: On 32-bit, load -> fild or movq would be more efficient
10996 // (The only way to get a 16-byte load is cmpxchg16b)
10997 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000010998 SDValue Zero = DAG.getConstant(0, VT);
10999 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011000 Node->getOperand(0),
11001 Node->getOperand(1), Zero, Zero,
11002 cast<AtomicSDNode>(Node)->getMemOperand(),
11003 cast<AtomicSDNode>(Node)->getOrdering(),
11004 cast<AtomicSDNode>(Node)->getSynchScope());
11005 Results.push_back(Swap.getValue(0));
11006 Results.push_back(Swap.getValue(1));
11007}
11008
Duncan Sands1607f052008-12-01 11:39:25 +000011009void X86TargetLowering::
11010ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011011 SelectionDAG &DAG, unsigned NewOp) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011012 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011013 assert (Node->getValueType(0) == MVT::i64 &&
11014 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011015
11016 SDValue Chain = Node->getOperand(0);
11017 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011018 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011019 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011020 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011021 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011022 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011023 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011024 SDValue Result =
11025 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11026 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011027 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011028 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011029 Results.push_back(Result.getValue(2));
11030}
11031
Duncan Sands126d9072008-07-04 11:47:58 +000011032/// ReplaceNodeResults - Replace a node with an illegal result type
11033/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011034void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11035 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011036 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011037 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011038 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011039 default:
Duncan Sands1607f052008-12-01 11:39:25 +000011040 assert(false && "Do not know how to custom type legalize this operation!");
11041 return;
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011042 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011043 case ISD::ADDC:
11044 case ISD::ADDE:
11045 case ISD::SUBC:
11046 case ISD::SUBE:
11047 // We don't want to expand or promote these.
11048 return;
Duncan Sands1607f052008-12-01 11:39:25 +000011049 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +000011050 std::pair<SDValue,SDValue> Vals =
11051 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +000011052 SDValue FIST = Vals.first, StackSlot = Vals.second;
11053 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011054 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011055 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +000011056 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011057 MachinePointerInfo(),
11058 false, false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +000011059 }
11060 return;
11061 }
11062 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011063 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011064 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011065 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011066 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011067 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011068 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011069 eax.getValue(2));
11070 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11071 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011072 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011073 Results.push_back(edx.getValue(1));
11074 return;
11075 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011076 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011077 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011078 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011079 bool Regs64bit = T == MVT::i128;
11080 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011081 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011082 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11083 DAG.getConstant(0, HalfT));
11084 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11085 DAG.getConstant(1, HalfT));
11086 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11087 Regs64bit ? X86::RAX : X86::EAX,
11088 cpInL, SDValue());
11089 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11090 Regs64bit ? X86::RDX : X86::EDX,
11091 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011092 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011093 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11094 DAG.getConstant(0, HalfT));
11095 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11096 DAG.getConstant(1, HalfT));
11097 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11098 Regs64bit ? X86::RBX : X86::EBX,
11099 swapInL, cpInH.getValue(1));
11100 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
11101 Regs64bit ? X86::RCX : X86::ECX,
11102 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011103 SDValue Ops[] = { swapInH.getValue(0),
11104 N->getOperand(1),
11105 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011106 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011107 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011108 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11109 X86ISD::LCMPXCHG8_DAG;
11110 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011111 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011112 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11113 Regs64bit ? X86::RAX : X86::EAX,
11114 HalfT, Result.getValue(1));
11115 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11116 Regs64bit ? X86::RDX : X86::EDX,
11117 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011118 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011119 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011120 Results.push_back(cpOutH.getValue(1));
11121 return;
11122 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011123 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +000011124 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
11125 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011126 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +000011127 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
11128 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011129 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +000011130 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
11131 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011132 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +000011133 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
11134 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011135 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +000011136 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
11137 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011138 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +000011139 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
11140 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011141 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +000011142 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
11143 return;
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011144 case ISD::ATOMIC_LOAD:
11145 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011146 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011147}
11148
Evan Cheng72261582005-12-20 06:22:03 +000011149const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11150 switch (Opcode) {
11151 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011152 case X86ISD::BSF: return "X86ISD::BSF";
11153 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011154 case X86ISD::SHLD: return "X86ISD::SHLD";
11155 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011156 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011157 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011158 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011159 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011160 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011161 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011162 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11163 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11164 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011165 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011166 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011167 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011168 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011169 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011170 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011171 case X86ISD::COMI: return "X86ISD::COMI";
11172 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011173 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011174 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011175 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11176 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011177 case X86ISD::CMOV: return "X86ISD::CMOV";
11178 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011179 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011180 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11181 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011182 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011183 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011184 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011185 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011186 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011187 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11188 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011189 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011190 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011191 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011192 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011193 case X86ISD::BLENDV: return "X86ISD::BLENDV";
11194 case X86ISD::FHADD: return "X86ISD::FHADD";
11195 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011196 case X86ISD::FMAX: return "X86ISD::FMAX";
11197 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +000011198 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11199 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011200 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011201 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011202 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011203 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011204 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011205 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11206 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011207 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11208 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11209 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11210 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11211 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11212 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011213 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
11214 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +000011215 case X86ISD::VSHL: return "X86ISD::VSHL";
11216 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +000011217 case X86ISD::CMPPD: return "X86ISD::CMPPD";
11218 case X86ISD::CMPPS: return "X86ISD::CMPPS";
11219 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
11220 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
11221 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
11222 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
11223 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
11224 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
11225 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
11226 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011227 case X86ISD::ADD: return "X86ISD::ADD";
11228 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011229 case X86ISD::ADC: return "X86ISD::ADC";
11230 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011231 case X86ISD::SMUL: return "X86ISD::SMUL";
11232 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011233 case X86ISD::INC: return "X86ISD::INC";
11234 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011235 case X86ISD::OR: return "X86ISD::OR";
11236 case X86ISD::XOR: return "X86ISD::XOR";
11237 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011238 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011239 case X86ISD::BLSI: return "X86ISD::BLSI";
11240 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11241 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011242 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011243 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011244 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011245 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11246 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11247 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
11248 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
11249 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
11250 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
11251 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
11252 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
11253 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011254 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011255 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011256 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011257 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11258 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011259 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11260 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11261 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
11262 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
11263 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
11264 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11265 case X86ISD::MOVSS: return "X86ISD::MOVSS";
11266 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
11267 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
Craig Topper6347e862011-11-21 06:57:39 +000011268 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
David Greenefbf05d32011-02-22 23:31:46 +000011269 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011270 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
11271 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
11272 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
11273 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
11274 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
11275 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
Craig Topper6fa583d2011-11-21 08:26:50 +000011276 case X86ISD::VPUNPCKLBWY: return "X86ISD::VPUNPCKLBWY";
Craig Topper6347e862011-11-21 06:57:39 +000011277 case X86ISD::VPUNPCKLWDY: return "X86ISD::VPUNPCKLWDY";
11278 case X86ISD::VPUNPCKLDQY: return "X86ISD::VPUNPCKLDQY";
11279 case X86ISD::VPUNPCKLQDQY: return "X86ISD::VPUNPCKLQDQY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011280 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
11281 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
11282 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
11283 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Craig Topper6fa583d2011-11-21 08:26:50 +000011284 case X86ISD::VPUNPCKHBWY: return "X86ISD::VPUNPCKHBWY";
Craig Topper6347e862011-11-21 06:57:39 +000011285 case X86ISD::VPUNPCKHWDY: return "X86ISD::VPUNPCKHWDY";
11286 case X86ISD::VPUNPCKHDQY: return "X86ISD::VPUNPCKHDQY";
11287 case X86ISD::VPUNPCKHQDQY: return "X86ISD::VPUNPCKHQDQY";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011288 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000011289 case X86ISD::VPERMILPS: return "X86ISD::VPERMILPS";
11290 case X86ISD::VPERMILPSY: return "X86ISD::VPERMILPSY";
11291 case X86ISD::VPERMILPD: return "X86ISD::VPERMILPD";
11292 case X86ISD::VPERMILPDY: return "X86ISD::VPERMILPDY";
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000011293 case X86ISD::VPERM2F128: return "X86ISD::VPERM2F128";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011294 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011295 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011296 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011297 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011298 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +000011299 }
11300}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011301
Chris Lattnerc9addb72007-03-30 23:15:24 +000011302// isLegalAddressingMode - Return true if the addressing mode represented
11303// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011304bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011305 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011306 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011307 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011308 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011309
Chris Lattnerc9addb72007-03-30 23:15:24 +000011310 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011311 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011312 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011313
Chris Lattnerc9addb72007-03-30 23:15:24 +000011314 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011315 unsigned GVFlags =
11316 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011317
Chris Lattnerdfed4132009-07-10 07:38:24 +000011318 // If a reference to this global requires an extra load, we can't fold it.
11319 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011320 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011321
Chris Lattnerdfed4132009-07-10 07:38:24 +000011322 // If BaseGV requires a register for the PIC base, we cannot also have a
11323 // BaseReg specified.
11324 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011325 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011326
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011327 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011328 if ((M != CodeModel::Small || R != Reloc::Static) &&
11329 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011330 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011331 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011332
Chris Lattnerc9addb72007-03-30 23:15:24 +000011333 switch (AM.Scale) {
11334 case 0:
11335 case 1:
11336 case 2:
11337 case 4:
11338 case 8:
11339 // These scales always work.
11340 break;
11341 case 3:
11342 case 5:
11343 case 9:
11344 // These scales are formed with basereg+scalereg. Only accept if there is
11345 // no basereg yet.
11346 if (AM.HasBaseReg)
11347 return false;
11348 break;
11349 default: // Other stuff never works.
11350 return false;
11351 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011352
Chris Lattnerc9addb72007-03-30 23:15:24 +000011353 return true;
11354}
11355
11356
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011357bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011358 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011359 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011360 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11361 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011362 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011363 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011364 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011365}
11366
Owen Andersone50ed302009-08-10 22:56:29 +000011367bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011368 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011369 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011370 unsigned NumBits1 = VT1.getSizeInBits();
11371 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011372 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011373 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011374 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011375}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011376
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011377bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011378 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011379 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011380}
11381
Owen Andersone50ed302009-08-10 22:56:29 +000011382bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011383 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011384 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011385}
11386
Owen Andersone50ed302009-08-10 22:56:29 +000011387bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011388 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011389 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011390}
11391
Evan Cheng60c07e12006-07-05 22:17:51 +000011392/// isShuffleMaskLegal - Targets can use this to indicate that they only
11393/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11394/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11395/// are assumed to be legal.
11396bool
Eric Christopherfd179292009-08-27 18:07:15 +000011397X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011398 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011399 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011400 if (VT.getSizeInBits() == 64)
Craig Topperc0d82852011-11-22 00:44:41 +000011401 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX());
Nate Begeman9008ca62009-04-27 18:41:29 +000011402
Nate Begemana09008b2009-10-19 02:17:23 +000011403 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011404 return (VT.getVectorNumElements() == 2 ||
11405 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11406 isMOVLMask(M, VT) ||
11407 isSHUFPMask(M, VT) ||
11408 isPSHUFDMask(M, VT) ||
11409 isPSHUFHWMask(M, VT) ||
11410 isPSHUFLWMask(M, VT) ||
Craig Topperc0d82852011-11-22 00:44:41 +000011411 isPALIGNRMask(M, VT, Subtarget->hasSSSE3orAVX()) ||
Craig Topper6347e862011-11-21 06:57:39 +000011412 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11413 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011414 isUNPCKL_v_undef_Mask(M, VT) ||
11415 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011416}
11417
Dan Gohman7d8143f2008-04-09 20:09:42 +000011418bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011419X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011420 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011421 unsigned NumElts = VT.getVectorNumElements();
11422 // FIXME: This collection of masks seems suspect.
11423 if (NumElts == 2)
11424 return true;
11425 if (NumElts == 4 && VT.getSizeInBits() == 128) {
11426 return (isMOVLMask(Mask, VT) ||
11427 isCommutedMOVLMask(Mask, VT, true) ||
11428 isSHUFPMask(Mask, VT) ||
11429 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +000011430 }
11431 return false;
11432}
11433
11434//===----------------------------------------------------------------------===//
11435// X86 Scheduler Hooks
11436//===----------------------------------------------------------------------===//
11437
Mon P Wang63307c32008-05-05 19:05:59 +000011438// private utility function
11439MachineBasicBlock *
11440X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11441 MachineBasicBlock *MBB,
11442 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011443 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011444 unsigned LoadOpc,
11445 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011446 unsigned notOpc,
11447 unsigned EAXreg,
11448 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011449 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011450 // For the atomic bitwise operator, we generate
11451 // thisMBB:
11452 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011453 // ld t1 = [bitinstr.addr]
11454 // op t2 = t1, [bitinstr.val]
11455 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011456 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11457 // bz newMBB
11458 // fallthrough -->nextMBB
11459 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11460 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011461 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011462 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011463
Mon P Wang63307c32008-05-05 19:05:59 +000011464 /// First build the CFG
11465 MachineFunction *F = MBB->getParent();
11466 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011467 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11468 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11469 F->insert(MBBIter, newMBB);
11470 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011471
Dan Gohman14152b42010-07-06 20:24:04 +000011472 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11473 nextMBB->splice(nextMBB->begin(), thisMBB,
11474 llvm::next(MachineBasicBlock::iterator(bInstr)),
11475 thisMBB->end());
11476 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011477
Mon P Wang63307c32008-05-05 19:05:59 +000011478 // Update thisMBB to fall through to newMBB
11479 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011480
Mon P Wang63307c32008-05-05 19:05:59 +000011481 // newMBB jumps to itself and fall through to nextMBB
11482 newMBB->addSuccessor(nextMBB);
11483 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011484
Mon P Wang63307c32008-05-05 19:05:59 +000011485 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011486 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011487 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011488 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011489 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011490 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011491 int numArgs = bInstr->getNumOperands() - 1;
11492 for (int i=0; i < numArgs; ++i)
11493 argOpers[i] = &bInstr->getOperand(i+1);
11494
11495 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011496 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011497 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011498
Dale Johannesen140be2d2008-08-19 18:47:28 +000011499 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011500 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011501 for (int i=0; i <= lastAddrIndx; ++i)
11502 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011503
Dale Johannesen140be2d2008-08-19 18:47:28 +000011504 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011505 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011506 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011507 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011508 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011509 tt = t1;
11510
Dale Johannesen140be2d2008-08-19 18:47:28 +000011511 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011512 assert((argOpers[valArgIndx]->isReg() ||
11513 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011514 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011515 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011516 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011517 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011518 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011519 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +000011520 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011521
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011522 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +000011523 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011524
Dale Johannesene4d209d2009-02-03 20:21:25 +000011525 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011526 for (int i=0; i <= lastAddrIndx; ++i)
11527 (*MIB).addOperand(*argOpers[i]);
11528 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +000011529 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011530 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11531 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011532
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011533 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011534 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011535
Mon P Wang63307c32008-05-05 19:05:59 +000011536 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011537 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011538
Dan Gohman14152b42010-07-06 20:24:04 +000011539 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011540 return nextMBB;
11541}
11542
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011543// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011544MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011545X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11546 MachineBasicBlock *MBB,
11547 unsigned regOpcL,
11548 unsigned regOpcH,
11549 unsigned immOpcL,
11550 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011551 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011552 // For the atomic bitwise operator, we generate
11553 // thisMBB (instructions are in pairs, except cmpxchg8b)
11554 // ld t1,t2 = [bitinstr.addr]
11555 // newMBB:
11556 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11557 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011558 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011559 // mov ECX, EBX <- t5, t6
11560 // mov EAX, EDX <- t1, t2
11561 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11562 // mov t3, t4 <- EAX, EDX
11563 // bz newMBB
11564 // result in out1, out2
11565 // fallthrough -->nextMBB
11566
11567 const TargetRegisterClass *RC = X86::GR32RegisterClass;
11568 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011569 const unsigned NotOpc = X86::NOT32r;
11570 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11571 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11572 MachineFunction::iterator MBBIter = MBB;
11573 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011574
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011575 /// First build the CFG
11576 MachineFunction *F = MBB->getParent();
11577 MachineBasicBlock *thisMBB = MBB;
11578 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11579 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11580 F->insert(MBBIter, newMBB);
11581 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011582
Dan Gohman14152b42010-07-06 20:24:04 +000011583 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11584 nextMBB->splice(nextMBB->begin(), thisMBB,
11585 llvm::next(MachineBasicBlock::iterator(bInstr)),
11586 thisMBB->end());
11587 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011588
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011589 // Update thisMBB to fall through to newMBB
11590 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011591
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011592 // newMBB jumps to itself and fall through to nextMBB
11593 newMBB->addSuccessor(nextMBB);
11594 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011595
Dale Johannesene4d209d2009-02-03 20:21:25 +000011596 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011597 // Insert instructions into newMBB based on incoming instruction
11598 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011599 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011600 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011601 MachineOperand& dest1Oper = bInstr->getOperand(0);
11602 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011603 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11604 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011605 argOpers[i] = &bInstr->getOperand(i+2);
11606
Dan Gohman71ea4e52010-05-14 21:01:44 +000011607 // We use some of the operands multiple times, so conservatively just
11608 // clear any kill flags that might be present.
11609 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11610 argOpers[i]->setIsKill(false);
11611 }
11612
Evan Chengad5b52f2010-01-08 19:14:57 +000011613 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011614 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011615
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011616 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011617 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011618 for (int i=0; i <= lastAddrIndx; ++i)
11619 (*MIB).addOperand(*argOpers[i]);
11620 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011621 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011622 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011623 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011624 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011625 MachineOperand newOp3 = *(argOpers[3]);
11626 if (newOp3.isImm())
11627 newOp3.setImm(newOp3.getImm()+4);
11628 else
11629 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011630 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011631 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011632
11633 // t3/4 are defined later, at the bottom of the loop
11634 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11635 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011636 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011637 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011638 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011639 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11640
Evan Cheng306b4ca2010-01-08 23:41:50 +000011641 // The subsequent operations should be using the destination registers of
11642 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +000011643 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011644 t1 = F->getRegInfo().createVirtualRegister(RC);
11645 t2 = F->getRegInfo().createVirtualRegister(RC);
11646 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11647 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011648 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +000011649 t1 = dest1Oper.getReg();
11650 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011651 }
11652
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011653 int valArgIndx = lastAddrIndx + 1;
11654 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011655 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011656 "invalid operand");
11657 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11658 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011659 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011660 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011661 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011662 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011663 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011664 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011665 (*MIB).addOperand(*argOpers[valArgIndx]);
11666 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011667 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011668 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000011669 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011670 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011671 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011672 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011673 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000011674 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011675 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011676 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011677
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011678 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011679 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011680 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011681 MIB.addReg(t2);
11682
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011683 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011684 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011685 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011686 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +000011687
Dale Johannesene4d209d2009-02-03 20:21:25 +000011688 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011689 for (int i=0; i <= lastAddrIndx; ++i)
11690 (*MIB).addOperand(*argOpers[i]);
11691
11692 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011693 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11694 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011695
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011696 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011697 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011698 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011699 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011700
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011701 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011702 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011703
Dan Gohman14152b42010-07-06 20:24:04 +000011704 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011705 return nextMBB;
11706}
11707
11708// private utility function
11709MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000011710X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11711 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000011712 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011713 // For the atomic min/max operator, we generate
11714 // thisMBB:
11715 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011716 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000011717 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000011718 // cmp t1, t2
11719 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000011720 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000011721 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
11722 // bz newMBB
11723 // fallthrough -->nextMBB
11724 //
11725 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11726 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011727 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011728 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011729
Mon P Wang63307c32008-05-05 19:05:59 +000011730 /// First build the CFG
11731 MachineFunction *F = MBB->getParent();
11732 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011733 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11734 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11735 F->insert(MBBIter, newMBB);
11736 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011737
Dan Gohman14152b42010-07-06 20:24:04 +000011738 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11739 nextMBB->splice(nextMBB->begin(), thisMBB,
11740 llvm::next(MachineBasicBlock::iterator(mInstr)),
11741 thisMBB->end());
11742 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011743
Mon P Wang63307c32008-05-05 19:05:59 +000011744 // Update thisMBB to fall through to newMBB
11745 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011746
Mon P Wang63307c32008-05-05 19:05:59 +000011747 // newMBB jumps to newMBB and fall through to nextMBB
11748 newMBB->addSuccessor(nextMBB);
11749 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011750
Dale Johannesene4d209d2009-02-03 20:21:25 +000011751 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011752 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011753 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011754 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000011755 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011756 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011757 int numArgs = mInstr->getNumOperands() - 1;
11758 for (int i=0; i < numArgs; ++i)
11759 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011760
Mon P Wang63307c32008-05-05 19:05:59 +000011761 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011762 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011763 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011764
Mon P Wangab3e7472008-05-05 22:56:23 +000011765 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011766 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011767 for (int i=0; i <= lastAddrIndx; ++i)
11768 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000011769
Mon P Wang63307c32008-05-05 19:05:59 +000011770 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000011771 assert((argOpers[valArgIndx]->isReg() ||
11772 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011773 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000011774
11775 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000011776 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011777 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000011778 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011779 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011780 (*MIB).addOperand(*argOpers[valArgIndx]);
11781
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011782 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000011783 MIB.addReg(t1);
11784
Dale Johannesene4d209d2009-02-03 20:21:25 +000011785 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000011786 MIB.addReg(t1);
11787 MIB.addReg(t2);
11788
11789 // Generate movc
11790 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011791 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000011792 MIB.addReg(t2);
11793 MIB.addReg(t1);
11794
11795 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000011796 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000011797 for (int i=0; i <= lastAddrIndx; ++i)
11798 (*MIB).addOperand(*argOpers[i]);
11799 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011800 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011801 (*MIB).setMemRefs(mInstr->memoperands_begin(),
11802 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000011803
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011804 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000011805 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000011806
Mon P Wang63307c32008-05-05 19:05:59 +000011807 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011808 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011809
Dan Gohman14152b42010-07-06 20:24:04 +000011810 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011811 return nextMBB;
11812}
11813
Eric Christopherf83a5de2009-08-27 18:08:16 +000011814// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011815// or XMM0_V32I8 in AVX all of this code can be replaced with that
11816// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000011817MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000011818X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000011819 unsigned numArgs, bool memArg) const {
Craig Topperc0d82852011-11-22 00:44:41 +000011820 assert(Subtarget->hasSSE42orAVX() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011821 "Target must have SSE4.2 or AVX features enabled");
11822
Eric Christopherb120ab42009-08-18 22:50:32 +000011823 DebugLoc dl = MI->getDebugLoc();
11824 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000011825 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000011826 if (!Subtarget->hasAVX()) {
11827 if (memArg)
11828 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11829 else
11830 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11831 } else {
11832 if (memArg)
11833 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11834 else
11835 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11836 }
Eric Christopherb120ab42009-08-18 22:50:32 +000011837
Eric Christopher41c902f2010-11-30 08:20:21 +000011838 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000011839 for (unsigned i = 0; i < numArgs; ++i) {
11840 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000011841 if (!(Op.isReg() && Op.isImplicit()))
11842 MIB.addOperand(Op);
11843 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000011844 BuildMI(*BB, MI, dl,
11845 TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
11846 MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000011847 .addReg(X86::XMM0);
11848
Dan Gohman14152b42010-07-06 20:24:04 +000011849 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000011850 return BB;
11851}
11852
11853MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000011854X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011855 DebugLoc dl = MI->getDebugLoc();
11856 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011857
Eric Christopher228232b2010-11-30 07:20:12 +000011858 // Address into RAX/EAX, other two args into ECX, EDX.
11859 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
11860 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
11861 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
11862 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000011863 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011864
Eric Christopher228232b2010-11-30 07:20:12 +000011865 unsigned ValOps = X86::AddrNumOperands;
11866 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11867 .addReg(MI->getOperand(ValOps).getReg());
11868 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
11869 .addReg(MI->getOperand(ValOps+1).getReg());
11870
11871 // The instruction doesn't actually take any operands though.
11872 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011873
Eric Christopher228232b2010-11-30 07:20:12 +000011874 MI->eraseFromParent(); // The pseudo is gone now.
11875 return BB;
11876}
11877
11878MachineBasicBlock *
11879X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000011880 DebugLoc dl = MI->getDebugLoc();
11881 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011882
Eric Christopher228232b2010-11-30 07:20:12 +000011883 // First arg in ECX, the second in EAX.
11884 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
11885 .addReg(MI->getOperand(0).getReg());
11886 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
11887 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011888
Eric Christopher228232b2010-11-30 07:20:12 +000011889 // The instruction doesn't actually take any operands though.
11890 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011891
Eric Christopher228232b2010-11-30 07:20:12 +000011892 MI->eraseFromParent(); // The pseudo is gone now.
11893 return BB;
11894}
11895
11896MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000011897X86TargetLowering::EmitVAARG64WithCustomInserter(
11898 MachineInstr *MI,
11899 MachineBasicBlock *MBB) const {
11900 // Emit va_arg instruction on X86-64.
11901
11902 // Operands to this pseudo-instruction:
11903 // 0 ) Output : destination address (reg)
11904 // 1-5) Input : va_list address (addr, i64mem)
11905 // 6 ) ArgSize : Size (in bytes) of vararg type
11906 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
11907 // 8 ) Align : Alignment of type
11908 // 9 ) EFLAGS (implicit-def)
11909
11910 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
11911 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
11912
11913 unsigned DestReg = MI->getOperand(0).getReg();
11914 MachineOperand &Base = MI->getOperand(1);
11915 MachineOperand &Scale = MI->getOperand(2);
11916 MachineOperand &Index = MI->getOperand(3);
11917 MachineOperand &Disp = MI->getOperand(4);
11918 MachineOperand &Segment = MI->getOperand(5);
11919 unsigned ArgSize = MI->getOperand(6).getImm();
11920 unsigned ArgMode = MI->getOperand(7).getImm();
11921 unsigned Align = MI->getOperand(8).getImm();
11922
11923 // Memory Reference
11924 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
11925 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
11926 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
11927
11928 // Machine Information
11929 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11930 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
11931 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
11932 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
11933 DebugLoc DL = MI->getDebugLoc();
11934
11935 // struct va_list {
11936 // i32 gp_offset
11937 // i32 fp_offset
11938 // i64 overflow_area (address)
11939 // i64 reg_save_area (address)
11940 // }
11941 // sizeof(va_list) = 24
11942 // alignment(va_list) = 8
11943
11944 unsigned TotalNumIntRegs = 6;
11945 unsigned TotalNumXMMRegs = 8;
11946 bool UseGPOffset = (ArgMode == 1);
11947 bool UseFPOffset = (ArgMode == 2);
11948 unsigned MaxOffset = TotalNumIntRegs * 8 +
11949 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
11950
11951 /* Align ArgSize to a multiple of 8 */
11952 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
11953 bool NeedsAlign = (Align > 8);
11954
11955 MachineBasicBlock *thisMBB = MBB;
11956 MachineBasicBlock *overflowMBB;
11957 MachineBasicBlock *offsetMBB;
11958 MachineBasicBlock *endMBB;
11959
11960 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
11961 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
11962 unsigned OffsetReg = 0;
11963
11964 if (!UseGPOffset && !UseFPOffset) {
11965 // If we only pull from the overflow region, we don't create a branch.
11966 // We don't need to alter control flow.
11967 OffsetDestReg = 0; // unused
11968 OverflowDestReg = DestReg;
11969
11970 offsetMBB = NULL;
11971 overflowMBB = thisMBB;
11972 endMBB = thisMBB;
11973 } else {
11974 // First emit code to check if gp_offset (or fp_offset) is below the bound.
11975 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
11976 // If not, pull from overflow_area. (branch to overflowMBB)
11977 //
11978 // thisMBB
11979 // | .
11980 // | .
11981 // offsetMBB overflowMBB
11982 // | .
11983 // | .
11984 // endMBB
11985
11986 // Registers for the PHI in endMBB
11987 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
11988 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
11989
11990 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11991 MachineFunction *MF = MBB->getParent();
11992 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11993 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11994 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
11995
11996 MachineFunction::iterator MBBIter = MBB;
11997 ++MBBIter;
11998
11999 // Insert the new basic blocks
12000 MF->insert(MBBIter, offsetMBB);
12001 MF->insert(MBBIter, overflowMBB);
12002 MF->insert(MBBIter, endMBB);
12003
12004 // Transfer the remainder of MBB and its successor edges to endMBB.
12005 endMBB->splice(endMBB->begin(), thisMBB,
12006 llvm::next(MachineBasicBlock::iterator(MI)),
12007 thisMBB->end());
12008 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12009
12010 // Make offsetMBB and overflowMBB successors of thisMBB
12011 thisMBB->addSuccessor(offsetMBB);
12012 thisMBB->addSuccessor(overflowMBB);
12013
12014 // endMBB is a successor of both offsetMBB and overflowMBB
12015 offsetMBB->addSuccessor(endMBB);
12016 overflowMBB->addSuccessor(endMBB);
12017
12018 // Load the offset value into a register
12019 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12020 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12021 .addOperand(Base)
12022 .addOperand(Scale)
12023 .addOperand(Index)
12024 .addDisp(Disp, UseFPOffset ? 4 : 0)
12025 .addOperand(Segment)
12026 .setMemRefs(MMOBegin, MMOEnd);
12027
12028 // Check if there is enough room left to pull this argument.
12029 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12030 .addReg(OffsetReg)
12031 .addImm(MaxOffset + 8 - ArgSizeA8);
12032
12033 // Branch to "overflowMBB" if offset >= max
12034 // Fall through to "offsetMBB" otherwise
12035 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12036 .addMBB(overflowMBB);
12037 }
12038
12039 // In offsetMBB, emit code to use the reg_save_area.
12040 if (offsetMBB) {
12041 assert(OffsetReg != 0);
12042
12043 // Read the reg_save_area address.
12044 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12045 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12046 .addOperand(Base)
12047 .addOperand(Scale)
12048 .addOperand(Index)
12049 .addDisp(Disp, 16)
12050 .addOperand(Segment)
12051 .setMemRefs(MMOBegin, MMOEnd);
12052
12053 // Zero-extend the offset
12054 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12055 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12056 .addImm(0)
12057 .addReg(OffsetReg)
12058 .addImm(X86::sub_32bit);
12059
12060 // Add the offset to the reg_save_area to get the final address.
12061 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12062 .addReg(OffsetReg64)
12063 .addReg(RegSaveReg);
12064
12065 // Compute the offset for the next argument
12066 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12067 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12068 .addReg(OffsetReg)
12069 .addImm(UseFPOffset ? 16 : 8);
12070
12071 // Store it back into the va_list.
12072 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12073 .addOperand(Base)
12074 .addOperand(Scale)
12075 .addOperand(Index)
12076 .addDisp(Disp, UseFPOffset ? 4 : 0)
12077 .addOperand(Segment)
12078 .addReg(NextOffsetReg)
12079 .setMemRefs(MMOBegin, MMOEnd);
12080
12081 // Jump to endMBB
12082 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12083 .addMBB(endMBB);
12084 }
12085
12086 //
12087 // Emit code to use overflow area
12088 //
12089
12090 // Load the overflow_area address into a register.
12091 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12092 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12093 .addOperand(Base)
12094 .addOperand(Scale)
12095 .addOperand(Index)
12096 .addDisp(Disp, 8)
12097 .addOperand(Segment)
12098 .setMemRefs(MMOBegin, MMOEnd);
12099
12100 // If we need to align it, do so. Otherwise, just copy the address
12101 // to OverflowDestReg.
12102 if (NeedsAlign) {
12103 // Align the overflow address
12104 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12105 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12106
12107 // aligned_addr = (addr + (align-1)) & ~(align-1)
12108 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12109 .addReg(OverflowAddrReg)
12110 .addImm(Align-1);
12111
12112 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12113 .addReg(TmpReg)
12114 .addImm(~(uint64_t)(Align-1));
12115 } else {
12116 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12117 .addReg(OverflowAddrReg);
12118 }
12119
12120 // Compute the next overflow address after this argument.
12121 // (the overflow address should be kept 8-byte aligned)
12122 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12123 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12124 .addReg(OverflowDestReg)
12125 .addImm(ArgSizeA8);
12126
12127 // Store the new overflow address.
12128 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12129 .addOperand(Base)
12130 .addOperand(Scale)
12131 .addOperand(Index)
12132 .addDisp(Disp, 8)
12133 .addOperand(Segment)
12134 .addReg(NextAddrReg)
12135 .setMemRefs(MMOBegin, MMOEnd);
12136
12137 // If we branched, emit the PHI to the front of endMBB.
12138 if (offsetMBB) {
12139 BuildMI(*endMBB, endMBB->begin(), DL,
12140 TII->get(X86::PHI), DestReg)
12141 .addReg(OffsetDestReg).addMBB(offsetMBB)
12142 .addReg(OverflowDestReg).addMBB(overflowMBB);
12143 }
12144
12145 // Erase the pseudo instruction
12146 MI->eraseFromParent();
12147
12148 return endMBB;
12149}
12150
12151MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012152X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12153 MachineInstr *MI,
12154 MachineBasicBlock *MBB) const {
12155 // Emit code to save XMM registers to the stack. The ABI says that the
12156 // number of registers to save is given in %al, so it's theoretically
12157 // possible to do an indirect jump trick to avoid saving all of them,
12158 // however this code takes a simpler approach and just executes all
12159 // of the stores if %al is non-zero. It's less code, and it's probably
12160 // easier on the hardware branch predictor, and stores aren't all that
12161 // expensive anyway.
12162
12163 // Create the new basic blocks. One block contains all the XMM stores,
12164 // and one block is the final destination regardless of whether any
12165 // stores were performed.
12166 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12167 MachineFunction *F = MBB->getParent();
12168 MachineFunction::iterator MBBIter = MBB;
12169 ++MBBIter;
12170 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12171 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12172 F->insert(MBBIter, XMMSaveMBB);
12173 F->insert(MBBIter, EndMBB);
12174
Dan Gohman14152b42010-07-06 20:24:04 +000012175 // Transfer the remainder of MBB and its successor edges to EndMBB.
12176 EndMBB->splice(EndMBB->begin(), MBB,
12177 llvm::next(MachineBasicBlock::iterator(MI)),
12178 MBB->end());
12179 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12180
Dan Gohmand6708ea2009-08-15 01:38:56 +000012181 // The original block will now fall through to the XMM save block.
12182 MBB->addSuccessor(XMMSaveMBB);
12183 // The XMMSaveMBB will fall through to the end block.
12184 XMMSaveMBB->addSuccessor(EndMBB);
12185
12186 // Now add the instructions.
12187 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12188 DebugLoc DL = MI->getDebugLoc();
12189
12190 unsigned CountReg = MI->getOperand(0).getReg();
12191 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12192 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12193
12194 if (!Subtarget->isTargetWin64()) {
12195 // If %al is 0, branch around the XMM save block.
12196 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012197 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012198 MBB->addSuccessor(EndMBB);
12199 }
12200
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012201 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012202 // In the XMM save block, save all the XMM argument registers.
12203 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12204 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012205 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012206 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012207 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012208 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012209 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012210 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012211 .addFrameIndex(RegSaveFrameIndex)
12212 .addImm(/*Scale=*/1)
12213 .addReg(/*IndexReg=*/0)
12214 .addImm(/*Disp=*/Offset)
12215 .addReg(/*Segment=*/0)
12216 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012217 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012218 }
12219
Dan Gohman14152b42010-07-06 20:24:04 +000012220 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012221
12222 return EndMBB;
12223}
Mon P Wang63307c32008-05-05 19:05:59 +000012224
Evan Cheng60c07e12006-07-05 22:17:51 +000012225MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012226X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012227 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12229 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012230
Chris Lattner52600972009-09-02 05:57:00 +000012231 // To "insert" a SELECT_CC instruction, we actually have to insert the
12232 // diamond control-flow pattern. The incoming instruction knows the
12233 // destination vreg to set, the condition code register to branch on, the
12234 // true/false values to select between, and a branch opcode to use.
12235 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12236 MachineFunction::iterator It = BB;
12237 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012238
Chris Lattner52600972009-09-02 05:57:00 +000012239 // thisMBB:
12240 // ...
12241 // TrueVal = ...
12242 // cmpTY ccX, r1, r2
12243 // bCC copy1MBB
12244 // fallthrough --> copy0MBB
12245 MachineBasicBlock *thisMBB = BB;
12246 MachineFunction *F = BB->getParent();
12247 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12248 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012249 F->insert(It, copy0MBB);
12250 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012251
Bill Wendling730c07e2010-06-25 20:48:10 +000012252 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12253 // live into the sink and copy blocks.
Jakob Stoklund Olesen4a1b9d82011-09-02 23:52:49 +000012254 if (!MI->killsRegister(X86::EFLAGS)) {
12255 copy0MBB->addLiveIn(X86::EFLAGS);
12256 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012257 }
12258
Dan Gohman14152b42010-07-06 20:24:04 +000012259 // Transfer the remainder of BB and its successor edges to sinkMBB.
12260 sinkMBB->splice(sinkMBB->begin(), BB,
12261 llvm::next(MachineBasicBlock::iterator(MI)),
12262 BB->end());
12263 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12264
12265 // Add the true and fallthrough blocks as its successors.
12266 BB->addSuccessor(copy0MBB);
12267 BB->addSuccessor(sinkMBB);
12268
12269 // Create the conditional branch instruction.
12270 unsigned Opc =
12271 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12272 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12273
Chris Lattner52600972009-09-02 05:57:00 +000012274 // copy0MBB:
12275 // %FalseValue = ...
12276 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012277 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012278
Chris Lattner52600972009-09-02 05:57:00 +000012279 // sinkMBB:
12280 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12281 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012282 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12283 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012284 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12285 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12286
Dan Gohman14152b42010-07-06 20:24:04 +000012287 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012288 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012289}
12290
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012291MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012292X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12293 bool Is64Bit) const {
12294 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12295 DebugLoc DL = MI->getDebugLoc();
12296 MachineFunction *MF = BB->getParent();
12297 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12298
12299 assert(EnableSegmentedStacks);
12300
12301 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12302 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12303
12304 // BB:
12305 // ... [Till the alloca]
12306 // If stacklet is not large enough, jump to mallocMBB
12307 //
12308 // bumpMBB:
12309 // Allocate by subtracting from RSP
12310 // Jump to continueMBB
12311 //
12312 // mallocMBB:
12313 // Allocate by call to runtime
12314 //
12315 // continueMBB:
12316 // ...
12317 // [rest of original BB]
12318 //
12319
12320 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12321 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12322 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12323
12324 MachineRegisterInfo &MRI = MF->getRegInfo();
12325 const TargetRegisterClass *AddrRegClass =
12326 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12327
12328 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12329 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12330 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012331 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012332 sizeVReg = MI->getOperand(1).getReg(),
12333 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12334
12335 MachineFunction::iterator MBBIter = BB;
12336 ++MBBIter;
12337
12338 MF->insert(MBBIter, bumpMBB);
12339 MF->insert(MBBIter, mallocMBB);
12340 MF->insert(MBBIter, continueMBB);
12341
12342 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12343 (MachineBasicBlock::iterator(MI)), BB->end());
12344 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12345
12346 // Add code to the main basic block to check if the stack limit has been hit,
12347 // and if so, jump to mallocMBB otherwise to bumpMBB.
12348 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012349 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012350 .addReg(tmpSPVReg).addReg(sizeVReg);
12351 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
12352 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012353 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012354 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12355
12356 // bumpMBB simply decreases the stack pointer, since we know the current
12357 // stacklet has enough space.
12358 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012359 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012360 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012361 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012362 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12363
12364 // Calls into a routine in libgcc to allocate more space from the heap.
12365 if (Is64Bit) {
12366 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12367 .addReg(sizeVReg);
12368 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
12369 .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
12370 } else {
12371 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12372 .addImm(12);
12373 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12374 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
12375 .addExternalSymbol("__morestack_allocate_stack_space");
12376 }
12377
12378 if (!Is64Bit)
12379 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12380 .addImm(16);
12381
12382 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12383 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12384 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12385
12386 // Set up the CFG correctly.
12387 BB->addSuccessor(bumpMBB);
12388 BB->addSuccessor(mallocMBB);
12389 mallocMBB->addSuccessor(continueMBB);
12390 bumpMBB->addSuccessor(continueMBB);
12391
12392 // Take care of the PHI nodes.
12393 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12394 MI->getOperand(0).getReg())
12395 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12396 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12397
12398 // Delete the original pseudo instruction.
12399 MI->eraseFromParent();
12400
12401 // And we're done.
12402 return continueMBB;
12403}
12404
12405MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012406X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012407 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012408 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12409 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012410
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012411 assert(!Subtarget->isTargetEnvMacho());
12412
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012413 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12414 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012415
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012416 if (Subtarget->isTargetWin64()) {
12417 if (Subtarget->isTargetCygMing()) {
12418 // ___chkstk(Mingw64):
12419 // Clobbers R10, R11, RAX and EFLAGS.
12420 // Updates RSP.
12421 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12422 .addExternalSymbol("___chkstk")
12423 .addReg(X86::RAX, RegState::Implicit)
12424 .addReg(X86::RSP, RegState::Implicit)
12425 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12426 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12427 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12428 } else {
12429 // __chkstk(MSVCRT): does not update stack pointer.
12430 // Clobbers R10, R11 and EFLAGS.
12431 // FIXME: RAX(allocated size) might be reused and not killed.
12432 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12433 .addExternalSymbol("__chkstk")
12434 .addReg(X86::RAX, RegState::Implicit)
12435 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12436 // RAX has the offset to subtracted from RSP.
12437 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12438 .addReg(X86::RSP)
12439 .addReg(X86::RAX);
12440 }
12441 } else {
12442 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012443 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12444
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012445 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12446 .addExternalSymbol(StackProbeSymbol)
12447 .addReg(X86::EAX, RegState::Implicit)
12448 .addReg(X86::ESP, RegState::Implicit)
12449 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12450 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12451 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12452 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012453
Dan Gohman14152b42010-07-06 20:24:04 +000012454 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012455 return BB;
12456}
Chris Lattner52600972009-09-02 05:57:00 +000012457
12458MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012459X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12460 MachineBasicBlock *BB) const {
12461 // This is pretty easy. We're taking the value that we received from
12462 // our load from the relocation, sticking it in either RDI (x86-64)
12463 // or EAX and doing an indirect call. The return value will then
12464 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012465 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012466 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012467 DebugLoc DL = MI->getDebugLoc();
12468 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012469
12470 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012471 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012472
Eric Christopher30ef0e52010-06-03 04:07:48 +000012473 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012474 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12475 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012476 .addReg(X86::RIP)
12477 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012478 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012479 MI->getOperand(3).getTargetFlags())
12480 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012481 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012482 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000012483 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012484 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12485 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012486 .addReg(0)
12487 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012488 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012489 MI->getOperand(3).getTargetFlags())
12490 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012491 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012492 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012493 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012494 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12495 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012496 .addReg(TII->getGlobalBaseReg(F))
12497 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012498 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012499 MI->getOperand(3).getTargetFlags())
12500 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012501 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012502 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012503 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012504
Dan Gohman14152b42010-07-06 20:24:04 +000012505 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012506 return BB;
12507}
12508
12509MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012510X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012511 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012512 switch (MI->getOpcode()) {
Richard Trieu23946fc2011-09-21 03:09:09 +000012513 default: assert(0 && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012514 case X86::TAILJMPd64:
12515 case X86::TAILJMPr64:
12516 case X86::TAILJMPm64:
Richard Trieu23946fc2011-09-21 03:09:09 +000012517 assert(0 && "TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012518 case X86::TCRETURNdi64:
12519 case X86::TCRETURNri64:
12520 case X86::TCRETURNmi64:
12521 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
12522 // On AMD64, additional defs should be added before register allocation.
12523 if (!Subtarget->isTargetWin64()) {
12524 MI->addRegisterDefined(X86::RSI);
12525 MI->addRegisterDefined(X86::RDI);
12526 MI->addRegisterDefined(X86::XMM6);
12527 MI->addRegisterDefined(X86::XMM7);
12528 MI->addRegisterDefined(X86::XMM8);
12529 MI->addRegisterDefined(X86::XMM9);
12530 MI->addRegisterDefined(X86::XMM10);
12531 MI->addRegisterDefined(X86::XMM11);
12532 MI->addRegisterDefined(X86::XMM12);
12533 MI->addRegisterDefined(X86::XMM13);
12534 MI->addRegisterDefined(X86::XMM14);
12535 MI->addRegisterDefined(X86::XMM15);
12536 }
12537 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012538 case X86::WIN_ALLOCA:
12539 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012540 case X86::SEG_ALLOCA_32:
12541 return EmitLoweredSegAlloca(MI, BB, false);
12542 case X86::SEG_ALLOCA_64:
12543 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012544 case X86::TLSCall_32:
12545 case X86::TLSCall_64:
12546 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012547 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012548 case X86::CMOV_FR32:
12549 case X86::CMOV_FR64:
12550 case X86::CMOV_V4F32:
12551 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012552 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012553 case X86::CMOV_V8F32:
12554 case X86::CMOV_V4F64:
12555 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012556 case X86::CMOV_GR16:
12557 case X86::CMOV_GR32:
12558 case X86::CMOV_RFP32:
12559 case X86::CMOV_RFP64:
12560 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012561 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012562
Dale Johannesen849f2142007-07-03 00:53:03 +000012563 case X86::FP32_TO_INT16_IN_MEM:
12564 case X86::FP32_TO_INT32_IN_MEM:
12565 case X86::FP32_TO_INT64_IN_MEM:
12566 case X86::FP64_TO_INT16_IN_MEM:
12567 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012568 case X86::FP64_TO_INT64_IN_MEM:
12569 case X86::FP80_TO_INT16_IN_MEM:
12570 case X86::FP80_TO_INT32_IN_MEM:
12571 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012572 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12573 DebugLoc DL = MI->getDebugLoc();
12574
Evan Cheng60c07e12006-07-05 22:17:51 +000012575 // Change the floating point control register to use "round towards zero"
12576 // mode when truncating to an integer value.
12577 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012578 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012579 addFrameReference(BuildMI(*BB, MI, DL,
12580 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012581
12582 // Load the old value of the high byte of the control word...
12583 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000012584 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012585 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012586 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012587
12588 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012589 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012590 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012591
12592 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012593 addFrameReference(BuildMI(*BB, MI, DL,
12594 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012595
12596 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012597 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012598 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012599
12600 // Get the X86 opcode to use.
12601 unsigned Opc;
12602 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012603 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012604 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12605 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12606 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12607 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12608 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12609 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012610 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12611 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12612 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012613 }
12614
12615 X86AddressMode AM;
12616 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012617 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012618 AM.BaseType = X86AddressMode::RegBase;
12619 AM.Base.Reg = Op.getReg();
12620 } else {
12621 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012622 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012623 }
12624 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012625 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012626 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012627 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012628 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012629 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012630 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012631 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012632 AM.GV = Op.getGlobal();
12633 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012634 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012635 }
Dan Gohman14152b42010-07-06 20:24:04 +000012636 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012637 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000012638
12639 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000012640 addFrameReference(BuildMI(*BB, MI, DL,
12641 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012642
Dan Gohman14152b42010-07-06 20:24:04 +000012643 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000012644 return BB;
12645 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012646 // String/text processing lowering.
12647 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012648 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012649 return EmitPCMP(MI, BB, 3, false /* in-mem */);
12650 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012651 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012652 return EmitPCMP(MI, BB, 3, true /* in-mem */);
12653 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012654 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000012655 return EmitPCMP(MI, BB, 5, false /* in mem */);
12656 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012657 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000012658 return EmitPCMP(MI, BB, 5, true /* in mem */);
12659
Eric Christopher228232b2010-11-30 07:20:12 +000012660 // Thread synchronization.
12661 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012662 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000012663 case X86::MWAIT:
12664 return EmitMwait(MI, BB);
12665
Eric Christopherb120ab42009-08-18 22:50:32 +000012666 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000012667 case X86::ATOMAND32:
12668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012669 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012670 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012671 X86::NOT32r, X86::EAX,
12672 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012673 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000012674 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12675 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012676 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012677 X86::NOT32r, X86::EAX,
12678 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000012679 case X86::ATOMXOR32:
12680 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012681 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012682 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012683 X86::NOT32r, X86::EAX,
12684 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000012685 case X86::ATOMNAND32:
12686 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012687 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012688 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012689 X86::NOT32r, X86::EAX,
12690 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000012691 case X86::ATOMMIN32:
12692 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12693 case X86::ATOMMAX32:
12694 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12695 case X86::ATOMUMIN32:
12696 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12697 case X86::ATOMUMAX32:
12698 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000012699
12700 case X86::ATOMAND16:
12701 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12702 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012703 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012704 X86::NOT16r, X86::AX,
12705 X86::GR16RegisterClass);
12706 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000012707 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012708 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012709 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012710 X86::NOT16r, X86::AX,
12711 X86::GR16RegisterClass);
12712 case X86::ATOMXOR16:
12713 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12714 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012715 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012716 X86::NOT16r, X86::AX,
12717 X86::GR16RegisterClass);
12718 case X86::ATOMNAND16:
12719 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12720 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012721 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012722 X86::NOT16r, X86::AX,
12723 X86::GR16RegisterClass, true);
12724 case X86::ATOMMIN16:
12725 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12726 case X86::ATOMMAX16:
12727 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12728 case X86::ATOMUMIN16:
12729 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12730 case X86::ATOMUMAX16:
12731 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12732
12733 case X86::ATOMAND8:
12734 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12735 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012736 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012737 X86::NOT8r, X86::AL,
12738 X86::GR8RegisterClass);
12739 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000012740 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012741 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012742 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012743 X86::NOT8r, X86::AL,
12744 X86::GR8RegisterClass);
12745 case X86::ATOMXOR8:
12746 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12747 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012748 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012749 X86::NOT8r, X86::AL,
12750 X86::GR8RegisterClass);
12751 case X86::ATOMNAND8:
12752 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12753 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012754 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000012755 X86::NOT8r, X86::AL,
12756 X86::GR8RegisterClass, true);
12757 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012758 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000012759 case X86::ATOMAND64:
12760 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012761 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012762 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012763 X86::NOT64r, X86::RAX,
12764 X86::GR64RegisterClass);
12765 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000012766 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12767 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012768 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012769 X86::NOT64r, X86::RAX,
12770 X86::GR64RegisterClass);
12771 case X86::ATOMXOR64:
12772 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000012773 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012774 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012775 X86::NOT64r, X86::RAX,
12776 X86::GR64RegisterClass);
12777 case X86::ATOMNAND64:
12778 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12779 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012780 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000012781 X86::NOT64r, X86::RAX,
12782 X86::GR64RegisterClass, true);
12783 case X86::ATOMMIN64:
12784 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12785 case X86::ATOMMAX64:
12786 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12787 case X86::ATOMUMIN64:
12788 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12789 case X86::ATOMUMAX64:
12790 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012791
12792 // This group does 64-bit operations on a 32-bit host.
12793 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012794 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012795 X86::AND32rr, X86::AND32rr,
12796 X86::AND32ri, X86::AND32ri,
12797 false);
12798 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012799 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012800 X86::OR32rr, X86::OR32rr,
12801 X86::OR32ri, X86::OR32ri,
12802 false);
12803 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012804 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012805 X86::XOR32rr, X86::XOR32rr,
12806 X86::XOR32ri, X86::XOR32ri,
12807 false);
12808 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012809 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012810 X86::AND32rr, X86::AND32rr,
12811 X86::AND32ri, X86::AND32ri,
12812 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012813 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012814 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012815 X86::ADD32rr, X86::ADC32rr,
12816 X86::ADD32ri, X86::ADC32ri,
12817 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012818 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012819 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012820 X86::SUB32rr, X86::SBB32rr,
12821 X86::SUB32ri, X86::SBB32ri,
12822 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000012823 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000012824 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000012825 X86::MOV32rr, X86::MOV32rr,
12826 X86::MOV32ri, X86::MOV32ri,
12827 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012828 case X86::VASTART_SAVE_XMM_REGS:
12829 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000012830
12831 case X86::VAARG_64:
12832 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012833 }
12834}
12835
12836//===----------------------------------------------------------------------===//
12837// X86 Optimization Hooks
12838//===----------------------------------------------------------------------===//
12839
Dan Gohman475871a2008-07-27 21:46:04 +000012840void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000012841 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012842 APInt &KnownZero,
12843 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000012844 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000012845 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012846 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000012847 assert((Opc >= ISD::BUILTIN_OP_END ||
12848 Opc == ISD::INTRINSIC_WO_CHAIN ||
12849 Opc == ISD::INTRINSIC_W_CHAIN ||
12850 Opc == ISD::INTRINSIC_VOID) &&
12851 "Should use MaskedValueIsZero if you don't know whether Op"
12852 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012853
Dan Gohmanf4f92f52008-02-13 23:07:24 +000012854 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012855 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000012856 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012857 case X86ISD::ADD:
12858 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000012859 case X86ISD::ADC:
12860 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012861 case X86ISD::SMUL:
12862 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000012863 case X86ISD::INC:
12864 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000012865 case X86ISD::OR:
12866 case X86ISD::XOR:
12867 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000012868 // These nodes' second result is a boolean.
12869 if (Op.getResNo() == 0)
12870 break;
12871 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012872 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000012873 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12874 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000012875 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000012876 case ISD::INTRINSIC_WO_CHAIN: {
12877 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
12878 unsigned NumLoBits = 0;
12879 switch (IntId) {
12880 default: break;
12881 case Intrinsic::x86_sse_movmsk_ps:
12882 case Intrinsic::x86_avx_movmsk_ps_256:
12883 case Intrinsic::x86_sse2_movmsk_pd:
12884 case Intrinsic::x86_avx_movmsk_pd_256:
12885 case Intrinsic::x86_mmx_pmovmskb:
12886 case Intrinsic::x86_sse2_pmovmskb_128: {
12887 // High bits of movmskp{s|d}, pmovmskb are known zero.
12888 switch (IntId) {
12889 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
12890 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
12891 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
12892 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
12893 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
12894 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
12895 }
12896 KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
12897 Mask.getBitWidth() - NumLoBits);
12898 break;
12899 }
12900 }
12901 break;
12902 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012903 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012904}
Chris Lattner259e97c2006-01-31 19:43:35 +000012905
Owen Andersonbc146b02010-09-21 20:42:50 +000012906unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
12907 unsigned Depth) const {
12908 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
12909 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
12910 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012911
Owen Andersonbc146b02010-09-21 20:42:50 +000012912 // Fallback case.
12913 return 1;
12914}
12915
Evan Cheng206ee9d2006-07-07 08:33:52 +000012916/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000012917/// node is a GlobalAddress + offset.
12918bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000012919 const GlobalValue* &GA,
12920 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000012921 if (N->getOpcode() == X86ISD::Wrapper) {
12922 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012923 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000012924 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012925 return true;
12926 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000012927 }
Evan Chengad4196b2008-05-12 19:56:52 +000012928 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012929}
12930
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012931/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
12932/// same as extracting the high 128-bit part of 256-bit vector and then
12933/// inserting the result into the low part of a new 256-bit vector
12934static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
12935 EVT VT = SVOp->getValueType(0);
12936 int NumElems = VT.getVectorNumElements();
12937
12938 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
12939 for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
12940 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12941 SVOp->getMaskElt(j) >= 0)
12942 return false;
12943
12944 return true;
12945}
12946
12947/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
12948/// same as extracting the low 128-bit part of 256-bit vector and then
12949/// inserting the result into the high part of a new 256-bit vector
12950static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
12951 EVT VT = SVOp->getValueType(0);
12952 int NumElems = VT.getVectorNumElements();
12953
12954 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
12955 for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
12956 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
12957 SVOp->getMaskElt(j) >= 0)
12958 return false;
12959
12960 return true;
12961}
12962
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012963/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
12964static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
12965 TargetLowering::DAGCombinerInfo &DCI) {
12966 DebugLoc dl = N->getDebugLoc();
12967 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
12968 SDValue V1 = SVOp->getOperand(0);
12969 SDValue V2 = SVOp->getOperand(1);
12970 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000012971 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012972
12973 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
12974 V2.getOpcode() == ISD::CONCAT_VECTORS) {
12975 //
12976 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000012977 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012978 // V UNDEF BUILD_VECTOR UNDEF
12979 // \ / \ /
12980 // CONCAT_VECTOR CONCAT_VECTOR
12981 // \ /
12982 // \ /
12983 // RESULT: V + zero extended
12984 //
12985 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
12986 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
12987 V1.getOperand(1).getOpcode() != ISD::UNDEF)
12988 return SDValue();
12989
12990 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
12991 return SDValue();
12992
12993 // To match the shuffle mask, the first half of the mask should
12994 // be exactly the first vector, and all the rest a splat with the
12995 // first element of the second one.
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000012996 for (int i = 0; i < NumElems/2; ++i)
12997 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
12998 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
12999 return SDValue();
13000
13001 // Emit a zeroed vector and insert the desired subvector on its
13002 // first half.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013003 SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013004 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
13005 DAG.getConstant(0, MVT::i32), DAG, dl);
13006 return DCI.CombineTo(N, InsV);
13007 }
13008
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013009 //===--------------------------------------------------------------------===//
13010 // Combine some shuffles into subvector extracts and inserts:
13011 //
13012
13013 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13014 if (isShuffleHigh128VectorInsertLow(SVOp)) {
13015 SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
13016 DAG, dl);
13017 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
13018 V, DAG.getConstant(0, MVT::i32), DAG, dl);
13019 return DCI.CombineTo(N, InsV);
13020 }
13021
13022 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13023 if (isShuffleLow128VectorInsertHigh(SVOp)) {
13024 SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
13025 SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
13026 V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
13027 return DCI.CombineTo(N, InsV);
13028 }
13029
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013030 return SDValue();
13031}
13032
13033/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013034static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013035 TargetLowering::DAGCombinerInfo &DCI,
13036 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013037 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013038 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013039
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013040 // Don't create instructions with illegal types after legalize types has run.
13041 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13042 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13043 return SDValue();
13044
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013045 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
13046 if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
13047 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013048 return PerformShuffleCombine256(N, DAG, DCI);
13049
13050 // Only handle 128 wide vector from here on.
13051 if (VT.getSizeInBits() != 128)
13052 return SDValue();
13053
13054 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13055 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13056 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013057 SmallVector<SDValue, 16> Elts;
13058 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013059 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013060
Nate Begemanfdea31a2010-03-24 20:49:50 +000013061 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013062}
Evan Chengd880b972008-05-09 21:53:03 +000013063
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013064/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13065/// generation and convert it from being a bunch of shuffles and extracts
13066/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013067static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
13068 const TargetLowering &TLI) {
13069 SDValue InputVector = N->getOperand(0);
13070
13071 // Only operate on vectors of 4 elements, where the alternative shuffling
13072 // gets to be more expensive.
13073 if (InputVector.getValueType() != MVT::v4i32)
13074 return SDValue();
13075
13076 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13077 // single use which is a sign-extend or zero-extend, and all elements are
13078 // used.
13079 SmallVector<SDNode *, 4> Uses;
13080 unsigned ExtractedElements = 0;
13081 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13082 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13083 if (UI.getUse().getResNo() != InputVector.getResNo())
13084 return SDValue();
13085
13086 SDNode *Extract = *UI;
13087 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13088 return SDValue();
13089
13090 if (Extract->getValueType(0) != MVT::i32)
13091 return SDValue();
13092 if (!Extract->hasOneUse())
13093 return SDValue();
13094 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13095 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13096 return SDValue();
13097 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13098 return SDValue();
13099
13100 // Record which element was extracted.
13101 ExtractedElements |=
13102 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13103
13104 Uses.push_back(Extract);
13105 }
13106
13107 // If not all the elements were used, this may not be worthwhile.
13108 if (ExtractedElements != 15)
13109 return SDValue();
13110
13111 // Ok, we've now decided to do the transformation.
13112 DebugLoc dl = InputVector.getDebugLoc();
13113
13114 // Store the value to a temporary stack slot.
13115 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013116 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13117 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013118
13119 // Replace each use (extract) with a load of the appropriate element.
13120 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13121 UE = Uses.end(); UI != UE; ++UI) {
13122 SDNode *Extract = *UI;
13123
Nadav Rotem86694292011-05-17 08:31:57 +000013124 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013125 SDValue Idx = Extract->getOperand(1);
13126 unsigned EltSize =
13127 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13128 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
13129 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13130
Nadav Rotem86694292011-05-17 08:31:57 +000013131 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013132 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013133
13134 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013135 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013136 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013137 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013138
13139 // Replace the exact with the load.
13140 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13141 }
13142
13143 // The replacement was made in place; don't return anything.
13144 return SDValue();
13145}
13146
Duncan Sands6bcd2192011-09-17 16:49:39 +000013147/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13148/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013149static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013150 const X86Subtarget *Subtarget) {
13151 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013152 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013153 // Get the LHS/RHS of the select.
13154 SDValue LHS = N->getOperand(1);
13155 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013156 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013157
Dan Gohman670e5392009-09-21 18:03:22 +000013158 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013159 // instructions match the semantics of the common C idiom x<y?x:y but not
13160 // x<=y?x:y, because of how they handle negative zero (which can be
13161 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013162 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13163 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
13164 (Subtarget->hasXMMInt() ||
13165 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013166 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013167
Chris Lattner47b4ce82009-03-11 05:48:52 +000013168 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013169 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013170 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13171 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013172 switch (CC) {
13173 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013174 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013175 // Converting this to a min would handle NaNs incorrectly, and swapping
13176 // the operands would cause it to handle comparisons between positive
13177 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013178 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013179 if (!UnsafeFPMath &&
13180 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13181 break;
13182 std::swap(LHS, RHS);
13183 }
Dan Gohman670e5392009-09-21 18:03:22 +000013184 Opcode = X86ISD::FMIN;
13185 break;
13186 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013187 // Converting this to a min would handle comparisons between positive
13188 // and negative zero incorrectly.
13189 if (!UnsafeFPMath &&
13190 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13191 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013192 Opcode = X86ISD::FMIN;
13193 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013194 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013195 // Converting this to a min would handle both negative zeros and NaNs
13196 // incorrectly, but we can swap the operands to fix both.
13197 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013198 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013199 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013200 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013201 Opcode = X86ISD::FMIN;
13202 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013203
Dan Gohman670e5392009-09-21 18:03:22 +000013204 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013205 // Converting this to a max would handle comparisons between positive
13206 // and negative zero incorrectly.
13207 if (!UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013208 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013209 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013210 Opcode = X86ISD::FMAX;
13211 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013212 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013213 // Converting this to a max would handle NaNs incorrectly, and swapping
13214 // the operands would cause it to handle comparisons between positive
13215 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013216 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000013217 if (!UnsafeFPMath &&
13218 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13219 break;
13220 std::swap(LHS, RHS);
13221 }
Dan Gohman670e5392009-09-21 18:03:22 +000013222 Opcode = X86ISD::FMAX;
13223 break;
13224 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013225 // Converting this to a max would handle both negative zeros and NaNs
13226 // incorrectly, but we can swap the operands to fix both.
13227 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013228 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013229 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013230 case ISD::SETGE:
13231 Opcode = X86ISD::FMAX;
13232 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013233 }
Dan Gohman670e5392009-09-21 18:03:22 +000013234 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013235 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13236 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013237 switch (CC) {
13238 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013239 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013240 // Converting this to a min would handle comparisons between positive
13241 // and negative zero incorrectly, and swapping the operands would
13242 // cause it to handle NaNs incorrectly.
13243 if (!UnsafeFPMath &&
13244 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013245 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013246 break;
13247 std::swap(LHS, RHS);
13248 }
Dan Gohman670e5392009-09-21 18:03:22 +000013249 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013250 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013251 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013252 // Converting this to a min would handle NaNs incorrectly.
13253 if (!UnsafeFPMath &&
13254 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13255 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013256 Opcode = X86ISD::FMIN;
13257 break;
13258 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013259 // Converting this to a min would handle both negative zeros and NaNs
13260 // incorrectly, but we can swap the operands to fix both.
13261 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013262 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013263 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013264 case ISD::SETGE:
13265 Opcode = X86ISD::FMIN;
13266 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013267
Dan Gohman670e5392009-09-21 18:03:22 +000013268 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013269 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013270 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013271 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013272 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013273 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013274 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013275 // Converting this to a max would handle comparisons between positive
13276 // and negative zero incorrectly, and swapping the operands would
13277 // cause it to handle NaNs incorrectly.
13278 if (!UnsafeFPMath &&
13279 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013280 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013281 break;
13282 std::swap(LHS, RHS);
13283 }
Dan Gohman670e5392009-09-21 18:03:22 +000013284 Opcode = X86ISD::FMAX;
13285 break;
13286 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013287 // Converting this to a max would handle both negative zeros and NaNs
13288 // incorrectly, but we can swap the operands to fix both.
13289 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013290 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013291 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013292 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013293 Opcode = X86ISD::FMAX;
13294 break;
13295 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013296 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013297
Chris Lattner47b4ce82009-03-11 05:48:52 +000013298 if (Opcode)
13299 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013300 }
Eric Christopherfd179292009-08-27 18:07:15 +000013301
Chris Lattnerd1980a52009-03-12 06:52:53 +000013302 // If this is a select between two integer constants, try to do some
13303 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013304 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13305 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013306 // Don't do this for crazy integer types.
13307 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13308 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013309 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013310 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013311
Chris Lattnercee56e72009-03-13 05:53:31 +000013312 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013313 // Efficiently invertible.
13314 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13315 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13316 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13317 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013318 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013319 }
Eric Christopherfd179292009-08-27 18:07:15 +000013320
Chris Lattnerd1980a52009-03-12 06:52:53 +000013321 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013322 if (FalseC->getAPIntValue() == 0 &&
13323 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013324 if (NeedsCondInvert) // Invert the condition if needed.
13325 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13326 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013327
Chris Lattnerd1980a52009-03-12 06:52:53 +000013328 // Zero extend the condition if needed.
13329 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013330
Chris Lattnercee56e72009-03-13 05:53:31 +000013331 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013332 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013333 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013334 }
Eric Christopherfd179292009-08-27 18:07:15 +000013335
Chris Lattner97a29a52009-03-13 05:22:11 +000013336 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013337 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013338 if (NeedsCondInvert) // Invert the condition if needed.
13339 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13340 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013341
Chris Lattner97a29a52009-03-13 05:22:11 +000013342 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013343 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13344 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013345 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013346 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013347 }
Eric Christopherfd179292009-08-27 18:07:15 +000013348
Chris Lattnercee56e72009-03-13 05:53:31 +000013349 // Optimize cases that will turn into an LEA instruction. This requires
13350 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013351 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013352 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013353 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013354
Chris Lattnercee56e72009-03-13 05:53:31 +000013355 bool isFastMultiplier = false;
13356 if (Diff < 10) {
13357 switch ((unsigned char)Diff) {
13358 default: break;
13359 case 1: // result = add base, cond
13360 case 2: // result = lea base( , cond*2)
13361 case 3: // result = lea base(cond, cond*2)
13362 case 4: // result = lea base( , cond*4)
13363 case 5: // result = lea base(cond, cond*4)
13364 case 8: // result = lea base( , cond*8)
13365 case 9: // result = lea base(cond, cond*8)
13366 isFastMultiplier = true;
13367 break;
13368 }
13369 }
Eric Christopherfd179292009-08-27 18:07:15 +000013370
Chris Lattnercee56e72009-03-13 05:53:31 +000013371 if (isFastMultiplier) {
13372 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13373 if (NeedsCondInvert) // Invert the condition if needed.
13374 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13375 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013376
Chris Lattnercee56e72009-03-13 05:53:31 +000013377 // Zero extend the condition if needed.
13378 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13379 Cond);
13380 // Scale the condition by the difference.
13381 if (Diff != 1)
13382 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13383 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013384
Chris Lattnercee56e72009-03-13 05:53:31 +000013385 // Add the base if non-zero.
13386 if (FalseC->getAPIntValue() != 0)
13387 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13388 SDValue(FalseC, 0));
13389 return Cond;
13390 }
Eric Christopherfd179292009-08-27 18:07:15 +000013391 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013392 }
13393 }
Eric Christopherfd179292009-08-27 18:07:15 +000013394
Dan Gohman475871a2008-07-27 21:46:04 +000013395 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000013396}
13397
Chris Lattnerd1980a52009-03-12 06:52:53 +000013398/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
13399static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
13400 TargetLowering::DAGCombinerInfo &DCI) {
13401 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000013402
Chris Lattnerd1980a52009-03-12 06:52:53 +000013403 // If the flag operand isn't dead, don't touch this CMOV.
13404 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
13405 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000013406
Evan Chengb5a55d92011-05-24 01:48:22 +000013407 SDValue FalseOp = N->getOperand(0);
13408 SDValue TrueOp = N->getOperand(1);
13409 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
13410 SDValue Cond = N->getOperand(3);
13411 if (CC == X86::COND_E || CC == X86::COND_NE) {
13412 switch (Cond.getOpcode()) {
13413 default: break;
13414 case X86ISD::BSR:
13415 case X86ISD::BSF:
13416 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
13417 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
13418 return (CC == X86::COND_E) ? FalseOp : TrueOp;
13419 }
13420 }
13421
Chris Lattnerd1980a52009-03-12 06:52:53 +000013422 // If this is a select between two integer constants, try to do some
13423 // optimizations. Note that the operands are ordered the opposite of SELECT
13424 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000013425 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
13426 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013427 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
13428 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000013429 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
13430 CC = X86::GetOppositeBranchCondition(CC);
13431 std::swap(TrueC, FalseC);
13432 }
Eric Christopherfd179292009-08-27 18:07:15 +000013433
Chris Lattnerd1980a52009-03-12 06:52:53 +000013434 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013435 // This is efficient for any integer data type (including i8/i16) and
13436 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013437 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013438 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13439 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013440
Chris Lattnerd1980a52009-03-12 06:52:53 +000013441 // Zero extend the condition if needed.
13442 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013443
Chris Lattnerd1980a52009-03-12 06:52:53 +000013444 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
13445 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013446 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013447 if (N->getNumValues() == 2) // Dead flag value?
13448 return DCI.CombineTo(N, Cond, SDValue());
13449 return Cond;
13450 }
Eric Christopherfd179292009-08-27 18:07:15 +000013451
Chris Lattnercee56e72009-03-13 05:53:31 +000013452 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
13453 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000013454 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013455 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13456 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013457
Chris Lattner97a29a52009-03-13 05:22:11 +000013458 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013459 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13460 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013461 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13462 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000013463
Chris Lattner97a29a52009-03-13 05:22:11 +000013464 if (N->getNumValues() == 2) // Dead flag value?
13465 return DCI.CombineTo(N, Cond, SDValue());
13466 return Cond;
13467 }
Eric Christopherfd179292009-08-27 18:07:15 +000013468
Chris Lattnercee56e72009-03-13 05:53:31 +000013469 // Optimize cases that will turn into an LEA instruction. This requires
13470 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013471 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013472 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013473 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013474
Chris Lattnercee56e72009-03-13 05:53:31 +000013475 bool isFastMultiplier = false;
13476 if (Diff < 10) {
13477 switch ((unsigned char)Diff) {
13478 default: break;
13479 case 1: // result = add base, cond
13480 case 2: // result = lea base( , cond*2)
13481 case 3: // result = lea base(cond, cond*2)
13482 case 4: // result = lea base( , cond*4)
13483 case 5: // result = lea base(cond, cond*4)
13484 case 8: // result = lea base( , cond*8)
13485 case 9: // result = lea base(cond, cond*8)
13486 isFastMultiplier = true;
13487 break;
13488 }
13489 }
Eric Christopherfd179292009-08-27 18:07:15 +000013490
Chris Lattnercee56e72009-03-13 05:53:31 +000013491 if (isFastMultiplier) {
13492 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013493 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
13494 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000013495 // Zero extend the condition if needed.
13496 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
13497 Cond);
13498 // Scale the condition by the difference.
13499 if (Diff != 1)
13500 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
13501 DAG.getConstant(Diff, Cond.getValueType()));
13502
13503 // Add the base if non-zero.
13504 if (FalseC->getAPIntValue() != 0)
13505 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
13506 SDValue(FalseC, 0));
13507 if (N->getNumValues() == 2) // Dead flag value?
13508 return DCI.CombineTo(N, Cond, SDValue());
13509 return Cond;
13510 }
Eric Christopherfd179292009-08-27 18:07:15 +000013511 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000013512 }
13513 }
13514 return SDValue();
13515}
13516
13517
Evan Cheng0b0cd912009-03-28 05:57:29 +000013518/// PerformMulCombine - Optimize a single multiply with constant into two
13519/// in order to implement it with two cheaper instructions, e.g.
13520/// LEA + SHL, LEA + LEA.
13521static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13522 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000013523 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13524 return SDValue();
13525
Owen Andersone50ed302009-08-10 22:56:29 +000013526 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000013527 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000013528 return SDValue();
13529
13530 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13531 if (!C)
13532 return SDValue();
13533 uint64_t MulAmt = C->getZExtValue();
13534 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13535 return SDValue();
13536
13537 uint64_t MulAmt1 = 0;
13538 uint64_t MulAmt2 = 0;
13539 if ((MulAmt % 9) == 0) {
13540 MulAmt1 = 9;
13541 MulAmt2 = MulAmt / 9;
13542 } else if ((MulAmt % 5) == 0) {
13543 MulAmt1 = 5;
13544 MulAmt2 = MulAmt / 5;
13545 } else if ((MulAmt % 3) == 0) {
13546 MulAmt1 = 3;
13547 MulAmt2 = MulAmt / 3;
13548 }
13549 if (MulAmt2 &&
13550 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13551 DebugLoc DL = N->getDebugLoc();
13552
13553 if (isPowerOf2_64(MulAmt2) &&
13554 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13555 // If second multiplifer is pow2, issue it first. We want the multiply by
13556 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13557 // is an add.
13558 std::swap(MulAmt1, MulAmt2);
13559
13560 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000013561 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013562 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000013563 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000013564 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013565 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000013566 DAG.getConstant(MulAmt1, VT));
13567
Eric Christopherfd179292009-08-27 18:07:15 +000013568 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000013569 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000013570 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000013571 else
Evan Cheng73f24c92009-03-30 21:36:47 +000013572 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000013573 DAG.getConstant(MulAmt2, VT));
13574
13575 // Do not add new nodes to DAG combiner worklist.
13576 DCI.CombineTo(N, NewMul, false);
13577 }
13578 return SDValue();
13579}
13580
Evan Chengad9c0a32009-12-15 00:53:42 +000013581static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13582 SDValue N0 = N->getOperand(0);
13583 SDValue N1 = N->getOperand(1);
13584 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13585 EVT VT = N0.getValueType();
13586
13587 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13588 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013589 if (VT.isInteger() && !VT.isVector() &&
13590 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000013591 N0.getOperand(1).getOpcode() == ISD::Constant) {
13592 SDValue N00 = N0.getOperand(0);
13593 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13594 ((N00.getOpcode() == ISD::ANY_EXTEND ||
13595 N00.getOpcode() == ISD::ZERO_EXTEND) &&
13596 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13597 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13598 APInt ShAmt = N1C->getAPIntValue();
13599 Mask = Mask.shl(ShAmt);
13600 if (Mask != 0)
13601 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13602 N00, DAG.getConstant(Mask, VT));
13603 }
13604 }
13605
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013606
13607 // Hardware support for vector shifts is sparse which makes us scalarize the
13608 // vector operations in many cases. Also, on sandybridge ADD is faster than
13609 // shl.
13610 // (shl V, 1) -> add V,V
13611 if (isSplatVector(N1.getNode())) {
13612 assert(N0.getValueType().isVector() && "Invalid vector shift type");
13613 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
13614 // We shift all of the values by one. In many cases we do not have
13615 // hardware support for this operation. This is better expressed as an ADD
13616 // of two values.
13617 if (N1C && (1 == N1C->getZExtValue())) {
13618 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
13619 }
13620 }
13621
Evan Chengad9c0a32009-12-15 00:53:42 +000013622 return SDValue();
13623}
Evan Cheng0b0cd912009-03-28 05:57:29 +000013624
Nate Begeman740ab032009-01-26 00:52:55 +000013625/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13626/// when possible.
13627static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13628 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000013629 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000013630 if (N->getOpcode() == ISD::SHL) {
13631 SDValue V = PerformSHLCombine(N, DAG);
13632 if (V.getNode()) return V;
13633 }
Evan Chengad9c0a32009-12-15 00:53:42 +000013634
Nate Begeman740ab032009-01-26 00:52:55 +000013635 // On X86 with SSE2 support, we can transform this to a vector shift if
13636 // all elements are shifted by the same amount. We can't do this in legalize
13637 // because the a constant vector is typically transformed to a constant pool
13638 // so we have no knowledge of the shift amount.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013639 if (!Subtarget->hasXMMInt())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013640 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013641
Craig Topper7be5dfd2011-11-12 09:58:49 +000013642 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
13643 (!Subtarget->hasAVX2() ||
13644 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013645 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000013646
Mon P Wang3becd092009-01-28 08:12:05 +000013647 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000013648 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000013649 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000013650 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000013651 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13652 unsigned NumElts = VT.getVectorNumElements();
13653 unsigned i = 0;
13654 for (; i != NumElts; ++i) {
13655 SDValue Arg = ShAmtOp.getOperand(i);
13656 if (Arg.getOpcode() == ISD::UNDEF) continue;
13657 BaseShAmt = Arg;
13658 break;
13659 }
13660 for (; i != NumElts; ++i) {
13661 SDValue Arg = ShAmtOp.getOperand(i);
13662 if (Arg.getOpcode() == ISD::UNDEF) continue;
13663 if (Arg != BaseShAmt) {
13664 return SDValue();
13665 }
13666 }
13667 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000013668 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000013669 SDValue InVec = ShAmtOp.getOperand(0);
13670 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13671 unsigned NumElts = InVec.getValueType().getVectorNumElements();
13672 unsigned i = 0;
13673 for (; i != NumElts; ++i) {
13674 SDValue Arg = InVec.getOperand(i);
13675 if (Arg.getOpcode() == ISD::UNDEF) continue;
13676 BaseShAmt = Arg;
13677 break;
13678 }
13679 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13680 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000013681 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000013682 if (C->getZExtValue() == SplatIdx)
13683 BaseShAmt = InVec.getOperand(1);
13684 }
13685 }
13686 if (BaseShAmt.getNode() == 0)
13687 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13688 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000013689 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013690 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000013691
Mon P Wangefa42202009-09-03 19:56:25 +000013692 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000013693 if (EltVT.bitsGT(MVT::i32))
13694 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13695 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000013696 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000013697
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013698 // The shift amount is identical so we can do a vector shift.
13699 SDValue ValOp = N->getOperand(0);
13700 switch (N->getOpcode()) {
13701 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000013702 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013703 break;
13704 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013705 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013706 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013707 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013708 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013709 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013710 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013711 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013712 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013713 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013714 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013715 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013716 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013717 if (VT == MVT::v4i64)
13718 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13719 DAG.getConstant(Intrinsic::x86_avx2_pslli_q, MVT::i32),
13720 ValOp, BaseShAmt);
13721 if (VT == MVT::v8i32)
13722 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13723 DAG.getConstant(Intrinsic::x86_avx2_pslli_d, MVT::i32),
13724 ValOp, BaseShAmt);
13725 if (VT == MVT::v16i16)
13726 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13727 DAG.getConstant(Intrinsic::x86_avx2_pslli_w, MVT::i32),
13728 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013729 break;
13730 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000013731 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013732 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013733 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013734 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013735 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013736 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013737 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013738 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013739 if (VT == MVT::v8i32)
13740 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13741 DAG.getConstant(Intrinsic::x86_avx2_psrai_d, MVT::i32),
13742 ValOp, BaseShAmt);
13743 if (VT == MVT::v16i16)
13744 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13745 DAG.getConstant(Intrinsic::x86_avx2_psrai_w, MVT::i32),
13746 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013747 break;
13748 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000013749 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013750 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013751 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013752 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013753 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013754 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013755 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013756 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000013757 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000013758 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000013759 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000013760 ValOp, BaseShAmt);
Craig Topper7be5dfd2011-11-12 09:58:49 +000013761 if (VT == MVT::v4i64)
13762 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13763 DAG.getConstant(Intrinsic::x86_avx2_psrli_q, MVT::i32),
13764 ValOp, BaseShAmt);
13765 if (VT == MVT::v8i32)
13766 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13767 DAG.getConstant(Intrinsic::x86_avx2_psrli_d, MVT::i32),
13768 ValOp, BaseShAmt);
13769 if (VT == MVT::v16i16)
13770 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13771 DAG.getConstant(Intrinsic::x86_avx2_psrli_w, MVT::i32),
13772 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000013773 break;
Nate Begeman740ab032009-01-26 00:52:55 +000013774 }
13775 return SDValue();
13776}
13777
Nate Begemanb65c1752010-12-17 22:55:37 +000013778
Stuart Hastings865f0932011-06-03 23:53:54 +000013779// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
13780// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
13781// and friends. Likewise for OR -> CMPNEQSS.
13782static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
13783 TargetLowering::DAGCombinerInfo &DCI,
13784 const X86Subtarget *Subtarget) {
13785 unsigned opcode;
13786
13787 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
13788 // we're requiring SSE2 for both.
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000013789 if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000013790 SDValue N0 = N->getOperand(0);
13791 SDValue N1 = N->getOperand(1);
13792 SDValue CMP0 = N0->getOperand(1);
13793 SDValue CMP1 = N1->getOperand(1);
13794 DebugLoc DL = N->getDebugLoc();
13795
13796 // The SETCCs should both refer to the same CMP.
13797 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
13798 return SDValue();
13799
13800 SDValue CMP00 = CMP0->getOperand(0);
13801 SDValue CMP01 = CMP0->getOperand(1);
13802 EVT VT = CMP00.getValueType();
13803
13804 if (VT == MVT::f32 || VT == MVT::f64) {
13805 bool ExpectingFlags = false;
13806 // Check for any users that want flags:
13807 for (SDNode::use_iterator UI = N->use_begin(),
13808 UE = N->use_end();
13809 !ExpectingFlags && UI != UE; ++UI)
13810 switch (UI->getOpcode()) {
13811 default:
13812 case ISD::BR_CC:
13813 case ISD::BRCOND:
13814 case ISD::SELECT:
13815 ExpectingFlags = true;
13816 break;
13817 case ISD::CopyToReg:
13818 case ISD::SIGN_EXTEND:
13819 case ISD::ZERO_EXTEND:
13820 case ISD::ANY_EXTEND:
13821 break;
13822 }
13823
13824 if (!ExpectingFlags) {
13825 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
13826 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
13827
13828 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
13829 X86::CondCode tmp = cc0;
13830 cc0 = cc1;
13831 cc1 = tmp;
13832 }
13833
13834 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
13835 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
13836 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
13837 X86ISD::NodeType NTOperator = is64BitFP ?
13838 X86ISD::FSETCCsd : X86ISD::FSETCCss;
13839 // FIXME: need symbolic constants for these magic numbers.
13840 // See X86ATTInstPrinter.cpp:printSSECC().
13841 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
13842 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
13843 DAG.getConstant(x86cc, MVT::i8));
13844 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
13845 OnesOrZeroesF);
13846 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
13847 DAG.getConstant(1, MVT::i32));
13848 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
13849 return OneBitOfTruth;
13850 }
13851 }
13852 }
13853 }
13854 return SDValue();
13855}
13856
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013857/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
13858/// so it can be folded inside ANDNP.
13859static bool CanFoldXORWithAllOnes(const SDNode *N) {
13860 EVT VT = N->getValueType(0);
13861
13862 // Match direct AllOnes for 128 and 256-bit vectors
13863 if (ISD::isBuildVectorAllOnes(N))
13864 return true;
13865
13866 // Look through a bit convert.
13867 if (N->getOpcode() == ISD::BITCAST)
13868 N = N->getOperand(0).getNode();
13869
13870 // Sometimes the operand may come from a insert_subvector building a 256-bit
13871 // allones vector
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013872 if (VT.getSizeInBits() == 256 &&
Bill Wendling456a9252011-08-04 00:32:58 +000013873 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
13874 SDValue V1 = N->getOperand(0);
13875 SDValue V2 = N->getOperand(1);
13876
13877 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
13878 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
13879 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
13880 ISD::isBuildVectorAllOnes(V2.getNode()))
13881 return true;
13882 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013883
13884 return false;
13885}
13886
Nate Begemanb65c1752010-12-17 22:55:37 +000013887static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
13888 TargetLowering::DAGCombinerInfo &DCI,
13889 const X86Subtarget *Subtarget) {
13890 if (DCI.isBeforeLegalizeOps())
13891 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013892
Stuart Hastings865f0932011-06-03 23:53:54 +000013893 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13894 if (R.getNode())
13895 return R;
13896
Craig Topper54a11172011-10-14 07:06:56 +000013897 EVT VT = N->getValueType(0);
13898
Craig Topperb4c94572011-10-21 06:55:01 +000013899 // Create ANDN, BLSI, and BLSR instructions
13900 // BLSI is X & (-X)
13901 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000013902 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
13903 SDValue N0 = N->getOperand(0);
13904 SDValue N1 = N->getOperand(1);
13905 DebugLoc DL = N->getDebugLoc();
13906
13907 // Check LHS for not
13908 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
13909 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
13910 // Check RHS for not
13911 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
13912 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
13913
Craig Topperb4c94572011-10-21 06:55:01 +000013914 // Check LHS for neg
13915 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
13916 isZero(N0.getOperand(0)))
13917 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
13918
13919 // Check RHS for neg
13920 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
13921 isZero(N1.getOperand(0)))
13922 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
13923
13924 // Check LHS for X-1
13925 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
13926 isAllOnes(N0.getOperand(1)))
13927 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
13928
13929 // Check RHS for X-1
13930 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
13931 isAllOnes(N1.getOperand(1)))
13932 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
13933
Craig Topper54a11172011-10-14 07:06:56 +000013934 return SDValue();
13935 }
13936
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013937 // Want to form ANDNP nodes:
13938 // 1) In the hopes of then easily combining them with OR and AND nodes
13939 // to form PBLEND/PSIGN.
13940 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000013941 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000013942 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013943
Nate Begemanb65c1752010-12-17 22:55:37 +000013944 SDValue N0 = N->getOperand(0);
13945 SDValue N1 = N->getOperand(1);
13946 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013947
Nate Begemanb65c1752010-12-17 22:55:37 +000013948 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013949 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013950 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
13951 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013952 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000013953
13954 // Check RHS for vnot
13955 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000013956 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
13957 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013958 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013959
Nate Begemanb65c1752010-12-17 22:55:37 +000013960 return SDValue();
13961}
13962
Evan Cheng760d1942010-01-04 21:22:48 +000013963static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000013964 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000013965 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000013966 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000013967 return SDValue();
13968
Stuart Hastings865f0932011-06-03 23:53:54 +000013969 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
13970 if (R.getNode())
13971 return R;
13972
Evan Cheng760d1942010-01-04 21:22:48 +000013973 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000013974
Evan Cheng760d1942010-01-04 21:22:48 +000013975 SDValue N0 = N->getOperand(0);
13976 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013977
Nate Begemanb65c1752010-12-17 22:55:37 +000013978 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000013979 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperc0d82852011-11-22 00:44:41 +000013980 if (!Subtarget->hasSSSE3orAVX() ||
Craig Topper1666cb62011-11-19 07:07:26 +000013981 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
13982 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013983
Craig Topper1666cb62011-11-19 07:07:26 +000013984 // Canonicalize pandn to RHS
13985 if (N0.getOpcode() == X86ISD::ANDNP)
13986 std::swap(N0, N1);
13987 // or (and (m, x), (pandn m, y))
13988 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
13989 SDValue Mask = N1.getOperand(0);
13990 SDValue X = N1.getOperand(1);
13991 SDValue Y;
13992 if (N0.getOperand(0) == Mask)
13993 Y = N0.getOperand(1);
13994 if (N0.getOperand(1) == Mask)
13995 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013996
Craig Topper1666cb62011-11-19 07:07:26 +000013997 // Check to see if the mask appeared in both the AND and ANDNP and
13998 if (!Y.getNode())
13999 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014000
Craig Topper1666cb62011-11-19 07:07:26 +000014001 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
14002 if (Mask.getOpcode() != ISD::BITCAST ||
14003 X.getOpcode() != ISD::BITCAST ||
14004 Y.getOpcode() != ISD::BITCAST)
14005 return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000014006
Craig Topper1666cb62011-11-19 07:07:26 +000014007 // Look through mask bitcast.
14008 Mask = Mask.getOperand(0);
14009 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014010
Craig Topper1666cb62011-11-19 07:07:26 +000014011 // Validate that the Mask operand is a vector sra node. The sra node
14012 // will be an intrinsic.
14013 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
14014 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014015
Craig Topper1666cb62011-11-19 07:07:26 +000014016 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14017 // there is no psrai.b
14018 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
14019 case Intrinsic::x86_sse2_psrai_w:
14020 case Intrinsic::x86_sse2_psrai_d:
14021 case Intrinsic::x86_avx2_psrai_w:
14022 case Intrinsic::x86_avx2_psrai_d:
14023 break;
14024 default: return SDValue();
Nate Begemanb65c1752010-12-17 22:55:37 +000014025 }
Craig Topper1666cb62011-11-19 07:07:26 +000014026
14027 // Check that the SRA is all signbits.
14028 SDValue SraC = Mask.getOperand(2);
14029 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14030 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14031 if ((SraAmt + 1) != EltBits)
14032 return SDValue();
14033
14034 DebugLoc DL = N->getDebugLoc();
14035
14036 // Now we know we at least have a plendvb with the mask val. See if
14037 // we can form a psignb/w/d.
14038 // psign = x.type == y.type == mask.type && y = sub(0, x);
14039 X = X.getOperand(0);
14040 Y = Y.getOperand(0);
14041 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14042 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Topper31133842011-11-19 07:33:10 +000014043 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType() &&
14044 (EltBits == 8 || EltBits == 16 || EltBits == 32)) {
14045 SDValue Sign = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X,
14046 Mask.getOperand(1));
14047 return DAG.getNode(ISD::BITCAST, DL, VT, Sign);
Craig Topper1666cb62011-11-19 07:07:26 +000014048 }
14049 // PBLENDVB only available on SSE 4.1
Craig Topperc0d82852011-11-22 00:44:41 +000014050 if (!Subtarget->hasSSE41orAVX())
Craig Topper1666cb62011-11-19 07:07:26 +000014051 return SDValue();
14052
14053 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14054
14055 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14056 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14057 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
14058 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, X, Y);
14059 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014060 }
14061 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014062
Craig Topper1666cb62011-11-19 07:07:26 +000014063 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14064 return SDValue();
14065
Nate Begemanb65c1752010-12-17 22:55:37 +000014066 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014067 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14068 std::swap(N0, N1);
14069 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14070 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014071 if (!N0.hasOneUse() || !N1.hasOneUse())
14072 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014073
14074 SDValue ShAmt0 = N0.getOperand(1);
14075 if (ShAmt0.getValueType() != MVT::i8)
14076 return SDValue();
14077 SDValue ShAmt1 = N1.getOperand(1);
14078 if (ShAmt1.getValueType() != MVT::i8)
14079 return SDValue();
14080 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14081 ShAmt0 = ShAmt0.getOperand(0);
14082 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14083 ShAmt1 = ShAmt1.getOperand(0);
14084
14085 DebugLoc DL = N->getDebugLoc();
14086 unsigned Opc = X86ISD::SHLD;
14087 SDValue Op0 = N0.getOperand(0);
14088 SDValue Op1 = N1.getOperand(0);
14089 if (ShAmt0.getOpcode() == ISD::SUB) {
14090 Opc = X86ISD::SHRD;
14091 std::swap(Op0, Op1);
14092 std::swap(ShAmt0, ShAmt1);
14093 }
14094
Evan Cheng8b1190a2010-04-28 01:18:01 +000014095 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014096 if (ShAmt1.getOpcode() == ISD::SUB) {
14097 SDValue Sum = ShAmt1.getOperand(0);
14098 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014099 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14100 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14101 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14102 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014103 return DAG.getNode(Opc, DL, VT,
14104 Op0, Op1,
14105 DAG.getNode(ISD::TRUNCATE, DL,
14106 MVT::i8, ShAmt0));
14107 }
14108 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14109 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14110 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014111 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014112 return DAG.getNode(Opc, DL, VT,
14113 N0.getOperand(0), N1.getOperand(0),
14114 DAG.getNode(ISD::TRUNCATE, DL,
14115 MVT::i8, ShAmt0));
14116 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014117
Evan Cheng760d1942010-01-04 21:22:48 +000014118 return SDValue();
14119}
14120
Craig Topperb4c94572011-10-21 06:55:01 +000014121static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14122 TargetLowering::DAGCombinerInfo &DCI,
14123 const X86Subtarget *Subtarget) {
14124 if (DCI.isBeforeLegalizeOps())
14125 return SDValue();
14126
14127 EVT VT = N->getValueType(0);
14128
14129 if (VT != MVT::i32 && VT != MVT::i64)
14130 return SDValue();
14131
14132 // Create BLSMSK instructions by finding X ^ (X-1)
14133 SDValue N0 = N->getOperand(0);
14134 SDValue N1 = N->getOperand(1);
14135 DebugLoc DL = N->getDebugLoc();
14136
14137 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14138 isAllOnes(N0.getOperand(1)))
14139 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14140
14141 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14142 isAllOnes(N1.getOperand(1)))
14143 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
14144
14145 return SDValue();
14146}
14147
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014148/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
14149static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
14150 const X86Subtarget *Subtarget) {
14151 LoadSDNode *Ld = cast<LoadSDNode>(N);
14152 EVT RegVT = Ld->getValueType(0);
14153 EVT MemVT = Ld->getMemoryVT();
14154 DebugLoc dl = Ld->getDebugLoc();
14155 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14156
14157 ISD::LoadExtType Ext = Ld->getExtensionType();
14158
Nadav Rotemca6f2962011-09-18 19:00:23 +000014159 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014160 // shuffle. We need SSE4 for the shuffles.
14161 // TODO: It is possible to support ZExt by zeroing the undef values
14162 // during the shuffle phase or after the shuffle.
14163 if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
14164 assert(MemVT != RegVT && "Cannot extend to the same type");
14165 assert(MemVT.isVector() && "Must load a vector from memory");
14166
14167 unsigned NumElems = RegVT.getVectorNumElements();
14168 unsigned RegSz = RegVT.getSizeInBits();
14169 unsigned MemSz = MemVT.getSizeInBits();
14170 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotemca6f2962011-09-18 19:00:23 +000014171 // All sizes must be a power of two
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014172 if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
14173
14174 // Attempt to load the original value using a single load op.
14175 // Find a scalar type which is equal to the loaded word size.
14176 MVT SclrLoadTy = MVT::i8;
14177 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14178 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14179 MVT Tp = (MVT::SimpleValueType)tp;
14180 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() == MemSz) {
14181 SclrLoadTy = Tp;
14182 break;
14183 }
14184 }
14185
14186 // Proceed if a load word is found.
14187 if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
14188
14189 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
14190 RegSz/SclrLoadTy.getSizeInBits());
14191
14192 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
14193 RegSz/MemVT.getScalarType().getSizeInBits());
14194 // Can't shuffle using an illegal type.
14195 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14196
14197 // Perform a single load.
14198 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
14199 Ld->getBasePtr(),
14200 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014201 Ld->isNonTemporal(), Ld->isInvariant(),
14202 Ld->getAlignment());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014203
14204 // Insert the word loaded into a vector.
14205 SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
14206 LoadUnitVecVT, ScalarLoad);
14207
14208 // Bitcast the loaded value to a vector of the original element type, in
14209 // the size of the target vector type.
14210 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
14211 unsigned SizeRatio = RegSz/MemSz;
14212
14213 // Redistribute the loaded elements into the different locations.
14214 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14215 for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
14216
14217 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
14218 DAG.getUNDEF(SlicedVec.getValueType()),
14219 ShuffleVec.data());
14220
14221 // Bitcast to the requested type.
14222 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
14223 // Replace the original load with the new sequence
14224 // and return the new chain.
14225 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
14226 return SDValue(ScalarLoad.getNode(), 1);
14227 }
14228
14229 return SDValue();
14230}
14231
Chris Lattner149a4e52008-02-22 02:09:43 +000014232/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014233static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000014234 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000014235 StoreSDNode *St = cast<StoreSDNode>(N);
14236 EVT VT = St->getValue().getValueType();
14237 EVT StVT = St->getMemoryVT();
14238 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000014239 SDValue StoredVal = St->getOperand(1);
14240 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14241
14242 // If we are saving a concatination of two XMM registers, perform two stores.
Nadav Rotem6236f7f2011-08-11 17:05:47 +000014243 // This is better in Sandy Bridge cause one 256-bit mem op is done via two
14244 // 128-bit ones. If in the future the cost becomes only one memory access the
14245 // first version would be better.
Nadav Rotem5e742a32011-08-11 16:41:21 +000014246 if (VT.getSizeInBits() == 256 &&
14247 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
14248 StoredVal.getNumOperands() == 2) {
14249
14250 SDValue Value0 = StoredVal.getOperand(0);
14251 SDValue Value1 = StoredVal.getOperand(1);
14252
14253 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
14254 SDValue Ptr0 = St->getBasePtr();
14255 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
14256
14257 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
14258 St->getPointerInfo(), St->isVolatile(),
14259 St->isNonTemporal(), St->getAlignment());
14260 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
14261 St->getPointerInfo(), St->isVolatile(),
14262 St->isNonTemporal(), St->getAlignment());
14263 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
14264 }
Nadav Rotem614061b2011-08-10 19:30:14 +000014265
14266 // Optimize trunc store (of multiple scalars) to shuffle and store.
14267 // First, pack all of the elements in one place. Next, store to memory
14268 // in fewer chunks.
14269 if (St->isTruncatingStore() && VT.isVector()) {
14270 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14271 unsigned NumElems = VT.getVectorNumElements();
14272 assert(StVT != VT && "Cannot truncate to the same type");
14273 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
14274 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
14275
14276 // From, To sizes and ElemCount must be pow of two
14277 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014278 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000014279 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000014280 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014281
Nadav Rotem614061b2011-08-10 19:30:14 +000014282 unsigned SizeRatio = FromSz / ToSz;
14283
14284 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
14285
14286 // Create a type on which we perform the shuffle
14287 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
14288 StVT.getScalarType(), NumElems*SizeRatio);
14289
14290 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
14291
14292 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
14293 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
14294 for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
14295
14296 // Can't shuffle using an illegal type
14297 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
14298
14299 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
14300 DAG.getUNDEF(WideVec.getValueType()),
14301 ShuffleVec.data());
14302 // At this point all of the data is stored at the bottom of the
14303 // register. We now need to save it to mem.
14304
14305 // Find the largest store unit
14306 MVT StoreType = MVT::i8;
14307 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
14308 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
14309 MVT Tp = (MVT::SimpleValueType)tp;
14310 if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
14311 StoreType = Tp;
14312 }
14313
14314 // Bitcast the original vector into a vector of store-size units
14315 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
14316 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
14317 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
14318 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
14319 SmallVector<SDValue, 8> Chains;
14320 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
14321 TLI.getPointerTy());
14322 SDValue Ptr = St->getBasePtr();
14323
14324 // Perform one or more big stores into memory.
14325 for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
14326 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
14327 StoreType, ShuffWide,
14328 DAG.getIntPtrConstant(i));
14329 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
14330 St->getPointerInfo(), St->isVolatile(),
14331 St->isNonTemporal(), St->getAlignment());
14332 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
14333 Chains.push_back(Ch);
14334 }
14335
14336 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
14337 Chains.size());
14338 }
14339
14340
Chris Lattner149a4e52008-02-22 02:09:43 +000014341 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
14342 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000014343 // A preferable solution to the general problem is to figure out the right
14344 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000014345
14346 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000014347 if (VT.getSizeInBits() != 64)
14348 return SDValue();
14349
Devang Patel578efa92009-06-05 21:57:13 +000014350 const Function *F = DAG.getMachineFunction().getFunction();
14351 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000014352 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +000014353 && Subtarget->hasXMMInt();
Evan Cheng536e6672009-03-12 05:59:15 +000014354 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000014355 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000014356 isa<LoadSDNode>(St->getValue()) &&
14357 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
14358 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014359 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014360 LoadSDNode *Ld = 0;
14361 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000014362 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000014363 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014364 // Must be a store of a load. We currently handle two cases: the load
14365 // is a direct child, and it's under an intervening TokenFactor. It is
14366 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000014367 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000014368 Ld = cast<LoadSDNode>(St->getChain());
14369 else if (St->getValue().hasOneUse() &&
14370 ChainVal->getOpcode() == ISD::TokenFactor) {
14371 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000014372 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000014373 TokenFactorIndex = i;
14374 Ld = cast<LoadSDNode>(St->getValue());
14375 } else
14376 Ops.push_back(ChainVal->getOperand(i));
14377 }
14378 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000014379
Evan Cheng536e6672009-03-12 05:59:15 +000014380 if (!Ld || !ISD::isNormalLoad(Ld))
14381 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014382
Evan Cheng536e6672009-03-12 05:59:15 +000014383 // If this is not the MMX case, i.e. we are just turning i64 load/store
14384 // into f64 load/store, avoid the transformation if there are multiple
14385 // uses of the loaded value.
14386 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
14387 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000014388
Evan Cheng536e6672009-03-12 05:59:15 +000014389 DebugLoc LdDL = Ld->getDebugLoc();
14390 DebugLoc StDL = N->getDebugLoc();
14391 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
14392 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
14393 // pair instead.
14394 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014395 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000014396 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
14397 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014398 Ld->isNonTemporal(), Ld->isInvariant(),
14399 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014400 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000014401 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000014402 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000014403 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000014404 Ops.size());
14405 }
Evan Cheng536e6672009-03-12 05:59:15 +000014406 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000014407 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014408 St->isVolatile(), St->isNonTemporal(),
14409 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000014410 }
Evan Cheng536e6672009-03-12 05:59:15 +000014411
14412 // Otherwise, lower to two pairs of 32-bit loads / stores.
14413 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014414 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
14415 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014416
Owen Anderson825b72b2009-08-11 20:47:22 +000014417 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014418 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014419 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014420 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000014421 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000014422 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000014423 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000014424 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000014425 MinAlign(Ld->getAlignment(), 4));
14426
14427 SDValue NewChain = LoLd.getValue(1);
14428 if (TokenFactorIndex != -1) {
14429 Ops.push_back(LoLd);
14430 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000014431 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000014432 Ops.size());
14433 }
14434
14435 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000014436 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
14437 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000014438
14439 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014440 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000014441 St->isVolatile(), St->isNonTemporal(),
14442 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000014443 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000014444 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000014445 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000014446 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000014447 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000014448 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000014449 }
Dan Gohman475871a2008-07-27 21:46:04 +000014450 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000014451}
14452
Duncan Sands17470be2011-09-22 20:15:48 +000014453/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
14454/// and return the operands for the horizontal operation in LHS and RHS. A
14455/// horizontal operation performs the binary operation on successive elements
14456/// of its first operand, then on successive elements of its second operand,
14457/// returning the resulting values in a vector. For example, if
14458/// A = < float a0, float a1, float a2, float a3 >
14459/// and
14460/// B = < float b0, float b1, float b2, float b3 >
14461/// then the result of doing a horizontal operation on A and B is
14462/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
14463/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
14464/// A horizontal-op B, for some already available A and B, and if so then LHS is
14465/// set to A, RHS to B, and the routine returns 'true'.
14466/// Note that the binary operation should have the property that if one of the
14467/// operands is UNDEF then the result is UNDEF.
14468static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
14469 // Look for the following pattern: if
14470 // A = < float a0, float a1, float a2, float a3 >
14471 // B = < float b0, float b1, float b2, float b3 >
14472 // and
14473 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
14474 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
14475 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
14476 // which is A horizontal-op B.
14477
14478 // At least one of the operands should be a vector shuffle.
14479 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
14480 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
14481 return false;
14482
14483 EVT VT = LHS.getValueType();
14484 unsigned N = VT.getVectorNumElements();
14485
14486 // View LHS in the form
14487 // LHS = VECTOR_SHUFFLE A, B, LMask
14488 // If LHS is not a shuffle then pretend it is the shuffle
14489 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
14490 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
14491 // type VT.
14492 SDValue A, B;
14493 SmallVector<int, 8> LMask(N);
14494 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14495 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
14496 A = LHS.getOperand(0);
14497 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
14498 B = LHS.getOperand(1);
14499 cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
14500 } else {
14501 if (LHS.getOpcode() != ISD::UNDEF)
14502 A = LHS;
14503 for (unsigned i = 0; i != N; ++i)
14504 LMask[i] = i;
14505 }
14506
14507 // Likewise, view RHS in the form
14508 // RHS = VECTOR_SHUFFLE C, D, RMask
14509 SDValue C, D;
14510 SmallVector<int, 8> RMask(N);
14511 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
14512 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
14513 C = RHS.getOperand(0);
14514 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
14515 D = RHS.getOperand(1);
14516 cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
14517 } else {
14518 if (RHS.getOpcode() != ISD::UNDEF)
14519 C = RHS;
14520 for (unsigned i = 0; i != N; ++i)
14521 RMask[i] = i;
14522 }
14523
14524 // Check that the shuffles are both shuffling the same vectors.
14525 if (!(A == C && B == D) && !(A == D && B == C))
14526 return false;
14527
14528 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
14529 if (!A.getNode() && !B.getNode())
14530 return false;
14531
14532 // If A and B occur in reverse order in RHS, then "swap" them (which means
14533 // rewriting the mask).
14534 if (A != C)
14535 for (unsigned i = 0; i != N; ++i) {
14536 unsigned Idx = RMask[i];
14537 if (Idx < N)
14538 RMask[i] += N;
14539 else if (Idx < 2*N)
14540 RMask[i] -= N;
14541 }
14542
14543 // At this point LHS and RHS are equivalent to
14544 // LHS = VECTOR_SHUFFLE A, B, LMask
14545 // RHS = VECTOR_SHUFFLE A, B, RMask
14546 // Check that the masks correspond to performing a horizontal operation.
14547 for (unsigned i = 0; i != N; ++i) {
14548 unsigned LIdx = LMask[i], RIdx = RMask[i];
14549
14550 // Ignore any UNDEF components.
14551 if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
14552 || (!B.getNode() && (LIdx >= N || RIdx >= N)))
14553 continue;
14554
14555 // Check that successive elements are being operated on. If not, this is
14556 // not a horizontal operation.
14557 if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
14558 !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
14559 return false;
14560 }
14561
14562 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
14563 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
14564 return true;
14565}
14566
14567/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
14568static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
14569 const X86Subtarget *Subtarget) {
14570 EVT VT = N->getValueType(0);
14571 SDValue LHS = N->getOperand(0);
14572 SDValue RHS = N->getOperand(1);
14573
14574 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperc0d82852011-11-22 00:44:41 +000014575 if (Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014576 isHorizontalBinOp(LHS, RHS, true))
14577 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
14578 return SDValue();
14579}
14580
14581/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
14582static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
14583 const X86Subtarget *Subtarget) {
14584 EVT VT = N->getValueType(0);
14585 SDValue LHS = N->getOperand(0);
14586 SDValue RHS = N->getOperand(1);
14587
14588 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperc0d82852011-11-22 00:44:41 +000014589 if (Subtarget->hasSSE3orAVX() && (VT == MVT::v4f32 || VT == MVT::v2f64) &&
Duncan Sands17470be2011-09-22 20:15:48 +000014590 isHorizontalBinOp(LHS, RHS, false))
14591 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
14592 return SDValue();
14593}
14594
Chris Lattner6cf73262008-01-25 06:14:17 +000014595/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
14596/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014597static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000014598 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
14599 // F[X]OR(0.0, x) -> x
14600 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000014601 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14602 if (C->getValueAPF().isPosZero())
14603 return N->getOperand(1);
14604 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14605 if (C->getValueAPF().isPosZero())
14606 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000014607 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014608}
14609
14610/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000014611static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000014612 // FAND(0.0, x) -> 0.0
14613 // FAND(x, 0.0) -> 0.0
14614 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
14615 if (C->getValueAPF().isPosZero())
14616 return N->getOperand(0);
14617 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14618 if (C->getValueAPF().isPosZero())
14619 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000014620 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000014621}
14622
Dan Gohmane5af2d32009-01-29 01:59:02 +000014623static SDValue PerformBTCombine(SDNode *N,
14624 SelectionDAG &DAG,
14625 TargetLowering::DAGCombinerInfo &DCI) {
14626 // BT ignores high bits in the bit index operand.
14627 SDValue Op1 = N->getOperand(1);
14628 if (Op1.hasOneUse()) {
14629 unsigned BitWidth = Op1.getValueSizeInBits();
14630 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14631 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014632 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14633 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000014634 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000014635 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14636 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14637 DCI.CommitTargetLoweringOpt(TLO);
14638 }
14639 return SDValue();
14640}
Chris Lattner83e6c992006-10-04 06:57:07 +000014641
Eli Friedman7a5e5552009-06-07 06:52:44 +000014642static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14643 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014644 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000014645 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000014646 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000014647 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000014648 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000014649 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000014650 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014651 }
14652 return SDValue();
14653}
14654
Evan Cheng2e489c42009-12-16 00:53:11 +000014655static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14656 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
14657 // (and (i32 x86isd::setcc_carry), 1)
14658 // This eliminates the zext. This transformation is necessary because
14659 // ISD::SETCC is always legalized to i8.
14660 DebugLoc dl = N->getDebugLoc();
14661 SDValue N0 = N->getOperand(0);
14662 EVT VT = N->getValueType(0);
14663 if (N0.getOpcode() == ISD::AND &&
14664 N0.hasOneUse() &&
14665 N0.getOperand(0).hasOneUse()) {
14666 SDValue N00 = N0.getOperand(0);
14667 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14668 return SDValue();
14669 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14670 if (!C || C->getZExtValue() != 1)
14671 return SDValue();
14672 return DAG.getNode(ISD::AND, dl, VT,
14673 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14674 N00.getOperand(0), N00.getOperand(1)),
14675 DAG.getConstant(1, VT));
14676 }
14677
14678 return SDValue();
14679}
14680
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014681// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
14682static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
14683 unsigned X86CC = N->getConstantOperandVal(0);
14684 SDValue EFLAG = N->getOperand(1);
14685 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014686
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014687 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
14688 // a zext and produces an all-ones bit which is more useful than 0/1 in some
14689 // cases.
14690 if (X86CC == X86::COND_B)
14691 return DAG.getNode(ISD::AND, DL, MVT::i8,
14692 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
14693 DAG.getConstant(X86CC, MVT::i8), EFLAG),
14694 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014695
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014696 return SDValue();
14697}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014698
Benjamin Kramer1396c402011-06-18 11:09:41 +000014699static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
14700 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014701 SDValue Op0 = N->getOperand(0);
14702 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
14703 // a 32-bit target where SSE doesn't support i64->FP operations.
14704 if (Op0.getOpcode() == ISD::LOAD) {
14705 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
14706 EVT VT = Ld->getValueType(0);
14707 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
14708 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
14709 !XTLI->getSubtarget()->is64Bit() &&
14710 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000014711 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
14712 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014713 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
14714 return FILDChain;
14715 }
14716 }
14717 return SDValue();
14718}
14719
Chris Lattner23a01992010-12-20 01:37:09 +000014720// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
14721static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
14722 X86TargetLowering::DAGCombinerInfo &DCI) {
14723 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
14724 // the result is either zero or one (depending on the input carry bit).
14725 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
14726 if (X86::isZeroNode(N->getOperand(0)) &&
14727 X86::isZeroNode(N->getOperand(1)) &&
14728 // We don't have a good way to replace an EFLAGS use, so only do this when
14729 // dead right now.
14730 SDValue(N, 1).use_empty()) {
14731 DebugLoc DL = N->getDebugLoc();
14732 EVT VT = N->getValueType(0);
14733 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
14734 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
14735 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
14736 DAG.getConstant(X86::COND_B,MVT::i8),
14737 N->getOperand(2)),
14738 DAG.getConstant(1, VT));
14739 return DCI.CombineTo(N, Res1, CarryOut);
14740 }
14741
14742 return SDValue();
14743}
14744
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014745// fold (add Y, (sete X, 0)) -> adc 0, Y
14746// (add Y, (setne X, 0)) -> sbb -1, Y
14747// (sub (sete X, 0), Y) -> sbb 0, Y
14748// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014749static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014750 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014751
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014752 // Look through ZExts.
14753 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
14754 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
14755 return SDValue();
14756
14757 SDValue SetCC = Ext.getOperand(0);
14758 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
14759 return SDValue();
14760
14761 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
14762 if (CC != X86::COND_E && CC != X86::COND_NE)
14763 return SDValue();
14764
14765 SDValue Cmp = SetCC.getOperand(1);
14766 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000014767 !X86::isZeroNode(Cmp.getOperand(1)) ||
14768 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000014769 return SDValue();
14770
14771 SDValue CmpOp0 = Cmp.getOperand(0);
14772 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
14773 DAG.getConstant(1, CmpOp0.getValueType()));
14774
14775 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
14776 if (CC == X86::COND_NE)
14777 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
14778 DL, OtherVal.getValueType(), OtherVal,
14779 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
14780 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
14781 DL, OtherVal.getValueType(), OtherVal,
14782 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
14783}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014784
Craig Topper54f952a2011-11-19 09:02:40 +000014785/// PerformADDCombine - Do target-specific dag combines on integer adds.
14786static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
14787 const X86Subtarget *Subtarget) {
14788 EVT VT = N->getValueType(0);
14789 SDValue Op0 = N->getOperand(0);
14790 SDValue Op1 = N->getOperand(1);
14791
14792 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperc0d82852011-11-22 00:44:41 +000014793 if ((Subtarget->hasSSSE3orAVX()) && (VT == MVT::v8i16 || VT == MVT::v4i32) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014794 isHorizontalBinOp(Op0, Op1, true))
14795 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
14796
14797 return OptimizeConditionalInDecrement(N, DAG);
14798}
14799
14800static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
14801 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014802 SDValue Op0 = N->getOperand(0);
14803 SDValue Op1 = N->getOperand(1);
14804
14805 // X86 can't encode an immediate LHS of a sub. See if we can push the
14806 // negation into a preceding instruction.
14807 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014808 // If the RHS of the sub is a XOR with one use and a constant, invert the
14809 // immediate. Then add one to the LHS of the sub so we can turn
14810 // X-Y -> X+~Y+1, saving one register.
14811 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
14812 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000014813 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014814 EVT VT = Op0.getValueType();
14815 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
14816 Op1.getOperand(0),
14817 DAG.getConstant(~XorC, VT));
14818 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000014819 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014820 }
14821 }
14822
Craig Topper54f952a2011-11-19 09:02:40 +000014823 // Try to synthesize horizontal adds from adds of shuffles.
14824 EVT VT = N->getValueType(0);
Craig Topperc0d82852011-11-22 00:44:41 +000014825 if ((Subtarget->hasSSSE3orAVX()) && (VT == MVT::v8i16 || VT == MVT::v4i32) &&
Craig Topper54f952a2011-11-19 09:02:40 +000014826 isHorizontalBinOp(Op0, Op1, false))
14827 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
14828
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000014829 return OptimizeConditionalInDecrement(N, DAG);
14830}
14831
Dan Gohman475871a2008-07-27 21:46:04 +000014832SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000014833 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014834 SelectionDAG &DAG = DCI.DAG;
14835 switch (N->getOpcode()) {
14836 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000014837 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014838 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Duncan Sands6bcd2192011-09-17 16:49:39 +000014839 case ISD::VSELECT:
Chris Lattneraf723b92008-01-25 05:46:26 +000014840 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014841 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Craig Topper54f952a2011-11-19 09:02:40 +000014842 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
14843 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000014844 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000014845 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000014846 case ISD::SHL:
14847 case ISD::SRA:
14848 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000014849 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000014850 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000014851 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000014852 case ISD::LOAD: return PerformLOADCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000014853 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000014854 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000014855 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
14856 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000014857 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000014858 case X86ISD::FOR: return PerformFORCombine(N, DAG);
14859 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000014860 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000014861 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000014862 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000014863 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014864 case X86ISD::SHUFPS: // Handle all target specific shuffles
14865 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000014866 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014867 case X86ISD::PUNPCKHBW:
14868 case X86ISD::PUNPCKHWD:
14869 case X86ISD::PUNPCKHDQ:
14870 case X86ISD::PUNPCKHQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +000014871 case X86ISD::VPUNPCKHBWY:
Craig Topper6347e862011-11-21 06:57:39 +000014872 case X86ISD::VPUNPCKHWDY:
14873 case X86ISD::VPUNPCKHDQY:
14874 case X86ISD::VPUNPCKHQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014875 case X86ISD::UNPCKHPS:
14876 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +000014877 case X86ISD::VUNPCKHPSY:
14878 case X86ISD::VUNPCKHPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014879 case X86ISD::PUNPCKLBW:
14880 case X86ISD::PUNPCKLWD:
14881 case X86ISD::PUNPCKLDQ:
14882 case X86ISD::PUNPCKLQDQ:
Craig Topper6fa583d2011-11-21 08:26:50 +000014883 case X86ISD::VPUNPCKLBWY:
Craig Topper6347e862011-11-21 06:57:39 +000014884 case X86ISD::VPUNPCKLWDY:
14885 case X86ISD::VPUNPCKLDQY:
14886 case X86ISD::VPUNPCKLQDQY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014887 case X86ISD::UNPCKLPS:
14888 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000014889 case X86ISD::VUNPCKLPSY:
14890 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014891 case X86ISD::MOVHLPS:
14892 case X86ISD::MOVLHPS:
14893 case X86ISD::PSHUFD:
14894 case X86ISD::PSHUFHW:
14895 case X86ISD::PSHUFLW:
14896 case X86ISD::MOVSS:
14897 case X86ISD::MOVSD:
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +000014898 case X86ISD::VPERMILPS:
14899 case X86ISD::VPERMILPSY:
14900 case X86ISD::VPERMILPD:
14901 case X86ISD::VPERMILPDY:
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +000014902 case X86ISD::VPERM2F128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014903 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014904 }
14905
Dan Gohman475871a2008-07-27 21:46:04 +000014906 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014907}
14908
Evan Chenge5b51ac2010-04-17 06:13:15 +000014909/// isTypeDesirableForOp - Return true if the target has native support for
14910/// the specified value type and it is 'desirable' to use the type for the
14911/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14912/// instruction encodings are longer and some i16 instructions are slow.
14913bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14914 if (!isTypeLegal(VT))
14915 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014916 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000014917 return true;
14918
14919 switch (Opc) {
14920 default:
14921 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000014922 case ISD::LOAD:
14923 case ISD::SIGN_EXTEND:
14924 case ISD::ZERO_EXTEND:
14925 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014926 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000014927 case ISD::SRL:
14928 case ISD::SUB:
14929 case ISD::ADD:
14930 case ISD::MUL:
14931 case ISD::AND:
14932 case ISD::OR:
14933 case ISD::XOR:
14934 return false;
14935 }
14936}
14937
14938/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000014939/// beneficial for dag combiner to promote the specified node. If true, it
14940/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000014941bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014942 EVT VT = Op.getValueType();
14943 if (VT != MVT::i16)
14944 return false;
14945
Evan Cheng4c26e932010-04-19 19:29:22 +000014946 bool Promote = false;
14947 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014948 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000014949 default: break;
14950 case ISD::LOAD: {
14951 LoadSDNode *LD = cast<LoadSDNode>(Op);
14952 // If the non-extending load has a single use and it's not live out, then it
14953 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014954 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14955 Op.hasOneUse()*/) {
14956 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14957 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14958 // The only case where we'd want to promote LOAD (rather then it being
14959 // promoted as an operand is when it's only use is liveout.
14960 if (UI->getOpcode() != ISD::CopyToReg)
14961 return false;
14962 }
14963 }
Evan Cheng4c26e932010-04-19 19:29:22 +000014964 Promote = true;
14965 break;
14966 }
14967 case ISD::SIGN_EXTEND:
14968 case ISD::ZERO_EXTEND:
14969 case ISD::ANY_EXTEND:
14970 Promote = true;
14971 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014972 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000014973 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000014974 SDValue N0 = Op.getOperand(0);
14975 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000014976 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000014977 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014978 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000014979 break;
14980 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000014981 case ISD::ADD:
14982 case ISD::MUL:
14983 case ISD::AND:
14984 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000014985 case ISD::XOR:
14986 Commute = true;
14987 // fallthrough
14988 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000014989 SDValue N0 = Op.getOperand(0);
14990 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000014991 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014992 return false;
14993 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000014994 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014995 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000014996 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000014997 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000014998 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000014999 }
15000 }
15001
15002 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000015003 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000015004}
15005
Evan Cheng60c07e12006-07-05 22:17:51 +000015006//===----------------------------------------------------------------------===//
15007// X86 Inline Assembly Support
15008//===----------------------------------------------------------------------===//
15009
Chris Lattnerb8105652009-07-20 17:51:36 +000015010bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
15011 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000015012
15013 std::string AsmStr = IA->getAsmString();
15014
15015 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000015016 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000015017 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000015018
15019 switch (AsmPieces.size()) {
15020 default: return false;
15021 case 1:
15022 AsmStr = AsmPieces[0];
15023 AsmPieces.clear();
15024 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
15025
Chris Lattner7a2bdde2011-04-15 05:18:47 +000015026 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000015027 // we will turn this bswap into something that will be lowered to logical ops
15028 // instead of emitting the bswap asm. For now, we don't support 486 or lower
15029 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000015030 // bswap $0
15031 if (AsmPieces.size() == 2 &&
15032 (AsmPieces[0] == "bswap" ||
15033 AsmPieces[0] == "bswapq" ||
15034 AsmPieces[0] == "bswapl") &&
15035 (AsmPieces[1] == "$0" ||
15036 AsmPieces[1] == "${0:q}")) {
15037 // No need to check constraints, nothing other than the equivalent of
15038 // "=r,0" would be valid here.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015039 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015040 if (!Ty || Ty->getBitWidth() % 16 != 0)
15041 return false;
15042 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000015043 }
15044 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000015045 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000015046 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000015047 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000015048 AsmPieces[1] == "$$8," &&
15049 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000015050 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
15051 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015052 const std::string &ConstraintsStr = IA->getConstraintString();
15053 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000015054 std::sort(AsmPieces.begin(), AsmPieces.end());
15055 if (AsmPieces.size() == 4 &&
15056 AsmPieces[0] == "~{cc}" &&
15057 AsmPieces[1] == "~{dirflag}" &&
15058 AsmPieces[2] == "~{flags}" &&
15059 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015060 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015061 if (!Ty || Ty->getBitWidth() % 16 != 0)
15062 return false;
15063 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000015064 }
Chris Lattnerb8105652009-07-20 17:51:36 +000015065 }
15066 break;
15067 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000015068 if (CI->getType()->isIntegerTy(32) &&
15069 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
15070 SmallVector<StringRef, 4> Words;
15071 SplitString(AsmPieces[0], Words, " \t,");
15072 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
15073 Words[2] == "${0:w}") {
15074 Words.clear();
15075 SplitString(AsmPieces[1], Words, " \t,");
15076 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
15077 Words[2] == "$0") {
15078 Words.clear();
15079 SplitString(AsmPieces[2], Words, " \t,");
15080 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
15081 Words[2] == "${0:w}") {
15082 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015083 const std::string &ConstraintsStr = IA->getConstraintString();
15084 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000015085 std::sort(AsmPieces.begin(), AsmPieces.end());
15086 if (AsmPieces.size() == 4 &&
15087 AsmPieces[0] == "~{cc}" &&
15088 AsmPieces[1] == "~{dirflag}" &&
15089 AsmPieces[2] == "~{flags}" &&
15090 AsmPieces[3] == "~{fpsr}") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015091 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015092 if (!Ty || Ty->getBitWidth() % 16 != 0)
15093 return false;
15094 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000015095 }
15096 }
15097 }
15098 }
15099 }
Evan Cheng55d42002011-01-08 01:24:27 +000015100
15101 if (CI->getType()->isIntegerTy(64)) {
15102 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
15103 if (Constraints.size() >= 2 &&
15104 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
15105 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
15106 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
15107 SmallVector<StringRef, 4> Words;
15108 SplitString(AsmPieces[0], Words, " \t");
15109 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000015110 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000015111 SplitString(AsmPieces[1], Words, " \t");
15112 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
15113 Words.clear();
15114 SplitString(AsmPieces[2], Words, " \t,");
15115 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
15116 Words[2] == "%edx") {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015117 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000015118 if (!Ty || Ty->getBitWidth() % 16 != 0)
15119 return false;
15120 return IntrinsicLowering::LowerToByteSwap(CI);
15121 }
Chris Lattnerb8105652009-07-20 17:51:36 +000015122 }
15123 }
15124 }
15125 }
15126 break;
15127 }
15128 return false;
15129}
15130
15131
15132
Chris Lattnerf4dff842006-07-11 02:54:03 +000015133/// getConstraintType - Given a constraint letter, return the type of
15134/// constraint it is for this target.
15135X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000015136X86TargetLowering::getConstraintType(const std::string &Constraint) const {
15137 if (Constraint.size() == 1) {
15138 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000015139 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000015140 case 'q':
15141 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000015142 case 'f':
15143 case 't':
15144 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000015145 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000015146 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000015147 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000015148 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000015149 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000015150 case 'a':
15151 case 'b':
15152 case 'c':
15153 case 'd':
15154 case 'S':
15155 case 'D':
15156 case 'A':
15157 return C_Register;
15158 case 'I':
15159 case 'J':
15160 case 'K':
15161 case 'L':
15162 case 'M':
15163 case 'N':
15164 case 'G':
15165 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000015166 case 'e':
15167 case 'Z':
15168 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000015169 default:
15170 break;
15171 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000015172 }
Chris Lattner4234f572007-03-25 02:14:49 +000015173 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000015174}
15175
John Thompson44ab89e2010-10-29 17:29:13 +000015176/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000015177/// This object must already have been set up with the operand type
15178/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000015179TargetLowering::ConstraintWeight
15180 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000015181 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000015182 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015183 Value *CallOperandVal = info.CallOperandVal;
15184 // If we don't have a value, we can't do a match,
15185 // but allow it at the lowest weight.
15186 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000015187 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000015188 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000015189 // Look at the constraint type.
15190 switch (*constraint) {
15191 default:
John Thompson44ab89e2010-10-29 17:29:13 +000015192 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
15193 case 'R':
15194 case 'q':
15195 case 'Q':
15196 case 'a':
15197 case 'b':
15198 case 'c':
15199 case 'd':
15200 case 'S':
15201 case 'D':
15202 case 'A':
15203 if (CallOperandVal->getType()->isIntegerTy())
15204 weight = CW_SpecificReg;
15205 break;
15206 case 'f':
15207 case 't':
15208 case 'u':
15209 if (type->isFloatingPointTy())
15210 weight = CW_SpecificReg;
15211 break;
15212 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000015213 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000015214 weight = CW_SpecificReg;
15215 break;
15216 case 'x':
15217 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015218 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000015219 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015220 break;
15221 case 'I':
15222 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
15223 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000015224 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015225 }
15226 break;
John Thompson44ab89e2010-10-29 17:29:13 +000015227 case 'J':
15228 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15229 if (C->getZExtValue() <= 63)
15230 weight = CW_Constant;
15231 }
15232 break;
15233 case 'K':
15234 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15235 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
15236 weight = CW_Constant;
15237 }
15238 break;
15239 case 'L':
15240 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15241 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
15242 weight = CW_Constant;
15243 }
15244 break;
15245 case 'M':
15246 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15247 if (C->getZExtValue() <= 3)
15248 weight = CW_Constant;
15249 }
15250 break;
15251 case 'N':
15252 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15253 if (C->getZExtValue() <= 0xff)
15254 weight = CW_Constant;
15255 }
15256 break;
15257 case 'G':
15258 case 'C':
15259 if (dyn_cast<ConstantFP>(CallOperandVal)) {
15260 weight = CW_Constant;
15261 }
15262 break;
15263 case 'e':
15264 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15265 if ((C->getSExtValue() >= -0x80000000LL) &&
15266 (C->getSExtValue() <= 0x7fffffffLL))
15267 weight = CW_Constant;
15268 }
15269 break;
15270 case 'Z':
15271 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
15272 if (C->getZExtValue() <= 0xffffffff)
15273 weight = CW_Constant;
15274 }
15275 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000015276 }
15277 return weight;
15278}
15279
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015280/// LowerXConstraint - try to replace an X constraint, which matches anything,
15281/// with another that has more specific requirements based on the type of the
15282/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000015283const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000015284LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000015285 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
15286 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000015287 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015288 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000015289 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015290 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000015291 return "x";
15292 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015293
Chris Lattner5e764232008-04-26 23:02:14 +000015294 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000015295}
15296
Chris Lattner48884cd2007-08-25 00:47:38 +000015297/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
15298/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000015299void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000015300 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000015301 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000015302 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000015303 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000015304
Eric Christopher100c8332011-06-02 23:16:42 +000015305 // Only support length 1 constraints for now.
15306 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000015307
Eric Christopher100c8332011-06-02 23:16:42 +000015308 char ConstraintLetter = Constraint[0];
15309 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015310 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000015311 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000015312 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015313 if (C->getZExtValue() <= 31) {
15314 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015315 break;
15316 }
Devang Patel84f7fd22007-03-17 00:13:28 +000015317 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015318 return;
Evan Cheng364091e2008-09-22 23:57:37 +000015319 case 'J':
15320 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015321 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000015322 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15323 break;
15324 }
15325 }
15326 return;
15327 case 'K':
15328 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000015329 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000015330 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15331 break;
15332 }
15333 }
15334 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000015335 case 'N':
15336 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000015337 if (C->getZExtValue() <= 255) {
15338 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000015339 break;
15340 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000015341 }
Chris Lattner48884cd2007-08-25 00:47:38 +000015342 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000015343 case 'e': {
15344 // 32-bit signed value
15345 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015346 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15347 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015348 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015349 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000015350 break;
15351 }
15352 // FIXME gcc accepts some relocatable values here too, but only in certain
15353 // memory models; it's complicated.
15354 }
15355 return;
15356 }
15357 case 'Z': {
15358 // 32-bit unsigned value
15359 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000015360 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
15361 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015362 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
15363 break;
15364 }
15365 }
15366 // FIXME gcc accepts some relocatable values here too, but only in certain
15367 // memory models; it's complicated.
15368 return;
15369 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015370 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015371 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000015372 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000015373 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000015374 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000015375 break;
15376 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015377
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015378 // In any sort of PIC mode addresses need to be computed at runtime by
15379 // adding in a register or some sort of table lookup. These can't
15380 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000015381 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000015382 return;
15383
Chris Lattnerdc43a882007-05-03 16:52:29 +000015384 // If we are in non-pic codegen mode, we allow the address of a global (with
15385 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000015386 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015387 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000015388
Chris Lattner49921962009-05-08 18:23:14 +000015389 // Match either (GA), (GA+C), (GA+C1+C2), etc.
15390 while (1) {
15391 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
15392 Offset += GA->getOffset();
15393 break;
15394 } else if (Op.getOpcode() == ISD::ADD) {
15395 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15396 Offset += C->getZExtValue();
15397 Op = Op.getOperand(0);
15398 continue;
15399 }
15400 } else if (Op.getOpcode() == ISD::SUB) {
15401 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
15402 Offset += -C->getZExtValue();
15403 Op = Op.getOperand(0);
15404 continue;
15405 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015406 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015407
Chris Lattner49921962009-05-08 18:23:14 +000015408 // Otherwise, this isn't something we can handle, reject it.
15409 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000015410 }
Eric Christopherfd179292009-08-27 18:07:15 +000015411
Dan Gohman46510a72010-04-15 01:51:59 +000015412 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015413 // If we require an extra load to get this address, as in PIC mode, we
15414 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000015415 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
15416 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000015417 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000015418
Devang Patel0d881da2010-07-06 22:08:15 +000015419 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
15420 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000015421 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015422 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000015423 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015424
Gabor Greifba36cb52008-08-28 21:40:38 +000015425 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000015426 Ops.push_back(Result);
15427 return;
15428 }
Dale Johannesen1784d162010-06-25 21:55:36 +000015429 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000015430}
15431
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015432std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000015433X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000015434 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000015435 // First, see if this is a constraint that directly corresponds to an LLVM
15436 // register class.
15437 if (Constraint.size() == 1) {
15438 // GCC Constraint Letters
15439 switch (Constraint[0]) {
15440 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000015441 // TODO: Slight differences here in allocation order and leaving
15442 // RIP in the class. Do they matter any more here than they do
15443 // in the normal allocation?
15444 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
15445 if (Subtarget->is64Bit()) {
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015446 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015447 return std::make_pair(0U, X86::GR32RegisterClass);
15448 else if (VT == MVT::i16)
15449 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015450 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015451 return std::make_pair(0U, X86::GR8RegisterClass);
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015452 else if (VT == MVT::i64 || VT == MVT::f64)
Eric Christopherd176af82011-06-29 17:23:50 +000015453 return std::make_pair(0U, X86::GR64RegisterClass);
15454 break;
15455 }
15456 // 32-bit fallthrough
15457 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000015458 if (VT == MVT::i32 || VT == MVT::f32)
Eric Christopherd176af82011-06-29 17:23:50 +000015459 return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
15460 else if (VT == MVT::i16)
15461 return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
Eric Christopher5427ede2011-07-14 20:13:52 +000015462 else if (VT == MVT::i8 || VT == MVT::i1)
Eric Christopherd176af82011-06-29 17:23:50 +000015463 return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
15464 else if (VT == MVT::i64)
15465 return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
15466 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015467 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000015468 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015469 if (VT == MVT::i8 || VT == MVT::i1)
Chris Lattner0f65cad2007-04-09 05:49:22 +000015470 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015471 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000015472 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000015473 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000015474 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000015475 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015476 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000015477 if (VT == MVT::i8 || VT == MVT::i1)
Dale Johannesen5f3663e2009-10-07 22:47:20 +000015478 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
15479 if (VT == MVT::i16)
15480 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
15481 if (VT == MVT::i32 || !Subtarget->is64Bit())
15482 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
15483 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015484 case 'f': // FP Stack registers.
15485 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
15486 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000015487 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015488 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015489 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000015490 return std::make_pair(0U, X86::RFP64RegisterClass);
15491 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000015492 case 'y': // MMX_REGS if MMX allowed.
15493 if (!Subtarget->hasMMX()) break;
15494 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015495 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015496 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000015497 // FALL THROUGH.
15498 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000015499 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000015500
Owen Anderson825b72b2009-08-11 20:47:22 +000015501 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000015502 default: break;
15503 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015504 case MVT::f32:
15505 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000015506 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000015507 case MVT::f64:
15508 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000015509 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000015510 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000015511 case MVT::v16i8:
15512 case MVT::v8i16:
15513 case MVT::v4i32:
15514 case MVT::v2i64:
15515 case MVT::v4f32:
15516 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000015517 return std::make_pair(0U, X86::VR128RegisterClass);
15518 }
Chris Lattnerad043e82007-04-09 05:11:28 +000015519 break;
15520 }
15521 }
Scott Michelfdc40a02009-02-17 22:15:04 +000015522
Chris Lattnerf76d1802006-07-31 23:26:50 +000015523 // Use the default implementation in TargetLowering to convert the register
15524 // constraint into a member of a register class.
15525 std::pair<unsigned, const TargetRegisterClass*> Res;
15526 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000015527
15528 // Not found as a standard register?
15529 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015530 // Map st(0) -> st(7) -> ST0
15531 if (Constraint.size() == 7 && Constraint[0] == '{' &&
15532 tolower(Constraint[1]) == 's' &&
15533 tolower(Constraint[2]) == 't' &&
15534 Constraint[3] == '(' &&
15535 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
15536 Constraint[5] == ')' &&
15537 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000015538
Chris Lattner56d77c72009-09-13 22:41:48 +000015539 Res.first = X86::ST0+Constraint[4]-'0';
15540 Res.second = X86::RFP80RegisterClass;
15541 return Res;
15542 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015543
Chris Lattner56d77c72009-09-13 22:41:48 +000015544 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015545 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000015546 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000015547 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015548 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000015549 }
Chris Lattner56d77c72009-09-13 22:41:48 +000015550
15551 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000015552 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000015553 Res.first = X86::EFLAGS;
15554 Res.second = X86::CCRRegisterClass;
15555 return Res;
15556 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000015557
Dale Johannesen330169f2008-11-13 21:52:36 +000015558 // 'A' means EAX + EDX.
15559 if (Constraint == "A") {
15560 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000015561 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000015562 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000015563 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000015564 return Res;
15565 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015566
Chris Lattnerf76d1802006-07-31 23:26:50 +000015567 // Otherwise, check to see if this is a register class of the wrong value
15568 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
15569 // turn into {ax},{dx}.
15570 if (Res.second->hasType(VT))
15571 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015572
Chris Lattnerf76d1802006-07-31 23:26:50 +000015573 // All of the single-register GCC register classes map their values onto
15574 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
15575 // really want an 8-bit or 32-bit register, map to the appropriate register
15576 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000015577 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015578 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015579 unsigned DestReg = 0;
15580 switch (Res.first) {
15581 default: break;
15582 case X86::AX: DestReg = X86::AL; break;
15583 case X86::DX: DestReg = X86::DL; break;
15584 case X86::CX: DestReg = X86::CL; break;
15585 case X86::BX: DestReg = X86::BL; break;
15586 }
15587 if (DestReg) {
15588 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015589 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015590 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015591 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015592 unsigned DestReg = 0;
15593 switch (Res.first) {
15594 default: break;
15595 case X86::AX: DestReg = X86::EAX; break;
15596 case X86::DX: DestReg = X86::EDX; break;
15597 case X86::CX: DestReg = X86::ECX; break;
15598 case X86::BX: DestReg = X86::EBX; break;
15599 case X86::SI: DestReg = X86::ESI; break;
15600 case X86::DI: DestReg = X86::EDI; break;
15601 case X86::BP: DestReg = X86::EBP; break;
15602 case X86::SP: DestReg = X86::ESP; break;
15603 }
15604 if (DestReg) {
15605 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015606 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015607 }
Owen Anderson825b72b2009-08-11 20:47:22 +000015608 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000015609 unsigned DestReg = 0;
15610 switch (Res.first) {
15611 default: break;
15612 case X86::AX: DestReg = X86::RAX; break;
15613 case X86::DX: DestReg = X86::RDX; break;
15614 case X86::CX: DestReg = X86::RCX; break;
15615 case X86::BX: DestReg = X86::RBX; break;
15616 case X86::SI: DestReg = X86::RSI; break;
15617 case X86::DI: DestReg = X86::RDI; break;
15618 case X86::BP: DestReg = X86::RBP; break;
15619 case X86::SP: DestReg = X86::RSP; break;
15620 }
15621 if (DestReg) {
15622 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000015623 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000015624 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000015625 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000015626 } else if (Res.second == X86::FR32RegisterClass ||
15627 Res.second == X86::FR64RegisterClass ||
15628 Res.second == X86::VR128RegisterClass) {
15629 // Handle references to XMM physical registers that got mapped into the
15630 // wrong class. This can happen with constraints like {xmm0} where the
15631 // target independent register mapper will just pick the first match it can
15632 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000015633 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015634 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000015635 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000015636 Res.second = X86::FR64RegisterClass;
15637 else if (X86::VR128RegisterClass->hasType(VT))
15638 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000015639 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015640
Chris Lattnerf76d1802006-07-31 23:26:50 +000015641 return Res;
15642}