blob: d0e2abdf5f970ef0180cfc221bad19ab69a82687 [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"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000048#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.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
David Greenef125a292011-02-08 19:04:41 +000074static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
75
76
David Greenea5f26012011-02-07 19:36:54 +000077/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
78/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000079/// simple subregister reference. Idx is an index in the 128 bits we
80/// want. It need not be aligned to a 128-bit bounday. That makes
81/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000082static SDValue Extract128BitVector(SDValue Vec,
83 SDValue Idx,
84 SelectionDAG &DAG,
85 DebugLoc dl) {
86 EVT VT = Vec.getValueType();
87 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
88
89 EVT ElVT = VT.getVectorElementType();
90
91 int Factor = VT.getSizeInBits() / 128;
92
93 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
94 ElVT,
95 VT.getVectorNumElements() / Factor);
96
97 // Extract from UNDEF is UNDEF.
98 if (Vec.getOpcode() == ISD::UNDEF)
99 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
100
101 if (isa<ConstantSDNode>(Idx)) {
102 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
103
104 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
105 // we can match to VEXTRACTF128.
106 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
107
108 // This is the index of the first element of the 128-bit chunk
109 // we want.
110 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
111 * ElemsPerChunk);
112
113 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
114
115 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
116 VecIdx);
117
118 return Result;
119 }
120
121 return SDValue();
122}
123
124/// Generate a DAG to put 128-bits into a vector > 128 bits. This
125/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000126/// simple superregister reference. Idx is an index in the 128 bits
127/// we want. It need not be aligned to a 128-bit bounday. That makes
128/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000129static SDValue Insert128BitVector(SDValue Result,
130 SDValue Vec,
131 SDValue Idx,
132 SelectionDAG &DAG,
133 DebugLoc dl) {
134 if (isa<ConstantSDNode>(Idx)) {
135 EVT VT = Vec.getValueType();
136 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
137
138 EVT ElVT = VT.getVectorElementType();
139
140 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
141
142 EVT ResultVT = Result.getValueType();
143
144 // Insert the relevant 128 bits.
145 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
146
147 // This is the index of the first element of the 128-bit chunk
148 // we want.
149 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
150 * ElemsPerChunk);
151
152 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
153
154 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
155 VecIdx);
156 return Result;
157 }
158
159 return SDValue();
160}
161
David Greenef125a292011-02-08 19:04:41 +0000162/// Given two vectors, concat them.
163static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
164 DebugLoc dl = Lower.getDebugLoc();
165
166 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
167
168 EVT VT = EVT::getVectorVT(*DAG.getContext(),
169 Lower.getValueType().getVectorElementType(),
170 Lower.getValueType().getVectorNumElements() * 2);
171
172 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
173 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
174
175 // Insert the upper subvector.
176 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
177 DAG.getConstant(
178 // This is half the length of the result
179 // vector. Start inserting the upper 128
180 // bits here.
181 Lower.getValueType().getVectorNumElements(),
182 MVT::i32),
183 DAG, dl);
184
185 // Insert the lower subvector.
186 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
187 return Vec;
188}
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
192 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000193
Evan Cheng2bffee22011-02-01 01:14:13 +0000194 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000195 if (is64Bit)
196 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000197 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000198 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000199
Evan Cheng2bffee22011-02-01 01:14:13 +0000200 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000201 if (is64Bit)
202 return new X8664_ELFTargetObjectFile(TM);
203 return new X8632_ELFTargetObjectFile(TM);
204 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000205 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000206 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000207 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000208}
209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000210X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000211 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000212 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000213 X86ScalarSSEf64 = Subtarget->hasXMMInt();
214 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000216
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000217 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000218 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000221 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222
223 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000224 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopherde5e1012011-03-11 01:05:58 +0000225
226 // For 64-bit since we have so many registers use the ILP scheduler, for
227 // 32-bit code use the register pressure specific scheduling.
228 if (Subtarget->is64Bit())
229 setSchedulingPreference(Sched::ILP);
230 else
231 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000233
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000234 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000235 // Setup Windows compiler runtime calls.
236 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000237 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
238 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000239 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000240 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000241 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000242 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
243 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000244 }
245
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000246 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000247 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000248 setUseUnderscoreSetJmp(false);
249 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000250 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000251 // MS runtime is weird: it exports _setjmp, but longjmp!
252 setUseUnderscoreSetJmp(true);
253 setUseUnderscoreLongJmp(false);
254 } else {
255 setUseUnderscoreSetJmp(true);
256 setUseUnderscoreLongJmp(true);
257 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000258
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000261 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000267
Scott Michelfdc40a02009-02-17 22:15:04 +0000268 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000270 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000272 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
274 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000275
276 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
278 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
279 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
280 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
281 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
282 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000283
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
285 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
287 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
288 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000289
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
292 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000293 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000294 // We have an algorithm for SSE2->double, and we turn this into a
295 // 64-bit FILD followed by conditional FADD for other targets.
296 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000297 // We have an algorithm for SSE2, and we turn this into a 64-bit
298 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000299 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000300 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301
302 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
303 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
305 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306
Devang Patel6a784892009-06-05 18:48:29 +0000307 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000308 // SSE has no i16 to fp conversion, only i32
309 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000311 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000313 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
315 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000316 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000317 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
319 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000320 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000321
Dale Johannesen73328d12007-09-19 23:55:34 +0000322 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
323 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
325 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000326
Evan Cheng02568ff2006-01-30 22:13:22 +0000327 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
328 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
330 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000331
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000332 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000334 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000336 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
338 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339 }
340
341 // Handle FP_TO_UINT by promoting the destination to a larger signed
342 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
344 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
345 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000346
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
349 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000350 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000351 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 // Expand FP_TO_UINT into a select.
353 // FIXME: We would like to use a Custom expander here eventually to do
354 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000356 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000357 // With SSE3 we can use fisttpll to convert to a signed i64; without
358 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000360 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000361
Chris Lattner399610a2006-12-05 18:22:22 +0000362 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000363 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
365 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000366 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000368 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000369 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000370 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000371 }
Chris Lattner21f66852005-12-23 05:15:23 +0000372
Dan Gohmanb00ee212008-02-18 19:34:53 +0000373 // Scalar integer divide and remainder are lowered to use operations that
374 // produce two results, to match the available instructions. This exposes
375 // the two-result form to trivial CSE, which is able to combine x/y and x%y
376 // into a single instruction.
377 //
378 // Scalar integer multiply-high is also lowered to use two-result
379 // operations, to match the available instructions. However, plain multiply
380 // (low) operations are left as Legal, as there are single-result
381 // instructions for this in x86. Using the two-result multiply instructions
382 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000383 for (unsigned i = 0, e = 4; i != e; ++i) {
384 MVT VT = IntVTs[i];
385 setOperationAction(ISD::MULHS, VT, Expand);
386 setOperationAction(ISD::MULHU, VT, Expand);
387 setOperationAction(ISD::SDIV, VT, Expand);
388 setOperationAction(ISD::UDIV, VT, Expand);
389 setOperationAction(ISD::SREM, VT, Expand);
390 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000391
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000392 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000393 setOperationAction(ISD::ADDC, VT, Custom);
394 setOperationAction(ISD::ADDE, VT, Custom);
395 setOperationAction(ISD::SUBC, VT, Custom);
396 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000397 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
400 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
401 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
402 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
408 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
409 setOperationAction(ISD::FREM , MVT::f32 , Expand);
410 setOperationAction(ISD::FREM , MVT::f64 , Expand);
411 setOperationAction(ISD::FREM , MVT::f80 , Expand);
412 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000416 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
419 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000420 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 }
424
Benjamin Kramer1292c222010-12-04 20:32:23 +0000425 if (Subtarget->hasPOPCNT()) {
426 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
427 } else {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
429 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
430 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
431 if (Subtarget->is64Bit())
432 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
433 }
434
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
436 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000437
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000438 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000439 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000440 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000442 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
444 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
445 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
447 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000448 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
450 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
451 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000453 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000455 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000456 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000458
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000459 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
461 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
462 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
463 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000464 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
466 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000467 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
470 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
471 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
472 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000473 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000474 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000475 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
477 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
478 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000479 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
481 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
482 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000483 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000485 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000487
Eric Christopher9a9d2752010-07-22 02:48:34 +0000488 // We may not have a libcall for MEMBARRIER so we should lower this.
489 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000490
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000491 // On X86 and X86-64, atomic operations are lowered to locked instructions.
492 // Locked instructions, in turn, have implicit fence semantics (all memory
493 // operations are flushed before issuing the locked instruction, and they
494 // are not buffered), so we can fold away the common pattern of
495 // fence-atomic-fence.
496 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000497
Mon P Wang63307c32008-05-05 19:05:59 +0000498 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000499 for (unsigned i = 0, e = 4; i != e; ++i) {
500 MVT VT = IntVTs[i];
501 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
503 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000504
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000505 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
507 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
508 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
509 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
510 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000513 }
514
Evan Cheng3c992d22006-03-07 02:02:57 +0000515 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000516 if (!Subtarget->isTargetDarwin() &&
517 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000518 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000520 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000521
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
523 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
524 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
525 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000526 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000527 setExceptionPointerRegister(X86::RAX);
528 setExceptionSelectorRegister(X86::RDX);
529 } else {
530 setExceptionPointerRegister(X86::EAX);
531 setExceptionSelectorRegister(X86::EDX);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
534 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000535
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000539
Nate Begemanacc398c2006-01-25 18:21:52 +0000540 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::VASTART , MVT::Other, Custom);
542 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000543 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::VAARG , MVT::Other, Custom);
545 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000546 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VAARG , MVT::Other, Expand);
548 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000549 }
Evan Chengae642192007-03-02 23:16:35 +0000550
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
552 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
NAKAMURA Takumia2e07622011-03-24 07:07:00 +0000553 setOperationAction(ISD::DYNAMIC_STACKALLOC,
554 (Subtarget->is64Bit() ? MVT::i64 : MVT::i32),
555 (Subtarget->isTargetCOFF()
556 && !Subtarget->isTargetEnvMacho()
557 ? Custom : Expand));
Chris Lattnerb99329e2006-01-13 02:42:53 +0000558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000560 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
563 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000564
Evan Cheng223547a2006-01-31 22:28:30 +0000565 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FABS , MVT::f64, Custom);
567 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000568
569 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::FNEG , MVT::f64, Custom);
571 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000572
Evan Cheng68c47cb2007-01-05 07:55:56 +0000573 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
575 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000576
Evan Chengd25e9e82006-02-02 00:28:23 +0000577 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FSIN , MVT::f32, Expand);
581 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000582
Chris Lattnera54aa942006-01-29 06:26:08 +0000583 // Expand FP immediates into loads from the stack, except for the special
584 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000585 addLegalFPImmediate(APFloat(+0.0)); // xorpd
586 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000587 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000588 // Use SSE for f32, x87 for f64.
589 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
591 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
593 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600
601 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FSIN , MVT::f32, Expand);
607 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
Nate Begemane1795842008-02-14 08:57:00 +0000609 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 addLegalFPImmediate(APFloat(+0.0f)); // xorps
611 addLegalFPImmediate(APFloat(+0.0)); // FLD0
612 addLegalFPImmediate(APFloat(+1.0)); // FLD1
613 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
614 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
615
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
618 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000619 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000620 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000621 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
624 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
627 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
628 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000630
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
633 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000635 addLegalFPImmediate(APFloat(+0.0)); // FLD0
636 addLegalFPImmediate(APFloat(+1.0)); // FLD1
637 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
638 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000643 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644
Dale Johannesen59a58732007-08-05 18:49:15 +0000645 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000646 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
648 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000650 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000651 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 addLegalFPImmediate(TmpFlt); // FLD0
653 TmpFlt.changeSign();
654 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000655
656 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000657 APFloat TmpFlt2(+1.0);
658 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
659 &ignored);
660 addLegalFPImmediate(TmpFlt2); // FLD1
661 TmpFlt2.changeSign();
662 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
663 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Evan Chengc7ce29b2009-02-13 22:36:38 +0000665 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
667 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000669 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
673 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
674 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FLOG, MVT::f80, Expand);
677 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
678 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
679 setOperationAction(ISD::FEXP, MVT::f80, Expand);
680 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000681
Mon P Wangf007a8b2008-11-06 05:31:54 +0000682 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000683 // (for widening) or expand (for scalarization). Then we will selectively
684 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
686 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
687 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000703 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
704 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000736 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000737 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
741 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743 setTruncStoreAction((MVT::SimpleValueType)VT,
744 (MVT::SimpleValueType)InnerVT, Expand);
745 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000748 }
749
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000752 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000753 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000754 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000755 }
756
Dale Johannesen0488fb62010-09-30 23:57:10 +0000757 // MMX-sized vectors (other than x86mmx) are expected to be expanded
758 // into smaller operations.
759 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
760 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
761 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
762 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
763 setOperationAction(ISD::AND, MVT::v8i8, Expand);
764 setOperationAction(ISD::AND, MVT::v4i16, Expand);
765 setOperationAction(ISD::AND, MVT::v2i32, Expand);
766 setOperationAction(ISD::AND, MVT::v1i64, Expand);
767 setOperationAction(ISD::OR, MVT::v8i8, Expand);
768 setOperationAction(ISD::OR, MVT::v4i16, Expand);
769 setOperationAction(ISD::OR, MVT::v2i32, Expand);
770 setOperationAction(ISD::OR, MVT::v1i64, Expand);
771 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
772 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
773 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
774 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
775 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
780 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
781 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
782 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
783 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
785 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
786 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
787 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000789 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
793 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
794 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
795 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
796 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
797 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
798 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
799 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
800 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
803 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804 }
805
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000806 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000808
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000809 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000815
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
817 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
818 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
819 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
821 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
822 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
823 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
824 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
825 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
826 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
827 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
828 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
829 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
831 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
834 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
835 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
836 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000837
Owen Anderson825b72b2009-08-11 20:47:22 +0000838 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
839 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000843
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000844 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
845 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
846 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
847 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
849
Evan Cheng2c3ae372006-04-12 21:21:57 +0000850 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000853 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000855 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000856 // Do not attempt to custom lower non-128-bit vectors
857 if (!VT.is128BitVector())
858 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::BUILD_VECTOR,
860 VT.getSimpleVT().SimpleTy, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE,
862 VT.getSimpleVT().SimpleTy, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
868 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
869 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
870 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
871 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
872 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000873
Nate Begemancdd1eec2008-02-12 22:51:28 +0000874 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000877 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000878
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000879 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000882 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000885 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000886 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000887
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000898 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000901
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
904 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
905 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
906 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
909 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000910 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000911
Nate Begeman14d12ca2008-02-11 04:19:36 +0000912 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000913 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
916 setOperationAction(ISD::FRINT, MVT::f32, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
918 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
919 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
920 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
921 setOperationAction(ISD::FRINT, MVT::f64, Legal);
922 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
923
Nate Begeman14d12ca2008-02-11 04:19:36 +0000924 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000927 // Can turn SHL into an integer multiply.
928 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000929 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000930
Nate Begeman14d12ca2008-02-11 04:19:36 +0000931 // i8 and i16 vectors are custom , because the source register and source
932 // source memory operand types are not the same width. f32 vectors are
933 // custom since the immediate controlling the insert encodes additional
934 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
943 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000944
945 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948 }
949 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000950
Nadav Rotem43012222011-05-11 08:12:09 +0000951 if (Subtarget->hasSSE2()) {
952 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
953 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
954 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
955
956 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
957 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
958 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
959
960 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
961 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
962 }
963
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000964 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000966
David Greene9b9838d2009-06-29 16:47:10 +0000967 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
969 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
970 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
971 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000972 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000973
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
975 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
976 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
977 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000978
Owen Anderson825b72b2009-08-11 20:47:22 +0000979 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
980 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
981 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
982 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
983 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
984 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000985
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
987 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
988 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
989 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
990 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
991 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000992
David Greene54d8eba2011-01-27 22:38:56 +0000993 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
994 // insert_vector_elt extract_subvector and extract_vector_elt for
995 // 256-bit types.
996 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
997 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
998 ++i) {
999 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1000 // Do not attempt to custom lower non-256-bit vectors
1001 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
1002 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +00001003 continue;
David Greene9b9838d2009-06-29 16:47:10 +00001004 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1005 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001006 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001007 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001008 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001009 }
David Greene54d8eba2011-01-27 22:38:56 +00001010 // Custom-lower insert_subvector and extract_subvector based on
1011 // the result type.
1012 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1013 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1014 ++i) {
1015 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1016 // Do not attempt to custom lower non-256-bit vectors
1017 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001018 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001019
1020 if (MVT(VT).getSizeInBits() == 128) {
1021 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001022 }
David Greene54d8eba2011-01-27 22:38:56 +00001023 else if (MVT(VT).getSizeInBits() == 256) {
1024 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1025 }
David Greene9b9838d2009-06-29 16:47:10 +00001026 }
1027
David Greene54d8eba2011-01-27 22:38:56 +00001028 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1029 // Don't promote loads because we need them for VPERM vector index versions.
1030
1031 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1032 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1033 VT++) {
1034 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1035 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1036 continue;
1037 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1038 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1039 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1040 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1041 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1042 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1043 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1044 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1045 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1046 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1047 }
David Greene9b9838d2009-06-29 16:47:10 +00001048 }
1049
Evan Cheng6be2c582006-04-05 23:38:46 +00001050 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001052
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001053
Eli Friedman962f5492010-06-02 19:35:46 +00001054 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1055 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001056 //
Eli Friedman962f5492010-06-02 19:35:46 +00001057 // FIXME: We really should do custom legalization for addition and
1058 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1059 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001060 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1061 // Add/Sub/Mul with overflow operations are custom lowered.
1062 MVT VT = IntVTs[i];
1063 setOperationAction(ISD::SADDO, VT, Custom);
1064 setOperationAction(ISD::UADDO, VT, Custom);
1065 setOperationAction(ISD::SSUBO, VT, Custom);
1066 setOperationAction(ISD::USUBO, VT, Custom);
1067 setOperationAction(ISD::SMULO, VT, Custom);
1068 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001069 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001070
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001071 // There are no 8-bit 3-address imul/mul instructions
1072 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1073 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001074
Evan Chengd54f2d52009-03-31 19:38:51 +00001075 if (!Subtarget->is64Bit()) {
1076 // These libcalls are not available in 32-bit.
1077 setLibcallName(RTLIB::SHL_I128, 0);
1078 setLibcallName(RTLIB::SRL_I128, 0);
1079 setLibcallName(RTLIB::SRA_I128, 0);
1080 }
1081
Evan Cheng206ee9d2006-07-07 08:33:52 +00001082 // We have target-specific dag combine patterns for the following nodes:
1083 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001084 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001085 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001086 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001087 setTargetDAGCombine(ISD::SHL);
1088 setTargetDAGCombine(ISD::SRA);
1089 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001090 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001091 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001092 setTargetDAGCombine(ISD::ADD);
1093 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001094 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001095 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001096 if (Subtarget->is64Bit())
1097 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001098
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001099 computeRegisterProperties();
1100
Evan Cheng05219282011-01-06 06:52:41 +00001101 // On Darwin, -Os means optimize for size without hurting performance,
1102 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001103 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001104 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001105 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001106 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1107 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1108 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001109 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001110 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001111
1112 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001113}
1114
Scott Michel5b8f82e2008-03-10 15:42:14 +00001115
Owen Anderson825b72b2009-08-11 20:47:22 +00001116MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1117 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001118}
1119
1120
Evan Cheng29286502008-01-23 23:17:41 +00001121/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1122/// the desired ByVal argument alignment.
1123static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1124 if (MaxAlign == 16)
1125 return;
1126 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1127 if (VTy->getBitWidth() == 128)
1128 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001129 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1130 unsigned EltAlign = 0;
1131 getMaxByValAlign(ATy->getElementType(), EltAlign);
1132 if (EltAlign > MaxAlign)
1133 MaxAlign = EltAlign;
1134 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1135 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1136 unsigned EltAlign = 0;
1137 getMaxByValAlign(STy->getElementType(i), EltAlign);
1138 if (EltAlign > MaxAlign)
1139 MaxAlign = EltAlign;
1140 if (MaxAlign == 16)
1141 break;
1142 }
1143 }
1144 return;
1145}
1146
1147/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1148/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001149/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1150/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001151unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001152 if (Subtarget->is64Bit()) {
1153 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001154 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001155 if (TyAlign > 8)
1156 return TyAlign;
1157 return 8;
1158 }
1159
Evan Cheng29286502008-01-23 23:17:41 +00001160 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001161 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001162 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001163 return Align;
1164}
Chris Lattner2b02a442007-02-25 08:29:00 +00001165
Evan Chengf0df0312008-05-15 08:39:06 +00001166/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001167/// and store operations as a result of memset, memcpy, and memmove
1168/// lowering. If DstAlign is zero that means it's safe to destination
1169/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1170/// means there isn't a need to check it against alignment requirement,
1171/// probably because the source does not need to be loaded. If
1172/// 'NonScalarIntSafe' is true, that means it's safe to return a
1173/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1174/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1175/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001176/// It returns EVT::Other if the type should be determined using generic
1177/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001178EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001179X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1180 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001181 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001182 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001183 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001184 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1185 // linux. This is because the stack realignment code can't handle certain
1186 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001187 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001188 if (NonScalarIntSafe &&
1189 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001190 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001191 (Subtarget->isUnalignedMemAccessFast() ||
1192 ((DstAlign == 0 || DstAlign >= 16) &&
1193 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001194 Subtarget->getStackAlignment() >= 16) {
1195 if (Subtarget->hasSSE2())
1196 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001197 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001198 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001199 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001200 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001201 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001202 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001203 // Do not use f64 to lower memcpy if source is string constant. It's
1204 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001205 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001206 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001207 }
Evan Chengf0df0312008-05-15 08:39:06 +00001208 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001209 return MVT::i64;
1210 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001211}
1212
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001213/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1214/// current function. The returned value is a member of the
1215/// MachineJumpTableInfo::JTEntryKind enum.
1216unsigned X86TargetLowering::getJumpTableEncoding() const {
1217 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1218 // symbol.
1219 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1220 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001221 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001222
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001223 // Otherwise, use the normal jump table encoding heuristics.
1224 return TargetLowering::getJumpTableEncoding();
1225}
1226
Chris Lattnerc64daab2010-01-26 05:02:42 +00001227const MCExpr *
1228X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1229 const MachineBasicBlock *MBB,
1230 unsigned uid,MCContext &Ctx) const{
1231 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1232 Subtarget->isPICStyleGOT());
1233 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1234 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001235 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1236 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001237}
1238
Evan Chengcc415862007-11-09 01:32:10 +00001239/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1240/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001241SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001242 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001243 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001244 // This doesn't have DebugLoc associated with it, but is not really the
1245 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001246 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001247 return Table;
1248}
1249
Chris Lattner589c6f62010-01-26 06:28:43 +00001250/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1251/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1252/// MCExpr.
1253const MCExpr *X86TargetLowering::
1254getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1255 MCContext &Ctx) const {
1256 // X86-64 uses RIP relative addressing based on the jump table label.
1257 if (Subtarget->isPICStyleRIPRel())
1258 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1259
1260 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001261 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001262}
1263
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001264// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001265std::pair<const TargetRegisterClass*, uint8_t>
1266X86TargetLowering::findRepresentativeClass(EVT VT) const{
1267 const TargetRegisterClass *RRC = 0;
1268 uint8_t Cost = 1;
1269 switch (VT.getSimpleVT().SimpleTy) {
1270 default:
1271 return TargetLowering::findRepresentativeClass(VT);
1272 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1273 RRC = (Subtarget->is64Bit()
1274 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1275 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001276 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001277 RRC = X86::VR64RegisterClass;
1278 break;
1279 case MVT::f32: case MVT::f64:
1280 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1281 case MVT::v4f32: case MVT::v2f64:
1282 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1283 case MVT::v4f64:
1284 RRC = X86::VR128RegisterClass;
1285 break;
1286 }
1287 return std::make_pair(RRC, Cost);
1288}
1289
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001290bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1291 unsigned &Offset) const {
1292 if (!Subtarget->isTargetLinux())
1293 return false;
1294
1295 if (Subtarget->is64Bit()) {
1296 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1297 Offset = 0x28;
1298 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1299 AddressSpace = 256;
1300 else
1301 AddressSpace = 257;
1302 } else {
1303 // %gs:0x14 on i386
1304 Offset = 0x14;
1305 AddressSpace = 256;
1306 }
1307 return true;
1308}
1309
1310
Chris Lattner2b02a442007-02-25 08:29:00 +00001311//===----------------------------------------------------------------------===//
1312// Return Value Calling Convention Implementation
1313//===----------------------------------------------------------------------===//
1314
Chris Lattner59ed56b2007-02-28 04:55:35 +00001315#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001316
Michael J. Spencerec38de22010-10-10 22:04:20 +00001317bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001318X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001319 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001320 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001321 SmallVector<CCValAssign, 16> RVLocs;
1322 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001323 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001324 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001325}
1326
Dan Gohman98ca4f22009-08-05 01:29:28 +00001327SDValue
1328X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001329 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001330 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001331 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001332 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001333 MachineFunction &MF = DAG.getMachineFunction();
1334 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001335
Chris Lattner9774c912007-02-27 05:28:59 +00001336 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1338 RVLocs, *DAG.getContext());
1339 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Evan Chengdcea1632010-02-04 02:40:39 +00001341 // Add the regs to the liveout set for the function.
1342 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1343 for (unsigned i = 0; i != RVLocs.size(); ++i)
1344 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1345 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001348
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001350 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1351 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001352 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1353 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001354
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001355 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001356 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1357 CCValAssign &VA = RVLocs[i];
1358 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001359 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001360 EVT ValVT = ValToCopy.getValueType();
1361
Dale Johannesenc4510512010-09-24 19:05:48 +00001362 // If this is x86-64, and we disabled SSE, we can't return FP values,
1363 // or SSE or MMX vectors.
1364 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1365 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001366 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001367 report_fatal_error("SSE register return with SSE disabled");
1368 }
1369 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1370 // llvm-gcc has never done it right and no one has noticed, so this
1371 // should be OK for now.
1372 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001373 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001374 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001375
Chris Lattner447ff682008-03-11 03:23:40 +00001376 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1377 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001378 if (VA.getLocReg() == X86::ST0 ||
1379 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001380 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1381 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001382 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001383 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001384 RetOps.push_back(ValToCopy);
1385 // Don't emit a copytoreg.
1386 continue;
1387 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001388
Evan Cheng242b38b2009-02-23 09:03:22 +00001389 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1390 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001391 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001392 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001393 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001394 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001395 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1396 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001397 // If we don't have SSE2 available, convert to v4f32 so the generated
1398 // register is legal.
1399 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001401 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001402 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001403 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001404
Dale Johannesendd64c412009-02-04 00:33:20 +00001405 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001406 Flag = Chain.getValue(1);
1407 }
Dan Gohman61a92132008-04-21 23:59:07 +00001408
1409 // The x86-64 ABI for returning structs by value requires that we copy
1410 // the sret argument into %rax for the return. We saved the argument into
1411 // a virtual register in the entry block, so now we copy the value out
1412 // and into %rax.
1413 if (Subtarget->is64Bit() &&
1414 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1415 MachineFunction &MF = DAG.getMachineFunction();
1416 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1417 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001418 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001419 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001420 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001421
Dale Johannesendd64c412009-02-04 00:33:20 +00001422 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001423 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001424
1425 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001426 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001427 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001428
Chris Lattner447ff682008-03-11 03:23:40 +00001429 RetOps[0] = Chain; // Update chain.
1430
1431 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001433 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001434
1435 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001437}
1438
Evan Cheng3d2125c2010-11-30 23:55:39 +00001439bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1440 if (N->getNumValues() != 1)
1441 return false;
1442 if (!N->hasNUsesOfValue(1, 0))
1443 return false;
1444
1445 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001446 if (Copy->getOpcode() != ISD::CopyToReg &&
1447 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001448 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001449
1450 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001451 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001452 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001453 if (UI->getOpcode() != X86ISD::RET_FLAG)
1454 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455 HasRet = true;
1456 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459}
1460
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001461EVT
1462X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001463 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001464 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001465 // TODO: Is this also valid on 32-bit?
1466 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001467 ReturnMVT = MVT::i8;
1468 else
1469 ReturnMVT = MVT::i32;
1470
1471 EVT MinVT = getRegisterType(Context, ReturnMVT);
1472 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001473}
1474
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475/// LowerCallResult - Lower the result values of a call into the
1476/// appropriate copies out of appropriate physical registers.
1477///
1478SDValue
1479X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001480 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481 const SmallVectorImpl<ISD::InputArg> &Ins,
1482 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001483 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001484
Chris Lattnere32bbf62007-02-28 07:09:55 +00001485 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001486 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001487 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001489 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001490 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001491
Chris Lattner3085e152007-02-25 08:59:22 +00001492 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001493 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001494 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001495 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001496
Torok Edwin3f142c32009-02-01 18:15:56 +00001497 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001499 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001500 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001501 }
1502
Evan Cheng79fb3b42009-02-20 20:43:02 +00001503 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001504
1505 // If this is a call to a function that returns an fp value on the floating
1506 // point stack, we must guarantee the the value is popped from the stack, so
1507 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1508 // if the return value is not used. We use the FpGET_ST0 instructions
1509 // instead.
1510 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1511 // If we prefer to use the value in xmm registers, copy it out as f80 and
1512 // use a truncate to move it from fp stack reg to xmm reg.
1513 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1514 bool isST0 = VA.getLocReg() == X86::ST0;
1515 unsigned Opc = 0;
1516 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1517 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1518 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1519 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001520 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001521 Ops, 2), 1);
1522 Val = Chain.getValue(0);
1523
1524 // Round the f80 to the right size, which also moves it to the appropriate
1525 // xmm register.
1526 if (CopyVT != VA.getValVT())
1527 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1528 // This truncation won't change the value.
1529 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001530 } else {
1531 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1532 CopyVT, InFlag).getValue(1);
1533 Val = Chain.getValue(0);
1534 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001535 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001536 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001537 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001538
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001540}
1541
1542
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001543//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001544// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001545//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001546// StdCall calling convention seems to be standard for many Windows' API
1547// routines and around. It differs from C calling convention just a little:
1548// callee should clean up the stack, not caller. Symbols should be also
1549// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001550// For info on fast calling convention see Fast Calling Convention (tail call)
1551// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001552
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001554/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001555static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1556 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001558
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001560}
1561
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001562/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001563/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564static bool
1565ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1566 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001568
Dan Gohman98ca4f22009-08-05 01:29:28 +00001569 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001570}
1571
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001572/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1573/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001574/// the specific parameter attribute. The copy will be passed as a byval
1575/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001576static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001577CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001578 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1579 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001580 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001581
Dale Johannesendd64c412009-02-04 00:33:20 +00001582 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001583 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001584 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001585}
1586
Chris Lattner29689432010-03-11 00:22:57 +00001587/// IsTailCallConvention - Return true if the calling convention is one that
1588/// supports tail call optimization.
1589static bool IsTailCallConvention(CallingConv::ID CC) {
1590 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1591}
1592
Evan Cheng485fafc2011-03-21 01:19:09 +00001593bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1594 if (!CI->isTailCall())
1595 return false;
1596
1597 CallSite CS(CI);
1598 CallingConv::ID CalleeCC = CS.getCallingConv();
1599 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1600 return false;
1601
1602 return true;
1603}
1604
Evan Cheng0c439eb2010-01-27 00:07:07 +00001605/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1606/// a tailcall target by changing its ABI.
1607static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001608 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001609}
1610
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611SDValue
1612X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001613 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001614 const SmallVectorImpl<ISD::InputArg> &Ins,
1615 DebugLoc dl, SelectionDAG &DAG,
1616 const CCValAssign &VA,
1617 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001618 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001619 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001620 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001621 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001622 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001623 EVT ValVT;
1624
1625 // If value is passed by pointer we have address passed instead of the value
1626 // itself.
1627 if (VA.getLocInfo() == CCValAssign::Indirect)
1628 ValVT = VA.getLocVT();
1629 else
1630 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001631
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001632 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001633 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001634 // In case of tail call optimization mark all arguments mutable. Since they
1635 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001636 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001637 unsigned Bytes = Flags.getByValSize();
1638 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1639 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001640 return DAG.getFrameIndex(FI, getPointerTy());
1641 } else {
1642 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001643 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001644 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1645 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001646 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001647 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001648 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001649}
1650
Dan Gohman475871a2008-07-27 21:46:04 +00001651SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001653 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 bool isVarArg,
1655 const SmallVectorImpl<ISD::InputArg> &Ins,
1656 DebugLoc dl,
1657 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001658 SmallVectorImpl<SDValue> &InVals)
1659 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001660 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001662
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 const Function* Fn = MF.getFunction();
1664 if (Fn->hasExternalLinkage() &&
1665 Subtarget->isTargetCygMing() &&
1666 Fn->getName() == "main")
1667 FuncInfo->setForceFramePointer(true);
1668
Evan Cheng1bc78042006-04-26 01:20:17 +00001669 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001671 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001672
Chris Lattner29689432010-03-11 00:22:57 +00001673 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1674 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001675
Chris Lattner638402b2007-02-28 07:00:42 +00001676 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001677 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1679 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001680
1681 // Allocate shadow area for Win64
1682 if (IsWin64) {
1683 CCInfo.AllocateStack(32, 8);
1684 }
1685
Duncan Sands45907662010-10-31 13:21:44 +00001686 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001687
Chris Lattnerf39f7712007-02-28 05:46:49 +00001688 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001689 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001690 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1691 CCValAssign &VA = ArgLocs[i];
1692 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1693 // places.
1694 assert(VA.getValNo() != LastVal &&
1695 "Don't support value assigned to multiple locs yet");
1696 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001697
Chris Lattnerf39f7712007-02-28 05:46:49 +00001698 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001699 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001700 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001702 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001707 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001709 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1710 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001711 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001712 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001713 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001714 RC = X86::VR64RegisterClass;
1715 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001716 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001717
Devang Patel68e6bee2011-02-21 23:21:26 +00001718 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Chris Lattnerf39f7712007-02-28 05:46:49 +00001721 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1722 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1723 // right size.
1724 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001725 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 DAG.getValueType(VA.getValVT()));
1727 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001728 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001729 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001730 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001731 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001732
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001733 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001734 // Handle MMX values passed in XMM regs.
1735 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001736 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1737 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001738 } else
1739 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001740 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001741 } else {
1742 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001743 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001744 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001745
1746 // If value is passed via pointer - do a load.
1747 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001748 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1749 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001750
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001752 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001753
Dan Gohman61a92132008-04-21 23:59:07 +00001754 // The x86-64 ABI for returning structs by value requires that we copy
1755 // the sret argument into %rax for the return. Save the argument into
1756 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001757 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001758 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1759 unsigned Reg = FuncInfo->getSRetReturnReg();
1760 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001761 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001762 FuncInfo->setSRetReturnReg(Reg);
1763 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001764 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001765 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001766 }
1767
Chris Lattnerf39f7712007-02-28 05:46:49 +00001768 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001769 // Align stack specially for tail calls.
1770 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001771 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001772
Evan Cheng1bc78042006-04-26 01:20:17 +00001773 // If the function takes variable number of arguments, make a frame index for
1774 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001775 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001776 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1777 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001778 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001779 }
1780 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001781 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1782
1783 // FIXME: We should really autogenerate these arrays
1784 static const unsigned GPR64ArgRegsWin64[] = {
1785 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001786 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001787 static const unsigned GPR64ArgRegs64Bit[] = {
1788 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1789 };
1790 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1792 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1793 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001794 const unsigned *GPR64ArgRegs;
1795 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001796
1797 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001798 // The XMM registers which might contain var arg parameters are shadowed
1799 // in their paired GPR. So we only need to save the GPR to their home
1800 // slots.
1801 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001802 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001803 } else {
1804 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1805 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001806
1807 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808 }
1809 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1810 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001811
Devang Patel578efa92009-06-05 21:57:13 +00001812 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001813 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001814 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001815 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001816 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001817 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001818 // Kernel mode asks for SSE to be disabled, so don't push them
1819 // on the stack.
1820 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001821
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001822 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001823 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001824 // Get to the caller-allocated home save location. Add 8 to account
1825 // for the return address.
1826 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001827 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001828 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001829 // Fixup to set vararg frame on shadow area (4 x i64).
1830 if (NumIntRegs < 4)
1831 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001832 } else {
1833 // For X86-64, if there are vararg parameters that are passed via
1834 // registers, then we must store them to their spots on the stack so they
1835 // may be loaded by deferencing the result of va_next.
1836 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1837 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1838 FuncInfo->setRegSaveFrameIndex(
1839 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001840 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001841 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001842
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001845 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1846 getPointerTy());
1847 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001848 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001849 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1850 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001851 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001852 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001854 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001855 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001856 MachinePointerInfo::getFixedStack(
1857 FuncInfo->getRegSaveFrameIndex(), Offset),
1858 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001859 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001860 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001862
Dan Gohmanface41a2009-08-16 21:24:25 +00001863 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1864 // Now store the XMM (fp + vector) parameter registers.
1865 SmallVector<SDValue, 11> SaveXMMOps;
1866 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001867
Devang Patel68e6bee2011-02-21 23:21:26 +00001868 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001869 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1870 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001871
Dan Gohman1e93df62010-04-17 14:41:14 +00001872 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1873 FuncInfo->getRegSaveFrameIndex()));
1874 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1875 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001876
Dan Gohmanface41a2009-08-16 21:24:25 +00001877 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001878 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001879 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001880 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1881 SaveXMMOps.push_back(Val);
1882 }
1883 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1884 MVT::Other,
1885 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001886 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001887
1888 if (!MemOps.empty())
1889 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1890 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001892 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001893
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001895 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001896 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001897 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001898 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001899 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001900 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001901 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001902 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001903
Gordon Henriksen86737662008-01-05 16:56:59 +00001904 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001905 // RegSaveFrameIndex is X86-64 only.
1906 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001907 if (CallConv == CallingConv::X86_FastCall ||
1908 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001909 // fastcc functions can't have varargs.
1910 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001911 }
Evan Cheng25caf632006-05-23 21:06:34 +00001912
Dan Gohman98ca4f22009-08-05 01:29:28 +00001913 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001914}
1915
Dan Gohman475871a2008-07-27 21:46:04 +00001916SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001917X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1918 SDValue StackPtr, SDValue Arg,
1919 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001920 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001921 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001922 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001923 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001924 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001925 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001926 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001927
1928 return DAG.getStore(Chain, dl, Arg, PtrOff,
1929 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001930 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001931}
1932
Bill Wendling64e87322009-01-16 19:25:27 +00001933/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001934/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001935SDValue
1936X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001937 SDValue &OutRetAddr, SDValue Chain,
1938 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001939 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001940 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001941 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001942 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001943
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001944 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001945 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1946 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001947 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001948}
1949
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001950/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001951/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001952static SDValue
1953EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001955 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001956 // Store the return address to the appropriate stack slot.
1957 if (!FPDiff) return Chain;
1958 // Calculate the new stack slot for the return address.
1959 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001960 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001961 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001964 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001965 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001966 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001967 return Chain;
1968}
1969
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001971X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001972 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001973 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001975 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 const SmallVectorImpl<ISD::InputArg> &Ins,
1977 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001978 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 MachineFunction &MF = DAG.getMachineFunction();
1980 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001981 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001983 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984
Evan Cheng5f941932010-02-05 02:21:12 +00001985 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001986 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001987 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1988 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001989 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001990
1991 // Sibcalls are automatically detected tailcalls which do not require
1992 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001993 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001994 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001995
1996 if (isTailCall)
1997 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001998 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001999
Chris Lattner29689432010-03-11 00:22:57 +00002000 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2001 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002002
Chris Lattner638402b2007-02-28 07:00:42 +00002003 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002004 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2006 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002007
2008 // Allocate shadow area for Win64
2009 if (IsWin64) {
2010 CCInfo.AllocateStack(32, 8);
2011 }
2012
Duncan Sands45907662010-10-31 13:21:44 +00002013 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002014
Chris Lattner423c5f42007-02-28 05:31:48 +00002015 // Get a count of how many bytes are to be pushed on the stack.
2016 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002017 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002018 // This is a sibcall. The memory operands are available in caller's
2019 // own caller's stack.
2020 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002021 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002022 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002023
Gordon Henriksen86737662008-01-05 16:56:59 +00002024 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002025 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002027 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002028 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2029 FPDiff = NumBytesCallerPushed - NumBytes;
2030
2031 // Set the delta of movement of the returnaddr stackslot.
2032 // But only set if delta is greater than previous delta.
2033 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2034 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2035 }
2036
Evan Chengf22f9b32010-02-06 03:28:46 +00002037 if (!IsSibcall)
2038 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002039
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002041 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002042 if (isTailCall && FPDiff)
2043 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2044 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002045
Dan Gohman475871a2008-07-27 21:46:04 +00002046 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2047 SmallVector<SDValue, 8> MemOpChains;
2048 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002049
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002050 // Walk the register/memloc assignments, inserting copies/loads. In the case
2051 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002052 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2053 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002054 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002055 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002056 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002057 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002058
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 // Promote the value if needed.
2060 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002061 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002062 case CCValAssign::Full: break;
2063 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002064 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002065 break;
2066 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002067 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002068 break;
2069 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002070 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2071 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002072 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2074 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002075 } else
2076 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2077 break;
2078 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002079 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002080 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002081 case CCValAssign::Indirect: {
2082 // Store the argument.
2083 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002084 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002085 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002086 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002087 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002088 Arg = SpillSlot;
2089 break;
2090 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Chris Lattner423c5f42007-02-28 05:31:48 +00002093 if (VA.isRegLoc()) {
Stuart Hastings504421e2011-05-17 16:45:55 +00002094 if (isByVal && (!IsSibcall && !isTailCall)) {
2095 // 64-bit only. x86_32 passes everything on the stack.
2096 assert(CCInfo.isFirstByValRegValid() && "isByVal, but no valid register assigned!");
2097 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2098 unsigned int i, j;
2099 for (i = 0, j = CCInfo.getFirstByValReg(); j <= X86::R9; i++, j++) {
2100 SDValue Const = DAG.getConstant(8*i, MVT::i64);
2101 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
2102 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
2103 MachinePointerInfo(),
2104 false, false, 0);
2105 MemOpChains.push_back(Load.getValue(1));
2106 RegsToPass.push_back(std::make_pair(j, Load));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002107 }
Stuart Hastings504421e2011-05-17 16:45:55 +00002108 CCInfo.clearFirstByValReg();
2109 } else {
2110 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2111 if (isVarArg && IsWin64) {
2112 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2113 // shadow reg if callee is a varargs function.
2114 unsigned ShadowReg = 0;
2115 switch (VA.getLocReg()) {
2116 case X86::XMM0: ShadowReg = X86::RCX; break;
2117 case X86::XMM1: ShadowReg = X86::RDX; break;
2118 case X86::XMM2: ShadowReg = X86::R8; break;
2119 case X86::XMM3: ShadowReg = X86::R9; break;
2120 }
2121 if (ShadowReg)
2122 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2123 }
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002124 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002125 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002126 assert(VA.isMemLoc());
2127 if (StackPtr.getNode() == 0)
2128 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2129 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2130 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002131 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Evan Cheng32fe1032006-05-25 00:59:30 +00002134 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002136 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002137
Evan Cheng347d5f72006-04-28 21:29:37 +00002138 // Build a sequence of copy-to-reg nodes chained together with token chain
2139 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002141 // Tail call byval lowering might overwrite argument registers so in case of
2142 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002144 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002145 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002146 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002147 InFlag = Chain.getValue(1);
2148 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002149
Chris Lattner88e1fd52009-07-09 04:24:46 +00002150 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002151 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2152 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002153 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002154 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2155 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002156 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002157 InFlag);
2158 InFlag = Chain.getValue(1);
2159 } else {
2160 // If we are tail calling and generating PIC/GOT style code load the
2161 // address of the callee into ECX. The value in ecx is used as target of
2162 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2163 // for tail calls on PIC/GOT architectures. Normally we would just put the
2164 // address of GOT into ebx and then call target@PLT. But for tail calls
2165 // ebx would be restored (since ebx is callee saved) before jumping to the
2166 // target@PLT.
2167
2168 // Note: The actual moving to ECX is done further down.
2169 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2170 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2171 !G->getGlobal()->hasProtectedVisibility())
2172 Callee = LowerGlobalAddress(Callee, DAG);
2173 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002174 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002175 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002176 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002177
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002178 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002179 // From AMD64 ABI document:
2180 // For calls that may call functions that use varargs or stdargs
2181 // (prototype-less calls or calls to functions containing ellipsis (...) in
2182 // the declaration) %al is used as hidden argument to specify the number
2183 // of SSE registers used. The contents of %al do not need to match exactly
2184 // the number of registers, but must be an ubound on the number of SSE
2185 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002186
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 // Count the number of XMM registers allocated.
2188 static const unsigned XMMArgRegs[] = {
2189 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2190 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2191 };
2192 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002193 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002194 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002195
Dale Johannesendd64c412009-02-04 00:33:20 +00002196 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002197 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002198 InFlag = Chain.getValue(1);
2199 }
2200
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002201
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002202 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 if (isTailCall) {
2204 // Force all the incoming stack arguments to be loaded from the stack
2205 // before any new outgoing arguments are stored to the stack, because the
2206 // outgoing stack slots may alias the incoming argument stack slots, and
2207 // the alias isn't otherwise explicit. This is slightly more conservative
2208 // than necessary, because it means that each store effectively depends
2209 // on every argument instead of just those arguments it would clobber.
2210 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2211
Dan Gohman475871a2008-07-27 21:46:04 +00002212 SmallVector<SDValue, 8> MemOpChains2;
2213 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002214 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002215 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002216 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002217 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002218 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2219 CCValAssign &VA = ArgLocs[i];
2220 if (VA.isRegLoc())
2221 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002222 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002223 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002225 // Create frame index.
2226 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002227 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002228 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002229 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002230
Duncan Sands276dcbd2008-03-21 09:14:45 +00002231 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002232 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002233 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002234 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002236 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002237 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2240 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002241 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002243 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002244 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002245 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002246 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002247 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002248 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002249 }
2250 }
2251
2252 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002253 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002254 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002255
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002256 // Copy arguments to their registers.
2257 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002259 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002260 InFlag = Chain.getValue(1);
2261 }
Dan Gohman475871a2008-07-27 21:46:04 +00002262 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002263
Gordon Henriksen86737662008-01-05 16:56:59 +00002264 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002265 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002266 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002267 }
2268
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002269 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2270 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2271 // In the 64-bit large code model, we have to make all calls
2272 // through a register, since the call instruction's 32-bit
2273 // pc-relative offset may not be large enough to hold the whole
2274 // address.
2275 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002276 // If the callee is a GlobalAddress node (quite common, every direct call
2277 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2278 // it.
2279
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002280 // We should use extra load for direct calls to dllimported functions in
2281 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002282 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002283 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002284 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002285
Chris Lattner48a7d022009-07-09 05:02:21 +00002286 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2287 // external symbols most go through the PLT in PIC mode. If the symbol
2288 // has hidden or protected visibility, or if it is static or local, then
2289 // we don't need to use the PLT - we can directly call it.
2290 if (Subtarget->isTargetELF() &&
2291 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002292 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002293 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002294 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002295 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002296 (!Subtarget->getTargetTriple().isMacOSX() ||
2297 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002298 // PC-relative references to external symbols should go through $stub,
2299 // unless we're building with the leopard linker or later, which
2300 // automatically synthesizes these stubs.
2301 OpFlags = X86II::MO_DARWIN_STUB;
2302 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002303
Devang Patel0d881da2010-07-06 22:08:15 +00002304 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002305 G->getOffset(), OpFlags);
2306 }
Bill Wendling056292f2008-09-16 21:48:12 +00002307 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002308 unsigned char OpFlags = 0;
2309
Evan Cheng1bf891a2010-12-01 22:59:46 +00002310 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2311 // external symbols should go through the PLT.
2312 if (Subtarget->isTargetELF() &&
2313 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2314 OpFlags = X86II::MO_PLT;
2315 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002316 (!Subtarget->getTargetTriple().isMacOSX() ||
2317 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002318 // PC-relative references to external symbols should go through $stub,
2319 // unless we're building with the leopard linker or later, which
2320 // automatically synthesizes these stubs.
2321 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002322 }
Eric Christopherfd179292009-08-27 18:07:15 +00002323
Chris Lattner48a7d022009-07-09 05:02:21 +00002324 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2325 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002326 }
2327
Chris Lattnerd96d0722007-02-25 06:40:16 +00002328 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002329 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002330 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002331
Evan Chengf22f9b32010-02-06 03:28:46 +00002332 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002333 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2334 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002335 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002337
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002338 Ops.push_back(Chain);
2339 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002340
Dan Gohman98ca4f22009-08-05 01:29:28 +00002341 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002343
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 // Add argument registers to the end of the list so that they are known live
2345 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002346 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2347 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2348 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002349
Evan Cheng586ccac2008-03-18 23:36:35 +00002350 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002351 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002352 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2353
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002354 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002355 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002357
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002359 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002360
Dan Gohman98ca4f22009-08-05 01:29:28 +00002361 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002362 // We used to do:
2363 //// If this is the first return lowered for this function, add the regs
2364 //// to the liveout set for the function.
2365 // This isn't right, although it's probably harmless on x86; liveouts
2366 // should be computed from returns not tail calls. Consider a void
2367 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 return DAG.getNode(X86ISD::TC_RETURN, dl,
2369 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002370 }
2371
Dale Johannesenace16102009-02-03 19:33:06 +00002372 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002373 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002374
Chris Lattner2d297092006-05-23 18:50:38 +00002375 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002376 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002377 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002379 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002380 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002381 // pops the hidden struct pointer, so we have to push it back.
2382 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002383 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002384 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002385 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002386
Gordon Henriksenae636f82008-01-03 16:47:34 +00002387 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002388 if (!IsSibcall) {
2389 Chain = DAG.getCALLSEQ_END(Chain,
2390 DAG.getIntPtrConstant(NumBytes, true),
2391 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2392 true),
2393 InFlag);
2394 InFlag = Chain.getValue(1);
2395 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002396
Chris Lattner3085e152007-02-25 08:59:22 +00002397 // Handle result values, copying them out of physregs into vregs that we
2398 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002399 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2400 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002401}
2402
Evan Cheng25ab6902006-09-08 06:48:29 +00002403
2404//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002405// Fast Calling Convention (tail call) implementation
2406//===----------------------------------------------------------------------===//
2407
2408// Like std call, callee cleans arguments, convention except that ECX is
2409// reserved for storing the tail called function address. Only 2 registers are
2410// free for argument passing (inreg). Tail call optimization is performed
2411// provided:
2412// * tailcallopt is enabled
2413// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002414// On X86_64 architecture with GOT-style position independent code only local
2415// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002416// To keep the stack aligned according to platform abi the function
2417// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2418// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002419// If a tail called function callee has more arguments than the caller the
2420// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002421// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002422// original REtADDR, but before the saved framepointer or the spilled registers
2423// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2424// stack layout:
2425// arg1
2426// arg2
2427// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002428// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002429// move area ]
2430// (possible EBP)
2431// ESI
2432// EDI
2433// local1 ..
2434
2435/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2436/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002437unsigned
2438X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2439 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002440 MachineFunction &MF = DAG.getMachineFunction();
2441 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002442 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002443 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002444 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002445 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002446 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002447 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2448 // Number smaller than 12 so just add the difference.
2449 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2450 } else {
2451 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002452 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002453 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002454 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002455 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002456}
2457
Stuart Hastings504421e2011-05-17 16:45:55 +00002458/// HandleByVal - Every parameter *after* a byval parameter is passed
2459/// on the stack. Remember the next parameter register to allocate,
2460/// and then confiscate the rest of the parameter registers to insure
2461/// this.
2462void
2463X86TargetLowering::HandleByVal(CCState *State, unsigned &size) const {
2464 // X86_32 passes all parameters on the stack, byval or whatever.
2465 // X86_64 does not split parameters between registers and memory; if
2466 // the parameter does not fit entirely inside the remaining
2467 // parameter registers, it goes on the stack.
2468 static const unsigned RegList2[] = {
2469 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2470 };
2471
2472 if (!Subtarget->is64Bit())
2473 return;
2474
2475 if (size > 16) // X86_64 aggregates > 16 bytes are passed in memory.
2476 return;
2477
2478 unsigned reg = State->getFirstUnallocated(RegList2, 6);
2479
2480 if (reg == 6) // Out of regs to allocate.
2481 return;
2482
2483 // We expect the size to be 32 bits, or some non-zero multiple of 64 bits.
2484 unsigned nregs = size / 8;
2485 if (nregs == 0) nregs=1; // 32-bit case.
2486
2487 unsigned regs_available = 6 - reg;
2488
2489 if (nregs <= regs_available) {
2490 size = 0;
2491 State->setFirstByValReg(RegList2[reg]);
2492 while (nregs--) {
2493 State->AllocateReg(RegList2[reg]);
2494 reg++;
2495 }
2496 }
2497}
2498
Evan Cheng5f941932010-02-05 02:21:12 +00002499/// MatchingStackOffset - Return true if the given stack call argument is
2500/// already available in the same position (relatively) of the caller's
2501/// incoming argument stack.
2502static
2503bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2504 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2505 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002506 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2507 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002508 if (Arg.getOpcode() == ISD::CopyFromReg) {
2509 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002510 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002511 return false;
2512 MachineInstr *Def = MRI->getVRegDef(VR);
2513 if (!Def)
2514 return false;
2515 if (!Flags.isByVal()) {
2516 if (!TII->isLoadFromStackSlot(Def, FI))
2517 return false;
2518 } else {
2519 unsigned Opcode = Def->getOpcode();
2520 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2521 Def->getOperand(1).isFI()) {
2522 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002523 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002524 } else
2525 return false;
2526 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002527 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2528 if (Flags.isByVal())
2529 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002530 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002531 // define @foo(%struct.X* %A) {
2532 // tail call @bar(%struct.X* byval %A)
2533 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002534 return false;
2535 SDValue Ptr = Ld->getBasePtr();
2536 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2537 if (!FINode)
2538 return false;
2539 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002540 } else
2541 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002542
Evan Cheng4cae1332010-03-05 08:38:04 +00002543 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002544 if (!MFI->isFixedObjectIndex(FI))
2545 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002546 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002547}
2548
Dan Gohman98ca4f22009-08-05 01:29:28 +00002549/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2550/// for tail call optimization. Targets which want to do tail call
2551/// optimization should implement this function.
2552bool
2553X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002554 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002556 bool isCalleeStructRet,
2557 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002558 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002559 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002560 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002561 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002562 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002563 CalleeCC != CallingConv::C)
2564 return false;
2565
Evan Cheng7096ae42010-01-29 06:45:59 +00002566 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002567 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002568 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002569 CallingConv::ID CallerCC = CallerF->getCallingConv();
2570 bool CCMatch = CallerCC == CalleeCC;
2571
Dan Gohman1797ed52010-02-08 20:27:50 +00002572 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002573 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002574 return true;
2575 return false;
2576 }
2577
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002578 // Look for obvious safe cases to perform tail call optimization that do not
2579 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002580
Evan Cheng2c12cb42010-03-26 16:26:03 +00002581 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2582 // emit a special epilogue.
2583 if (RegInfo->needsStackRealignment(MF))
2584 return false;
2585
Eric Christopher90eb4022010-07-22 00:26:08 +00002586 // Do not sibcall optimize vararg calls unless the call site is not passing
2587 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002588 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002589 return false;
2590
Evan Chenga375d472010-03-15 18:54:48 +00002591 // Also avoid sibcall optimization if either caller or callee uses struct
2592 // return semantics.
2593 if (isCalleeStructRet || isCallerStructRet)
2594 return false;
2595
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002596 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2597 // Therefore if it's not used by the call it is not safe to optimize this into
2598 // a sibcall.
2599 bool Unused = false;
2600 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2601 if (!Ins[i].Used) {
2602 Unused = true;
2603 break;
2604 }
2605 }
2606 if (Unused) {
2607 SmallVector<CCValAssign, 16> RVLocs;
2608 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2609 RVLocs, *DAG.getContext());
2610 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002611 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002612 CCValAssign &VA = RVLocs[i];
2613 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2614 return false;
2615 }
2616 }
2617
Evan Cheng13617962010-04-30 01:12:32 +00002618 // If the calling conventions do not match, then we'd better make sure the
2619 // results are returned in the same way as what the caller expects.
2620 if (!CCMatch) {
2621 SmallVector<CCValAssign, 16> RVLocs1;
2622 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2623 RVLocs1, *DAG.getContext());
2624 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2625
2626 SmallVector<CCValAssign, 16> RVLocs2;
2627 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2628 RVLocs2, *DAG.getContext());
2629 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2630
2631 if (RVLocs1.size() != RVLocs2.size())
2632 return false;
2633 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2634 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2635 return false;
2636 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2637 return false;
2638 if (RVLocs1[i].isRegLoc()) {
2639 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2640 return false;
2641 } else {
2642 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2643 return false;
2644 }
2645 }
2646 }
2647
Evan Chenga6bff982010-01-30 01:22:00 +00002648 // If the callee takes no arguments then go on to check the results of the
2649 // call.
2650 if (!Outs.empty()) {
2651 // Check if stack adjustment is needed. For now, do not do this if any
2652 // argument is passed on the stack.
2653 SmallVector<CCValAssign, 16> ArgLocs;
2654 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2655 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002656
2657 // Allocate shadow area for Win64
2658 if (Subtarget->isTargetWin64()) {
2659 CCInfo.AllocateStack(32, 8);
2660 }
2661
Duncan Sands45907662010-10-31 13:21:44 +00002662 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings504421e2011-05-17 16:45:55 +00002663 if (ArgLocs.size()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002664 MachineFunction &MF = DAG.getMachineFunction();
2665 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2666 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002667
2668 // Check if the arguments are already laid out in the right way as
2669 // the caller's fixed stack objects.
2670 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002671 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2672 const X86InstrInfo *TII =
2673 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002674 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2675 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002676 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002677 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002678 if (VA.getLocInfo() == CCValAssign::Indirect)
2679 return false;
Stuart Hastings504421e2011-05-17 16:45:55 +00002680 if (Flags.isByVal())
2681 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002682 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002683 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2684 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002685 return false;
2686 }
2687 }
2688 }
Evan Cheng9c044672010-05-29 01:35:22 +00002689
2690 // If the tailcall address may be in a register, then make sure it's
2691 // possible to register allocate for it. In 32-bit, the call address can
2692 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002693 // callee-saved registers are restored. These happen to be the same
2694 // registers used to pass 'inreg' arguments so watch out for those.
2695 if (!Subtarget->is64Bit() &&
2696 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002697 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002698 unsigned NumInRegs = 0;
2699 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2700 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002701 if (!VA.isRegLoc())
2702 continue;
2703 unsigned Reg = VA.getLocReg();
2704 switch (Reg) {
2705 default: break;
2706 case X86::EAX: case X86::EDX: case X86::ECX:
2707 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002708 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002709 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002710 }
2711 }
2712 }
Evan Chenga6bff982010-01-30 01:22:00 +00002713 }
Evan Chengb1712452010-01-27 06:25:16 +00002714
Dale Johannesend155d7e2010-10-25 22:17:05 +00002715 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002716 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002717 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2718 return false;
2719
Evan Cheng86809cc2010-02-03 03:28:02 +00002720 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002721}
2722
Dan Gohman3df24e62008-09-03 23:12:08 +00002723FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002724X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2725 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002726}
2727
2728
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002729//===----------------------------------------------------------------------===//
2730// Other Lowering Hooks
2731//===----------------------------------------------------------------------===//
2732
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002733static bool MayFoldLoad(SDValue Op) {
2734 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2735}
2736
2737static bool MayFoldIntoStore(SDValue Op) {
2738 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2739}
2740
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002741static bool isTargetShuffle(unsigned Opcode) {
2742 switch(Opcode) {
2743 default: return false;
2744 case X86ISD::PSHUFD:
2745 case X86ISD::PSHUFHW:
2746 case X86ISD::PSHUFLW:
2747 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002748 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002749 case X86ISD::SHUFPS:
2750 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002751 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002752 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002753 case X86ISD::MOVLPS:
2754 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002755 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002756 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002757 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002758 case X86ISD::MOVSS:
2759 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002760 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002761 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002762 case X86ISD::VUNPCKLPS:
2763 case X86ISD::VUNPCKLPD:
2764 case X86ISD::VUNPCKLPSY:
2765 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002766 case X86ISD::PUNPCKLWD:
2767 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002768 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002769 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002770 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002771 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002772 case X86ISD::PUNPCKHWD:
2773 case X86ISD::PUNPCKHBW:
2774 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002775 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002776 return true;
2777 }
2778 return false;
2779}
2780
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002781static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002782 SDValue V1, SelectionDAG &DAG) {
2783 switch(Opc) {
2784 default: llvm_unreachable("Unknown x86 shuffle node");
2785 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002786 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002787 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002788 return DAG.getNode(Opc, dl, VT, V1);
2789 }
2790
2791 return SDValue();
2792}
2793
2794static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002795 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002796 switch(Opc) {
2797 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002798 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002799 case X86ISD::PSHUFHW:
2800 case X86ISD::PSHUFLW:
2801 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2802 }
2803
2804 return SDValue();
2805}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002806
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002807static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2808 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2809 switch(Opc) {
2810 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002811 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002812 case X86ISD::SHUFPD:
2813 case X86ISD::SHUFPS:
2814 return DAG.getNode(Opc, dl, VT, V1, V2,
2815 DAG.getConstant(TargetMask, MVT::i8));
2816 }
2817 return SDValue();
2818}
2819
2820static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2821 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2822 switch(Opc) {
2823 default: llvm_unreachable("Unknown x86 shuffle node");
2824 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002825 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002826 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002827 case X86ISD::MOVLPS:
2828 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002829 case X86ISD::MOVSS:
2830 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002831 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002832 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002833 case X86ISD::VUNPCKLPS:
2834 case X86ISD::VUNPCKLPD:
2835 case X86ISD::VUNPCKLPSY:
2836 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002837 case X86ISD::PUNPCKLWD:
2838 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002839 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002840 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002841 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002842 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002843 case X86ISD::PUNPCKHWD:
2844 case X86ISD::PUNPCKHBW:
2845 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002846 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002847 return DAG.getNode(Opc, dl, VT, V1, V2);
2848 }
2849 return SDValue();
2850}
2851
Dan Gohmand858e902010-04-17 15:26:15 +00002852SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002853 MachineFunction &MF = DAG.getMachineFunction();
2854 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2855 int ReturnAddrIndex = FuncInfo->getRAIndex();
2856
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002857 if (ReturnAddrIndex == 0) {
2858 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002859 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002860 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002861 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002862 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002863 }
2864
Evan Cheng25ab6902006-09-08 06:48:29 +00002865 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002866}
2867
2868
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002869bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2870 bool hasSymbolicDisplacement) {
2871 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002872 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002873 return false;
2874
2875 // If we don't have a symbolic displacement - we don't have any extra
2876 // restrictions.
2877 if (!hasSymbolicDisplacement)
2878 return true;
2879
2880 // FIXME: Some tweaks might be needed for medium code model.
2881 if (M != CodeModel::Small && M != CodeModel::Kernel)
2882 return false;
2883
2884 // For small code model we assume that latest object is 16MB before end of 31
2885 // bits boundary. We may also accept pretty large negative constants knowing
2886 // that all objects are in the positive half of address space.
2887 if (M == CodeModel::Small && Offset < 16*1024*1024)
2888 return true;
2889
2890 // For kernel code model we know that all object resist in the negative half
2891 // of 32bits address space. We may not accept negative offsets, since they may
2892 // be just off and we may accept pretty large positive ones.
2893 if (M == CodeModel::Kernel && Offset > 0)
2894 return true;
2895
2896 return false;
2897}
2898
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002899/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2900/// specific condition code, returning the condition code and the LHS/RHS of the
2901/// comparison to make.
2902static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2903 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002904 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002905 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2906 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2907 // X > -1 -> X == 0, jump !sign.
2908 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002909 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002910 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2911 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002912 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002913 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002914 // X < 1 -> X <= 0
2915 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002916 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002917 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002918 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002919
Evan Chengd9558e02006-01-06 00:43:03 +00002920 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002921 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002922 case ISD::SETEQ: return X86::COND_E;
2923 case ISD::SETGT: return X86::COND_G;
2924 case ISD::SETGE: return X86::COND_GE;
2925 case ISD::SETLT: return X86::COND_L;
2926 case ISD::SETLE: return X86::COND_LE;
2927 case ISD::SETNE: return X86::COND_NE;
2928 case ISD::SETULT: return X86::COND_B;
2929 case ISD::SETUGT: return X86::COND_A;
2930 case ISD::SETULE: return X86::COND_BE;
2931 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002932 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002934
Chris Lattner4c78e022008-12-23 23:42:27 +00002935 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002936
Chris Lattner4c78e022008-12-23 23:42:27 +00002937 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002938 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2939 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002940 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2941 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002942 }
2943
Chris Lattner4c78e022008-12-23 23:42:27 +00002944 switch (SetCCOpcode) {
2945 default: break;
2946 case ISD::SETOLT:
2947 case ISD::SETOLE:
2948 case ISD::SETUGT:
2949 case ISD::SETUGE:
2950 std::swap(LHS, RHS);
2951 break;
2952 }
2953
2954 // On a floating point condition, the flags are set as follows:
2955 // ZF PF CF op
2956 // 0 | 0 | 0 | X > Y
2957 // 0 | 0 | 1 | X < Y
2958 // 1 | 0 | 0 | X == Y
2959 // 1 | 1 | 1 | unordered
2960 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002961 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002962 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002963 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002964 case ISD::SETOLT: // flipped
2965 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002966 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002967 case ISD::SETOLE: // flipped
2968 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002969 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002970 case ISD::SETUGT: // flipped
2971 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002972 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002973 case ISD::SETUGE: // flipped
2974 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002975 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002976 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002977 case ISD::SETNE: return X86::COND_NE;
2978 case ISD::SETUO: return X86::COND_P;
2979 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002980 case ISD::SETOEQ:
2981 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002982 }
Evan Chengd9558e02006-01-06 00:43:03 +00002983}
2984
Evan Cheng4a460802006-01-11 00:33:36 +00002985/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2986/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002987/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002988static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002989 switch (X86CC) {
2990 default:
2991 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002992 case X86::COND_B:
2993 case X86::COND_BE:
2994 case X86::COND_E:
2995 case X86::COND_P:
2996 case X86::COND_A:
2997 case X86::COND_AE:
2998 case X86::COND_NE:
2999 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003000 return true;
3001 }
3002}
3003
Evan Chengeb2f9692009-10-27 19:56:55 +00003004/// isFPImmLegal - Returns true if the target can instruction select the
3005/// specified FP immediate natively. If false, the legalizer will
3006/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003007bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003008 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3009 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3010 return true;
3011 }
3012 return false;
3013}
3014
Nate Begeman9008ca62009-04-27 18:41:29 +00003015/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3016/// the specified range (L, H].
3017static bool isUndefOrInRange(int Val, int Low, int Hi) {
3018 return (Val < 0) || (Val >= Low && Val < Hi);
3019}
3020
3021/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3022/// specified value.
3023static bool isUndefOrEqual(int Val, int CmpVal) {
3024 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003027}
3028
Nate Begeman9008ca62009-04-27 18:41:29 +00003029/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3030/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3031/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003032static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003033 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 return (Mask[0] < 2 && Mask[1] < 2);
3037 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038}
3039
Nate Begeman9008ca62009-04-27 18:41:29 +00003040bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003041 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 N->getMask(M);
3043 return ::isPSHUFDMask(M, N->getValueType(0));
3044}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003045
Nate Begeman9008ca62009-04-27 18:41:29 +00003046/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3047/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003048static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003050 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003051
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 // Lower quadword copied in order or undef.
3053 for (int i = 0; i != 4; ++i)
3054 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003055 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003056
Evan Cheng506d3df2006-03-29 23:07:14 +00003057 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 for (int i = 4; i != 8; ++i)
3059 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003060 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Evan Cheng506d3df2006-03-29 23:07:14 +00003062 return true;
3063}
3064
Nate Begeman9008ca62009-04-27 18:41:29 +00003065bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003066 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003067 N->getMask(M);
3068 return ::isPSHUFHWMask(M, N->getValueType(0));
3069}
Evan Cheng506d3df2006-03-29 23:07:14 +00003070
Nate Begeman9008ca62009-04-27 18:41:29 +00003071/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3072/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003073static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Rafael Espindola15684b22009-04-24 12:40:33 +00003077 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 for (int i = 4; i != 8; ++i)
3079 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003080 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003081
Rafael Espindola15684b22009-04-24 12:40:33 +00003082 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 for (int i = 0; i != 4; ++i)
3084 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003085 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003086
Rafael Espindola15684b22009-04-24 12:40:33 +00003087 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003088}
3089
Nate Begeman9008ca62009-04-27 18:41:29 +00003090bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003091 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003092 N->getMask(M);
3093 return ::isPSHUFLWMask(M, N->getValueType(0));
3094}
3095
Nate Begemana09008b2009-10-19 02:17:23 +00003096/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3097/// is suitable for input to PALIGNR.
3098static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3099 bool hasSSSE3) {
3100 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003101
Nate Begemana09008b2009-10-19 02:17:23 +00003102 // Do not handle v2i64 / v2f64 shuffles with palignr.
3103 if (e < 4 || !hasSSSE3)
3104 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003105
Nate Begemana09008b2009-10-19 02:17:23 +00003106 for (i = 0; i != e; ++i)
3107 if (Mask[i] >= 0)
3108 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003109
Nate Begemana09008b2009-10-19 02:17:23 +00003110 // All undef, not a palignr.
3111 if (i == e)
3112 return false;
3113
3114 // Determine if it's ok to perform a palignr with only the LHS, since we
3115 // don't have access to the actual shuffle elements to see if RHS is undef.
3116 bool Unary = Mask[i] < (int)e;
3117 bool NeedsUnary = false;
3118
3119 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003120
Nate Begemana09008b2009-10-19 02:17:23 +00003121 // Check the rest of the elements to see if they are consecutive.
3122 for (++i; i != e; ++i) {
3123 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003124 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003125 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003126
Nate Begemana09008b2009-10-19 02:17:23 +00003127 Unary = Unary && (m < (int)e);
3128 NeedsUnary = NeedsUnary || (m < s);
3129
3130 if (NeedsUnary && !Unary)
3131 return false;
3132 if (Unary && m != ((s+i) & (e-1)))
3133 return false;
3134 if (!Unary && m != (s+i))
3135 return false;
3136 }
3137 return true;
3138}
3139
3140bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3141 SmallVector<int, 8> M;
3142 N->getMask(M);
3143 return ::isPALIGNRMask(M, N->getValueType(0), true);
3144}
3145
Evan Cheng14aed5e2006-03-24 01:18:28 +00003146/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3147/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003148static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 int NumElems = VT.getVectorNumElements();
3150 if (NumElems != 2 && NumElems != 4)
3151 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 int Half = NumElems / 2;
3154 for (int i = 0; i < Half; ++i)
3155 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003156 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 for (int i = Half; i < NumElems; ++i)
3158 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Evan Cheng14aed5e2006-03-24 01:18:28 +00003161 return true;
3162}
3163
Nate Begeman9008ca62009-04-27 18:41:29 +00003164bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3165 SmallVector<int, 8> M;
3166 N->getMask(M);
3167 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003168}
3169
Evan Cheng213d2cf2007-05-17 18:45:50 +00003170/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003171/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3172/// half elements to come from vector 1 (which would equal the dest.) and
3173/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003174static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003176
3177 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 int Half = NumElems / 2;
3181 for (int i = 0; i < Half; ++i)
3182 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003183 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 for (int i = Half; i < NumElems; ++i)
3185 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003186 return false;
3187 return true;
3188}
3189
Nate Begeman9008ca62009-04-27 18:41:29 +00003190static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3191 SmallVector<int, 8> M;
3192 N->getMask(M);
3193 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003194}
3195
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003196/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3197/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003198bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3199 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003200 return false;
3201
Evan Cheng2064a2b2006-03-28 06:50:32 +00003202 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3204 isUndefOrEqual(N->getMaskElt(1), 7) &&
3205 isUndefOrEqual(N->getMaskElt(2), 2) &&
3206 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003207}
3208
Nate Begeman0b10b912009-11-07 23:17:15 +00003209/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3210/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3211/// <2, 3, 2, 3>
3212bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3213 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003214
Nate Begeman0b10b912009-11-07 23:17:15 +00003215 if (NumElems != 4)
3216 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003217
Nate Begeman0b10b912009-11-07 23:17:15 +00003218 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3219 isUndefOrEqual(N->getMaskElt(1), 3) &&
3220 isUndefOrEqual(N->getMaskElt(2), 2) &&
3221 isUndefOrEqual(N->getMaskElt(3), 3);
3222}
3223
Evan Cheng5ced1d82006-04-06 23:23:56 +00003224/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3225/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003226bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3227 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003228
Evan Cheng5ced1d82006-04-06 23:23:56 +00003229 if (NumElems != 2 && NumElems != 4)
3230 return false;
3231
Evan Chengc5cdff22006-04-07 21:53:05 +00003232 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003234 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003235
Evan Chengc5cdff22006-04-07 21:53:05 +00003236 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003238 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003239
3240 return true;
3241}
3242
Nate Begeman0b10b912009-11-07 23:17:15 +00003243/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3244/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3245bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003247
David Greenea20244d2011-03-02 17:23:43 +00003248 if ((NumElems != 2 && NumElems != 4)
3249 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250 return false;
3251
Evan Chengc5cdff22006-04-07 21:53:05 +00003252 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003254 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 for (unsigned i = 0; i < NumElems/2; ++i)
3257 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003258 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003259
3260 return true;
3261}
3262
Evan Cheng0038e592006-03-28 00:39:58 +00003263/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3264/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003265static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003266 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003268 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003269 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003270
David Greenea20244d2011-03-02 17:23:43 +00003271 // Handle vector lengths > 128 bits. Define a "section" as a set of
3272 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3273 // sections.
3274 unsigned NumSections = VT.getSizeInBits() / 128;
3275 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3276 unsigned NumSectionElts = NumElts / NumSections;
3277
3278 unsigned Start = 0;
3279 unsigned End = NumSectionElts;
3280 for (unsigned s = 0; s < NumSections; ++s) {
3281 for (unsigned i = Start, j = s * NumSectionElts;
3282 i != End;
3283 i += 2, ++j) {
3284 int BitI = Mask[i];
3285 int BitI1 = Mask[i+1];
3286 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003287 return false;
David Greenea20244d2011-03-02 17:23:43 +00003288 if (V2IsSplat) {
3289 if (!isUndefOrEqual(BitI1, NumElts))
3290 return false;
3291 } else {
3292 if (!isUndefOrEqual(BitI1, j + NumElts))
3293 return false;
3294 }
Evan Cheng39623da2006-04-20 08:58:49 +00003295 }
David Greenea20244d2011-03-02 17:23:43 +00003296 // Process the next 128 bits.
3297 Start += NumSectionElts;
3298 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003299 }
David Greenea20244d2011-03-02 17:23:43 +00003300
Evan Cheng0038e592006-03-28 00:39:58 +00003301 return true;
3302}
3303
Nate Begeman9008ca62009-04-27 18:41:29 +00003304bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3305 SmallVector<int, 8> M;
3306 N->getMask(M);
3307 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003308}
3309
Evan Cheng4fcb9222006-03-28 02:43:26 +00003310/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3311/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003312static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003313 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003315 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003316 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003317
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3319 int BitI = Mask[i];
3320 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003321 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003322 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003323 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003324 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003325 return false;
3326 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003327 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003328 return false;
3329 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003330 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003331 return true;
3332}
3333
Nate Begeman9008ca62009-04-27 18:41:29 +00003334bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3335 SmallVector<int, 8> M;
3336 N->getMask(M);
3337 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003338}
3339
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003340/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3341/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3342/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003343static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003345 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003346 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003347
David Greenea20244d2011-03-02 17:23:43 +00003348 // Handle vector lengths > 128 bits. Define a "section" as a set of
3349 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3350 // sections.
3351 unsigned NumSections = VT.getSizeInBits() / 128;
3352 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3353 unsigned NumSectionElts = NumElems / NumSections;
3354
3355 for (unsigned s = 0; s < NumSections; ++s) {
3356 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3357 i != NumSectionElts * (s + 1);
3358 i += 2, ++j) {
3359 int BitI = Mask[i];
3360 int BitI1 = Mask[i+1];
3361
3362 if (!isUndefOrEqual(BitI, j))
3363 return false;
3364 if (!isUndefOrEqual(BitI1, j))
3365 return false;
3366 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003367 }
David Greenea20244d2011-03-02 17:23:43 +00003368
Rafael Espindola15684b22009-04-24 12:40:33 +00003369 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3373 SmallVector<int, 8> M;
3374 N->getMask(M);
3375 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3376}
3377
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003378/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3379/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3380/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003381static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003383 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3384 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003385
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3387 int BitI = Mask[i];
3388 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003389 if (!isUndefOrEqual(BitI, j))
3390 return false;
3391 if (!isUndefOrEqual(BitI1, j))
3392 return false;
3393 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003394 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003395}
3396
Nate Begeman9008ca62009-04-27 18:41:29 +00003397bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3398 SmallVector<int, 8> M;
3399 N->getMask(M);
3400 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3401}
3402
Evan Cheng017dcc62006-04-21 01:05:10 +00003403/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3404/// specifies a shuffle of elements that is suitable for input to MOVSS,
3405/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003406static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003407 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003408 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003409
3410 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003411
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003413 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003414
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 for (int i = 1; i < NumElts; ++i)
3416 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003417 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003419 return true;
3420}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003421
Nate Begeman9008ca62009-04-27 18:41:29 +00003422bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3423 SmallVector<int, 8> M;
3424 N->getMask(M);
3425 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003426}
3427
Evan Cheng017dcc62006-04-21 01:05:10 +00003428/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3429/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003430/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003431static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003432 bool V2IsSplat = false, bool V2IsUndef = false) {
3433 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003434 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003435 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003436
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003438 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003439
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 for (int i = 1; i < NumOps; ++i)
3441 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3442 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3443 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003444 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003445
Evan Cheng39623da2006-04-20 08:58:49 +00003446 return true;
3447}
3448
Nate Begeman9008ca62009-04-27 18:41:29 +00003449static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003450 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 SmallVector<int, 8> M;
3452 N->getMask(M);
3453 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003454}
3455
Evan Chengd9539472006-04-14 21:59:03 +00003456/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3457/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003458bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3459 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003460 return false;
3461
3462 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003463 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 int Elt = N->getMaskElt(i);
3465 if (Elt >= 0 && Elt != 1)
3466 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003467 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003468
3469 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003470 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003471 int Elt = N->getMaskElt(i);
3472 if (Elt >= 0 && Elt != 3)
3473 return false;
3474 if (Elt == 3)
3475 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003476 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003477 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003479 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003480}
3481
3482/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3483/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003484bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3485 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003486 return false;
3487
3488 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 for (unsigned i = 0; i < 2; ++i)
3490 if (N->getMaskElt(i) > 0)
3491 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003492
3493 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003494 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 int Elt = N->getMaskElt(i);
3496 if (Elt >= 0 && Elt != 2)
3497 return false;
3498 if (Elt == 2)
3499 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003500 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003502 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003503}
3504
Evan Cheng0b457f02008-09-25 20:50:48 +00003505/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3506/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003507bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3508 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003509
Nate Begeman9008ca62009-04-27 18:41:29 +00003510 for (int i = 0; i < e; ++i)
3511 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003512 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003513 for (int i = 0; i < e; ++i)
3514 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003515 return false;
3516 return true;
3517}
3518
David Greenec38a03e2011-02-03 15:50:00 +00003519/// isVEXTRACTF128Index - Return true if the specified
3520/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3521/// suitable for input to VEXTRACTF128.
3522bool X86::isVEXTRACTF128Index(SDNode *N) {
3523 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3524 return false;
3525
3526 // The index should be aligned on a 128-bit boundary.
3527 uint64_t Index =
3528 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3529
3530 unsigned VL = N->getValueType(0).getVectorNumElements();
3531 unsigned VBits = N->getValueType(0).getSizeInBits();
3532 unsigned ElSize = VBits / VL;
3533 bool Result = (Index * ElSize) % 128 == 0;
3534
3535 return Result;
3536}
3537
David Greeneccacdc12011-02-04 16:08:29 +00003538/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3539/// operand specifies a subvector insert that is suitable for input to
3540/// VINSERTF128.
3541bool X86::isVINSERTF128Index(SDNode *N) {
3542 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3543 return false;
3544
3545 // The index should be aligned on a 128-bit boundary.
3546 uint64_t Index =
3547 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3548
3549 unsigned VL = N->getValueType(0).getVectorNumElements();
3550 unsigned VBits = N->getValueType(0).getSizeInBits();
3551 unsigned ElSize = VBits / VL;
3552 bool Result = (Index * ElSize) % 128 == 0;
3553
3554 return Result;
3555}
3556
Evan Cheng63d33002006-03-22 08:01:21 +00003557/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003558/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003559unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003560 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3561 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3562
Evan Chengb9df0ca2006-03-22 02:53:00 +00003563 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3564 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003565 for (int i = 0; i < NumOperands; ++i) {
3566 int Val = SVOp->getMaskElt(NumOperands-i-1);
3567 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003568 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003569 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003570 if (i != NumOperands - 1)
3571 Mask <<= Shift;
3572 }
Evan Cheng63d33002006-03-22 08:01:21 +00003573 return Mask;
3574}
3575
Evan Cheng506d3df2006-03-29 23:07:14 +00003576/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003577/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003578unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003579 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003580 unsigned Mask = 0;
3581 // 8 nodes, but we only care about the last 4.
3582 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 int Val = SVOp->getMaskElt(i);
3584 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003585 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003586 if (i != 4)
3587 Mask <<= 2;
3588 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003589 return Mask;
3590}
3591
3592/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003593/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003594unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003596 unsigned Mask = 0;
3597 // 8 nodes, but we only care about the first 4.
3598 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 int Val = SVOp->getMaskElt(i);
3600 if (Val >= 0)
3601 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003602 if (i != 0)
3603 Mask <<= 2;
3604 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003605 return Mask;
3606}
3607
Nate Begemana09008b2009-10-19 02:17:23 +00003608/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3609/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3610unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3611 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3612 EVT VVT = N->getValueType(0);
3613 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3614 int Val = 0;
3615
3616 unsigned i, e;
3617 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3618 Val = SVOp->getMaskElt(i);
3619 if (Val >= 0)
3620 break;
3621 }
3622 return (Val - i) * EltSize;
3623}
3624
David Greenec38a03e2011-02-03 15:50:00 +00003625/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3626/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3627/// instructions.
3628unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3629 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3630 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3631
3632 uint64_t Index =
3633 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3634
3635 EVT VecVT = N->getOperand(0).getValueType();
3636 EVT ElVT = VecVT.getVectorElementType();
3637
3638 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3639
3640 return Index / NumElemsPerChunk;
3641}
3642
David Greeneccacdc12011-02-04 16:08:29 +00003643/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3644/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3645/// instructions.
3646unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3647 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3648 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3649
3650 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003651 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003652
3653 EVT VecVT = N->getValueType(0);
3654 EVT ElVT = VecVT.getVectorElementType();
3655
3656 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3657
3658 return Index / NumElemsPerChunk;
3659}
3660
Evan Cheng37b73872009-07-30 08:33:02 +00003661/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3662/// constant +0.0.
3663bool X86::isZeroNode(SDValue Elt) {
3664 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003665 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003666 (isa<ConstantFPSDNode>(Elt) &&
3667 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3668}
3669
Nate Begeman9008ca62009-04-27 18:41:29 +00003670/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3671/// their permute mask.
3672static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3673 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003674 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003675 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003676 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003677
Nate Begeman5a5ca152009-04-29 05:20:52 +00003678 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003679 int idx = SVOp->getMaskElt(i);
3680 if (idx < 0)
3681 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003682 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003683 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003684 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003685 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003686 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3688 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003689}
3690
Evan Cheng779ccea2007-12-07 21:30:01 +00003691/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3692/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003693static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003694 unsigned NumElems = VT.getVectorNumElements();
3695 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 int idx = Mask[i];
3697 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003698 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003699 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003700 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003701 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003702 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003703 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003704}
3705
Evan Cheng533a0aa2006-04-19 20:35:22 +00003706/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3707/// match movhlps. The lower half elements should come from upper half of
3708/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003709/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003710static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3711 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003712 return false;
3713 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003714 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003715 return false;
3716 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003718 return false;
3719 return true;
3720}
3721
Evan Cheng5ced1d82006-04-06 23:23:56 +00003722/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003723/// is promoted to a vector. It also returns the LoadSDNode by reference if
3724/// required.
3725static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003726 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3727 return false;
3728 N = N->getOperand(0).getNode();
3729 if (!ISD::isNON_EXTLoad(N))
3730 return false;
3731 if (LD)
3732 *LD = cast<LoadSDNode>(N);
3733 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003734}
3735
Evan Cheng533a0aa2006-04-19 20:35:22 +00003736/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3737/// match movlp{s|d}. The lower half elements should come from lower half of
3738/// V1 (and in order), and the upper half elements should come from the upper
3739/// half of V2 (and in order). And since V1 will become the source of the
3740/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003741static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3742 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003743 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003744 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003745 // Is V2 is a vector load, don't do this transformation. We will try to use
3746 // load folding shufps op.
3747 if (ISD::isNON_EXTLoad(V2))
3748 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003749
Nate Begeman5a5ca152009-04-29 05:20:52 +00003750 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003751
Evan Cheng533a0aa2006-04-19 20:35:22 +00003752 if (NumElems != 2 && NumElems != 4)
3753 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003754 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003755 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003756 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003757 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003758 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003759 return false;
3760 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003761}
3762
Evan Cheng39623da2006-04-20 08:58:49 +00003763/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3764/// all the same.
3765static bool isSplatVector(SDNode *N) {
3766 if (N->getOpcode() != ISD::BUILD_VECTOR)
3767 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003768
Dan Gohman475871a2008-07-27 21:46:04 +00003769 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003770 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3771 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003772 return false;
3773 return true;
3774}
3775
Evan Cheng213d2cf2007-05-17 18:45:50 +00003776/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003777/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003778/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003779static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003780 SDValue V1 = N->getOperand(0);
3781 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003782 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3783 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003784 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003785 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003787 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3788 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003789 if (Opc != ISD::BUILD_VECTOR ||
3790 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 return false;
3792 } else if (Idx >= 0) {
3793 unsigned Opc = V1.getOpcode();
3794 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3795 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003796 if (Opc != ISD::BUILD_VECTOR ||
3797 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003798 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003799 }
3800 }
3801 return true;
3802}
3803
3804/// getZeroVector - Returns a vector of specified type with all zero elements.
3805///
Owen Andersone50ed302009-08-10 22:56:29 +00003806static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003807 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003808 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003809
Dale Johannesen0488fb62010-09-30 23:57:10 +00003810 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003811 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003812 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003813 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003814 if (HasSSE2) { // SSE2
3815 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3816 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3817 } else { // SSE1
3818 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3819 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3820 }
3821 } else if (VT.getSizeInBits() == 256) { // AVX
3822 // 256-bit logic and arithmetic instructions in AVX are
3823 // all floating-point, no support for integer ops. Default
3824 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003825 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003826 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3827 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003828 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003829 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003830}
3831
Chris Lattner8a594482007-11-25 00:24:49 +00003832/// getOnesVector - Returns a vector of specified type with all bits set.
3833///
Owen Andersone50ed302009-08-10 22:56:29 +00003834static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003835 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003836
Chris Lattner8a594482007-11-25 00:24:49 +00003837 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3838 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003839 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003840 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003841 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003842 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003843}
3844
3845
Evan Cheng39623da2006-04-20 08:58:49 +00003846/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3847/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003848static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003849 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003850 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003851
Evan Cheng39623da2006-04-20 08:58:49 +00003852 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003853 SmallVector<int, 8> MaskVec;
3854 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003855
Nate Begeman5a5ca152009-04-29 05:20:52 +00003856 for (unsigned i = 0; i != NumElems; ++i) {
3857 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003858 MaskVec[i] = NumElems;
3859 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003860 }
Evan Cheng39623da2006-04-20 08:58:49 +00003861 }
Evan Cheng39623da2006-04-20 08:58:49 +00003862 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003863 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3864 SVOp->getOperand(1), &MaskVec[0]);
3865 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003866}
3867
Evan Cheng017dcc62006-04-21 01:05:10 +00003868/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3869/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003870static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 SDValue V2) {
3872 unsigned NumElems = VT.getVectorNumElements();
3873 SmallVector<int, 8> Mask;
3874 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003875 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 Mask.push_back(i);
3877 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003878}
3879
Nate Begeman9008ca62009-04-27 18:41:29 +00003880/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003881static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003882 SDValue V2) {
3883 unsigned NumElems = VT.getVectorNumElements();
3884 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003885 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 Mask.push_back(i);
3887 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003888 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003890}
3891
Nate Begeman9008ca62009-04-27 18:41:29 +00003892/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003893static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003894 SDValue V2) {
3895 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003896 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003898 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 Mask.push_back(i + Half);
3900 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003901 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003903}
3904
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003905/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3906static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003907 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003908 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 DebugLoc dl = SV->getDebugLoc();
3910 SDValue V1 = SV->getOperand(0);
3911 int NumElems = VT.getVectorNumElements();
3912 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003913
Nate Begeman9008ca62009-04-27 18:41:29 +00003914 // unpack elements to the correct location
3915 while (NumElems > 4) {
3916 if (EltNo < NumElems/2) {
3917 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3918 } else {
3919 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3920 EltNo -= NumElems/2;
3921 }
3922 NumElems >>= 1;
3923 }
Eric Christopherfd179292009-08-27 18:07:15 +00003924
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 // Perform the splat.
3926 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003927 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003929 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003930}
3931
Evan Chengba05f722006-04-21 23:03:30 +00003932/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003933/// vector of zero or undef vector. This produces a shuffle where the low
3934/// element of V2 is swizzled into the zero/undef vector, landing at element
3935/// 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 +00003936static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003937 bool isZero, bool HasSSE2,
3938 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003939 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3942 unsigned NumElems = VT.getVectorNumElements();
3943 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003944 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 // If this is the insertion idx, put the low elt of V2 here.
3946 MaskVec.push_back(i == Idx ? NumElems : i);
3947 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003948}
3949
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003950/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3951/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003952static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3953 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003954 if (Depth == 6)
3955 return SDValue(); // Limit search depth.
3956
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957 SDValue V = SDValue(N, 0);
3958 EVT VT = V.getValueType();
3959 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003960
3961 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3962 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3963 Index = SV->getMaskElt(Index);
3964
3965 if (Index < 0)
3966 return DAG.getUNDEF(VT.getVectorElementType());
3967
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003968 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003969 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003970 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003971 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003972
3973 // Recurse into target specific vector shuffles to find scalars.
3974 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003975 int NumElems = VT.getVectorNumElements();
3976 SmallVector<unsigned, 16> ShuffleMask;
3977 SDValue ImmN;
3978
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003979 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003980 case X86ISD::SHUFPS:
3981 case X86ISD::SHUFPD:
3982 ImmN = N->getOperand(N->getNumOperands()-1);
3983 DecodeSHUFPSMask(NumElems,
3984 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3985 ShuffleMask);
3986 break;
3987 case X86ISD::PUNPCKHBW:
3988 case X86ISD::PUNPCKHWD:
3989 case X86ISD::PUNPCKHDQ:
3990 case X86ISD::PUNPCKHQDQ:
3991 DecodePUNPCKHMask(NumElems, ShuffleMask);
3992 break;
3993 case X86ISD::UNPCKHPS:
3994 case X86ISD::UNPCKHPD:
3995 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3996 break;
3997 case X86ISD::PUNPCKLBW:
3998 case X86ISD::PUNPCKLWD:
3999 case X86ISD::PUNPCKLDQ:
4000 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00004001 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004002 break;
4003 case X86ISD::UNPCKLPS:
4004 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00004005 case X86ISD::VUNPCKLPS:
4006 case X86ISD::VUNPCKLPD:
4007 case X86ISD::VUNPCKLPSY:
4008 case X86ISD::VUNPCKLPDY:
4009 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004010 break;
4011 case X86ISD::MOVHLPS:
4012 DecodeMOVHLPSMask(NumElems, ShuffleMask);
4013 break;
4014 case X86ISD::MOVLHPS:
4015 DecodeMOVLHPSMask(NumElems, ShuffleMask);
4016 break;
4017 case X86ISD::PSHUFD:
4018 ImmN = N->getOperand(N->getNumOperands()-1);
4019 DecodePSHUFMask(NumElems,
4020 cast<ConstantSDNode>(ImmN)->getZExtValue(),
4021 ShuffleMask);
4022 break;
4023 case X86ISD::PSHUFHW:
4024 ImmN = N->getOperand(N->getNumOperands()-1);
4025 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4026 ShuffleMask);
4027 break;
4028 case X86ISD::PSHUFLW:
4029 ImmN = N->getOperand(N->getNumOperands()-1);
4030 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
4031 ShuffleMask);
4032 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004033 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004034 case X86ISD::MOVSD: {
4035 // The index 0 always comes from the first element of the second source,
4036 // this is why MOVSS and MOVSD are used in the first place. The other
4037 // elements come from the other positions of the first source vector.
4038 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004039 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
4040 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004041 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004042 default:
4043 assert("not implemented for target shuffle node");
4044 return SDValue();
4045 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004046
4047 Index = ShuffleMask[Index];
4048 if (Index < 0)
4049 return DAG.getUNDEF(VT.getVectorElementType());
4050
4051 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4052 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4053 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004054 }
4055
4056 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004057 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004058 V = V.getOperand(0);
4059 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004060 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004061
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004062 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004063 return SDValue();
4064 }
4065
4066 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4067 return (Index == 0) ? V.getOperand(0)
4068 : DAG.getUNDEF(VT.getVectorElementType());
4069
4070 if (V.getOpcode() == ISD::BUILD_VECTOR)
4071 return V.getOperand(Index);
4072
4073 return SDValue();
4074}
4075
4076/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4077/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004078/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004079static
4080unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4081 bool ZerosFromLeft, SelectionDAG &DAG) {
4082 int i = 0;
4083
4084 while (i < NumElems) {
4085 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004086 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004087 if (!(Elt.getNode() &&
4088 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4089 break;
4090 ++i;
4091 }
4092
4093 return i;
4094}
4095
4096/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4097/// MaskE correspond consecutively to elements from one of the vector operands,
4098/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4099static
4100bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4101 int OpIdx, int NumElems, unsigned &OpNum) {
4102 bool SeenV1 = false;
4103 bool SeenV2 = false;
4104
4105 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4106 int Idx = SVOp->getMaskElt(i);
4107 // Ignore undef indicies
4108 if (Idx < 0)
4109 continue;
4110
4111 if (Idx < NumElems)
4112 SeenV1 = true;
4113 else
4114 SeenV2 = true;
4115
4116 // Only accept consecutive elements from the same vector
4117 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4118 return false;
4119 }
4120
4121 OpNum = SeenV1 ? 0 : 1;
4122 return true;
4123}
4124
4125/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4126/// logical left shift of a vector.
4127static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4128 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4129 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4130 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4131 false /* check zeros from right */, DAG);
4132 unsigned OpSrc;
4133
4134 if (!NumZeros)
4135 return false;
4136
4137 // Considering the elements in the mask that are not consecutive zeros,
4138 // check if they consecutively come from only one of the source vectors.
4139 //
4140 // V1 = {X, A, B, C} 0
4141 // \ \ \ /
4142 // vector_shuffle V1, V2 <1, 2, 3, X>
4143 //
4144 if (!isShuffleMaskConsecutive(SVOp,
4145 0, // Mask Start Index
4146 NumElems-NumZeros-1, // Mask End Index
4147 NumZeros, // Where to start looking in the src vector
4148 NumElems, // Number of elements in vector
4149 OpSrc)) // Which source operand ?
4150 return false;
4151
4152 isLeft = false;
4153 ShAmt = NumZeros;
4154 ShVal = SVOp->getOperand(OpSrc);
4155 return true;
4156}
4157
4158/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4159/// logical left shift of a vector.
4160static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4161 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4162 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4163 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4164 true /* check zeros from left */, DAG);
4165 unsigned OpSrc;
4166
4167 if (!NumZeros)
4168 return false;
4169
4170 // Considering the elements in the mask that are not consecutive zeros,
4171 // check if they consecutively come from only one of the source vectors.
4172 //
4173 // 0 { A, B, X, X } = V2
4174 // / \ / /
4175 // vector_shuffle V1, V2 <X, X, 4, 5>
4176 //
4177 if (!isShuffleMaskConsecutive(SVOp,
4178 NumZeros, // Mask Start Index
4179 NumElems-1, // Mask End Index
4180 0, // Where to start looking in the src vector
4181 NumElems, // Number of elements in vector
4182 OpSrc)) // Which source operand ?
4183 return false;
4184
4185 isLeft = true;
4186 ShAmt = NumZeros;
4187 ShVal = SVOp->getOperand(OpSrc);
4188 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004189}
4190
4191/// isVectorShift - Returns true if the shuffle can be implemented as a
4192/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004193static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004194 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004195 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4196 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4197 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004198
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004199 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004200}
4201
Evan Chengc78d3b42006-04-24 18:01:45 +00004202/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4203///
Dan Gohman475871a2008-07-27 21:46:04 +00004204static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004205 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004206 SelectionDAG &DAG,
4207 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004208 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004209 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004210
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004211 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004212 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004213 bool First = true;
4214 for (unsigned i = 0; i < 16; ++i) {
4215 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4216 if (ThisIsNonZero && First) {
4217 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004219 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004221 First = false;
4222 }
4223
4224 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004226 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4227 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004228 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004229 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004230 }
4231 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4233 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4234 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004235 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004237 } else
4238 ThisElt = LastElt;
4239
Gabor Greifba36cb52008-08-28 21:40:38 +00004240 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004242 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004243 }
4244 }
4245
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004246 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004247}
4248
Bill Wendlinga348c562007-03-22 18:42:45 +00004249/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004250///
Dan Gohman475871a2008-07-27 21:46:04 +00004251static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004252 unsigned NumNonZero, unsigned NumZero,
4253 SelectionDAG &DAG,
4254 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004255 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004256 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004257
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004258 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004259 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004260 bool First = true;
4261 for (unsigned i = 0; i < 8; ++i) {
4262 bool isNonZero = (NonZeros & (1 << i)) != 0;
4263 if (isNonZero) {
4264 if (First) {
4265 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004266 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004267 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004269 First = false;
4270 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004271 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004272 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004273 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004274 }
4275 }
4276
4277 return V;
4278}
4279
Evan Chengf26ffe92008-05-29 08:22:04 +00004280/// getVShift - Return a vector logical shift node.
4281///
Owen Andersone50ed302009-08-10 22:56:29 +00004282static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004283 unsigned NumBits, SelectionDAG &DAG,
4284 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004285 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004286 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004287 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4288 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004289 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004290 DAG.getConstant(NumBits,
4291 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004292}
4293
Dan Gohman475871a2008-07-27 21:46:04 +00004294SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004295X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004296 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004297
Evan Chengc3630942009-12-09 21:00:30 +00004298 // Check if the scalar load can be widened into a vector load. And if
4299 // the address is "base + cst" see if the cst can be "absorbed" into
4300 // the shuffle mask.
4301 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4302 SDValue Ptr = LD->getBasePtr();
4303 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4304 return SDValue();
4305 EVT PVT = LD->getValueType(0);
4306 if (PVT != MVT::i32 && PVT != MVT::f32)
4307 return SDValue();
4308
4309 int FI = -1;
4310 int64_t Offset = 0;
4311 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4312 FI = FINode->getIndex();
4313 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004314 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004315 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4316 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4317 Offset = Ptr.getConstantOperandVal(1);
4318 Ptr = Ptr.getOperand(0);
4319 } else {
4320 return SDValue();
4321 }
4322
4323 SDValue Chain = LD->getChain();
4324 // Make sure the stack object alignment is at least 16.
4325 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4326 if (DAG.InferPtrAlignment(Ptr) < 16) {
4327 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004328 // Can't change the alignment. FIXME: It's possible to compute
4329 // the exact stack offset and reference FI + adjust offset instead.
4330 // If someone *really* cares about this. That's the way to implement it.
4331 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004332 } else {
4333 MFI->setObjectAlignment(FI, 16);
4334 }
4335 }
4336
4337 // (Offset % 16) must be multiple of 4. Then address is then
4338 // Ptr + (Offset & ~15).
4339 if (Offset < 0)
4340 return SDValue();
4341 if ((Offset % 16) & 3)
4342 return SDValue();
4343 int64_t StartOffset = Offset & ~15;
4344 if (StartOffset)
4345 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4346 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4347
4348 int EltNo = (Offset - StartOffset) >> 2;
4349 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4350 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004351 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4352 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004353 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004354 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004355 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4356 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004357 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004358 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004359 }
4360
4361 return SDValue();
4362}
4363
Michael J. Spencerec38de22010-10-10 22:04:20 +00004364/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4365/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004366/// load which has the same value as a build_vector whose operands are 'elts'.
4367///
4368/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004369///
Nate Begeman1449f292010-03-24 22:19:06 +00004370/// FIXME: we'd also like to handle the case where the last elements are zero
4371/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4372/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004373static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004374 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004375 EVT EltVT = VT.getVectorElementType();
4376 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004377
Nate Begemanfdea31a2010-03-24 20:49:50 +00004378 LoadSDNode *LDBase = NULL;
4379 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004380
Nate Begeman1449f292010-03-24 22:19:06 +00004381 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004382 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004383 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004384 for (unsigned i = 0; i < NumElems; ++i) {
4385 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004386
Nate Begemanfdea31a2010-03-24 20:49:50 +00004387 if (!Elt.getNode() ||
4388 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4389 return SDValue();
4390 if (!LDBase) {
4391 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4392 return SDValue();
4393 LDBase = cast<LoadSDNode>(Elt.getNode());
4394 LastLoadedElt = i;
4395 continue;
4396 }
4397 if (Elt.getOpcode() == ISD::UNDEF)
4398 continue;
4399
4400 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4401 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4402 return SDValue();
4403 LastLoadedElt = i;
4404 }
Nate Begeman1449f292010-03-24 22:19:06 +00004405
4406 // If we have found an entire vector of loads and undefs, then return a large
4407 // load of the entire vector width starting at the base pointer. If we found
4408 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004409 if (LastLoadedElt == NumElems - 1) {
4410 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004411 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004412 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004413 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004414 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004415 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004416 LDBase->isVolatile(), LDBase->isNonTemporal(),
4417 LDBase->getAlignment());
4418 } else if (NumElems == 4 && LastLoadedElt == 1) {
4419 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4420 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004421 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4422 Ops, 2, MVT::i32,
4423 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004424 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004425 }
4426 return SDValue();
4427}
4428
Evan Chengc3630942009-12-09 21:00:30 +00004429SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004430X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004431 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004432
David Greenef125a292011-02-08 19:04:41 +00004433 EVT VT = Op.getValueType();
4434 EVT ExtVT = VT.getVectorElementType();
4435
4436 unsigned NumElems = Op.getNumOperands();
4437
4438 // For AVX-length vectors, build the individual 128-bit pieces and
4439 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004440 if (VT.getSizeInBits() > 256 &&
4441 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004442 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4443 SmallVector<SDValue, 8> V;
4444 V.resize(NumElems);
4445 for (unsigned i = 0; i < NumElems; ++i) {
4446 V[i] = Op.getOperand(i);
4447 }
Owen Anderson95771af2011-02-25 21:41:48 +00004448
David Greenef125a292011-02-08 19:04:41 +00004449 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4450
4451 // Build the lower subvector.
4452 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4453 // Build the upper subvector.
4454 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4455 NumElems/2);
4456
4457 return ConcatVectors(Lower, Upper, DAG);
4458 }
4459
Chris Lattner6e80e442010-08-28 17:15:43 +00004460 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4461 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004462 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4463 // is present, so AllOnes is ignored.
4464 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4465 (Op.getValueType().getSizeInBits() != 256 &&
4466 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004467 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004468 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4469 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004470 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004471 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004472
Gabor Greifba36cb52008-08-28 21:40:38 +00004473 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004474 return getOnesVector(Op.getValueType(), DAG, dl);
4475 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004476 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477
Owen Andersone50ed302009-08-10 22:56:29 +00004478 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479
Evan Cheng0db9fe62006-04-25 20:13:52 +00004480 unsigned NumZero = 0;
4481 unsigned NumNonZero = 0;
4482 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004483 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004484 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004486 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004487 if (Elt.getOpcode() == ISD::UNDEF)
4488 continue;
4489 Values.insert(Elt);
4490 if (Elt.getOpcode() != ISD::Constant &&
4491 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004492 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004493 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004494 NumZero++;
4495 else {
4496 NonZeros |= (1 << i);
4497 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004498 }
4499 }
4500
Chris Lattner97a2a562010-08-26 05:24:29 +00004501 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4502 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004503 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004504
Chris Lattner67f453a2008-03-09 05:42:06 +00004505 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004506 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004508 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004509
Chris Lattner62098042008-03-09 01:05:04 +00004510 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4511 // the value are obviously zero, truncate the value to i32 and do the
4512 // insertion that way. Only do this if the value is non-constant or if the
4513 // value is a constant being inserted into element 0. It is cheaper to do
4514 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004515 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004516 (!IsAllConstants || Idx == 0)) {
4517 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004518 // Handle SSE only.
4519 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4520 EVT VecVT = MVT::v4i32;
4521 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004522
Chris Lattner62098042008-03-09 01:05:04 +00004523 // Truncate the value (which may itself be a constant) to i32, and
4524 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004525 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004526 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004527 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4528 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004529
Chris Lattner62098042008-03-09 01:05:04 +00004530 // Now we have our 32-bit value zero extended in the low element of
4531 // a vector. If Idx != 0, swizzle it into place.
4532 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004533 SmallVector<int, 4> Mask;
4534 Mask.push_back(Idx);
4535 for (unsigned i = 1; i != VecElts; ++i)
4536 Mask.push_back(i);
4537 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004538 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004539 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004540 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004541 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004542 }
4543 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Chris Lattner19f79692008-03-08 22:59:52 +00004545 // If we have a constant or non-constant insertion into the low element of
4546 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4547 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004548 // depending on what the source datatype is.
4549 if (Idx == 0) {
4550 if (NumZero == 0) {
4551 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4553 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004554 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4555 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4556 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4557 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004558 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4559 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004560 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4561 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004562 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4563 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4564 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004565 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004566 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004567 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004568
4569 // Is it a vector logical left shift?
4570 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004571 X86::isZeroNode(Op.getOperand(0)) &&
4572 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004573 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004574 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004575 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004576 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004577 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004578 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004579
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004580 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004581 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582
Chris Lattner19f79692008-03-08 22:59:52 +00004583 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4584 // is a non-constant being inserted into an element other than the low one,
4585 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4586 // movd/movss) to move this into the low element, then shuffle it into
4587 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004588 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004589 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004590
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004592 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4593 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004594 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 MaskVec.push_back(i == Idx ? 0 : 1);
4597 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 }
4599 }
4600
Chris Lattner67f453a2008-03-09 05:42:06 +00004601 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004602 if (Values.size() == 1) {
4603 if (EVTBits == 32) {
4604 // Instead of a shuffle like this:
4605 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4606 // Check if it's possible to issue this instead.
4607 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4608 unsigned Idx = CountTrailingZeros_32(NonZeros);
4609 SDValue Item = Op.getOperand(Idx);
4610 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4611 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4612 }
Dan Gohman475871a2008-07-27 21:46:04 +00004613 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004615
Dan Gohmana3941172007-07-24 22:55:08 +00004616 // A vector full of immediates; various special cases are already
4617 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004618 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004619 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004620
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004621 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004622 if (EVTBits == 64) {
4623 if (NumNonZero == 1) {
4624 // One half is zero or undef.
4625 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004626 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004627 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004628 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4629 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004630 }
Dan Gohman475871a2008-07-27 21:46:04 +00004631 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004632 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633
4634 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004635 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004636 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004637 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004638 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639 }
4640
Bill Wendling826f36f2007-03-28 00:57:11 +00004641 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004643 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004644 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004645 }
4646
4647 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004648 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004649 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004650 if (NumElems == 4 && NumZero > 0) {
4651 for (unsigned i = 0; i < 4; ++i) {
4652 bool isZero = !(NonZeros & (1 << i));
4653 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004654 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655 else
Dale Johannesenace16102009-02-03 19:33:06 +00004656 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004657 }
4658
4659 for (unsigned i = 0; i < 2; ++i) {
4660 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4661 default: break;
4662 case 0:
4663 V[i] = V[i*2]; // Must be a zero vector.
4664 break;
4665 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004666 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667 break;
4668 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004669 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004670 break;
4671 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004672 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004673 break;
4674 }
4675 }
4676
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004678 bool Reverse = (NonZeros & 0x3) == 2;
4679 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004680 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004681 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4682 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004683 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4684 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004685 }
4686
Nate Begemanfdea31a2010-03-24 20:49:50 +00004687 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4688 // Check for a build vector of consecutive loads.
4689 for (unsigned i = 0; i < NumElems; ++i)
4690 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004691
Nate Begemanfdea31a2010-03-24 20:49:50 +00004692 // Check for elements which are consecutive loads.
4693 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4694 if (LD.getNode())
4695 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004696
4697 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004698 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004699 SDValue Result;
4700 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4701 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4702 else
4703 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004704
Chris Lattner24faf612010-08-28 17:59:08 +00004705 for (unsigned i = 1; i < NumElems; ++i) {
4706 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4707 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004709 }
4710 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004712
Chris Lattner6e80e442010-08-28 17:15:43 +00004713 // Otherwise, expand into a number of unpckl*, start by extending each of
4714 // our (non-undef) elements to the full vector width with the element in the
4715 // bottom slot of the vector (which generates no code for SSE).
4716 for (unsigned i = 0; i < NumElems; ++i) {
4717 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4718 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4719 else
4720 V[i] = DAG.getUNDEF(VT);
4721 }
4722
4723 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004724 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4725 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4726 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004727 unsigned EltStride = NumElems >> 1;
4728 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004729 for (unsigned i = 0; i < EltStride; ++i) {
4730 // If V[i+EltStride] is undef and this is the first round of mixing,
4731 // then it is safe to just drop this shuffle: V[i] is already in the
4732 // right place, the one element (since it's the first round) being
4733 // inserted as undef can be dropped. This isn't safe for successive
4734 // rounds because they will permute elements within both vectors.
4735 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4736 EltStride == NumElems/2)
4737 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004738
Chris Lattner6e80e442010-08-28 17:15:43 +00004739 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004740 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004741 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004742 }
4743 return V[0];
4744 }
Dan Gohman475871a2008-07-27 21:46:04 +00004745 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746}
4747
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004748SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004749X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004750 // We support concatenate two MMX registers and place them in a MMX
4751 // register. This is better than doing a stack convert.
4752 DebugLoc dl = Op.getDebugLoc();
4753 EVT ResVT = Op.getValueType();
4754 assert(Op.getNumOperands() == 2);
4755 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4756 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4757 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004758 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004759 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4760 InVec = Op.getOperand(1);
4761 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4762 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004763 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004764 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4765 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4766 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004767 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004768 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4769 Mask[0] = 0; Mask[1] = 2;
4770 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4771 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004772 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004773}
4774
Nate Begemanb9a47b82009-02-23 08:49:38 +00004775// v8i16 shuffles - Prefer shuffles in the following order:
4776// 1. [all] pshuflw, pshufhw, optional move
4777// 2. [ssse3] 1 x pshufb
4778// 3. [ssse3] 2 x pshufb + 1 x por
4779// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004780SDValue
4781X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4782 SelectionDAG &DAG) const {
4783 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004784 SDValue V1 = SVOp->getOperand(0);
4785 SDValue V2 = SVOp->getOperand(1);
4786 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004788
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // Determine if more than 1 of the words in each of the low and high quadwords
4790 // of the result come from the same quadword of one of the two inputs. Undef
4791 // mask values count as coming from any quadword, for better codegen.
4792 SmallVector<unsigned, 4> LoQuad(4);
4793 SmallVector<unsigned, 4> HiQuad(4);
4794 BitVector InputQuads(4);
4795 for (unsigned i = 0; i < 8; ++i) {
4796 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004797 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004798 MaskVals.push_back(EltIdx);
4799 if (EltIdx < 0) {
4800 ++Quad[0];
4801 ++Quad[1];
4802 ++Quad[2];
4803 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004804 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 }
4806 ++Quad[EltIdx / 4];
4807 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004808 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004809
Nate Begemanb9a47b82009-02-23 08:49:38 +00004810 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004811 unsigned MaxQuad = 1;
4812 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 if (LoQuad[i] > MaxQuad) {
4814 BestLoQuad = i;
4815 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004816 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004817 }
4818
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004820 MaxQuad = 1;
4821 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 if (HiQuad[i] > MaxQuad) {
4823 BestHiQuad = i;
4824 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 }
4826 }
4827
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004829 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004830 // single pshufb instruction is necessary. If There are more than 2 input
4831 // quads, disable the next transformation since it does not help SSSE3.
4832 bool V1Used = InputQuads[0] || InputQuads[1];
4833 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004834 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004835 if (InputQuads.count() == 2 && V1Used && V2Used) {
4836 BestLoQuad = InputQuads.find_first();
4837 BestHiQuad = InputQuads.find_next(BestLoQuad);
4838 }
4839 if (InputQuads.count() > 2) {
4840 BestLoQuad = -1;
4841 BestHiQuad = -1;
4842 }
4843 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004844
Nate Begemanb9a47b82009-02-23 08:49:38 +00004845 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4846 // the shuffle mask. If a quad is scored as -1, that means that it contains
4847 // words from all 4 input quadwords.
4848 SDValue NewV;
4849 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004850 SmallVector<int, 8> MaskV;
4851 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4852 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004853 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004854 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4855 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4856 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004857
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4859 // source words for the shuffle, to aid later transformations.
4860 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004861 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004862 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004863 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004864 if (idx != (int)i)
4865 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004866 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004867 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 AllWordsInNewV = false;
4869 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004870 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004871
Nate Begemanb9a47b82009-02-23 08:49:38 +00004872 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4873 if (AllWordsInNewV) {
4874 for (int i = 0; i != 8; ++i) {
4875 int idx = MaskVals[i];
4876 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004877 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004878 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 if ((idx != i) && idx < 4)
4880 pshufhw = false;
4881 if ((idx != i) && idx > 3)
4882 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004883 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004884 V1 = NewV;
4885 V2Used = false;
4886 BestLoQuad = 0;
4887 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004888 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004889
Nate Begemanb9a47b82009-02-23 08:49:38 +00004890 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4891 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004892 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004893 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4894 unsigned TargetMask = 0;
4895 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004897 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4898 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4899 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004900 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004901 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004902 }
Eric Christopherfd179292009-08-27 18:07:15 +00004903
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 // If we have SSSE3, and all words of the result are from 1 input vector,
4905 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4906 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004907 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004908 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004909
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004911 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004912 // mask, and elements that come from V1 in the V2 mask, so that the two
4913 // results can be OR'd together.
4914 bool TwoInputs = V1Used && V2Used;
4915 for (unsigned i = 0; i != 8; ++i) {
4916 int EltIdx = MaskVals[i] * 2;
4917 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004918 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4919 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004920 continue;
4921 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004922 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4923 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004925 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004926 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004927 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004928 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004929 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004930 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004931
Nate Begemanb9a47b82009-02-23 08:49:38 +00004932 // Calculate the shuffle mask for the second input, shuffle it, and
4933 // OR it with the first shuffled input.
4934 pshufbMask.clear();
4935 for (unsigned i = 0; i != 8; ++i) {
4936 int EltIdx = MaskVals[i] * 2;
4937 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004938 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4939 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004940 continue;
4941 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4943 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004944 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004945 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004946 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004947 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004948 MVT::v16i8, &pshufbMask[0], 16));
4949 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004950 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 }
4952
4953 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4954 // and update MaskVals with new element order.
4955 BitVector InOrder(8);
4956 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004957 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 for (int i = 0; i != 4; ++i) {
4959 int idx = MaskVals[i];
4960 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004961 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004962 InOrder.set(i);
4963 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004964 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004965 InOrder.set(i);
4966 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004967 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004968 }
4969 }
4970 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004971 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004972 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004973 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004974
4975 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4976 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4977 NewV.getOperand(0),
4978 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4979 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004980 }
Eric Christopherfd179292009-08-27 18:07:15 +00004981
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4983 // and update MaskVals with the new element order.
4984 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004985 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004986 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004988 for (unsigned i = 4; i != 8; ++i) {
4989 int idx = MaskVals[i];
4990 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004991 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 InOrder.set(i);
4993 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004994 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004995 InOrder.set(i);
4996 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004997 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004998 }
4999 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005001 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005002
5003 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
5004 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
5005 NewV.getOperand(0),
5006 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
5007 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005008 }
Eric Christopherfd179292009-08-27 18:07:15 +00005009
Nate Begemanb9a47b82009-02-23 08:49:38 +00005010 // In case BestHi & BestLo were both -1, which means each quadword has a word
5011 // from each of the four input quadwords, calculate the InOrder bitvector now
5012 // before falling through to the insert/extract cleanup.
5013 if (BestLoQuad == -1 && BestHiQuad == -1) {
5014 NewV = V1;
5015 for (int i = 0; i != 8; ++i)
5016 if (MaskVals[i] < 0 || MaskVals[i] == i)
5017 InOrder.set(i);
5018 }
Eric Christopherfd179292009-08-27 18:07:15 +00005019
Nate Begemanb9a47b82009-02-23 08:49:38 +00005020 // The other elements are put in the right place using pextrw and pinsrw.
5021 for (unsigned i = 0; i != 8; ++i) {
5022 if (InOrder[i])
5023 continue;
5024 int EltIdx = MaskVals[i];
5025 if (EltIdx < 0)
5026 continue;
5027 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005028 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005029 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00005030 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 DAG.getIntPtrConstant(i));
5034 }
5035 return NewV;
5036}
5037
5038// v16i8 shuffles - Prefer shuffles in the following order:
5039// 1. [ssse3] 1 x pshufb
5040// 2. [ssse3] 2 x pshufb + 1 x por
5041// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5042static
Nate Begeman9008ca62009-04-27 18:41:29 +00005043SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005044 SelectionDAG &DAG,
5045 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005046 SDValue V1 = SVOp->getOperand(0);
5047 SDValue V2 = SVOp->getOperand(1);
5048 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005049 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005050 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005051
Nate Begemanb9a47b82009-02-23 08:49:38 +00005052 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005053 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005054 // present, fall back to case 3.
5055 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5056 bool V1Only = true;
5057 bool V2Only = true;
5058 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005059 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005060 if (EltIdx < 0)
5061 continue;
5062 if (EltIdx < 16)
5063 V2Only = false;
5064 else
5065 V1Only = false;
5066 }
Eric Christopherfd179292009-08-27 18:07:15 +00005067
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5069 if (TLI.getSubtarget()->hasSSSE3()) {
5070 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005071
Nate Begemanb9a47b82009-02-23 08:49:38 +00005072 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005073 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 //
5075 // Otherwise, we have elements from both input vectors, and must zero out
5076 // elements that come from V2 in the first mask, and V1 in the second mask
5077 // so that we can OR them together.
5078 bool TwoInputs = !(V1Only || V2Only);
5079 for (unsigned i = 0; i != 16; ++i) {
5080 int EltIdx = MaskVals[i];
5081 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005082 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005083 continue;
5084 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005085 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005086 }
5087 // If all the elements are from V2, assign it to V1 and return after
5088 // building the first pshufb.
5089 if (V2Only)
5090 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005091 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005092 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005093 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005094 if (!TwoInputs)
5095 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005096
Nate Begemanb9a47b82009-02-23 08:49:38 +00005097 // Calculate the shuffle mask for the second input, shuffle it, and
5098 // OR it with the first shuffled input.
5099 pshufbMask.clear();
5100 for (unsigned i = 0; i != 16; ++i) {
5101 int EltIdx = MaskVals[i];
5102 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005103 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005104 continue;
5105 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005106 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005107 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005109 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 MVT::v16i8, &pshufbMask[0], 16));
5111 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005112 }
Eric Christopherfd179292009-08-27 18:07:15 +00005113
Nate Begemanb9a47b82009-02-23 08:49:38 +00005114 // No SSSE3 - Calculate in place words and then fix all out of place words
5115 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5116 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005117 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5118 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005119 SDValue NewV = V2Only ? V2 : V1;
5120 for (int i = 0; i != 8; ++i) {
5121 int Elt0 = MaskVals[i*2];
5122 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005123
Nate Begemanb9a47b82009-02-23 08:49:38 +00005124 // This word of the result is all undef, skip it.
5125 if (Elt0 < 0 && Elt1 < 0)
5126 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005127
Nate Begemanb9a47b82009-02-23 08:49:38 +00005128 // This word of the result is already in the correct place, skip it.
5129 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5130 continue;
5131 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5132 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005133
Nate Begemanb9a47b82009-02-23 08:49:38 +00005134 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5135 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5136 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005137
5138 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5139 // using a single extract together, load it and store it.
5140 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005141 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005142 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005144 DAG.getIntPtrConstant(i));
5145 continue;
5146 }
5147
Nate Begemanb9a47b82009-02-23 08:49:38 +00005148 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005149 // source byte is not also odd, shift the extracted word left 8 bits
5150 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005151 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005152 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005153 DAG.getIntPtrConstant(Elt1 / 2));
5154 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005156 DAG.getConstant(8,
5157 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005158 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5160 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005161 }
5162 // If Elt0 is defined, extract it from the appropriate source. If the
5163 // source byte is not also even, shift the extracted word right 8 bits. If
5164 // Elt1 was also defined, OR the extracted values together before
5165 // inserting them in the result.
5166 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005167 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005168 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5169 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005170 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005171 DAG.getConstant(8,
5172 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005173 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5175 DAG.getConstant(0x00FF, MVT::i16));
5176 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005177 : InsElt0;
5178 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005179 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005180 DAG.getIntPtrConstant(i));
5181 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005182 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005183}
5184
Evan Cheng7a831ce2007-12-15 03:00:47 +00005185/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005186/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005187/// done when every pair / quad of shuffle mask elements point to elements in
5188/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005189/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005190static
Nate Begeman9008ca62009-04-27 18:41:29 +00005191SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005192 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005193 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005194 SDValue V1 = SVOp->getOperand(0);
5195 SDValue V2 = SVOp->getOperand(1);
5196 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005197 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005198 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005199 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005200 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005201 case MVT::v4f32: NewVT = MVT::v2f64; break;
5202 case MVT::v4i32: NewVT = MVT::v2i64; break;
5203 case MVT::v8i16: NewVT = MVT::v4i32; break;
5204 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005205 }
5206
Nate Begeman9008ca62009-04-27 18:41:29 +00005207 int Scale = NumElems / NewWidth;
5208 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005209 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005210 int StartIdx = -1;
5211 for (int j = 0; j < Scale; ++j) {
5212 int EltIdx = SVOp->getMaskElt(i+j);
5213 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005214 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005215 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005216 StartIdx = EltIdx - (EltIdx % Scale);
5217 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005218 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005219 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005220 if (StartIdx == -1)
5221 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005222 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005223 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005224 }
5225
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005226 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5227 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005228 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005229}
5230
Evan Chengd880b972008-05-09 21:53:03 +00005231/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005232///
Owen Andersone50ed302009-08-10 22:56:29 +00005233static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005234 SDValue SrcOp, SelectionDAG &DAG,
5235 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005236 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005237 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005238 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005239 LD = dyn_cast<LoadSDNode>(SrcOp);
5240 if (!LD) {
5241 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5242 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005243 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005244 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005245 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005246 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005247 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005248 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005250 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005251 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5252 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5253 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005254 SrcOp.getOperand(0)
5255 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005256 }
5257 }
5258 }
5259
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005260 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005261 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005262 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005263 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005264}
5265
Evan Chengace3c172008-07-22 21:13:36 +00005266/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5267/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005268static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005269LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5270 SDValue V1 = SVOp->getOperand(0);
5271 SDValue V2 = SVOp->getOperand(1);
5272 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005273 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005274
Evan Chengace3c172008-07-22 21:13:36 +00005275 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005276 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005277 SmallVector<int, 8> Mask1(4U, -1);
5278 SmallVector<int, 8> PermMask;
5279 SVOp->getMask(PermMask);
5280
Evan Chengace3c172008-07-22 21:13:36 +00005281 unsigned NumHi = 0;
5282 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005283 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005284 int Idx = PermMask[i];
5285 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005286 Locs[i] = std::make_pair(-1, -1);
5287 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5289 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005290 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005292 NumLo++;
5293 } else {
5294 Locs[i] = std::make_pair(1, NumHi);
5295 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005296 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005297 NumHi++;
5298 }
5299 }
5300 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005301
Evan Chengace3c172008-07-22 21:13:36 +00005302 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005303 // If no more than two elements come from either vector. This can be
5304 // implemented with two shuffles. First shuffle gather the elements.
5305 // The second shuffle, which takes the first shuffle as both of its
5306 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005308
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005310
Evan Chengace3c172008-07-22 21:13:36 +00005311 for (unsigned i = 0; i != 4; ++i) {
5312 if (Locs[i].first == -1)
5313 continue;
5314 else {
5315 unsigned Idx = (i < 2) ? 0 : 4;
5316 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005318 }
5319 }
5320
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005322 } else if (NumLo == 3 || NumHi == 3) {
5323 // Otherwise, we must have three elements from one vector, call it X, and
5324 // one element from the other, call it Y. First, use a shufps to build an
5325 // intermediate vector with the one element from Y and the element from X
5326 // that will be in the same half in the final destination (the indexes don't
5327 // matter). Then, use a shufps to build the final vector, taking the half
5328 // containing the element from Y from the intermediate, and the other half
5329 // from X.
5330 if (NumHi == 3) {
5331 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005332 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005333 std::swap(V1, V2);
5334 }
5335
5336 // Find the element from V2.
5337 unsigned HiIndex;
5338 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005339 int Val = PermMask[HiIndex];
5340 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005341 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005342 if (Val >= 4)
5343 break;
5344 }
5345
Nate Begeman9008ca62009-04-27 18:41:29 +00005346 Mask1[0] = PermMask[HiIndex];
5347 Mask1[1] = -1;
5348 Mask1[2] = PermMask[HiIndex^1];
5349 Mask1[3] = -1;
5350 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005351
5352 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005353 Mask1[0] = PermMask[0];
5354 Mask1[1] = PermMask[1];
5355 Mask1[2] = HiIndex & 1 ? 6 : 4;
5356 Mask1[3] = HiIndex & 1 ? 4 : 6;
5357 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005358 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005359 Mask1[0] = HiIndex & 1 ? 2 : 0;
5360 Mask1[1] = HiIndex & 1 ? 0 : 2;
5361 Mask1[2] = PermMask[2];
5362 Mask1[3] = PermMask[3];
5363 if (Mask1[2] >= 0)
5364 Mask1[2] += 4;
5365 if (Mask1[3] >= 0)
5366 Mask1[3] += 4;
5367 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005368 }
Evan Chengace3c172008-07-22 21:13:36 +00005369 }
5370
5371 // Break it into (shuffle shuffle_hi, shuffle_lo).
5372 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005373 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005374 SmallVector<int,8> LoMask(4U, -1);
5375 SmallVector<int,8> HiMask(4U, -1);
5376
5377 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005378 unsigned MaskIdx = 0;
5379 unsigned LoIdx = 0;
5380 unsigned HiIdx = 2;
5381 for (unsigned i = 0; i != 4; ++i) {
5382 if (i == 2) {
5383 MaskPtr = &HiMask;
5384 MaskIdx = 1;
5385 LoIdx = 0;
5386 HiIdx = 2;
5387 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005388 int Idx = PermMask[i];
5389 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005390 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005391 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005392 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005393 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005394 LoIdx++;
5395 } else {
5396 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005397 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005398 HiIdx++;
5399 }
5400 }
5401
Nate Begeman9008ca62009-04-27 18:41:29 +00005402 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5403 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5404 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005405 for (unsigned i = 0; i != 4; ++i) {
5406 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005407 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005408 } else {
5409 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005410 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005411 }
5412 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005413 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005414}
5415
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005416static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005417 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005418 V = V.getOperand(0);
5419 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5420 V = V.getOperand(0);
5421 if (MayFoldLoad(V))
5422 return true;
5423 return false;
5424}
5425
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005426// FIXME: the version above should always be used. Since there's
5427// a bug where several vector shuffles can't be folded because the
5428// DAG is not updated during lowering and a node claims to have two
5429// uses while it only has one, use this version, and let isel match
5430// another instruction if the load really happens to have more than
5431// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005432// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005433static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005434 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005435 V = V.getOperand(0);
5436 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5437 V = V.getOperand(0);
5438 if (ISD::isNormalLoad(V.getNode()))
5439 return true;
5440 return false;
5441}
5442
5443/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5444/// a vector extract, and if both can be later optimized into a single load.
5445/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5446/// here because otherwise a target specific shuffle node is going to be
5447/// emitted for this shuffle, and the optimization not done.
5448/// FIXME: This is probably not the best approach, but fix the problem
5449/// until the right path is decided.
5450static
5451bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5452 const TargetLowering &TLI) {
5453 EVT VT = V.getValueType();
5454 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5455
5456 // Be sure that the vector shuffle is present in a pattern like this:
5457 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5458 if (!V.hasOneUse())
5459 return false;
5460
5461 SDNode *N = *V.getNode()->use_begin();
5462 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5463 return false;
5464
5465 SDValue EltNo = N->getOperand(1);
5466 if (!isa<ConstantSDNode>(EltNo))
5467 return false;
5468
5469 // If the bit convert changed the number of elements, it is unsafe
5470 // to examine the mask.
5471 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005472 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005473 EVT SrcVT = V.getOperand(0).getValueType();
5474 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5475 return false;
5476 V = V.getOperand(0);
5477 HasShuffleIntoBitcast = true;
5478 }
5479
5480 // Select the input vector, guarding against out of range extract vector.
5481 unsigned NumElems = VT.getVectorNumElements();
5482 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5483 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5484 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5485
5486 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005487 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005488 V = V.getOperand(0);
5489
5490 if (ISD::isNormalLoad(V.getNode())) {
5491 // Is the original load suitable?
5492 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5493
5494 // FIXME: avoid the multi-use bug that is preventing lots of
5495 // of foldings to be detected, this is still wrong of course, but
5496 // give the temporary desired behavior, and if it happens that
5497 // the load has real more uses, during isel it will not fold, and
5498 // will generate poor code.
5499 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5500 return false;
5501
5502 if (!HasShuffleIntoBitcast)
5503 return true;
5504
5505 // If there's a bitcast before the shuffle, check if the load type and
5506 // alignment is valid.
5507 unsigned Align = LN0->getAlignment();
5508 unsigned NewAlign =
5509 TLI.getTargetData()->getABITypeAlignment(
5510 VT.getTypeForEVT(*DAG.getContext()));
5511
5512 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5513 return false;
5514 }
5515
5516 return true;
5517}
5518
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005519static
Evan Cheng835580f2010-10-07 20:50:20 +00005520SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5521 EVT VT = Op.getValueType();
5522
5523 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005524 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5525 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005526 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5527 V1, DAG));
5528}
5529
5530static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005531SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5532 bool HasSSE2) {
5533 SDValue V1 = Op.getOperand(0);
5534 SDValue V2 = Op.getOperand(1);
5535 EVT VT = Op.getValueType();
5536
5537 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5538
5539 if (HasSSE2 && VT == MVT::v2f64)
5540 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5541
5542 // v4f32 or v4i32
5543 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5544}
5545
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005546static
5547SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5548 SDValue V1 = Op.getOperand(0);
5549 SDValue V2 = Op.getOperand(1);
5550 EVT VT = Op.getValueType();
5551
5552 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5553 "unsupported shuffle type");
5554
5555 if (V2.getOpcode() == ISD::UNDEF)
5556 V2 = V1;
5557
5558 // v4i32 or v4f32
5559 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5560}
5561
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005562static
5563SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5564 SDValue V1 = Op.getOperand(0);
5565 SDValue V2 = Op.getOperand(1);
5566 EVT VT = Op.getValueType();
5567 unsigned NumElems = VT.getVectorNumElements();
5568
5569 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5570 // operand of these instructions is only memory, so check if there's a
5571 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5572 // same masks.
5573 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005574
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005575 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005576 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005577 CanFoldLoad = true;
5578
5579 // When V1 is a load, it can be folded later into a store in isel, example:
5580 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5581 // turns into:
5582 // (MOVLPSmr addr:$src1, VR128:$src2)
5583 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005584 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005585 CanFoldLoad = true;
5586
Eric Christopher893a8822011-02-20 05:04:42 +00005587 // Both of them can't be memory operations though.
5588 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5589 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005590
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005591 if (CanFoldLoad) {
5592 if (HasSSE2 && NumElems == 2)
5593 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5594
5595 if (NumElems == 4)
5596 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5597 }
5598
5599 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5600 // movl and movlp will both match v2i64, but v2i64 is never matched by
5601 // movl earlier because we make it strict to avoid messing with the movlp load
5602 // folding logic (see the code above getMOVLP call). Match it here then,
5603 // this is horrible, but will stay like this until we move all shuffle
5604 // matching to x86 specific nodes. Note that for the 1st condition all
5605 // types are matched with movsd.
5606 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5607 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5608 else if (HasSSE2)
5609 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5610
5611
5612 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5613
5614 // Invert the operand order and use SHUFPS to match it.
5615 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5616 X86::getShuffleSHUFImmediate(SVOp), DAG);
5617}
5618
David Greenec4db4e52011-02-28 19:06:56 +00005619static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005620 switch(VT.getSimpleVT().SimpleTy) {
5621 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5622 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005623 case MVT::v4f32:
5624 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5625 case MVT::v2f64:
5626 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5627 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5628 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005629 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5630 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5631 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005632 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005633 }
5634 return 0;
5635}
5636
5637static inline unsigned getUNPCKHOpcode(EVT VT) {
5638 switch(VT.getSimpleVT().SimpleTy) {
5639 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5640 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5641 case MVT::v4f32: return X86ISD::UNPCKHPS;
5642 case MVT::v2f64: return X86ISD::UNPCKHPD;
5643 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5644 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5645 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005646 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005647 }
5648 return 0;
5649}
5650
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005651static
5652SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005653 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005654 const X86Subtarget *Subtarget) {
5655 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5656 EVT VT = Op.getValueType();
5657 DebugLoc dl = Op.getDebugLoc();
5658 SDValue V1 = Op.getOperand(0);
5659 SDValue V2 = Op.getOperand(1);
5660
5661 if (isZeroShuffle(SVOp))
5662 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5663
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005664 // Handle splat operations
5665 if (SVOp->isSplat()) {
5666 // Special case, this is the only place now where it's
5667 // allowed to return a vector_shuffle operation without
5668 // using a target specific node, because *hopefully* it
5669 // will be optimized away by the dag combiner.
5670 if (VT.getVectorNumElements() <= 4 &&
5671 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5672 return Op;
5673
5674 // Handle splats by matching through known masks
5675 if (VT.getVectorNumElements() <= 4)
5676 return SDValue();
5677
Evan Cheng835580f2010-10-07 20:50:20 +00005678 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005679 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005680 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005681
5682 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5683 // do it!
5684 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5685 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5686 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005687 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005688 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5689 // FIXME: Figure out a cleaner way to do this.
5690 // Try to make use of movq to zero out the top part.
5691 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5692 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5693 if (NewOp.getNode()) {
5694 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5695 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5696 DAG, Subtarget, dl);
5697 }
5698 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5699 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5700 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5701 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5702 DAG, Subtarget, dl);
5703 }
5704 }
5705 return SDValue();
5706}
5707
Dan Gohman475871a2008-07-27 21:46:04 +00005708SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005709X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005710 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005711 SDValue V1 = Op.getOperand(0);
5712 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005713 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005714 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005716 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5718 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005719 bool V1IsSplat = false;
5720 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005721 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005722 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005723 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005724 MachineFunction &MF = DAG.getMachineFunction();
5725 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726
Dale Johannesen0488fb62010-09-30 23:57:10 +00005727 // Shuffle operations on MMX not supported.
5728 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005729 return Op;
5730
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005731 // Vector shuffle lowering takes 3 steps:
5732 //
5733 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5734 // narrowing and commutation of operands should be handled.
5735 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5736 // shuffle nodes.
5737 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5738 // so the shuffle can be broken into other shuffles and the legalizer can
5739 // try the lowering again.
5740 //
5741 // The general ideia is that no vector_shuffle operation should be left to
5742 // be matched during isel, all of them must be converted to a target specific
5743 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005744
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005745 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5746 // narrowing and commutation of operands should be handled. The actual code
5747 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005748 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005749 if (NewOp.getNode())
5750 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005751
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005752 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5753 // unpckh_undef). Only use pshufd if speed is more important than size.
5754 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5755 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005756 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005757 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5758 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5759 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005760
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005761 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005762 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005763 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005764
Dale Johannesen0488fb62010-09-30 23:57:10 +00005765 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005766 return getMOVHighToLow(Op, dl, DAG);
5767
5768 // Use to match splats
5769 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5770 (VT == MVT::v2f64 || VT == MVT::v2i64))
5771 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5772
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005773 if (X86::isPSHUFDMask(SVOp)) {
5774 // The actual implementation will match the mask in the if above and then
5775 // during isel it can match several different instructions, not only pshufd
5776 // as its name says, sad but true, emulate the behavior for now...
5777 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5778 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5779
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005780 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5781
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005782 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005783 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5784
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005785 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005786 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5787 TargetMask, DAG);
5788
5789 if (VT == MVT::v4f32)
5790 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5791 TargetMask, DAG);
5792 }
Eric Christopherfd179292009-08-27 18:07:15 +00005793
Evan Chengf26ffe92008-05-29 08:22:04 +00005794 // Check if this can be converted into a logical shift.
5795 bool isLeft = false;
5796 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005797 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005798 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005799 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005800 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005801 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005802 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005803 EVT EltVT = VT.getVectorElementType();
5804 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005805 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005806 }
Eric Christopherfd179292009-08-27 18:07:15 +00005807
Nate Begeman9008ca62009-04-27 18:41:29 +00005808 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005809 if (V1IsUndef)
5810 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005811 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005812 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005813 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005814 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005815 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5816
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005817 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005818 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5819 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005820 }
Eric Christopherfd179292009-08-27 18:07:15 +00005821
Nate Begeman9008ca62009-04-27 18:41:29 +00005822 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005823 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5824 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005825
Dale Johannesen0488fb62010-09-30 23:57:10 +00005826 if (X86::isMOVHLPSMask(SVOp))
5827 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005828
Dale Johannesen0488fb62010-09-30 23:57:10 +00005829 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5830 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005831
Dale Johannesen0488fb62010-09-30 23:57:10 +00005832 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5833 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005834
Dale Johannesen0488fb62010-09-30 23:57:10 +00005835 if (X86::isMOVLPMask(SVOp))
5836 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005837
Nate Begeman9008ca62009-04-27 18:41:29 +00005838 if (ShouldXformToMOVHLPS(SVOp) ||
5839 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5840 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005841
Evan Chengf26ffe92008-05-29 08:22:04 +00005842 if (isShift) {
5843 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005844 EVT EltVT = VT.getVectorElementType();
5845 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005846 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005847 }
Eric Christopherfd179292009-08-27 18:07:15 +00005848
Evan Cheng9eca5e82006-10-25 21:49:50 +00005849 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005850 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5851 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005852 V1IsSplat = isSplatVector(V1.getNode());
5853 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005854
Chris Lattner8a594482007-11-25 00:24:49 +00005855 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005856 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005857 Op = CommuteVectorShuffle(SVOp, DAG);
5858 SVOp = cast<ShuffleVectorSDNode>(Op);
5859 V1 = SVOp->getOperand(0);
5860 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005861 std::swap(V1IsSplat, V2IsSplat);
5862 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005863 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005864 }
5865
Nate Begeman9008ca62009-04-27 18:41:29 +00005866 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5867 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005868 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005869 return V1;
5870 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5871 // the instruction selector will not match, so get a canonical MOVL with
5872 // swapped operands to undo the commute.
5873 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005874 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005876 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005877 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5878 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005879
5880 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005881 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005882
Evan Cheng9bbbb982006-10-25 20:48:19 +00005883 if (V2IsSplat) {
5884 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005885 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005886 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005887 SDValue NewMask = NormalizeMask(SVOp, DAG);
5888 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5889 if (NSVOp != SVOp) {
5890 if (X86::isUNPCKLMask(NSVOp, true)) {
5891 return NewMask;
5892 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5893 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005894 }
5895 }
5896 }
5897
Evan Cheng9eca5e82006-10-25 21:49:50 +00005898 if (Commuted) {
5899 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005900 // FIXME: this seems wrong.
5901 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5902 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005903
5904 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005905 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5906 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005907
5908 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005909 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005910 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005911
Nate Begeman9008ca62009-04-27 18:41:29 +00005912 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005913 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005914 return CommuteVectorShuffle(SVOp, DAG);
5915
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005916 // The checks below are all present in isShuffleMaskLegal, but they are
5917 // inlined here right now to enable us to directly emit target specific
5918 // nodes, and remove one by one until they don't return Op anymore.
5919 SmallVector<int, 16> M;
5920 SVOp->getMask(M);
5921
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005922 if (isPALIGNRMask(M, VT, HasSSSE3))
5923 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5924 X86::getShufflePALIGNRImmediate(SVOp),
5925 DAG);
5926
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005927 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5928 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005929 if (VT == MVT::v2f64) {
5930 X86ISD::NodeType Opcode =
5931 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5932 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5933 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005934 if (VT == MVT::v2i64)
5935 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5936 }
5937
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005938 if (isPSHUFHWMask(M, VT))
5939 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5940 X86::getShufflePSHUFHWImmediate(SVOp),
5941 DAG);
5942
5943 if (isPSHUFLWMask(M, VT))
5944 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5945 X86::getShufflePSHUFLWImmediate(SVOp),
5946 DAG);
5947
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005948 if (isSHUFPMask(M, VT)) {
5949 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5950 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5951 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5952 TargetMask, DAG);
5953 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5954 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5955 TargetMask, DAG);
5956 }
5957
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005958 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5959 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005960 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5961 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005962 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5963 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5964 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5965
Evan Cheng14b32e12007-12-11 01:46:18 +00005966 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005967 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005968 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005969 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005970 return NewOp;
5971 }
5972
Owen Anderson825b72b2009-08-11 20:47:22 +00005973 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005974 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 if (NewOp.getNode())
5976 return NewOp;
5977 }
Eric Christopherfd179292009-08-27 18:07:15 +00005978
Dale Johannesen0488fb62010-09-30 23:57:10 +00005979 // Handle all 4 wide cases with a number of shuffles.
5980 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005981 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005982
Dan Gohman475871a2008-07-27 21:46:04 +00005983 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005984}
5985
Dan Gohman475871a2008-07-27 21:46:04 +00005986SDValue
5987X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005988 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005989 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005990 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005991 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005993 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005995 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005996 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005997 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005998 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5999 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6000 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6002 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006003 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006005 Op.getOperand(0)),
6006 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006008 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006010 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006011 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006013 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6014 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006015 // result has a single use which is a store or a bitcast to i32. And in
6016 // the case of a store, it's not worth it if the index is a constant 0,
6017 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006018 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006019 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006020 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006021 if ((User->getOpcode() != ISD::STORE ||
6022 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6023 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006024 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006025 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006026 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006027 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006028 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006029 Op.getOperand(0)),
6030 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006031 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00006032 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006033 // ExtractPS works with constant index.
6034 if (isa<ConstantSDNode>(Op.getOperand(1)))
6035 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006036 }
Dan Gohman475871a2008-07-27 21:46:04 +00006037 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006038}
6039
6040
Dan Gohman475871a2008-07-27 21:46:04 +00006041SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006042X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6043 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006044 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006045 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006046
David Greene74a579d2011-02-10 16:57:36 +00006047 SDValue Vec = Op.getOperand(0);
6048 EVT VecVT = Vec.getValueType();
6049
6050 // If this is a 256-bit vector result, first extract the 128-bit
6051 // vector and then extract from the 128-bit vector.
6052 if (VecVT.getSizeInBits() > 128) {
6053 DebugLoc dl = Op.getNode()->getDebugLoc();
6054 unsigned NumElems = VecVT.getVectorNumElements();
6055 SDValue Idx = Op.getOperand(1);
6056
6057 if (!isa<ConstantSDNode>(Idx))
6058 return SDValue();
6059
6060 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6061 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6062
6063 // Get the 128-bit vector.
6064 bool Upper = IdxVal >= ExtractNumElems;
6065 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6066
6067 // Extract from it.
6068 SDValue ScaledIdx = Idx;
6069 if (Upper)
6070 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6071 DAG.getConstant(ExtractNumElems,
6072 Idx.getValueType()));
6073 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6074 ScaledIdx);
6075 }
6076
6077 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6078
Evan Cheng62a3f152008-03-24 21:52:23 +00006079 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006080 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006081 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006082 return Res;
6083 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006084
Owen Andersone50ed302009-08-10 22:56:29 +00006085 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006086 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006087 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006088 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006089 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006090 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006091 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006092 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6093 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006094 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006095 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006096 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006097 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006098 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006099 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006100 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006101 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006102 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006103 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006104 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006105 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006106 if (Idx == 0)
6107 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006108
Evan Cheng0db9fe62006-04-25 20:13:52 +00006109 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006110 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006111 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006112 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006114 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006115 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006116 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006117 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6118 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6119 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006120 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006121 if (Idx == 0)
6122 return Op;
6123
6124 // UNPCKHPD the element to the lowest double word, then movsd.
6125 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6126 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006128 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006129 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006130 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006131 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006132 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006133 }
6134
Dan Gohman475871a2008-07-27 21:46:04 +00006135 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006136}
6137
Dan Gohman475871a2008-07-27 21:46:04 +00006138SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006139X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6140 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006141 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006142 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006143 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006144
Dan Gohman475871a2008-07-27 21:46:04 +00006145 SDValue N0 = Op.getOperand(0);
6146 SDValue N1 = Op.getOperand(1);
6147 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006148
Dan Gohman8a55ce42009-09-23 21:02:20 +00006149 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006150 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006151 unsigned Opc;
6152 if (VT == MVT::v8i16)
6153 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006154 else if (VT == MVT::v16i8)
6155 Opc = X86ISD::PINSRB;
6156 else
6157 Opc = X86ISD::PINSRB;
6158
Nate Begeman14d12ca2008-02-11 04:19:36 +00006159 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6160 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006161 if (N1.getValueType() != MVT::i32)
6162 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6163 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006164 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006165 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006166 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006167 // Bits [7:6] of the constant are the source select. This will always be
6168 // zero here. The DAG Combiner may combine an extract_elt index into these
6169 // bits. For example (insert (extract, 3), 2) could be matched by putting
6170 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006171 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006172 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006173 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006174 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006175 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006176 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006177 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006178 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006179 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006180 // PINSR* works with constant index.
6181 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006182 }
Dan Gohman475871a2008-07-27 21:46:04 +00006183 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006184}
6185
Dan Gohman475871a2008-07-27 21:46:04 +00006186SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006187X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006188 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006189 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006190
David Greene6b381262011-02-09 15:32:06 +00006191 DebugLoc dl = Op.getDebugLoc();
6192 SDValue N0 = Op.getOperand(0);
6193 SDValue N1 = Op.getOperand(1);
6194 SDValue N2 = Op.getOperand(2);
6195
6196 // If this is a 256-bit vector result, first insert into a 128-bit
6197 // vector and then insert into the 256-bit vector.
6198 if (VT.getSizeInBits() > 128) {
6199 if (!isa<ConstantSDNode>(N2))
6200 return SDValue();
6201
6202 // Get the 128-bit vector.
6203 unsigned NumElems = VT.getVectorNumElements();
6204 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6205 bool Upper = IdxVal >= NumElems / 2;
6206
6207 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6208
6209 // Insert into it.
6210 SDValue ScaledN2 = N2;
6211 if (Upper)
6212 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006213 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006214 (VT.getSizeInBits() / 128),
6215 N2.getValueType()));
6216 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6217 N1, ScaledN2);
6218
6219 // Insert the 128-bit vector
6220 // FIXME: Why UNDEF?
6221 return Insert128BitVector(N0, Op, N2, DAG, dl);
6222 }
6223
Nate Begeman14d12ca2008-02-11 04:19:36 +00006224 if (Subtarget->hasSSE41())
6225 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6226
Dan Gohman8a55ce42009-09-23 21:02:20 +00006227 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006228 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006229
Dan Gohman8a55ce42009-09-23 21:02:20 +00006230 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006231 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6232 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006233 if (N1.getValueType() != MVT::i32)
6234 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6235 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006236 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006237 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006238 }
Dan Gohman475871a2008-07-27 21:46:04 +00006239 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006240}
6241
Dan Gohman475871a2008-07-27 21:46:04 +00006242SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006243X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006244 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006245 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006246 EVT OpVT = Op.getValueType();
6247
6248 // If this is a 256-bit vector result, first insert into a 128-bit
6249 // vector and then insert into the 256-bit vector.
6250 if (OpVT.getSizeInBits() > 128) {
6251 // Insert into a 128-bit vector.
6252 EVT VT128 = EVT::getVectorVT(*Context,
6253 OpVT.getVectorElementType(),
6254 OpVT.getVectorNumElements() / 2);
6255
6256 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6257
6258 // Insert the 128-bit vector.
6259 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6260 DAG.getConstant(0, MVT::i32),
6261 DAG, dl);
6262 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006263
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006264 if (Op.getValueType() == MVT::v1i64 &&
6265 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006266 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006267
Owen Anderson825b72b2009-08-11 20:47:22 +00006268 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006269 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6270 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006271 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006272 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006273}
6274
David Greene91585092011-01-26 15:38:49 +00006275// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6276// a simple subregister reference or explicit instructions to grab
6277// upper bits of a vector.
6278SDValue
6279X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6280 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006281 DebugLoc dl = Op.getNode()->getDebugLoc();
6282 SDValue Vec = Op.getNode()->getOperand(0);
6283 SDValue Idx = Op.getNode()->getOperand(1);
6284
6285 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6286 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6287 return Extract128BitVector(Vec, Idx, DAG, dl);
6288 }
David Greene91585092011-01-26 15:38:49 +00006289 }
6290 return SDValue();
6291}
6292
David Greenecfe33c42011-01-26 19:13:22 +00006293// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6294// simple superregister reference or explicit instructions to insert
6295// the upper bits of a vector.
6296SDValue
6297X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6298 if (Subtarget->hasAVX()) {
6299 DebugLoc dl = Op.getNode()->getDebugLoc();
6300 SDValue Vec = Op.getNode()->getOperand(0);
6301 SDValue SubVec = Op.getNode()->getOperand(1);
6302 SDValue Idx = Op.getNode()->getOperand(2);
6303
6304 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6305 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006306 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006307 }
6308 }
6309 return SDValue();
6310}
6311
Bill Wendling056292f2008-09-16 21:48:12 +00006312// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6313// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6314// one of the above mentioned nodes. It has to be wrapped because otherwise
6315// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6316// be used to form addressing mode. These wrapped nodes will be selected
6317// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006318SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006319X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006320 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006321
Chris Lattner41621a22009-06-26 19:22:52 +00006322 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6323 // global base reg.
6324 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006325 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006326 CodeModel::Model M = getTargetMachine().getCodeModel();
6327
Chris Lattner4f066492009-07-11 20:29:19 +00006328 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006329 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006330 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006331 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006332 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006333 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006334 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006335
Evan Cheng1606e8e2009-03-13 07:51:59 +00006336 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006337 CP->getAlignment(),
6338 CP->getOffset(), OpFlag);
6339 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006340 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006341 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006342 if (OpFlag) {
6343 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006344 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006345 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006346 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006347 }
6348
6349 return Result;
6350}
6351
Dan Gohmand858e902010-04-17 15:26:15 +00006352SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006353 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006354
Chris Lattner18c59872009-06-27 04:16:01 +00006355 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6356 // global base reg.
6357 unsigned char OpFlag = 0;
6358 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006359 CodeModel::Model M = getTargetMachine().getCodeModel();
6360
Chris Lattner4f066492009-07-11 20:29:19 +00006361 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006362 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006363 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006364 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006365 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006366 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006367 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006368
Chris Lattner18c59872009-06-27 04:16:01 +00006369 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6370 OpFlag);
6371 DebugLoc DL = JT->getDebugLoc();
6372 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006373
Chris Lattner18c59872009-06-27 04:16:01 +00006374 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006375 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006376 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6377 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006378 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006379 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006380
Chris Lattner18c59872009-06-27 04:16:01 +00006381 return Result;
6382}
6383
6384SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006385X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006386 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006387
Chris Lattner18c59872009-06-27 04:16:01 +00006388 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6389 // global base reg.
6390 unsigned char OpFlag = 0;
6391 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006392 CodeModel::Model M = getTargetMachine().getCodeModel();
6393
Chris Lattner4f066492009-07-11 20:29:19 +00006394 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006395 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006396 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006397 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006398 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006399 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006400 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006401
Chris Lattner18c59872009-06-27 04:16:01 +00006402 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006403
Chris Lattner18c59872009-06-27 04:16:01 +00006404 DebugLoc DL = Op.getDebugLoc();
6405 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006406
6407
Chris Lattner18c59872009-06-27 04:16:01 +00006408 // With PIC, the address is actually $g + Offset.
6409 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006410 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006411 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6412 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006413 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006414 Result);
6415 }
Eric Christopherfd179292009-08-27 18:07:15 +00006416
Chris Lattner18c59872009-06-27 04:16:01 +00006417 return Result;
6418}
6419
Dan Gohman475871a2008-07-27 21:46:04 +00006420SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006421X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006422 // Create the TargetBlockAddressAddress node.
6423 unsigned char OpFlags =
6424 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006425 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006426 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006427 DebugLoc dl = Op.getDebugLoc();
6428 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6429 /*isTarget=*/true, OpFlags);
6430
Dan Gohmanf705adb2009-10-30 01:28:02 +00006431 if (Subtarget->isPICStyleRIPRel() &&
6432 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006433 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6434 else
6435 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006436
Dan Gohman29cbade2009-11-20 23:18:13 +00006437 // With PIC, the address is actually $g + Offset.
6438 if (isGlobalRelativeToPICBase(OpFlags)) {
6439 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6440 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6441 Result);
6442 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006443
6444 return Result;
6445}
6446
6447SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006448X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006449 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006450 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006451 // Create the TargetGlobalAddress node, folding in the constant
6452 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006453 unsigned char OpFlags =
6454 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006455 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006456 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006457 if (OpFlags == X86II::MO_NO_FLAG &&
6458 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006459 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006460 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006461 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006462 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006463 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006464 }
Eric Christopherfd179292009-08-27 18:07:15 +00006465
Chris Lattner4f066492009-07-11 20:29:19 +00006466 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006467 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006468 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6469 else
6470 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006471
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006472 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006473 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006474 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6475 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006476 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006478
Chris Lattner36c25012009-07-10 07:34:39 +00006479 // For globals that require a load from a stub to get the address, emit the
6480 // load.
6481 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006482 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006483 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484
Dan Gohman6520e202008-10-18 02:06:02 +00006485 // If there was a non-zero offset that we didn't fold, create an explicit
6486 // addition for it.
6487 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006488 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006489 DAG.getConstant(Offset, getPointerTy()));
6490
Evan Cheng0db9fe62006-04-25 20:13:52 +00006491 return Result;
6492}
6493
Evan Chengda43bcf2008-09-24 00:05:32 +00006494SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006495X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006496 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006497 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006498 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006499}
6500
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006501static SDValue
6502GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006503 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006504 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006505 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006506 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006507 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006508 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006509 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006510 GA->getOffset(),
6511 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006512 if (InFlag) {
6513 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006514 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006515 } else {
6516 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006517 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006518 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006519
6520 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006521 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006522
Rafael Espindola15f1b662009-04-24 12:59:40 +00006523 SDValue Flag = Chain.getValue(1);
6524 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006525}
6526
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006527// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006528static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006529LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006530 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006531 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006532 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6533 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006534 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006535 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006536 InFlag = Chain.getValue(1);
6537
Chris Lattnerb903bed2009-06-26 21:20:29 +00006538 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006539}
6540
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006541// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006542static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006543LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006544 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006545 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6546 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006547}
6548
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006549// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6550// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006551static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006552 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006553 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006554 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006555
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006556 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6557 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6558 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006559
Michael J. Spencerec38de22010-10-10 22:04:20 +00006560 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006561 DAG.getIntPtrConstant(0),
6562 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006563
Chris Lattnerb903bed2009-06-26 21:20:29 +00006564 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006565 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6566 // initialexec.
6567 unsigned WrapperKind = X86ISD::Wrapper;
6568 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006569 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006570 } else if (is64Bit) {
6571 assert(model == TLSModel::InitialExec);
6572 OperandFlags = X86II::MO_GOTTPOFF;
6573 WrapperKind = X86ISD::WrapperRIP;
6574 } else {
6575 assert(model == TLSModel::InitialExec);
6576 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006577 }
Eric Christopherfd179292009-08-27 18:07:15 +00006578
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006579 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6580 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006581 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006582 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006583 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006584 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006585
Rafael Espindola9a580232009-02-27 13:37:18 +00006586 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006587 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006588 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006589
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006590 // The address of the thread local variable is the add of the thread
6591 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006592 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006593}
6594
Dan Gohman475871a2008-07-27 21:46:04 +00006595SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006596X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006597
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006598 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006599 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006600
Eric Christopher30ef0e52010-06-03 04:07:48 +00006601 if (Subtarget->isTargetELF()) {
6602 // TODO: implement the "local dynamic" model
6603 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006604
Eric Christopher30ef0e52010-06-03 04:07:48 +00006605 // If GV is an alias then use the aliasee for determining
6606 // thread-localness.
6607 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6608 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006609
6610 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006611 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006612
Eric Christopher30ef0e52010-06-03 04:07:48 +00006613 switch (model) {
6614 case TLSModel::GeneralDynamic:
6615 case TLSModel::LocalDynamic: // not implemented
6616 if (Subtarget->is64Bit())
6617 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6618 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006619
Eric Christopher30ef0e52010-06-03 04:07:48 +00006620 case TLSModel::InitialExec:
6621 case TLSModel::LocalExec:
6622 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6623 Subtarget->is64Bit());
6624 }
6625 } else if (Subtarget->isTargetDarwin()) {
6626 // Darwin only has one model of TLS. Lower to that.
6627 unsigned char OpFlag = 0;
6628 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6629 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006630
Eric Christopher30ef0e52010-06-03 04:07:48 +00006631 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6632 // global base reg.
6633 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6634 !Subtarget->is64Bit();
6635 if (PIC32)
6636 OpFlag = X86II::MO_TLVP_PIC_BASE;
6637 else
6638 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006639 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006640 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006641 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006642 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006643 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006644
Eric Christopher30ef0e52010-06-03 04:07:48 +00006645 // With PIC32, the address is actually $g + Offset.
6646 if (PIC32)
6647 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6648 DAG.getNode(X86ISD::GlobalBaseReg,
6649 DebugLoc(), getPointerTy()),
6650 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006651
Eric Christopher30ef0e52010-06-03 04:07:48 +00006652 // Lowering the machine isd will make sure everything is in the right
6653 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006654 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006655 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006656 SDValue Args[] = { Chain, Offset };
6657 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006658
Eric Christopher30ef0e52010-06-03 04:07:48 +00006659 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6660 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6661 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006662
Eric Christopher30ef0e52010-06-03 04:07:48 +00006663 // And our return value (tls address) is in the standard call return value
6664 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006665 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6666 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006667 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006668
Eric Christopher30ef0e52010-06-03 04:07:48 +00006669 assert(false &&
6670 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006671
Torok Edwinc23197a2009-07-14 16:55:14 +00006672 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006673 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006674}
6675
Evan Cheng0db9fe62006-04-25 20:13:52 +00006676
Nadav Rotem43012222011-05-11 08:12:09 +00006677/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006678/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006679SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006680 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006681 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006682 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006683 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006684 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006685 SDValue ShOpLo = Op.getOperand(0);
6686 SDValue ShOpHi = Op.getOperand(1);
6687 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006688 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006689 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006690 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006691
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006693 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006694 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6695 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006696 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006697 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6698 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006699 }
Evan Chenge3413162006-01-09 18:33:28 +00006700
Owen Anderson825b72b2009-08-11 20:47:22 +00006701 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6702 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006703 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006704 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006705
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006708 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6709 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006710
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006711 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006712 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6713 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006714 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006715 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6716 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006717 }
6718
Dan Gohman475871a2008-07-27 21:46:04 +00006719 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006720 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721}
Evan Chenga3195e82006-01-12 22:54:21 +00006722
Dan Gohmand858e902010-04-17 15:26:15 +00006723SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6724 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006725 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006726
Dale Johannesen0488fb62010-09-30 23:57:10 +00006727 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006728 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006729
Owen Anderson825b72b2009-08-11 20:47:22 +00006730 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006731 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006732
Eli Friedman36df4992009-05-27 00:47:34 +00006733 // These are really Legal; return the operand so the caller accepts it as
6734 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006735 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006736 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006737 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006738 Subtarget->is64Bit()) {
6739 return Op;
6740 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006741
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006742 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006743 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006745 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006746 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006747 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006748 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006749 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006750 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006751 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6752}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006753
Owen Andersone50ed302009-08-10 22:56:29 +00006754SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006755 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006756 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006757 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006758 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006759 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006760 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006761 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006762 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006763 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006764 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006765
Chris Lattner492a43e2010-09-22 01:28:21 +00006766 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006767
Chris Lattner492a43e2010-09-22 01:28:21 +00006768 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6769 MachineMemOperand *MMO =
6770 DAG.getMachineFunction()
6771 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6772 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006773
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006774 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006775 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6776 X86ISD::FILD, DL,
6777 Tys, Ops, array_lengthof(Ops),
6778 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006779
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006780 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006782 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006783
6784 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6785 // shouldn't be necessary except that RFP cannot be live across
6786 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006787 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006788 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6789 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006790 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006791 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006792 SDValue Ops[] = {
6793 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6794 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006795 MachineMemOperand *MMO =
6796 DAG.getMachineFunction()
6797 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006798 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006799
Chris Lattner492a43e2010-09-22 01:28:21 +00006800 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6801 Ops, array_lengthof(Ops),
6802 Op.getValueType(), MMO);
6803 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006804 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006805 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006806 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006807
Evan Cheng0db9fe62006-04-25 20:13:52 +00006808 return Result;
6809}
6810
Bill Wendling8b8a6362009-01-17 03:56:04 +00006811// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006812SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6813 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006814 // This algorithm is not obvious. Here it is in C code, more or less:
6815 /*
6816 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6817 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6818 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006819
Bill Wendling8b8a6362009-01-17 03:56:04 +00006820 // Copy ints to xmm registers.
6821 __m128i xh = _mm_cvtsi32_si128( hi );
6822 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006823
Bill Wendling8b8a6362009-01-17 03:56:04 +00006824 // Combine into low half of a single xmm register.
6825 __m128i x = _mm_unpacklo_epi32( xh, xl );
6826 __m128d d;
6827 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006828
Bill Wendling8b8a6362009-01-17 03:56:04 +00006829 // Merge in appropriate exponents to give the integer bits the right
6830 // magnitude.
6831 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006832
Bill Wendling8b8a6362009-01-17 03:56:04 +00006833 // Subtract away the biases to deal with the IEEE-754 double precision
6834 // implicit 1.
6835 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006836
Bill Wendling8b8a6362009-01-17 03:56:04 +00006837 // All conversions up to here are exact. The correctly rounded result is
6838 // calculated using the current rounding mode using the following
6839 // horizontal add.
6840 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6841 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6842 // store doesn't really need to be here (except
6843 // maybe to zero the other double)
6844 return sd;
6845 }
6846 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006847
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006848 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006849 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006850
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006851 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006852 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006853 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6854 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6855 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6856 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006857 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006858 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006859
Bill Wendling8b8a6362009-01-17 03:56:04 +00006860 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006861 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006862 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006863 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006864 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006865 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006866 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006867
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6869 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006870 Op.getOperand(0),
6871 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006872 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6873 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006874 Op.getOperand(0),
6875 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006876 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6877 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006878 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006879 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006880 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006881 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006882 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006883 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006884 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006886
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006887 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006888 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6890 DAG.getUNDEF(MVT::v2f64), ShufMask);
6891 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6892 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006893 DAG.getIntPtrConstant(0));
6894}
6895
Bill Wendling8b8a6362009-01-17 03:56:04 +00006896// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006897SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6898 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006899 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006900 // FP constant to bias correct the final result.
6901 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006903
6904 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6906 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006907 Op.getOperand(0),
6908 DAG.getIntPtrConstant(0)));
6909
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006911 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006912 DAG.getIntPtrConstant(0));
6913
6914 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006915 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006916 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006917 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006919 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006920 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 MVT::v2f64, Bias)));
6922 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006923 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006924 DAG.getIntPtrConstant(0));
6925
6926 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006928
6929 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006930 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006931
Owen Anderson825b72b2009-08-11 20:47:22 +00006932 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006933 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006934 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006935 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006936 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006937 }
6938
6939 // Handle final rounding.
6940 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006941}
6942
Dan Gohmand858e902010-04-17 15:26:15 +00006943SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6944 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006945 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006946 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006947
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006948 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006949 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6950 // the optimization here.
6951 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006952 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006953
Owen Andersone50ed302009-08-10 22:56:29 +00006954 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006955 EVT DstVT = Op.getValueType();
6956 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006957 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006958 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006959 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006960
6961 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006962 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006963 if (SrcVT == MVT::i32) {
6964 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6965 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6966 getPointerTy(), StackSlot, WordOff);
6967 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006968 StackSlot, MachinePointerInfo(),
6969 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006970 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006971 OffsetSlot, MachinePointerInfo(),
6972 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006973 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6974 return Fild;
6975 }
6976
6977 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6978 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006979 StackSlot, MachinePointerInfo(),
6980 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006981 // For i64 source, we need to add the appropriate power of 2 if the input
6982 // was negative. This is the same as the optimization in
6983 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6984 // we must be careful to do the computation in x87 extended precision, not
6985 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006986 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6987 MachineMemOperand *MMO =
6988 DAG.getMachineFunction()
6989 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6990 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006991
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006992 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6993 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006994 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6995 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006996
6997 APInt FF(32, 0x5F800000ULL);
6998
6999 // Check whether the sign bit is set.
7000 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7001 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7002 ISD::SETLT);
7003
7004 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7005 SDValue FudgePtr = DAG.getConstantPool(
7006 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7007 getPointerTy());
7008
7009 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7010 SDValue Zero = DAG.getIntPtrConstant(0);
7011 SDValue Four = DAG.getIntPtrConstant(4);
7012 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7013 Zero, Four);
7014 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7015
7016 // Load the value out, extending it from f32 to f80.
7017 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00007018 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00007019 FudgePtr, MachinePointerInfo::getConstantPool(),
7020 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007021 // Extend everything to 80 bits to force it to be done on x87.
7022 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7023 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007024}
7025
Dan Gohman475871a2008-07-27 21:46:04 +00007026std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00007027FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00007028 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00007029
Owen Andersone50ed302009-08-10 22:56:29 +00007030 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00007031
7032 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7034 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007035 }
7036
Owen Anderson825b72b2009-08-11 20:47:22 +00007037 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7038 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007039 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007040
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007041 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007042 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007043 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007044 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007045 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007047 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007048 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007049
Evan Cheng87c89352007-10-15 20:11:21 +00007050 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7051 // stack slot.
7052 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007053 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007054 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007055 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007056
Michael J. Spencerec38de22010-10-10 22:04:20 +00007057
7058
Evan Cheng0db9fe62006-04-25 20:13:52 +00007059 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007061 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7063 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7064 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007066
Dan Gohman475871a2008-07-27 21:46:04 +00007067 SDValue Chain = DAG.getEntryNode();
7068 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007069 EVT TheVT = Op.getOperand(0).getValueType();
7070 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007071 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007072 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007073 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007074 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007075 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007076 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007077 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007078 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007079
Chris Lattner492a43e2010-09-22 01:28:21 +00007080 MachineMemOperand *MMO =
7081 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7082 MachineMemOperand::MOLoad, MemSize, MemSize);
7083 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7084 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007085 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007086 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007087 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7088 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007089
Chris Lattner07290932010-09-22 01:05:16 +00007090 MachineMemOperand *MMO =
7091 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7092 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007093
Evan Cheng0db9fe62006-04-25 20:13:52 +00007094 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007095 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007096 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7097 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007098
Chris Lattner27a6c732007-11-24 07:07:01 +00007099 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007100}
7101
Dan Gohmand858e902010-04-17 15:26:15 +00007102SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7103 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007104 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007105 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007106
Eli Friedman948e95a2009-05-23 09:59:16 +00007107 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007109 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7110 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007111
Chris Lattner27a6c732007-11-24 07:07:01 +00007112 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007113 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007114 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007115}
7116
Dan Gohmand858e902010-04-17 15:26:15 +00007117SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7118 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007119 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7120 SDValue FIST = Vals.first, StackSlot = Vals.second;
7121 assert(FIST.getNode() && "Unexpected failure");
7122
7123 // Load the result.
7124 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007125 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007126}
7127
Dan Gohmand858e902010-04-17 15:26:15 +00007128SDValue X86TargetLowering::LowerFABS(SDValue Op,
7129 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007130 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007131 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007132 EVT VT = Op.getValueType();
7133 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007134 if (VT.isVector())
7135 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007136 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007138 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007139 CV.push_back(C);
7140 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007141 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007142 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007143 CV.push_back(C);
7144 CV.push_back(C);
7145 CV.push_back(C);
7146 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007147 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007148 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007149 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007150 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007151 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007152 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007153 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154}
7155
Dan Gohmand858e902010-04-17 15:26:15 +00007156SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007157 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007158 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007159 EVT VT = Op.getValueType();
7160 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007161 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007162 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007163 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007164 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007165 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007166 CV.push_back(C);
7167 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007168 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007169 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007170 CV.push_back(C);
7171 CV.push_back(C);
7172 CV.push_back(C);
7173 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007174 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007175 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007176 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007177 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007178 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007179 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007180 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007181 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007182 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007183 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007184 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007185 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007186 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007187 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007188 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007189}
7190
Dan Gohmand858e902010-04-17 15:26:15 +00007191SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007192 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007193 SDValue Op0 = Op.getOperand(0);
7194 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007195 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007196 EVT VT = Op.getValueType();
7197 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007198
7199 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007200 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007201 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007202 SrcVT = VT;
7203 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007204 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007205 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007206 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007207 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007208 }
7209
7210 // At this point the operands and the result should have the same
7211 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007212
Evan Cheng68c47cb2007-01-05 07:55:56 +00007213 // First get the sign bit of second operand.
7214 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007215 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007216 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7217 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007218 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007219 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7220 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7221 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7222 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007223 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007224 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007225 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007226 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007227 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007228 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007229 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007230
7231 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007232 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007233 // Op0 is MVT::f32, Op1 is MVT::f64.
7234 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7235 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7236 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007237 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007238 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007239 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007240 }
7241
Evan Cheng73d6cf12007-01-05 21:37:56 +00007242 // Clear first operand sign bit.
7243 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007245 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7246 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007247 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007248 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7249 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7250 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7251 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007252 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007253 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007254 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007255 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007256 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007257 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007258 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007259
7260 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007261 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007262}
7263
Dan Gohman076aee32009-03-04 19:44:21 +00007264/// Emit nodes that will be selected as "test Op0,Op0", or something
7265/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007266SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007267 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007268 DebugLoc dl = Op.getDebugLoc();
7269
Dan Gohman31125812009-03-07 01:58:32 +00007270 // CF and OF aren't always set the way we want. Determine which
7271 // of these we need.
7272 bool NeedCF = false;
7273 bool NeedOF = false;
7274 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007275 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007276 case X86::COND_A: case X86::COND_AE:
7277 case X86::COND_B: case X86::COND_BE:
7278 NeedCF = true;
7279 break;
7280 case X86::COND_G: case X86::COND_GE:
7281 case X86::COND_L: case X86::COND_LE:
7282 case X86::COND_O: case X86::COND_NO:
7283 NeedOF = true;
7284 break;
Dan Gohman31125812009-03-07 01:58:32 +00007285 }
7286
Dan Gohman076aee32009-03-04 19:44:21 +00007287 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007288 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7289 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007290 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7291 // Emit a CMP with 0, which is the TEST pattern.
7292 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7293 DAG.getConstant(0, Op.getValueType()));
7294
7295 unsigned Opcode = 0;
7296 unsigned NumOperands = 0;
7297 switch (Op.getNode()->getOpcode()) {
7298 case ISD::ADD:
7299 // Due to an isel shortcoming, be conservative if this add is likely to be
7300 // selected as part of a load-modify-store instruction. When the root node
7301 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7302 // uses of other nodes in the match, such as the ADD in this case. This
7303 // leads to the ADD being left around and reselected, with the result being
7304 // two adds in the output. Alas, even if none our users are stores, that
7305 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7306 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7307 // climbing the DAG back to the root, and it doesn't seem to be worth the
7308 // effort.
7309 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007310 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007311 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7312 goto default_case;
7313
7314 if (ConstantSDNode *C =
7315 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7316 // An add of one will be selected as an INC.
7317 if (C->getAPIntValue() == 1) {
7318 Opcode = X86ISD::INC;
7319 NumOperands = 1;
7320 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007321 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007322
7323 // An add of negative one (subtract of one) will be selected as a DEC.
7324 if (C->getAPIntValue().isAllOnesValue()) {
7325 Opcode = X86ISD::DEC;
7326 NumOperands = 1;
7327 break;
7328 }
Dan Gohman076aee32009-03-04 19:44:21 +00007329 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007330
7331 // Otherwise use a regular EFLAGS-setting add.
7332 Opcode = X86ISD::ADD;
7333 NumOperands = 2;
7334 break;
7335 case ISD::AND: {
7336 // If the primary and result isn't used, don't bother using X86ISD::AND,
7337 // because a TEST instruction will be better.
7338 bool NonFlagUse = false;
7339 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7340 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7341 SDNode *User = *UI;
7342 unsigned UOpNo = UI.getOperandNo();
7343 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7344 // Look pass truncate.
7345 UOpNo = User->use_begin().getOperandNo();
7346 User = *User->use_begin();
7347 }
7348
7349 if (User->getOpcode() != ISD::BRCOND &&
7350 User->getOpcode() != ISD::SETCC &&
7351 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7352 NonFlagUse = true;
7353 break;
7354 }
Dan Gohman076aee32009-03-04 19:44:21 +00007355 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007356
7357 if (!NonFlagUse)
7358 break;
7359 }
7360 // FALL THROUGH
7361 case ISD::SUB:
7362 case ISD::OR:
7363 case ISD::XOR:
7364 // Due to the ISEL shortcoming noted above, be conservative if this op is
7365 // likely to be selected as part of a load-modify-store instruction.
7366 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7367 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7368 if (UI->getOpcode() == ISD::STORE)
7369 goto default_case;
7370
7371 // Otherwise use a regular EFLAGS-setting instruction.
7372 switch (Op.getNode()->getOpcode()) {
7373 default: llvm_unreachable("unexpected operator!");
7374 case ISD::SUB: Opcode = X86ISD::SUB; break;
7375 case ISD::OR: Opcode = X86ISD::OR; break;
7376 case ISD::XOR: Opcode = X86ISD::XOR; break;
7377 case ISD::AND: Opcode = X86ISD::AND; break;
7378 }
7379
7380 NumOperands = 2;
7381 break;
7382 case X86ISD::ADD:
7383 case X86ISD::SUB:
7384 case X86ISD::INC:
7385 case X86ISD::DEC:
7386 case X86ISD::OR:
7387 case X86ISD::XOR:
7388 case X86ISD::AND:
7389 return SDValue(Op.getNode(), 1);
7390 default:
7391 default_case:
7392 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007393 }
7394
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007395 if (Opcode == 0)
7396 // Emit a CMP with 0, which is the TEST pattern.
7397 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7398 DAG.getConstant(0, Op.getValueType()));
7399
7400 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7401 SmallVector<SDValue, 4> Ops;
7402 for (unsigned i = 0; i != NumOperands; ++i)
7403 Ops.push_back(Op.getOperand(i));
7404
7405 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7406 DAG.ReplaceAllUsesWith(Op, New);
7407 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007408}
7409
7410/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7411/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007412SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007413 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7415 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007416 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007417
7418 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007419 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007420}
7421
Evan Chengd40d03e2010-01-06 19:38:29 +00007422/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7423/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007424SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7425 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007426 SDValue Op0 = And.getOperand(0);
7427 SDValue Op1 = And.getOperand(1);
7428 if (Op0.getOpcode() == ISD::TRUNCATE)
7429 Op0 = Op0.getOperand(0);
7430 if (Op1.getOpcode() == ISD::TRUNCATE)
7431 Op1 = Op1.getOperand(0);
7432
Evan Chengd40d03e2010-01-06 19:38:29 +00007433 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007434 if (Op1.getOpcode() == ISD::SHL)
7435 std::swap(Op0, Op1);
7436 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007437 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7438 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007439 // If we looked past a truncate, check that it's only truncating away
7440 // known zeros.
7441 unsigned BitWidth = Op0.getValueSizeInBits();
7442 unsigned AndBitWidth = And.getValueSizeInBits();
7443 if (BitWidth > AndBitWidth) {
7444 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7445 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7446 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7447 return SDValue();
7448 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007449 LHS = Op1;
7450 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007451 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007452 } else if (Op1.getOpcode() == ISD::Constant) {
7453 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7454 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007455 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7456 LHS = AndLHS.getOperand(0);
7457 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007458 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007459 }
Evan Cheng0488db92007-09-25 01:57:46 +00007460
Evan Chengd40d03e2010-01-06 19:38:29 +00007461 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007462 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007463 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007464 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007465 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007466 // Also promote i16 to i32 for performance / code size reason.
7467 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007468 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007469 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007470
Evan Chengd40d03e2010-01-06 19:38:29 +00007471 // If the operand types disagree, extend the shift amount to match. Since
7472 // BT ignores high bits (like shifts) we can use anyextend.
7473 if (LHS.getValueType() != RHS.getValueType())
7474 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007475
Evan Chengd40d03e2010-01-06 19:38:29 +00007476 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7477 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7478 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7479 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007480 }
7481
Evan Cheng54de3ea2010-01-05 06:52:31 +00007482 return SDValue();
7483}
7484
Dan Gohmand858e902010-04-17 15:26:15 +00007485SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007486 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7487 SDValue Op0 = Op.getOperand(0);
7488 SDValue Op1 = Op.getOperand(1);
7489 DebugLoc dl = Op.getDebugLoc();
7490 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7491
7492 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007493 // Lower (X & (1 << N)) == 0 to BT(X, N).
7494 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7495 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007496 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007497 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007498 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007499 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7500 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7501 if (NewSetCC.getNode())
7502 return NewSetCC;
7503 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007504
Chris Lattner481eebc2010-12-19 21:23:48 +00007505 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7506 // these.
7507 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007508 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007509 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7510 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007511
Chris Lattner481eebc2010-12-19 21:23:48 +00007512 // If the input is a setcc, then reuse the input setcc or use a new one with
7513 // the inverted condition.
7514 if (Op0.getOpcode() == X86ISD::SETCC) {
7515 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7516 bool Invert = (CC == ISD::SETNE) ^
7517 cast<ConstantSDNode>(Op1)->isNullValue();
7518 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007519
Evan Cheng2c755ba2010-02-27 07:36:59 +00007520 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007521 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7522 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7523 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007524 }
7525
Evan Chenge5b51ac2010-04-17 06:13:15 +00007526 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007527 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007528 if (X86CC == X86::COND_INVALID)
7529 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007530
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007531 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007532 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007533 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007534}
7535
Dan Gohmand858e902010-04-17 15:26:15 +00007536SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue Cond;
7538 SDValue Op0 = Op.getOperand(0);
7539 SDValue Op1 = Op.getOperand(1);
7540 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007541 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007542 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7543 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007544 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007545
7546 if (isFP) {
7547 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007548 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7550 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007551 bool Swap = false;
7552
7553 switch (SetCCOpcode) {
7554 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007555 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007556 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007557 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007558 case ISD::SETGT: Swap = true; // Fallthrough
7559 case ISD::SETLT:
7560 case ISD::SETOLT: SSECC = 1; break;
7561 case ISD::SETOGE:
7562 case ISD::SETGE: Swap = true; // Fallthrough
7563 case ISD::SETLE:
7564 case ISD::SETOLE: SSECC = 2; break;
7565 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007566 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007567 case ISD::SETNE: SSECC = 4; break;
7568 case ISD::SETULE: Swap = true;
7569 case ISD::SETUGE: SSECC = 5; break;
7570 case ISD::SETULT: Swap = true;
7571 case ISD::SETUGT: SSECC = 6; break;
7572 case ISD::SETO: SSECC = 7; break;
7573 }
7574 if (Swap)
7575 std::swap(Op0, Op1);
7576
Nate Begemanfb8ead02008-07-25 19:05:58 +00007577 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007578 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007579 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007580 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007581 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7582 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007583 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007584 }
7585 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007586 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007587 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7588 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007589 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007590 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007591 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007592 }
7593 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007594 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007595 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007596
Nate Begeman30a0de92008-07-17 16:51:19 +00007597 // We are handling one of the integer comparisons here. Since SSE only has
7598 // GT and EQ comparisons for integer, swapping operands and multiple
7599 // operations may be required for some comparisons.
7600 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7601 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007602
Owen Anderson825b72b2009-08-11 20:47:22 +00007603 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007604 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007605 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007606 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007607 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7608 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007609 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007610
Nate Begeman30a0de92008-07-17 16:51:19 +00007611 switch (SetCCOpcode) {
7612 default: break;
7613 case ISD::SETNE: Invert = true;
7614 case ISD::SETEQ: Opc = EQOpc; break;
7615 case ISD::SETLT: Swap = true;
7616 case ISD::SETGT: Opc = GTOpc; break;
7617 case ISD::SETGE: Swap = true;
7618 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7619 case ISD::SETULT: Swap = true;
7620 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7621 case ISD::SETUGE: Swap = true;
7622 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7623 }
7624 if (Swap)
7625 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007626
Nate Begeman30a0de92008-07-17 16:51:19 +00007627 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7628 // bits of the inputs before performing those operations.
7629 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007630 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007631 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7632 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007633 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007634 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7635 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007636 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7637 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007638 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007639
Dale Johannesenace16102009-02-03 19:33:06 +00007640 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007641
7642 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007643 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007644 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007645
Nate Begeman30a0de92008-07-17 16:51:19 +00007646 return Result;
7647}
Evan Cheng0488db92007-09-25 01:57:46 +00007648
Evan Cheng370e5342008-12-03 08:38:43 +00007649// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007650static bool isX86LogicalCmp(SDValue Op) {
7651 unsigned Opc = Op.getNode()->getOpcode();
7652 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7653 return true;
7654 if (Op.getResNo() == 1 &&
7655 (Opc == X86ISD::ADD ||
7656 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007657 Opc == X86ISD::ADC ||
7658 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007659 Opc == X86ISD::SMUL ||
7660 Opc == X86ISD::UMUL ||
7661 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007662 Opc == X86ISD::DEC ||
7663 Opc == X86ISD::OR ||
7664 Opc == X86ISD::XOR ||
7665 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007666 return true;
7667
Chris Lattner9637d5b2010-12-05 07:49:54 +00007668 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7669 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007670
Dan Gohman076aee32009-03-04 19:44:21 +00007671 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007672}
7673
Chris Lattnera2b56002010-12-05 01:23:24 +00007674static bool isZero(SDValue V) {
7675 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7676 return C && C->isNullValue();
7677}
7678
Chris Lattner96908b12010-12-05 02:00:51 +00007679static bool isAllOnes(SDValue V) {
7680 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7681 return C && C->isAllOnesValue();
7682}
7683
Dan Gohmand858e902010-04-17 15:26:15 +00007684SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007685 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007686 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007687 SDValue Op1 = Op.getOperand(1);
7688 SDValue Op2 = Op.getOperand(2);
7689 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007690 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007691
Dan Gohman1a492952009-10-20 16:22:37 +00007692 if (Cond.getOpcode() == ISD::SETCC) {
7693 SDValue NewCond = LowerSETCC(Cond, DAG);
7694 if (NewCond.getNode())
7695 Cond = NewCond;
7696 }
Evan Cheng734503b2006-09-11 02:19:56 +00007697
Chris Lattnera2b56002010-12-05 01:23:24 +00007698 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007699 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007700 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007701 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007702 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007703 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7704 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007705 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007706
Chris Lattnera2b56002010-12-05 01:23:24 +00007707 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007708
7709 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007710 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7711 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007712
7713 SDValue CmpOp0 = Cmp.getOperand(0);
7714 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7715 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007716
Chris Lattner96908b12010-12-05 02:00:51 +00007717 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007718 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7719 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007720
Chris Lattner96908b12010-12-05 02:00:51 +00007721 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7722 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007723
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007724 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007725 if (N2C == 0 || !N2C->isNullValue())
7726 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7727 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007728 }
7729 }
7730
Chris Lattnera2b56002010-12-05 01:23:24 +00007731 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007732 if (Cond.getOpcode() == ISD::AND &&
7733 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7734 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007735 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007736 Cond = Cond.getOperand(0);
7737 }
7738
Evan Cheng3f41d662007-10-08 22:16:29 +00007739 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7740 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007741 if (Cond.getOpcode() == X86ISD::SETCC ||
7742 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007743 CC = Cond.getOperand(0);
7744
Dan Gohman475871a2008-07-27 21:46:04 +00007745 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007746 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007747 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007748
Evan Cheng3f41d662007-10-08 22:16:29 +00007749 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007750 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007751 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007752 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007753
Chris Lattnerd1980a52009-03-12 06:52:53 +00007754 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7755 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007756 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007757 addTest = false;
7758 }
7759 }
7760
7761 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007762 // Look pass the truncate.
7763 if (Cond.getOpcode() == ISD::TRUNCATE)
7764 Cond = Cond.getOperand(0);
7765
7766 // We know the result of AND is compared against zero. Try to match
7767 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007768 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007769 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007770 if (NewSetCC.getNode()) {
7771 CC = NewSetCC.getOperand(0);
7772 Cond = NewSetCC.getOperand(1);
7773 addTest = false;
7774 }
7775 }
7776 }
7777
7778 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007779 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007780 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007781 }
7782
Benjamin Kramere915ff32010-12-22 23:09:28 +00007783 // a < b ? -1 : 0 -> RES = ~setcc_carry
7784 // a < b ? 0 : -1 -> RES = setcc_carry
7785 // a >= b ? -1 : 0 -> RES = setcc_carry
7786 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7787 if (Cond.getOpcode() == X86ISD::CMP) {
7788 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7789
7790 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7791 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7792 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7793 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7794 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7795 return DAG.getNOT(DL, Res, Res.getValueType());
7796 return Res;
7797 }
7798 }
7799
Evan Cheng0488db92007-09-25 01:57:46 +00007800 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7801 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007802 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007803 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007804 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007805}
7806
Evan Cheng370e5342008-12-03 08:38:43 +00007807// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7808// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7809// from the AND / OR.
7810static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7811 Opc = Op.getOpcode();
7812 if (Opc != ISD::OR && Opc != ISD::AND)
7813 return false;
7814 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7815 Op.getOperand(0).hasOneUse() &&
7816 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7817 Op.getOperand(1).hasOneUse());
7818}
7819
Evan Cheng961d6d42009-02-02 08:19:07 +00007820// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7821// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007822static bool isXor1OfSetCC(SDValue Op) {
7823 if (Op.getOpcode() != ISD::XOR)
7824 return false;
7825 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7826 if (N1C && N1C->getAPIntValue() == 1) {
7827 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7828 Op.getOperand(0).hasOneUse();
7829 }
7830 return false;
7831}
7832
Dan Gohmand858e902010-04-17 15:26:15 +00007833SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007834 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007835 SDValue Chain = Op.getOperand(0);
7836 SDValue Cond = Op.getOperand(1);
7837 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007838 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007839 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007840
Dan Gohman1a492952009-10-20 16:22:37 +00007841 if (Cond.getOpcode() == ISD::SETCC) {
7842 SDValue NewCond = LowerSETCC(Cond, DAG);
7843 if (NewCond.getNode())
7844 Cond = NewCond;
7845 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007846#if 0
7847 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007848 else if (Cond.getOpcode() == X86ISD::ADD ||
7849 Cond.getOpcode() == X86ISD::SUB ||
7850 Cond.getOpcode() == X86ISD::SMUL ||
7851 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007852 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007853#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007854
Evan Chengad9c0a32009-12-15 00:53:42 +00007855 // Look pass (and (setcc_carry (cmp ...)), 1).
7856 if (Cond.getOpcode() == ISD::AND &&
7857 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7858 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007859 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007860 Cond = Cond.getOperand(0);
7861 }
7862
Evan Cheng3f41d662007-10-08 22:16:29 +00007863 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7864 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007865 if (Cond.getOpcode() == X86ISD::SETCC ||
7866 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007867 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007868
Dan Gohman475871a2008-07-27 21:46:04 +00007869 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007870 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007871 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007872 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007873 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007874 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007875 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007876 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007877 default: break;
7878 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007879 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007880 // These can only come from an arithmetic instruction with overflow,
7881 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007882 Cond = Cond.getNode()->getOperand(1);
7883 addTest = false;
7884 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007885 }
Evan Cheng0488db92007-09-25 01:57:46 +00007886 }
Evan Cheng370e5342008-12-03 08:38:43 +00007887 } else {
7888 unsigned CondOpc;
7889 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7890 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007891 if (CondOpc == ISD::OR) {
7892 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7893 // two branches instead of an explicit OR instruction with a
7894 // separate test.
7895 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007896 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007897 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007898 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007899 Chain, Dest, CC, Cmp);
7900 CC = Cond.getOperand(1).getOperand(0);
7901 Cond = Cmp;
7902 addTest = false;
7903 }
7904 } else { // ISD::AND
7905 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7906 // two branches instead of an explicit AND instruction with a
7907 // separate test. However, we only do this if this block doesn't
7908 // have a fall-through edge, because this requires an explicit
7909 // jmp when the condition is false.
7910 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007911 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007912 Op.getNode()->hasOneUse()) {
7913 X86::CondCode CCode =
7914 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7915 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007917 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007918 // Look for an unconditional branch following this conditional branch.
7919 // We need this because we need to reverse the successors in order
7920 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007921 if (User->getOpcode() == ISD::BR) {
7922 SDValue FalseBB = User->getOperand(1);
7923 SDNode *NewBR =
7924 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007925 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007926 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007927 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007928
Dale Johannesene4d209d2009-02-03 20:21:25 +00007929 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007930 Chain, Dest, CC, Cmp);
7931 X86::CondCode CCode =
7932 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7933 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007934 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007935 Cond = Cmp;
7936 addTest = false;
7937 }
7938 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007939 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007940 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7941 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7942 // It should be transformed during dag combiner except when the condition
7943 // is set by a arithmetics with overflow node.
7944 X86::CondCode CCode =
7945 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7946 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007948 Cond = Cond.getOperand(0).getOperand(1);
7949 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007950 }
Evan Cheng0488db92007-09-25 01:57:46 +00007951 }
7952
7953 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007954 // Look pass the truncate.
7955 if (Cond.getOpcode() == ISD::TRUNCATE)
7956 Cond = Cond.getOperand(0);
7957
7958 // We know the result of AND is compared against zero. Try to match
7959 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007960 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007961 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7962 if (NewSetCC.getNode()) {
7963 CC = NewSetCC.getOperand(0);
7964 Cond = NewSetCC.getOperand(1);
7965 addTest = false;
7966 }
7967 }
7968 }
7969
7970 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007971 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007972 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007973 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007974 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007975 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007976}
7977
Anton Korobeynikove060b532007-04-17 19:34:00 +00007978
7979// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7980// Calls to _alloca is needed to probe the stack when allocating more than 4k
7981// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7982// that the guard pages used by the OS virtual memory manager are allocated in
7983// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007984SDValue
7985X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007986 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007987 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007988 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007989 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007990 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007991
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007992 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007993 SDValue Chain = Op.getOperand(0);
7994 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007995 // FIXME: Ensure alignment here
7996
Dan Gohman475871a2008-07-27 21:46:04 +00007997 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007998
Owen Anderson825b72b2009-08-11 20:47:22 +00007999 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008000 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008001
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00008002 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008003 Flag = Chain.getValue(1);
8004
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008005 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00008006
Michael J. Spencere9c253e2010-10-21 01:41:01 +00008007 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008008 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008009
Dale Johannesendd64c412009-02-04 00:33:20 +00008010 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00008011
Dan Gohman475871a2008-07-27 21:46:04 +00008012 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008013 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008014}
8015
Dan Gohmand858e902010-04-17 15:26:15 +00008016SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00008017 MachineFunction &MF = DAG.getMachineFunction();
8018 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8019
Dan Gohman69de1932008-02-06 22:27:42 +00008020 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00008021 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00008022
Anton Korobeynikove7beda12010-10-03 22:52:07 +00008023 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008024 // vastart just stores the address of the VarArgsFrameIndex slot into the
8025 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008026 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8027 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008028 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8029 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008030 }
8031
8032 // __va_list_tag:
8033 // gp_offset (0 - 6 * 8)
8034 // fp_offset (48 - 48 + 8 * 16)
8035 // overflow_arg_area (point to parameters coming in memory).
8036 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008037 SmallVector<SDValue, 8> MemOps;
8038 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008039 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008040 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008041 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8042 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008043 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008044 MemOps.push_back(Store);
8045
8046 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008047 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008048 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008049 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008050 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8051 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008052 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008053 MemOps.push_back(Store);
8054
8055 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008056 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008057 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008058 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8059 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008060 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8061 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008062 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008063 MemOps.push_back(Store);
8064
8065 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008066 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008067 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008068 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8069 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008070 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8071 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008072 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008073 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008074 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008075}
8076
Dan Gohmand858e902010-04-17 15:26:15 +00008077SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008078 assert(Subtarget->is64Bit() &&
8079 "LowerVAARG only handles 64-bit va_arg!");
8080 assert((Subtarget->isTargetLinux() ||
8081 Subtarget->isTargetDarwin()) &&
8082 "Unhandled target in LowerVAARG");
8083 assert(Op.getNode()->getNumOperands() == 4);
8084 SDValue Chain = Op.getOperand(0);
8085 SDValue SrcPtr = Op.getOperand(1);
8086 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8087 unsigned Align = Op.getConstantOperandVal(3);
8088 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008089
Dan Gohman320afb82010-10-12 18:00:49 +00008090 EVT ArgVT = Op.getNode()->getValueType(0);
8091 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8092 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8093 uint8_t ArgMode;
8094
8095 // Decide which area this value should be read from.
8096 // TODO: Implement the AMD64 ABI in its entirety. This simple
8097 // selection mechanism works only for the basic types.
8098 if (ArgVT == MVT::f80) {
8099 llvm_unreachable("va_arg for f80 not yet implemented");
8100 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8101 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8102 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8103 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8104 } else {
8105 llvm_unreachable("Unhandled argument type in LowerVAARG");
8106 }
8107
8108 if (ArgMode == 2) {
8109 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008110 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008111 !(DAG.getMachineFunction()
8112 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008113 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008114 }
8115
8116 // Insert VAARG_64 node into the DAG
8117 // VAARG_64 returns two values: Variable Argument Address, Chain
8118 SmallVector<SDValue, 11> InstOps;
8119 InstOps.push_back(Chain);
8120 InstOps.push_back(SrcPtr);
8121 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8122 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8123 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8124 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8125 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8126 VTs, &InstOps[0], InstOps.size(),
8127 MVT::i64,
8128 MachinePointerInfo(SV),
8129 /*Align=*/0,
8130 /*Volatile=*/false,
8131 /*ReadMem=*/true,
8132 /*WriteMem=*/true);
8133 Chain = VAARG.getValue(1);
8134
8135 // Load the next argument and return it
8136 return DAG.getLoad(ArgVT, dl,
8137 Chain,
8138 VAARG,
8139 MachinePointerInfo(),
8140 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008141}
8142
Dan Gohmand858e902010-04-17 15:26:15 +00008143SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008144 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008145 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008146 SDValue Chain = Op.getOperand(0);
8147 SDValue DstPtr = Op.getOperand(1);
8148 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008149 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8150 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008151 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008152
Chris Lattnere72f2022010-09-21 05:40:29 +00008153 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008154 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008155 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008156 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008157}
8158
Dan Gohman475871a2008-07-27 21:46:04 +00008159SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008160X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008161 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008162 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008163 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008164 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008165 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008166 case Intrinsic::x86_sse_comieq_ss:
8167 case Intrinsic::x86_sse_comilt_ss:
8168 case Intrinsic::x86_sse_comile_ss:
8169 case Intrinsic::x86_sse_comigt_ss:
8170 case Intrinsic::x86_sse_comige_ss:
8171 case Intrinsic::x86_sse_comineq_ss:
8172 case Intrinsic::x86_sse_ucomieq_ss:
8173 case Intrinsic::x86_sse_ucomilt_ss:
8174 case Intrinsic::x86_sse_ucomile_ss:
8175 case Intrinsic::x86_sse_ucomigt_ss:
8176 case Intrinsic::x86_sse_ucomige_ss:
8177 case Intrinsic::x86_sse_ucomineq_ss:
8178 case Intrinsic::x86_sse2_comieq_sd:
8179 case Intrinsic::x86_sse2_comilt_sd:
8180 case Intrinsic::x86_sse2_comile_sd:
8181 case Intrinsic::x86_sse2_comigt_sd:
8182 case Intrinsic::x86_sse2_comige_sd:
8183 case Intrinsic::x86_sse2_comineq_sd:
8184 case Intrinsic::x86_sse2_ucomieq_sd:
8185 case Intrinsic::x86_sse2_ucomilt_sd:
8186 case Intrinsic::x86_sse2_ucomile_sd:
8187 case Intrinsic::x86_sse2_ucomigt_sd:
8188 case Intrinsic::x86_sse2_ucomige_sd:
8189 case Intrinsic::x86_sse2_ucomineq_sd: {
8190 unsigned Opc = 0;
8191 ISD::CondCode CC = ISD::SETCC_INVALID;
8192 switch (IntNo) {
8193 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008194 case Intrinsic::x86_sse_comieq_ss:
8195 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008196 Opc = X86ISD::COMI;
8197 CC = ISD::SETEQ;
8198 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008199 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008200 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008201 Opc = X86ISD::COMI;
8202 CC = ISD::SETLT;
8203 break;
8204 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008205 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008206 Opc = X86ISD::COMI;
8207 CC = ISD::SETLE;
8208 break;
8209 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008210 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008211 Opc = X86ISD::COMI;
8212 CC = ISD::SETGT;
8213 break;
8214 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008215 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008216 Opc = X86ISD::COMI;
8217 CC = ISD::SETGE;
8218 break;
8219 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008220 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008221 Opc = X86ISD::COMI;
8222 CC = ISD::SETNE;
8223 break;
8224 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008225 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008226 Opc = X86ISD::UCOMI;
8227 CC = ISD::SETEQ;
8228 break;
8229 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008230 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008231 Opc = X86ISD::UCOMI;
8232 CC = ISD::SETLT;
8233 break;
8234 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008235 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008236 Opc = X86ISD::UCOMI;
8237 CC = ISD::SETLE;
8238 break;
8239 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008240 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008241 Opc = X86ISD::UCOMI;
8242 CC = ISD::SETGT;
8243 break;
8244 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008245 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008246 Opc = X86ISD::UCOMI;
8247 CC = ISD::SETGE;
8248 break;
8249 case Intrinsic::x86_sse_ucomineq_ss:
8250 case Intrinsic::x86_sse2_ucomineq_sd:
8251 Opc = X86ISD::UCOMI;
8252 CC = ISD::SETNE;
8253 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008254 }
Evan Cheng734503b2006-09-11 02:19:56 +00008255
Dan Gohman475871a2008-07-27 21:46:04 +00008256 SDValue LHS = Op.getOperand(1);
8257 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008258 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008259 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008260 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8261 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8262 DAG.getConstant(X86CC, MVT::i8), Cond);
8263 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008264 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008265 // ptest and testp intrinsics. The intrinsic these come from are designed to
8266 // return an integer value, not just an instruction so lower it to the ptest
8267 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008268 case Intrinsic::x86_sse41_ptestz:
8269 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008270 case Intrinsic::x86_sse41_ptestnzc:
8271 case Intrinsic::x86_avx_ptestz_256:
8272 case Intrinsic::x86_avx_ptestc_256:
8273 case Intrinsic::x86_avx_ptestnzc_256:
8274 case Intrinsic::x86_avx_vtestz_ps:
8275 case Intrinsic::x86_avx_vtestc_ps:
8276 case Intrinsic::x86_avx_vtestnzc_ps:
8277 case Intrinsic::x86_avx_vtestz_pd:
8278 case Intrinsic::x86_avx_vtestc_pd:
8279 case Intrinsic::x86_avx_vtestnzc_pd:
8280 case Intrinsic::x86_avx_vtestz_ps_256:
8281 case Intrinsic::x86_avx_vtestc_ps_256:
8282 case Intrinsic::x86_avx_vtestnzc_ps_256:
8283 case Intrinsic::x86_avx_vtestz_pd_256:
8284 case Intrinsic::x86_avx_vtestc_pd_256:
8285 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8286 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008287 unsigned X86CC = 0;
8288 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008289 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008290 case Intrinsic::x86_avx_vtestz_ps:
8291 case Intrinsic::x86_avx_vtestz_pd:
8292 case Intrinsic::x86_avx_vtestz_ps_256:
8293 case Intrinsic::x86_avx_vtestz_pd_256:
8294 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008295 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008296 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008297 // ZF = 1
8298 X86CC = X86::COND_E;
8299 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008300 case Intrinsic::x86_avx_vtestc_ps:
8301 case Intrinsic::x86_avx_vtestc_pd:
8302 case Intrinsic::x86_avx_vtestc_ps_256:
8303 case Intrinsic::x86_avx_vtestc_pd_256:
8304 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008305 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008306 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008307 // CF = 1
8308 X86CC = X86::COND_B;
8309 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008310 case Intrinsic::x86_avx_vtestnzc_ps:
8311 case Intrinsic::x86_avx_vtestnzc_pd:
8312 case Intrinsic::x86_avx_vtestnzc_ps_256:
8313 case Intrinsic::x86_avx_vtestnzc_pd_256:
8314 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008315 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008316 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008317 // ZF and CF = 0
8318 X86CC = X86::COND_A;
8319 break;
8320 }
Eric Christopherfd179292009-08-27 18:07:15 +00008321
Eric Christopher71c67532009-07-29 00:28:05 +00008322 SDValue LHS = Op.getOperand(1);
8323 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008324 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8325 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008326 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8327 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8328 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008329 }
Evan Cheng5759f972008-05-04 09:15:50 +00008330
8331 // Fix vector shift instructions where the last operand is a non-immediate
8332 // i32 value.
8333 case Intrinsic::x86_sse2_pslli_w:
8334 case Intrinsic::x86_sse2_pslli_d:
8335 case Intrinsic::x86_sse2_pslli_q:
8336 case Intrinsic::x86_sse2_psrli_w:
8337 case Intrinsic::x86_sse2_psrli_d:
8338 case Intrinsic::x86_sse2_psrli_q:
8339 case Intrinsic::x86_sse2_psrai_w:
8340 case Intrinsic::x86_sse2_psrai_d:
8341 case Intrinsic::x86_mmx_pslli_w:
8342 case Intrinsic::x86_mmx_pslli_d:
8343 case Intrinsic::x86_mmx_pslli_q:
8344 case Intrinsic::x86_mmx_psrli_w:
8345 case Intrinsic::x86_mmx_psrli_d:
8346 case Intrinsic::x86_mmx_psrli_q:
8347 case Intrinsic::x86_mmx_psrai_w:
8348 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008349 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008350 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008351 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008352
8353 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008355 switch (IntNo) {
8356 case Intrinsic::x86_sse2_pslli_w:
8357 NewIntNo = Intrinsic::x86_sse2_psll_w;
8358 break;
8359 case Intrinsic::x86_sse2_pslli_d:
8360 NewIntNo = Intrinsic::x86_sse2_psll_d;
8361 break;
8362 case Intrinsic::x86_sse2_pslli_q:
8363 NewIntNo = Intrinsic::x86_sse2_psll_q;
8364 break;
8365 case Intrinsic::x86_sse2_psrli_w:
8366 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8367 break;
8368 case Intrinsic::x86_sse2_psrli_d:
8369 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8370 break;
8371 case Intrinsic::x86_sse2_psrli_q:
8372 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8373 break;
8374 case Intrinsic::x86_sse2_psrai_w:
8375 NewIntNo = Intrinsic::x86_sse2_psra_w;
8376 break;
8377 case Intrinsic::x86_sse2_psrai_d:
8378 NewIntNo = Intrinsic::x86_sse2_psra_d;
8379 break;
8380 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008382 switch (IntNo) {
8383 case Intrinsic::x86_mmx_pslli_w:
8384 NewIntNo = Intrinsic::x86_mmx_psll_w;
8385 break;
8386 case Intrinsic::x86_mmx_pslli_d:
8387 NewIntNo = Intrinsic::x86_mmx_psll_d;
8388 break;
8389 case Intrinsic::x86_mmx_pslli_q:
8390 NewIntNo = Intrinsic::x86_mmx_psll_q;
8391 break;
8392 case Intrinsic::x86_mmx_psrli_w:
8393 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8394 break;
8395 case Intrinsic::x86_mmx_psrli_d:
8396 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8397 break;
8398 case Intrinsic::x86_mmx_psrli_q:
8399 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8400 break;
8401 case Intrinsic::x86_mmx_psrai_w:
8402 NewIntNo = Intrinsic::x86_mmx_psra_w;
8403 break;
8404 case Intrinsic::x86_mmx_psrai_d:
8405 NewIntNo = Intrinsic::x86_mmx_psra_d;
8406 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008407 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008408 }
8409 break;
8410 }
8411 }
Mon P Wangefa42202009-09-03 19:56:25 +00008412
8413 // The vector shift intrinsics with scalars uses 32b shift amounts but
8414 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8415 // to be zero.
8416 SDValue ShOps[4];
8417 ShOps[0] = ShAmt;
8418 ShOps[1] = DAG.getConstant(0, MVT::i32);
8419 if (ShAmtVT == MVT::v4i32) {
8420 ShOps[2] = DAG.getUNDEF(MVT::i32);
8421 ShOps[3] = DAG.getUNDEF(MVT::i32);
8422 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8423 } else {
8424 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008425// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008426 }
8427
Owen Andersone50ed302009-08-10 22:56:29 +00008428 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008429 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008430 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008431 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008432 Op.getOperand(1), ShAmt);
8433 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008434 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008435}
Evan Cheng72261582005-12-20 06:22:03 +00008436
Dan Gohmand858e902010-04-17 15:26:15 +00008437SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8438 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008439 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8440 MFI->setReturnAddressIsTaken(true);
8441
Bill Wendling64e87322009-01-16 19:25:27 +00008442 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008443 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008444
8445 if (Depth > 0) {
8446 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8447 SDValue Offset =
8448 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008449 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008450 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008451 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008452 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008453 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008454 }
8455
8456 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008457 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008458 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008459 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008460}
8461
Dan Gohmand858e902010-04-17 15:26:15 +00008462SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008463 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8464 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008465
Owen Andersone50ed302009-08-10 22:56:29 +00008466 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008467 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008468 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8469 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008470 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008471 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008472 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8473 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008474 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008475 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008476}
8477
Dan Gohman475871a2008-07-27 21:46:04 +00008478SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008479 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008480 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008481}
8482
Dan Gohmand858e902010-04-17 15:26:15 +00008483SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008484 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008485 SDValue Chain = Op.getOperand(0);
8486 SDValue Offset = Op.getOperand(1);
8487 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008488 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008489
Dan Gohmand8816272010-08-11 18:14:00 +00008490 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8491 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8492 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008493 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008494
Dan Gohmand8816272010-08-11 18:14:00 +00008495 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8496 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008497 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008498 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8499 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008500 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008501 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008502
Dale Johannesene4d209d2009-02-03 20:21:25 +00008503 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008504 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008505 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008506}
8507
Dan Gohman475871a2008-07-27 21:46:04 +00008508SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008509 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008510 SDValue Root = Op.getOperand(0);
8511 SDValue Trmp = Op.getOperand(1); // trampoline
8512 SDValue FPtr = Op.getOperand(2); // nested function
8513 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008514 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008515
Dan Gohman69de1932008-02-06 22:27:42 +00008516 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008517
8518 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008519 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008520
8521 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008522 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8523 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008524
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008525 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8526 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008527
8528 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8529
8530 // Load the pointer to the nested function into R11.
8531 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008532 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008533 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008534 Addr, MachinePointerInfo(TrmpAddr),
8535 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008536
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8538 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008539 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8540 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008541 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008542
8543 // Load the 'nest' parameter value into R10.
8544 // R10 is specified in X86CallingConv.td
8545 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008546 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8547 DAG.getConstant(10, MVT::i64));
8548 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008549 Addr, MachinePointerInfo(TrmpAddr, 10),
8550 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008551
Owen Anderson825b72b2009-08-11 20:47:22 +00008552 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8553 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008554 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8555 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008556 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008557
8558 // Jump to the nested function.
8559 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008560 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8561 DAG.getConstant(20, MVT::i64));
8562 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008563 Addr, MachinePointerInfo(TrmpAddr, 20),
8564 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008565
8566 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008567 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8568 DAG.getConstant(22, MVT::i64));
8569 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008570 MachinePointerInfo(TrmpAddr, 22),
8571 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008572
Dan Gohman475871a2008-07-27 21:46:04 +00008573 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008574 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008575 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008576 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008577 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008578 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008579 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008580 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008581
8582 switch (CC) {
8583 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008584 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008585 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008586 case CallingConv::X86_StdCall: {
8587 // Pass 'nest' parameter in ECX.
8588 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008589 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008590
8591 // Check that ECX wasn't needed by an 'inreg' parameter.
8592 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008593 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008594
Chris Lattner58d74912008-03-12 17:45:29 +00008595 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008596 unsigned InRegCount = 0;
8597 unsigned Idx = 1;
8598
8599 for (FunctionType::param_iterator I = FTy->param_begin(),
8600 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008601 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008602 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008603 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008604
8605 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008606 report_fatal_error("Nest register in use - reduce number of inreg"
8607 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008608 }
8609 }
8610 break;
8611 }
8612 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008613 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008614 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008615 // Pass 'nest' parameter in EAX.
8616 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008617 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008618 break;
8619 }
8620
Dan Gohman475871a2008-07-27 21:46:04 +00008621 SDValue OutChains[4];
8622 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008623
Owen Anderson825b72b2009-08-11 20:47:22 +00008624 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8625 DAG.getConstant(10, MVT::i32));
8626 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008627
Chris Lattnera62fe662010-02-05 19:20:30 +00008628 // This is storing the opcode for MOV32ri.
8629 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008630 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008631 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008632 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008633 Trmp, MachinePointerInfo(TrmpAddr),
8634 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008635
Owen Anderson825b72b2009-08-11 20:47:22 +00008636 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8637 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008638 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8639 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008640 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008641
Chris Lattnera62fe662010-02-05 19:20:30 +00008642 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008643 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8644 DAG.getConstant(5, MVT::i32));
8645 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008646 MachinePointerInfo(TrmpAddr, 5),
8647 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008648
Owen Anderson825b72b2009-08-11 20:47:22 +00008649 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8650 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008651 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8652 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008653 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008654
Dan Gohman475871a2008-07-27 21:46:04 +00008655 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008656 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008657 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008658 }
8659}
8660
Dan Gohmand858e902010-04-17 15:26:15 +00008661SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8662 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008663 /*
8664 The rounding mode is in bits 11:10 of FPSR, and has the following
8665 settings:
8666 00 Round to nearest
8667 01 Round to -inf
8668 10 Round to +inf
8669 11 Round to 0
8670
8671 FLT_ROUNDS, on the other hand, expects the following:
8672 -1 Undefined
8673 0 Round to 0
8674 1 Round to nearest
8675 2 Round to +inf
8676 3 Round to -inf
8677
8678 To perform the conversion, we do:
8679 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8680 */
8681
8682 MachineFunction &MF = DAG.getMachineFunction();
8683 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008684 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008685 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008686 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008687 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008688
8689 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008690 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008691 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008692
Michael J. Spencerec38de22010-10-10 22:04:20 +00008693
Chris Lattner2156b792010-09-22 01:11:26 +00008694 MachineMemOperand *MMO =
8695 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8696 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008697
Chris Lattner2156b792010-09-22 01:11:26 +00008698 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8699 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8700 DAG.getVTList(MVT::Other),
8701 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008702
8703 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008704 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008705 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008706
8707 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008708 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008709 DAG.getNode(ISD::SRL, DL, MVT::i16,
8710 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008711 CWD, DAG.getConstant(0x800, MVT::i16)),
8712 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008713 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008714 DAG.getNode(ISD::SRL, DL, MVT::i16,
8715 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 CWD, DAG.getConstant(0x400, MVT::i16)),
8717 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008718
Dan Gohman475871a2008-07-27 21:46:04 +00008719 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008720 DAG.getNode(ISD::AND, DL, MVT::i16,
8721 DAG.getNode(ISD::ADD, DL, MVT::i16,
8722 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008723 DAG.getConstant(1, MVT::i16)),
8724 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008725
8726
Duncan Sands83ec4b62008-06-06 12:08:01 +00008727 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008728 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008729}
8730
Dan Gohmand858e902010-04-17 15:26:15 +00008731SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008732 EVT VT = Op.getValueType();
8733 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008734 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008735 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008736
8737 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008738 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008739 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008740 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008741 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008742 }
Evan Cheng18efe262007-12-14 02:13:44 +00008743
Evan Cheng152804e2007-12-14 08:30:15 +00008744 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008746 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008747
8748 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008749 SDValue Ops[] = {
8750 Op,
8751 DAG.getConstant(NumBits+NumBits-1, OpVT),
8752 DAG.getConstant(X86::COND_E, MVT::i8),
8753 Op.getValue(1)
8754 };
8755 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008756
8757 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008758 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008759
Owen Anderson825b72b2009-08-11 20:47:22 +00008760 if (VT == MVT::i8)
8761 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008762 return Op;
8763}
8764
Dan Gohmand858e902010-04-17 15:26:15 +00008765SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008766 EVT VT = Op.getValueType();
8767 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008768 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008769 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008770
8771 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008772 if (VT == MVT::i8) {
8773 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008774 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008775 }
Evan Cheng152804e2007-12-14 08:30:15 +00008776
8777 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008778 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008779 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008780
8781 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008782 SDValue Ops[] = {
8783 Op,
8784 DAG.getConstant(NumBits, OpVT),
8785 DAG.getConstant(X86::COND_E, MVT::i8),
8786 Op.getValue(1)
8787 };
8788 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008789
Owen Anderson825b72b2009-08-11 20:47:22 +00008790 if (VT == MVT::i8)
8791 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008792 return Op;
8793}
8794
Dan Gohmand858e902010-04-17 15:26:15 +00008795SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008796 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008797 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008798 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008799
Mon P Wangaf9b9522008-12-18 21:42:19 +00008800 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8801 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8802 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8803 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8804 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8805 //
8806 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8807 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8808 // return AloBlo + AloBhi + AhiBlo;
8809
8810 SDValue A = Op.getOperand(0);
8811 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008812
Dale Johannesene4d209d2009-02-03 20:21:25 +00008813 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008814 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8815 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008816 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008817 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8818 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008819 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008820 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008821 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008822 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008823 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008824 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008825 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008826 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008827 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008828 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008829 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8830 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008831 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008832 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8833 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008834 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8835 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008836 return Res;
8837}
8838
Nadav Rotem43012222011-05-11 08:12:09 +00008839SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8840
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008841 EVT VT = Op.getValueType();
8842 DebugLoc dl = Op.getDebugLoc();
8843 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008844 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008845
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008846 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008847
Nadav Rotem43012222011-05-11 08:12:09 +00008848 // Must have SSE2.
8849 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008850
Nadav Rotem43012222011-05-11 08:12:09 +00008851 // Optimize shl/srl/sra with constant shift amount.
8852 if (isSplatVector(Amt.getNode())) {
8853 SDValue SclrAmt = Amt->getOperand(0);
8854 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8855 uint64_t ShiftAmt = C->getZExtValue();
8856
8857 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8858 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8859 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8860 R, DAG.getConstant(ShiftAmt, MVT::i32));
8861
8862 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8863 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8864 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8865 R, DAG.getConstant(ShiftAmt, MVT::i32));
8866
8867 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8868 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8869 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8870 R, DAG.getConstant(ShiftAmt, MVT::i32));
8871
8872 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8873 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8874 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8875 R, DAG.getConstant(ShiftAmt, MVT::i32));
8876
8877 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8878 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8879 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8880 R, DAG.getConstant(ShiftAmt, MVT::i32));
8881
8882 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8883 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8884 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8885 R, DAG.getConstant(ShiftAmt, MVT::i32));
8886
8887 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8888 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8889 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8890 R, DAG.getConstant(ShiftAmt, MVT::i32));
8891
8892 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8893 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8894 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8895 R, DAG.getConstant(ShiftAmt, MVT::i32));
8896 }
8897 }
8898
8899 // Lower SHL with variable shift amount.
8900 // Cannot lower SHL without SSE4.1 or later.
8901 if (!Subtarget->hasSSE41()) return SDValue();
8902
8903 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008904 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8905 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8906 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8907
8908 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008909
Nate Begeman51409212010-07-28 00:21:48 +00008910 std::vector<Constant*> CV(4, CI);
8911 Constant *C = ConstantVector::get(CV);
8912 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8913 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008914 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008915 false, false, 16);
8916
8917 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008918 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008919 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8920 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8921 }
Nadav Rotem43012222011-05-11 08:12:09 +00008922 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008923 // a = a << 5;
8924 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8925 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8926 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8927
8928 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8929 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8930
8931 std::vector<Constant*> CVM1(16, CM1);
8932 std::vector<Constant*> CVM2(16, CM2);
8933 Constant *C = ConstantVector::get(CVM1);
8934 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8935 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008936 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008937 false, false, 16);
8938
8939 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8940 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8941 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8942 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8943 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008944 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008945 // a += a
8946 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008947
Nate Begeman51409212010-07-28 00:21:48 +00008948 C = ConstantVector::get(CVM2);
8949 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8950 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008951 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008952 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008953
Nate Begeman51409212010-07-28 00:21:48 +00008954 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8955 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8956 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8957 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8958 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008959 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008960 // a += a
8961 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008962
Nate Begeman51409212010-07-28 00:21:48 +00008963 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008964 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008965 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8966 return R;
8967 }
8968 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008969}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008970
Dan Gohmand858e902010-04-17 15:26:15 +00008971SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008972 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8973 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008974 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8975 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008976 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008977 SDValue LHS = N->getOperand(0);
8978 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008979 unsigned BaseOp = 0;
8980 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008981 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008982 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008983 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008984 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008985 // A subtract of one will be selected as a INC. Note that INC doesn't
8986 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008987 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8988 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008989 BaseOp = X86ISD::INC;
8990 Cond = X86::COND_O;
8991 break;
8992 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008993 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008994 Cond = X86::COND_O;
8995 break;
8996 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008997 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008998 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008999 break;
9000 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00009001 // A subtract of one will be selected as a DEC. Note that DEC doesn't
9002 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00009003 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
9004 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00009005 BaseOp = X86ISD::DEC;
9006 Cond = X86::COND_O;
9007 break;
9008 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009009 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00009010 Cond = X86::COND_O;
9011 break;
9012 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009013 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00009014 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00009015 break;
9016 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00009017 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00009018 Cond = X86::COND_O;
9019 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009020 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
9021 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
9022 MVT::i32);
9023 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009024
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009025 SDValue SetCC =
9026 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9027 DAG.getConstant(X86::COND_O, MVT::i32),
9028 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009029
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009030 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9031 return Sum;
9032 }
Bill Wendling74c37652008-12-09 22:08:41 +00009033 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009034
Bill Wendling61edeb52008-12-02 01:06:39 +00009035 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009036 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009037 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009038
Bill Wendling61edeb52008-12-02 01:06:39 +00009039 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009040 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9041 DAG.getConstant(Cond, MVT::i32),
9042 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009043
Bill Wendling61edeb52008-12-02 01:06:39 +00009044 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9045 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009046}
9047
Eric Christopher9a9d2752010-07-22 02:48:34 +00009048SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9049 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009050
Eric Christopherb6729dc2010-08-04 23:03:04 +00009051 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009052 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009053 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00009054 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009055 SDValue Ops[] = {
9056 DAG.getRegister(X86::ESP, MVT::i32), // Base
9057 DAG.getTargetConstant(1, MVT::i8), // Scale
9058 DAG.getRegister(0, MVT::i32), // Index
9059 DAG.getTargetConstant(0, MVT::i32), // Disp
9060 DAG.getRegister(0, MVT::i32), // Segment.
9061 Zero,
9062 Chain
9063 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009064 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009065 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9066 array_lengthof(Ops));
9067 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009068 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009069
Eric Christopher9a9d2752010-07-22 02:48:34 +00009070 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009071 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009072 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009073
Chris Lattner132929a2010-08-14 17:26:09 +00009074 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9075 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9076 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9077 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009078
Chris Lattner132929a2010-08-14 17:26:09 +00009079 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9080 if (!Op1 && !Op2 && !Op3 && Op4)
9081 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009082
Chris Lattner132929a2010-08-14 17:26:09 +00009083 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9084 if (Op1 && !Op2 && !Op3 && !Op4)
9085 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009086
9087 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009088 // (MFENCE)>;
9089 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009090}
9091
Dan Gohmand858e902010-04-17 15:26:15 +00009092SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009093 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009094 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009095 unsigned Reg = 0;
9096 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009097 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009098 default:
9099 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 case MVT::i8: Reg = X86::AL; size = 1; break;
9101 case MVT::i16: Reg = X86::AX; size = 2; break;
9102 case MVT::i32: Reg = X86::EAX; size = 4; break;
9103 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009104 assert(Subtarget->is64Bit() && "Node not type legal!");
9105 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009106 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009107 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009108 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009109 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009110 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009111 Op.getOperand(1),
9112 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009113 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009114 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009115 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009116 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9117 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9118 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009119 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009120 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009121 return cpOut;
9122}
9123
Duncan Sands1607f052008-12-01 11:39:25 +00009124SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009125 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009126 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009127 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009128 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009129 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009130 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009131 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9132 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009133 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009134 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9135 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009136 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009137 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009138 rdx.getValue(1)
9139 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009140 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009141}
9142
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009143SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009144 SelectionDAG &DAG) const {
9145 EVT SrcVT = Op.getOperand(0).getValueType();
9146 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009147 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9148 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009149 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009150 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009151 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009152 // i64 <=> MMX conversions are Legal.
9153 if (SrcVT==MVT::i64 && DstVT.isVector())
9154 return Op;
9155 if (DstVT==MVT::i64 && SrcVT.isVector())
9156 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009157 // MMX <=> MMX conversions are Legal.
9158 if (SrcVT.isVector() && DstVT.isVector())
9159 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009160 // All other conversions need to be expanded.
9161 return SDValue();
9162}
Chris Lattner5b856542010-12-20 00:59:46 +00009163
Dan Gohmand858e902010-04-17 15:26:15 +00009164SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009165 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009166 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009167 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009168 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009169 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009170 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009171 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009172 Node->getOperand(0),
9173 Node->getOperand(1), negOp,
9174 cast<AtomicSDNode>(Node)->getSrcValue(),
9175 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009176}
9177
Chris Lattner5b856542010-12-20 00:59:46 +00009178static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9179 EVT VT = Op.getNode()->getValueType(0);
9180
9181 // Let legalize expand this if it isn't a legal type yet.
9182 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9183 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009184
Chris Lattner5b856542010-12-20 00:59:46 +00009185 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009186
Chris Lattner5b856542010-12-20 00:59:46 +00009187 unsigned Opc;
9188 bool ExtraOp = false;
9189 switch (Op.getOpcode()) {
9190 default: assert(0 && "Invalid code");
9191 case ISD::ADDC: Opc = X86ISD::ADD; break;
9192 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9193 case ISD::SUBC: Opc = X86ISD::SUB; break;
9194 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9195 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009196
Chris Lattner5b856542010-12-20 00:59:46 +00009197 if (!ExtraOp)
9198 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9199 Op.getOperand(1));
9200 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9201 Op.getOperand(1), Op.getOperand(2));
9202}
9203
Evan Cheng0db9fe62006-04-25 20:13:52 +00009204/// LowerOperation - Provide custom lowering hooks for some operations.
9205///
Dan Gohmand858e902010-04-17 15:26:15 +00009206SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009207 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009208 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009209 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009210 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9211 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009212 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009213 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009214 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9215 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9216 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009217 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009218 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009219 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9220 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9221 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009222 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009223 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009224 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009225 case ISD::SHL_PARTS:
9226 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009227 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009228 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009229 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009230 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009231 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009232 case ISD::FABS: return LowerFABS(Op, DAG);
9233 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009234 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009235 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009236 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009237 case ISD::SELECT: return LowerSELECT(Op, DAG);
9238 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009239 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009240 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009241 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009242 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009243 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009244 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9245 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009246 case ISD::FRAME_TO_ARGS_OFFSET:
9247 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009248 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009249 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009250 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009251 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009252 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9253 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009254 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009255 case ISD::SRA:
9256 case ISD::SRL:
9257 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009258 case ISD::SADDO:
9259 case ISD::UADDO:
9260 case ISD::SSUBO:
9261 case ISD::USUBO:
9262 case ISD::SMULO:
9263 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009264 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009265 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009266 case ISD::ADDC:
9267 case ISD::ADDE:
9268 case ISD::SUBC:
9269 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009270 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009271}
9272
Duncan Sands1607f052008-12-01 11:39:25 +00009273void X86TargetLowering::
9274ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009275 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009276 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009277 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009278 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009279
9280 SDValue Chain = Node->getOperand(0);
9281 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009282 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009283 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009284 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009285 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009286 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009287 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009288 SDValue Result =
9289 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9290 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009291 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009292 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009293 Results.push_back(Result.getValue(2));
9294}
9295
Duncan Sands126d9072008-07-04 11:47:58 +00009296/// ReplaceNodeResults - Replace a node with an illegal result type
9297/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009298void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9299 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009300 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009301 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009302 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009303 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009304 assert(false && "Do not know how to custom type legalize this operation!");
9305 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009306 case ISD::ADDC:
9307 case ISD::ADDE:
9308 case ISD::SUBC:
9309 case ISD::SUBE:
9310 // We don't want to expand or promote these.
9311 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009312 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009313 std::pair<SDValue,SDValue> Vals =
9314 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009315 SDValue FIST = Vals.first, StackSlot = Vals.second;
9316 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009317 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009318 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009319 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9320 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009321 }
9322 return;
9323 }
9324 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009325 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009326 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009327 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009328 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009329 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009330 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009331 eax.getValue(2));
9332 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9333 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009334 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009335 Results.push_back(edx.getValue(1));
9336 return;
9337 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009338 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009339 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009340 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009341 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009342 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9343 DAG.getConstant(0, MVT::i32));
9344 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9345 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009346 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9347 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009348 cpInL.getValue(1));
9349 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009350 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9351 DAG.getConstant(0, MVT::i32));
9352 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9353 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009354 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009355 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009356 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009357 swapInL.getValue(1));
9358 SDValue Ops[] = { swapInH.getValue(0),
9359 N->getOperand(1),
9360 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009361 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009362 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9363 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9364 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009365 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009366 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009367 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009368 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009369 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009370 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009371 Results.push_back(cpOutH.getValue(1));
9372 return;
9373 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009374 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009375 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9376 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009377 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009378 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9379 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009380 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009381 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9382 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009383 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009384 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9385 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009386 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009387 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9388 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009389 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009390 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9391 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009392 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009393 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9394 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009395 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009396}
9397
Evan Cheng72261582005-12-20 06:22:03 +00009398const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9399 switch (Opcode) {
9400 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009401 case X86ISD::BSF: return "X86ISD::BSF";
9402 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009403 case X86ISD::SHLD: return "X86ISD::SHLD";
9404 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009405 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009406 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009407 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009408 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009409 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009410 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009411 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9412 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9413 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009414 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009415 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009416 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009417 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009418 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009419 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009420 case X86ISD::COMI: return "X86ISD::COMI";
9421 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009422 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009423 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009424 case X86ISD::CMOV: return "X86ISD::CMOV";
9425 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009426 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009427 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9428 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009429 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009430 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009431 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009432 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009433 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009434 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9435 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009436 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009437 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009438 case X86ISD::PANDN: return "X86ISD::PANDN";
9439 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9440 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9441 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009442 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009443 case X86ISD::FMAX: return "X86ISD::FMAX";
9444 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009445 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9446 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009447 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009448 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009449 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009450 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009451 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009452 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9453 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009454 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9455 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9456 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9457 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9458 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9459 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009460 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9461 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009462 case X86ISD::VSHL: return "X86ISD::VSHL";
9463 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009464 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9465 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9466 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9467 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9468 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9469 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9470 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9471 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9472 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9473 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009474 case X86ISD::ADD: return "X86ISD::ADD";
9475 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009476 case X86ISD::ADC: return "X86ISD::ADC";
9477 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009478 case X86ISD::SMUL: return "X86ISD::SMUL";
9479 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009480 case X86ISD::INC: return "X86ISD::INC";
9481 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009482 case X86ISD::OR: return "X86ISD::OR";
9483 case X86ISD::XOR: return "X86ISD::XOR";
9484 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009485 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009486 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009487 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009488 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9489 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9490 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9491 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9492 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9493 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9494 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9495 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9496 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009497 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009498 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009499 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009500 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9501 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009502 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9503 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9504 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9505 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9506 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9507 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9508 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9509 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9510 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009511 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9512 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9513 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9514 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009515 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9516 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9517 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9518 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9519 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9520 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9521 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9522 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9523 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9524 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009525 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009526 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009527 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009528 }
9529}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009530
Chris Lattnerc9addb72007-03-30 23:15:24 +00009531// isLegalAddressingMode - Return true if the addressing mode represented
9532// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009533bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009534 const Type *Ty) const {
9535 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009536 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009537 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009538
Chris Lattnerc9addb72007-03-30 23:15:24 +00009539 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009540 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009541 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009542
Chris Lattnerc9addb72007-03-30 23:15:24 +00009543 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009544 unsigned GVFlags =
9545 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009546
Chris Lattnerdfed4132009-07-10 07:38:24 +00009547 // If a reference to this global requires an extra load, we can't fold it.
9548 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009549 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009550
Chris Lattnerdfed4132009-07-10 07:38:24 +00009551 // If BaseGV requires a register for the PIC base, we cannot also have a
9552 // BaseReg specified.
9553 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009554 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009555
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009556 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009557 if ((M != CodeModel::Small || R != Reloc::Static) &&
9558 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009559 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009560 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009561
Chris Lattnerc9addb72007-03-30 23:15:24 +00009562 switch (AM.Scale) {
9563 case 0:
9564 case 1:
9565 case 2:
9566 case 4:
9567 case 8:
9568 // These scales always work.
9569 break;
9570 case 3:
9571 case 5:
9572 case 9:
9573 // These scales are formed with basereg+scalereg. Only accept if there is
9574 // no basereg yet.
9575 if (AM.HasBaseReg)
9576 return false;
9577 break;
9578 default: // Other stuff never works.
9579 return false;
9580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009581
Chris Lattnerc9addb72007-03-30 23:15:24 +00009582 return true;
9583}
9584
9585
Evan Cheng2bd122c2007-10-26 01:56:11 +00009586bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009587 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009588 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009589 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9590 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009591 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009592 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009593 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009594}
9595
Owen Andersone50ed302009-08-10 22:56:29 +00009596bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009597 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009598 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009599 unsigned NumBits1 = VT1.getSizeInBits();
9600 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009601 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009602 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009603 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009604}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009605
Dan Gohman97121ba2009-04-08 00:15:30 +00009606bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009607 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009608 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009609}
9610
Owen Andersone50ed302009-08-10 22:56:29 +00009611bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009612 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009614}
9615
Owen Andersone50ed302009-08-10 22:56:29 +00009616bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009617 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009618 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009619}
9620
Evan Cheng60c07e12006-07-05 22:17:51 +00009621/// isShuffleMaskLegal - Targets can use this to indicate that they only
9622/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9623/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9624/// are assumed to be legal.
9625bool
Eric Christopherfd179292009-08-27 18:07:15 +00009626X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009627 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009628 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009629 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009630 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009631
Nate Begemana09008b2009-10-19 02:17:23 +00009632 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009633 return (VT.getVectorNumElements() == 2 ||
9634 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9635 isMOVLMask(M, VT) ||
9636 isSHUFPMask(M, VT) ||
9637 isPSHUFDMask(M, VT) ||
9638 isPSHUFHWMask(M, VT) ||
9639 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009640 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009641 isUNPCKLMask(M, VT) ||
9642 isUNPCKHMask(M, VT) ||
9643 isUNPCKL_v_undef_Mask(M, VT) ||
9644 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009645}
9646
Dan Gohman7d8143f2008-04-09 20:09:42 +00009647bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009648X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009649 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009650 unsigned NumElts = VT.getVectorNumElements();
9651 // FIXME: This collection of masks seems suspect.
9652 if (NumElts == 2)
9653 return true;
9654 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9655 return (isMOVLMask(Mask, VT) ||
9656 isCommutedMOVLMask(Mask, VT, true) ||
9657 isSHUFPMask(Mask, VT) ||
9658 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009659 }
9660 return false;
9661}
9662
9663//===----------------------------------------------------------------------===//
9664// X86 Scheduler Hooks
9665//===----------------------------------------------------------------------===//
9666
Mon P Wang63307c32008-05-05 19:05:59 +00009667// private utility function
9668MachineBasicBlock *
9669X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9670 MachineBasicBlock *MBB,
9671 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009672 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009673 unsigned LoadOpc,
9674 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009675 unsigned notOpc,
9676 unsigned EAXreg,
9677 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009678 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009679 // For the atomic bitwise operator, we generate
9680 // thisMBB:
9681 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009682 // ld t1 = [bitinstr.addr]
9683 // op t2 = t1, [bitinstr.val]
9684 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009685 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9686 // bz newMBB
9687 // fallthrough -->nextMBB
9688 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9689 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009690 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009691 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009692
Mon P Wang63307c32008-05-05 19:05:59 +00009693 /// First build the CFG
9694 MachineFunction *F = MBB->getParent();
9695 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009696 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9697 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9698 F->insert(MBBIter, newMBB);
9699 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009700
Dan Gohman14152b42010-07-06 20:24:04 +00009701 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9702 nextMBB->splice(nextMBB->begin(), thisMBB,
9703 llvm::next(MachineBasicBlock::iterator(bInstr)),
9704 thisMBB->end());
9705 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009706
Mon P Wang63307c32008-05-05 19:05:59 +00009707 // Update thisMBB to fall through to newMBB
9708 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009709
Mon P Wang63307c32008-05-05 19:05:59 +00009710 // newMBB jumps to itself and fall through to nextMBB
9711 newMBB->addSuccessor(nextMBB);
9712 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009713
Mon P Wang63307c32008-05-05 19:05:59 +00009714 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009715 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009716 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009717 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009718 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009719 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009720 int numArgs = bInstr->getNumOperands() - 1;
9721 for (int i=0; i < numArgs; ++i)
9722 argOpers[i] = &bInstr->getOperand(i+1);
9723
9724 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009725 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009726 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009727
Dale Johannesen140be2d2008-08-19 18:47:28 +00009728 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009729 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009730 for (int i=0; i <= lastAddrIndx; ++i)
9731 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009732
Dale Johannesen140be2d2008-08-19 18:47:28 +00009733 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009734 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009735 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009737 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009738 tt = t1;
9739
Dale Johannesen140be2d2008-08-19 18:47:28 +00009740 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009741 assert((argOpers[valArgIndx]->isReg() ||
9742 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009743 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009744 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009745 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009746 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009747 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009748 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009749 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009750
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009751 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009752 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009753
Dale Johannesene4d209d2009-02-03 20:21:25 +00009754 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009755 for (int i=0; i <= lastAddrIndx; ++i)
9756 (*MIB).addOperand(*argOpers[i]);
9757 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009758 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009759 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9760 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009761
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009762 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009763 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009764
Mon P Wang63307c32008-05-05 19:05:59 +00009765 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009766 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009767
Dan Gohman14152b42010-07-06 20:24:04 +00009768 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009769 return nextMBB;
9770}
9771
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009772// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009773MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009774X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9775 MachineBasicBlock *MBB,
9776 unsigned regOpcL,
9777 unsigned regOpcH,
9778 unsigned immOpcL,
9779 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009780 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009781 // For the atomic bitwise operator, we generate
9782 // thisMBB (instructions are in pairs, except cmpxchg8b)
9783 // ld t1,t2 = [bitinstr.addr]
9784 // newMBB:
9785 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9786 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009787 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009788 // mov ECX, EBX <- t5, t6
9789 // mov EAX, EDX <- t1, t2
9790 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9791 // mov t3, t4 <- EAX, EDX
9792 // bz newMBB
9793 // result in out1, out2
9794 // fallthrough -->nextMBB
9795
9796 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9797 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009798 const unsigned NotOpc = X86::NOT32r;
9799 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9800 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9801 MachineFunction::iterator MBBIter = MBB;
9802 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009803
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009804 /// First build the CFG
9805 MachineFunction *F = MBB->getParent();
9806 MachineBasicBlock *thisMBB = MBB;
9807 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9808 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9809 F->insert(MBBIter, newMBB);
9810 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009811
Dan Gohman14152b42010-07-06 20:24:04 +00009812 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9813 nextMBB->splice(nextMBB->begin(), thisMBB,
9814 llvm::next(MachineBasicBlock::iterator(bInstr)),
9815 thisMBB->end());
9816 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009817
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009818 // Update thisMBB to fall through to newMBB
9819 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009820
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009821 // newMBB jumps to itself and fall through to nextMBB
9822 newMBB->addSuccessor(nextMBB);
9823 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009824
Dale Johannesene4d209d2009-02-03 20:21:25 +00009825 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009826 // Insert instructions into newMBB based on incoming instruction
9827 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009828 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009829 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009830 MachineOperand& dest1Oper = bInstr->getOperand(0);
9831 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009832 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9833 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009834 argOpers[i] = &bInstr->getOperand(i+2);
9835
Dan Gohman71ea4e52010-05-14 21:01:44 +00009836 // We use some of the operands multiple times, so conservatively just
9837 // clear any kill flags that might be present.
9838 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9839 argOpers[i]->setIsKill(false);
9840 }
9841
Evan Chengad5b52f2010-01-08 19:14:57 +00009842 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009843 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009844
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009845 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009847 for (int i=0; i <= lastAddrIndx; ++i)
9848 (*MIB).addOperand(*argOpers[i]);
9849 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009850 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009851 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009852 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009853 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009854 MachineOperand newOp3 = *(argOpers[3]);
9855 if (newOp3.isImm())
9856 newOp3.setImm(newOp3.getImm()+4);
9857 else
9858 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009859 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009860 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009861
9862 // t3/4 are defined later, at the bottom of the loop
9863 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9864 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009865 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009866 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009867 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009868 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9869
Evan Cheng306b4ca2010-01-08 23:41:50 +00009870 // The subsequent operations should be using the destination registers of
9871 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009872 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009873 t1 = F->getRegInfo().createVirtualRegister(RC);
9874 t2 = F->getRegInfo().createVirtualRegister(RC);
9875 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9876 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009877 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009878 t1 = dest1Oper.getReg();
9879 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009880 }
9881
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009882 int valArgIndx = lastAddrIndx + 1;
9883 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009884 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009885 "invalid operand");
9886 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9887 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009888 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009889 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009890 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009891 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009892 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009893 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009894 (*MIB).addOperand(*argOpers[valArgIndx]);
9895 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009896 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009897 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009898 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009899 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009900 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009901 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009902 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009903 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009904 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009905 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009906
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009907 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009908 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009909 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009910 MIB.addReg(t2);
9911
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009912 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009913 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009914 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009915 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009916
Dale Johannesene4d209d2009-02-03 20:21:25 +00009917 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009918 for (int i=0; i <= lastAddrIndx; ++i)
9919 (*MIB).addOperand(*argOpers[i]);
9920
9921 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009922 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9923 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009924
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009925 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009926 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009927 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009928 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009929
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009930 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009931 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009932
Dan Gohman14152b42010-07-06 20:24:04 +00009933 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009934 return nextMBB;
9935}
9936
9937// private utility function
9938MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009939X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9940 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009941 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009942 // For the atomic min/max operator, we generate
9943 // thisMBB:
9944 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009945 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009946 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009947 // cmp t1, t2
9948 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009949 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009950 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9951 // bz newMBB
9952 // fallthrough -->nextMBB
9953 //
9954 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9955 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009956 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009957 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009958
Mon P Wang63307c32008-05-05 19:05:59 +00009959 /// First build the CFG
9960 MachineFunction *F = MBB->getParent();
9961 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009962 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9963 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9964 F->insert(MBBIter, newMBB);
9965 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009966
Dan Gohman14152b42010-07-06 20:24:04 +00009967 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9968 nextMBB->splice(nextMBB->begin(), thisMBB,
9969 llvm::next(MachineBasicBlock::iterator(mInstr)),
9970 thisMBB->end());
9971 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009972
Mon P Wang63307c32008-05-05 19:05:59 +00009973 // Update thisMBB to fall through to newMBB
9974 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009975
Mon P Wang63307c32008-05-05 19:05:59 +00009976 // newMBB jumps to newMBB and fall through to nextMBB
9977 newMBB->addSuccessor(nextMBB);
9978 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009979
Dale Johannesene4d209d2009-02-03 20:21:25 +00009980 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009981 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009982 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009983 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009984 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009985 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009986 int numArgs = mInstr->getNumOperands() - 1;
9987 for (int i=0; i < numArgs; ++i)
9988 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009989
Mon P Wang63307c32008-05-05 19:05:59 +00009990 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009991 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009992 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009993
Mon P Wangab3e7472008-05-05 22:56:23 +00009994 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009995 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009996 for (int i=0; i <= lastAddrIndx; ++i)
9997 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009998
Mon P Wang63307c32008-05-05 19:05:59 +00009999 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000010000 assert((argOpers[valArgIndx]->isReg() ||
10001 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000010002 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000010003
10004 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +000010005 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010006 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010007 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000010008 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000010009 (*MIB).addOperand(*argOpers[valArgIndx]);
10010
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010011 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000010012 MIB.addReg(t1);
10013
Dale Johannesene4d209d2009-02-03 20:21:25 +000010014 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000010015 MIB.addReg(t1);
10016 MIB.addReg(t2);
10017
10018 // Generate movc
10019 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010020 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010021 MIB.addReg(t2);
10022 MIB.addReg(t1);
10023
10024 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010025 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010026 for (int i=0; i <= lastAddrIndx; ++i)
10027 (*MIB).addOperand(*argOpers[i]);
10028 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010029 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010030 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10031 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010032
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010033 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010034 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010035
Mon P Wang63307c32008-05-05 19:05:59 +000010036 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010037 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010038
Dan Gohman14152b42010-07-06 20:24:04 +000010039 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010040 return nextMBB;
10041}
10042
Eric Christopherf83a5de2009-08-27 18:08:16 +000010043// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010044// or XMM0_V32I8 in AVX all of this code can be replaced with that
10045// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010046MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010047X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010048 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010049 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10050 "Target must have SSE4.2 or AVX features enabled");
10051
Eric Christopherb120ab42009-08-18 22:50:32 +000010052 DebugLoc dl = MI->getDebugLoc();
10053 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010054 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010055 if (!Subtarget->hasAVX()) {
10056 if (memArg)
10057 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10058 else
10059 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10060 } else {
10061 if (memArg)
10062 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10063 else
10064 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10065 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010066
Eric Christopher41c902f2010-11-30 08:20:21 +000010067 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010068 for (unsigned i = 0; i < numArgs; ++i) {
10069 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010070 if (!(Op.isReg() && Op.isImplicit()))
10071 MIB.addOperand(Op);
10072 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010073 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010074 .addReg(X86::XMM0);
10075
Dan Gohman14152b42010-07-06 20:24:04 +000010076 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010077 return BB;
10078}
10079
10080MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010081X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010082 DebugLoc dl = MI->getDebugLoc();
10083 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010084
Eric Christopher228232b2010-11-30 07:20:12 +000010085 // Address into RAX/EAX, other two args into ECX, EDX.
10086 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10087 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10088 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10089 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010090 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010091
Eric Christopher228232b2010-11-30 07:20:12 +000010092 unsigned ValOps = X86::AddrNumOperands;
10093 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10094 .addReg(MI->getOperand(ValOps).getReg());
10095 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10096 .addReg(MI->getOperand(ValOps+1).getReg());
10097
10098 // The instruction doesn't actually take any operands though.
10099 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010100
Eric Christopher228232b2010-11-30 07:20:12 +000010101 MI->eraseFromParent(); // The pseudo is gone now.
10102 return BB;
10103}
10104
10105MachineBasicBlock *
10106X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010107 DebugLoc dl = MI->getDebugLoc();
10108 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010109
Eric Christopher228232b2010-11-30 07:20:12 +000010110 // First arg in ECX, the second in EAX.
10111 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10112 .addReg(MI->getOperand(0).getReg());
10113 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10114 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010115
Eric Christopher228232b2010-11-30 07:20:12 +000010116 // The instruction doesn't actually take any operands though.
10117 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010118
Eric Christopher228232b2010-11-30 07:20:12 +000010119 MI->eraseFromParent(); // The pseudo is gone now.
10120 return BB;
10121}
10122
10123MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010124X86TargetLowering::EmitVAARG64WithCustomInserter(
10125 MachineInstr *MI,
10126 MachineBasicBlock *MBB) const {
10127 // Emit va_arg instruction on X86-64.
10128
10129 // Operands to this pseudo-instruction:
10130 // 0 ) Output : destination address (reg)
10131 // 1-5) Input : va_list address (addr, i64mem)
10132 // 6 ) ArgSize : Size (in bytes) of vararg type
10133 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10134 // 8 ) Align : Alignment of type
10135 // 9 ) EFLAGS (implicit-def)
10136
10137 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10138 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10139
10140 unsigned DestReg = MI->getOperand(0).getReg();
10141 MachineOperand &Base = MI->getOperand(1);
10142 MachineOperand &Scale = MI->getOperand(2);
10143 MachineOperand &Index = MI->getOperand(3);
10144 MachineOperand &Disp = MI->getOperand(4);
10145 MachineOperand &Segment = MI->getOperand(5);
10146 unsigned ArgSize = MI->getOperand(6).getImm();
10147 unsigned ArgMode = MI->getOperand(7).getImm();
10148 unsigned Align = MI->getOperand(8).getImm();
10149
10150 // Memory Reference
10151 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10152 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10153 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10154
10155 // Machine Information
10156 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10157 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10158 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10159 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10160 DebugLoc DL = MI->getDebugLoc();
10161
10162 // struct va_list {
10163 // i32 gp_offset
10164 // i32 fp_offset
10165 // i64 overflow_area (address)
10166 // i64 reg_save_area (address)
10167 // }
10168 // sizeof(va_list) = 24
10169 // alignment(va_list) = 8
10170
10171 unsigned TotalNumIntRegs = 6;
10172 unsigned TotalNumXMMRegs = 8;
10173 bool UseGPOffset = (ArgMode == 1);
10174 bool UseFPOffset = (ArgMode == 2);
10175 unsigned MaxOffset = TotalNumIntRegs * 8 +
10176 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10177
10178 /* Align ArgSize to a multiple of 8 */
10179 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10180 bool NeedsAlign = (Align > 8);
10181
10182 MachineBasicBlock *thisMBB = MBB;
10183 MachineBasicBlock *overflowMBB;
10184 MachineBasicBlock *offsetMBB;
10185 MachineBasicBlock *endMBB;
10186
10187 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10188 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10189 unsigned OffsetReg = 0;
10190
10191 if (!UseGPOffset && !UseFPOffset) {
10192 // If we only pull from the overflow region, we don't create a branch.
10193 // We don't need to alter control flow.
10194 OffsetDestReg = 0; // unused
10195 OverflowDestReg = DestReg;
10196
10197 offsetMBB = NULL;
10198 overflowMBB = thisMBB;
10199 endMBB = thisMBB;
10200 } else {
10201 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10202 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10203 // If not, pull from overflow_area. (branch to overflowMBB)
10204 //
10205 // thisMBB
10206 // | .
10207 // | .
10208 // offsetMBB overflowMBB
10209 // | .
10210 // | .
10211 // endMBB
10212
10213 // Registers for the PHI in endMBB
10214 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10215 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10216
10217 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10218 MachineFunction *MF = MBB->getParent();
10219 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10220 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10221 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10222
10223 MachineFunction::iterator MBBIter = MBB;
10224 ++MBBIter;
10225
10226 // Insert the new basic blocks
10227 MF->insert(MBBIter, offsetMBB);
10228 MF->insert(MBBIter, overflowMBB);
10229 MF->insert(MBBIter, endMBB);
10230
10231 // Transfer the remainder of MBB and its successor edges to endMBB.
10232 endMBB->splice(endMBB->begin(), thisMBB,
10233 llvm::next(MachineBasicBlock::iterator(MI)),
10234 thisMBB->end());
10235 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10236
10237 // Make offsetMBB and overflowMBB successors of thisMBB
10238 thisMBB->addSuccessor(offsetMBB);
10239 thisMBB->addSuccessor(overflowMBB);
10240
10241 // endMBB is a successor of both offsetMBB and overflowMBB
10242 offsetMBB->addSuccessor(endMBB);
10243 overflowMBB->addSuccessor(endMBB);
10244
10245 // Load the offset value into a register
10246 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10247 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10248 .addOperand(Base)
10249 .addOperand(Scale)
10250 .addOperand(Index)
10251 .addDisp(Disp, UseFPOffset ? 4 : 0)
10252 .addOperand(Segment)
10253 .setMemRefs(MMOBegin, MMOEnd);
10254
10255 // Check if there is enough room left to pull this argument.
10256 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10257 .addReg(OffsetReg)
10258 .addImm(MaxOffset + 8 - ArgSizeA8);
10259
10260 // Branch to "overflowMBB" if offset >= max
10261 // Fall through to "offsetMBB" otherwise
10262 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10263 .addMBB(overflowMBB);
10264 }
10265
10266 // In offsetMBB, emit code to use the reg_save_area.
10267 if (offsetMBB) {
10268 assert(OffsetReg != 0);
10269
10270 // Read the reg_save_area address.
10271 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10272 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10273 .addOperand(Base)
10274 .addOperand(Scale)
10275 .addOperand(Index)
10276 .addDisp(Disp, 16)
10277 .addOperand(Segment)
10278 .setMemRefs(MMOBegin, MMOEnd);
10279
10280 // Zero-extend the offset
10281 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10282 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10283 .addImm(0)
10284 .addReg(OffsetReg)
10285 .addImm(X86::sub_32bit);
10286
10287 // Add the offset to the reg_save_area to get the final address.
10288 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10289 .addReg(OffsetReg64)
10290 .addReg(RegSaveReg);
10291
10292 // Compute the offset for the next argument
10293 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10294 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10295 .addReg(OffsetReg)
10296 .addImm(UseFPOffset ? 16 : 8);
10297
10298 // Store it back into the va_list.
10299 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10300 .addOperand(Base)
10301 .addOperand(Scale)
10302 .addOperand(Index)
10303 .addDisp(Disp, UseFPOffset ? 4 : 0)
10304 .addOperand(Segment)
10305 .addReg(NextOffsetReg)
10306 .setMemRefs(MMOBegin, MMOEnd);
10307
10308 // Jump to endMBB
10309 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10310 .addMBB(endMBB);
10311 }
10312
10313 //
10314 // Emit code to use overflow area
10315 //
10316
10317 // Load the overflow_area address into a register.
10318 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10319 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10320 .addOperand(Base)
10321 .addOperand(Scale)
10322 .addOperand(Index)
10323 .addDisp(Disp, 8)
10324 .addOperand(Segment)
10325 .setMemRefs(MMOBegin, MMOEnd);
10326
10327 // If we need to align it, do so. Otherwise, just copy the address
10328 // to OverflowDestReg.
10329 if (NeedsAlign) {
10330 // Align the overflow address
10331 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10332 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10333
10334 // aligned_addr = (addr + (align-1)) & ~(align-1)
10335 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10336 .addReg(OverflowAddrReg)
10337 .addImm(Align-1);
10338
10339 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10340 .addReg(TmpReg)
10341 .addImm(~(uint64_t)(Align-1));
10342 } else {
10343 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10344 .addReg(OverflowAddrReg);
10345 }
10346
10347 // Compute the next overflow address after this argument.
10348 // (the overflow address should be kept 8-byte aligned)
10349 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10350 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10351 .addReg(OverflowDestReg)
10352 .addImm(ArgSizeA8);
10353
10354 // Store the new overflow address.
10355 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10356 .addOperand(Base)
10357 .addOperand(Scale)
10358 .addOperand(Index)
10359 .addDisp(Disp, 8)
10360 .addOperand(Segment)
10361 .addReg(NextAddrReg)
10362 .setMemRefs(MMOBegin, MMOEnd);
10363
10364 // If we branched, emit the PHI to the front of endMBB.
10365 if (offsetMBB) {
10366 BuildMI(*endMBB, endMBB->begin(), DL,
10367 TII->get(X86::PHI), DestReg)
10368 .addReg(OffsetDestReg).addMBB(offsetMBB)
10369 .addReg(OverflowDestReg).addMBB(overflowMBB);
10370 }
10371
10372 // Erase the pseudo instruction
10373 MI->eraseFromParent();
10374
10375 return endMBB;
10376}
10377
10378MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010379X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10380 MachineInstr *MI,
10381 MachineBasicBlock *MBB) const {
10382 // Emit code to save XMM registers to the stack. The ABI says that the
10383 // number of registers to save is given in %al, so it's theoretically
10384 // possible to do an indirect jump trick to avoid saving all of them,
10385 // however this code takes a simpler approach and just executes all
10386 // of the stores if %al is non-zero. It's less code, and it's probably
10387 // easier on the hardware branch predictor, and stores aren't all that
10388 // expensive anyway.
10389
10390 // Create the new basic blocks. One block contains all the XMM stores,
10391 // and one block is the final destination regardless of whether any
10392 // stores were performed.
10393 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10394 MachineFunction *F = MBB->getParent();
10395 MachineFunction::iterator MBBIter = MBB;
10396 ++MBBIter;
10397 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10398 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10399 F->insert(MBBIter, XMMSaveMBB);
10400 F->insert(MBBIter, EndMBB);
10401
Dan Gohman14152b42010-07-06 20:24:04 +000010402 // Transfer the remainder of MBB and its successor edges to EndMBB.
10403 EndMBB->splice(EndMBB->begin(), MBB,
10404 llvm::next(MachineBasicBlock::iterator(MI)),
10405 MBB->end());
10406 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10407
Dan Gohmand6708ea2009-08-15 01:38:56 +000010408 // The original block will now fall through to the XMM save block.
10409 MBB->addSuccessor(XMMSaveMBB);
10410 // The XMMSaveMBB will fall through to the end block.
10411 XMMSaveMBB->addSuccessor(EndMBB);
10412
10413 // Now add the instructions.
10414 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10415 DebugLoc DL = MI->getDebugLoc();
10416
10417 unsigned CountReg = MI->getOperand(0).getReg();
10418 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10419 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10420
10421 if (!Subtarget->isTargetWin64()) {
10422 // If %al is 0, branch around the XMM save block.
10423 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010424 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010425 MBB->addSuccessor(EndMBB);
10426 }
10427
10428 // In the XMM save block, save all the XMM argument registers.
10429 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10430 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010431 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010432 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010433 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010434 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010435 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010436 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10437 .addFrameIndex(RegSaveFrameIndex)
10438 .addImm(/*Scale=*/1)
10439 .addReg(/*IndexReg=*/0)
10440 .addImm(/*Disp=*/Offset)
10441 .addReg(/*Segment=*/0)
10442 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010443 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010444 }
10445
Dan Gohman14152b42010-07-06 20:24:04 +000010446 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010447
10448 return EndMBB;
10449}
Mon P Wang63307c32008-05-05 19:05:59 +000010450
Evan Cheng60c07e12006-07-05 22:17:51 +000010451MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010452X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010453 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010454 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10455 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010456
Chris Lattner52600972009-09-02 05:57:00 +000010457 // To "insert" a SELECT_CC instruction, we actually have to insert the
10458 // diamond control-flow pattern. The incoming instruction knows the
10459 // destination vreg to set, the condition code register to branch on, the
10460 // true/false values to select between, and a branch opcode to use.
10461 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10462 MachineFunction::iterator It = BB;
10463 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010464
Chris Lattner52600972009-09-02 05:57:00 +000010465 // thisMBB:
10466 // ...
10467 // TrueVal = ...
10468 // cmpTY ccX, r1, r2
10469 // bCC copy1MBB
10470 // fallthrough --> copy0MBB
10471 MachineBasicBlock *thisMBB = BB;
10472 MachineFunction *F = BB->getParent();
10473 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10474 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010475 F->insert(It, copy0MBB);
10476 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010477
Bill Wendling730c07e2010-06-25 20:48:10 +000010478 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10479 // live into the sink and copy blocks.
10480 const MachineFunction *MF = BB->getParent();
10481 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10482 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010483
Dan Gohman14152b42010-07-06 20:24:04 +000010484 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10485 const MachineOperand &MO = MI->getOperand(I);
10486 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010487 unsigned Reg = MO.getReg();
10488 if (Reg != X86::EFLAGS) continue;
10489 copy0MBB->addLiveIn(Reg);
10490 sinkMBB->addLiveIn(Reg);
10491 }
10492
Dan Gohman14152b42010-07-06 20:24:04 +000010493 // Transfer the remainder of BB and its successor edges to sinkMBB.
10494 sinkMBB->splice(sinkMBB->begin(), BB,
10495 llvm::next(MachineBasicBlock::iterator(MI)),
10496 BB->end());
10497 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10498
10499 // Add the true and fallthrough blocks as its successors.
10500 BB->addSuccessor(copy0MBB);
10501 BB->addSuccessor(sinkMBB);
10502
10503 // Create the conditional branch instruction.
10504 unsigned Opc =
10505 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10506 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10507
Chris Lattner52600972009-09-02 05:57:00 +000010508 // copy0MBB:
10509 // %FalseValue = ...
10510 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010511 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010512
Chris Lattner52600972009-09-02 05:57:00 +000010513 // sinkMBB:
10514 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10515 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010516 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10517 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010518 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10519 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10520
Dan Gohman14152b42010-07-06 20:24:04 +000010521 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010522 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010523}
10524
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010525MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010526X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010527 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010528 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10529 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010530
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010531 assert(!Subtarget->isTargetEnvMacho());
10532
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010533 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10534 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010535
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010536 if (Subtarget->isTargetWin64()) {
10537 if (Subtarget->isTargetCygMing()) {
10538 // ___chkstk(Mingw64):
10539 // Clobbers R10, R11, RAX and EFLAGS.
10540 // Updates RSP.
10541 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10542 .addExternalSymbol("___chkstk")
10543 .addReg(X86::RAX, RegState::Implicit)
10544 .addReg(X86::RSP, RegState::Implicit)
10545 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10546 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10547 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10548 } else {
10549 // __chkstk(MSVCRT): does not update stack pointer.
10550 // Clobbers R10, R11 and EFLAGS.
10551 // FIXME: RAX(allocated size) might be reused and not killed.
10552 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10553 .addExternalSymbol("__chkstk")
10554 .addReg(X86::RAX, RegState::Implicit)
10555 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10556 // RAX has the offset to subtracted from RSP.
10557 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10558 .addReg(X86::RSP)
10559 .addReg(X86::RAX);
10560 }
10561 } else {
10562 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010563 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10564
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010565 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10566 .addExternalSymbol(StackProbeSymbol)
10567 .addReg(X86::EAX, RegState::Implicit)
10568 .addReg(X86::ESP, RegState::Implicit)
10569 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10570 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10571 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10572 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010573
Dan Gohman14152b42010-07-06 20:24:04 +000010574 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010575 return BB;
10576}
Chris Lattner52600972009-09-02 05:57:00 +000010577
10578MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010579X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10580 MachineBasicBlock *BB) const {
10581 // This is pretty easy. We're taking the value that we received from
10582 // our load from the relocation, sticking it in either RDI (x86-64)
10583 // or EAX and doing an indirect call. The return value will then
10584 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010585 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010586 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010587 DebugLoc DL = MI->getDebugLoc();
10588 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010589
10590 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010591 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010592
Eric Christopher30ef0e52010-06-03 04:07:48 +000010593 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010594 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10595 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010596 .addReg(X86::RIP)
10597 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010598 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010599 MI->getOperand(3).getTargetFlags())
10600 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010601 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010602 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010603 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010604 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10605 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010606 .addReg(0)
10607 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010608 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010609 MI->getOperand(3).getTargetFlags())
10610 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010611 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010612 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010613 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010614 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10615 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010616 .addReg(TII->getGlobalBaseReg(F))
10617 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010618 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010619 MI->getOperand(3).getTargetFlags())
10620 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010621 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010622 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010623 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010624
Dan Gohman14152b42010-07-06 20:24:04 +000010625 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010626 return BB;
10627}
10628
10629MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010630X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010631 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010632 switch (MI->getOpcode()) {
10633 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010634 case X86::TAILJMPd64:
10635 case X86::TAILJMPr64:
10636 case X86::TAILJMPm64:
10637 assert(!"TAILJMP64 would not be touched here.");
10638 case X86::TCRETURNdi64:
10639 case X86::TCRETURNri64:
10640 case X86::TCRETURNmi64:
10641 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10642 // On AMD64, additional defs should be added before register allocation.
10643 if (!Subtarget->isTargetWin64()) {
10644 MI->addRegisterDefined(X86::RSI);
10645 MI->addRegisterDefined(X86::RDI);
10646 MI->addRegisterDefined(X86::XMM6);
10647 MI->addRegisterDefined(X86::XMM7);
10648 MI->addRegisterDefined(X86::XMM8);
10649 MI->addRegisterDefined(X86::XMM9);
10650 MI->addRegisterDefined(X86::XMM10);
10651 MI->addRegisterDefined(X86::XMM11);
10652 MI->addRegisterDefined(X86::XMM12);
10653 MI->addRegisterDefined(X86::XMM13);
10654 MI->addRegisterDefined(X86::XMM14);
10655 MI->addRegisterDefined(X86::XMM15);
10656 }
10657 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010658 case X86::WIN_ALLOCA:
10659 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010660 case X86::TLSCall_32:
10661 case X86::TLSCall_64:
10662 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010663 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010664 case X86::CMOV_FR32:
10665 case X86::CMOV_FR64:
10666 case X86::CMOV_V4F32:
10667 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010668 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010669 case X86::CMOV_GR16:
10670 case X86::CMOV_GR32:
10671 case X86::CMOV_RFP32:
10672 case X86::CMOV_RFP64:
10673 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010674 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010675
Dale Johannesen849f2142007-07-03 00:53:03 +000010676 case X86::FP32_TO_INT16_IN_MEM:
10677 case X86::FP32_TO_INT32_IN_MEM:
10678 case X86::FP32_TO_INT64_IN_MEM:
10679 case X86::FP64_TO_INT16_IN_MEM:
10680 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010681 case X86::FP64_TO_INT64_IN_MEM:
10682 case X86::FP80_TO_INT16_IN_MEM:
10683 case X86::FP80_TO_INT32_IN_MEM:
10684 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010685 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10686 DebugLoc DL = MI->getDebugLoc();
10687
Evan Cheng60c07e12006-07-05 22:17:51 +000010688 // Change the floating point control register to use "round towards zero"
10689 // mode when truncating to an integer value.
10690 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010691 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010692 addFrameReference(BuildMI(*BB, MI, DL,
10693 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010694
10695 // Load the old value of the high byte of the control word...
10696 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010697 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010698 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010699 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010700
10701 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010702 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010703 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010704
10705 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010706 addFrameReference(BuildMI(*BB, MI, DL,
10707 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010708
10709 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010710 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010711 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010712
10713 // Get the X86 opcode to use.
10714 unsigned Opc;
10715 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010716 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010717 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10718 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10719 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10720 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10721 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10722 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010723 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10724 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10725 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010726 }
10727
10728 X86AddressMode AM;
10729 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010730 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010731 AM.BaseType = X86AddressMode::RegBase;
10732 AM.Base.Reg = Op.getReg();
10733 } else {
10734 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010735 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010736 }
10737 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010738 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010739 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010740 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010741 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010742 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010743 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010744 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010745 AM.GV = Op.getGlobal();
10746 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010747 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010748 }
Dan Gohman14152b42010-07-06 20:24:04 +000010749 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010750 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010751
10752 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010753 addFrameReference(BuildMI(*BB, MI, DL,
10754 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010755
Dan Gohman14152b42010-07-06 20:24:04 +000010756 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010757 return BB;
10758 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010759 // String/text processing lowering.
10760 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010761 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010762 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10763 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010764 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010765 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10766 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010767 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010768 return EmitPCMP(MI, BB, 5, false /* in mem */);
10769 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010770 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010771 return EmitPCMP(MI, BB, 5, true /* in mem */);
10772
Eric Christopher228232b2010-11-30 07:20:12 +000010773 // Thread synchronization.
10774 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010775 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010776 case X86::MWAIT:
10777 return EmitMwait(MI, BB);
10778
Eric Christopherb120ab42009-08-18 22:50:32 +000010779 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010780 case X86::ATOMAND32:
10781 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010782 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010783 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010784 X86::NOT32r, X86::EAX,
10785 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010786 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010787 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10788 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010789 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010790 X86::NOT32r, X86::EAX,
10791 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010792 case X86::ATOMXOR32:
10793 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010794 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010795 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010796 X86::NOT32r, X86::EAX,
10797 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010798 case X86::ATOMNAND32:
10799 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010800 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010801 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010802 X86::NOT32r, X86::EAX,
10803 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010804 case X86::ATOMMIN32:
10805 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10806 case X86::ATOMMAX32:
10807 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10808 case X86::ATOMUMIN32:
10809 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10810 case X86::ATOMUMAX32:
10811 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010812
10813 case X86::ATOMAND16:
10814 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10815 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010816 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010817 X86::NOT16r, X86::AX,
10818 X86::GR16RegisterClass);
10819 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010820 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010821 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010822 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010823 X86::NOT16r, X86::AX,
10824 X86::GR16RegisterClass);
10825 case X86::ATOMXOR16:
10826 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10827 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010828 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010829 X86::NOT16r, X86::AX,
10830 X86::GR16RegisterClass);
10831 case X86::ATOMNAND16:
10832 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10833 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010834 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010835 X86::NOT16r, X86::AX,
10836 X86::GR16RegisterClass, true);
10837 case X86::ATOMMIN16:
10838 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10839 case X86::ATOMMAX16:
10840 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10841 case X86::ATOMUMIN16:
10842 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10843 case X86::ATOMUMAX16:
10844 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10845
10846 case X86::ATOMAND8:
10847 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10848 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010849 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010850 X86::NOT8r, X86::AL,
10851 X86::GR8RegisterClass);
10852 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010853 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010854 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010855 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010856 X86::NOT8r, X86::AL,
10857 X86::GR8RegisterClass);
10858 case X86::ATOMXOR8:
10859 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10860 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010861 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010862 X86::NOT8r, X86::AL,
10863 X86::GR8RegisterClass);
10864 case X86::ATOMNAND8:
10865 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10866 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010867 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010868 X86::NOT8r, X86::AL,
10869 X86::GR8RegisterClass, true);
10870 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010871 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010872 case X86::ATOMAND64:
10873 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010874 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010875 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010876 X86::NOT64r, X86::RAX,
10877 X86::GR64RegisterClass);
10878 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010879 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10880 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010881 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010882 X86::NOT64r, X86::RAX,
10883 X86::GR64RegisterClass);
10884 case X86::ATOMXOR64:
10885 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010886 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010887 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010888 X86::NOT64r, X86::RAX,
10889 X86::GR64RegisterClass);
10890 case X86::ATOMNAND64:
10891 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10892 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010893 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010894 X86::NOT64r, X86::RAX,
10895 X86::GR64RegisterClass, true);
10896 case X86::ATOMMIN64:
10897 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10898 case X86::ATOMMAX64:
10899 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10900 case X86::ATOMUMIN64:
10901 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10902 case X86::ATOMUMAX64:
10903 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010904
10905 // This group does 64-bit operations on a 32-bit host.
10906 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010907 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010908 X86::AND32rr, X86::AND32rr,
10909 X86::AND32ri, X86::AND32ri,
10910 false);
10911 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010912 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010913 X86::OR32rr, X86::OR32rr,
10914 X86::OR32ri, X86::OR32ri,
10915 false);
10916 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010917 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010918 X86::XOR32rr, X86::XOR32rr,
10919 X86::XOR32ri, X86::XOR32ri,
10920 false);
10921 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010922 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010923 X86::AND32rr, X86::AND32rr,
10924 X86::AND32ri, X86::AND32ri,
10925 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010926 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010927 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010928 X86::ADD32rr, X86::ADC32rr,
10929 X86::ADD32ri, X86::ADC32ri,
10930 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010931 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010932 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010933 X86::SUB32rr, X86::SBB32rr,
10934 X86::SUB32ri, X86::SBB32ri,
10935 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010936 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010937 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010938 X86::MOV32rr, X86::MOV32rr,
10939 X86::MOV32ri, X86::MOV32ri,
10940 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010941 case X86::VASTART_SAVE_XMM_REGS:
10942 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010943
10944 case X86::VAARG_64:
10945 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010946 }
10947}
10948
10949//===----------------------------------------------------------------------===//
10950// X86 Optimization Hooks
10951//===----------------------------------------------------------------------===//
10952
Dan Gohman475871a2008-07-27 21:46:04 +000010953void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010954 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010955 APInt &KnownZero,
10956 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010957 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010958 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010959 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010960 assert((Opc >= ISD::BUILTIN_OP_END ||
10961 Opc == ISD::INTRINSIC_WO_CHAIN ||
10962 Opc == ISD::INTRINSIC_W_CHAIN ||
10963 Opc == ISD::INTRINSIC_VOID) &&
10964 "Should use MaskedValueIsZero if you don't know whether Op"
10965 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010966
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010967 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010968 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010969 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010970 case X86ISD::ADD:
10971 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010972 case X86ISD::ADC:
10973 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010974 case X86ISD::SMUL:
10975 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010976 case X86ISD::INC:
10977 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010978 case X86ISD::OR:
10979 case X86ISD::XOR:
10980 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010981 // These nodes' second result is a boolean.
10982 if (Op.getResNo() == 0)
10983 break;
10984 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010985 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010986 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10987 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010988 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010989 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010990}
Chris Lattner259e97c2006-01-31 19:43:35 +000010991
Owen Andersonbc146b02010-09-21 20:42:50 +000010992unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10993 unsigned Depth) const {
10994 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10995 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10996 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010997
Owen Andersonbc146b02010-09-21 20:42:50 +000010998 // Fallback case.
10999 return 1;
11000}
11001
Evan Cheng206ee9d2006-07-07 08:33:52 +000011002/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000011003/// node is a GlobalAddress + offset.
11004bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000011005 const GlobalValue* &GA,
11006 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000011007 if (N->getOpcode() == X86ISD::Wrapper) {
11008 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011009 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000011010 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011011 return true;
11012 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000011013 }
Evan Chengad4196b2008-05-12 19:56:52 +000011014 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011015}
11016
Evan Cheng206ee9d2006-07-07 08:33:52 +000011017/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
11018/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
11019/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000011020/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011021static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011022 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011023 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011024 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011025
Eli Friedman7a5e5552009-06-07 06:52:44 +000011026 if (VT.getSizeInBits() != 128)
11027 return SDValue();
11028
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011029 // Don't create instructions with illegal types after legalize types has run.
11030 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11031 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11032 return SDValue();
11033
Nate Begemanfdea31a2010-03-24 20:49:50 +000011034 SmallVector<SDValue, 16> Elts;
11035 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011036 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011037
Nate Begemanfdea31a2010-03-24 20:49:50 +000011038 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011039}
Evan Chengd880b972008-05-09 21:53:03 +000011040
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011041/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11042/// generation and convert it from being a bunch of shuffles and extracts
11043/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011044static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11045 const TargetLowering &TLI) {
11046 SDValue InputVector = N->getOperand(0);
11047
11048 // Only operate on vectors of 4 elements, where the alternative shuffling
11049 // gets to be more expensive.
11050 if (InputVector.getValueType() != MVT::v4i32)
11051 return SDValue();
11052
11053 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11054 // single use which is a sign-extend or zero-extend, and all elements are
11055 // used.
11056 SmallVector<SDNode *, 4> Uses;
11057 unsigned ExtractedElements = 0;
11058 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11059 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11060 if (UI.getUse().getResNo() != InputVector.getResNo())
11061 return SDValue();
11062
11063 SDNode *Extract = *UI;
11064 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11065 return SDValue();
11066
11067 if (Extract->getValueType(0) != MVT::i32)
11068 return SDValue();
11069 if (!Extract->hasOneUse())
11070 return SDValue();
11071 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11072 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11073 return SDValue();
11074 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11075 return SDValue();
11076
11077 // Record which element was extracted.
11078 ExtractedElements |=
11079 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11080
11081 Uses.push_back(Extract);
11082 }
11083
11084 // If not all the elements were used, this may not be worthwhile.
11085 if (ExtractedElements != 15)
11086 return SDValue();
11087
11088 // Ok, we've now decided to do the transformation.
11089 DebugLoc dl = InputVector.getDebugLoc();
11090
11091 // Store the value to a temporary stack slot.
11092 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011093 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11094 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011095
11096 // Replace each use (extract) with a load of the appropriate element.
11097 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11098 UE = Uses.end(); UI != UE; ++UI) {
11099 SDNode *Extract = *UI;
11100
Nadav Rotem86694292011-05-17 08:31:57 +000011101 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011102 SDValue Idx = Extract->getOperand(1);
11103 unsigned EltSize =
11104 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11105 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11106 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11107
Nadav Rotem86694292011-05-17 08:31:57 +000011108 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011109 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011110
11111 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011112 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011113 ScalarAddr, MachinePointerInfo(),
11114 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011115
11116 // Replace the exact with the load.
11117 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11118 }
11119
11120 // The replacement was made in place; don't return anything.
11121 return SDValue();
11122}
11123
Chris Lattner83e6c992006-10-04 06:57:07 +000011124/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011125static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011126 const X86Subtarget *Subtarget) {
11127 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011128 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011129 // Get the LHS/RHS of the select.
11130 SDValue LHS = N->getOperand(1);
11131 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011132
Dan Gohman670e5392009-09-21 18:03:22 +000011133 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011134 // instructions match the semantics of the common C idiom x<y?x:y but not
11135 // x<=y?x:y, because of how they handle negative zero (which can be
11136 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011137 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011138 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011139 Cond.getOpcode() == ISD::SETCC) {
11140 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011141
Chris Lattner47b4ce82009-03-11 05:48:52 +000011142 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011143 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011144 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11145 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011146 switch (CC) {
11147 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011148 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011149 // Converting this to a min would handle NaNs incorrectly, and swapping
11150 // the operands would cause it to handle comparisons between positive
11151 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011152 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011153 if (!UnsafeFPMath &&
11154 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11155 break;
11156 std::swap(LHS, RHS);
11157 }
Dan Gohman670e5392009-09-21 18:03:22 +000011158 Opcode = X86ISD::FMIN;
11159 break;
11160 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011161 // Converting this to a min would handle comparisons between positive
11162 // and negative zero incorrectly.
11163 if (!UnsafeFPMath &&
11164 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11165 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011166 Opcode = X86ISD::FMIN;
11167 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011168 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011169 // Converting this to a min would handle both negative zeros and NaNs
11170 // incorrectly, but we can swap the operands to fix both.
11171 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011172 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011173 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011174 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011175 Opcode = X86ISD::FMIN;
11176 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011177
Dan Gohman670e5392009-09-21 18:03:22 +000011178 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011179 // Converting this to a max would handle comparisons between positive
11180 // and negative zero incorrectly.
11181 if (!UnsafeFPMath &&
11182 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11183 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011184 Opcode = X86ISD::FMAX;
11185 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011186 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011187 // Converting this to a max would handle NaNs incorrectly, and swapping
11188 // the operands would cause it to handle comparisons between positive
11189 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011190 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011191 if (!UnsafeFPMath &&
11192 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11193 break;
11194 std::swap(LHS, RHS);
11195 }
Dan Gohman670e5392009-09-21 18:03:22 +000011196 Opcode = X86ISD::FMAX;
11197 break;
11198 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011199 // Converting this to a max would handle both negative zeros and NaNs
11200 // incorrectly, but we can swap the operands to fix both.
11201 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011202 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011203 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011204 case ISD::SETGE:
11205 Opcode = X86ISD::FMAX;
11206 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011207 }
Dan Gohman670e5392009-09-21 18:03:22 +000011208 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011209 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11210 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011211 switch (CC) {
11212 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011213 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011214 // Converting this to a min would handle comparisons between positive
11215 // and negative zero incorrectly, and swapping the operands would
11216 // cause it to handle NaNs incorrectly.
11217 if (!UnsafeFPMath &&
11218 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011219 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011220 break;
11221 std::swap(LHS, RHS);
11222 }
Dan Gohman670e5392009-09-21 18:03:22 +000011223 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011224 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011225 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011226 // Converting this to a min would handle NaNs incorrectly.
11227 if (!UnsafeFPMath &&
11228 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11229 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011230 Opcode = X86ISD::FMIN;
11231 break;
11232 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011233 // Converting this to a min would handle both negative zeros and NaNs
11234 // incorrectly, but we can swap the operands to fix both.
11235 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011236 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011237 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011238 case ISD::SETGE:
11239 Opcode = X86ISD::FMIN;
11240 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011241
Dan Gohman670e5392009-09-21 18:03:22 +000011242 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011243 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011244 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011245 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011246 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011247 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011248 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011249 // Converting this to a max would handle comparisons between positive
11250 // and negative zero incorrectly, and swapping the operands would
11251 // cause it to handle NaNs incorrectly.
11252 if (!UnsafeFPMath &&
11253 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011254 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011255 break;
11256 std::swap(LHS, RHS);
11257 }
Dan Gohman670e5392009-09-21 18:03:22 +000011258 Opcode = X86ISD::FMAX;
11259 break;
11260 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011261 // Converting this to a max would handle both negative zeros and NaNs
11262 // incorrectly, but we can swap the operands to fix both.
11263 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011264 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011265 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011266 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011267 Opcode = X86ISD::FMAX;
11268 break;
11269 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011270 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011271
Chris Lattner47b4ce82009-03-11 05:48:52 +000011272 if (Opcode)
11273 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011274 }
Eric Christopherfd179292009-08-27 18:07:15 +000011275
Chris Lattnerd1980a52009-03-12 06:52:53 +000011276 // If this is a select between two integer constants, try to do some
11277 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011278 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11279 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011280 // Don't do this for crazy integer types.
11281 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11282 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011283 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011284 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011285
Chris Lattnercee56e72009-03-13 05:53:31 +000011286 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011287 // Efficiently invertible.
11288 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11289 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11290 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11291 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011292 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011293 }
Eric Christopherfd179292009-08-27 18:07:15 +000011294
Chris Lattnerd1980a52009-03-12 06:52:53 +000011295 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011296 if (FalseC->getAPIntValue() == 0 &&
11297 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011298 if (NeedsCondInvert) // Invert the condition if needed.
11299 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11300 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011301
Chris Lattnerd1980a52009-03-12 06:52:53 +000011302 // Zero extend the condition if needed.
11303 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011304
Chris Lattnercee56e72009-03-13 05:53:31 +000011305 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011306 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011307 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011308 }
Eric Christopherfd179292009-08-27 18:07:15 +000011309
Chris Lattner97a29a52009-03-13 05:22:11 +000011310 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011311 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011312 if (NeedsCondInvert) // Invert the condition if needed.
11313 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11314 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011315
Chris Lattner97a29a52009-03-13 05:22:11 +000011316 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011317 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11318 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011319 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011320 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011321 }
Eric Christopherfd179292009-08-27 18:07:15 +000011322
Chris Lattnercee56e72009-03-13 05:53:31 +000011323 // Optimize cases that will turn into an LEA instruction. This requires
11324 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011325 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011326 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011327 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011328
Chris Lattnercee56e72009-03-13 05:53:31 +000011329 bool isFastMultiplier = false;
11330 if (Diff < 10) {
11331 switch ((unsigned char)Diff) {
11332 default: break;
11333 case 1: // result = add base, cond
11334 case 2: // result = lea base( , cond*2)
11335 case 3: // result = lea base(cond, cond*2)
11336 case 4: // result = lea base( , cond*4)
11337 case 5: // result = lea base(cond, cond*4)
11338 case 8: // result = lea base( , cond*8)
11339 case 9: // result = lea base(cond, cond*8)
11340 isFastMultiplier = true;
11341 break;
11342 }
11343 }
Eric Christopherfd179292009-08-27 18:07:15 +000011344
Chris Lattnercee56e72009-03-13 05:53:31 +000011345 if (isFastMultiplier) {
11346 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11347 if (NeedsCondInvert) // Invert the condition if needed.
11348 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11349 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011350
Chris Lattnercee56e72009-03-13 05:53:31 +000011351 // Zero extend the condition if needed.
11352 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11353 Cond);
11354 // Scale the condition by the difference.
11355 if (Diff != 1)
11356 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11357 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011358
Chris Lattnercee56e72009-03-13 05:53:31 +000011359 // Add the base if non-zero.
11360 if (FalseC->getAPIntValue() != 0)
11361 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11362 SDValue(FalseC, 0));
11363 return Cond;
11364 }
Eric Christopherfd179292009-08-27 18:07:15 +000011365 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011366 }
11367 }
Eric Christopherfd179292009-08-27 18:07:15 +000011368
Dan Gohman475871a2008-07-27 21:46:04 +000011369 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011370}
11371
Chris Lattnerd1980a52009-03-12 06:52:53 +000011372/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11373static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11374 TargetLowering::DAGCombinerInfo &DCI) {
11375 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011376
Chris Lattnerd1980a52009-03-12 06:52:53 +000011377 // If the flag operand isn't dead, don't touch this CMOV.
11378 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11379 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011380
Chris Lattnerd1980a52009-03-12 06:52:53 +000011381 // If this is a select between two integer constants, try to do some
11382 // optimizations. Note that the operands are ordered the opposite of SELECT
11383 // operands.
11384 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11385 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11386 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11387 // larger than FalseC (the false value).
11388 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011389
Chris Lattnerd1980a52009-03-12 06:52:53 +000011390 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11391 CC = X86::GetOppositeBranchCondition(CC);
11392 std::swap(TrueC, FalseC);
11393 }
Eric Christopherfd179292009-08-27 18:07:15 +000011394
Chris Lattnerd1980a52009-03-12 06:52:53 +000011395 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011396 // This is efficient for any integer data type (including i8/i16) and
11397 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011398 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11399 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011400 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11401 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011402
Chris Lattnerd1980a52009-03-12 06:52:53 +000011403 // Zero extend the condition if needed.
11404 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011405
Chris Lattnerd1980a52009-03-12 06:52:53 +000011406 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11407 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011408 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011409 if (N->getNumValues() == 2) // Dead flag value?
11410 return DCI.CombineTo(N, Cond, SDValue());
11411 return Cond;
11412 }
Eric Christopherfd179292009-08-27 18:07:15 +000011413
Chris Lattnercee56e72009-03-13 05:53:31 +000011414 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11415 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011416 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11417 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011418 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11419 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011420
Chris Lattner97a29a52009-03-13 05:22:11 +000011421 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011422 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11423 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011424 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11425 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011426
Chris Lattner97a29a52009-03-13 05:22:11 +000011427 if (N->getNumValues() == 2) // Dead flag value?
11428 return DCI.CombineTo(N, Cond, SDValue());
11429 return Cond;
11430 }
Eric Christopherfd179292009-08-27 18:07:15 +000011431
Chris Lattnercee56e72009-03-13 05:53:31 +000011432 // Optimize cases that will turn into an LEA instruction. This requires
11433 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011434 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011435 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011436 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011437
Chris Lattnercee56e72009-03-13 05:53:31 +000011438 bool isFastMultiplier = false;
11439 if (Diff < 10) {
11440 switch ((unsigned char)Diff) {
11441 default: break;
11442 case 1: // result = add base, cond
11443 case 2: // result = lea base( , cond*2)
11444 case 3: // result = lea base(cond, cond*2)
11445 case 4: // result = lea base( , cond*4)
11446 case 5: // result = lea base(cond, cond*4)
11447 case 8: // result = lea base( , cond*8)
11448 case 9: // result = lea base(cond, cond*8)
11449 isFastMultiplier = true;
11450 break;
11451 }
11452 }
Eric Christopherfd179292009-08-27 18:07:15 +000011453
Chris Lattnercee56e72009-03-13 05:53:31 +000011454 if (isFastMultiplier) {
11455 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11456 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011457 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11458 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011459 // Zero extend the condition if needed.
11460 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11461 Cond);
11462 // Scale the condition by the difference.
11463 if (Diff != 1)
11464 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11465 DAG.getConstant(Diff, Cond.getValueType()));
11466
11467 // Add the base if non-zero.
11468 if (FalseC->getAPIntValue() != 0)
11469 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11470 SDValue(FalseC, 0));
11471 if (N->getNumValues() == 2) // Dead flag value?
11472 return DCI.CombineTo(N, Cond, SDValue());
11473 return Cond;
11474 }
Eric Christopherfd179292009-08-27 18:07:15 +000011475 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011476 }
11477 }
11478 return SDValue();
11479}
11480
11481
Evan Cheng0b0cd912009-03-28 05:57:29 +000011482/// PerformMulCombine - Optimize a single multiply with constant into two
11483/// in order to implement it with two cheaper instructions, e.g.
11484/// LEA + SHL, LEA + LEA.
11485static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11486 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011487 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11488 return SDValue();
11489
Owen Andersone50ed302009-08-10 22:56:29 +000011490 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011491 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011492 return SDValue();
11493
11494 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11495 if (!C)
11496 return SDValue();
11497 uint64_t MulAmt = C->getZExtValue();
11498 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11499 return SDValue();
11500
11501 uint64_t MulAmt1 = 0;
11502 uint64_t MulAmt2 = 0;
11503 if ((MulAmt % 9) == 0) {
11504 MulAmt1 = 9;
11505 MulAmt2 = MulAmt / 9;
11506 } else if ((MulAmt % 5) == 0) {
11507 MulAmt1 = 5;
11508 MulAmt2 = MulAmt / 5;
11509 } else if ((MulAmt % 3) == 0) {
11510 MulAmt1 = 3;
11511 MulAmt2 = MulAmt / 3;
11512 }
11513 if (MulAmt2 &&
11514 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11515 DebugLoc DL = N->getDebugLoc();
11516
11517 if (isPowerOf2_64(MulAmt2) &&
11518 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11519 // If second multiplifer is pow2, issue it first. We want the multiply by
11520 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11521 // is an add.
11522 std::swap(MulAmt1, MulAmt2);
11523
11524 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011525 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011526 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011527 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011528 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011529 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011530 DAG.getConstant(MulAmt1, VT));
11531
Eric Christopherfd179292009-08-27 18:07:15 +000011532 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011533 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011534 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011535 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011536 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011537 DAG.getConstant(MulAmt2, VT));
11538
11539 // Do not add new nodes to DAG combiner worklist.
11540 DCI.CombineTo(N, NewMul, false);
11541 }
11542 return SDValue();
11543}
11544
Evan Chengad9c0a32009-12-15 00:53:42 +000011545static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11546 SDValue N0 = N->getOperand(0);
11547 SDValue N1 = N->getOperand(1);
11548 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11549 EVT VT = N0.getValueType();
11550
11551 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11552 // since the result of setcc_c is all zero's or all ones.
11553 if (N1C && N0.getOpcode() == ISD::AND &&
11554 N0.getOperand(1).getOpcode() == ISD::Constant) {
11555 SDValue N00 = N0.getOperand(0);
11556 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11557 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11558 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11559 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11560 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11561 APInt ShAmt = N1C->getAPIntValue();
11562 Mask = Mask.shl(ShAmt);
11563 if (Mask != 0)
11564 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11565 N00, DAG.getConstant(Mask, VT));
11566 }
11567 }
11568
11569 return SDValue();
11570}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011571
Nate Begeman740ab032009-01-26 00:52:55 +000011572/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11573/// when possible.
11574static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11575 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011576 EVT VT = N->getValueType(0);
11577 if (!VT.isVector() && VT.isInteger() &&
11578 N->getOpcode() == ISD::SHL)
11579 return PerformSHLCombine(N, DAG);
11580
Nate Begeman740ab032009-01-26 00:52:55 +000011581 // On X86 with SSE2 support, we can transform this to a vector shift if
11582 // all elements are shifted by the same amount. We can't do this in legalize
11583 // because the a constant vector is typically transformed to a constant pool
11584 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011585 if (!Subtarget->hasSSE2())
11586 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011587
Owen Anderson825b72b2009-08-11 20:47:22 +000011588 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011589 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011590
Mon P Wang3becd092009-01-28 08:12:05 +000011591 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011592 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011593 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011594 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011595 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11596 unsigned NumElts = VT.getVectorNumElements();
11597 unsigned i = 0;
11598 for (; i != NumElts; ++i) {
11599 SDValue Arg = ShAmtOp.getOperand(i);
11600 if (Arg.getOpcode() == ISD::UNDEF) continue;
11601 BaseShAmt = Arg;
11602 break;
11603 }
11604 for (; i != NumElts; ++i) {
11605 SDValue Arg = ShAmtOp.getOperand(i);
11606 if (Arg.getOpcode() == ISD::UNDEF) continue;
11607 if (Arg != BaseShAmt) {
11608 return SDValue();
11609 }
11610 }
11611 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011612 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011613 SDValue InVec = ShAmtOp.getOperand(0);
11614 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11615 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11616 unsigned i = 0;
11617 for (; i != NumElts; ++i) {
11618 SDValue Arg = InVec.getOperand(i);
11619 if (Arg.getOpcode() == ISD::UNDEF) continue;
11620 BaseShAmt = Arg;
11621 break;
11622 }
11623 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011625 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011626 if (C->getZExtValue() == SplatIdx)
11627 BaseShAmt = InVec.getOperand(1);
11628 }
11629 }
11630 if (BaseShAmt.getNode() == 0)
11631 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11632 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011633 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011634 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011635
Mon P Wangefa42202009-09-03 19:56:25 +000011636 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011637 if (EltVT.bitsGT(MVT::i32))
11638 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11639 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011640 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011641
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011642 // The shift amount is identical so we can do a vector shift.
11643 SDValue ValOp = N->getOperand(0);
11644 switch (N->getOpcode()) {
11645 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011646 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011647 break;
11648 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011649 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011650 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011651 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011652 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011653 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011654 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011655 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011656 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011657 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011658 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011659 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011660 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011661 break;
11662 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011663 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011664 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011665 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011666 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011667 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011668 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011669 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011670 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011671 break;
11672 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011673 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011674 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011675 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011676 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011677 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011678 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011679 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011680 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011681 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011682 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011683 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011684 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011685 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011686 }
11687 return SDValue();
11688}
11689
Nate Begemanb65c1752010-12-17 22:55:37 +000011690
11691static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11692 TargetLowering::DAGCombinerInfo &DCI,
11693 const X86Subtarget *Subtarget) {
11694 if (DCI.isBeforeLegalizeOps())
11695 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011696
Nate Begemanb65c1752010-12-17 22:55:37 +000011697 // Want to form PANDN nodes, in the hopes of then easily combining them with
11698 // OR and AND nodes to form PBLEND/PSIGN.
11699 EVT VT = N->getValueType(0);
11700 if (VT != MVT::v2i64)
11701 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011702
Nate Begemanb65c1752010-12-17 22:55:37 +000011703 SDValue N0 = N->getOperand(0);
11704 SDValue N1 = N->getOperand(1);
11705 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011706
Nate Begemanb65c1752010-12-17 22:55:37 +000011707 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011708 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011709 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11710 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11711
11712 // Check RHS for vnot
11713 if (N1.getOpcode() == ISD::XOR &&
11714 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11715 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011716
Nate Begemanb65c1752010-12-17 22:55:37 +000011717 return SDValue();
11718}
11719
Evan Cheng760d1942010-01-04 21:22:48 +000011720static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011721 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011722 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011723 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011724 return SDValue();
11725
Evan Cheng760d1942010-01-04 21:22:48 +000011726 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011727 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011728 return SDValue();
11729
Evan Cheng760d1942010-01-04 21:22:48 +000011730 SDValue N0 = N->getOperand(0);
11731 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011732
Nate Begemanb65c1752010-12-17 22:55:37 +000011733 // look for psign/blend
11734 if (Subtarget->hasSSSE3()) {
11735 if (VT == MVT::v2i64) {
11736 // Canonicalize pandn to RHS
11737 if (N0.getOpcode() == X86ISD::PANDN)
11738 std::swap(N0, N1);
11739 // or (and (m, x), (pandn m, y))
11740 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11741 SDValue Mask = N1.getOperand(0);
11742 SDValue X = N1.getOperand(1);
11743 SDValue Y;
11744 if (N0.getOperand(0) == Mask)
11745 Y = N0.getOperand(1);
11746 if (N0.getOperand(1) == Mask)
11747 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011748
Nate Begemanb65c1752010-12-17 22:55:37 +000011749 // Check to see if the mask appeared in both the AND and PANDN and
11750 if (!Y.getNode())
11751 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011752
Nate Begemanb65c1752010-12-17 22:55:37 +000011753 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11754 if (Mask.getOpcode() != ISD::BITCAST ||
11755 X.getOpcode() != ISD::BITCAST ||
11756 Y.getOpcode() != ISD::BITCAST)
11757 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011758
Nate Begemanb65c1752010-12-17 22:55:37 +000011759 // Look through mask bitcast.
11760 Mask = Mask.getOperand(0);
11761 EVT MaskVT = Mask.getValueType();
11762
11763 // Validate that the Mask operand is a vector sra node. The sra node
11764 // will be an intrinsic.
11765 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11766 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011767
Nate Begemanb65c1752010-12-17 22:55:37 +000011768 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11769 // there is no psrai.b
11770 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11771 case Intrinsic::x86_sse2_psrai_w:
11772 case Intrinsic::x86_sse2_psrai_d:
11773 break;
11774 default: return SDValue();
11775 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011776
Nate Begemanb65c1752010-12-17 22:55:37 +000011777 // Check that the SRA is all signbits.
11778 SDValue SraC = Mask.getOperand(2);
11779 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11780 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11781 if ((SraAmt + 1) != EltBits)
11782 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011783
Nate Begemanb65c1752010-12-17 22:55:37 +000011784 DebugLoc DL = N->getDebugLoc();
11785
11786 // Now we know we at least have a plendvb with the mask val. See if
11787 // we can form a psignb/w/d.
11788 // psign = x.type == y.type == mask.type && y = sub(0, x);
11789 X = X.getOperand(0);
11790 Y = Y.getOperand(0);
11791 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11792 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11793 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11794 unsigned Opc = 0;
11795 switch (EltBits) {
11796 case 8: Opc = X86ISD::PSIGNB; break;
11797 case 16: Opc = X86ISD::PSIGNW; break;
11798 case 32: Opc = X86ISD::PSIGND; break;
11799 default: break;
11800 }
11801 if (Opc) {
11802 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11803 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11804 }
11805 }
11806 // PBLENDVB only available on SSE 4.1
11807 if (!Subtarget->hasSSE41())
11808 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011809
Nate Begemanb65c1752010-12-17 22:55:37 +000011810 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11811 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11812 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011813 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011814 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11815 }
11816 }
11817 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011818
Nate Begemanb65c1752010-12-17 22:55:37 +000011819 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011820 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11821 std::swap(N0, N1);
11822 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11823 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011824 if (!N0.hasOneUse() || !N1.hasOneUse())
11825 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011826
11827 SDValue ShAmt0 = N0.getOperand(1);
11828 if (ShAmt0.getValueType() != MVT::i8)
11829 return SDValue();
11830 SDValue ShAmt1 = N1.getOperand(1);
11831 if (ShAmt1.getValueType() != MVT::i8)
11832 return SDValue();
11833 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11834 ShAmt0 = ShAmt0.getOperand(0);
11835 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11836 ShAmt1 = ShAmt1.getOperand(0);
11837
11838 DebugLoc DL = N->getDebugLoc();
11839 unsigned Opc = X86ISD::SHLD;
11840 SDValue Op0 = N0.getOperand(0);
11841 SDValue Op1 = N1.getOperand(0);
11842 if (ShAmt0.getOpcode() == ISD::SUB) {
11843 Opc = X86ISD::SHRD;
11844 std::swap(Op0, Op1);
11845 std::swap(ShAmt0, ShAmt1);
11846 }
11847
Evan Cheng8b1190a2010-04-28 01:18:01 +000011848 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011849 if (ShAmt1.getOpcode() == ISD::SUB) {
11850 SDValue Sum = ShAmt1.getOperand(0);
11851 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011852 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11853 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11854 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11855 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011856 return DAG.getNode(Opc, DL, VT,
11857 Op0, Op1,
11858 DAG.getNode(ISD::TRUNCATE, DL,
11859 MVT::i8, ShAmt0));
11860 }
11861 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11862 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11863 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011864 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011865 return DAG.getNode(Opc, DL, VT,
11866 N0.getOperand(0), N1.getOperand(0),
11867 DAG.getNode(ISD::TRUNCATE, DL,
11868 MVT::i8, ShAmt0));
11869 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011870
Evan Cheng760d1942010-01-04 21:22:48 +000011871 return SDValue();
11872}
11873
Chris Lattner149a4e52008-02-22 02:09:43 +000011874/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011875static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011876 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011877 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11878 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011879 // A preferable solution to the general problem is to figure out the right
11880 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011881
11882 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011883 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011884 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011885 if (VT.getSizeInBits() != 64)
11886 return SDValue();
11887
Devang Patel578efa92009-06-05 21:57:13 +000011888 const Function *F = DAG.getMachineFunction().getFunction();
11889 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011890 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011891 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011892 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011893 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011894 isa<LoadSDNode>(St->getValue()) &&
11895 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11896 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011897 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011898 LoadSDNode *Ld = 0;
11899 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011900 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011901 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011902 // Must be a store of a load. We currently handle two cases: the load
11903 // is a direct child, and it's under an intervening TokenFactor. It is
11904 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011905 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011906 Ld = cast<LoadSDNode>(St->getChain());
11907 else if (St->getValue().hasOneUse() &&
11908 ChainVal->getOpcode() == ISD::TokenFactor) {
11909 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011910 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011911 TokenFactorIndex = i;
11912 Ld = cast<LoadSDNode>(St->getValue());
11913 } else
11914 Ops.push_back(ChainVal->getOperand(i));
11915 }
11916 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011917
Evan Cheng536e6672009-03-12 05:59:15 +000011918 if (!Ld || !ISD::isNormalLoad(Ld))
11919 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011920
Evan Cheng536e6672009-03-12 05:59:15 +000011921 // If this is not the MMX case, i.e. we are just turning i64 load/store
11922 // into f64 load/store, avoid the transformation if there are multiple
11923 // uses of the loaded value.
11924 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11925 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011926
Evan Cheng536e6672009-03-12 05:59:15 +000011927 DebugLoc LdDL = Ld->getDebugLoc();
11928 DebugLoc StDL = N->getDebugLoc();
11929 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11930 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11931 // pair instead.
11932 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011933 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011934 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11935 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011936 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011937 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011938 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011939 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011940 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011941 Ops.size());
11942 }
Evan Cheng536e6672009-03-12 05:59:15 +000011943 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011944 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011945 St->isVolatile(), St->isNonTemporal(),
11946 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011947 }
Evan Cheng536e6672009-03-12 05:59:15 +000011948
11949 // Otherwise, lower to two pairs of 32-bit loads / stores.
11950 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011951 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11952 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011953
Owen Anderson825b72b2009-08-11 20:47:22 +000011954 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011955 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011956 Ld->isVolatile(), Ld->isNonTemporal(),
11957 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011958 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011959 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011960 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011961 MinAlign(Ld->getAlignment(), 4));
11962
11963 SDValue NewChain = LoLd.getValue(1);
11964 if (TokenFactorIndex != -1) {
11965 Ops.push_back(LoLd);
11966 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011967 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011968 Ops.size());
11969 }
11970
11971 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011972 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11973 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011974
11975 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011976 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011977 St->isVolatile(), St->isNonTemporal(),
11978 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011979 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011980 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011981 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011982 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011983 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011984 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011985 }
Dan Gohman475871a2008-07-27 21:46:04 +000011986 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011987}
11988
Chris Lattner6cf73262008-01-25 06:14:17 +000011989/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11990/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011991static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011992 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11993 // F[X]OR(0.0, x) -> x
11994 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011995 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11996 if (C->getValueAPF().isPosZero())
11997 return N->getOperand(1);
11998 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11999 if (C->getValueAPF().isPosZero())
12000 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012001 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012002}
12003
12004/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012005static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012006 // FAND(0.0, x) -> 0.0
12007 // FAND(x, 0.0) -> 0.0
12008 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12009 if (C->getValueAPF().isPosZero())
12010 return N->getOperand(0);
12011 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12012 if (C->getValueAPF().isPosZero())
12013 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012014 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012015}
12016
Dan Gohmane5af2d32009-01-29 01:59:02 +000012017static SDValue PerformBTCombine(SDNode *N,
12018 SelectionDAG &DAG,
12019 TargetLowering::DAGCombinerInfo &DCI) {
12020 // BT ignores high bits in the bit index operand.
12021 SDValue Op1 = N->getOperand(1);
12022 if (Op1.hasOneUse()) {
12023 unsigned BitWidth = Op1.getValueSizeInBits();
12024 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12025 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012026 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12027 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012028 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012029 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12030 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12031 DCI.CommitTargetLoweringOpt(TLO);
12032 }
12033 return SDValue();
12034}
Chris Lattner83e6c992006-10-04 06:57:07 +000012035
Eli Friedman7a5e5552009-06-07 06:52:44 +000012036static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12037 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012038 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012039 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012040 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012041 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012042 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012043 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012044 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012045 }
12046 return SDValue();
12047}
12048
Evan Cheng2e489c42009-12-16 00:53:11 +000012049static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12050 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12051 // (and (i32 x86isd::setcc_carry), 1)
12052 // This eliminates the zext. This transformation is necessary because
12053 // ISD::SETCC is always legalized to i8.
12054 DebugLoc dl = N->getDebugLoc();
12055 SDValue N0 = N->getOperand(0);
12056 EVT VT = N->getValueType(0);
12057 if (N0.getOpcode() == ISD::AND &&
12058 N0.hasOneUse() &&
12059 N0.getOperand(0).hasOneUse()) {
12060 SDValue N00 = N0.getOperand(0);
12061 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12062 return SDValue();
12063 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12064 if (!C || C->getZExtValue() != 1)
12065 return SDValue();
12066 return DAG.getNode(ISD::AND, dl, VT,
12067 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12068 N00.getOperand(0), N00.getOperand(1)),
12069 DAG.getConstant(1, VT));
12070 }
12071
12072 return SDValue();
12073}
12074
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012075// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12076static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12077 unsigned X86CC = N->getConstantOperandVal(0);
12078 SDValue EFLAG = N->getOperand(1);
12079 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012080
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012081 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12082 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12083 // cases.
12084 if (X86CC == X86::COND_B)
12085 return DAG.getNode(ISD::AND, DL, MVT::i8,
12086 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12087 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12088 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012089
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012090 return SDValue();
12091}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012092
Chris Lattner23a01992010-12-20 01:37:09 +000012093// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12094static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12095 X86TargetLowering::DAGCombinerInfo &DCI) {
12096 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12097 // the result is either zero or one (depending on the input carry bit).
12098 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12099 if (X86::isZeroNode(N->getOperand(0)) &&
12100 X86::isZeroNode(N->getOperand(1)) &&
12101 // We don't have a good way to replace an EFLAGS use, so only do this when
12102 // dead right now.
12103 SDValue(N, 1).use_empty()) {
12104 DebugLoc DL = N->getDebugLoc();
12105 EVT VT = N->getValueType(0);
12106 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12107 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12108 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12109 DAG.getConstant(X86::COND_B,MVT::i8),
12110 N->getOperand(2)),
12111 DAG.getConstant(1, VT));
12112 return DCI.CombineTo(N, Res1, CarryOut);
12113 }
12114
12115 return SDValue();
12116}
12117
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012118// fold (add Y, (sete X, 0)) -> adc 0, Y
12119// (add Y, (setne X, 0)) -> sbb -1, Y
12120// (sub (sete X, 0), Y) -> sbb 0, Y
12121// (sub (setne X, 0), Y) -> adc -1, Y
12122static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12123 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012124
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012125 // Look through ZExts.
12126 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12127 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12128 return SDValue();
12129
12130 SDValue SetCC = Ext.getOperand(0);
12131 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12132 return SDValue();
12133
12134 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12135 if (CC != X86::COND_E && CC != X86::COND_NE)
12136 return SDValue();
12137
12138 SDValue Cmp = SetCC.getOperand(1);
12139 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012140 !X86::isZeroNode(Cmp.getOperand(1)) ||
12141 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012142 return SDValue();
12143
12144 SDValue CmpOp0 = Cmp.getOperand(0);
12145 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12146 DAG.getConstant(1, CmpOp0.getValueType()));
12147
12148 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12149 if (CC == X86::COND_NE)
12150 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12151 DL, OtherVal.getValueType(), OtherVal,
12152 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12153 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12154 DL, OtherVal.getValueType(), OtherVal,
12155 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12156}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012157
Dan Gohman475871a2008-07-27 21:46:04 +000012158SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012159 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012160 SelectionDAG &DAG = DCI.DAG;
12161 switch (N->getOpcode()) {
12162 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012163 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012164 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012165 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012166 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012167 case ISD::ADD:
12168 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012169 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012170 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012171 case ISD::SHL:
12172 case ISD::SRA:
12173 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012174 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012175 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012176 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012177 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012178 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12179 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012180 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012181 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012182 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012183 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012184 case X86ISD::SHUFPS: // Handle all target specific shuffles
12185 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012186 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012187 case X86ISD::PUNPCKHBW:
12188 case X86ISD::PUNPCKHWD:
12189 case X86ISD::PUNPCKHDQ:
12190 case X86ISD::PUNPCKHQDQ:
12191 case X86ISD::UNPCKHPS:
12192 case X86ISD::UNPCKHPD:
12193 case X86ISD::PUNPCKLBW:
12194 case X86ISD::PUNPCKLWD:
12195 case X86ISD::PUNPCKLDQ:
12196 case X86ISD::PUNPCKLQDQ:
12197 case X86ISD::UNPCKLPS:
12198 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012199 case X86ISD::VUNPCKLPS:
12200 case X86ISD::VUNPCKLPD:
12201 case X86ISD::VUNPCKLPSY:
12202 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012203 case X86ISD::MOVHLPS:
12204 case X86ISD::MOVLHPS:
12205 case X86ISD::PSHUFD:
12206 case X86ISD::PSHUFHW:
12207 case X86ISD::PSHUFLW:
12208 case X86ISD::MOVSS:
12209 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012210 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012211 }
12212
Dan Gohman475871a2008-07-27 21:46:04 +000012213 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012214}
12215
Evan Chenge5b51ac2010-04-17 06:13:15 +000012216/// isTypeDesirableForOp - Return true if the target has native support for
12217/// the specified value type and it is 'desirable' to use the type for the
12218/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12219/// instruction encodings are longer and some i16 instructions are slow.
12220bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12221 if (!isTypeLegal(VT))
12222 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012223 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012224 return true;
12225
12226 switch (Opc) {
12227 default:
12228 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012229 case ISD::LOAD:
12230 case ISD::SIGN_EXTEND:
12231 case ISD::ZERO_EXTEND:
12232 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012233 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012234 case ISD::SRL:
12235 case ISD::SUB:
12236 case ISD::ADD:
12237 case ISD::MUL:
12238 case ISD::AND:
12239 case ISD::OR:
12240 case ISD::XOR:
12241 return false;
12242 }
12243}
12244
12245/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012246/// beneficial for dag combiner to promote the specified node. If true, it
12247/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012248bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012249 EVT VT = Op.getValueType();
12250 if (VT != MVT::i16)
12251 return false;
12252
Evan Cheng4c26e932010-04-19 19:29:22 +000012253 bool Promote = false;
12254 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012255 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012256 default: break;
12257 case ISD::LOAD: {
12258 LoadSDNode *LD = cast<LoadSDNode>(Op);
12259 // If the non-extending load has a single use and it's not live out, then it
12260 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012261 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12262 Op.hasOneUse()*/) {
12263 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12264 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12265 // The only case where we'd want to promote LOAD (rather then it being
12266 // promoted as an operand is when it's only use is liveout.
12267 if (UI->getOpcode() != ISD::CopyToReg)
12268 return false;
12269 }
12270 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012271 Promote = true;
12272 break;
12273 }
12274 case ISD::SIGN_EXTEND:
12275 case ISD::ZERO_EXTEND:
12276 case ISD::ANY_EXTEND:
12277 Promote = true;
12278 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012279 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012280 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012281 SDValue N0 = Op.getOperand(0);
12282 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012283 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012284 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012285 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012286 break;
12287 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012288 case ISD::ADD:
12289 case ISD::MUL:
12290 case ISD::AND:
12291 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012292 case ISD::XOR:
12293 Commute = true;
12294 // fallthrough
12295 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012296 SDValue N0 = Op.getOperand(0);
12297 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012298 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012299 return false;
12300 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012301 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012302 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012303 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012304 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012305 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012306 }
12307 }
12308
12309 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012310 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012311}
12312
Evan Cheng60c07e12006-07-05 22:17:51 +000012313//===----------------------------------------------------------------------===//
12314// X86 Inline Assembly Support
12315//===----------------------------------------------------------------------===//
12316
Chris Lattnerb8105652009-07-20 17:51:36 +000012317bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12318 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012319
12320 std::string AsmStr = IA->getAsmString();
12321
12322 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012323 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012324 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012325
12326 switch (AsmPieces.size()) {
12327 default: return false;
12328 case 1:
12329 AsmStr = AsmPieces[0];
12330 AsmPieces.clear();
12331 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12332
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012333 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012334 // we will turn this bswap into something that will be lowered to logical ops
12335 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12336 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012337 // bswap $0
12338 if (AsmPieces.size() == 2 &&
12339 (AsmPieces[0] == "bswap" ||
12340 AsmPieces[0] == "bswapq" ||
12341 AsmPieces[0] == "bswapl") &&
12342 (AsmPieces[1] == "$0" ||
12343 AsmPieces[1] == "${0:q}")) {
12344 // No need to check constraints, nothing other than the equivalent of
12345 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012346 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12347 if (!Ty || Ty->getBitWidth() % 16 != 0)
12348 return false;
12349 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012350 }
12351 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012352 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012353 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012354 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012355 AsmPieces[1] == "$$8," &&
12356 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012357 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12358 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012359 const std::string &ConstraintsStr = IA->getConstraintString();
12360 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012361 std::sort(AsmPieces.begin(), AsmPieces.end());
12362 if (AsmPieces.size() == 4 &&
12363 AsmPieces[0] == "~{cc}" &&
12364 AsmPieces[1] == "~{dirflag}" &&
12365 AsmPieces[2] == "~{flags}" &&
12366 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012367 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12368 if (!Ty || Ty->getBitWidth() % 16 != 0)
12369 return false;
12370 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012371 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012372 }
12373 break;
12374 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012375 if (CI->getType()->isIntegerTy(32) &&
12376 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12377 SmallVector<StringRef, 4> Words;
12378 SplitString(AsmPieces[0], Words, " \t,");
12379 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12380 Words[2] == "${0:w}") {
12381 Words.clear();
12382 SplitString(AsmPieces[1], Words, " \t,");
12383 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12384 Words[2] == "$0") {
12385 Words.clear();
12386 SplitString(AsmPieces[2], Words, " \t,");
12387 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12388 Words[2] == "${0:w}") {
12389 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012390 const std::string &ConstraintsStr = IA->getConstraintString();
12391 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012392 std::sort(AsmPieces.begin(), AsmPieces.end());
12393 if (AsmPieces.size() == 4 &&
12394 AsmPieces[0] == "~{cc}" &&
12395 AsmPieces[1] == "~{dirflag}" &&
12396 AsmPieces[2] == "~{flags}" &&
12397 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012398 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12399 if (!Ty || Ty->getBitWidth() % 16 != 0)
12400 return false;
12401 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012402 }
12403 }
12404 }
12405 }
12406 }
Evan Cheng55d42002011-01-08 01:24:27 +000012407
12408 if (CI->getType()->isIntegerTy(64)) {
12409 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12410 if (Constraints.size() >= 2 &&
12411 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12412 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12413 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12414 SmallVector<StringRef, 4> Words;
12415 SplitString(AsmPieces[0], Words, " \t");
12416 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012417 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012418 SplitString(AsmPieces[1], Words, " \t");
12419 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12420 Words.clear();
12421 SplitString(AsmPieces[2], Words, " \t,");
12422 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12423 Words[2] == "%edx") {
12424 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12425 if (!Ty || Ty->getBitWidth() % 16 != 0)
12426 return false;
12427 return IntrinsicLowering::LowerToByteSwap(CI);
12428 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012429 }
12430 }
12431 }
12432 }
12433 break;
12434 }
12435 return false;
12436}
12437
12438
12439
Chris Lattnerf4dff842006-07-11 02:54:03 +000012440/// getConstraintType - Given a constraint letter, return the type of
12441/// constraint it is for this target.
12442X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012443X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12444 if (Constraint.size() == 1) {
12445 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012446 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012447 case 'q':
12448 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012449 case 'f':
12450 case 't':
12451 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012452 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012453 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012454 case 'Y':
12455 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012456 case 'a':
12457 case 'b':
12458 case 'c':
12459 case 'd':
12460 case 'S':
12461 case 'D':
12462 case 'A':
12463 return C_Register;
12464 case 'I':
12465 case 'J':
12466 case 'K':
12467 case 'L':
12468 case 'M':
12469 case 'N':
12470 case 'G':
12471 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012472 case 'e':
12473 case 'Z':
12474 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012475 default:
12476 break;
12477 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012478 }
Chris Lattner4234f572007-03-25 02:14:49 +000012479 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012480}
12481
John Thompson44ab89e2010-10-29 17:29:13 +000012482/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012483/// This object must already have been set up with the operand type
12484/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012485TargetLowering::ConstraintWeight
12486 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012487 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012488 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012489 Value *CallOperandVal = info.CallOperandVal;
12490 // If we don't have a value, we can't do a match,
12491 // but allow it at the lowest weight.
12492 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012493 return CW_Default;
12494 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012495 // Look at the constraint type.
12496 switch (*constraint) {
12497 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012498 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12499 case 'R':
12500 case 'q':
12501 case 'Q':
12502 case 'a':
12503 case 'b':
12504 case 'c':
12505 case 'd':
12506 case 'S':
12507 case 'D':
12508 case 'A':
12509 if (CallOperandVal->getType()->isIntegerTy())
12510 weight = CW_SpecificReg;
12511 break;
12512 case 'f':
12513 case 't':
12514 case 'u':
12515 if (type->isFloatingPointTy())
12516 weight = CW_SpecificReg;
12517 break;
12518 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012519 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012520 weight = CW_SpecificReg;
12521 break;
12522 case 'x':
12523 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012524 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012525 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012526 break;
12527 case 'I':
12528 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12529 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012530 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012531 }
12532 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012533 case 'J':
12534 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12535 if (C->getZExtValue() <= 63)
12536 weight = CW_Constant;
12537 }
12538 break;
12539 case 'K':
12540 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12541 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12542 weight = CW_Constant;
12543 }
12544 break;
12545 case 'L':
12546 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12547 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12548 weight = CW_Constant;
12549 }
12550 break;
12551 case 'M':
12552 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12553 if (C->getZExtValue() <= 3)
12554 weight = CW_Constant;
12555 }
12556 break;
12557 case 'N':
12558 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12559 if (C->getZExtValue() <= 0xff)
12560 weight = CW_Constant;
12561 }
12562 break;
12563 case 'G':
12564 case 'C':
12565 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12566 weight = CW_Constant;
12567 }
12568 break;
12569 case 'e':
12570 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12571 if ((C->getSExtValue() >= -0x80000000LL) &&
12572 (C->getSExtValue() <= 0x7fffffffLL))
12573 weight = CW_Constant;
12574 }
12575 break;
12576 case 'Z':
12577 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12578 if (C->getZExtValue() <= 0xffffffff)
12579 weight = CW_Constant;
12580 }
12581 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012582 }
12583 return weight;
12584}
12585
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012586/// LowerXConstraint - try to replace an X constraint, which matches anything,
12587/// with another that has more specific requirements based on the type of the
12588/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012589const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012590LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012591 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12592 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012593 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012594 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012595 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012596 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012597 return "x";
12598 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012599
Chris Lattner5e764232008-04-26 23:02:14 +000012600 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012601}
12602
Chris Lattner48884cd2007-08-25 00:47:38 +000012603/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12604/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012605void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012606 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012607 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012608 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012609 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012610
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012611 switch (Constraint) {
12612 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012613 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012614 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012615 if (C->getZExtValue() <= 31) {
12616 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012617 break;
12618 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012619 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012620 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012621 case 'J':
12622 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012623 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012624 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12625 break;
12626 }
12627 }
12628 return;
12629 case 'K':
12630 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012631 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012632 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12633 break;
12634 }
12635 }
12636 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012637 case 'N':
12638 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012639 if (C->getZExtValue() <= 255) {
12640 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012641 break;
12642 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012643 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012644 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012645 case 'e': {
12646 // 32-bit signed value
12647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012648 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12649 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012650 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012651 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012652 break;
12653 }
12654 // FIXME gcc accepts some relocatable values here too, but only in certain
12655 // memory models; it's complicated.
12656 }
12657 return;
12658 }
12659 case 'Z': {
12660 // 32-bit unsigned value
12661 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012662 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12663 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012664 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12665 break;
12666 }
12667 }
12668 // FIXME gcc accepts some relocatable values here too, but only in certain
12669 // memory models; it's complicated.
12670 return;
12671 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012672 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012673 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012674 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012675 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012676 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012677 break;
12678 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012679
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012680 // In any sort of PIC mode addresses need to be computed at runtime by
12681 // adding in a register or some sort of table lookup. These can't
12682 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012683 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012684 return;
12685
Chris Lattnerdc43a882007-05-03 16:52:29 +000012686 // If we are in non-pic codegen mode, we allow the address of a global (with
12687 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012688 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012689 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012690
Chris Lattner49921962009-05-08 18:23:14 +000012691 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12692 while (1) {
12693 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12694 Offset += GA->getOffset();
12695 break;
12696 } else if (Op.getOpcode() == ISD::ADD) {
12697 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12698 Offset += C->getZExtValue();
12699 Op = Op.getOperand(0);
12700 continue;
12701 }
12702 } else if (Op.getOpcode() == ISD::SUB) {
12703 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12704 Offset += -C->getZExtValue();
12705 Op = Op.getOperand(0);
12706 continue;
12707 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012708 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012709
Chris Lattner49921962009-05-08 18:23:14 +000012710 // Otherwise, this isn't something we can handle, reject it.
12711 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012712 }
Eric Christopherfd179292009-08-27 18:07:15 +000012713
Dan Gohman46510a72010-04-15 01:51:59 +000012714 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012715 // If we require an extra load to get this address, as in PIC mode, we
12716 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012717 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12718 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012719 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012720
Devang Patel0d881da2010-07-06 22:08:15 +000012721 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12722 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012723 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012724 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012725 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012726
Gabor Greifba36cb52008-08-28 21:40:38 +000012727 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012728 Ops.push_back(Result);
12729 return;
12730 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012731 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012732}
12733
Chris Lattner259e97c2006-01-31 19:43:35 +000012734std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012735getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012736 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012737 if (Constraint.size() == 1) {
12738 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012739 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012740 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012741 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12742 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012743 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012744 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12745 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12746 X86::R10D,X86::R11D,X86::R12D,
12747 X86::R13D,X86::R14D,X86::R15D,
12748 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012749 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012750 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12751 X86::SI, X86::DI, X86::R8W,X86::R9W,
12752 X86::R10W,X86::R11W,X86::R12W,
12753 X86::R13W,X86::R14W,X86::R15W,
12754 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012755 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012756 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12757 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12758 X86::R10B,X86::R11B,X86::R12B,
12759 X86::R13B,X86::R14B,X86::R15B,
12760 X86::BPL, X86::SPL, 0);
12761
Owen Anderson825b72b2009-08-11 20:47:22 +000012762 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012763 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12764 X86::RSI, X86::RDI, X86::R8, X86::R9,
12765 X86::R10, X86::R11, X86::R12,
12766 X86::R13, X86::R14, X86::R15,
12767 X86::RBP, X86::RSP, 0);
12768
12769 break;
12770 }
Eric Christopherfd179292009-08-27 18:07:15 +000012771 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012772 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012773 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012774 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012775 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012776 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012777 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012778 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012779 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012780 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12781 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012782 }
12783 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012784
Chris Lattner1efa40f2006-02-22 00:56:39 +000012785 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012786}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012787
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012788std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012789X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012790 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012791 // First, see if this is a constraint that directly corresponds to an LLVM
12792 // register class.
12793 if (Constraint.size() == 1) {
12794 // GCC Constraint Letters
12795 switch (Constraint[0]) {
12796 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012797 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012798 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012799 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012800 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012801 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012802 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012803 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012804 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012805 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012806 case 'R': // LEGACY_REGS
12807 if (VT == MVT::i8)
12808 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12809 if (VT == MVT::i16)
12810 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12811 if (VT == MVT::i32 || !Subtarget->is64Bit())
12812 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12813 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012814 case 'f': // FP Stack registers.
12815 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12816 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012817 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012818 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012819 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012820 return std::make_pair(0U, X86::RFP64RegisterClass);
12821 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012822 case 'y': // MMX_REGS if MMX allowed.
12823 if (!Subtarget->hasMMX()) break;
12824 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012825 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012826 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012827 // FALL THROUGH.
12828 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012829 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012830
Owen Anderson825b72b2009-08-11 20:47:22 +000012831 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012832 default: break;
12833 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012834 case MVT::f32:
12835 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012836 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012837 case MVT::f64:
12838 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012839 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012840 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012841 case MVT::v16i8:
12842 case MVT::v8i16:
12843 case MVT::v4i32:
12844 case MVT::v2i64:
12845 case MVT::v4f32:
12846 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012847 return std::make_pair(0U, X86::VR128RegisterClass);
12848 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012849 break;
12850 }
12851 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012852
Chris Lattnerf76d1802006-07-31 23:26:50 +000012853 // Use the default implementation in TargetLowering to convert the register
12854 // constraint into a member of a register class.
12855 std::pair<unsigned, const TargetRegisterClass*> Res;
12856 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012857
12858 // Not found as a standard register?
12859 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012860 // Map st(0) -> st(7) -> ST0
12861 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12862 tolower(Constraint[1]) == 's' &&
12863 tolower(Constraint[2]) == 't' &&
12864 Constraint[3] == '(' &&
12865 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12866 Constraint[5] == ')' &&
12867 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012868
Chris Lattner56d77c72009-09-13 22:41:48 +000012869 Res.first = X86::ST0+Constraint[4]-'0';
12870 Res.second = X86::RFP80RegisterClass;
12871 return Res;
12872 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012873
Chris Lattner56d77c72009-09-13 22:41:48 +000012874 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012875 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012876 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012877 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012878 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012879 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012880
12881 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012882 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012883 Res.first = X86::EFLAGS;
12884 Res.second = X86::CCRRegisterClass;
12885 return Res;
12886 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012887
Dale Johannesen330169f2008-11-13 21:52:36 +000012888 // 'A' means EAX + EDX.
12889 if (Constraint == "A") {
12890 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012891 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012892 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012893 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012894 return Res;
12895 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012896
Chris Lattnerf76d1802006-07-31 23:26:50 +000012897 // Otherwise, check to see if this is a register class of the wrong value
12898 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12899 // turn into {ax},{dx}.
12900 if (Res.second->hasType(VT))
12901 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012902
Chris Lattnerf76d1802006-07-31 23:26:50 +000012903 // All of the single-register GCC register classes map their values onto
12904 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12905 // really want an 8-bit or 32-bit register, map to the appropriate register
12906 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012907 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012908 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012909 unsigned DestReg = 0;
12910 switch (Res.first) {
12911 default: break;
12912 case X86::AX: DestReg = X86::AL; break;
12913 case X86::DX: DestReg = X86::DL; break;
12914 case X86::CX: DestReg = X86::CL; break;
12915 case X86::BX: DestReg = X86::BL; break;
12916 }
12917 if (DestReg) {
12918 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012919 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012920 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012921 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012922 unsigned DestReg = 0;
12923 switch (Res.first) {
12924 default: break;
12925 case X86::AX: DestReg = X86::EAX; break;
12926 case X86::DX: DestReg = X86::EDX; break;
12927 case X86::CX: DestReg = X86::ECX; break;
12928 case X86::BX: DestReg = X86::EBX; break;
12929 case X86::SI: DestReg = X86::ESI; break;
12930 case X86::DI: DestReg = X86::EDI; break;
12931 case X86::BP: DestReg = X86::EBP; break;
12932 case X86::SP: DestReg = X86::ESP; break;
12933 }
12934 if (DestReg) {
12935 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012936 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012937 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012938 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012939 unsigned DestReg = 0;
12940 switch (Res.first) {
12941 default: break;
12942 case X86::AX: DestReg = X86::RAX; break;
12943 case X86::DX: DestReg = X86::RDX; break;
12944 case X86::CX: DestReg = X86::RCX; break;
12945 case X86::BX: DestReg = X86::RBX; break;
12946 case X86::SI: DestReg = X86::RSI; break;
12947 case X86::DI: DestReg = X86::RDI; break;
12948 case X86::BP: DestReg = X86::RBP; break;
12949 case X86::SP: DestReg = X86::RSP; break;
12950 }
12951 if (DestReg) {
12952 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012953 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012954 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012955 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012956 } else if (Res.second == X86::FR32RegisterClass ||
12957 Res.second == X86::FR64RegisterClass ||
12958 Res.second == X86::VR128RegisterClass) {
12959 // Handle references to XMM physical registers that got mapped into the
12960 // wrong class. This can happen with constraints like {xmm0} where the
12961 // target independent register mapper will just pick the first match it can
12962 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012963 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012964 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012965 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012966 Res.second = X86::FR64RegisterClass;
12967 else if (X86::VR128RegisterClass->hasType(VT))
12968 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012969 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012970
Chris Lattnerf76d1802006-07-31 23:26:50 +000012971 return Res;
12972}