blob: eed4bca40b91e1011cb8f24e6bbcfb03ca6ada17 [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
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000577 // Lower this to FGETSIGNx86 plus an AND.
578 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
579 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
580
Evan Chengd25e9e82006-02-02 00:28:23 +0000581 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::FSIN , MVT::f64, Expand);
583 setOperationAction(ISD::FCOS , MVT::f64, Expand);
584 setOperationAction(ISD::FSIN , MVT::f32, Expand);
585 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000586
Chris Lattnera54aa942006-01-29 06:26:08 +0000587 // Expand FP immediates into loads from the stack, except for the special
588 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589 addLegalFPImmediate(APFloat(+0.0)); // xorpd
590 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000591 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592 // Use SSE for f32, x87 for f64.
593 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
595 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000596
597 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604
605 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
607 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000608
609 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FSIN , MVT::f32, Expand);
611 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000612
Nate Begemane1795842008-02-14 08:57:00 +0000613 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 addLegalFPImmediate(APFloat(+0.0f)); // xorps
615 addLegalFPImmediate(APFloat(+0.0)); // FLD0
616 addLegalFPImmediate(APFloat(+1.0)); // FLD1
617 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
618 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
619
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000620 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
622 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000623 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000624 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000625 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
628 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
631 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
632 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
633 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000634
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
637 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000639 addLegalFPImmediate(APFloat(+0.0)); // FLD0
640 addLegalFPImmediate(APFloat(+1.0)); // FLD1
641 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
642 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000643 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
644 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
645 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
646 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000647 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000648
Dale Johannesen59a58732007-08-05 18:49:15 +0000649 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000650 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
652 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
653 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000654 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000655 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656 addLegalFPImmediate(TmpFlt); // FLD0
657 TmpFlt.changeSign();
658 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000659
660 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000661 APFloat TmpFlt2(+1.0);
662 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
663 &ignored);
664 addLegalFPImmediate(TmpFlt2); // FLD1
665 TmpFlt2.changeSign();
666 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
667 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Evan Chengc7ce29b2009-02-13 22:36:38 +0000669 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
671 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000673 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000674
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000675 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
677 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
678 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FLOG, MVT::f80, Expand);
681 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
682 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
683 setOperationAction(ISD::FEXP, MVT::f80, Expand);
684 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000685
Mon P Wangf007a8b2008-11-06 05:31:54 +0000686 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000687 // (for widening) or expand (for scalarization). Then we will selectively
688 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
690 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
691 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
697 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
698 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
699 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000707 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
708 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000740 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000741 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
745 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
746 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
747 setTruncStoreAction((MVT::SimpleValueType)VT,
748 (MVT::SimpleValueType)InnerVT, Expand);
749 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
750 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
751 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000752 }
753
Evan Chengc7ce29b2009-02-13 22:36:38 +0000754 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
755 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000756 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000757 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000758 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000759 }
760
Dale Johannesen0488fb62010-09-30 23:57:10 +0000761 // MMX-sized vectors (other than x86mmx) are expected to be expanded
762 // into smaller operations.
763 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
764 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
765 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
766 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
767 setOperationAction(ISD::AND, MVT::v8i8, Expand);
768 setOperationAction(ISD::AND, MVT::v4i16, Expand);
769 setOperationAction(ISD::AND, MVT::v2i32, Expand);
770 setOperationAction(ISD::AND, MVT::v1i64, Expand);
771 setOperationAction(ISD::OR, MVT::v8i8, Expand);
772 setOperationAction(ISD::OR, MVT::v4i16, Expand);
773 setOperationAction(ISD::OR, MVT::v2i32, Expand);
774 setOperationAction(ISD::OR, MVT::v1i64, Expand);
775 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
776 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
777 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
778 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
779 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
780 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
781 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
782 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
784 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
785 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
786 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
787 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
789 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
790 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
791 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000792
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000793 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000795
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
797 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
798 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
799 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
800 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
801 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
802 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
803 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
804 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
805 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
806 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
807 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000808 }
809
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000810 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000812
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000813 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
814 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
816 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
817 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
818 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
821 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
822 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
823 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
824 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
825 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
826 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
827 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
828 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
829 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
830 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
831 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
832 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
833 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
834 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
835 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000836
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
838 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
839 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
840 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
843 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000847
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000848 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
849 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
850 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
851 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
852 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
853
Evan Cheng2c3ae372006-04-12 21:21:57 +0000854 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
856 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000857 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 // Do not attempt to custom lower non-128-bit vectors
861 if (!VT.is128BitVector())
862 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::BUILD_VECTOR,
864 VT.getSimpleVT().SimpleTy, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE,
866 VT.getSimpleVT().SimpleTy, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
868 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
872 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
873 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
874 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
876 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000877
Nate Begemancdd1eec2008-02-12 22:51:28 +0000878 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000881 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000882
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000883 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
885 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000886 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000889 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000890 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000891
Owen Andersond6662ad2009-08-10 20:46:15 +0000892 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000894 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000896 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000898 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000900 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000902 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000905
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
908 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
909 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
910 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
913 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000914 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000915
Nate Begeman14d12ca2008-02-11 04:19:36 +0000916 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000917 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
918 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
919 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
920 setOperationAction(ISD::FRINT, MVT::f32, Legal);
921 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
922 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
923 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
924 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
925 setOperationAction(ISD::FRINT, MVT::f64, Legal);
926 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
927
Nate Begeman14d12ca2008-02-11 04:19:36 +0000928 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000930
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000931 // Can turn SHL into an integer multiply.
932 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000933 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000934
Nate Begeman14d12ca2008-02-11 04:19:36 +0000935 // i8 and i16 vectors are custom , because the source register and source
936 // source memory operand types are not the same width. f32 vectors are
937 // custom since the immediate controlling the insert encodes additional
938 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000939 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
942 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
946 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
947 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948
949 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
951 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000952 }
953 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000954
Nadav Rotem43012222011-05-11 08:12:09 +0000955 if (Subtarget->hasSSE2()) {
956 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
957 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
958 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
959
960 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
961 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
962 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
963
964 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
965 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
966 }
967
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000968 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000970
David Greene9b9838d2009-06-29 16:47:10 +0000971 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000972 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
973 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
974 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
975 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000976 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000977
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
979 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
980 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
981 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
984 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
985 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
986 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
987 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
988 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
991 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
992 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
993 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
994 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
995 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000996
David Greene54d8eba2011-01-27 22:38:56 +0000997 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
998 // insert_vector_elt extract_subvector and extract_vector_elt for
999 // 256-bit types.
1000 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1001 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1002 ++i) {
1003 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1004 // Do not attempt to custom lower non-256-bit vectors
1005 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
1006 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +00001007 continue;
David Greene9b9838d2009-06-29 16:47:10 +00001008 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1009 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001010 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001011 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +00001012 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001013 }
David Greene54d8eba2011-01-27 22:38:56 +00001014 // Custom-lower insert_subvector and extract_subvector based on
1015 // the result type.
1016 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1017 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1018 ++i) {
1019 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
1020 // Do not attempt to custom lower non-256-bit vectors
1021 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001022 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001023
1024 if (MVT(VT).getSizeInBits() == 128) {
1025 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001026 }
David Greene54d8eba2011-01-27 22:38:56 +00001027 else if (MVT(VT).getSizeInBits() == 256) {
1028 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1029 }
David Greene9b9838d2009-06-29 16:47:10 +00001030 }
1031
David Greene54d8eba2011-01-27 22:38:56 +00001032 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1033 // Don't promote loads because we need them for VPERM vector index versions.
1034
1035 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1036 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1037 VT++) {
1038 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1039 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1040 continue;
1041 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1042 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1043 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1044 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1045 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1046 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1047 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1048 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1049 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1050 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1051 }
David Greene9b9838d2009-06-29 16:47:10 +00001052 }
1053
Evan Cheng6be2c582006-04-05 23:38:46 +00001054 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001055 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001056
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001057
Eli Friedman962f5492010-06-02 19:35:46 +00001058 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1059 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001060 //
Eli Friedman962f5492010-06-02 19:35:46 +00001061 // FIXME: We really should do custom legalization for addition and
1062 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1063 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001064 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1065 // Add/Sub/Mul with overflow operations are custom lowered.
1066 MVT VT = IntVTs[i];
1067 setOperationAction(ISD::SADDO, VT, Custom);
1068 setOperationAction(ISD::UADDO, VT, Custom);
1069 setOperationAction(ISD::SSUBO, VT, Custom);
1070 setOperationAction(ISD::USUBO, VT, Custom);
1071 setOperationAction(ISD::SMULO, VT, Custom);
1072 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001073 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001074
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001075 // There are no 8-bit 3-address imul/mul instructions
1076 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1077 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001078
Evan Chengd54f2d52009-03-31 19:38:51 +00001079 if (!Subtarget->is64Bit()) {
1080 // These libcalls are not available in 32-bit.
1081 setLibcallName(RTLIB::SHL_I128, 0);
1082 setLibcallName(RTLIB::SRL_I128, 0);
1083 setLibcallName(RTLIB::SRA_I128, 0);
1084 }
1085
Evan Cheng206ee9d2006-07-07 08:33:52 +00001086 // We have target-specific dag combine patterns for the following nodes:
1087 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001088 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001089 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001090 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001091 setTargetDAGCombine(ISD::SHL);
1092 setTargetDAGCombine(ISD::SRA);
1093 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001094 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001095 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001096 setTargetDAGCombine(ISD::ADD);
1097 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001098 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001099 setTargetDAGCombine(ISD::ZERO_EXTEND);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001100 setTargetDAGCombine(ISD::SINT_TO_FP);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001101 if (Subtarget->is64Bit())
1102 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001103
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001104 computeRegisterProperties();
1105
Evan Cheng05219282011-01-06 06:52:41 +00001106 // On Darwin, -Os means optimize for size without hurting performance,
1107 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001108 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001109 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001110 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001111 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1112 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1113 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001114 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001115 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001116
1117 setPrefFunctionAlignment(4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118}
1119
Scott Michel5b8f82e2008-03-10 15:42:14 +00001120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1122 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001123}
1124
1125
Evan Cheng29286502008-01-23 23:17:41 +00001126/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1127/// the desired ByVal argument alignment.
1128static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1129 if (MaxAlign == 16)
1130 return;
1131 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1132 if (VTy->getBitWidth() == 128)
1133 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001134 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1135 unsigned EltAlign = 0;
1136 getMaxByValAlign(ATy->getElementType(), EltAlign);
1137 if (EltAlign > MaxAlign)
1138 MaxAlign = EltAlign;
1139 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1140 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1141 unsigned EltAlign = 0;
1142 getMaxByValAlign(STy->getElementType(i), EltAlign);
1143 if (EltAlign > MaxAlign)
1144 MaxAlign = EltAlign;
1145 if (MaxAlign == 16)
1146 break;
1147 }
1148 }
1149 return;
1150}
1151
1152/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1153/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001154/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1155/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001156unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001157 if (Subtarget->is64Bit()) {
1158 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001159 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001160 if (TyAlign > 8)
1161 return TyAlign;
1162 return 8;
1163 }
1164
Evan Cheng29286502008-01-23 23:17:41 +00001165 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001166 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001167 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001168 return Align;
1169}
Chris Lattner2b02a442007-02-25 08:29:00 +00001170
Evan Chengf0df0312008-05-15 08:39:06 +00001171/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001172/// and store operations as a result of memset, memcpy, and memmove
1173/// lowering. If DstAlign is zero that means it's safe to destination
1174/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1175/// means there isn't a need to check it against alignment requirement,
1176/// probably because the source does not need to be loaded. If
1177/// 'NonScalarIntSafe' is true, that means it's safe to return a
1178/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1179/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1180/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001181/// It returns EVT::Other if the type should be determined using generic
1182/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001183EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001184X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1185 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001186 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001187 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001188 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001189 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1190 // linux. This is because the stack realignment code can't handle certain
1191 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001192 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001193 if (NonScalarIntSafe &&
1194 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001195 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001196 (Subtarget->isUnalignedMemAccessFast() ||
1197 ((DstAlign == 0 || DstAlign >= 16) &&
1198 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001199 Subtarget->getStackAlignment() >= 16) {
1200 if (Subtarget->hasSSE2())
1201 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001202 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001203 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001204 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001205 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001206 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001207 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001208 // Do not use f64 to lower memcpy if source is string constant. It's
1209 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001210 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001211 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001212 }
Evan Chengf0df0312008-05-15 08:39:06 +00001213 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 return MVT::i64;
1215 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001216}
1217
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001218/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1219/// current function. The returned value is a member of the
1220/// MachineJumpTableInfo::JTEntryKind enum.
1221unsigned X86TargetLowering::getJumpTableEncoding() const {
1222 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1223 // symbol.
1224 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1225 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001226 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001227
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001228 // Otherwise, use the normal jump table encoding heuristics.
1229 return TargetLowering::getJumpTableEncoding();
1230}
1231
Chris Lattnerc64daab2010-01-26 05:02:42 +00001232const MCExpr *
1233X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1234 const MachineBasicBlock *MBB,
1235 unsigned uid,MCContext &Ctx) const{
1236 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT());
1238 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1239 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001240 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1241 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001242}
1243
Evan Chengcc415862007-11-09 01:32:10 +00001244/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1245/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001246SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001247 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001248 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001249 // This doesn't have DebugLoc associated with it, but is not really the
1250 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001251 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001252 return Table;
1253}
1254
Chris Lattner589c6f62010-01-26 06:28:43 +00001255/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1256/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1257/// MCExpr.
1258const MCExpr *X86TargetLowering::
1259getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1260 MCContext &Ctx) const {
1261 // X86-64 uses RIP relative addressing based on the jump table label.
1262 if (Subtarget->isPICStyleRIPRel())
1263 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1264
1265 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001266 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001267}
1268
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001269// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001270std::pair<const TargetRegisterClass*, uint8_t>
1271X86TargetLowering::findRepresentativeClass(EVT VT) const{
1272 const TargetRegisterClass *RRC = 0;
1273 uint8_t Cost = 1;
1274 switch (VT.getSimpleVT().SimpleTy) {
1275 default:
1276 return TargetLowering::findRepresentativeClass(VT);
1277 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1278 RRC = (Subtarget->is64Bit()
1279 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1280 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001281 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001282 RRC = X86::VR64RegisterClass;
1283 break;
1284 case MVT::f32: case MVT::f64:
1285 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1286 case MVT::v4f32: case MVT::v2f64:
1287 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1288 case MVT::v4f64:
1289 RRC = X86::VR128RegisterClass;
1290 break;
1291 }
1292 return std::make_pair(RRC, Cost);
1293}
1294
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001295bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1296 unsigned &Offset) const {
1297 if (!Subtarget->isTargetLinux())
1298 return false;
1299
1300 if (Subtarget->is64Bit()) {
1301 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1302 Offset = 0x28;
1303 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1304 AddressSpace = 256;
1305 else
1306 AddressSpace = 257;
1307 } else {
1308 // %gs:0x14 on i386
1309 Offset = 0x14;
1310 AddressSpace = 256;
1311 }
1312 return true;
1313}
1314
1315
Chris Lattner2b02a442007-02-25 08:29:00 +00001316//===----------------------------------------------------------------------===//
1317// Return Value Calling Convention Implementation
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner59ed56b2007-02-28 04:55:35 +00001320#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001321
Michael J. Spencerec38de22010-10-10 22:04:20 +00001322bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001323X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001324 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001325 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001326 SmallVector<CCValAssign, 16> RVLocs;
1327 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001328 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001329 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001330}
1331
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332SDValue
1333X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001334 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001335 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001336 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001337 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001338 MachineFunction &MF = DAG.getMachineFunction();
1339 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Chris Lattner9774c912007-02-27 05:28:59 +00001341 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1343 RVLocs, *DAG.getContext());
1344 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001345
Evan Chengdcea1632010-02-04 02:40:39 +00001346 // Add the regs to the liveout set for the function.
1347 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1348 for (unsigned i = 0; i != RVLocs.size(); ++i)
1349 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1350 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001353
Dan Gohman475871a2008-07-27 21:46:04 +00001354 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001355 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1356 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001357 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1358 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001359
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001360 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001361 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1362 CCValAssign &VA = RVLocs[i];
1363 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001364 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001365 EVT ValVT = ValToCopy.getValueType();
1366
Dale Johannesenc4510512010-09-24 19:05:48 +00001367 // If this is x86-64, and we disabled SSE, we can't return FP values,
1368 // or SSE or MMX vectors.
1369 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1370 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001371 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001372 report_fatal_error("SSE register return with SSE disabled");
1373 }
1374 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1375 // llvm-gcc has never done it right and no one has noticed, so this
1376 // should be OK for now.
1377 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001378 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001379 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001380
Chris Lattner447ff682008-03-11 03:23:40 +00001381 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1382 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001383 if (VA.getLocReg() == X86::ST0 ||
1384 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001385 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1386 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001387 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001388 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001389 RetOps.push_back(ValToCopy);
1390 // Don't emit a copytoreg.
1391 continue;
1392 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001393
Evan Cheng242b38b2009-02-23 09:03:22 +00001394 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1395 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001396 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001397 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001398 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001399 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001400 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1401 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001402 // If we don't have SSE2 available, convert to v4f32 so the generated
1403 // register is legal.
1404 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001405 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001406 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001408 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001409
Dale Johannesendd64c412009-02-04 00:33:20 +00001410 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001411 Flag = Chain.getValue(1);
1412 }
Dan Gohman61a92132008-04-21 23:59:07 +00001413
1414 // The x86-64 ABI for returning structs by value requires that we copy
1415 // the sret argument into %rax for the return. We saved the argument into
1416 // a virtual register in the entry block, so now we copy the value out
1417 // and into %rax.
1418 if (Subtarget->is64Bit() &&
1419 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1420 MachineFunction &MF = DAG.getMachineFunction();
1421 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1422 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001423 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001424 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001425 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001426
Dale Johannesendd64c412009-02-04 00:33:20 +00001427 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001428 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001429
1430 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001431 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001433
Chris Lattner447ff682008-03-11 03:23:40 +00001434 RetOps[0] = Chain; // Update chain.
1435
1436 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001437 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001438 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001439
1440 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001442}
1443
Evan Cheng3d2125c2010-11-30 23:55:39 +00001444bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1445 if (N->getNumValues() != 1)
1446 return false;
1447 if (!N->hasNUsesOfValue(1, 0))
1448 return false;
1449
1450 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001451 if (Copy->getOpcode() != ISD::CopyToReg &&
1452 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001453 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001454
1455 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001456 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001457 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001458 if (UI->getOpcode() != X86ISD::RET_FLAG)
1459 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001460 HasRet = true;
1461 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001462
Evan Cheng1bf891a2010-12-01 22:59:46 +00001463 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001464}
1465
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001466EVT
1467X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001468 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001469 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001470 // TODO: Is this also valid on 32-bit?
1471 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001472 ReturnMVT = MVT::i8;
1473 else
1474 ReturnMVT = MVT::i32;
1475
1476 EVT MinVT = getRegisterType(Context, ReturnMVT);
1477 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001478}
1479
Dan Gohman98ca4f22009-08-05 01:29:28 +00001480/// LowerCallResult - Lower the result values of a call into the
1481/// appropriate copies out of appropriate physical registers.
1482///
1483SDValue
1484X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001485 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001486 const SmallVectorImpl<ISD::InputArg> &Ins,
1487 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001488 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001489
Chris Lattnere32bbf62007-02-28 07:09:55 +00001490 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001491 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001492 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001494 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001495 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001496
Chris Lattner3085e152007-02-25 08:59:22 +00001497 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001498 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001499 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001500 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001501
Torok Edwin3f142c32009-02-01 18:15:56 +00001502 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001504 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001505 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001506 }
1507
Evan Cheng79fb3b42009-02-20 20:43:02 +00001508 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001509
1510 // If this is a call to a function that returns an fp value on the floating
1511 // point stack, we must guarantee the the value is popped from the stack, so
1512 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1513 // if the return value is not used. We use the FpGET_ST0 instructions
1514 // instead.
1515 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1516 // If we prefer to use the value in xmm registers, copy it out as f80 and
1517 // use a truncate to move it from fp stack reg to xmm reg.
1518 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1519 bool isST0 = VA.getLocReg() == X86::ST0;
1520 unsigned Opc = 0;
1521 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1522 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1523 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1524 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001525 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001526 Ops, 2), 1);
1527 Val = Chain.getValue(0);
1528
1529 // Round the f80 to the right size, which also moves it to the appropriate
1530 // xmm register.
1531 if (CopyVT != VA.getValVT())
1532 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1533 // This truncation won't change the value.
1534 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001535 } else {
1536 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1537 CopyVT, InFlag).getValue(1);
1538 Val = Chain.getValue(0);
1539 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001540 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001542 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001543
Dan Gohman98ca4f22009-08-05 01:29:28 +00001544 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001545}
1546
1547
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001548//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001549// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001550//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001551// StdCall calling convention seems to be standard for many Windows' API
1552// routines and around. It differs from C calling convention just a little:
1553// callee should clean up the stack, not caller. Symbols should be also
1554// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001555// For info on fast calling convention see Fast Calling Convention (tail call)
1556// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001557
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001559/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1561 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001562 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001563
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001565}
1566
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001567/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001568/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001569static bool
1570ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1571 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001572 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001573
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001575}
1576
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001577/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1578/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001579/// the specific parameter attribute. The copy will be passed as a byval
1580/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001581static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001582CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001583 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1584 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001585 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001586
Dale Johannesendd64c412009-02-04 00:33:20 +00001587 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001588 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001589 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001590}
1591
Chris Lattner29689432010-03-11 00:22:57 +00001592/// IsTailCallConvention - Return true if the calling convention is one that
1593/// supports tail call optimization.
1594static bool IsTailCallConvention(CallingConv::ID CC) {
1595 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1596}
1597
Evan Cheng485fafc2011-03-21 01:19:09 +00001598bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1599 if (!CI->isTailCall())
1600 return false;
1601
1602 CallSite CS(CI);
1603 CallingConv::ID CalleeCC = CS.getCallingConv();
1604 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1605 return false;
1606
1607 return true;
1608}
1609
Evan Cheng0c439eb2010-01-27 00:07:07 +00001610/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1611/// a tailcall target by changing its ABI.
1612static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001613 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001614}
1615
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616SDValue
1617X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001618 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001619 const SmallVectorImpl<ISD::InputArg> &Ins,
1620 DebugLoc dl, SelectionDAG &DAG,
1621 const CCValAssign &VA,
1622 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001623 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001624 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001626 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001627 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001628 EVT ValVT;
1629
1630 // If value is passed by pointer we have address passed instead of the value
1631 // itself.
1632 if (VA.getLocInfo() == CCValAssign::Indirect)
1633 ValVT = VA.getLocVT();
1634 else
1635 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001636
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001637 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001638 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001639 // In case of tail call optimization mark all arguments mutable. Since they
1640 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001641 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001642 unsigned Bytes = Flags.getByValSize();
1643 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1644 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001645 return DAG.getFrameIndex(FI, getPointerTy());
1646 } else {
1647 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001648 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001649 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1650 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001651 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001652 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001653 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001654}
1655
Dan Gohman475871a2008-07-27 21:46:04 +00001656SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001658 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 bool isVarArg,
1660 const SmallVectorImpl<ISD::InputArg> &Ins,
1661 DebugLoc dl,
1662 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001663 SmallVectorImpl<SDValue> &InVals)
1664 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001665 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 const Function* Fn = MF.getFunction();
1669 if (Fn->hasExternalLinkage() &&
1670 Subtarget->isTargetCygMing() &&
1671 Fn->getName() == "main")
1672 FuncInfo->setForceFramePointer(true);
1673
Evan Cheng1bc78042006-04-26 01:20:17 +00001674 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001676 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001677
Chris Lattner29689432010-03-11 00:22:57 +00001678 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1679 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001680
Chris Lattner638402b2007-02-28 07:00:42 +00001681 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001682 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001683 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1684 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001685
1686 // Allocate shadow area for Win64
1687 if (IsWin64) {
1688 CCInfo.AllocateStack(32, 8);
1689 }
1690
Duncan Sands45907662010-10-31 13:21:44 +00001691 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001692
Chris Lattnerf39f7712007-02-28 05:46:49 +00001693 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001694 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001695 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1696 CCValAssign &VA = ArgLocs[i];
1697 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1698 // places.
1699 assert(VA.getValNo() != LastVal &&
1700 "Don't support value assigned to multiple locs yet");
1701 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001702
Chris Lattnerf39f7712007-02-28 05:46:49 +00001703 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001704 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001705 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001707 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001711 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001713 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001714 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1715 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001716 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001717 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001718 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001719 RC = X86::VR64RegisterClass;
1720 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001721 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001722
Devang Patel68e6bee2011-02-21 23:21:26 +00001723 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001725
Chris Lattnerf39f7712007-02-28 05:46:49 +00001726 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1727 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1728 // right size.
1729 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001730 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001731 DAG.getValueType(VA.getValVT()));
1732 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001733 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001734 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001735 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001736 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001738 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001739 // Handle MMX values passed in XMM regs.
1740 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001741 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1742 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001743 } else
1744 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001745 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001746 } else {
1747 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001749 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001750
1751 // If value is passed via pointer - do a load.
1752 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001753 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1754 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001755
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001757 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001758
Dan Gohman61a92132008-04-21 23:59:07 +00001759 // The x86-64 ABI for returning structs by value requires that we copy
1760 // the sret argument into %rax for the return. Save the argument into
1761 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001762 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001763 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1764 unsigned Reg = FuncInfo->getSRetReturnReg();
1765 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001767 FuncInfo->setSRetReturnReg(Reg);
1768 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001769 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001771 }
1772
Chris Lattnerf39f7712007-02-28 05:46:49 +00001773 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001774 // Align stack specially for tail calls.
1775 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001776 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001777
Evan Cheng1bc78042006-04-26 01:20:17 +00001778 // If the function takes variable number of arguments, make a frame index for
1779 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001780 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001781 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1782 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001783 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001784 }
1785 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001786 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1787
1788 // FIXME: We should really autogenerate these arrays
1789 static const unsigned GPR64ArgRegsWin64[] = {
1790 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001792 static const unsigned GPR64ArgRegs64Bit[] = {
1793 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1794 };
1795 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001796 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1797 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1798 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001799 const unsigned *GPR64ArgRegs;
1800 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001801
1802 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001803 // The XMM registers which might contain var arg parameters are shadowed
1804 // in their paired GPR. So we only need to save the GPR to their home
1805 // slots.
1806 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001807 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001808 } else {
1809 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1810 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001811
1812 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001813 }
1814 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1815 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001816
Devang Patel578efa92009-06-05 21:57:13 +00001817 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001818 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001819 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001820 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001821 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001822 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001823 // Kernel mode asks for SSE to be disabled, so don't push them
1824 // on the stack.
1825 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001826
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001827 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001828 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001829 // Get to the caller-allocated home save location. Add 8 to account
1830 // for the return address.
1831 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001832 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001833 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001834 // Fixup to set vararg frame on shadow area (4 x i64).
1835 if (NumIntRegs < 4)
1836 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001837 } else {
1838 // For X86-64, if there are vararg parameters that are passed via
1839 // registers, then we must store them to their spots on the stack so they
1840 // may be loaded by deferencing the result of va_next.
1841 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1842 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1843 FuncInfo->setRegSaveFrameIndex(
1844 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001845 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001846 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001847
Gordon Henriksen86737662008-01-05 16:56:59 +00001848 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001849 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001850 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1851 getPointerTy());
1852 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001853 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001854 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1855 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001856 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001857 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001860 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001861 MachinePointerInfo::getFixedStack(
1862 FuncInfo->getRegSaveFrameIndex(), Offset),
1863 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001864 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001865 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001866 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001867
Dan Gohmanface41a2009-08-16 21:24:25 +00001868 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1869 // Now store the XMM (fp + vector) parameter registers.
1870 SmallVector<SDValue, 11> SaveXMMOps;
1871 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001872
Devang Patel68e6bee2011-02-21 23:21:26 +00001873 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001874 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1875 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001876
Dan Gohman1e93df62010-04-17 14:41:14 +00001877 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1878 FuncInfo->getRegSaveFrameIndex()));
1879 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1880 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001881
Dan Gohmanface41a2009-08-16 21:24:25 +00001882 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001883 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001884 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001885 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1886 SaveXMMOps.push_back(Val);
1887 }
1888 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1889 MVT::Other,
1890 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001892
1893 if (!MemOps.empty())
1894 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1895 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001896 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001897 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001898
Gordon Henriksen86737662008-01-05 16:56:59 +00001899 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001900 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001901 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001902 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001903 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001904 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001905 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001906 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001907 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001908
Gordon Henriksen86737662008-01-05 16:56:59 +00001909 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001910 // RegSaveFrameIndex is X86-64 only.
1911 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001912 if (CallConv == CallingConv::X86_FastCall ||
1913 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001914 // fastcc functions can't have varargs.
1915 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001916 }
Evan Cheng25caf632006-05-23 21:06:34 +00001917
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001919}
1920
Dan Gohman475871a2008-07-27 21:46:04 +00001921SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001922X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1923 SDValue StackPtr, SDValue Arg,
1924 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001925 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001926 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001927 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001929 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001930 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001931 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001932
1933 return DAG.getStore(Chain, dl, Arg, PtrOff,
1934 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001935 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001936}
1937
Bill Wendling64e87322009-01-16 19:25:27 +00001938/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001939/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001940SDValue
1941X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001942 SDValue &OutRetAddr, SDValue Chain,
1943 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001944 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001945 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001946 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001947 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001948
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001949 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001950 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1951 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001952 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001953}
1954
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001955/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001956/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001957static SDValue
1958EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001960 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001961 // Store the return address to the appropriate stack slot.
1962 if (!FPDiff) return Chain;
1963 // Calculate the new stack slot for the return address.
1964 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001965 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001966 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001967 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001969 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001970 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001971 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001972 return Chain;
1973}
1974
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001976X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001977 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001978 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001980 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001981 const SmallVectorImpl<ISD::InputArg> &Ins,
1982 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001983 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 MachineFunction &MF = DAG.getMachineFunction();
1985 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001986 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001987 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001988 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001989
Evan Cheng5f941932010-02-05 02:21:12 +00001990 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001991 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001992 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1993 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001994 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001995
1996 // Sibcalls are automatically detected tailcalls which do not require
1997 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001998 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001999 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002000
2001 if (isTailCall)
2002 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002003 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002004
Chris Lattner29689432010-03-11 00:22:57 +00002005 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2006 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002007
Chris Lattner638402b2007-02-28 07:00:42 +00002008 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002009 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002010 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2011 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002012
2013 // Allocate shadow area for Win64
2014 if (IsWin64) {
2015 CCInfo.AllocateStack(32, 8);
2016 }
2017
Duncan Sands45907662010-10-31 13:21:44 +00002018 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002019
Chris Lattner423c5f42007-02-28 05:31:48 +00002020 // Get a count of how many bytes are to be pushed on the stack.
2021 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002022 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002023 // This is a sibcall. The memory operands are available in caller's
2024 // own caller's stack.
2025 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002026 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002027 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002028
Gordon Henriksen86737662008-01-05 16:56:59 +00002029 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002030 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002032 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002033 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2034 FPDiff = NumBytesCallerPushed - NumBytes;
2035
2036 // Set the delta of movement of the returnaddr stackslot.
2037 // But only set if delta is greater than previous delta.
2038 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2039 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2040 }
2041
Evan Chengf22f9b32010-02-06 03:28:46 +00002042 if (!IsSibcall)
2043 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002044
Dan Gohman475871a2008-07-27 21:46:04 +00002045 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002046 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002047 if (isTailCall && FPDiff)
2048 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2049 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002050
Dan Gohman475871a2008-07-27 21:46:04 +00002051 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2052 SmallVector<SDValue, 8> MemOpChains;
2053 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002054
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002055 // Walk the register/memloc assignments, inserting copies/loads. In the case
2056 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002057 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2058 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002059 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002060 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002061 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002062 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002063
Chris Lattner423c5f42007-02-28 05:31:48 +00002064 // Promote the value if needed.
2065 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002066 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002067 case CCValAssign::Full: break;
2068 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002069 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002070 break;
2071 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002072 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002073 break;
2074 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002075 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2076 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002077 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002078 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2079 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002080 } else
2081 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2082 break;
2083 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002084 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002085 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002086 case CCValAssign::Indirect: {
2087 // Store the argument.
2088 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002089 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002090 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002091 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002092 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002093 Arg = SpillSlot;
2094 break;
2095 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Chris Lattner423c5f42007-02-28 05:31:48 +00002098 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002099 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2100 if (isVarArg && IsWin64) {
2101 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2102 // shadow reg if callee is a varargs function.
2103 unsigned ShadowReg = 0;
2104 switch (VA.getLocReg()) {
2105 case X86::XMM0: ShadowReg = X86::RCX; break;
2106 case X86::XMM1: ShadowReg = X86::RDX; break;
2107 case X86::XMM2: ShadowReg = X86::R8; break;
2108 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002109 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002110 if (ShadowReg)
2111 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002112 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002113 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002114 assert(VA.isMemLoc());
2115 if (StackPtr.getNode() == 0)
2116 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2117 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2118 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002119 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Evan Cheng32fe1032006-05-25 00:59:30 +00002122 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002124 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002125
Evan Cheng347d5f72006-04-28 21:29:37 +00002126 // Build a sequence of copy-to-reg nodes chained together with token chain
2127 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002129 // Tail call byval lowering might overwrite argument registers so in case of
2130 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002131 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002132 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002133 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002134 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002135 InFlag = Chain.getValue(1);
2136 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002137
Chris Lattner88e1fd52009-07-09 04:24:46 +00002138 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002139 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2140 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002141 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002142 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2143 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002144 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002145 InFlag);
2146 InFlag = Chain.getValue(1);
2147 } else {
2148 // If we are tail calling and generating PIC/GOT style code load the
2149 // address of the callee into ECX. The value in ecx is used as target of
2150 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2151 // for tail calls on PIC/GOT architectures. Normally we would just put the
2152 // address of GOT into ebx and then call target@PLT. But for tail calls
2153 // ebx would be restored (since ebx is callee saved) before jumping to the
2154 // target@PLT.
2155
2156 // Note: The actual moving to ECX is done further down.
2157 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2158 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2159 !G->getGlobal()->hasProtectedVisibility())
2160 Callee = LowerGlobalAddress(Callee, DAG);
2161 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002162 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002163 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002164 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002165
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002166 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002167 // From AMD64 ABI document:
2168 // For calls that may call functions that use varargs or stdargs
2169 // (prototype-less calls or calls to functions containing ellipsis (...) in
2170 // the declaration) %al is used as hidden argument to specify the number
2171 // of SSE registers used. The contents of %al do not need to match exactly
2172 // the number of registers, but must be an ubound on the number of SSE
2173 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002174
Gordon Henriksen86737662008-01-05 16:56:59 +00002175 // Count the number of XMM registers allocated.
2176 static const unsigned XMMArgRegs[] = {
2177 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2178 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2179 };
2180 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002181 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002182 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Dale Johannesendd64c412009-02-04 00:33:20 +00002184 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002185 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002186 InFlag = Chain.getValue(1);
2187 }
2188
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002189
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002190 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 if (isTailCall) {
2192 // Force all the incoming stack arguments to be loaded from the stack
2193 // before any new outgoing arguments are stored to the stack, because the
2194 // outgoing stack slots may alias the incoming argument stack slots, and
2195 // the alias isn't otherwise explicit. This is slightly more conservative
2196 // than necessary, because it means that each store effectively depends
2197 // on every argument instead of just those arguments it would clobber.
2198 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2199
Dan Gohman475871a2008-07-27 21:46:04 +00002200 SmallVector<SDValue, 8> MemOpChains2;
2201 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002202 int FI = 0;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002203 // Do not flag preceding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002204 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002205 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002206 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2207 CCValAssign &VA = ArgLocs[i];
2208 if (VA.isRegLoc())
2209 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002210 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002211 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002212 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002213 // Create frame index.
2214 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002215 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002216 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002218
Duncan Sands276dcbd2008-03-21 09:14:45 +00002219 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002220 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002222 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002223 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002224 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002225 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002226
Dan Gohman98ca4f22009-08-05 01:29:28 +00002227 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2228 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002229 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002230 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002231 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002232 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002234 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002235 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002236 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002237 }
2238 }
2239
2240 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002241 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002242 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002243
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002244 // Copy arguments to their registers.
2245 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002246 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002247 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002248 InFlag = Chain.getValue(1);
2249 }
Dan Gohman475871a2008-07-27 21:46:04 +00002250 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002251
Gordon Henriksen86737662008-01-05 16:56:59 +00002252 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002253 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002254 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002255 }
2256
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002257 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2258 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2259 // In the 64-bit large code model, we have to make all calls
2260 // through a register, since the call instruction's 32-bit
2261 // pc-relative offset may not be large enough to hold the whole
2262 // address.
2263 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002264 // If the callee is a GlobalAddress node (quite common, every direct call
2265 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2266 // it.
2267
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002268 // We should use extra load for direct calls to dllimported functions in
2269 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002270 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002271 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002272 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002273
Chris Lattner48a7d022009-07-09 05:02:21 +00002274 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2275 // external symbols most go through the PLT in PIC mode. If the symbol
2276 // has hidden or protected visibility, or if it is static or local, then
2277 // we don't need to use the PLT - we can directly call it.
2278 if (Subtarget->isTargetELF() &&
2279 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002280 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002281 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002282 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002283 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002284 (!Subtarget->getTargetTriple().isMacOSX() ||
2285 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002286 // PC-relative references to external symbols should go through $stub,
2287 // unless we're building with the leopard linker or later, which
2288 // automatically synthesizes these stubs.
2289 OpFlags = X86II::MO_DARWIN_STUB;
2290 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002291
Devang Patel0d881da2010-07-06 22:08:15 +00002292 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002293 G->getOffset(), OpFlags);
2294 }
Bill Wendling056292f2008-09-16 21:48:12 +00002295 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002296 unsigned char OpFlags = 0;
2297
Evan Cheng1bf891a2010-12-01 22:59:46 +00002298 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2299 // external symbols should go through the PLT.
2300 if (Subtarget->isTargetELF() &&
2301 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2302 OpFlags = X86II::MO_PLT;
2303 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002304 (!Subtarget->getTargetTriple().isMacOSX() ||
2305 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002306 // PC-relative references to external symbols should go through $stub,
2307 // unless we're building with the leopard linker or later, which
2308 // automatically synthesizes these stubs.
2309 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002310 }
Eric Christopherfd179292009-08-27 18:07:15 +00002311
Chris Lattner48a7d022009-07-09 05:02:21 +00002312 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2313 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002314 }
2315
Chris Lattnerd96d0722007-02-25 06:40:16 +00002316 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002317 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002318 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002319
Evan Chengf22f9b32010-02-06 03:28:46 +00002320 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002321 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2322 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002323 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002326 Ops.push_back(Chain);
2327 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002328
Dan Gohman98ca4f22009-08-05 01:29:28 +00002329 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002331
Gordon Henriksen86737662008-01-05 16:56:59 +00002332 // Add argument registers to the end of the list so that they are known live
2333 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002334 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2335 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2336 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002337
Evan Cheng586ccac2008-03-18 23:36:35 +00002338 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002339 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002340 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2341
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002342 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002343 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002345
Gabor Greifba36cb52008-08-28 21:40:38 +00002346 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002347 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002348
Dan Gohman98ca4f22009-08-05 01:29:28 +00002349 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002350 // We used to do:
2351 //// If this is the first return lowered for this function, add the regs
2352 //// to the liveout set for the function.
2353 // This isn't right, although it's probably harmless on x86; liveouts
2354 // should be computed from returns not tail calls. Consider a void
2355 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002356 return DAG.getNode(X86ISD::TC_RETURN, dl,
2357 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002358 }
2359
Dale Johannesenace16102009-02-03 19:33:06 +00002360 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002361 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002362
Chris Lattner2d297092006-05-23 18:50:38 +00002363 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002364 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002365 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002366 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002367 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002368 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002369 // pops the hidden struct pointer, so we have to push it back.
2370 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002371 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002372 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002373 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002374
Gordon Henriksenae636f82008-01-03 16:47:34 +00002375 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002376 if (!IsSibcall) {
2377 Chain = DAG.getCALLSEQ_END(Chain,
2378 DAG.getIntPtrConstant(NumBytes, true),
2379 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2380 true),
2381 InFlag);
2382 InFlag = Chain.getValue(1);
2383 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002384
Chris Lattner3085e152007-02-25 08:59:22 +00002385 // Handle result values, copying them out of physregs into vregs that we
2386 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002387 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2388 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002389}
2390
Evan Cheng25ab6902006-09-08 06:48:29 +00002391
2392//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002393// Fast Calling Convention (tail call) implementation
2394//===----------------------------------------------------------------------===//
2395
2396// Like std call, callee cleans arguments, convention except that ECX is
2397// reserved for storing the tail called function address. Only 2 registers are
2398// free for argument passing (inreg). Tail call optimization is performed
2399// provided:
2400// * tailcallopt is enabled
2401// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002402// On X86_64 architecture with GOT-style position independent code only local
2403// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002404// To keep the stack aligned according to platform abi the function
2405// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2406// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002407// If a tail called function callee has more arguments than the caller the
2408// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002409// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002410// original REtADDR, but before the saved framepointer or the spilled registers
2411// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2412// stack layout:
2413// arg1
2414// arg2
2415// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002416// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002417// move area ]
2418// (possible EBP)
2419// ESI
2420// EDI
2421// local1 ..
2422
2423/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2424/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002425unsigned
2426X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2427 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002428 MachineFunction &MF = DAG.getMachineFunction();
2429 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002430 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002431 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002432 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002433 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002434 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002435 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2436 // Number smaller than 12 so just add the difference.
2437 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2438 } else {
2439 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002440 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002441 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002442 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002443 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002444}
2445
Evan Cheng5f941932010-02-05 02:21:12 +00002446/// MatchingStackOffset - Return true if the given stack call argument is
2447/// already available in the same position (relatively) of the caller's
2448/// incoming argument stack.
2449static
2450bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2451 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2452 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002453 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2454 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002455 if (Arg.getOpcode() == ISD::CopyFromReg) {
2456 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002457 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002458 return false;
2459 MachineInstr *Def = MRI->getVRegDef(VR);
2460 if (!Def)
2461 return false;
2462 if (!Flags.isByVal()) {
2463 if (!TII->isLoadFromStackSlot(Def, FI))
2464 return false;
2465 } else {
2466 unsigned Opcode = Def->getOpcode();
2467 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2468 Def->getOperand(1).isFI()) {
2469 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002470 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002471 } else
2472 return false;
2473 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002474 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2475 if (Flags.isByVal())
2476 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002477 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002478 // define @foo(%struct.X* %A) {
2479 // tail call @bar(%struct.X* byval %A)
2480 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002481 return false;
2482 SDValue Ptr = Ld->getBasePtr();
2483 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2484 if (!FINode)
2485 return false;
2486 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002487 } else
2488 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002489
Evan Cheng4cae1332010-03-05 08:38:04 +00002490 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002491 if (!MFI->isFixedObjectIndex(FI))
2492 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002493 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002494}
2495
Dan Gohman98ca4f22009-08-05 01:29:28 +00002496/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2497/// for tail call optimization. Targets which want to do tail call
2498/// optimization should implement this function.
2499bool
2500X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002501 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002502 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002503 bool isCalleeStructRet,
2504 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002505 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002506 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002507 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002508 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002509 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002510 CalleeCC != CallingConv::C)
2511 return false;
2512
Evan Cheng7096ae42010-01-29 06:45:59 +00002513 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002514 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002515 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002516 CallingConv::ID CallerCC = CallerF->getCallingConv();
2517 bool CCMatch = CallerCC == CalleeCC;
2518
Dan Gohman1797ed52010-02-08 20:27:50 +00002519 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002520 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002521 return true;
2522 return false;
2523 }
2524
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002525 // Look for obvious safe cases to perform tail call optimization that do not
2526 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002527
Evan Cheng2c12cb42010-03-26 16:26:03 +00002528 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2529 // emit a special epilogue.
2530 if (RegInfo->needsStackRealignment(MF))
2531 return false;
2532
Evan Chenga375d472010-03-15 18:54:48 +00002533 // Also avoid sibcall optimization if either caller or callee uses struct
2534 // return semantics.
2535 if (isCalleeStructRet || isCallerStructRet)
2536 return false;
2537
Chad Rosier871f6642011-05-18 19:59:50 +00002538 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002539 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002540 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002541
2542 // Optimizing for varargs on Win64 is unlikely to be safe without
2543 // additional testing.
2544 if (Subtarget->isTargetWin64())
2545 return false;
2546
Chad Rosier871f6642011-05-18 19:59:50 +00002547 SmallVector<CCValAssign, 16> ArgLocs;
2548 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2549 ArgLocs, *DAG.getContext());
2550
Chad Rosier871f6642011-05-18 19:59:50 +00002551 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2552 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2553 if (!ArgLocs[i].isRegLoc())
2554 return false;
2555 }
2556
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002557 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2558 // Therefore if it's not used by the call it is not safe to optimize this into
2559 // a sibcall.
2560 bool Unused = false;
2561 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2562 if (!Ins[i].Used) {
2563 Unused = true;
2564 break;
2565 }
2566 }
2567 if (Unused) {
2568 SmallVector<CCValAssign, 16> RVLocs;
2569 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2570 RVLocs, *DAG.getContext());
2571 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002572 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002573 CCValAssign &VA = RVLocs[i];
2574 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2575 return false;
2576 }
2577 }
2578
Evan Cheng13617962010-04-30 01:12:32 +00002579 // If the calling conventions do not match, then we'd better make sure the
2580 // results are returned in the same way as what the caller expects.
2581 if (!CCMatch) {
2582 SmallVector<CCValAssign, 16> RVLocs1;
2583 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2584 RVLocs1, *DAG.getContext());
2585 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2586
2587 SmallVector<CCValAssign, 16> RVLocs2;
2588 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2589 RVLocs2, *DAG.getContext());
2590 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2591
2592 if (RVLocs1.size() != RVLocs2.size())
2593 return false;
2594 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2595 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2596 return false;
2597 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2598 return false;
2599 if (RVLocs1[i].isRegLoc()) {
2600 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2601 return false;
2602 } else {
2603 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2604 return false;
2605 }
2606 }
2607 }
2608
Evan Chenga6bff982010-01-30 01:22:00 +00002609 // If the callee takes no arguments then go on to check the results of the
2610 // call.
2611 if (!Outs.empty()) {
2612 // Check if stack adjustment is needed. For now, do not do this if any
2613 // argument is passed on the stack.
2614 SmallVector<CCValAssign, 16> ArgLocs;
2615 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2616 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002617
2618 // Allocate shadow area for Win64
2619 if (Subtarget->isTargetWin64()) {
2620 CCInfo.AllocateStack(32, 8);
2621 }
2622
Duncan Sands45907662010-10-31 13:21:44 +00002623 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002624 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002625 MachineFunction &MF = DAG.getMachineFunction();
2626 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2627 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002628
2629 // Check if the arguments are already laid out in the right way as
2630 // the caller's fixed stack objects.
2631 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002632 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2633 const X86InstrInfo *TII =
2634 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002635 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2636 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002637 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002638 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002639 if (VA.getLocInfo() == CCValAssign::Indirect)
2640 return false;
2641 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002642 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2643 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002644 return false;
2645 }
2646 }
2647 }
Evan Cheng9c044672010-05-29 01:35:22 +00002648
2649 // If the tailcall address may be in a register, then make sure it's
2650 // possible to register allocate for it. In 32-bit, the call address can
2651 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002652 // callee-saved registers are restored. These happen to be the same
2653 // registers used to pass 'inreg' arguments so watch out for those.
2654 if (!Subtarget->is64Bit() &&
2655 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002656 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002657 unsigned NumInRegs = 0;
2658 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2659 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002660 if (!VA.isRegLoc())
2661 continue;
2662 unsigned Reg = VA.getLocReg();
2663 switch (Reg) {
2664 default: break;
2665 case X86::EAX: case X86::EDX: case X86::ECX:
2666 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002667 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002668 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002669 }
2670 }
2671 }
Evan Chenga6bff982010-01-30 01:22:00 +00002672 }
Evan Chengb1712452010-01-27 06:25:16 +00002673
Dale Johannesend155d7e2010-10-25 22:17:05 +00002674 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002675 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002676 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2677 return false;
2678
Evan Cheng86809cc2010-02-03 03:28:02 +00002679 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002680}
2681
Dan Gohman3df24e62008-09-03 23:12:08 +00002682FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002683X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2684 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002685}
2686
2687
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002688//===----------------------------------------------------------------------===//
2689// Other Lowering Hooks
2690//===----------------------------------------------------------------------===//
2691
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002692static bool MayFoldLoad(SDValue Op) {
2693 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2694}
2695
2696static bool MayFoldIntoStore(SDValue Op) {
2697 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2698}
2699
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002700static bool isTargetShuffle(unsigned Opcode) {
2701 switch(Opcode) {
2702 default: return false;
2703 case X86ISD::PSHUFD:
2704 case X86ISD::PSHUFHW:
2705 case X86ISD::PSHUFLW:
2706 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002707 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002708 case X86ISD::SHUFPS:
2709 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002710 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002711 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002712 case X86ISD::MOVLPS:
2713 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002714 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002715 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002716 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002717 case X86ISD::MOVSS:
2718 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002719 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002720 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002721 case X86ISD::VUNPCKLPS:
2722 case X86ISD::VUNPCKLPD:
2723 case X86ISD::VUNPCKLPSY:
2724 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002725 case X86ISD::PUNPCKLWD:
2726 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002727 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002728 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002729 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002730 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002731 case X86ISD::PUNPCKHWD:
2732 case X86ISD::PUNPCKHBW:
2733 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002734 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002735 return true;
2736 }
2737 return false;
2738}
2739
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002740static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002741 SDValue V1, SelectionDAG &DAG) {
2742 switch(Opc) {
2743 default: llvm_unreachable("Unknown x86 shuffle node");
2744 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002745 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002746 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002747 return DAG.getNode(Opc, dl, VT, V1);
2748 }
2749
2750 return SDValue();
2751}
2752
2753static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002754 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002755 switch(Opc) {
2756 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002757 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002758 case X86ISD::PSHUFHW:
2759 case X86ISD::PSHUFLW:
2760 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2761 }
2762
2763 return SDValue();
2764}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002765
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002766static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2767 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2768 switch(Opc) {
2769 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002770 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002771 case X86ISD::SHUFPD:
2772 case X86ISD::SHUFPS:
2773 return DAG.getNode(Opc, dl, VT, V1, V2,
2774 DAG.getConstant(TargetMask, MVT::i8));
2775 }
2776 return SDValue();
2777}
2778
2779static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2780 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2781 switch(Opc) {
2782 default: llvm_unreachable("Unknown x86 shuffle node");
2783 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002784 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002785 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002786 case X86ISD::MOVLPS:
2787 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002788 case X86ISD::MOVSS:
2789 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002790 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002791 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002792 case X86ISD::VUNPCKLPS:
2793 case X86ISD::VUNPCKLPD:
2794 case X86ISD::VUNPCKLPSY:
2795 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002796 case X86ISD::PUNPCKLWD:
2797 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002798 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002799 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002800 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002801 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002802 case X86ISD::PUNPCKHWD:
2803 case X86ISD::PUNPCKHBW:
2804 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002805 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002806 return DAG.getNode(Opc, dl, VT, V1, V2);
2807 }
2808 return SDValue();
2809}
2810
Dan Gohmand858e902010-04-17 15:26:15 +00002811SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002812 MachineFunction &MF = DAG.getMachineFunction();
2813 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2814 int ReturnAddrIndex = FuncInfo->getRAIndex();
2815
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002816 if (ReturnAddrIndex == 0) {
2817 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002818 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002819 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002820 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002821 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002822 }
2823
Evan Cheng25ab6902006-09-08 06:48:29 +00002824 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002825}
2826
2827
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002828bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2829 bool hasSymbolicDisplacement) {
2830 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002831 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002832 return false;
2833
2834 // If we don't have a symbolic displacement - we don't have any extra
2835 // restrictions.
2836 if (!hasSymbolicDisplacement)
2837 return true;
2838
2839 // FIXME: Some tweaks might be needed for medium code model.
2840 if (M != CodeModel::Small && M != CodeModel::Kernel)
2841 return false;
2842
2843 // For small code model we assume that latest object is 16MB before end of 31
2844 // bits boundary. We may also accept pretty large negative constants knowing
2845 // that all objects are in the positive half of address space.
2846 if (M == CodeModel::Small && Offset < 16*1024*1024)
2847 return true;
2848
2849 // For kernel code model we know that all object resist in the negative half
2850 // of 32bits address space. We may not accept negative offsets, since they may
2851 // be just off and we may accept pretty large positive ones.
2852 if (M == CodeModel::Kernel && Offset > 0)
2853 return true;
2854
2855 return false;
2856}
2857
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002858/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2859/// specific condition code, returning the condition code and the LHS/RHS of the
2860/// comparison to make.
2861static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2862 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002863 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002864 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2865 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2866 // X > -1 -> X == 0, jump !sign.
2867 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002868 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002869 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2870 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002871 return X86::COND_S;
Andrew Trickf6c39412011-03-23 23:11:02 +00002872 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002873 // X < 1 -> X <= 0
2874 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002875 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002876 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002877 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002878
Evan Chengd9558e02006-01-06 00:43:03 +00002879 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002880 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002881 case ISD::SETEQ: return X86::COND_E;
2882 case ISD::SETGT: return X86::COND_G;
2883 case ISD::SETGE: return X86::COND_GE;
2884 case ISD::SETLT: return X86::COND_L;
2885 case ISD::SETLE: return X86::COND_LE;
2886 case ISD::SETNE: return X86::COND_NE;
2887 case ISD::SETULT: return X86::COND_B;
2888 case ISD::SETUGT: return X86::COND_A;
2889 case ISD::SETULE: return X86::COND_BE;
2890 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002891 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002892 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002893
Chris Lattner4c78e022008-12-23 23:42:27 +00002894 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002895
Chris Lattner4c78e022008-12-23 23:42:27 +00002896 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002897 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2898 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002899 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2900 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002901 }
2902
Chris Lattner4c78e022008-12-23 23:42:27 +00002903 switch (SetCCOpcode) {
2904 default: break;
2905 case ISD::SETOLT:
2906 case ISD::SETOLE:
2907 case ISD::SETUGT:
2908 case ISD::SETUGE:
2909 std::swap(LHS, RHS);
2910 break;
2911 }
2912
2913 // On a floating point condition, the flags are set as follows:
2914 // ZF PF CF op
2915 // 0 | 0 | 0 | X > Y
2916 // 0 | 0 | 1 | X < Y
2917 // 1 | 0 | 0 | X == Y
2918 // 1 | 1 | 1 | unordered
2919 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002920 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002921 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002922 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002923 case ISD::SETOLT: // flipped
2924 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002925 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002926 case ISD::SETOLE: // flipped
2927 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002928 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002929 case ISD::SETUGT: // flipped
2930 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002931 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002932 case ISD::SETUGE: // flipped
2933 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002934 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002935 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002936 case ISD::SETNE: return X86::COND_NE;
2937 case ISD::SETUO: return X86::COND_P;
2938 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002939 case ISD::SETOEQ:
2940 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002941 }
Evan Chengd9558e02006-01-06 00:43:03 +00002942}
2943
Evan Cheng4a460802006-01-11 00:33:36 +00002944/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2945/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002946/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002947static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002948 switch (X86CC) {
2949 default:
2950 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002951 case X86::COND_B:
2952 case X86::COND_BE:
2953 case X86::COND_E:
2954 case X86::COND_P:
2955 case X86::COND_A:
2956 case X86::COND_AE:
2957 case X86::COND_NE:
2958 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002959 return true;
2960 }
2961}
2962
Evan Chengeb2f9692009-10-27 19:56:55 +00002963/// isFPImmLegal - Returns true if the target can instruction select the
2964/// specified FP immediate natively. If false, the legalizer will
2965/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002966bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002967 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2968 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2969 return true;
2970 }
2971 return false;
2972}
2973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2975/// the specified range (L, H].
2976static bool isUndefOrInRange(int Val, int Low, int Hi) {
2977 return (Val < 0) || (Val >= Low && Val < Hi);
2978}
2979
2980/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2981/// specified value.
2982static bool isUndefOrEqual(int Val, int CmpVal) {
2983 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002984 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002985 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002986}
2987
Nate Begeman9008ca62009-04-27 18:41:29 +00002988/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2989/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2990/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002991static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002992 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002993 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002994 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 return (Mask[0] < 2 && Mask[1] < 2);
2996 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002997}
2998
Nate Begeman9008ca62009-04-27 18:41:29 +00002999bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003000 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 N->getMask(M);
3002 return ::isPSHUFDMask(M, N->getValueType(0));
3003}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003004
Nate Begeman9008ca62009-04-27 18:41:29 +00003005/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3006/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00003007static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00003009 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003010
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 // Lower quadword copied in order or undef.
3012 for (int i = 0; i != 4; ++i)
3013 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00003014 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003015
Evan Cheng506d3df2006-03-29 23:07:14 +00003016 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003017 for (int i = 4; i != 8; ++i)
3018 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00003019 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003020
Evan Cheng506d3df2006-03-29 23:07:14 +00003021 return true;
3022}
3023
Nate Begeman9008ca62009-04-27 18:41:29 +00003024bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003025 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 N->getMask(M);
3027 return ::isPSHUFHWMask(M, N->getValueType(0));
3028}
Evan Cheng506d3df2006-03-29 23:07:14 +00003029
Nate Begeman9008ca62009-04-27 18:41:29 +00003030/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3031/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003032static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003033 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003034 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003035
Rafael Espindola15684b22009-04-24 12:40:33 +00003036 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 for (int i = 4; i != 8; ++i)
3038 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003039 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003040
Rafael Espindola15684b22009-04-24 12:40:33 +00003041 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 for (int i = 0; i != 4; ++i)
3043 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003044 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003045
Rafael Espindola15684b22009-04-24 12:40:33 +00003046 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003047}
3048
Nate Begeman9008ca62009-04-27 18:41:29 +00003049bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003050 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003051 N->getMask(M);
3052 return ::isPSHUFLWMask(M, N->getValueType(0));
3053}
3054
Nate Begemana09008b2009-10-19 02:17:23 +00003055/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3056/// is suitable for input to PALIGNR.
3057static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3058 bool hasSSSE3) {
3059 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003060
Nate Begemana09008b2009-10-19 02:17:23 +00003061 // Do not handle v2i64 / v2f64 shuffles with palignr.
3062 if (e < 4 || !hasSSSE3)
3063 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003064
Nate Begemana09008b2009-10-19 02:17:23 +00003065 for (i = 0; i != e; ++i)
3066 if (Mask[i] >= 0)
3067 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003068
Nate Begemana09008b2009-10-19 02:17:23 +00003069 // All undef, not a palignr.
3070 if (i == e)
3071 return false;
3072
3073 // Determine if it's ok to perform a palignr with only the LHS, since we
3074 // don't have access to the actual shuffle elements to see if RHS is undef.
3075 bool Unary = Mask[i] < (int)e;
3076 bool NeedsUnary = false;
3077
3078 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003079
Nate Begemana09008b2009-10-19 02:17:23 +00003080 // Check the rest of the elements to see if they are consecutive.
3081 for (++i; i != e; ++i) {
3082 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003083 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003084 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003085
Nate Begemana09008b2009-10-19 02:17:23 +00003086 Unary = Unary && (m < (int)e);
3087 NeedsUnary = NeedsUnary || (m < s);
3088
3089 if (NeedsUnary && !Unary)
3090 return false;
3091 if (Unary && m != ((s+i) & (e-1)))
3092 return false;
3093 if (!Unary && m != (s+i))
3094 return false;
3095 }
3096 return true;
3097}
3098
3099bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3100 SmallVector<int, 8> M;
3101 N->getMask(M);
3102 return ::isPALIGNRMask(M, N->getValueType(0), true);
3103}
3104
Evan Cheng14aed5e2006-03-24 01:18:28 +00003105/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3106/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003107static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 int NumElems = VT.getVectorNumElements();
3109 if (NumElems != 2 && NumElems != 4)
3110 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003111
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 int Half = NumElems / 2;
3113 for (int i = 0; i < Half; ++i)
3114 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003115 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003116 for (int i = Half; i < NumElems; ++i)
3117 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003118 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003119
Evan Cheng14aed5e2006-03-24 01:18:28 +00003120 return true;
3121}
3122
Nate Begeman9008ca62009-04-27 18:41:29 +00003123bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3124 SmallVector<int, 8> M;
3125 N->getMask(M);
3126 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003127}
3128
Evan Cheng213d2cf2007-05-17 18:45:50 +00003129/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003130/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3131/// half elements to come from vector 1 (which would equal the dest.) and
3132/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003133static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003135
3136 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003137 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003138
Nate Begeman9008ca62009-04-27 18:41:29 +00003139 int Half = NumElems / 2;
3140 for (int i = 0; i < Half; ++i)
3141 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003142 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003143 for (int i = Half; i < NumElems; ++i)
3144 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003145 return false;
3146 return true;
3147}
3148
Nate Begeman9008ca62009-04-27 18:41:29 +00003149static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3150 SmallVector<int, 8> M;
3151 N->getMask(M);
3152 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003153}
3154
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003155/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3156/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003157bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3158 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003159 return false;
3160
Evan Cheng2064a2b2006-03-28 06:50:32 +00003161 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3163 isUndefOrEqual(N->getMaskElt(1), 7) &&
3164 isUndefOrEqual(N->getMaskElt(2), 2) &&
3165 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003166}
3167
Nate Begeman0b10b912009-11-07 23:17:15 +00003168/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3169/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3170/// <2, 3, 2, 3>
3171bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3172 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003173
Nate Begeman0b10b912009-11-07 23:17:15 +00003174 if (NumElems != 4)
3175 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003176
Nate Begeman0b10b912009-11-07 23:17:15 +00003177 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3178 isUndefOrEqual(N->getMaskElt(1), 3) &&
3179 isUndefOrEqual(N->getMaskElt(2), 2) &&
3180 isUndefOrEqual(N->getMaskElt(3), 3);
3181}
3182
Evan Cheng5ced1d82006-04-06 23:23:56 +00003183/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3184/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003185bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3186 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003187
Evan Cheng5ced1d82006-04-06 23:23:56 +00003188 if (NumElems != 2 && NumElems != 4)
3189 return false;
3190
Evan Chengc5cdff22006-04-07 21:53:05 +00003191 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003193 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003194
Evan Chengc5cdff22006-04-07 21:53:05 +00003195 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003196 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003197 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003198
3199 return true;
3200}
3201
Nate Begeman0b10b912009-11-07 23:17:15 +00003202/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3203/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3204bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003206
David Greenea20244d2011-03-02 17:23:43 +00003207 if ((NumElems != 2 && NumElems != 4)
3208 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003209 return false;
3210
Evan Chengc5cdff22006-04-07 21:53:05 +00003211 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003213 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003214
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 for (unsigned i = 0; i < NumElems/2; ++i)
3216 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003217 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003218
3219 return true;
3220}
3221
Evan Cheng0038e592006-03-28 00:39:58 +00003222/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3223/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003224static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003225 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003227 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003228 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003229
David Greenea20244d2011-03-02 17:23:43 +00003230 // Handle vector lengths > 128 bits. Define a "section" as a set of
3231 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3232 // sections.
3233 unsigned NumSections = VT.getSizeInBits() / 128;
3234 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3235 unsigned NumSectionElts = NumElts / NumSections;
3236
3237 unsigned Start = 0;
3238 unsigned End = NumSectionElts;
3239 for (unsigned s = 0; s < NumSections; ++s) {
3240 for (unsigned i = Start, j = s * NumSectionElts;
3241 i != End;
3242 i += 2, ++j) {
3243 int BitI = Mask[i];
3244 int BitI1 = Mask[i+1];
3245 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003246 return false;
David Greenea20244d2011-03-02 17:23:43 +00003247 if (V2IsSplat) {
3248 if (!isUndefOrEqual(BitI1, NumElts))
3249 return false;
3250 } else {
3251 if (!isUndefOrEqual(BitI1, j + NumElts))
3252 return false;
3253 }
Evan Cheng39623da2006-04-20 08:58:49 +00003254 }
David Greenea20244d2011-03-02 17:23:43 +00003255 // Process the next 128 bits.
3256 Start += NumSectionElts;
3257 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003258 }
David Greenea20244d2011-03-02 17:23:43 +00003259
Evan Cheng0038e592006-03-28 00:39:58 +00003260 return true;
3261}
3262
Nate Begeman9008ca62009-04-27 18:41:29 +00003263bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3264 SmallVector<int, 8> M;
3265 N->getMask(M);
3266 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003267}
3268
Evan Cheng4fcb9222006-03-28 02:43:26 +00003269/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3270/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003271static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003272 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003274 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003275 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003276
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3278 int BitI = Mask[i];
3279 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003280 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003281 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003282 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003283 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003284 return false;
3285 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003286 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003287 return false;
3288 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003289 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003290 return true;
3291}
3292
Nate Begeman9008ca62009-04-27 18:41:29 +00003293bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3294 SmallVector<int, 8> M;
3295 N->getMask(M);
3296 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003297}
3298
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003299/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3300/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3301/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003302static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003304 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003305 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003306
David Greenea20244d2011-03-02 17:23:43 +00003307 // Handle vector lengths > 128 bits. Define a "section" as a set of
3308 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3309 // sections.
3310 unsigned NumSections = VT.getSizeInBits() / 128;
3311 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3312 unsigned NumSectionElts = NumElems / NumSections;
3313
3314 for (unsigned s = 0; s < NumSections; ++s) {
3315 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3316 i != NumSectionElts * (s + 1);
3317 i += 2, ++j) {
3318 int BitI = Mask[i];
3319 int BitI1 = Mask[i+1];
3320
3321 if (!isUndefOrEqual(BitI, j))
3322 return false;
3323 if (!isUndefOrEqual(BitI1, j))
3324 return false;
3325 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003326 }
David Greenea20244d2011-03-02 17:23:43 +00003327
Rafael Espindola15684b22009-04-24 12:40:33 +00003328 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003329}
3330
Nate Begeman9008ca62009-04-27 18:41:29 +00003331bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3332 SmallVector<int, 8> M;
3333 N->getMask(M);
3334 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3335}
3336
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003337/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3338/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3339/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003340static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003342 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3343 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003344
Nate Begeman9008ca62009-04-27 18:41:29 +00003345 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3346 int BitI = Mask[i];
3347 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003348 if (!isUndefOrEqual(BitI, j))
3349 return false;
3350 if (!isUndefOrEqual(BitI1, j))
3351 return false;
3352 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003353 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3357 SmallVector<int, 8> M;
3358 N->getMask(M);
3359 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3360}
3361
Evan Cheng017dcc62006-04-21 01:05:10 +00003362/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3363/// specifies a shuffle of elements that is suitable for input to MOVSS,
3364/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003365static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003366 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003367 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003368
3369 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003370
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003372 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003373
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 for (int i = 1; i < NumElts; ++i)
3375 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003376 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003377
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003378 return true;
3379}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003380
Nate Begeman9008ca62009-04-27 18:41:29 +00003381bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3382 SmallVector<int, 8> M;
3383 N->getMask(M);
3384 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003385}
3386
Evan Cheng017dcc62006-04-21 01:05:10 +00003387/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3388/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003389/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003390static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 bool V2IsSplat = false, bool V2IsUndef = false) {
3392 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003393 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003394 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003395
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003397 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003398
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 for (int i = 1; i < NumOps; ++i)
3400 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3401 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3402 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003403 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003404
Evan Cheng39623da2006-04-20 08:58:49 +00003405 return true;
3406}
3407
Nate Begeman9008ca62009-04-27 18:41:29 +00003408static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003409 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 SmallVector<int, 8> M;
3411 N->getMask(M);
3412 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003413}
3414
Evan Chengd9539472006-04-14 21:59:03 +00003415/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3416/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003417bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3418 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003419 return false;
3420
3421 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003422 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 int Elt = N->getMaskElt(i);
3424 if (Elt >= 0 && Elt != 1)
3425 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003426 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003427
3428 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003429 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003430 int Elt = N->getMaskElt(i);
3431 if (Elt >= 0 && Elt != 3)
3432 return false;
3433 if (Elt == 3)
3434 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003435 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003436 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003438 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003439}
3440
3441/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3442/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003443bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3444 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003445 return false;
3446
3447 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 for (unsigned i = 0; i < 2; ++i)
3449 if (N->getMaskElt(i) > 0)
3450 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003451
3452 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003453 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003454 int Elt = N->getMaskElt(i);
3455 if (Elt >= 0 && Elt != 2)
3456 return false;
3457 if (Elt == 2)
3458 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003459 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003461 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003462}
3463
Evan Cheng0b457f02008-09-25 20:50:48 +00003464/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3465/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003466bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3467 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003468
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 for (int i = 0; i < e; ++i)
3470 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003471 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 for (int i = 0; i < e; ++i)
3473 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003474 return false;
3475 return true;
3476}
3477
David Greenec38a03e2011-02-03 15:50:00 +00003478/// isVEXTRACTF128Index - Return true if the specified
3479/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3480/// suitable for input to VEXTRACTF128.
3481bool X86::isVEXTRACTF128Index(SDNode *N) {
3482 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3483 return false;
3484
3485 // The index should be aligned on a 128-bit boundary.
3486 uint64_t Index =
3487 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3488
3489 unsigned VL = N->getValueType(0).getVectorNumElements();
3490 unsigned VBits = N->getValueType(0).getSizeInBits();
3491 unsigned ElSize = VBits / VL;
3492 bool Result = (Index * ElSize) % 128 == 0;
3493
3494 return Result;
3495}
3496
David Greeneccacdc12011-02-04 16:08:29 +00003497/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3498/// operand specifies a subvector insert that is suitable for input to
3499/// VINSERTF128.
3500bool X86::isVINSERTF128Index(SDNode *N) {
3501 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3502 return false;
3503
3504 // The index should be aligned on a 128-bit boundary.
3505 uint64_t Index =
3506 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3507
3508 unsigned VL = N->getValueType(0).getVectorNumElements();
3509 unsigned VBits = N->getValueType(0).getSizeInBits();
3510 unsigned ElSize = VBits / VL;
3511 bool Result = (Index * ElSize) % 128 == 0;
3512
3513 return Result;
3514}
3515
Evan Cheng63d33002006-03-22 08:01:21 +00003516/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003517/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003518unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003519 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3520 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3521
Evan Chengb9df0ca2006-03-22 02:53:00 +00003522 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3523 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003524 for (int i = 0; i < NumOperands; ++i) {
3525 int Val = SVOp->getMaskElt(NumOperands-i-1);
3526 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003527 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003528 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003529 if (i != NumOperands - 1)
3530 Mask <<= Shift;
3531 }
Evan Cheng63d33002006-03-22 08:01:21 +00003532 return Mask;
3533}
3534
Evan Cheng506d3df2006-03-29 23:07:14 +00003535/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003536/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003537unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003538 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003539 unsigned Mask = 0;
3540 // 8 nodes, but we only care about the last 4.
3541 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003542 int Val = SVOp->getMaskElt(i);
3543 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003544 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003545 if (i != 4)
3546 Mask <<= 2;
3547 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003548 return Mask;
3549}
3550
3551/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003552/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003553unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003554 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003555 unsigned Mask = 0;
3556 // 8 nodes, but we only care about the first 4.
3557 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 int Val = SVOp->getMaskElt(i);
3559 if (Val >= 0)
3560 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003561 if (i != 0)
3562 Mask <<= 2;
3563 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003564 return Mask;
3565}
3566
Nate Begemana09008b2009-10-19 02:17:23 +00003567/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3568/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3569unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3570 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3571 EVT VVT = N->getValueType(0);
3572 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3573 int Val = 0;
3574
3575 unsigned i, e;
3576 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3577 Val = SVOp->getMaskElt(i);
3578 if (Val >= 0)
3579 break;
3580 }
3581 return (Val - i) * EltSize;
3582}
3583
David Greenec38a03e2011-02-03 15:50:00 +00003584/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3585/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3586/// instructions.
3587unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3588 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3589 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3590
3591 uint64_t Index =
3592 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3593
3594 EVT VecVT = N->getOperand(0).getValueType();
3595 EVT ElVT = VecVT.getVectorElementType();
3596
3597 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3598
3599 return Index / NumElemsPerChunk;
3600}
3601
David Greeneccacdc12011-02-04 16:08:29 +00003602/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3603/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3604/// instructions.
3605unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3606 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3607 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3608
3609 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003610 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003611
3612 EVT VecVT = N->getValueType(0);
3613 EVT ElVT = VecVT.getVectorElementType();
3614
3615 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3616
3617 return Index / NumElemsPerChunk;
3618}
3619
Evan Cheng37b73872009-07-30 08:33:02 +00003620/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3621/// constant +0.0.
3622bool X86::isZeroNode(SDValue Elt) {
3623 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003624 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003625 (isa<ConstantFPSDNode>(Elt) &&
3626 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3627}
3628
Nate Begeman9008ca62009-04-27 18:41:29 +00003629/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3630/// their permute mask.
3631static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3632 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003633 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003634 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003635 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003636
Nate Begeman5a5ca152009-04-29 05:20:52 +00003637 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 int idx = SVOp->getMaskElt(i);
3639 if (idx < 0)
3640 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003641 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003642 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003643 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003644 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003645 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3647 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003648}
3649
Evan Cheng779ccea2007-12-07 21:30:01 +00003650/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3651/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003652static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003653 unsigned NumElems = VT.getVectorNumElements();
3654 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 int idx = Mask[i];
3656 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003657 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003658 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003660 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003661 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003662 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003663}
3664
Evan Cheng533a0aa2006-04-19 20:35:22 +00003665/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3666/// match movhlps. The lower half elements should come from upper half of
3667/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003668/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003669static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3670 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003671 return false;
3672 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003673 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003674 return false;
3675 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003676 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003677 return false;
3678 return true;
3679}
3680
Evan Cheng5ced1d82006-04-06 23:23:56 +00003681/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003682/// is promoted to a vector. It also returns the LoadSDNode by reference if
3683/// required.
3684static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003685 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3686 return false;
3687 N = N->getOperand(0).getNode();
3688 if (!ISD::isNON_EXTLoad(N))
3689 return false;
3690 if (LD)
3691 *LD = cast<LoadSDNode>(N);
3692 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003693}
3694
Evan Cheng533a0aa2006-04-19 20:35:22 +00003695/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3696/// match movlp{s|d}. The lower half elements should come from lower half of
3697/// V1 (and in order), and the upper half elements should come from the upper
3698/// half of V2 (and in order). And since V1 will become the source of the
3699/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003700static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3701 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003702 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003703 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003704 // Is V2 is a vector load, don't do this transformation. We will try to use
3705 // load folding shufps op.
3706 if (ISD::isNON_EXTLoad(V2))
3707 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003708
Nate Begeman5a5ca152009-04-29 05:20:52 +00003709 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003710
Evan Cheng533a0aa2006-04-19 20:35:22 +00003711 if (NumElems != 2 && NumElems != 4)
3712 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003713 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003714 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003715 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003716 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003718 return false;
3719 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003720}
3721
Evan Cheng39623da2006-04-20 08:58:49 +00003722/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3723/// all the same.
3724static bool isSplatVector(SDNode *N) {
3725 if (N->getOpcode() != ISD::BUILD_VECTOR)
3726 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003727
Dan Gohman475871a2008-07-27 21:46:04 +00003728 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003729 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3730 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003731 return false;
3732 return true;
3733}
3734
Evan Cheng213d2cf2007-05-17 18:45:50 +00003735/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003736/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003737/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003738static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003739 SDValue V1 = N->getOperand(0);
3740 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003741 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3742 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003743 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003744 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003745 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003746 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3747 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003748 if (Opc != ISD::BUILD_VECTOR ||
3749 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003750 return false;
3751 } else if (Idx >= 0) {
3752 unsigned Opc = V1.getOpcode();
3753 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3754 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003755 if (Opc != ISD::BUILD_VECTOR ||
3756 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003757 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003758 }
3759 }
3760 return true;
3761}
3762
3763/// getZeroVector - Returns a vector of specified type with all zero elements.
3764///
Owen Andersone50ed302009-08-10 22:56:29 +00003765static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003766 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003767 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003768
Dale Johannesen0488fb62010-09-30 23:57:10 +00003769 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003770 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003771 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003772 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003773 if (HasSSE2) { // SSE2
3774 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3775 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3776 } else { // SSE1
3777 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3778 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3779 }
3780 } else if (VT.getSizeInBits() == 256) { // AVX
3781 // 256-bit logic and arithmetic instructions in AVX are
3782 // all floating-point, no support for integer ops. Default
3783 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003784 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003785 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3786 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003787 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003788 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003789}
3790
Chris Lattner8a594482007-11-25 00:24:49 +00003791/// getOnesVector - Returns a vector of specified type with all bits set.
3792///
Owen Andersone50ed302009-08-10 22:56:29 +00003793static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003794 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003795
Chris Lattner8a594482007-11-25 00:24:49 +00003796 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3797 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003799 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003800 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003801 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003802}
3803
3804
Evan Cheng39623da2006-04-20 08:58:49 +00003805/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3806/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003807static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003808 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003809 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003810
Evan Cheng39623da2006-04-20 08:58:49 +00003811 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003812 SmallVector<int, 8> MaskVec;
3813 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003814
Nate Begeman5a5ca152009-04-29 05:20:52 +00003815 for (unsigned i = 0; i != NumElems; ++i) {
3816 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 MaskVec[i] = NumElems;
3818 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003819 }
Evan Cheng39623da2006-04-20 08:58:49 +00003820 }
Evan Cheng39623da2006-04-20 08:58:49 +00003821 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3823 SVOp->getOperand(1), &MaskVec[0]);
3824 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003825}
3826
Evan Cheng017dcc62006-04-21 01:05:10 +00003827/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3828/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003829static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 SDValue V2) {
3831 unsigned NumElems = VT.getVectorNumElements();
3832 SmallVector<int, 8> Mask;
3833 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003834 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003835 Mask.push_back(i);
3836 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003837}
3838
Nate Begeman9008ca62009-04-27 18:41:29 +00003839/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003840static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003841 SDValue V2) {
3842 unsigned NumElems = VT.getVectorNumElements();
3843 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003844 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003845 Mask.push_back(i);
3846 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003847 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003849}
3850
Nate Begeman9008ca62009-04-27 18:41:29 +00003851/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003852static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003853 SDValue V2) {
3854 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003855 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003857 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003858 Mask.push_back(i + Half);
3859 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003860 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003861 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003862}
3863
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003864/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3865static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003866 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003867 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003868 DebugLoc dl = SV->getDebugLoc();
3869 SDValue V1 = SV->getOperand(0);
3870 int NumElems = VT.getVectorNumElements();
3871 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003872
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 // unpack elements to the correct location
3874 while (NumElems > 4) {
3875 if (EltNo < NumElems/2) {
3876 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3877 } else {
3878 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3879 EltNo -= NumElems/2;
3880 }
3881 NumElems >>= 1;
3882 }
Eric Christopherfd179292009-08-27 18:07:15 +00003883
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 // Perform the splat.
3885 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003886 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003888 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003889}
3890
Evan Chengba05f722006-04-21 23:03:30 +00003891/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003892/// vector of zero or undef vector. This produces a shuffle where the low
3893/// element of V2 is swizzled into the zero/undef vector, landing at element
3894/// 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 +00003895static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003896 bool isZero, bool HasSSE2,
3897 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003898 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3901 unsigned NumElems = VT.getVectorNumElements();
3902 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003903 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 // If this is the insertion idx, put the low elt of V2 here.
3905 MaskVec.push_back(i == Idx ? NumElems : i);
3906 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003907}
3908
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003909/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3910/// element of the result of the vector shuffle.
Benjamin Kramer050db522011-03-26 12:38:19 +00003911static SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3912 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003913 if (Depth == 6)
3914 return SDValue(); // Limit search depth.
3915
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003916 SDValue V = SDValue(N, 0);
3917 EVT VT = V.getValueType();
3918 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003919
3920 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3921 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3922 Index = SV->getMaskElt(Index);
3923
3924 if (Index < 0)
3925 return DAG.getUNDEF(VT.getVectorElementType());
3926
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003927 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003928 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003929 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003930 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003931
3932 // Recurse into target specific vector shuffles to find scalars.
3933 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003934 int NumElems = VT.getVectorNumElements();
3935 SmallVector<unsigned, 16> ShuffleMask;
3936 SDValue ImmN;
3937
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003938 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003939 case X86ISD::SHUFPS:
3940 case X86ISD::SHUFPD:
3941 ImmN = N->getOperand(N->getNumOperands()-1);
3942 DecodeSHUFPSMask(NumElems,
3943 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3944 ShuffleMask);
3945 break;
3946 case X86ISD::PUNPCKHBW:
3947 case X86ISD::PUNPCKHWD:
3948 case X86ISD::PUNPCKHDQ:
3949 case X86ISD::PUNPCKHQDQ:
3950 DecodePUNPCKHMask(NumElems, ShuffleMask);
3951 break;
3952 case X86ISD::UNPCKHPS:
3953 case X86ISD::UNPCKHPD:
3954 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3955 break;
3956 case X86ISD::PUNPCKLBW:
3957 case X86ISD::PUNPCKLWD:
3958 case X86ISD::PUNPCKLDQ:
3959 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003960 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003961 break;
3962 case X86ISD::UNPCKLPS:
3963 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003964 case X86ISD::VUNPCKLPS:
3965 case X86ISD::VUNPCKLPD:
3966 case X86ISD::VUNPCKLPSY:
3967 case X86ISD::VUNPCKLPDY:
3968 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003969 break;
3970 case X86ISD::MOVHLPS:
3971 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3972 break;
3973 case X86ISD::MOVLHPS:
3974 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3975 break;
3976 case X86ISD::PSHUFD:
3977 ImmN = N->getOperand(N->getNumOperands()-1);
3978 DecodePSHUFMask(NumElems,
3979 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3980 ShuffleMask);
3981 break;
3982 case X86ISD::PSHUFHW:
3983 ImmN = N->getOperand(N->getNumOperands()-1);
3984 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3985 ShuffleMask);
3986 break;
3987 case X86ISD::PSHUFLW:
3988 ImmN = N->getOperand(N->getNumOperands()-1);
3989 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3990 ShuffleMask);
3991 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003992 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003993 case X86ISD::MOVSD: {
3994 // The index 0 always comes from the first element of the second source,
3995 // this is why MOVSS and MOVSD are used in the first place. The other
3996 // elements come from the other positions of the first source vector.
3997 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003998 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3999 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00004000 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004001 default:
4002 assert("not implemented for target shuffle node");
4003 return SDValue();
4004 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004005
4006 Index = ShuffleMask[Index];
4007 if (Index < 0)
4008 return DAG.getUNDEF(VT.getVectorElementType());
4009
4010 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
4011 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
4012 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004013 }
4014
4015 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004016 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004017 V = V.getOperand(0);
4018 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004019 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004020
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004021 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004022 return SDValue();
4023 }
4024
4025 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4026 return (Index == 0) ? V.getOperand(0)
4027 : DAG.getUNDEF(VT.getVectorElementType());
4028
4029 if (V.getOpcode() == ISD::BUILD_VECTOR)
4030 return V.getOperand(Index);
4031
4032 return SDValue();
4033}
4034
4035/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4036/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004037/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004038static
4039unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4040 bool ZerosFromLeft, SelectionDAG &DAG) {
4041 int i = 0;
4042
4043 while (i < NumElems) {
4044 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004045 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004046 if (!(Elt.getNode() &&
4047 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4048 break;
4049 ++i;
4050 }
4051
4052 return i;
4053}
4054
4055/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4056/// MaskE correspond consecutively to elements from one of the vector operands,
4057/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4058static
4059bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4060 int OpIdx, int NumElems, unsigned &OpNum) {
4061 bool SeenV1 = false;
4062 bool SeenV2 = false;
4063
4064 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4065 int Idx = SVOp->getMaskElt(i);
4066 // Ignore undef indicies
4067 if (Idx < 0)
4068 continue;
4069
4070 if (Idx < NumElems)
4071 SeenV1 = true;
4072 else
4073 SeenV2 = true;
4074
4075 // Only accept consecutive elements from the same vector
4076 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4077 return false;
4078 }
4079
4080 OpNum = SeenV1 ? 0 : 1;
4081 return true;
4082}
4083
4084/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4085/// logical left shift of a vector.
4086static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4087 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4088 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4089 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4090 false /* check zeros from right */, DAG);
4091 unsigned OpSrc;
4092
4093 if (!NumZeros)
4094 return false;
4095
4096 // Considering the elements in the mask that are not consecutive zeros,
4097 // check if they consecutively come from only one of the source vectors.
4098 //
4099 // V1 = {X, A, B, C} 0
4100 // \ \ \ /
4101 // vector_shuffle V1, V2 <1, 2, 3, X>
4102 //
4103 if (!isShuffleMaskConsecutive(SVOp,
4104 0, // Mask Start Index
4105 NumElems-NumZeros-1, // Mask End Index
4106 NumZeros, // Where to start looking in the src vector
4107 NumElems, // Number of elements in vector
4108 OpSrc)) // Which source operand ?
4109 return false;
4110
4111 isLeft = false;
4112 ShAmt = NumZeros;
4113 ShVal = SVOp->getOperand(OpSrc);
4114 return true;
4115}
4116
4117/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4118/// logical left shift of a vector.
4119static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4120 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4121 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4122 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4123 true /* check zeros from left */, DAG);
4124 unsigned OpSrc;
4125
4126 if (!NumZeros)
4127 return false;
4128
4129 // Considering the elements in the mask that are not consecutive zeros,
4130 // check if they consecutively come from only one of the source vectors.
4131 //
4132 // 0 { A, B, X, X } = V2
4133 // / \ / /
4134 // vector_shuffle V1, V2 <X, X, 4, 5>
4135 //
4136 if (!isShuffleMaskConsecutive(SVOp,
4137 NumZeros, // Mask Start Index
4138 NumElems-1, // Mask End Index
4139 0, // Where to start looking in the src vector
4140 NumElems, // Number of elements in vector
4141 OpSrc)) // Which source operand ?
4142 return false;
4143
4144 isLeft = true;
4145 ShAmt = NumZeros;
4146 ShVal = SVOp->getOperand(OpSrc);
4147 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004148}
4149
4150/// isVectorShift - Returns true if the shuffle can be implemented as a
4151/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004152static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004153 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004154 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4155 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4156 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004157
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004158 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004159}
4160
Evan Chengc78d3b42006-04-24 18:01:45 +00004161/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4162///
Dan Gohman475871a2008-07-27 21:46:04 +00004163static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004164 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004165 SelectionDAG &DAG,
4166 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004167 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004168 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004169
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004170 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004171 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004172 bool First = true;
4173 for (unsigned i = 0; i < 16; ++i) {
4174 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4175 if (ThisIsNonZero && First) {
4176 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004178 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004180 First = false;
4181 }
4182
4183 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004184 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004185 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4186 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004187 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004189 }
4190 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004191 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4192 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4193 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004194 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004196 } else
4197 ThisElt = LastElt;
4198
Gabor Greifba36cb52008-08-28 21:40:38 +00004199 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004201 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004202 }
4203 }
4204
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004205 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004206}
4207
Bill Wendlinga348c562007-03-22 18:42:45 +00004208/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004209///
Dan Gohman475871a2008-07-27 21:46:04 +00004210static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004211 unsigned NumNonZero, unsigned NumZero,
4212 SelectionDAG &DAG,
4213 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004214 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004215 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004216
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004217 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004218 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004219 bool First = true;
4220 for (unsigned i = 0; i < 8; ++i) {
4221 bool isNonZero = (NonZeros & (1 << i)) != 0;
4222 if (isNonZero) {
4223 if (First) {
4224 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004226 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004228 First = false;
4229 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004230 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004232 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004233 }
4234 }
4235
4236 return V;
4237}
4238
Evan Chengf26ffe92008-05-29 08:22:04 +00004239/// getVShift - Return a vector logical shift node.
4240///
Owen Andersone50ed302009-08-10 22:56:29 +00004241static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004242 unsigned NumBits, SelectionDAG &DAG,
4243 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004244 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004245 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004246 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4247 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004248 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004249 DAG.getConstant(NumBits,
4250 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004251}
4252
Dan Gohman475871a2008-07-27 21:46:04 +00004253SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004254X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004255 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004256
Evan Chengc3630942009-12-09 21:00:30 +00004257 // Check if the scalar load can be widened into a vector load. And if
4258 // the address is "base + cst" see if the cst can be "absorbed" into
4259 // the shuffle mask.
4260 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4261 SDValue Ptr = LD->getBasePtr();
4262 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4263 return SDValue();
4264 EVT PVT = LD->getValueType(0);
4265 if (PVT != MVT::i32 && PVT != MVT::f32)
4266 return SDValue();
4267
4268 int FI = -1;
4269 int64_t Offset = 0;
4270 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4271 FI = FINode->getIndex();
4272 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004273 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004274 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4275 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4276 Offset = Ptr.getConstantOperandVal(1);
4277 Ptr = Ptr.getOperand(0);
4278 } else {
4279 return SDValue();
4280 }
4281
4282 SDValue Chain = LD->getChain();
4283 // Make sure the stack object alignment is at least 16.
4284 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4285 if (DAG.InferPtrAlignment(Ptr) < 16) {
4286 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004287 // Can't change the alignment. FIXME: It's possible to compute
4288 // the exact stack offset and reference FI + adjust offset instead.
4289 // If someone *really* cares about this. That's the way to implement it.
4290 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004291 } else {
4292 MFI->setObjectAlignment(FI, 16);
4293 }
4294 }
4295
4296 // (Offset % 16) must be multiple of 4. Then address is then
4297 // Ptr + (Offset & ~15).
4298 if (Offset < 0)
4299 return SDValue();
4300 if ((Offset % 16) & 3)
4301 return SDValue();
4302 int64_t StartOffset = Offset & ~15;
4303 if (StartOffset)
4304 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4305 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4306
4307 int EltNo = (Offset - StartOffset) >> 2;
4308 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4309 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004310 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4311 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004312 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004313 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004314 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4315 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004316 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004317 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004318 }
4319
4320 return SDValue();
4321}
4322
Michael J. Spencerec38de22010-10-10 22:04:20 +00004323/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4324/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004325/// load which has the same value as a build_vector whose operands are 'elts'.
4326///
4327/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004328///
Nate Begeman1449f292010-03-24 22:19:06 +00004329/// FIXME: we'd also like to handle the case where the last elements are zero
4330/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4331/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004332static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004333 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004334 EVT EltVT = VT.getVectorElementType();
4335 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004336
Nate Begemanfdea31a2010-03-24 20:49:50 +00004337 LoadSDNode *LDBase = NULL;
4338 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004339
Nate Begeman1449f292010-03-24 22:19:06 +00004340 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004341 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004342 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004343 for (unsigned i = 0; i < NumElems; ++i) {
4344 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004345
Nate Begemanfdea31a2010-03-24 20:49:50 +00004346 if (!Elt.getNode() ||
4347 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4348 return SDValue();
4349 if (!LDBase) {
4350 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4351 return SDValue();
4352 LDBase = cast<LoadSDNode>(Elt.getNode());
4353 LastLoadedElt = i;
4354 continue;
4355 }
4356 if (Elt.getOpcode() == ISD::UNDEF)
4357 continue;
4358
4359 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4360 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4361 return SDValue();
4362 LastLoadedElt = i;
4363 }
Nate Begeman1449f292010-03-24 22:19:06 +00004364
4365 // If we have found an entire vector of loads and undefs, then return a large
4366 // load of the entire vector width starting at the base pointer. If we found
4367 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004368 if (LastLoadedElt == NumElems - 1) {
4369 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004370 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004371 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004372 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004373 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004374 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004375 LDBase->isVolatile(), LDBase->isNonTemporal(),
4376 LDBase->getAlignment());
4377 } else if (NumElems == 4 && LastLoadedElt == 1) {
4378 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4379 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004380 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4381 Ops, 2, MVT::i32,
4382 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004383 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004384 }
4385 return SDValue();
4386}
4387
Evan Chengc3630942009-12-09 21:00:30 +00004388SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004389X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004390 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004391
David Greenef125a292011-02-08 19:04:41 +00004392 EVT VT = Op.getValueType();
4393 EVT ExtVT = VT.getVectorElementType();
4394
4395 unsigned NumElems = Op.getNumOperands();
4396
4397 // For AVX-length vectors, build the individual 128-bit pieces and
4398 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004399 if (VT.getSizeInBits() > 256 &&
4400 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004401 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4402 SmallVector<SDValue, 8> V;
4403 V.resize(NumElems);
4404 for (unsigned i = 0; i < NumElems; ++i) {
4405 V[i] = Op.getOperand(i);
4406 }
Owen Anderson95771af2011-02-25 21:41:48 +00004407
David Greenef125a292011-02-08 19:04:41 +00004408 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4409
4410 // Build the lower subvector.
4411 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4412 // Build the upper subvector.
4413 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4414 NumElems/2);
4415
4416 return ConcatVectors(Lower, Upper, DAG);
4417 }
4418
Chris Lattner6e80e442010-08-28 17:15:43 +00004419 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4420 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004421 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4422 // is present, so AllOnes is ignored.
4423 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4424 (Op.getValueType().getSizeInBits() != 256 &&
4425 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004426 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004427 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4428 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004429 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004430 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004431
Gabor Greifba36cb52008-08-28 21:40:38 +00004432 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004433 return getOnesVector(Op.getValueType(), DAG, dl);
4434 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004435 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436
Owen Andersone50ed302009-08-10 22:56:29 +00004437 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004438
Evan Cheng0db9fe62006-04-25 20:13:52 +00004439 unsigned NumZero = 0;
4440 unsigned NumNonZero = 0;
4441 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004442 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004443 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004444 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004445 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004446 if (Elt.getOpcode() == ISD::UNDEF)
4447 continue;
4448 Values.insert(Elt);
4449 if (Elt.getOpcode() != ISD::Constant &&
4450 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004451 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004452 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004453 NumZero++;
4454 else {
4455 NonZeros |= (1 << i);
4456 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004457 }
4458 }
4459
Chris Lattner97a2a562010-08-26 05:24:29 +00004460 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4461 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004462 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004463
Chris Lattner67f453a2008-03-09 05:42:06 +00004464 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004465 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004466 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004467 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004468
Chris Lattner62098042008-03-09 01:05:04 +00004469 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4470 // the value are obviously zero, truncate the value to i32 and do the
4471 // insertion that way. Only do this if the value is non-constant or if the
4472 // value is a constant being inserted into element 0. It is cheaper to do
4473 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004474 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004475 (!IsAllConstants || Idx == 0)) {
4476 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004477 // Handle SSE only.
4478 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4479 EVT VecVT = MVT::v4i32;
4480 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004481
Chris Lattner62098042008-03-09 01:05:04 +00004482 // Truncate the value (which may itself be a constant) to i32, and
4483 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004484 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004485 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004486 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4487 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004488
Chris Lattner62098042008-03-09 01:05:04 +00004489 // Now we have our 32-bit value zero extended in the low element of
4490 // a vector. If Idx != 0, swizzle it into place.
4491 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 SmallVector<int, 4> Mask;
4493 Mask.push_back(Idx);
4494 for (unsigned i = 1; i != VecElts; ++i)
4495 Mask.push_back(i);
4496 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004497 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004499 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004500 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004501 }
4502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004503
Chris Lattner19f79692008-03-08 22:59:52 +00004504 // If we have a constant or non-constant insertion into the low element of
4505 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4506 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004507 // depending on what the source datatype is.
4508 if (Idx == 0) {
4509 if (NumZero == 0) {
4510 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004511 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4512 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004513 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4514 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4515 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4516 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004517 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4518 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004519 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4520 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004521 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4522 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4523 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004524 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004525 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004526 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004527
4528 // Is it a vector logical left shift?
4529 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004530 X86::isZeroNode(Op.getOperand(0)) &&
4531 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004532 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004533 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004534 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004535 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004536 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004538
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004539 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004540 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004541
Chris Lattner19f79692008-03-08 22:59:52 +00004542 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4543 // is a non-constant being inserted into an element other than the low one,
4544 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4545 // movd/movss) to move this into the low element, then shuffle it into
4546 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004548 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004549
Evan Cheng0db9fe62006-04-25 20:13:52 +00004550 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004551 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4552 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004555 MaskVec.push_back(i == Idx ? 0 : 1);
4556 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557 }
4558 }
4559
Chris Lattner67f453a2008-03-09 05:42:06 +00004560 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004561 if (Values.size() == 1) {
4562 if (EVTBits == 32) {
4563 // Instead of a shuffle like this:
4564 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4565 // Check if it's possible to issue this instead.
4566 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4567 unsigned Idx = CountTrailingZeros_32(NonZeros);
4568 SDValue Item = Op.getOperand(Idx);
4569 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4570 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4571 }
Dan Gohman475871a2008-07-27 21:46:04 +00004572 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004573 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004574
Dan Gohmana3941172007-07-24 22:55:08 +00004575 // A vector full of immediates; various special cases are already
4576 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004577 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004578 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004579
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004580 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004581 if (EVTBits == 64) {
4582 if (NumNonZero == 1) {
4583 // One half is zero or undef.
4584 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004585 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004586 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004587 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4588 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004589 }
Dan Gohman475871a2008-07-27 21:46:04 +00004590 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004591 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592
4593 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004594 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004596 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004597 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 }
4599
Bill Wendling826f36f2007-03-28 00:57:11 +00004600 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004601 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004602 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004603 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004604 }
4605
4606 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004607 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004608 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 if (NumElems == 4 && NumZero > 0) {
4610 for (unsigned i = 0; i < 4; ++i) {
4611 bool isZero = !(NonZeros & (1 << i));
4612 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004613 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 else
Dale Johannesenace16102009-02-03 19:33:06 +00004615 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004616 }
4617
4618 for (unsigned i = 0; i < 2; ++i) {
4619 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4620 default: break;
4621 case 0:
4622 V[i] = V[i*2]; // Must be a zero vector.
4623 break;
4624 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004625 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004626 break;
4627 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004628 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004629 break;
4630 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004632 break;
4633 }
4634 }
4635
Nate Begeman9008ca62009-04-27 18:41:29 +00004636 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004637 bool Reverse = (NonZeros & 0x3) == 2;
4638 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004639 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004640 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4641 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4643 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004644 }
4645
Nate Begemanfdea31a2010-03-24 20:49:50 +00004646 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4647 // Check for a build vector of consecutive loads.
4648 for (unsigned i = 0; i < NumElems; ++i)
4649 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004650
Nate Begemanfdea31a2010-03-24 20:49:50 +00004651 // Check for elements which are consecutive loads.
4652 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4653 if (LD.getNode())
4654 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004655
4656 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004657 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004658 SDValue Result;
4659 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4660 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4661 else
4662 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004663
Chris Lattner24faf612010-08-28 17:59:08 +00004664 for (unsigned i = 1; i < NumElems; ++i) {
4665 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4666 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004668 }
4669 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004670 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004671
Chris Lattner6e80e442010-08-28 17:15:43 +00004672 // Otherwise, expand into a number of unpckl*, start by extending each of
4673 // our (non-undef) elements to the full vector width with the element in the
4674 // bottom slot of the vector (which generates no code for SSE).
4675 for (unsigned i = 0; i < NumElems; ++i) {
4676 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4677 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4678 else
4679 V[i] = DAG.getUNDEF(VT);
4680 }
4681
4682 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4684 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4685 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004686 unsigned EltStride = NumElems >> 1;
4687 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004688 for (unsigned i = 0; i < EltStride; ++i) {
4689 // If V[i+EltStride] is undef and this is the first round of mixing,
4690 // then it is safe to just drop this shuffle: V[i] is already in the
4691 // right place, the one element (since it's the first round) being
4692 // inserted as undef can be dropped. This isn't safe for successive
4693 // rounds because they will permute elements within both vectors.
4694 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4695 EltStride == NumElems/2)
4696 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004697
Chris Lattner6e80e442010-08-28 17:15:43 +00004698 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004699 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004700 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004701 }
4702 return V[0];
4703 }
Dan Gohman475871a2008-07-27 21:46:04 +00004704 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004705}
4706
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004707SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004708X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004709 // We support concatenate two MMX registers and place them in a MMX
4710 // register. This is better than doing a stack convert.
4711 DebugLoc dl = Op.getDebugLoc();
4712 EVT ResVT = Op.getValueType();
4713 assert(Op.getNumOperands() == 2);
4714 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4715 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4716 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004717 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004718 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4719 InVec = Op.getOperand(1);
4720 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4721 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004722 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004723 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4724 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4725 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004726 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004727 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4728 Mask[0] = 0; Mask[1] = 2;
4729 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4730 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004731 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004732}
4733
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734// v8i16 shuffles - Prefer shuffles in the following order:
4735// 1. [all] pshuflw, pshufhw, optional move
4736// 2. [ssse3] 1 x pshufb
4737// 3. [ssse3] 2 x pshufb + 1 x por
4738// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004739SDValue
4740X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4741 SelectionDAG &DAG) const {
4742 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004743 SDValue V1 = SVOp->getOperand(0);
4744 SDValue V2 = SVOp->getOperand(1);
4745 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004746 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004747
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 // Determine if more than 1 of the words in each of the low and high quadwords
4749 // of the result come from the same quadword of one of the two inputs. Undef
4750 // mask values count as coming from any quadword, for better codegen.
4751 SmallVector<unsigned, 4> LoQuad(4);
4752 SmallVector<unsigned, 4> HiQuad(4);
4753 BitVector InputQuads(4);
4754 for (unsigned i = 0; i < 8; ++i) {
4755 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004756 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004757 MaskVals.push_back(EltIdx);
4758 if (EltIdx < 0) {
4759 ++Quad[0];
4760 ++Quad[1];
4761 ++Quad[2];
4762 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004763 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004764 }
4765 ++Quad[EltIdx / 4];
4766 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004767 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004768
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004770 unsigned MaxQuad = 1;
4771 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 if (LoQuad[i] > MaxQuad) {
4773 BestLoQuad = i;
4774 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004775 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004776 }
4777
Nate Begemanb9a47b82009-02-23 08:49:38 +00004778 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004779 MaxQuad = 1;
4780 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 if (HiQuad[i] > MaxQuad) {
4782 BestHiQuad = i;
4783 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004784 }
4785 }
4786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004788 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004789 // single pshufb instruction is necessary. If There are more than 2 input
4790 // quads, disable the next transformation since it does not help SSSE3.
4791 bool V1Used = InputQuads[0] || InputQuads[1];
4792 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004793 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 if (InputQuads.count() == 2 && V1Used && V2Used) {
4795 BestLoQuad = InputQuads.find_first();
4796 BestHiQuad = InputQuads.find_next(BestLoQuad);
4797 }
4798 if (InputQuads.count() > 2) {
4799 BestLoQuad = -1;
4800 BestHiQuad = -1;
4801 }
4802 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004803
Nate Begemanb9a47b82009-02-23 08:49:38 +00004804 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4805 // the shuffle mask. If a quad is scored as -1, that means that it contains
4806 // words from all 4 input quadwords.
4807 SDValue NewV;
4808 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004809 SmallVector<int, 8> MaskV;
4810 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4811 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004812 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004813 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4814 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4815 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004816
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4818 // source words for the shuffle, to aid later transformations.
4819 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004820 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004821 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004823 if (idx != (int)i)
4824 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004826 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 AllWordsInNewV = false;
4828 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004829 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004830
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4832 if (AllWordsInNewV) {
4833 for (int i = 0; i != 8; ++i) {
4834 int idx = MaskVals[i];
4835 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004836 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004837 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 if ((idx != i) && idx < 4)
4839 pshufhw = false;
4840 if ((idx != i) && idx > 3)
4841 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004842 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004843 V1 = NewV;
4844 V2Used = false;
4845 BestLoQuad = 0;
4846 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004847 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004848
Nate Begemanb9a47b82009-02-23 08:49:38 +00004849 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4850 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004851 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004852 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4853 unsigned TargetMask = 0;
4854 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004855 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004856 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4857 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4858 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004859 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004860 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004861 }
Eric Christopherfd179292009-08-27 18:07:15 +00004862
Nate Begemanb9a47b82009-02-23 08:49:38 +00004863 // If we have SSSE3, and all words of the result are from 1 input vector,
4864 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4865 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004866 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004867 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004868
Nate Begemanb9a47b82009-02-23 08:49:38 +00004869 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004870 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004871 // mask, and elements that come from V1 in the V2 mask, so that the two
4872 // results can be OR'd together.
4873 bool TwoInputs = V1Used && V2Used;
4874 for (unsigned i = 0; i != 8; ++i) {
4875 int EltIdx = MaskVals[i] * 2;
4876 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4878 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 continue;
4880 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4882 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004883 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004884 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004885 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004886 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004889 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004890
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 // Calculate the shuffle mask for the second input, shuffle it, and
4892 // OR it with the first shuffled input.
4893 pshufbMask.clear();
4894 for (unsigned i = 0; i != 8; ++i) {
4895 int EltIdx = MaskVals[i] * 2;
4896 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4898 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004899 continue;
4900 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4902 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004903 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004904 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004905 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004906 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 MVT::v16i8, &pshufbMask[0], 16));
4908 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004909 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 }
4911
4912 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4913 // and update MaskVals with new element order.
4914 BitVector InOrder(8);
4915 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004916 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 for (int i = 0; i != 4; ++i) {
4918 int idx = MaskVals[i];
4919 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004920 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004921 InOrder.set(i);
4922 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004923 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 InOrder.set(i);
4925 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004926 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004927 }
4928 }
4929 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004930 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004931 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004932 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004933
4934 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4935 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4936 NewV.getOperand(0),
4937 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4938 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004939 }
Eric Christopherfd179292009-08-27 18:07:15 +00004940
Nate Begemanb9a47b82009-02-23 08:49:38 +00004941 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4942 // and update MaskVals with the new element order.
4943 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004944 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004945 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004946 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004947 for (unsigned i = 4; i != 8; ++i) {
4948 int idx = MaskVals[i];
4949 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004950 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 InOrder.set(i);
4952 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004954 InOrder.set(i);
4955 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004956 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004957 }
4958 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004959 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004960 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004961
4962 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4963 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4964 NewV.getOperand(0),
4965 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4966 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004967 }
Eric Christopherfd179292009-08-27 18:07:15 +00004968
Nate Begemanb9a47b82009-02-23 08:49:38 +00004969 // In case BestHi & BestLo were both -1, which means each quadword has a word
4970 // from each of the four input quadwords, calculate the InOrder bitvector now
4971 // before falling through to the insert/extract cleanup.
4972 if (BestLoQuad == -1 && BestHiQuad == -1) {
4973 NewV = V1;
4974 for (int i = 0; i != 8; ++i)
4975 if (MaskVals[i] < 0 || MaskVals[i] == i)
4976 InOrder.set(i);
4977 }
Eric Christopherfd179292009-08-27 18:07:15 +00004978
Nate Begemanb9a47b82009-02-23 08:49:38 +00004979 // The other elements are put in the right place using pextrw and pinsrw.
4980 for (unsigned i = 0; i != 8; ++i) {
4981 if (InOrder[i])
4982 continue;
4983 int EltIdx = MaskVals[i];
4984 if (EltIdx < 0)
4985 continue;
4986 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004987 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004988 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004989 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004991 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 DAG.getIntPtrConstant(i));
4993 }
4994 return NewV;
4995}
4996
4997// v16i8 shuffles - Prefer shuffles in the following order:
4998// 1. [ssse3] 1 x pshufb
4999// 2. [ssse3] 2 x pshufb + 1 x por
5000// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5001static
Nate Begeman9008ca62009-04-27 18:41:29 +00005002SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005003 SelectionDAG &DAG,
5004 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005005 SDValue V1 = SVOp->getOperand(0);
5006 SDValue V2 = SVOp->getOperand(1);
5007 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005008 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00005009 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00005010
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005012 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005013 // present, fall back to case 3.
5014 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5015 bool V1Only = true;
5016 bool V2Only = true;
5017 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005018 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00005019 if (EltIdx < 0)
5020 continue;
5021 if (EltIdx < 16)
5022 V2Only = false;
5023 else
5024 V1Only = false;
5025 }
Eric Christopherfd179292009-08-27 18:07:15 +00005026
Nate Begemanb9a47b82009-02-23 08:49:38 +00005027 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
5028 if (TLI.getSubtarget()->hasSSSE3()) {
5029 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005030
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005032 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 //
5034 // Otherwise, we have elements from both input vectors, and must zero out
5035 // elements that come from V2 in the first mask, and V1 in the second mask
5036 // so that we can OR them together.
5037 bool TwoInputs = !(V1Only || V2Only);
5038 for (unsigned i = 0; i != 16; ++i) {
5039 int EltIdx = MaskVals[i];
5040 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005041 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005042 continue;
5043 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005044 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005045 }
5046 // If all the elements are from V2, assign it to V1 and return after
5047 // building the first pshufb.
5048 if (V2Only)
5049 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005050 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005051 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005053 if (!TwoInputs)
5054 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005055
Nate Begemanb9a47b82009-02-23 08:49:38 +00005056 // Calculate the shuffle mask for the second input, shuffle it, and
5057 // OR it with the first shuffled input.
5058 pshufbMask.clear();
5059 for (unsigned i = 0; i != 16; ++i) {
5060 int EltIdx = MaskVals[i];
5061 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005062 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 continue;
5064 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005066 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005068 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005069 MVT::v16i8, &pshufbMask[0], 16));
5070 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005071 }
Eric Christopherfd179292009-08-27 18:07:15 +00005072
Nate Begemanb9a47b82009-02-23 08:49:38 +00005073 // No SSSE3 - Calculate in place words and then fix all out of place words
5074 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5075 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005076 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5077 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005078 SDValue NewV = V2Only ? V2 : V1;
5079 for (int i = 0; i != 8; ++i) {
5080 int Elt0 = MaskVals[i*2];
5081 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005082
Nate Begemanb9a47b82009-02-23 08:49:38 +00005083 // This word of the result is all undef, skip it.
5084 if (Elt0 < 0 && Elt1 < 0)
5085 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005086
Nate Begemanb9a47b82009-02-23 08:49:38 +00005087 // This word of the result is already in the correct place, skip it.
5088 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5089 continue;
5090 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5091 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005092
Nate Begemanb9a47b82009-02-23 08:49:38 +00005093 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5094 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5095 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005096
5097 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5098 // using a single extract together, load it and store it.
5099 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005100 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005101 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005102 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005103 DAG.getIntPtrConstant(i));
5104 continue;
5105 }
5106
Nate Begemanb9a47b82009-02-23 08:49:38 +00005107 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005108 // source byte is not also odd, shift the extracted word left 8 bits
5109 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005110 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005111 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005112 DAG.getIntPtrConstant(Elt1 / 2));
5113 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005114 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005115 DAG.getConstant(8,
5116 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005117 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005118 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5119 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005120 }
5121 // If Elt0 is defined, extract it from the appropriate source. If the
5122 // source byte is not also even, shift the extracted word right 8 bits. If
5123 // Elt1 was also defined, OR the extracted values together before
5124 // inserting them in the result.
5125 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005126 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005127 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5128 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005129 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005130 DAG.getConstant(8,
5131 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005132 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5134 DAG.getConstant(0x00FF, MVT::i16));
5135 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005136 : InsElt0;
5137 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005138 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005139 DAG.getIntPtrConstant(i));
5140 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005141 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005142}
5143
Evan Cheng7a831ce2007-12-15 03:00:47 +00005144/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005145/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005146/// done when every pair / quad of shuffle mask elements point to elements in
5147/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005148/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005149static
Nate Begeman9008ca62009-04-27 18:41:29 +00005150SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005151 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005152 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005153 SDValue V1 = SVOp->getOperand(0);
5154 SDValue V2 = SVOp->getOperand(1);
5155 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005156 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005157 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005158 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005159 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005160 case MVT::v4f32: NewVT = MVT::v2f64; break;
5161 case MVT::v4i32: NewVT = MVT::v2i64; break;
5162 case MVT::v8i16: NewVT = MVT::v4i32; break;
5163 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005164 }
5165
Nate Begeman9008ca62009-04-27 18:41:29 +00005166 int Scale = NumElems / NewWidth;
5167 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005168 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005169 int StartIdx = -1;
5170 for (int j = 0; j < Scale; ++j) {
5171 int EltIdx = SVOp->getMaskElt(i+j);
5172 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005173 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005174 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005175 StartIdx = EltIdx - (EltIdx % Scale);
5176 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005177 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005178 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005179 if (StartIdx == -1)
5180 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005181 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005182 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005183 }
5184
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005185 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5186 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005187 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005188}
5189
Evan Chengd880b972008-05-09 21:53:03 +00005190/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005191///
Owen Andersone50ed302009-08-10 22:56:29 +00005192static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005193 SDValue SrcOp, SelectionDAG &DAG,
5194 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005195 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005196 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005197 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005198 LD = dyn_cast<LoadSDNode>(SrcOp);
5199 if (!LD) {
5200 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5201 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005202 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005203 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005204 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005205 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005206 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005207 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005208 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005209 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005210 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5211 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5212 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005213 SrcOp.getOperand(0)
5214 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005215 }
5216 }
5217 }
5218
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005219 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005220 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005221 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005222 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005223}
5224
Evan Chengace3c172008-07-22 21:13:36 +00005225/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5226/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005227static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005228LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5229 SDValue V1 = SVOp->getOperand(0);
5230 SDValue V2 = SVOp->getOperand(1);
5231 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005232 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005233
Evan Chengace3c172008-07-22 21:13:36 +00005234 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005235 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 SmallVector<int, 8> Mask1(4U, -1);
5237 SmallVector<int, 8> PermMask;
5238 SVOp->getMask(PermMask);
5239
Evan Chengace3c172008-07-22 21:13:36 +00005240 unsigned NumHi = 0;
5241 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005242 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005243 int Idx = PermMask[i];
5244 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005245 Locs[i] = std::make_pair(-1, -1);
5246 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005247 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5248 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005249 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005251 NumLo++;
5252 } else {
5253 Locs[i] = std::make_pair(1, NumHi);
5254 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005255 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005256 NumHi++;
5257 }
5258 }
5259 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005260
Evan Chengace3c172008-07-22 21:13:36 +00005261 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005262 // If no more than two elements come from either vector. This can be
5263 // implemented with two shuffles. First shuffle gather the elements.
5264 // The second shuffle, which takes the first shuffle as both of its
5265 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005266 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005267
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005269
Evan Chengace3c172008-07-22 21:13:36 +00005270 for (unsigned i = 0; i != 4; ++i) {
5271 if (Locs[i].first == -1)
5272 continue;
5273 else {
5274 unsigned Idx = (i < 2) ? 0 : 4;
5275 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005276 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005277 }
5278 }
5279
Nate Begeman9008ca62009-04-27 18:41:29 +00005280 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005281 } else if (NumLo == 3 || NumHi == 3) {
5282 // Otherwise, we must have three elements from one vector, call it X, and
5283 // one element from the other, call it Y. First, use a shufps to build an
5284 // intermediate vector with the one element from Y and the element from X
5285 // that will be in the same half in the final destination (the indexes don't
5286 // matter). Then, use a shufps to build the final vector, taking the half
5287 // containing the element from Y from the intermediate, and the other half
5288 // from X.
5289 if (NumHi == 3) {
5290 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005291 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005292 std::swap(V1, V2);
5293 }
5294
5295 // Find the element from V2.
5296 unsigned HiIndex;
5297 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 int Val = PermMask[HiIndex];
5299 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005300 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005301 if (Val >= 4)
5302 break;
5303 }
5304
Nate Begeman9008ca62009-04-27 18:41:29 +00005305 Mask1[0] = PermMask[HiIndex];
5306 Mask1[1] = -1;
5307 Mask1[2] = PermMask[HiIndex^1];
5308 Mask1[3] = -1;
5309 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005310
5311 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005312 Mask1[0] = PermMask[0];
5313 Mask1[1] = PermMask[1];
5314 Mask1[2] = HiIndex & 1 ? 6 : 4;
5315 Mask1[3] = HiIndex & 1 ? 4 : 6;
5316 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005317 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005318 Mask1[0] = HiIndex & 1 ? 2 : 0;
5319 Mask1[1] = HiIndex & 1 ? 0 : 2;
5320 Mask1[2] = PermMask[2];
5321 Mask1[3] = PermMask[3];
5322 if (Mask1[2] >= 0)
5323 Mask1[2] += 4;
5324 if (Mask1[3] >= 0)
5325 Mask1[3] += 4;
5326 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005327 }
Evan Chengace3c172008-07-22 21:13:36 +00005328 }
5329
5330 // Break it into (shuffle shuffle_hi, shuffle_lo).
5331 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005332 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 SmallVector<int,8> LoMask(4U, -1);
5334 SmallVector<int,8> HiMask(4U, -1);
5335
5336 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005337 unsigned MaskIdx = 0;
5338 unsigned LoIdx = 0;
5339 unsigned HiIdx = 2;
5340 for (unsigned i = 0; i != 4; ++i) {
5341 if (i == 2) {
5342 MaskPtr = &HiMask;
5343 MaskIdx = 1;
5344 LoIdx = 0;
5345 HiIdx = 2;
5346 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005347 int Idx = PermMask[i];
5348 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005349 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005350 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005351 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005352 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005353 LoIdx++;
5354 } else {
5355 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005356 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005357 HiIdx++;
5358 }
5359 }
5360
Nate Begeman9008ca62009-04-27 18:41:29 +00005361 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5362 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5363 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005364 for (unsigned i = 0; i != 4; ++i) {
5365 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005366 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005367 } else {
5368 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005369 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005370 }
5371 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005372 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005373}
5374
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005375static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005376 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005377 V = V.getOperand(0);
5378 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5379 V = V.getOperand(0);
5380 if (MayFoldLoad(V))
5381 return true;
5382 return false;
5383}
5384
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005385// FIXME: the version above should always be used. Since there's
5386// a bug where several vector shuffles can't be folded because the
5387// DAG is not updated during lowering and a node claims to have two
5388// uses while it only has one, use this version, and let isel match
5389// another instruction if the load really happens to have more than
5390// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005391// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005392static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005393 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005394 V = V.getOperand(0);
5395 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5396 V = V.getOperand(0);
5397 if (ISD::isNormalLoad(V.getNode()))
5398 return true;
5399 return false;
5400}
5401
5402/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5403/// a vector extract, and if both can be later optimized into a single load.
5404/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5405/// here because otherwise a target specific shuffle node is going to be
5406/// emitted for this shuffle, and the optimization not done.
5407/// FIXME: This is probably not the best approach, but fix the problem
5408/// until the right path is decided.
5409static
5410bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5411 const TargetLowering &TLI) {
5412 EVT VT = V.getValueType();
5413 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5414
5415 // Be sure that the vector shuffle is present in a pattern like this:
5416 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5417 if (!V.hasOneUse())
5418 return false;
5419
5420 SDNode *N = *V.getNode()->use_begin();
5421 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5422 return false;
5423
5424 SDValue EltNo = N->getOperand(1);
5425 if (!isa<ConstantSDNode>(EltNo))
5426 return false;
5427
5428 // If the bit convert changed the number of elements, it is unsafe
5429 // to examine the mask.
5430 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005431 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005432 EVT SrcVT = V.getOperand(0).getValueType();
5433 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5434 return false;
5435 V = V.getOperand(0);
5436 HasShuffleIntoBitcast = true;
5437 }
5438
5439 // Select the input vector, guarding against out of range extract vector.
5440 unsigned NumElems = VT.getVectorNumElements();
5441 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5442 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5443 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5444
5445 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005446 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005447 V = V.getOperand(0);
5448
5449 if (ISD::isNormalLoad(V.getNode())) {
5450 // Is the original load suitable?
5451 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5452
5453 // FIXME: avoid the multi-use bug that is preventing lots of
5454 // of foldings to be detected, this is still wrong of course, but
5455 // give the temporary desired behavior, and if it happens that
5456 // the load has real more uses, during isel it will not fold, and
5457 // will generate poor code.
5458 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5459 return false;
5460
5461 if (!HasShuffleIntoBitcast)
5462 return true;
5463
5464 // If there's a bitcast before the shuffle, check if the load type and
5465 // alignment is valid.
5466 unsigned Align = LN0->getAlignment();
5467 unsigned NewAlign =
5468 TLI.getTargetData()->getABITypeAlignment(
5469 VT.getTypeForEVT(*DAG.getContext()));
5470
5471 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5472 return false;
5473 }
5474
5475 return true;
5476}
5477
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005478static
Evan Cheng835580f2010-10-07 20:50:20 +00005479SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5480 EVT VT = Op.getValueType();
5481
5482 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005483 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5484 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005485 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5486 V1, DAG));
5487}
5488
5489static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005490SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5491 bool HasSSE2) {
5492 SDValue V1 = Op.getOperand(0);
5493 SDValue V2 = Op.getOperand(1);
5494 EVT VT = Op.getValueType();
5495
5496 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5497
5498 if (HasSSE2 && VT == MVT::v2f64)
5499 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5500
5501 // v4f32 or v4i32
5502 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5503}
5504
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005505static
5506SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5507 SDValue V1 = Op.getOperand(0);
5508 SDValue V2 = Op.getOperand(1);
5509 EVT VT = Op.getValueType();
5510
5511 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5512 "unsupported shuffle type");
5513
5514 if (V2.getOpcode() == ISD::UNDEF)
5515 V2 = V1;
5516
5517 // v4i32 or v4f32
5518 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5519}
5520
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005521static
5522SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5523 SDValue V1 = Op.getOperand(0);
5524 SDValue V2 = Op.getOperand(1);
5525 EVT VT = Op.getValueType();
5526 unsigned NumElems = VT.getVectorNumElements();
5527
5528 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5529 // operand of these instructions is only memory, so check if there's a
5530 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5531 // same masks.
5532 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005533
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005534 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005535 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005536 CanFoldLoad = true;
5537
5538 // When V1 is a load, it can be folded later into a store in isel, example:
5539 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5540 // turns into:
5541 // (MOVLPSmr addr:$src1, VR128:$src2)
5542 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005543 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005544 CanFoldLoad = true;
5545
Eric Christopher893a8822011-02-20 05:04:42 +00005546 // Both of them can't be memory operations though.
5547 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5548 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005549
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005550 if (CanFoldLoad) {
5551 if (HasSSE2 && NumElems == 2)
5552 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5553
5554 if (NumElems == 4)
5555 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5556 }
5557
5558 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5559 // movl and movlp will both match v2i64, but v2i64 is never matched by
5560 // movl earlier because we make it strict to avoid messing with the movlp load
5561 // folding logic (see the code above getMOVLP call). Match it here then,
5562 // this is horrible, but will stay like this until we move all shuffle
5563 // matching to x86 specific nodes. Note that for the 1st condition all
5564 // types are matched with movsd.
5565 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5566 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5567 else if (HasSSE2)
5568 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5569
5570
5571 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5572
5573 // Invert the operand order and use SHUFPS to match it.
5574 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5575 X86::getShuffleSHUFImmediate(SVOp), DAG);
5576}
5577
David Greenec4db4e52011-02-28 19:06:56 +00005578static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005579 switch(VT.getSimpleVT().SimpleTy) {
5580 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5581 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005582 case MVT::v4f32:
5583 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5584 case MVT::v2f64:
5585 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5586 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5587 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005588 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5589 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5590 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005591 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005592 }
5593 return 0;
5594}
5595
5596static inline unsigned getUNPCKHOpcode(EVT VT) {
5597 switch(VT.getSimpleVT().SimpleTy) {
5598 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5599 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5600 case MVT::v4f32: return X86ISD::UNPCKHPS;
5601 case MVT::v2f64: return X86ISD::UNPCKHPD;
5602 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5603 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5604 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005605 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005606 }
5607 return 0;
5608}
5609
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005610static
5611SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005612 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005613 const X86Subtarget *Subtarget) {
5614 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5615 EVT VT = Op.getValueType();
5616 DebugLoc dl = Op.getDebugLoc();
5617 SDValue V1 = Op.getOperand(0);
5618 SDValue V2 = Op.getOperand(1);
5619
5620 if (isZeroShuffle(SVOp))
5621 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5622
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005623 // Handle splat operations
5624 if (SVOp->isSplat()) {
5625 // Special case, this is the only place now where it's
5626 // allowed to return a vector_shuffle operation without
5627 // using a target specific node, because *hopefully* it
5628 // will be optimized away by the dag combiner.
5629 if (VT.getVectorNumElements() <= 4 &&
5630 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5631 return Op;
5632
5633 // Handle splats by matching through known masks
5634 if (VT.getVectorNumElements() <= 4)
5635 return SDValue();
5636
Evan Cheng835580f2010-10-07 20:50:20 +00005637 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005638 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005639 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005640
5641 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5642 // do it!
5643 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5644 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5645 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005646 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005647 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5648 // FIXME: Figure out a cleaner way to do this.
5649 // Try to make use of movq to zero out the top part.
5650 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5651 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5652 if (NewOp.getNode()) {
5653 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5654 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5655 DAG, Subtarget, dl);
5656 }
5657 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5658 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5659 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5660 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5661 DAG, Subtarget, dl);
5662 }
5663 }
5664 return SDValue();
5665}
5666
Dan Gohman475871a2008-07-27 21:46:04 +00005667SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005668X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005670 SDValue V1 = Op.getOperand(0);
5671 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005672 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005673 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005674 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005675 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005676 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5677 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005678 bool V1IsSplat = false;
5679 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005680 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005681 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005682 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005683 MachineFunction &MF = DAG.getMachineFunction();
5684 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005685
Dale Johannesen0488fb62010-09-30 23:57:10 +00005686 // Shuffle operations on MMX not supported.
5687 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005688 return Op;
5689
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005690 // Vector shuffle lowering takes 3 steps:
5691 //
5692 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5693 // narrowing and commutation of operands should be handled.
5694 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5695 // shuffle nodes.
5696 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5697 // so the shuffle can be broken into other shuffles and the legalizer can
5698 // try the lowering again.
5699 //
5700 // The general ideia is that no vector_shuffle operation should be left to
5701 // be matched during isel, all of them must be converted to a target specific
5702 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005703
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005704 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5705 // narrowing and commutation of operands should be handled. The actual code
5706 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005707 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005708 if (NewOp.getNode())
5709 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005710
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005711 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5712 // unpckh_undef). Only use pshufd if speed is more important than size.
5713 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5714 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005715 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005716 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5717 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5718 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005719
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005720 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005721 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005722 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005723
Dale Johannesen0488fb62010-09-30 23:57:10 +00005724 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005725 return getMOVHighToLow(Op, dl, DAG);
5726
5727 // Use to match splats
5728 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5729 (VT == MVT::v2f64 || VT == MVT::v2i64))
5730 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5731
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005732 if (X86::isPSHUFDMask(SVOp)) {
5733 // The actual implementation will match the mask in the if above and then
5734 // during isel it can match several different instructions, not only pshufd
5735 // as its name says, sad but true, emulate the behavior for now...
5736 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5737 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5738
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005739 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5740
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005741 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005742 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5743
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005744 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005745 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5746 TargetMask, DAG);
5747
5748 if (VT == MVT::v4f32)
5749 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5750 TargetMask, DAG);
5751 }
Eric Christopherfd179292009-08-27 18:07:15 +00005752
Evan Chengf26ffe92008-05-29 08:22:04 +00005753 // Check if this can be converted into a logical shift.
5754 bool isLeft = false;
5755 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005756 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005757 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005758 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005759 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005760 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005761 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005762 EVT EltVT = VT.getVectorElementType();
5763 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005764 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005765 }
Eric Christopherfd179292009-08-27 18:07:15 +00005766
Nate Begeman9008ca62009-04-27 18:41:29 +00005767 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005768 if (V1IsUndef)
5769 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005770 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005771 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005772 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005773 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005774 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5775
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005776 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005777 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5778 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005779 }
Eric Christopherfd179292009-08-27 18:07:15 +00005780
Nate Begeman9008ca62009-04-27 18:41:29 +00005781 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005782 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5783 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005784
Dale Johannesen0488fb62010-09-30 23:57:10 +00005785 if (X86::isMOVHLPSMask(SVOp))
5786 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005787
Dale Johannesen0488fb62010-09-30 23:57:10 +00005788 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5789 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005790
Dale Johannesen0488fb62010-09-30 23:57:10 +00005791 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5792 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005793
Dale Johannesen0488fb62010-09-30 23:57:10 +00005794 if (X86::isMOVLPMask(SVOp))
5795 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005796
Nate Begeman9008ca62009-04-27 18:41:29 +00005797 if (ShouldXformToMOVHLPS(SVOp) ||
5798 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5799 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005800
Evan Chengf26ffe92008-05-29 08:22:04 +00005801 if (isShift) {
5802 // No better options. Use a vshl / vsrl.
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
Evan Cheng9eca5e82006-10-25 21:49:50 +00005808 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005809 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5810 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005811 V1IsSplat = isSplatVector(V1.getNode());
5812 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005813
Chris Lattner8a594482007-11-25 00:24:49 +00005814 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005815 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005816 Op = CommuteVectorShuffle(SVOp, DAG);
5817 SVOp = cast<ShuffleVectorSDNode>(Op);
5818 V1 = SVOp->getOperand(0);
5819 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005820 std::swap(V1IsSplat, V2IsSplat);
5821 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005822 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005823 }
5824
Nate Begeman9008ca62009-04-27 18:41:29 +00005825 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5826 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005827 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005828 return V1;
5829 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5830 // the instruction selector will not match, so get a canonical MOVL with
5831 // swapped operands to undo the commute.
5832 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005833 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005834
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005835 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005836 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5837 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005838
5839 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005840 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005841
Evan Cheng9bbbb982006-10-25 20:48:19 +00005842 if (V2IsSplat) {
5843 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005844 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005845 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005846 SDValue NewMask = NormalizeMask(SVOp, DAG);
5847 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5848 if (NSVOp != SVOp) {
5849 if (X86::isUNPCKLMask(NSVOp, true)) {
5850 return NewMask;
5851 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5852 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005853 }
5854 }
5855 }
5856
Evan Cheng9eca5e82006-10-25 21:49:50 +00005857 if (Commuted) {
5858 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005859 // FIXME: this seems wrong.
5860 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5861 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005862
5863 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005864 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5865 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005866
5867 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005868 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005869 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005870
Nate Begeman9008ca62009-04-27 18:41:29 +00005871 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005872 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005873 return CommuteVectorShuffle(SVOp, DAG);
5874
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005875 // The checks below are all present in isShuffleMaskLegal, but they are
5876 // inlined here right now to enable us to directly emit target specific
5877 // nodes, and remove one by one until they don't return Op anymore.
5878 SmallVector<int, 16> M;
5879 SVOp->getMask(M);
5880
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005881 if (isPALIGNRMask(M, VT, HasSSSE3))
5882 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5883 X86::getShufflePALIGNRImmediate(SVOp),
5884 DAG);
5885
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005886 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5887 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005888 if (VT == MVT::v2f64) {
5889 X86ISD::NodeType Opcode =
5890 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5891 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5892 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005893 if (VT == MVT::v2i64)
5894 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5895 }
5896
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005897 if (isPSHUFHWMask(M, VT))
5898 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5899 X86::getShufflePSHUFHWImmediate(SVOp),
5900 DAG);
5901
5902 if (isPSHUFLWMask(M, VT))
5903 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5904 X86::getShufflePSHUFLWImmediate(SVOp),
5905 DAG);
5906
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005907 if (isSHUFPMask(M, VT)) {
5908 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5909 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5910 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5911 TargetMask, DAG);
5912 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5913 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5914 TargetMask, DAG);
5915 }
5916
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005917 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5918 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005919 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5920 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005921 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5922 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5923 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5924
Evan Cheng14b32e12007-12-11 01:46:18 +00005925 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005926 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005927 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005928 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005929 return NewOp;
5930 }
5931
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005933 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 if (NewOp.getNode())
5935 return NewOp;
5936 }
Eric Christopherfd179292009-08-27 18:07:15 +00005937
Dale Johannesen0488fb62010-09-30 23:57:10 +00005938 // Handle all 4 wide cases with a number of shuffles.
5939 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005940 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005941
Dan Gohman475871a2008-07-27 21:46:04 +00005942 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005943}
5944
Dan Gohman475871a2008-07-27 21:46:04 +00005945SDValue
5946X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005947 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005948 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005949 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005950 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005952 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005954 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005955 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005956 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005957 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5958 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5959 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005960 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5961 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005962 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005963 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005964 Op.getOperand(0)),
5965 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005966 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005967 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005968 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005969 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005970 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005971 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005972 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5973 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005974 // result has a single use which is a store or a bitcast to i32. And in
5975 // the case of a store, it's not worth it if the index is a constant 0,
5976 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005977 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005978 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005979 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005980 if ((User->getOpcode() != ISD::STORE ||
5981 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5982 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005983 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005985 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005987 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005988 Op.getOperand(0)),
5989 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005990 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005991 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005992 // ExtractPS works with constant index.
5993 if (isa<ConstantSDNode>(Op.getOperand(1)))
5994 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005995 }
Dan Gohman475871a2008-07-27 21:46:04 +00005996 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005997}
5998
5999
Dan Gohman475871a2008-07-27 21:46:04 +00006000SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006001X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6002 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006003 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006004 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006005
David Greene74a579d2011-02-10 16:57:36 +00006006 SDValue Vec = Op.getOperand(0);
6007 EVT VecVT = Vec.getValueType();
6008
6009 // If this is a 256-bit vector result, first extract the 128-bit
6010 // vector and then extract from the 128-bit vector.
6011 if (VecVT.getSizeInBits() > 128) {
6012 DebugLoc dl = Op.getNode()->getDebugLoc();
6013 unsigned NumElems = VecVT.getVectorNumElements();
6014 SDValue Idx = Op.getOperand(1);
6015
6016 if (!isa<ConstantSDNode>(Idx))
6017 return SDValue();
6018
6019 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
6020 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6021
6022 // Get the 128-bit vector.
6023 bool Upper = IdxVal >= ExtractNumElems;
6024 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
6025
6026 // Extract from it.
6027 SDValue ScaledIdx = Idx;
6028 if (Upper)
6029 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
6030 DAG.getConstant(ExtractNumElems,
6031 Idx.getValueType()));
6032 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6033 ScaledIdx);
6034 }
6035
6036 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6037
Evan Cheng62a3f152008-03-24 21:52:23 +00006038 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006039 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006040 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006041 return Res;
6042 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006043
Owen Andersone50ed302009-08-10 22:56:29 +00006044 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006045 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006046 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006047 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006048 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006049 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006050 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006051 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6052 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006053 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006055 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006056 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006057 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006058 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006059 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006060 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006061 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006062 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006063 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006064 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006065 if (Idx == 0)
6066 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006067
Evan Cheng0db9fe62006-04-25 20:13:52 +00006068 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006069 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006070 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006071 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006073 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006074 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006075 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006076 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6077 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6078 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006079 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006080 if (Idx == 0)
6081 return Op;
6082
6083 // UNPCKHPD the element to the lowest double word, then movsd.
6084 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6085 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006086 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006087 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006088 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006089 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006090 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006091 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006092 }
6093
Dan Gohman475871a2008-07-27 21:46:04 +00006094 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006095}
6096
Dan Gohman475871a2008-07-27 21:46:04 +00006097SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006098X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6099 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006100 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006101 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006102 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006103
Dan Gohman475871a2008-07-27 21:46:04 +00006104 SDValue N0 = Op.getOperand(0);
6105 SDValue N1 = Op.getOperand(1);
6106 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006107
Dan Gohman8a55ce42009-09-23 21:02:20 +00006108 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006109 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006110 unsigned Opc;
6111 if (VT == MVT::v8i16)
6112 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006113 else if (VT == MVT::v16i8)
6114 Opc = X86ISD::PINSRB;
6115 else
6116 Opc = X86ISD::PINSRB;
6117
Nate Begeman14d12ca2008-02-11 04:19:36 +00006118 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6119 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006120 if (N1.getValueType() != MVT::i32)
6121 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6122 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006123 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006124 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006125 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006126 // Bits [7:6] of the constant are the source select. This will always be
6127 // zero here. The DAG Combiner may combine an extract_elt index into these
6128 // bits. For example (insert (extract, 3), 2) could be matched by putting
6129 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006130 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006131 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006132 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006133 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006134 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006135 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006136 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006137 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006138 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006139 // PINSR* works with constant index.
6140 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006141 }
Dan Gohman475871a2008-07-27 21:46:04 +00006142 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006143}
6144
Dan Gohman475871a2008-07-27 21:46:04 +00006145SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006146X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006147 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006148 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006149
David Greene6b381262011-02-09 15:32:06 +00006150 DebugLoc dl = Op.getDebugLoc();
6151 SDValue N0 = Op.getOperand(0);
6152 SDValue N1 = Op.getOperand(1);
6153 SDValue N2 = Op.getOperand(2);
6154
6155 // If this is a 256-bit vector result, first insert into a 128-bit
6156 // vector and then insert into the 256-bit vector.
6157 if (VT.getSizeInBits() > 128) {
6158 if (!isa<ConstantSDNode>(N2))
6159 return SDValue();
6160
6161 // Get the 128-bit vector.
6162 unsigned NumElems = VT.getVectorNumElements();
6163 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6164 bool Upper = IdxVal >= NumElems / 2;
6165
6166 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6167
6168 // Insert into it.
6169 SDValue ScaledN2 = N2;
6170 if (Upper)
6171 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006172 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006173 (VT.getSizeInBits() / 128),
6174 N2.getValueType()));
6175 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6176 N1, ScaledN2);
6177
6178 // Insert the 128-bit vector
6179 // FIXME: Why UNDEF?
6180 return Insert128BitVector(N0, Op, N2, DAG, dl);
6181 }
6182
Nate Begeman14d12ca2008-02-11 04:19:36 +00006183 if (Subtarget->hasSSE41())
6184 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6185
Dan Gohman8a55ce42009-09-23 21:02:20 +00006186 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006187 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006188
Dan Gohman8a55ce42009-09-23 21:02:20 +00006189 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006190 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6191 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006192 if (N1.getValueType() != MVT::i32)
6193 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6194 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006195 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006196 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006197 }
Dan Gohman475871a2008-07-27 21:46:04 +00006198 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006199}
6200
Dan Gohman475871a2008-07-27 21:46:04 +00006201SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006202X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006203 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006204 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006205 EVT OpVT = Op.getValueType();
6206
6207 // If this is a 256-bit vector result, first insert into a 128-bit
6208 // vector and then insert into the 256-bit vector.
6209 if (OpVT.getSizeInBits() > 128) {
6210 // Insert into a 128-bit vector.
6211 EVT VT128 = EVT::getVectorVT(*Context,
6212 OpVT.getVectorElementType(),
6213 OpVT.getVectorNumElements() / 2);
6214
6215 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6216
6217 // Insert the 128-bit vector.
6218 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6219 DAG.getConstant(0, MVT::i32),
6220 DAG, dl);
6221 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006222
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006223 if (Op.getValueType() == MVT::v1i64 &&
6224 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006225 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006226
Owen Anderson825b72b2009-08-11 20:47:22 +00006227 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006228 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6229 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006230 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006231 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006232}
6233
David Greene91585092011-01-26 15:38:49 +00006234// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6235// a simple subregister reference or explicit instructions to grab
6236// upper bits of a vector.
6237SDValue
6238X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6239 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006240 DebugLoc dl = Op.getNode()->getDebugLoc();
6241 SDValue Vec = Op.getNode()->getOperand(0);
6242 SDValue Idx = Op.getNode()->getOperand(1);
6243
6244 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6245 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6246 return Extract128BitVector(Vec, Idx, DAG, dl);
6247 }
David Greene91585092011-01-26 15:38:49 +00006248 }
6249 return SDValue();
6250}
6251
David Greenecfe33c42011-01-26 19:13:22 +00006252// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6253// simple superregister reference or explicit instructions to insert
6254// the upper bits of a vector.
6255SDValue
6256X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6257 if (Subtarget->hasAVX()) {
6258 DebugLoc dl = Op.getNode()->getDebugLoc();
6259 SDValue Vec = Op.getNode()->getOperand(0);
6260 SDValue SubVec = Op.getNode()->getOperand(1);
6261 SDValue Idx = Op.getNode()->getOperand(2);
6262
6263 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6264 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006265 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006266 }
6267 }
6268 return SDValue();
6269}
6270
Bill Wendling056292f2008-09-16 21:48:12 +00006271// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6272// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6273// one of the above mentioned nodes. It has to be wrapped because otherwise
6274// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6275// be used to form addressing mode. These wrapped nodes will be selected
6276// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006277SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006278X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006279 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006280
Chris Lattner41621a22009-06-26 19:22:52 +00006281 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6282 // global base reg.
6283 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006284 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006285 CodeModel::Model M = getTargetMachine().getCodeModel();
6286
Chris Lattner4f066492009-07-11 20:29:19 +00006287 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006288 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006289 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006290 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006291 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006292 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006293 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006294
Evan Cheng1606e8e2009-03-13 07:51:59 +00006295 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006296 CP->getAlignment(),
6297 CP->getOffset(), OpFlag);
6298 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006299 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006300 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006301 if (OpFlag) {
6302 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006303 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006304 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006305 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006306 }
6307
6308 return Result;
6309}
6310
Dan Gohmand858e902010-04-17 15:26:15 +00006311SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006312 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006313
Chris Lattner18c59872009-06-27 04:16:01 +00006314 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6315 // global base reg.
6316 unsigned char OpFlag = 0;
6317 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006318 CodeModel::Model M = getTargetMachine().getCodeModel();
6319
Chris Lattner4f066492009-07-11 20:29:19 +00006320 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006321 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006322 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006323 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006324 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006325 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006326 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006327
Chris Lattner18c59872009-06-27 04:16:01 +00006328 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6329 OpFlag);
6330 DebugLoc DL = JT->getDebugLoc();
6331 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006332
Chris Lattner18c59872009-06-27 04:16:01 +00006333 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006334 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006335 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6336 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006337 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006338 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006339
Chris Lattner18c59872009-06-27 04:16:01 +00006340 return Result;
6341}
6342
6343SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006344X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006345 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006346
Chris Lattner18c59872009-06-27 04:16:01 +00006347 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6348 // global base reg.
6349 unsigned char OpFlag = 0;
6350 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006351 CodeModel::Model M = getTargetMachine().getCodeModel();
6352
Chris Lattner4f066492009-07-11 20:29:19 +00006353 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006354 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006355 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006356 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006357 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006358 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006359 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006360
Chris Lattner18c59872009-06-27 04:16:01 +00006361 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006362
Chris Lattner18c59872009-06-27 04:16:01 +00006363 DebugLoc DL = Op.getDebugLoc();
6364 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006365
6366
Chris Lattner18c59872009-06-27 04:16:01 +00006367 // With PIC, the address is actually $g + Offset.
6368 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006369 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006370 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6371 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006372 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006373 Result);
6374 }
Eric Christopherfd179292009-08-27 18:07:15 +00006375
Chris Lattner18c59872009-06-27 04:16:01 +00006376 return Result;
6377}
6378
Dan Gohman475871a2008-07-27 21:46:04 +00006379SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006380X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006381 // Create the TargetBlockAddressAddress node.
6382 unsigned char OpFlags =
6383 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006384 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006385 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006386 DebugLoc dl = Op.getDebugLoc();
6387 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6388 /*isTarget=*/true, OpFlags);
6389
Dan Gohmanf705adb2009-10-30 01:28:02 +00006390 if (Subtarget->isPICStyleRIPRel() &&
6391 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006392 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6393 else
6394 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006395
Dan Gohman29cbade2009-11-20 23:18:13 +00006396 // With PIC, the address is actually $g + Offset.
6397 if (isGlobalRelativeToPICBase(OpFlags)) {
6398 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6399 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6400 Result);
6401 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006402
6403 return Result;
6404}
6405
6406SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006407X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006408 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006409 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006410 // Create the TargetGlobalAddress node, folding in the constant
6411 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006412 unsigned char OpFlags =
6413 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006414 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006415 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006416 if (OpFlags == X86II::MO_NO_FLAG &&
6417 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006418 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006419 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006420 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006421 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006422 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006423 }
Eric Christopherfd179292009-08-27 18:07:15 +00006424
Chris Lattner4f066492009-07-11 20:29:19 +00006425 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006426 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006427 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6428 else
6429 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006430
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006431 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006432 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006433 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6434 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006435 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006437
Chris Lattner36c25012009-07-10 07:34:39 +00006438 // For globals that require a load from a stub to get the address, emit the
6439 // load.
6440 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006441 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006442 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006443
Dan Gohman6520e202008-10-18 02:06:02 +00006444 // If there was a non-zero offset that we didn't fold, create an explicit
6445 // addition for it.
6446 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006447 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006448 DAG.getConstant(Offset, getPointerTy()));
6449
Evan Cheng0db9fe62006-04-25 20:13:52 +00006450 return Result;
6451}
6452
Evan Chengda43bcf2008-09-24 00:05:32 +00006453SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006454X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006455 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006456 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006457 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006458}
6459
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006460static SDValue
6461GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006462 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006463 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006464 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006465 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006466 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006467 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006468 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006469 GA->getOffset(),
6470 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006471 if (InFlag) {
6472 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006473 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006474 } else {
6475 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006476 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006477 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006478
6479 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006480 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006481
Rafael Espindola15f1b662009-04-24 12:59:40 +00006482 SDValue Flag = Chain.getValue(1);
6483 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006484}
6485
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006486// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006487static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006488LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006489 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006490 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006491 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6492 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006493 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006494 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006495 InFlag = Chain.getValue(1);
6496
Chris Lattnerb903bed2009-06-26 21:20:29 +00006497 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006498}
6499
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006500// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006501static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006502LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006503 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006504 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6505 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006506}
6507
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006508// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6509// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006510static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006511 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006512 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006513 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006514
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006515 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6516 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6517 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006518
Michael J. Spencerec38de22010-10-10 22:04:20 +00006519 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006520 DAG.getIntPtrConstant(0),
6521 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006522
Chris Lattnerb903bed2009-06-26 21:20:29 +00006523 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006524 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6525 // initialexec.
6526 unsigned WrapperKind = X86ISD::Wrapper;
6527 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006528 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006529 } else if (is64Bit) {
6530 assert(model == TLSModel::InitialExec);
6531 OperandFlags = X86II::MO_GOTTPOFF;
6532 WrapperKind = X86ISD::WrapperRIP;
6533 } else {
6534 assert(model == TLSModel::InitialExec);
6535 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006536 }
Eric Christopherfd179292009-08-27 18:07:15 +00006537
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006538 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6539 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006540 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006541 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006542 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006543 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006544
Rafael Espindola9a580232009-02-27 13:37:18 +00006545 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006546 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006547 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006548
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006549 // The address of the thread local variable is the add of the thread
6550 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006551 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006552}
6553
Dan Gohman475871a2008-07-27 21:46:04 +00006554SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006555X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006556
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006557 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006558 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006559
Eric Christopher30ef0e52010-06-03 04:07:48 +00006560 if (Subtarget->isTargetELF()) {
6561 // TODO: implement the "local dynamic" model
6562 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006563
Eric Christopher30ef0e52010-06-03 04:07:48 +00006564 // If GV is an alias then use the aliasee for determining
6565 // thread-localness.
6566 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6567 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006568
6569 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006570 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006571
Eric Christopher30ef0e52010-06-03 04:07:48 +00006572 switch (model) {
6573 case TLSModel::GeneralDynamic:
6574 case TLSModel::LocalDynamic: // not implemented
6575 if (Subtarget->is64Bit())
6576 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6577 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006578
Eric Christopher30ef0e52010-06-03 04:07:48 +00006579 case TLSModel::InitialExec:
6580 case TLSModel::LocalExec:
6581 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6582 Subtarget->is64Bit());
6583 }
6584 } else if (Subtarget->isTargetDarwin()) {
6585 // Darwin only has one model of TLS. Lower to that.
6586 unsigned char OpFlag = 0;
6587 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6588 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006589
Eric Christopher30ef0e52010-06-03 04:07:48 +00006590 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6591 // global base reg.
6592 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6593 !Subtarget->is64Bit();
6594 if (PIC32)
6595 OpFlag = X86II::MO_TLVP_PIC_BASE;
6596 else
6597 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006598 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006599 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006600 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006601 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006602 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006603
Eric Christopher30ef0e52010-06-03 04:07:48 +00006604 // With PIC32, the address is actually $g + Offset.
6605 if (PIC32)
6606 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6607 DAG.getNode(X86ISD::GlobalBaseReg,
6608 DebugLoc(), getPointerTy()),
6609 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006610
Eric Christopher30ef0e52010-06-03 04:07:48 +00006611 // Lowering the machine isd will make sure everything is in the right
6612 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006613 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006614 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006615 SDValue Args[] = { Chain, Offset };
6616 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006617
Eric Christopher30ef0e52010-06-03 04:07:48 +00006618 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6619 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6620 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006621
Eric Christopher30ef0e52010-06-03 04:07:48 +00006622 // And our return value (tls address) is in the standard call return value
6623 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006624 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6625 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006626 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006627
Eric Christopher30ef0e52010-06-03 04:07:48 +00006628 assert(false &&
6629 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006630
Torok Edwinc23197a2009-07-14 16:55:14 +00006631 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006632 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006633}
6634
Evan Cheng0db9fe62006-04-25 20:13:52 +00006635
Nadav Rotem43012222011-05-11 08:12:09 +00006636/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006637/// take a 2 x i32 value to shift plus a shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +00006638SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006639 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006640 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006641 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006642 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006643 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006644 SDValue ShOpLo = Op.getOperand(0);
6645 SDValue ShOpHi = Op.getOperand(1);
6646 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006647 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006648 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006649 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006650
Dan Gohman475871a2008-07-27 21:46:04 +00006651 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006652 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006653 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6654 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006655 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006656 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6657 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006658 }
Evan Chenge3413162006-01-09 18:33:28 +00006659
Owen Anderson825b72b2009-08-11 20:47:22 +00006660 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6661 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006662 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006663 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006664
Dan Gohman475871a2008-07-27 21:46:04 +00006665 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006666 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006667 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6668 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006669
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006670 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006671 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6672 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006673 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006674 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6675 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006676 }
6677
Dan Gohman475871a2008-07-27 21:46:04 +00006678 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006679 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680}
Evan Chenga3195e82006-01-12 22:54:21 +00006681
Dan Gohmand858e902010-04-17 15:26:15 +00006682SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6683 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006684 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006685
Dale Johannesen0488fb62010-09-30 23:57:10 +00006686 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006687 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006688
Owen Anderson825b72b2009-08-11 20:47:22 +00006689 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006690 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006691
Eli Friedman36df4992009-05-27 00:47:34 +00006692 // These are really Legal; return the operand so the caller accepts it as
6693 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006694 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006695 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006696 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006697 Subtarget->is64Bit()) {
6698 return Op;
6699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006700
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006701 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006702 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006704 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006705 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006706 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006707 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006708 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006709 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006710 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6711}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006712
Owen Andersone50ed302009-08-10 22:56:29 +00006713SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006714 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006715 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006716 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006717 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006718 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006719 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006720 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006721 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006722 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006723 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006724
Chris Lattner492a43e2010-09-22 01:28:21 +00006725 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006726
Stuart Hastings84be9582011-06-02 15:57:11 +00006727 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
6728 MachineMemOperand *MMO;
6729 if (FI) {
6730 int SSFI = FI->getIndex();
6731 MMO =
6732 DAG.getMachineFunction()
6733 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6734 MachineMemOperand::MOLoad, ByteSize, ByteSize);
6735 } else {
6736 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
6737 StackSlot = StackSlot.getOperand(1);
6738 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006739 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006740 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6741 X86ISD::FILD, DL,
6742 Tys, Ops, array_lengthof(Ops),
6743 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006745 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006746 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006747 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006748
6749 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6750 // shouldn't be necessary except that RFP cannot be live across
6751 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006752 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006753 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6754 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006755 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006757 SDValue Ops[] = {
6758 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6759 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006760 MachineMemOperand *MMO =
6761 DAG.getMachineFunction()
6762 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006763 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006764
Chris Lattner492a43e2010-09-22 01:28:21 +00006765 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6766 Ops, array_lengthof(Ops),
6767 Op.getValueType(), MMO);
6768 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006769 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006770 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006771 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006772
Evan Cheng0db9fe62006-04-25 20:13:52 +00006773 return Result;
6774}
6775
Bill Wendling8b8a6362009-01-17 03:56:04 +00006776// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006777SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6778 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006779 // This algorithm is not obvious. Here it is in C code, more or less:
6780 /*
6781 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6782 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6783 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006784
Bill Wendling8b8a6362009-01-17 03:56:04 +00006785 // Copy ints to xmm registers.
6786 __m128i xh = _mm_cvtsi32_si128( hi );
6787 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006788
Bill Wendling8b8a6362009-01-17 03:56:04 +00006789 // Combine into low half of a single xmm register.
6790 __m128i x = _mm_unpacklo_epi32( xh, xl );
6791 __m128d d;
6792 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006793
Bill Wendling8b8a6362009-01-17 03:56:04 +00006794 // Merge in appropriate exponents to give the integer bits the right
6795 // magnitude.
6796 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006797
Bill Wendling8b8a6362009-01-17 03:56:04 +00006798 // Subtract away the biases to deal with the IEEE-754 double precision
6799 // implicit 1.
6800 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006801
Bill Wendling8b8a6362009-01-17 03:56:04 +00006802 // All conversions up to here are exact. The correctly rounded result is
6803 // calculated using the current rounding mode using the following
6804 // horizontal add.
6805 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6806 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6807 // store doesn't really need to be here (except
6808 // maybe to zero the other double)
6809 return sd;
6810 }
6811 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006812
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006813 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006814 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006815
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006816 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006817 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006818 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6819 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6820 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6821 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006822 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006823 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006824
Bill Wendling8b8a6362009-01-17 03:56:04 +00006825 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006826 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006827 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006828 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006829 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006830 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006831 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006832
Owen Anderson825b72b2009-08-11 20:47:22 +00006833 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6834 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006835 Op.getOperand(0),
6836 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006837 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6838 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006839 Op.getOperand(0),
6840 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006841 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6842 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006843 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006844 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006845 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006848 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006849 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006851
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006852 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006853 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006854 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6855 DAG.getUNDEF(MVT::v2f64), ShufMask);
6856 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6857 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006858 DAG.getIntPtrConstant(0));
6859}
6860
Bill Wendling8b8a6362009-01-17 03:56:04 +00006861// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006862SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6863 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006864 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006865 // FP constant to bias correct the final result.
6866 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006867 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006868
6869 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6871 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006872 Op.getOperand(0),
6873 DAG.getIntPtrConstant(0)));
6874
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006876 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006877 DAG.getIntPtrConstant(0));
6878
6879 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006880 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006881 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006882 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006883 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006884 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006885 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 MVT::v2f64, Bias)));
6887 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006888 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006889 DAG.getIntPtrConstant(0));
6890
6891 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006892 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006893
6894 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006895 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006896
Owen Anderson825b72b2009-08-11 20:47:22 +00006897 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006898 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006899 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006900 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006901 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006902 }
6903
6904 // Handle final rounding.
6905 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006906}
6907
Dan Gohmand858e902010-04-17 15:26:15 +00006908SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6909 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006910 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006911 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006912
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006913 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006914 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6915 // the optimization here.
6916 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006917 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006918
Owen Andersone50ed302009-08-10 22:56:29 +00006919 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006920 EVT DstVT = Op.getValueType();
6921 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006922 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006923 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006924 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006925
6926 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006928 if (SrcVT == MVT::i32) {
6929 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6930 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6931 getPointerTy(), StackSlot, WordOff);
6932 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006933 StackSlot, MachinePointerInfo(),
6934 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006935 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006936 OffsetSlot, MachinePointerInfo(),
6937 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006938 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6939 return Fild;
6940 }
6941
6942 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6943 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006944 StackSlot, MachinePointerInfo(),
6945 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006946 // For i64 source, we need to add the appropriate power of 2 if the input
6947 // was negative. This is the same as the optimization in
6948 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6949 // we must be careful to do the computation in x87 extended precision, not
6950 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006951 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6952 MachineMemOperand *MMO =
6953 DAG.getMachineFunction()
6954 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6955 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006956
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006957 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6958 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006959 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6960 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006961
6962 APInt FF(32, 0x5F800000ULL);
6963
6964 // Check whether the sign bit is set.
6965 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6966 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6967 ISD::SETLT);
6968
6969 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6970 SDValue FudgePtr = DAG.getConstantPool(
6971 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6972 getPointerTy());
6973
6974 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6975 SDValue Zero = DAG.getIntPtrConstant(0);
6976 SDValue Four = DAG.getIntPtrConstant(4);
6977 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6978 Zero, Four);
6979 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6980
6981 // Load the value out, extending it from f32 to f80.
6982 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006983 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006984 FudgePtr, MachinePointerInfo::getConstantPool(),
6985 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006986 // Extend everything to 80 bits to force it to be done on x87.
6987 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6988 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006989}
6990
Dan Gohman475871a2008-07-27 21:46:04 +00006991std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006992FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006993 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006994
Owen Andersone50ed302009-08-10 22:56:29 +00006995 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006996
6997 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6999 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00007000 }
7001
Owen Anderson825b72b2009-08-11 20:47:22 +00007002 assert(DstTy.getSimpleVT() <= MVT::i64 &&
7003 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00007004 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00007005
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007006 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007007 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00007008 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007009 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00007010 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007011 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00007012 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007013 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007014
Evan Cheng87c89352007-10-15 20:11:21 +00007015 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7016 // stack slot.
7017 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00007018 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00007019 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007020 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00007021
Michael J. Spencerec38de22010-10-10 22:04:20 +00007022
7023
Evan Cheng0db9fe62006-04-25 20:13:52 +00007024 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00007025 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007026 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007027 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7028 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7029 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007030 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007031
Dan Gohman475871a2008-07-27 21:46:04 +00007032 SDValue Chain = DAG.getEntryNode();
7033 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00007034 EVT TheVT = Op.getOperand(0).getValueType();
7035 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007037 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007038 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007039 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007040 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007041 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007042 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007043 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007044
Chris Lattner492a43e2010-09-22 01:28:21 +00007045 MachineMemOperand *MMO =
7046 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7047 MachineMemOperand::MOLoad, MemSize, MemSize);
7048 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7049 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007050 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007051 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007052 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7053 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007054
Chris Lattner07290932010-09-22 01:05:16 +00007055 MachineMemOperand *MMO =
7056 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7057 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007058
Evan Cheng0db9fe62006-04-25 20:13:52 +00007059 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007060 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007061 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7062 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007063
Chris Lattner27a6c732007-11-24 07:07:01 +00007064 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065}
7066
Dan Gohmand858e902010-04-17 15:26:15 +00007067SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7068 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007069 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007070 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007071
Eli Friedman948e95a2009-05-23 09:59:16 +00007072 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007073 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007074 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7075 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007076
Chris Lattner27a6c732007-11-24 07:07:01 +00007077 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007078 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007079 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007080}
7081
Dan Gohmand858e902010-04-17 15:26:15 +00007082SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7083 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007084 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7085 SDValue FIST = Vals.first, StackSlot = Vals.second;
7086 assert(FIST.getNode() && "Unexpected failure");
7087
7088 // Load the result.
7089 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007090 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007091}
7092
Dan Gohmand858e902010-04-17 15:26:15 +00007093SDValue X86TargetLowering::LowerFABS(SDValue Op,
7094 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007095 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007096 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007097 EVT VT = Op.getValueType();
7098 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007099 if (VT.isVector())
7100 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007101 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007102 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007103 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007104 CV.push_back(C);
7105 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007106 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007107 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007108 CV.push_back(C);
7109 CV.push_back(C);
7110 CV.push_back(C);
7111 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007112 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007113 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007114 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007115 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007116 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007117 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007118 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007119}
7120
Dan Gohmand858e902010-04-17 15:26:15 +00007121SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007122 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007123 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007124 EVT VT = Op.getValueType();
7125 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007126 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007127 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007128 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007129 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007130 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007131 CV.push_back(C);
7132 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007133 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007134 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007135 CV.push_back(C);
7136 CV.push_back(C);
7137 CV.push_back(C);
7138 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007139 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007140 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007141 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007142 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007143 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007144 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007145 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007146 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007148 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007149 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007150 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007151 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007152 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007153 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154}
7155
Dan Gohmand858e902010-04-17 15:26:15 +00007156SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007157 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007158 SDValue Op0 = Op.getOperand(0);
7159 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007160 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007161 EVT VT = Op.getValueType();
7162 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007163
7164 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007165 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007166 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007167 SrcVT = VT;
7168 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007169 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007170 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007171 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007172 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007173 }
7174
7175 // At this point the operands and the result should have the same
7176 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007177
Evan Cheng68c47cb2007-01-05 07:55:56 +00007178 // First get the sign bit of second operand.
7179 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007181 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7182 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007183 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007184 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7185 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7186 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7187 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007188 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007189 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007190 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007191 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007192 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007193 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007194 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007195
7196 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007197 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 // Op0 is MVT::f32, Op1 is MVT::f64.
7199 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7200 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7201 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007202 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007203 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007204 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007205 }
7206
Evan Cheng73d6cf12007-01-05 21:37:56 +00007207 // Clear first operand sign bit.
7208 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007209 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007210 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7211 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007212 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007213 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7214 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7215 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7216 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007217 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007218 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007219 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007220 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007221 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007222 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007223 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007224
7225 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007226 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007227}
7228
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00007229SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
7230 SDValue N0 = Op.getOperand(0);
7231 DebugLoc dl = Op.getDebugLoc();
7232 EVT VT = Op.getValueType();
7233
7234 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
7235 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
7236 DAG.getConstant(1, VT));
7237 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
7238}
7239
Dan Gohman076aee32009-03-04 19:44:21 +00007240/// Emit nodes that will be selected as "test Op0,Op0", or something
7241/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007242SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007243 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007244 DebugLoc dl = Op.getDebugLoc();
7245
Dan Gohman31125812009-03-07 01:58:32 +00007246 // CF and OF aren't always set the way we want. Determine which
7247 // of these we need.
7248 bool NeedCF = false;
7249 bool NeedOF = false;
7250 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007251 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007252 case X86::COND_A: case X86::COND_AE:
7253 case X86::COND_B: case X86::COND_BE:
7254 NeedCF = true;
7255 break;
7256 case X86::COND_G: case X86::COND_GE:
7257 case X86::COND_L: case X86::COND_LE:
7258 case X86::COND_O: case X86::COND_NO:
7259 NeedOF = true;
7260 break;
Dan Gohman31125812009-03-07 01:58:32 +00007261 }
7262
Dan Gohman076aee32009-03-04 19:44:21 +00007263 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007264 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7265 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007266 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7267 // Emit a CMP with 0, which is the TEST pattern.
7268 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7269 DAG.getConstant(0, Op.getValueType()));
7270
7271 unsigned Opcode = 0;
7272 unsigned NumOperands = 0;
7273 switch (Op.getNode()->getOpcode()) {
7274 case ISD::ADD:
7275 // Due to an isel shortcoming, be conservative if this add is likely to be
7276 // selected as part of a load-modify-store instruction. When the root node
7277 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7278 // uses of other nodes in the match, such as the ADD in this case. This
7279 // leads to the ADD being left around and reselected, with the result being
7280 // two adds in the output. Alas, even if none our users are stores, that
7281 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7282 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7283 // climbing the DAG back to the root, and it doesn't seem to be worth the
7284 // effort.
7285 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007286 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007287 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7288 goto default_case;
7289
7290 if (ConstantSDNode *C =
7291 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7292 // An add of one will be selected as an INC.
7293 if (C->getAPIntValue() == 1) {
7294 Opcode = X86ISD::INC;
7295 NumOperands = 1;
7296 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007297 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007298
7299 // An add of negative one (subtract of one) will be selected as a DEC.
7300 if (C->getAPIntValue().isAllOnesValue()) {
7301 Opcode = X86ISD::DEC;
7302 NumOperands = 1;
7303 break;
7304 }
Dan Gohman076aee32009-03-04 19:44:21 +00007305 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007306
7307 // Otherwise use a regular EFLAGS-setting add.
7308 Opcode = X86ISD::ADD;
7309 NumOperands = 2;
7310 break;
7311 case ISD::AND: {
7312 // If the primary and result isn't used, don't bother using X86ISD::AND,
7313 // because a TEST instruction will be better.
7314 bool NonFlagUse = false;
7315 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7316 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7317 SDNode *User = *UI;
7318 unsigned UOpNo = UI.getOperandNo();
7319 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7320 // Look pass truncate.
7321 UOpNo = User->use_begin().getOperandNo();
7322 User = *User->use_begin();
7323 }
7324
7325 if (User->getOpcode() != ISD::BRCOND &&
7326 User->getOpcode() != ISD::SETCC &&
7327 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7328 NonFlagUse = true;
7329 break;
7330 }
Dan Gohman076aee32009-03-04 19:44:21 +00007331 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007332
7333 if (!NonFlagUse)
7334 break;
7335 }
7336 // FALL THROUGH
7337 case ISD::SUB:
7338 case ISD::OR:
7339 case ISD::XOR:
7340 // Due to the ISEL shortcoming noted above, be conservative if this op is
7341 // likely to be selected as part of a load-modify-store instruction.
7342 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7343 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7344 if (UI->getOpcode() == ISD::STORE)
7345 goto default_case;
7346
7347 // Otherwise use a regular EFLAGS-setting instruction.
7348 switch (Op.getNode()->getOpcode()) {
7349 default: llvm_unreachable("unexpected operator!");
7350 case ISD::SUB: Opcode = X86ISD::SUB; break;
7351 case ISD::OR: Opcode = X86ISD::OR; break;
7352 case ISD::XOR: Opcode = X86ISD::XOR; break;
7353 case ISD::AND: Opcode = X86ISD::AND; break;
7354 }
7355
7356 NumOperands = 2;
7357 break;
7358 case X86ISD::ADD:
7359 case X86ISD::SUB:
7360 case X86ISD::INC:
7361 case X86ISD::DEC:
7362 case X86ISD::OR:
7363 case X86ISD::XOR:
7364 case X86ISD::AND:
7365 return SDValue(Op.getNode(), 1);
7366 default:
7367 default_case:
7368 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007369 }
7370
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007371 if (Opcode == 0)
7372 // Emit a CMP with 0, which is the TEST pattern.
7373 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7374 DAG.getConstant(0, Op.getValueType()));
7375
7376 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7377 SmallVector<SDValue, 4> Ops;
7378 for (unsigned i = 0; i != NumOperands; ++i)
7379 Ops.push_back(Op.getOperand(i));
7380
7381 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7382 DAG.ReplaceAllUsesWith(Op, New);
7383 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007384}
7385
7386/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7387/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007388SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007389 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007390 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7391 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007392 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007393
7394 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007395 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007396}
7397
Evan Chengd40d03e2010-01-06 19:38:29 +00007398/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7399/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007400SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7401 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007402 SDValue Op0 = And.getOperand(0);
7403 SDValue Op1 = And.getOperand(1);
7404 if (Op0.getOpcode() == ISD::TRUNCATE)
7405 Op0 = Op0.getOperand(0);
7406 if (Op1.getOpcode() == ISD::TRUNCATE)
7407 Op1 = Op1.getOperand(0);
7408
Evan Chengd40d03e2010-01-06 19:38:29 +00007409 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007410 if (Op1.getOpcode() == ISD::SHL)
7411 std::swap(Op0, Op1);
7412 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007413 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7414 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007415 // If we looked past a truncate, check that it's only truncating away
7416 // known zeros.
7417 unsigned BitWidth = Op0.getValueSizeInBits();
7418 unsigned AndBitWidth = And.getValueSizeInBits();
7419 if (BitWidth > AndBitWidth) {
7420 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7421 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7422 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7423 return SDValue();
7424 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007425 LHS = Op1;
7426 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007427 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007428 } else if (Op1.getOpcode() == ISD::Constant) {
7429 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7430 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007431 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7432 LHS = AndLHS.getOperand(0);
7433 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007434 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007435 }
Evan Cheng0488db92007-09-25 01:57:46 +00007436
Evan Chengd40d03e2010-01-06 19:38:29 +00007437 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007438 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007439 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007440 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007441 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007442 // Also promote i16 to i32 for performance / code size reason.
7443 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007444 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007445 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007446
Evan Chengd40d03e2010-01-06 19:38:29 +00007447 // If the operand types disagree, extend the shift amount to match. Since
7448 // BT ignores high bits (like shifts) we can use anyextend.
7449 if (LHS.getValueType() != RHS.getValueType())
7450 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007451
Evan Chengd40d03e2010-01-06 19:38:29 +00007452 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7453 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7454 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7455 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007456 }
7457
Evan Cheng54de3ea2010-01-05 06:52:31 +00007458 return SDValue();
7459}
7460
Dan Gohmand858e902010-04-17 15:26:15 +00007461SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007462 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7463 SDValue Op0 = Op.getOperand(0);
7464 SDValue Op1 = Op.getOperand(1);
7465 DebugLoc dl = Op.getDebugLoc();
7466 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7467
7468 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007469 // Lower (X & (1 << N)) == 0 to BT(X, N).
7470 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7471 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00007472 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007473 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007474 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007475 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7476 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7477 if (NewSetCC.getNode())
7478 return NewSetCC;
7479 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007480
Chris Lattner481eebc2010-12-19 21:23:48 +00007481 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7482 // these.
7483 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00007484 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00007485 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7486 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007487
Chris Lattner481eebc2010-12-19 21:23:48 +00007488 // If the input is a setcc, then reuse the input setcc or use a new one with
7489 // the inverted condition.
7490 if (Op0.getOpcode() == X86ISD::SETCC) {
7491 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7492 bool Invert = (CC == ISD::SETNE) ^
7493 cast<ConstantSDNode>(Op1)->isNullValue();
7494 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007495
Evan Cheng2c755ba2010-02-27 07:36:59 +00007496 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007497 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7498 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7499 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007500 }
7501
Evan Chenge5b51ac2010-04-17 06:13:15 +00007502 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007503 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007504 if (X86CC == X86::COND_INVALID)
7505 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007506
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007507 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007508 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007509 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007510}
7511
Dan Gohmand858e902010-04-17 15:26:15 +00007512SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007513 SDValue Cond;
7514 SDValue Op0 = Op.getOperand(0);
7515 SDValue Op1 = Op.getOperand(1);
7516 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007517 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007518 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7519 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007520 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007521
7522 if (isFP) {
7523 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007524 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007525 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7526 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007527 bool Swap = false;
7528
7529 switch (SetCCOpcode) {
7530 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007531 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007532 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007533 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007534 case ISD::SETGT: Swap = true; // Fallthrough
7535 case ISD::SETLT:
7536 case ISD::SETOLT: SSECC = 1; break;
7537 case ISD::SETOGE:
7538 case ISD::SETGE: Swap = true; // Fallthrough
7539 case ISD::SETLE:
7540 case ISD::SETOLE: SSECC = 2; break;
7541 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007542 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007543 case ISD::SETNE: SSECC = 4; break;
7544 case ISD::SETULE: Swap = true;
7545 case ISD::SETUGE: SSECC = 5; break;
7546 case ISD::SETULT: Swap = true;
7547 case ISD::SETUGT: SSECC = 6; break;
7548 case ISD::SETO: SSECC = 7; break;
7549 }
7550 if (Swap)
7551 std::swap(Op0, Op1);
7552
Nate Begemanfb8ead02008-07-25 19:05:58 +00007553 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007554 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007555 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007556 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007557 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7558 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007559 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007560 }
7561 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007562 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7564 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007565 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007566 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007567 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007568 }
7569 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007570 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007571 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007572
Nate Begeman30a0de92008-07-17 16:51:19 +00007573 // We are handling one of the integer comparisons here. Since SSE only has
7574 // GT and EQ comparisons for integer, swapping operands and multiple
7575 // operations may be required for some comparisons.
7576 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7577 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007578
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007580 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007581 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007583 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7584 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007586
Nate Begeman30a0de92008-07-17 16:51:19 +00007587 switch (SetCCOpcode) {
7588 default: break;
7589 case ISD::SETNE: Invert = true;
7590 case ISD::SETEQ: Opc = EQOpc; break;
7591 case ISD::SETLT: Swap = true;
7592 case ISD::SETGT: Opc = GTOpc; break;
7593 case ISD::SETGE: Swap = true;
7594 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7595 case ISD::SETULT: Swap = true;
7596 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7597 case ISD::SETUGE: Swap = true;
7598 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7599 }
7600 if (Swap)
7601 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007602
Nate Begeman30a0de92008-07-17 16:51:19 +00007603 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7604 // bits of the inputs before performing those operations.
7605 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007606 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007607 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7608 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007609 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007610 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7611 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007612 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7613 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007615
Dale Johannesenace16102009-02-03 19:33:06 +00007616 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007617
7618 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007619 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007620 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007621
Nate Begeman30a0de92008-07-17 16:51:19 +00007622 return Result;
7623}
Evan Cheng0488db92007-09-25 01:57:46 +00007624
Evan Cheng370e5342008-12-03 08:38:43 +00007625// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007626static bool isX86LogicalCmp(SDValue Op) {
7627 unsigned Opc = Op.getNode()->getOpcode();
7628 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7629 return true;
7630 if (Op.getResNo() == 1 &&
7631 (Opc == X86ISD::ADD ||
7632 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007633 Opc == X86ISD::ADC ||
7634 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007635 Opc == X86ISD::SMUL ||
7636 Opc == X86ISD::UMUL ||
7637 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007638 Opc == X86ISD::DEC ||
7639 Opc == X86ISD::OR ||
7640 Opc == X86ISD::XOR ||
7641 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007642 return true;
7643
Chris Lattner9637d5b2010-12-05 07:49:54 +00007644 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7645 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007646
Dan Gohman076aee32009-03-04 19:44:21 +00007647 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007648}
7649
Chris Lattnera2b56002010-12-05 01:23:24 +00007650static bool isZero(SDValue V) {
7651 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7652 return C && C->isNullValue();
7653}
7654
Chris Lattner96908b12010-12-05 02:00:51 +00007655static bool isAllOnes(SDValue V) {
7656 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7657 return C && C->isAllOnesValue();
7658}
7659
Dan Gohmand858e902010-04-17 15:26:15 +00007660SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007661 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007662 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007663 SDValue Op1 = Op.getOperand(1);
7664 SDValue Op2 = Op.getOperand(2);
7665 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007666 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007667
Dan Gohman1a492952009-10-20 16:22:37 +00007668 if (Cond.getOpcode() == ISD::SETCC) {
7669 SDValue NewCond = LowerSETCC(Cond, DAG);
7670 if (NewCond.getNode())
7671 Cond = NewCond;
7672 }
Evan Cheng734503b2006-09-11 02:19:56 +00007673
Chris Lattnera2b56002010-12-05 01:23:24 +00007674 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007675 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007676 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007677 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007678 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007679 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7680 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007681 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007682
Chris Lattnera2b56002010-12-05 01:23:24 +00007683 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007684
7685 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007686 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7687 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007688
7689 SDValue CmpOp0 = Cmp.getOperand(0);
7690 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7691 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007692
Chris Lattner96908b12010-12-05 02:00:51 +00007693 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007694 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7695 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007696
Chris Lattner96908b12010-12-05 02:00:51 +00007697 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7698 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007699
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007700 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007701 if (N2C == 0 || !N2C->isNullValue())
7702 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7703 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007704 }
7705 }
7706
Chris Lattnera2b56002010-12-05 01:23:24 +00007707 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007708 if (Cond.getOpcode() == ISD::AND &&
7709 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7710 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007711 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007712 Cond = Cond.getOperand(0);
7713 }
7714
Evan Cheng3f41d662007-10-08 22:16:29 +00007715 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7716 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007717 if (Cond.getOpcode() == X86ISD::SETCC ||
7718 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007719 CC = Cond.getOperand(0);
7720
Dan Gohman475871a2008-07-27 21:46:04 +00007721 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007722 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007723 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007724
Evan Cheng3f41d662007-10-08 22:16:29 +00007725 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007726 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007727 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007728 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007729
Chris Lattnerd1980a52009-03-12 06:52:53 +00007730 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7731 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007732 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007733 addTest = false;
7734 }
7735 }
7736
7737 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007738 // Look pass the truncate.
7739 if (Cond.getOpcode() == ISD::TRUNCATE)
7740 Cond = Cond.getOperand(0);
7741
7742 // We know the result of AND is compared against zero. Try to match
7743 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007744 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007745 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007746 if (NewSetCC.getNode()) {
7747 CC = NewSetCC.getOperand(0);
7748 Cond = NewSetCC.getOperand(1);
7749 addTest = false;
7750 }
7751 }
7752 }
7753
7754 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007755 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007756 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007757 }
7758
Benjamin Kramere915ff32010-12-22 23:09:28 +00007759 // a < b ? -1 : 0 -> RES = ~setcc_carry
7760 // a < b ? 0 : -1 -> RES = setcc_carry
7761 // a >= b ? -1 : 0 -> RES = setcc_carry
7762 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7763 if (Cond.getOpcode() == X86ISD::CMP) {
7764 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7765
7766 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7767 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7768 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7769 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7770 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7771 return DAG.getNOT(DL, Res, Res.getValueType());
7772 return Res;
7773 }
7774 }
7775
Evan Cheng0488db92007-09-25 01:57:46 +00007776 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7777 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007778 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007779 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007780 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007781}
7782
Evan Cheng370e5342008-12-03 08:38:43 +00007783// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7784// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7785// from the AND / OR.
7786static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7787 Opc = Op.getOpcode();
7788 if (Opc != ISD::OR && Opc != ISD::AND)
7789 return false;
7790 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7791 Op.getOperand(0).hasOneUse() &&
7792 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7793 Op.getOperand(1).hasOneUse());
7794}
7795
Evan Cheng961d6d42009-02-02 08:19:07 +00007796// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7797// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007798static bool isXor1OfSetCC(SDValue Op) {
7799 if (Op.getOpcode() != ISD::XOR)
7800 return false;
7801 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7802 if (N1C && N1C->getAPIntValue() == 1) {
7803 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7804 Op.getOperand(0).hasOneUse();
7805 }
7806 return false;
7807}
7808
Dan Gohmand858e902010-04-17 15:26:15 +00007809SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007810 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007811 SDValue Chain = Op.getOperand(0);
7812 SDValue Cond = Op.getOperand(1);
7813 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007814 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007815 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007816
Dan Gohman1a492952009-10-20 16:22:37 +00007817 if (Cond.getOpcode() == ISD::SETCC) {
7818 SDValue NewCond = LowerSETCC(Cond, DAG);
7819 if (NewCond.getNode())
7820 Cond = NewCond;
7821 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007822#if 0
7823 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007824 else if (Cond.getOpcode() == X86ISD::ADD ||
7825 Cond.getOpcode() == X86ISD::SUB ||
7826 Cond.getOpcode() == X86ISD::SMUL ||
7827 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007828 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007829#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007830
Evan Chengad9c0a32009-12-15 00:53:42 +00007831 // Look pass (and (setcc_carry (cmp ...)), 1).
7832 if (Cond.getOpcode() == ISD::AND &&
7833 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7834 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007835 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007836 Cond = Cond.getOperand(0);
7837 }
7838
Evan Cheng3f41d662007-10-08 22:16:29 +00007839 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7840 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007841 if (Cond.getOpcode() == X86ISD::SETCC ||
7842 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007843 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007844
Dan Gohman475871a2008-07-27 21:46:04 +00007845 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007846 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007847 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007848 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007849 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007850 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007851 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007852 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007853 default: break;
7854 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007855 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007856 // These can only come from an arithmetic instruction with overflow,
7857 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007858 Cond = Cond.getNode()->getOperand(1);
7859 addTest = false;
7860 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007861 }
Evan Cheng0488db92007-09-25 01:57:46 +00007862 }
Evan Cheng370e5342008-12-03 08:38:43 +00007863 } else {
7864 unsigned CondOpc;
7865 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7866 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007867 if (CondOpc == ISD::OR) {
7868 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7869 // two branches instead of an explicit OR instruction with a
7870 // separate test.
7871 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007872 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007873 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007874 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007875 Chain, Dest, CC, Cmp);
7876 CC = Cond.getOperand(1).getOperand(0);
7877 Cond = Cmp;
7878 addTest = false;
7879 }
7880 } else { // ISD::AND
7881 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7882 // two branches instead of an explicit AND instruction with a
7883 // separate test. However, we only do this if this block doesn't
7884 // have a fall-through edge, because this requires an explicit
7885 // jmp when the condition is false.
7886 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007887 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007888 Op.getNode()->hasOneUse()) {
7889 X86::CondCode CCode =
7890 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7891 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007892 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007893 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007894 // Look for an unconditional branch following this conditional branch.
7895 // We need this because we need to reverse the successors in order
7896 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007897 if (User->getOpcode() == ISD::BR) {
7898 SDValue FalseBB = User->getOperand(1);
7899 SDNode *NewBR =
7900 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007901 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007902 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007903 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007904
Dale Johannesene4d209d2009-02-03 20:21:25 +00007905 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007906 Chain, Dest, CC, Cmp);
7907 X86::CondCode CCode =
7908 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7909 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007911 Cond = Cmp;
7912 addTest = false;
7913 }
7914 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007915 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007916 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7917 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7918 // It should be transformed during dag combiner except when the condition
7919 // is set by a arithmetics with overflow node.
7920 X86::CondCode CCode =
7921 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7922 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007924 Cond = Cond.getOperand(0).getOperand(1);
7925 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007926 }
Evan Cheng0488db92007-09-25 01:57:46 +00007927 }
7928
7929 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007930 // Look pass the truncate.
7931 if (Cond.getOpcode() == ISD::TRUNCATE)
7932 Cond = Cond.getOperand(0);
7933
7934 // We know the result of AND is compared against zero. Try to match
7935 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007936 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007937 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7938 if (NewSetCC.getNode()) {
7939 CC = NewSetCC.getOperand(0);
7940 Cond = NewSetCC.getOperand(1);
7941 addTest = false;
7942 }
7943 }
7944 }
7945
7946 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007948 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007949 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007950 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007951 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007952}
7953
Anton Korobeynikove060b532007-04-17 19:34:00 +00007954
7955// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7956// Calls to _alloca is needed to probe the stack when allocating more than 4k
7957// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7958// that the guard pages used by the OS virtual memory manager are allocated in
7959// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007960SDValue
7961X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007962 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007963 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007964 "This should be used only on Windows targets");
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007965 assert(!Subtarget->isTargetEnvMacho());
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007966 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007967
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007968 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007969 SDValue Chain = Op.getOperand(0);
7970 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007971 // FIXME: Ensure alignment here
7972
Dan Gohman475871a2008-07-27 21:46:04 +00007973 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007974
Owen Anderson825b72b2009-08-11 20:47:22 +00007975 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007976 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007977
NAKAMURA Takumia2e07622011-03-24 07:07:00 +00007978 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007979 Flag = Chain.getValue(1);
7980
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007981 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007982
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007983 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007984 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007985
Dale Johannesendd64c412009-02-04 00:33:20 +00007986 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007987
Dan Gohman475871a2008-07-27 21:46:04 +00007988 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007989 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007990}
7991
Dan Gohmand858e902010-04-17 15:26:15 +00007992SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007993 MachineFunction &MF = DAG.getMachineFunction();
7994 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7995
Dan Gohman69de1932008-02-06 22:27:42 +00007996 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007997 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007998
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007999 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00008000 // vastart just stores the address of the VarArgsFrameIndex slot into the
8001 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00008002 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8003 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008004 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
8005 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008006 }
8007
8008 // __va_list_tag:
8009 // gp_offset (0 - 6 * 8)
8010 // fp_offset (48 - 48 + 8 * 16)
8011 // overflow_arg_area (point to parameters coming in memory).
8012 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00008013 SmallVector<SDValue, 8> MemOps;
8014 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00008015 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008016 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008017 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
8018 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008019 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008020 MemOps.push_back(Store);
8021
8022 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00008023 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008024 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008025 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00008026 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
8027 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008028 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008029 MemOps.push_back(Store);
8030
8031 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00008032 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008033 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00008034 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8035 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008036 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
8037 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00008038 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008039 MemOps.push_back(Store);
8040
8041 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00008042 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008043 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00008044 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
8045 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00008046 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
8047 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00008048 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008049 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008050 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008051}
8052
Dan Gohmand858e902010-04-17 15:26:15 +00008053SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008054 assert(Subtarget->is64Bit() &&
8055 "LowerVAARG only handles 64-bit va_arg!");
8056 assert((Subtarget->isTargetLinux() ||
8057 Subtarget->isTargetDarwin()) &&
8058 "Unhandled target in LowerVAARG");
8059 assert(Op.getNode()->getNumOperands() == 4);
8060 SDValue Chain = Op.getOperand(0);
8061 SDValue SrcPtr = Op.getOperand(1);
8062 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8063 unsigned Align = Op.getConstantOperandVal(3);
8064 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008065
Dan Gohman320afb82010-10-12 18:00:49 +00008066 EVT ArgVT = Op.getNode()->getValueType(0);
8067 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8068 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8069 uint8_t ArgMode;
8070
8071 // Decide which area this value should be read from.
8072 // TODO: Implement the AMD64 ABI in its entirety. This simple
8073 // selection mechanism works only for the basic types.
8074 if (ArgVT == MVT::f80) {
8075 llvm_unreachable("va_arg for f80 not yet implemented");
8076 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8077 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8078 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8079 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8080 } else {
8081 llvm_unreachable("Unhandled argument type in LowerVAARG");
8082 }
8083
8084 if (ArgMode == 2) {
8085 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008086 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008087 !(DAG.getMachineFunction()
8088 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008089 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008090 }
8091
8092 // Insert VAARG_64 node into the DAG
8093 // VAARG_64 returns two values: Variable Argument Address, Chain
8094 SmallVector<SDValue, 11> InstOps;
8095 InstOps.push_back(Chain);
8096 InstOps.push_back(SrcPtr);
8097 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8098 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8099 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8100 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8101 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8102 VTs, &InstOps[0], InstOps.size(),
8103 MVT::i64,
8104 MachinePointerInfo(SV),
8105 /*Align=*/0,
8106 /*Volatile=*/false,
8107 /*ReadMem=*/true,
8108 /*WriteMem=*/true);
8109 Chain = VAARG.getValue(1);
8110
8111 // Load the next argument and return it
8112 return DAG.getLoad(ArgVT, dl,
8113 Chain,
8114 VAARG,
8115 MachinePointerInfo(),
8116 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008117}
8118
Dan Gohmand858e902010-04-17 15:26:15 +00008119SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008120 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008121 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008122 SDValue Chain = Op.getOperand(0);
8123 SDValue DstPtr = Op.getOperand(1);
8124 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008125 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8126 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008127 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008128
Chris Lattnere72f2022010-09-21 05:40:29 +00008129 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008130 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008131 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008132 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008133}
8134
Dan Gohman475871a2008-07-27 21:46:04 +00008135SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008136X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008137 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008138 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008139 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008140 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008141 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008142 case Intrinsic::x86_sse_comieq_ss:
8143 case Intrinsic::x86_sse_comilt_ss:
8144 case Intrinsic::x86_sse_comile_ss:
8145 case Intrinsic::x86_sse_comigt_ss:
8146 case Intrinsic::x86_sse_comige_ss:
8147 case Intrinsic::x86_sse_comineq_ss:
8148 case Intrinsic::x86_sse_ucomieq_ss:
8149 case Intrinsic::x86_sse_ucomilt_ss:
8150 case Intrinsic::x86_sse_ucomile_ss:
8151 case Intrinsic::x86_sse_ucomigt_ss:
8152 case Intrinsic::x86_sse_ucomige_ss:
8153 case Intrinsic::x86_sse_ucomineq_ss:
8154 case Intrinsic::x86_sse2_comieq_sd:
8155 case Intrinsic::x86_sse2_comilt_sd:
8156 case Intrinsic::x86_sse2_comile_sd:
8157 case Intrinsic::x86_sse2_comigt_sd:
8158 case Intrinsic::x86_sse2_comige_sd:
8159 case Intrinsic::x86_sse2_comineq_sd:
8160 case Intrinsic::x86_sse2_ucomieq_sd:
8161 case Intrinsic::x86_sse2_ucomilt_sd:
8162 case Intrinsic::x86_sse2_ucomile_sd:
8163 case Intrinsic::x86_sse2_ucomigt_sd:
8164 case Intrinsic::x86_sse2_ucomige_sd:
8165 case Intrinsic::x86_sse2_ucomineq_sd: {
8166 unsigned Opc = 0;
8167 ISD::CondCode CC = ISD::SETCC_INVALID;
8168 switch (IntNo) {
8169 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008170 case Intrinsic::x86_sse_comieq_ss:
8171 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008172 Opc = X86ISD::COMI;
8173 CC = ISD::SETEQ;
8174 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008175 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008176 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008177 Opc = X86ISD::COMI;
8178 CC = ISD::SETLT;
8179 break;
8180 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008181 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008182 Opc = X86ISD::COMI;
8183 CC = ISD::SETLE;
8184 break;
8185 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008186 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008187 Opc = X86ISD::COMI;
8188 CC = ISD::SETGT;
8189 break;
8190 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008191 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008192 Opc = X86ISD::COMI;
8193 CC = ISD::SETGE;
8194 break;
8195 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008196 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008197 Opc = X86ISD::COMI;
8198 CC = ISD::SETNE;
8199 break;
8200 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008201 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008202 Opc = X86ISD::UCOMI;
8203 CC = ISD::SETEQ;
8204 break;
8205 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008206 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008207 Opc = X86ISD::UCOMI;
8208 CC = ISD::SETLT;
8209 break;
8210 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008211 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008212 Opc = X86ISD::UCOMI;
8213 CC = ISD::SETLE;
8214 break;
8215 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008216 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008217 Opc = X86ISD::UCOMI;
8218 CC = ISD::SETGT;
8219 break;
8220 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008221 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008222 Opc = X86ISD::UCOMI;
8223 CC = ISD::SETGE;
8224 break;
8225 case Intrinsic::x86_sse_ucomineq_ss:
8226 case Intrinsic::x86_sse2_ucomineq_sd:
8227 Opc = X86ISD::UCOMI;
8228 CC = ISD::SETNE;
8229 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008230 }
Evan Cheng734503b2006-09-11 02:19:56 +00008231
Dan Gohman475871a2008-07-27 21:46:04 +00008232 SDValue LHS = Op.getOperand(1);
8233 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008234 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008235 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008236 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8237 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8238 DAG.getConstant(X86CC, MVT::i8), Cond);
8239 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008240 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008241 // ptest and testp intrinsics. The intrinsic these come from are designed to
8242 // return an integer value, not just an instruction so lower it to the ptest
8243 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008244 case Intrinsic::x86_sse41_ptestz:
8245 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008246 case Intrinsic::x86_sse41_ptestnzc:
8247 case Intrinsic::x86_avx_ptestz_256:
8248 case Intrinsic::x86_avx_ptestc_256:
8249 case Intrinsic::x86_avx_ptestnzc_256:
8250 case Intrinsic::x86_avx_vtestz_ps:
8251 case Intrinsic::x86_avx_vtestc_ps:
8252 case Intrinsic::x86_avx_vtestnzc_ps:
8253 case Intrinsic::x86_avx_vtestz_pd:
8254 case Intrinsic::x86_avx_vtestc_pd:
8255 case Intrinsic::x86_avx_vtestnzc_pd:
8256 case Intrinsic::x86_avx_vtestz_ps_256:
8257 case Intrinsic::x86_avx_vtestc_ps_256:
8258 case Intrinsic::x86_avx_vtestnzc_ps_256:
8259 case Intrinsic::x86_avx_vtestz_pd_256:
8260 case Intrinsic::x86_avx_vtestc_pd_256:
8261 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8262 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008263 unsigned X86CC = 0;
8264 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008265 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008266 case Intrinsic::x86_avx_vtestz_ps:
8267 case Intrinsic::x86_avx_vtestz_pd:
8268 case Intrinsic::x86_avx_vtestz_ps_256:
8269 case Intrinsic::x86_avx_vtestz_pd_256:
8270 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008271 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008272 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008273 // ZF = 1
8274 X86CC = X86::COND_E;
8275 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008276 case Intrinsic::x86_avx_vtestc_ps:
8277 case Intrinsic::x86_avx_vtestc_pd:
8278 case Intrinsic::x86_avx_vtestc_ps_256:
8279 case Intrinsic::x86_avx_vtestc_pd_256:
8280 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008281 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008282 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008283 // CF = 1
8284 X86CC = X86::COND_B;
8285 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008286 case Intrinsic::x86_avx_vtestnzc_ps:
8287 case Intrinsic::x86_avx_vtestnzc_pd:
8288 case Intrinsic::x86_avx_vtestnzc_ps_256:
8289 case Intrinsic::x86_avx_vtestnzc_pd_256:
8290 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008291 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008292 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008293 // ZF and CF = 0
8294 X86CC = X86::COND_A;
8295 break;
8296 }
Eric Christopherfd179292009-08-27 18:07:15 +00008297
Eric Christopher71c67532009-07-29 00:28:05 +00008298 SDValue LHS = Op.getOperand(1);
8299 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008300 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8301 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008302 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8303 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8304 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008305 }
Evan Cheng5759f972008-05-04 09:15:50 +00008306
8307 // Fix vector shift instructions where the last operand is a non-immediate
8308 // i32 value.
8309 case Intrinsic::x86_sse2_pslli_w:
8310 case Intrinsic::x86_sse2_pslli_d:
8311 case Intrinsic::x86_sse2_pslli_q:
8312 case Intrinsic::x86_sse2_psrli_w:
8313 case Intrinsic::x86_sse2_psrli_d:
8314 case Intrinsic::x86_sse2_psrli_q:
8315 case Intrinsic::x86_sse2_psrai_w:
8316 case Intrinsic::x86_sse2_psrai_d:
8317 case Intrinsic::x86_mmx_pslli_w:
8318 case Intrinsic::x86_mmx_pslli_d:
8319 case Intrinsic::x86_mmx_pslli_q:
8320 case Intrinsic::x86_mmx_psrli_w:
8321 case Intrinsic::x86_mmx_psrli_d:
8322 case Intrinsic::x86_mmx_psrli_q:
8323 case Intrinsic::x86_mmx_psrai_w:
8324 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008325 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008326 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008327 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008328
8329 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008330 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008331 switch (IntNo) {
8332 case Intrinsic::x86_sse2_pslli_w:
8333 NewIntNo = Intrinsic::x86_sse2_psll_w;
8334 break;
8335 case Intrinsic::x86_sse2_pslli_d:
8336 NewIntNo = Intrinsic::x86_sse2_psll_d;
8337 break;
8338 case Intrinsic::x86_sse2_pslli_q:
8339 NewIntNo = Intrinsic::x86_sse2_psll_q;
8340 break;
8341 case Intrinsic::x86_sse2_psrli_w:
8342 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8343 break;
8344 case Intrinsic::x86_sse2_psrli_d:
8345 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8346 break;
8347 case Intrinsic::x86_sse2_psrli_q:
8348 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8349 break;
8350 case Intrinsic::x86_sse2_psrai_w:
8351 NewIntNo = Intrinsic::x86_sse2_psra_w;
8352 break;
8353 case Intrinsic::x86_sse2_psrai_d:
8354 NewIntNo = Intrinsic::x86_sse2_psra_d;
8355 break;
8356 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008357 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008358 switch (IntNo) {
8359 case Intrinsic::x86_mmx_pslli_w:
8360 NewIntNo = Intrinsic::x86_mmx_psll_w;
8361 break;
8362 case Intrinsic::x86_mmx_pslli_d:
8363 NewIntNo = Intrinsic::x86_mmx_psll_d;
8364 break;
8365 case Intrinsic::x86_mmx_pslli_q:
8366 NewIntNo = Intrinsic::x86_mmx_psll_q;
8367 break;
8368 case Intrinsic::x86_mmx_psrli_w:
8369 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8370 break;
8371 case Intrinsic::x86_mmx_psrli_d:
8372 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8373 break;
8374 case Intrinsic::x86_mmx_psrli_q:
8375 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8376 break;
8377 case Intrinsic::x86_mmx_psrai_w:
8378 NewIntNo = Intrinsic::x86_mmx_psra_w;
8379 break;
8380 case Intrinsic::x86_mmx_psrai_d:
8381 NewIntNo = Intrinsic::x86_mmx_psra_d;
8382 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008383 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008384 }
8385 break;
8386 }
8387 }
Mon P Wangefa42202009-09-03 19:56:25 +00008388
8389 // The vector shift intrinsics with scalars uses 32b shift amounts but
8390 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8391 // to be zero.
8392 SDValue ShOps[4];
8393 ShOps[0] = ShAmt;
8394 ShOps[1] = DAG.getConstant(0, MVT::i32);
8395 if (ShAmtVT == MVT::v4i32) {
8396 ShOps[2] = DAG.getUNDEF(MVT::i32);
8397 ShOps[3] = DAG.getUNDEF(MVT::i32);
8398 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8399 } else {
8400 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008401// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008402 }
8403
Owen Andersone50ed302009-08-10 22:56:29 +00008404 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008405 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008406 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008407 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008408 Op.getOperand(1), ShAmt);
8409 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008410 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008411}
Evan Cheng72261582005-12-20 06:22:03 +00008412
Dan Gohmand858e902010-04-17 15:26:15 +00008413SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8414 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008415 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8416 MFI->setReturnAddressIsTaken(true);
8417
Bill Wendling64e87322009-01-16 19:25:27 +00008418 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008419 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008420
8421 if (Depth > 0) {
8422 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8423 SDValue Offset =
8424 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008425 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008426 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008427 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008428 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008429 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008430 }
8431
8432 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008433 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008434 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008435 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008436}
8437
Dan Gohmand858e902010-04-17 15:26:15 +00008438SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008439 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8440 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008441
Owen Andersone50ed302009-08-10 22:56:29 +00008442 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008443 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008444 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8445 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008446 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008447 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008448 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8449 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008450 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008451 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008452}
8453
Dan Gohman475871a2008-07-27 21:46:04 +00008454SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008455 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008456 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008457}
8458
Dan Gohmand858e902010-04-17 15:26:15 +00008459SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008460 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008461 SDValue Chain = Op.getOperand(0);
8462 SDValue Offset = Op.getOperand(1);
8463 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008464 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008465
Dan Gohmand8816272010-08-11 18:14:00 +00008466 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8467 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8468 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008469 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008470
Dan Gohmand8816272010-08-11 18:14:00 +00008471 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8472 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008473 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008474 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8475 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008476 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008477 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008478
Dale Johannesene4d209d2009-02-03 20:21:25 +00008479 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008480 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008481 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008482}
8483
Dan Gohman475871a2008-07-27 21:46:04 +00008484SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008485 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008486 SDValue Root = Op.getOperand(0);
8487 SDValue Trmp = Op.getOperand(1); // trampoline
8488 SDValue FPtr = Op.getOperand(2); // nested function
8489 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008490 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008491
Dan Gohman69de1932008-02-06 22:27:42 +00008492 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008493
8494 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008495 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008496
8497 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008498 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8499 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008500
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008501 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8502 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008503
8504 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8505
8506 // Load the pointer to the nested function into R11.
8507 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008508 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008509 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008510 Addr, MachinePointerInfo(TrmpAddr),
8511 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008512
Owen Anderson825b72b2009-08-11 20:47:22 +00008513 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8514 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008515 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8516 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008517 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008518
8519 // Load the 'nest' parameter value into R10.
8520 // R10 is specified in X86CallingConv.td
8521 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008522 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8523 DAG.getConstant(10, MVT::i64));
8524 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008525 Addr, MachinePointerInfo(TrmpAddr, 10),
8526 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008527
Owen Anderson825b72b2009-08-11 20:47:22 +00008528 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8529 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008530 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8531 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008532 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008533
8534 // Jump to the nested function.
8535 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008536 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8537 DAG.getConstant(20, MVT::i64));
8538 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008539 Addr, MachinePointerInfo(TrmpAddr, 20),
8540 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008541
8542 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8544 DAG.getConstant(22, MVT::i64));
8545 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008546 MachinePointerInfo(TrmpAddr, 22),
8547 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008548
Dan Gohman475871a2008-07-27 21:46:04 +00008549 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008551 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008552 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008553 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008554 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008555 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008556 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008557
8558 switch (CC) {
8559 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008560 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008561 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008562 case CallingConv::X86_StdCall: {
8563 // Pass 'nest' parameter in ECX.
8564 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008565 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008566
8567 // Check that ECX wasn't needed by an 'inreg' parameter.
8568 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008569 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008570
Chris Lattner58d74912008-03-12 17:45:29 +00008571 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008572 unsigned InRegCount = 0;
8573 unsigned Idx = 1;
8574
8575 for (FunctionType::param_iterator I = FTy->param_begin(),
8576 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008577 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008578 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008579 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008580
8581 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008582 report_fatal_error("Nest register in use - reduce number of inreg"
8583 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008584 }
8585 }
8586 break;
8587 }
8588 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008589 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008590 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008591 // Pass 'nest' parameter in EAX.
8592 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008593 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008594 break;
8595 }
8596
Dan Gohman475871a2008-07-27 21:46:04 +00008597 SDValue OutChains[4];
8598 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008599
Owen Anderson825b72b2009-08-11 20:47:22 +00008600 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8601 DAG.getConstant(10, MVT::i32));
8602 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008603
Chris Lattnera62fe662010-02-05 19:20:30 +00008604 // This is storing the opcode for MOV32ri.
8605 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008606 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008607 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008608 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008609 Trmp, MachinePointerInfo(TrmpAddr),
8610 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008611
Owen Anderson825b72b2009-08-11 20:47:22 +00008612 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8613 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008614 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8615 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008616 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008617
Chris Lattnera62fe662010-02-05 19:20:30 +00008618 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008619 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8620 DAG.getConstant(5, MVT::i32));
8621 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008622 MachinePointerInfo(TrmpAddr, 5),
8623 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008624
Owen Anderson825b72b2009-08-11 20:47:22 +00008625 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8626 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008627 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8628 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008629 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008630
Dan Gohman475871a2008-07-27 21:46:04 +00008631 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008632 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008633 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008634 }
8635}
8636
Dan Gohmand858e902010-04-17 15:26:15 +00008637SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8638 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008639 /*
8640 The rounding mode is in bits 11:10 of FPSR, and has the following
8641 settings:
8642 00 Round to nearest
8643 01 Round to -inf
8644 10 Round to +inf
8645 11 Round to 0
8646
8647 FLT_ROUNDS, on the other hand, expects the following:
8648 -1 Undefined
8649 0 Round to 0
8650 1 Round to nearest
8651 2 Round to +inf
8652 3 Round to -inf
8653
8654 To perform the conversion, we do:
8655 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8656 */
8657
8658 MachineFunction &MF = DAG.getMachineFunction();
8659 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008660 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008661 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008662 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008663 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008664
8665 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008666 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008667 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008668
Michael J. Spencerec38de22010-10-10 22:04:20 +00008669
Chris Lattner2156b792010-09-22 01:11:26 +00008670 MachineMemOperand *MMO =
8671 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8672 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008673
Chris Lattner2156b792010-09-22 01:11:26 +00008674 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8675 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8676 DAG.getVTList(MVT::Other),
8677 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008678
8679 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008680 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008681 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008682
8683 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008684 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008685 DAG.getNode(ISD::SRL, DL, MVT::i16,
8686 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008687 CWD, DAG.getConstant(0x800, MVT::i16)),
8688 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008689 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008690 DAG.getNode(ISD::SRL, DL, MVT::i16,
8691 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 CWD, DAG.getConstant(0x400, MVT::i16)),
8693 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008694
Dan Gohman475871a2008-07-27 21:46:04 +00008695 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008696 DAG.getNode(ISD::AND, DL, MVT::i16,
8697 DAG.getNode(ISD::ADD, DL, MVT::i16,
8698 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008699 DAG.getConstant(1, MVT::i16)),
8700 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008701
8702
Duncan Sands83ec4b62008-06-06 12:08:01 +00008703 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008704 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008705}
8706
Dan Gohmand858e902010-04-17 15:26:15 +00008707SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008708 EVT VT = Op.getValueType();
8709 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008710 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008711 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008712
8713 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008714 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008715 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008717 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008718 }
Evan Cheng18efe262007-12-14 02:13:44 +00008719
Evan Cheng152804e2007-12-14 08:30:15 +00008720 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008721 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008722 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008723
8724 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008725 SDValue Ops[] = {
8726 Op,
8727 DAG.getConstant(NumBits+NumBits-1, OpVT),
8728 DAG.getConstant(X86::COND_E, MVT::i8),
8729 Op.getValue(1)
8730 };
8731 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008732
8733 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008734 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008735
Owen Anderson825b72b2009-08-11 20:47:22 +00008736 if (VT == MVT::i8)
8737 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008738 return Op;
8739}
8740
Dan Gohmand858e902010-04-17 15:26:15 +00008741SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008742 EVT VT = Op.getValueType();
8743 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008744 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008745 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008746
8747 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008748 if (VT == MVT::i8) {
8749 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008750 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008751 }
Evan Cheng152804e2007-12-14 08:30:15 +00008752
8753 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008754 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008755 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008756
8757 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008758 SDValue Ops[] = {
8759 Op,
8760 DAG.getConstant(NumBits, OpVT),
8761 DAG.getConstant(X86::COND_E, MVT::i8),
8762 Op.getValue(1)
8763 };
8764 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008765
Owen Anderson825b72b2009-08-11 20:47:22 +00008766 if (VT == MVT::i8)
8767 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008768 return Op;
8769}
8770
Dan Gohmand858e902010-04-17 15:26:15 +00008771SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008772 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008773 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008774 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008775
Mon P Wangaf9b9522008-12-18 21:42:19 +00008776 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8777 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8778 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8779 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8780 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8781 //
8782 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8783 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8784 // return AloBlo + AloBhi + AhiBlo;
8785
8786 SDValue A = Op.getOperand(0);
8787 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008788
Dale Johannesene4d209d2009-02-03 20:21:25 +00008789 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008790 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8791 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008792 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008793 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8794 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008795 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008796 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008797 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008798 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008799 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008800 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008801 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008802 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008803 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008804 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008805 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8806 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008807 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008808 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8809 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008810 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8811 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008812 return Res;
8813}
8814
Nadav Rotem43012222011-05-11 08:12:09 +00008815SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
8816
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008817 EVT VT = Op.getValueType();
8818 DebugLoc dl = Op.getDebugLoc();
8819 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +00008820 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008821
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008822 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008823
Nadav Rotem43012222011-05-11 08:12:09 +00008824 // Must have SSE2.
8825 if (!Subtarget->hasSSE2()) return SDValue();
Nate Begeman51409212010-07-28 00:21:48 +00008826
Nadav Rotem43012222011-05-11 08:12:09 +00008827 // Optimize shl/srl/sra with constant shift amount.
8828 if (isSplatVector(Amt.getNode())) {
8829 SDValue SclrAmt = Amt->getOperand(0);
8830 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
8831 uint64_t ShiftAmt = C->getZExtValue();
8832
8833 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
8834 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8835 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8836 R, DAG.getConstant(ShiftAmt, MVT::i32));
8837
8838 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
8839 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8840 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8841 R, DAG.getConstant(ShiftAmt, MVT::i32));
8842
8843 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
8844 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8845 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8846 R, DAG.getConstant(ShiftAmt, MVT::i32));
8847
8848 if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
8849 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8850 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8851 R, DAG.getConstant(ShiftAmt, MVT::i32));
8852
8853 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
8854 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8855 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8856 R, DAG.getConstant(ShiftAmt, MVT::i32));
8857
8858 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
8859 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8860 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8861 R, DAG.getConstant(ShiftAmt, MVT::i32));
8862
8863 if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
8864 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8865 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8866 R, DAG.getConstant(ShiftAmt, MVT::i32));
8867
8868 if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
8869 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8870 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8871 R, DAG.getConstant(ShiftAmt, MVT::i32));
8872 }
8873 }
8874
8875 // Lower SHL with variable shift amount.
8876 // Cannot lower SHL without SSE4.1 or later.
8877 if (!Subtarget->hasSSE41()) return SDValue();
8878
8879 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008880 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8881 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8882 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8883
8884 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008885
Nate Begeman51409212010-07-28 00:21:48 +00008886 std::vector<Constant*> CV(4, CI);
8887 Constant *C = ConstantVector::get(CV);
8888 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8889 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008890 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008891 false, false, 16);
8892
8893 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008894 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008895 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8896 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8897 }
Nadav Rotem43012222011-05-11 08:12:09 +00008898 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Nate Begeman51409212010-07-28 00:21:48 +00008899 // a = a << 5;
8900 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8901 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8902 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8903
8904 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8905 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8906
8907 std::vector<Constant*> CVM1(16, CM1);
8908 std::vector<Constant*> CVM2(16, CM2);
8909 Constant *C = ConstantVector::get(CVM1);
8910 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8911 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008912 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008913 false, false, 16);
8914
8915 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8916 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8917 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8918 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8919 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008920 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008921 // a += a
8922 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008923
Nate Begeman51409212010-07-28 00:21:48 +00008924 C = ConstantVector::get(CVM2);
8925 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8926 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008927 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008928 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008929
Nate Begeman51409212010-07-28 00:21:48 +00008930 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8931 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8932 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8933 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8934 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008935 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008936 // a += a
8937 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008938
Nate Begeman51409212010-07-28 00:21:48 +00008939 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008940 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008941 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8942 return R;
8943 }
8944 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008945}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008946
Dan Gohmand858e902010-04-17 15:26:15 +00008947SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008948 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8949 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008950 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8951 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008952 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008953 SDValue LHS = N->getOperand(0);
8954 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008955 unsigned BaseOp = 0;
8956 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008957 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008958 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008959 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008960 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008961 // A subtract of one will be selected as a INC. Note that INC doesn't
8962 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008963 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8964 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008965 BaseOp = X86ISD::INC;
8966 Cond = X86::COND_O;
8967 break;
8968 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008969 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008970 Cond = X86::COND_O;
8971 break;
8972 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008973 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008974 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008975 break;
8976 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008977 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8978 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008979 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8980 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008981 BaseOp = X86ISD::DEC;
8982 Cond = X86::COND_O;
8983 break;
8984 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008985 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008986 Cond = X86::COND_O;
8987 break;
8988 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008989 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008990 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008991 break;
8992 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008993 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008994 Cond = X86::COND_O;
8995 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008996 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8997 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8998 MVT::i32);
8999 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009000
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009001 SDValue SetCC =
9002 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9003 DAG.getConstant(X86::COND_O, MVT::i32),
9004 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009005
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009006 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9007 return Sum;
9008 }
Bill Wendling74c37652008-12-09 22:08:41 +00009009 }
Bill Wendling3fafd932008-11-26 22:37:40 +00009010
Bill Wendling61edeb52008-12-02 01:06:39 +00009011 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009013 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00009014
Bill Wendling61edeb52008-12-02 01:06:39 +00009015 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00009016 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
9017 DAG.getConstant(Cond, MVT::i32),
9018 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00009019
Bill Wendling61edeb52008-12-02 01:06:39 +00009020 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
9021 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00009022}
9023
Eric Christopher9a9d2752010-07-22 02:48:34 +00009024SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
9025 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009026
Eric Christopherb6729dc2010-08-04 23:03:04 +00009027 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00009028 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00009029 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00009030 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00009031 SDValue Ops[] = {
9032 DAG.getRegister(X86::ESP, MVT::i32), // Base
9033 DAG.getTargetConstant(1, MVT::i8), // Scale
9034 DAG.getRegister(0, MVT::i32), // Index
9035 DAG.getTargetConstant(0, MVT::i32), // Disp
9036 DAG.getRegister(0, MVT::i32), // Segment.
9037 Zero,
9038 Chain
9039 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00009040 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00009041 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
9042 array_lengthof(Ops));
9043 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00009044 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00009045
Eric Christopher9a9d2752010-07-22 02:48:34 +00009046 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00009047 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00009048 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009049
Chris Lattner132929a2010-08-14 17:26:09 +00009050 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
9051 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
9052 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
9053 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00009054
Chris Lattner132929a2010-08-14 17:26:09 +00009055 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
9056 if (!Op1 && !Op2 && !Op3 && Op4)
9057 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009058
Chris Lattner132929a2010-08-14 17:26:09 +00009059 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
9060 if (Op1 && !Op2 && !Op3 && !Op4)
9061 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009062
9063 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00009064 // (MFENCE)>;
9065 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00009066}
9067
Dan Gohmand858e902010-04-17 15:26:15 +00009068SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009069 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009070 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00009071 unsigned Reg = 0;
9072 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00009073 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009074 default:
9075 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009076 case MVT::i8: Reg = X86::AL; size = 1; break;
9077 case MVT::i16: Reg = X86::AX; size = 2; break;
9078 case MVT::i32: Reg = X86::EAX; size = 4; break;
9079 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00009080 assert(Subtarget->is64Bit() && "Node not type legal!");
9081 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00009082 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009083 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009084 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00009085 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00009086 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009087 Op.getOperand(1),
9088 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00009089 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00009090 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009091 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009092 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
9093 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
9094 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00009095 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00009096 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00009097 return cpOut;
9098}
9099
Duncan Sands1607f052008-12-01 11:39:25 +00009100SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009101 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00009102 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009103 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009104 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009105 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009106 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009107 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9108 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009109 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009110 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9111 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009112 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009113 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009114 rdx.getValue(1)
9115 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009116 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009117}
9118
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009119SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009120 SelectionDAG &DAG) const {
9121 EVT SrcVT = Op.getOperand(0).getValueType();
9122 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009123 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9124 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009125 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009126 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009127 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009128 // i64 <=> MMX conversions are Legal.
9129 if (SrcVT==MVT::i64 && DstVT.isVector())
9130 return Op;
9131 if (DstVT==MVT::i64 && SrcVT.isVector())
9132 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009133 // MMX <=> MMX conversions are Legal.
9134 if (SrcVT.isVector() && DstVT.isVector())
9135 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009136 // All other conversions need to be expanded.
9137 return SDValue();
9138}
Chris Lattner5b856542010-12-20 00:59:46 +00009139
Dan Gohmand858e902010-04-17 15:26:15 +00009140SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009141 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009142 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009143 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009144 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009145 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009146 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009147 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009148 Node->getOperand(0),
9149 Node->getOperand(1), negOp,
9150 cast<AtomicSDNode>(Node)->getSrcValue(),
9151 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009152}
9153
Chris Lattner5b856542010-12-20 00:59:46 +00009154static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9155 EVT VT = Op.getNode()->getValueType(0);
9156
9157 // Let legalize expand this if it isn't a legal type yet.
9158 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9159 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009160
Chris Lattner5b856542010-12-20 00:59:46 +00009161 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009162
Chris Lattner5b856542010-12-20 00:59:46 +00009163 unsigned Opc;
9164 bool ExtraOp = false;
9165 switch (Op.getOpcode()) {
9166 default: assert(0 && "Invalid code");
9167 case ISD::ADDC: Opc = X86ISD::ADD; break;
9168 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9169 case ISD::SUBC: Opc = X86ISD::SUB; break;
9170 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9171 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009172
Chris Lattner5b856542010-12-20 00:59:46 +00009173 if (!ExtraOp)
9174 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9175 Op.getOperand(1));
9176 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9177 Op.getOperand(1), Op.getOperand(2));
9178}
9179
Evan Cheng0db9fe62006-04-25 20:13:52 +00009180/// LowerOperation - Provide custom lowering hooks for some operations.
9181///
Dan Gohmand858e902010-04-17 15:26:15 +00009182SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009183 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009184 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009185 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009186 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9187 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009188 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009189 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009190 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9191 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9192 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009193 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009194 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009195 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9196 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9197 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009198 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009199 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009200 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009201 case ISD::SHL_PARTS:
9202 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +00009203 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009204 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009205 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009206 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009207 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009208 case ISD::FABS: return LowerFABS(Op, DAG);
9209 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009210 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009211 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009212 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009213 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009214 case ISD::SELECT: return LowerSELECT(Op, DAG);
9215 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009216 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009217 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009218 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009219 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009220 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009221 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9222 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009223 case ISD::FRAME_TO_ARGS_OFFSET:
9224 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009225 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009226 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009227 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009228 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009229 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9230 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009231 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +00009232 case ISD::SRA:
9233 case ISD::SRL:
9234 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009235 case ISD::SADDO:
9236 case ISD::UADDO:
9237 case ISD::SSUBO:
9238 case ISD::USUBO:
9239 case ISD::SMULO:
9240 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009241 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009242 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009243 case ISD::ADDC:
9244 case ISD::ADDE:
9245 case ISD::SUBC:
9246 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009247 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009248}
9249
Duncan Sands1607f052008-12-01 11:39:25 +00009250void X86TargetLowering::
9251ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009252 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009253 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009254 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009255 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009256
9257 SDValue Chain = Node->getOperand(0);
9258 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009259 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009260 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009261 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009262 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009263 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009264 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009265 SDValue Result =
9266 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9267 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009268 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009269 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009270 Results.push_back(Result.getValue(2));
9271}
9272
Duncan Sands126d9072008-07-04 11:47:58 +00009273/// ReplaceNodeResults - Replace a node with an illegal result type
9274/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009275void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9276 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009277 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009278 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009279 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009280 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009281 assert(false && "Do not know how to custom type legalize this operation!");
9282 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009283 case ISD::ADDC:
9284 case ISD::ADDE:
9285 case ISD::SUBC:
9286 case ISD::SUBE:
9287 // We don't want to expand or promote these.
9288 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009289 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009290 std::pair<SDValue,SDValue> Vals =
9291 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009292 SDValue FIST = Vals.first, StackSlot = Vals.second;
9293 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009294 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009295 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009296 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9297 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009298 }
9299 return;
9300 }
9301 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009302 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009303 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009304 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009305 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009306 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009307 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009308 eax.getValue(2));
9309 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9310 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009311 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009312 Results.push_back(edx.getValue(1));
9313 return;
9314 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009315 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009316 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009317 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009318 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009319 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9320 DAG.getConstant(0, MVT::i32));
9321 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9322 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009323 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9324 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009325 cpInL.getValue(1));
9326 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009327 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9328 DAG.getConstant(0, MVT::i32));
9329 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9330 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009331 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009332 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009333 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009334 swapInL.getValue(1));
9335 SDValue Ops[] = { swapInH.getValue(0),
9336 N->getOperand(1),
9337 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009338 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009339 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9340 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9341 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009342 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009343 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009344 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009345 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009346 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009347 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009348 Results.push_back(cpOutH.getValue(1));
9349 return;
9350 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009351 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009352 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9353 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009354 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009355 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9356 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009357 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009358 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9359 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009360 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009361 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9362 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009363 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009364 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9365 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009366 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009367 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9368 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009369 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009370 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9371 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009372 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009373}
9374
Evan Cheng72261582005-12-20 06:22:03 +00009375const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9376 switch (Opcode) {
9377 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009378 case X86ISD::BSF: return "X86ISD::BSF";
9379 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009380 case X86ISD::SHLD: return "X86ISD::SHLD";
9381 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009382 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009383 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009384 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009385 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009386 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009387 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009388 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9389 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9390 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009391 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009392 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009393 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009394 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009395 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009396 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009397 case X86ISD::COMI: return "X86ISD::COMI";
9398 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009399 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009400 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +00009401 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
9402 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +00009403 case X86ISD::CMOV: return "X86ISD::CMOV";
9404 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009405 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009406 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9407 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009408 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009409 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009410 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009411 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009412 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009413 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9414 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009415 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009416 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009417 case X86ISD::PANDN: return "X86ISD::PANDN";
9418 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9419 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9420 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009421 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009422 case X86ISD::FMAX: return "X86ISD::FMAX";
9423 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009424 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9425 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009426 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009427 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009428 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009429 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009430 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009431 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9432 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009433 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9434 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9435 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9436 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9437 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9438 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009439 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9440 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009441 case X86ISD::VSHL: return "X86ISD::VSHL";
9442 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009443 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9444 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9445 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9446 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9447 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9448 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9449 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9450 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9451 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9452 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009453 case X86ISD::ADD: return "X86ISD::ADD";
9454 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009455 case X86ISD::ADC: return "X86ISD::ADC";
9456 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009457 case X86ISD::SMUL: return "X86ISD::SMUL";
9458 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009459 case X86ISD::INC: return "X86ISD::INC";
9460 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009461 case X86ISD::OR: return "X86ISD::OR";
9462 case X86ISD::XOR: return "X86ISD::XOR";
9463 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009464 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009465 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009466 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009467 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9468 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9469 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9470 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9471 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9472 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9473 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9474 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9475 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009476 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009477 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009478 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009479 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9480 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009481 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9482 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9483 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9484 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9485 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9486 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9487 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9488 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9489 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009490 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9491 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9492 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9493 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009494 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9495 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9496 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9497 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9498 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9499 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9500 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9501 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9502 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9503 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009504 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009505 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009506 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009507 }
9508}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009509
Chris Lattnerc9addb72007-03-30 23:15:24 +00009510// isLegalAddressingMode - Return true if the addressing mode represented
9511// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009512bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009513 const Type *Ty) const {
9514 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009515 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009516 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009517
Chris Lattnerc9addb72007-03-30 23:15:24 +00009518 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009519 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009520 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009521
Chris Lattnerc9addb72007-03-30 23:15:24 +00009522 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009523 unsigned GVFlags =
9524 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009525
Chris Lattnerdfed4132009-07-10 07:38:24 +00009526 // If a reference to this global requires an extra load, we can't fold it.
9527 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009528 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009529
Chris Lattnerdfed4132009-07-10 07:38:24 +00009530 // If BaseGV requires a register for the PIC base, we cannot also have a
9531 // BaseReg specified.
9532 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009533 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009534
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009535 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009536 if ((M != CodeModel::Small || R != Reloc::Static) &&
9537 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009538 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009539 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009540
Chris Lattnerc9addb72007-03-30 23:15:24 +00009541 switch (AM.Scale) {
9542 case 0:
9543 case 1:
9544 case 2:
9545 case 4:
9546 case 8:
9547 // These scales always work.
9548 break;
9549 case 3:
9550 case 5:
9551 case 9:
9552 // These scales are formed with basereg+scalereg. Only accept if there is
9553 // no basereg yet.
9554 if (AM.HasBaseReg)
9555 return false;
9556 break;
9557 default: // Other stuff never works.
9558 return false;
9559 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009560
Chris Lattnerc9addb72007-03-30 23:15:24 +00009561 return true;
9562}
9563
9564
Evan Cheng2bd122c2007-10-26 01:56:11 +00009565bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009566 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009567 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009568 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9569 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009570 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009571 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009572 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009573}
9574
Owen Andersone50ed302009-08-10 22:56:29 +00009575bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009576 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009577 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009578 unsigned NumBits1 = VT1.getSizeInBits();
9579 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009580 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009581 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009582 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009583}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009584
Dan Gohman97121ba2009-04-08 00:15:30 +00009585bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009586 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009587 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009588}
9589
Owen Andersone50ed302009-08-10 22:56:29 +00009590bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009591 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009593}
9594
Owen Andersone50ed302009-08-10 22:56:29 +00009595bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009596 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009597 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009598}
9599
Evan Cheng60c07e12006-07-05 22:17:51 +00009600/// isShuffleMaskLegal - Targets can use this to indicate that they only
9601/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9602/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9603/// are assumed to be legal.
9604bool
Eric Christopherfd179292009-08-27 18:07:15 +00009605X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009606 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009607 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009608 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009609 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009610
Nate Begemana09008b2009-10-19 02:17:23 +00009611 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009612 return (VT.getVectorNumElements() == 2 ||
9613 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9614 isMOVLMask(M, VT) ||
9615 isSHUFPMask(M, VT) ||
9616 isPSHUFDMask(M, VT) ||
9617 isPSHUFHWMask(M, VT) ||
9618 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009619 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009620 isUNPCKLMask(M, VT) ||
9621 isUNPCKHMask(M, VT) ||
9622 isUNPCKL_v_undef_Mask(M, VT) ||
9623 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009624}
9625
Dan Gohman7d8143f2008-04-09 20:09:42 +00009626bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009627X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009628 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009629 unsigned NumElts = VT.getVectorNumElements();
9630 // FIXME: This collection of masks seems suspect.
9631 if (NumElts == 2)
9632 return true;
9633 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9634 return (isMOVLMask(Mask, VT) ||
9635 isCommutedMOVLMask(Mask, VT, true) ||
9636 isSHUFPMask(Mask, VT) ||
9637 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009638 }
9639 return false;
9640}
9641
9642//===----------------------------------------------------------------------===//
9643// X86 Scheduler Hooks
9644//===----------------------------------------------------------------------===//
9645
Mon P Wang63307c32008-05-05 19:05:59 +00009646// private utility function
9647MachineBasicBlock *
9648X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9649 MachineBasicBlock *MBB,
9650 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009651 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009652 unsigned LoadOpc,
9653 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009654 unsigned notOpc,
9655 unsigned EAXreg,
9656 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009657 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009658 // For the atomic bitwise operator, we generate
9659 // thisMBB:
9660 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009661 // ld t1 = [bitinstr.addr]
9662 // op t2 = t1, [bitinstr.val]
9663 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009664 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9665 // bz newMBB
9666 // fallthrough -->nextMBB
9667 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9668 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009669 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009670 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009671
Mon P Wang63307c32008-05-05 19:05:59 +00009672 /// First build the CFG
9673 MachineFunction *F = MBB->getParent();
9674 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009675 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9676 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9677 F->insert(MBBIter, newMBB);
9678 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009679
Dan Gohman14152b42010-07-06 20:24:04 +00009680 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9681 nextMBB->splice(nextMBB->begin(), thisMBB,
9682 llvm::next(MachineBasicBlock::iterator(bInstr)),
9683 thisMBB->end());
9684 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009685
Mon P Wang63307c32008-05-05 19:05:59 +00009686 // Update thisMBB to fall through to newMBB
9687 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009688
Mon P Wang63307c32008-05-05 19:05:59 +00009689 // newMBB jumps to itself and fall through to nextMBB
9690 newMBB->addSuccessor(nextMBB);
9691 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009692
Mon P Wang63307c32008-05-05 19:05:59 +00009693 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009694 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009695 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009696 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009697 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009698 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009699 int numArgs = bInstr->getNumOperands() - 1;
9700 for (int i=0; i < numArgs; ++i)
9701 argOpers[i] = &bInstr->getOperand(i+1);
9702
9703 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009704 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009705 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009706
Dale Johannesen140be2d2008-08-19 18:47:28 +00009707 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009708 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009709 for (int i=0; i <= lastAddrIndx; ++i)
9710 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009711
Dale Johannesen140be2d2008-08-19 18:47:28 +00009712 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009713 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009714 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009715 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009716 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009717 tt = t1;
9718
Dale Johannesen140be2d2008-08-19 18:47:28 +00009719 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009720 assert((argOpers[valArgIndx]->isReg() ||
9721 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009722 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009723 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009724 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009725 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009726 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009727 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009728 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009729
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009730 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009731 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009732
Dale Johannesene4d209d2009-02-03 20:21:25 +00009733 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009734 for (int i=0; i <= lastAddrIndx; ++i)
9735 (*MIB).addOperand(*argOpers[i]);
9736 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009737 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009738 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9739 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009740
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009741 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009742 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009743
Mon P Wang63307c32008-05-05 19:05:59 +00009744 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009745 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009746
Dan Gohman14152b42010-07-06 20:24:04 +00009747 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009748 return nextMBB;
9749}
9750
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009751// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009752MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009753X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9754 MachineBasicBlock *MBB,
9755 unsigned regOpcL,
9756 unsigned regOpcH,
9757 unsigned immOpcL,
9758 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009759 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009760 // For the atomic bitwise operator, we generate
9761 // thisMBB (instructions are in pairs, except cmpxchg8b)
9762 // ld t1,t2 = [bitinstr.addr]
9763 // newMBB:
9764 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9765 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009766 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009767 // mov ECX, EBX <- t5, t6
9768 // mov EAX, EDX <- t1, t2
9769 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9770 // mov t3, t4 <- EAX, EDX
9771 // bz newMBB
9772 // result in out1, out2
9773 // fallthrough -->nextMBB
9774
9775 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9776 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009777 const unsigned NotOpc = X86::NOT32r;
9778 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9779 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9780 MachineFunction::iterator MBBIter = MBB;
9781 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009782
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009783 /// First build the CFG
9784 MachineFunction *F = MBB->getParent();
9785 MachineBasicBlock *thisMBB = MBB;
9786 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9787 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9788 F->insert(MBBIter, newMBB);
9789 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009790
Dan Gohman14152b42010-07-06 20:24:04 +00009791 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9792 nextMBB->splice(nextMBB->begin(), thisMBB,
9793 llvm::next(MachineBasicBlock::iterator(bInstr)),
9794 thisMBB->end());
9795 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009796
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009797 // Update thisMBB to fall through to newMBB
9798 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009799
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009800 // newMBB jumps to itself and fall through to nextMBB
9801 newMBB->addSuccessor(nextMBB);
9802 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009803
Dale Johannesene4d209d2009-02-03 20:21:25 +00009804 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009805 // Insert instructions into newMBB based on incoming instruction
9806 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009807 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009808 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009809 MachineOperand& dest1Oper = bInstr->getOperand(0);
9810 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009811 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9812 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009813 argOpers[i] = &bInstr->getOperand(i+2);
9814
Dan Gohman71ea4e52010-05-14 21:01:44 +00009815 // We use some of the operands multiple times, so conservatively just
9816 // clear any kill flags that might be present.
9817 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9818 argOpers[i]->setIsKill(false);
9819 }
9820
Evan Chengad5b52f2010-01-08 19:14:57 +00009821 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009822 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009823
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009824 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009825 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009826 for (int i=0; i <= lastAddrIndx; ++i)
9827 (*MIB).addOperand(*argOpers[i]);
9828 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009829 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009830 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009831 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009832 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009833 MachineOperand newOp3 = *(argOpers[3]);
9834 if (newOp3.isImm())
9835 newOp3.setImm(newOp3.getImm()+4);
9836 else
9837 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009838 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009839 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009840
9841 // t3/4 are defined later, at the bottom of the loop
9842 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9843 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009844 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009845 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009847 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9848
Evan Cheng306b4ca2010-01-08 23:41:50 +00009849 // The subsequent operations should be using the destination registers of
9850 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009851 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009852 t1 = F->getRegInfo().createVirtualRegister(RC);
9853 t2 = F->getRegInfo().createVirtualRegister(RC);
9854 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9855 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009856 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009857 t1 = dest1Oper.getReg();
9858 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009859 }
9860
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009861 int valArgIndx = lastAddrIndx + 1;
9862 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009863 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009864 "invalid operand");
9865 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9866 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009867 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009868 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009869 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009870 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009871 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009872 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009873 (*MIB).addOperand(*argOpers[valArgIndx]);
9874 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009875 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009876 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009877 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009878 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009879 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009880 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009881 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009882 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009883 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009884 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009885
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009886 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009887 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009888 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009889 MIB.addReg(t2);
9890
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009891 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009892 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009893 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009894 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009895
Dale Johannesene4d209d2009-02-03 20:21:25 +00009896 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009897 for (int i=0; i <= lastAddrIndx; ++i)
9898 (*MIB).addOperand(*argOpers[i]);
9899
9900 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009901 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9902 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009903
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009904 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009905 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009906 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009907 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009908
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009909 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009910 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009911
Dan Gohman14152b42010-07-06 20:24:04 +00009912 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009913 return nextMBB;
9914}
9915
9916// private utility function
9917MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009918X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9919 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009920 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009921 // For the atomic min/max operator, we generate
9922 // thisMBB:
9923 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009924 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009925 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009926 // cmp t1, t2
9927 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009928 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009929 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9930 // bz newMBB
9931 // fallthrough -->nextMBB
9932 //
9933 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9934 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009935 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009936 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009937
Mon P Wang63307c32008-05-05 19:05:59 +00009938 /// First build the CFG
9939 MachineFunction *F = MBB->getParent();
9940 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009941 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9942 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9943 F->insert(MBBIter, newMBB);
9944 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009945
Dan Gohman14152b42010-07-06 20:24:04 +00009946 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9947 nextMBB->splice(nextMBB->begin(), thisMBB,
9948 llvm::next(MachineBasicBlock::iterator(mInstr)),
9949 thisMBB->end());
9950 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009951
Mon P Wang63307c32008-05-05 19:05:59 +00009952 // Update thisMBB to fall through to newMBB
9953 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009954
Mon P Wang63307c32008-05-05 19:05:59 +00009955 // newMBB jumps to newMBB and fall through to nextMBB
9956 newMBB->addSuccessor(nextMBB);
9957 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009958
Dale Johannesene4d209d2009-02-03 20:21:25 +00009959 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009960 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009961 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009962 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009963 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009964 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009965 int numArgs = mInstr->getNumOperands() - 1;
9966 for (int i=0; i < numArgs; ++i)
9967 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009968
Mon P Wang63307c32008-05-05 19:05:59 +00009969 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009970 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009971 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009972
Mon P Wangab3e7472008-05-05 22:56:23 +00009973 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009974 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009975 for (int i=0; i <= lastAddrIndx; ++i)
9976 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009977
Mon P Wang63307c32008-05-05 19:05:59 +00009978 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009979 assert((argOpers[valArgIndx]->isReg() ||
9980 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009981 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009982
9983 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009984 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009985 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009986 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009987 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009988 (*MIB).addOperand(*argOpers[valArgIndx]);
9989
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009990 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009991 MIB.addReg(t1);
9992
Dale Johannesene4d209d2009-02-03 20:21:25 +00009993 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009994 MIB.addReg(t1);
9995 MIB.addReg(t2);
9996
9997 // Generate movc
9998 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009999 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000010000 MIB.addReg(t2);
10001 MIB.addReg(t1);
10002
10003 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000010004 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000010005 for (int i=0; i <= lastAddrIndx; ++i)
10006 (*MIB).addOperand(*argOpers[i]);
10007 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000010008 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000010009 (*MIB).setMemRefs(mInstr->memoperands_begin(),
10010 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000010011
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010012 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000010013 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000010014
Mon P Wang63307c32008-05-05 19:05:59 +000010015 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010016 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000010017
Dan Gohman14152b42010-07-06 20:24:04 +000010018 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000010019 return nextMBB;
10020}
10021
Eric Christopherf83a5de2009-08-27 18:08:16 +000010022// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010023// or XMM0_V32I8 in AVX all of this code can be replaced with that
10024// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010025MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000010026X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000010027 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010028 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
10029 "Target must have SSE4.2 or AVX features enabled");
10030
Eric Christopherb120ab42009-08-18 22:50:32 +000010031 DebugLoc dl = MI->getDebugLoc();
10032 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000010033 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010034 if (!Subtarget->hasAVX()) {
10035 if (memArg)
10036 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
10037 else
10038 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
10039 } else {
10040 if (memArg)
10041 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
10042 else
10043 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
10044 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010045
Eric Christopher41c902f2010-11-30 08:20:21 +000010046 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000010047 for (unsigned i = 0; i < numArgs; ++i) {
10048 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000010049 if (!(Op.isReg() && Op.isImplicit()))
10050 MIB.addOperand(Op);
10051 }
Eric Christopher41c902f2010-11-30 08:20:21 +000010052 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000010053 .addReg(X86::XMM0);
10054
Dan Gohman14152b42010-07-06 20:24:04 +000010055 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000010056 return BB;
10057}
10058
10059MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000010060X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010061 DebugLoc dl = MI->getDebugLoc();
10062 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010063
Eric Christopher228232b2010-11-30 07:20:12 +000010064 // Address into RAX/EAX, other two args into ECX, EDX.
10065 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
10066 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
10067 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
10068 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000010069 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010070
Eric Christopher228232b2010-11-30 07:20:12 +000010071 unsigned ValOps = X86::AddrNumOperands;
10072 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10073 .addReg(MI->getOperand(ValOps).getReg());
10074 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
10075 .addReg(MI->getOperand(ValOps+1).getReg());
10076
10077 // The instruction doesn't actually take any operands though.
10078 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010079
Eric Christopher228232b2010-11-30 07:20:12 +000010080 MI->eraseFromParent(); // The pseudo is gone now.
10081 return BB;
10082}
10083
10084MachineBasicBlock *
10085X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000010086 DebugLoc dl = MI->getDebugLoc();
10087 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010088
Eric Christopher228232b2010-11-30 07:20:12 +000010089 // First arg in ECX, the second in EAX.
10090 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
10091 .addReg(MI->getOperand(0).getReg());
10092 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
10093 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010094
Eric Christopher228232b2010-11-30 07:20:12 +000010095 // The instruction doesn't actually take any operands though.
10096 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010097
Eric Christopher228232b2010-11-30 07:20:12 +000010098 MI->eraseFromParent(); // The pseudo is gone now.
10099 return BB;
10100}
10101
10102MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000010103X86TargetLowering::EmitVAARG64WithCustomInserter(
10104 MachineInstr *MI,
10105 MachineBasicBlock *MBB) const {
10106 // Emit va_arg instruction on X86-64.
10107
10108 // Operands to this pseudo-instruction:
10109 // 0 ) Output : destination address (reg)
10110 // 1-5) Input : va_list address (addr, i64mem)
10111 // 6 ) ArgSize : Size (in bytes) of vararg type
10112 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10113 // 8 ) Align : Alignment of type
10114 // 9 ) EFLAGS (implicit-def)
10115
10116 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10117 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10118
10119 unsigned DestReg = MI->getOperand(0).getReg();
10120 MachineOperand &Base = MI->getOperand(1);
10121 MachineOperand &Scale = MI->getOperand(2);
10122 MachineOperand &Index = MI->getOperand(3);
10123 MachineOperand &Disp = MI->getOperand(4);
10124 MachineOperand &Segment = MI->getOperand(5);
10125 unsigned ArgSize = MI->getOperand(6).getImm();
10126 unsigned ArgMode = MI->getOperand(7).getImm();
10127 unsigned Align = MI->getOperand(8).getImm();
10128
10129 // Memory Reference
10130 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10131 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10132 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10133
10134 // Machine Information
10135 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10136 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10137 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10138 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10139 DebugLoc DL = MI->getDebugLoc();
10140
10141 // struct va_list {
10142 // i32 gp_offset
10143 // i32 fp_offset
10144 // i64 overflow_area (address)
10145 // i64 reg_save_area (address)
10146 // }
10147 // sizeof(va_list) = 24
10148 // alignment(va_list) = 8
10149
10150 unsigned TotalNumIntRegs = 6;
10151 unsigned TotalNumXMMRegs = 8;
10152 bool UseGPOffset = (ArgMode == 1);
10153 bool UseFPOffset = (ArgMode == 2);
10154 unsigned MaxOffset = TotalNumIntRegs * 8 +
10155 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10156
10157 /* Align ArgSize to a multiple of 8 */
10158 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10159 bool NeedsAlign = (Align > 8);
10160
10161 MachineBasicBlock *thisMBB = MBB;
10162 MachineBasicBlock *overflowMBB;
10163 MachineBasicBlock *offsetMBB;
10164 MachineBasicBlock *endMBB;
10165
10166 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10167 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10168 unsigned OffsetReg = 0;
10169
10170 if (!UseGPOffset && !UseFPOffset) {
10171 // If we only pull from the overflow region, we don't create a branch.
10172 // We don't need to alter control flow.
10173 OffsetDestReg = 0; // unused
10174 OverflowDestReg = DestReg;
10175
10176 offsetMBB = NULL;
10177 overflowMBB = thisMBB;
10178 endMBB = thisMBB;
10179 } else {
10180 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10181 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10182 // If not, pull from overflow_area. (branch to overflowMBB)
10183 //
10184 // thisMBB
10185 // | .
10186 // | .
10187 // offsetMBB overflowMBB
10188 // | .
10189 // | .
10190 // endMBB
10191
10192 // Registers for the PHI in endMBB
10193 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10194 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10195
10196 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10197 MachineFunction *MF = MBB->getParent();
10198 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10199 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10200 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10201
10202 MachineFunction::iterator MBBIter = MBB;
10203 ++MBBIter;
10204
10205 // Insert the new basic blocks
10206 MF->insert(MBBIter, offsetMBB);
10207 MF->insert(MBBIter, overflowMBB);
10208 MF->insert(MBBIter, endMBB);
10209
10210 // Transfer the remainder of MBB and its successor edges to endMBB.
10211 endMBB->splice(endMBB->begin(), thisMBB,
10212 llvm::next(MachineBasicBlock::iterator(MI)),
10213 thisMBB->end());
10214 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10215
10216 // Make offsetMBB and overflowMBB successors of thisMBB
10217 thisMBB->addSuccessor(offsetMBB);
10218 thisMBB->addSuccessor(overflowMBB);
10219
10220 // endMBB is a successor of both offsetMBB and overflowMBB
10221 offsetMBB->addSuccessor(endMBB);
10222 overflowMBB->addSuccessor(endMBB);
10223
10224 // Load the offset value into a register
10225 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10226 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10227 .addOperand(Base)
10228 .addOperand(Scale)
10229 .addOperand(Index)
10230 .addDisp(Disp, UseFPOffset ? 4 : 0)
10231 .addOperand(Segment)
10232 .setMemRefs(MMOBegin, MMOEnd);
10233
10234 // Check if there is enough room left to pull this argument.
10235 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10236 .addReg(OffsetReg)
10237 .addImm(MaxOffset + 8 - ArgSizeA8);
10238
10239 // Branch to "overflowMBB" if offset >= max
10240 // Fall through to "offsetMBB" otherwise
10241 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10242 .addMBB(overflowMBB);
10243 }
10244
10245 // In offsetMBB, emit code to use the reg_save_area.
10246 if (offsetMBB) {
10247 assert(OffsetReg != 0);
10248
10249 // Read the reg_save_area address.
10250 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10251 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10252 .addOperand(Base)
10253 .addOperand(Scale)
10254 .addOperand(Index)
10255 .addDisp(Disp, 16)
10256 .addOperand(Segment)
10257 .setMemRefs(MMOBegin, MMOEnd);
10258
10259 // Zero-extend the offset
10260 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10261 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10262 .addImm(0)
10263 .addReg(OffsetReg)
10264 .addImm(X86::sub_32bit);
10265
10266 // Add the offset to the reg_save_area to get the final address.
10267 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10268 .addReg(OffsetReg64)
10269 .addReg(RegSaveReg);
10270
10271 // Compute the offset for the next argument
10272 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10273 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10274 .addReg(OffsetReg)
10275 .addImm(UseFPOffset ? 16 : 8);
10276
10277 // Store it back into the va_list.
10278 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10279 .addOperand(Base)
10280 .addOperand(Scale)
10281 .addOperand(Index)
10282 .addDisp(Disp, UseFPOffset ? 4 : 0)
10283 .addOperand(Segment)
10284 .addReg(NextOffsetReg)
10285 .setMemRefs(MMOBegin, MMOEnd);
10286
10287 // Jump to endMBB
10288 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10289 .addMBB(endMBB);
10290 }
10291
10292 //
10293 // Emit code to use overflow area
10294 //
10295
10296 // Load the overflow_area address into a register.
10297 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10298 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10299 .addOperand(Base)
10300 .addOperand(Scale)
10301 .addOperand(Index)
10302 .addDisp(Disp, 8)
10303 .addOperand(Segment)
10304 .setMemRefs(MMOBegin, MMOEnd);
10305
10306 // If we need to align it, do so. Otherwise, just copy the address
10307 // to OverflowDestReg.
10308 if (NeedsAlign) {
10309 // Align the overflow address
10310 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10311 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10312
10313 // aligned_addr = (addr + (align-1)) & ~(align-1)
10314 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10315 .addReg(OverflowAddrReg)
10316 .addImm(Align-1);
10317
10318 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10319 .addReg(TmpReg)
10320 .addImm(~(uint64_t)(Align-1));
10321 } else {
10322 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10323 .addReg(OverflowAddrReg);
10324 }
10325
10326 // Compute the next overflow address after this argument.
10327 // (the overflow address should be kept 8-byte aligned)
10328 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10329 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10330 .addReg(OverflowDestReg)
10331 .addImm(ArgSizeA8);
10332
10333 // Store the new overflow address.
10334 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10335 .addOperand(Base)
10336 .addOperand(Scale)
10337 .addOperand(Index)
10338 .addDisp(Disp, 8)
10339 .addOperand(Segment)
10340 .addReg(NextAddrReg)
10341 .setMemRefs(MMOBegin, MMOEnd);
10342
10343 // If we branched, emit the PHI to the front of endMBB.
10344 if (offsetMBB) {
10345 BuildMI(*endMBB, endMBB->begin(), DL,
10346 TII->get(X86::PHI), DestReg)
10347 .addReg(OffsetDestReg).addMBB(offsetMBB)
10348 .addReg(OverflowDestReg).addMBB(overflowMBB);
10349 }
10350
10351 // Erase the pseudo instruction
10352 MI->eraseFromParent();
10353
10354 return endMBB;
10355}
10356
10357MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010358X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10359 MachineInstr *MI,
10360 MachineBasicBlock *MBB) const {
10361 // Emit code to save XMM registers to the stack. The ABI says that the
10362 // number of registers to save is given in %al, so it's theoretically
10363 // possible to do an indirect jump trick to avoid saving all of them,
10364 // however this code takes a simpler approach and just executes all
10365 // of the stores if %al is non-zero. It's less code, and it's probably
10366 // easier on the hardware branch predictor, and stores aren't all that
10367 // expensive anyway.
10368
10369 // Create the new basic blocks. One block contains all the XMM stores,
10370 // and one block is the final destination regardless of whether any
10371 // stores were performed.
10372 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10373 MachineFunction *F = MBB->getParent();
10374 MachineFunction::iterator MBBIter = MBB;
10375 ++MBBIter;
10376 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10377 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10378 F->insert(MBBIter, XMMSaveMBB);
10379 F->insert(MBBIter, EndMBB);
10380
Dan Gohman14152b42010-07-06 20:24:04 +000010381 // Transfer the remainder of MBB and its successor edges to EndMBB.
10382 EndMBB->splice(EndMBB->begin(), MBB,
10383 llvm::next(MachineBasicBlock::iterator(MI)),
10384 MBB->end());
10385 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10386
Dan Gohmand6708ea2009-08-15 01:38:56 +000010387 // The original block will now fall through to the XMM save block.
10388 MBB->addSuccessor(XMMSaveMBB);
10389 // The XMMSaveMBB will fall through to the end block.
10390 XMMSaveMBB->addSuccessor(EndMBB);
10391
10392 // Now add the instructions.
10393 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10394 DebugLoc DL = MI->getDebugLoc();
10395
10396 unsigned CountReg = MI->getOperand(0).getReg();
10397 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10398 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10399
10400 if (!Subtarget->isTargetWin64()) {
10401 // If %al is 0, branch around the XMM save block.
10402 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010403 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010404 MBB->addSuccessor(EndMBB);
10405 }
10406
10407 // In the XMM save block, save all the XMM argument registers.
10408 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10409 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010410 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010411 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010412 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010413 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010414 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010415 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10416 .addFrameIndex(RegSaveFrameIndex)
10417 .addImm(/*Scale=*/1)
10418 .addReg(/*IndexReg=*/0)
10419 .addImm(/*Disp=*/Offset)
10420 .addReg(/*Segment=*/0)
10421 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010422 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010423 }
10424
Dan Gohman14152b42010-07-06 20:24:04 +000010425 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010426
10427 return EndMBB;
10428}
Mon P Wang63307c32008-05-05 19:05:59 +000010429
Evan Cheng60c07e12006-07-05 22:17:51 +000010430MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010431X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010432 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010433 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10434 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010435
Chris Lattner52600972009-09-02 05:57:00 +000010436 // To "insert" a SELECT_CC instruction, we actually have to insert the
10437 // diamond control-flow pattern. The incoming instruction knows the
10438 // destination vreg to set, the condition code register to branch on, the
10439 // true/false values to select between, and a branch opcode to use.
10440 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10441 MachineFunction::iterator It = BB;
10442 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010443
Chris Lattner52600972009-09-02 05:57:00 +000010444 // thisMBB:
10445 // ...
10446 // TrueVal = ...
10447 // cmpTY ccX, r1, r2
10448 // bCC copy1MBB
10449 // fallthrough --> copy0MBB
10450 MachineBasicBlock *thisMBB = BB;
10451 MachineFunction *F = BB->getParent();
10452 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10453 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010454 F->insert(It, copy0MBB);
10455 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010456
Bill Wendling730c07e2010-06-25 20:48:10 +000010457 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10458 // live into the sink and copy blocks.
10459 const MachineFunction *MF = BB->getParent();
10460 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10461 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010462
Dan Gohman14152b42010-07-06 20:24:04 +000010463 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10464 const MachineOperand &MO = MI->getOperand(I);
10465 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010466 unsigned Reg = MO.getReg();
10467 if (Reg != X86::EFLAGS) continue;
10468 copy0MBB->addLiveIn(Reg);
10469 sinkMBB->addLiveIn(Reg);
10470 }
10471
Dan Gohman14152b42010-07-06 20:24:04 +000010472 // Transfer the remainder of BB and its successor edges to sinkMBB.
10473 sinkMBB->splice(sinkMBB->begin(), BB,
10474 llvm::next(MachineBasicBlock::iterator(MI)),
10475 BB->end());
10476 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10477
10478 // Add the true and fallthrough blocks as its successors.
10479 BB->addSuccessor(copy0MBB);
10480 BB->addSuccessor(sinkMBB);
10481
10482 // Create the conditional branch instruction.
10483 unsigned Opc =
10484 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10485 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10486
Chris Lattner52600972009-09-02 05:57:00 +000010487 // copy0MBB:
10488 // %FalseValue = ...
10489 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010490 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010491
Chris Lattner52600972009-09-02 05:57:00 +000010492 // sinkMBB:
10493 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10494 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010495 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10496 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010497 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10498 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10499
Dan Gohman14152b42010-07-06 20:24:04 +000010500 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010501 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010502}
10503
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010504MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010505X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010506 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010507 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10508 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010509
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010510 assert(!Subtarget->isTargetEnvMacho());
10511
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010512 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10513 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010514
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010515 if (Subtarget->isTargetWin64()) {
10516 if (Subtarget->isTargetCygMing()) {
10517 // ___chkstk(Mingw64):
10518 // Clobbers R10, R11, RAX and EFLAGS.
10519 // Updates RSP.
10520 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10521 .addExternalSymbol("___chkstk")
10522 .addReg(X86::RAX, RegState::Implicit)
10523 .addReg(X86::RSP, RegState::Implicit)
10524 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
10525 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
10526 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10527 } else {
10528 // __chkstk(MSVCRT): does not update stack pointer.
10529 // Clobbers R10, R11 and EFLAGS.
10530 // FIXME: RAX(allocated size) might be reused and not killed.
10531 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
10532 .addExternalSymbol("__chkstk")
10533 .addReg(X86::RAX, RegState::Implicit)
10534 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10535 // RAX has the offset to subtracted from RSP.
10536 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
10537 .addReg(X86::RSP)
10538 .addReg(X86::RAX);
10539 }
10540 } else {
10541 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010542 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10543
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000010544 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
10545 .addExternalSymbol(StackProbeSymbol)
10546 .addReg(X86::EAX, RegState::Implicit)
10547 .addReg(X86::ESP, RegState::Implicit)
10548 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
10549 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10550 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
10551 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010552
Dan Gohman14152b42010-07-06 20:24:04 +000010553 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010554 return BB;
10555}
Chris Lattner52600972009-09-02 05:57:00 +000010556
10557MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010558X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10559 MachineBasicBlock *BB) const {
10560 // This is pretty easy. We're taking the value that we received from
10561 // our load from the relocation, sticking it in either RDI (x86-64)
10562 // or EAX and doing an indirect call. The return value will then
10563 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010564 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010565 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010566 DebugLoc DL = MI->getDebugLoc();
10567 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010568
10569 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010570 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010571
Eric Christopher30ef0e52010-06-03 04:07:48 +000010572 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010573 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10574 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010575 .addReg(X86::RIP)
10576 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010577 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010578 MI->getOperand(3).getTargetFlags())
10579 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010580 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010581 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010582 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010583 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10584 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010585 .addReg(0)
10586 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010587 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010588 MI->getOperand(3).getTargetFlags())
10589 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010590 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010591 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010592 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010593 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10594 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010595 .addReg(TII->getGlobalBaseReg(F))
10596 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010597 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010598 MI->getOperand(3).getTargetFlags())
10599 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010600 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010601 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010602 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010603
Dan Gohman14152b42010-07-06 20:24:04 +000010604 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010605 return BB;
10606}
10607
10608MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010609X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010610 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010611 switch (MI->getOpcode()) {
10612 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010613 case X86::TAILJMPd64:
10614 case X86::TAILJMPr64:
10615 case X86::TAILJMPm64:
10616 assert(!"TAILJMP64 would not be touched here.");
10617 case X86::TCRETURNdi64:
10618 case X86::TCRETURNri64:
10619 case X86::TCRETURNmi64:
10620 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10621 // On AMD64, additional defs should be added before register allocation.
10622 if (!Subtarget->isTargetWin64()) {
10623 MI->addRegisterDefined(X86::RSI);
10624 MI->addRegisterDefined(X86::RDI);
10625 MI->addRegisterDefined(X86::XMM6);
10626 MI->addRegisterDefined(X86::XMM7);
10627 MI->addRegisterDefined(X86::XMM8);
10628 MI->addRegisterDefined(X86::XMM9);
10629 MI->addRegisterDefined(X86::XMM10);
10630 MI->addRegisterDefined(X86::XMM11);
10631 MI->addRegisterDefined(X86::XMM12);
10632 MI->addRegisterDefined(X86::XMM13);
10633 MI->addRegisterDefined(X86::XMM14);
10634 MI->addRegisterDefined(X86::XMM15);
10635 }
10636 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010637 case X86::WIN_ALLOCA:
10638 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010639 case X86::TLSCall_32:
10640 case X86::TLSCall_64:
10641 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010642 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010643 case X86::CMOV_FR32:
10644 case X86::CMOV_FR64:
10645 case X86::CMOV_V4F32:
10646 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010647 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010648 case X86::CMOV_GR16:
10649 case X86::CMOV_GR32:
10650 case X86::CMOV_RFP32:
10651 case X86::CMOV_RFP64:
10652 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010653 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010654
Dale Johannesen849f2142007-07-03 00:53:03 +000010655 case X86::FP32_TO_INT16_IN_MEM:
10656 case X86::FP32_TO_INT32_IN_MEM:
10657 case X86::FP32_TO_INT64_IN_MEM:
10658 case X86::FP64_TO_INT16_IN_MEM:
10659 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010660 case X86::FP64_TO_INT64_IN_MEM:
10661 case X86::FP80_TO_INT16_IN_MEM:
10662 case X86::FP80_TO_INT32_IN_MEM:
10663 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010664 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10665 DebugLoc DL = MI->getDebugLoc();
10666
Evan Cheng60c07e12006-07-05 22:17:51 +000010667 // Change the floating point control register to use "round towards zero"
10668 // mode when truncating to an integer value.
10669 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010670 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010671 addFrameReference(BuildMI(*BB, MI, DL,
10672 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010673
10674 // Load the old value of the high byte of the control word...
10675 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010676 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010677 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010678 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010679
10680 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010681 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010682 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010683
10684 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010685 addFrameReference(BuildMI(*BB, MI, DL,
10686 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010687
10688 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010689 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010690 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010691
10692 // Get the X86 opcode to use.
10693 unsigned Opc;
10694 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010695 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010696 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10697 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10698 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10699 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10700 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10701 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010702 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10703 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10704 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010705 }
10706
10707 X86AddressMode AM;
10708 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010709 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010710 AM.BaseType = X86AddressMode::RegBase;
10711 AM.Base.Reg = Op.getReg();
10712 } else {
10713 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010714 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010715 }
10716 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010717 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010718 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010719 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010720 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010721 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010722 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010723 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010724 AM.GV = Op.getGlobal();
10725 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010726 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010727 }
Dan Gohman14152b42010-07-06 20:24:04 +000010728 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010729 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010730
10731 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010732 addFrameReference(BuildMI(*BB, MI, DL,
10733 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010734
Dan Gohman14152b42010-07-06 20:24:04 +000010735 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010736 return BB;
10737 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010738 // String/text processing lowering.
10739 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010740 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010741 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10742 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010743 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010744 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10745 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010746 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010747 return EmitPCMP(MI, BB, 5, false /* in mem */);
10748 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010749 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010750 return EmitPCMP(MI, BB, 5, true /* in mem */);
10751
Eric Christopher228232b2010-11-30 07:20:12 +000010752 // Thread synchronization.
10753 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010754 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010755 case X86::MWAIT:
10756 return EmitMwait(MI, BB);
10757
Eric Christopherb120ab42009-08-18 22:50:32 +000010758 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010759 case X86::ATOMAND32:
10760 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010761 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010762 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010763 X86::NOT32r, X86::EAX,
10764 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010765 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010766 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10767 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010768 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010769 X86::NOT32r, X86::EAX,
10770 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010771 case X86::ATOMXOR32:
10772 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010773 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010774 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010775 X86::NOT32r, X86::EAX,
10776 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010777 case X86::ATOMNAND32:
10778 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010779 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010780 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010781 X86::NOT32r, X86::EAX,
10782 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010783 case X86::ATOMMIN32:
10784 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10785 case X86::ATOMMAX32:
10786 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10787 case X86::ATOMUMIN32:
10788 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10789 case X86::ATOMUMAX32:
10790 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010791
10792 case X86::ATOMAND16:
10793 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10794 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010795 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010796 X86::NOT16r, X86::AX,
10797 X86::GR16RegisterClass);
10798 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010799 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010800 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010801 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010802 X86::NOT16r, X86::AX,
10803 X86::GR16RegisterClass);
10804 case X86::ATOMXOR16:
10805 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10806 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010807 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010808 X86::NOT16r, X86::AX,
10809 X86::GR16RegisterClass);
10810 case X86::ATOMNAND16:
10811 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10812 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010813 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010814 X86::NOT16r, X86::AX,
10815 X86::GR16RegisterClass, true);
10816 case X86::ATOMMIN16:
10817 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10818 case X86::ATOMMAX16:
10819 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10820 case X86::ATOMUMIN16:
10821 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10822 case X86::ATOMUMAX16:
10823 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10824
10825 case X86::ATOMAND8:
10826 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10827 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010828 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010829 X86::NOT8r, X86::AL,
10830 X86::GR8RegisterClass);
10831 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010832 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010833 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010834 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010835 X86::NOT8r, X86::AL,
10836 X86::GR8RegisterClass);
10837 case X86::ATOMXOR8:
10838 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10839 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010840 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010841 X86::NOT8r, X86::AL,
10842 X86::GR8RegisterClass);
10843 case X86::ATOMNAND8:
10844 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10845 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010846 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010847 X86::NOT8r, X86::AL,
10848 X86::GR8RegisterClass, true);
10849 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010850 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010851 case X86::ATOMAND64:
10852 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010853 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010854 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010855 X86::NOT64r, X86::RAX,
10856 X86::GR64RegisterClass);
10857 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010858 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10859 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010860 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010861 X86::NOT64r, X86::RAX,
10862 X86::GR64RegisterClass);
10863 case X86::ATOMXOR64:
10864 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010865 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010866 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010867 X86::NOT64r, X86::RAX,
10868 X86::GR64RegisterClass);
10869 case X86::ATOMNAND64:
10870 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10871 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010872 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010873 X86::NOT64r, X86::RAX,
10874 X86::GR64RegisterClass, true);
10875 case X86::ATOMMIN64:
10876 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10877 case X86::ATOMMAX64:
10878 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10879 case X86::ATOMUMIN64:
10880 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10881 case X86::ATOMUMAX64:
10882 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010883
10884 // This group does 64-bit operations on a 32-bit host.
10885 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010886 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010887 X86::AND32rr, X86::AND32rr,
10888 X86::AND32ri, X86::AND32ri,
10889 false);
10890 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010891 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010892 X86::OR32rr, X86::OR32rr,
10893 X86::OR32ri, X86::OR32ri,
10894 false);
10895 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010896 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010897 X86::XOR32rr, X86::XOR32rr,
10898 X86::XOR32ri, X86::XOR32ri,
10899 false);
10900 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010901 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010902 X86::AND32rr, X86::AND32rr,
10903 X86::AND32ri, X86::AND32ri,
10904 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010905 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010906 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010907 X86::ADD32rr, X86::ADC32rr,
10908 X86::ADD32ri, X86::ADC32ri,
10909 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010910 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010911 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010912 X86::SUB32rr, X86::SBB32rr,
10913 X86::SUB32ri, X86::SBB32ri,
10914 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010915 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010916 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010917 X86::MOV32rr, X86::MOV32rr,
10918 X86::MOV32ri, X86::MOV32ri,
10919 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010920 case X86::VASTART_SAVE_XMM_REGS:
10921 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010922
10923 case X86::VAARG_64:
10924 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010925 }
10926}
10927
10928//===----------------------------------------------------------------------===//
10929// X86 Optimization Hooks
10930//===----------------------------------------------------------------------===//
10931
Dan Gohman475871a2008-07-27 21:46:04 +000010932void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010933 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010934 APInt &KnownZero,
10935 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010936 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010937 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010938 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010939 assert((Opc >= ISD::BUILTIN_OP_END ||
10940 Opc == ISD::INTRINSIC_WO_CHAIN ||
10941 Opc == ISD::INTRINSIC_W_CHAIN ||
10942 Opc == ISD::INTRINSIC_VOID) &&
10943 "Should use MaskedValueIsZero if you don't know whether Op"
10944 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010945
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010946 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010947 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010948 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010949 case X86ISD::ADD:
10950 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010951 case X86ISD::ADC:
10952 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010953 case X86ISD::SMUL:
10954 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010955 case X86ISD::INC:
10956 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010957 case X86ISD::OR:
10958 case X86ISD::XOR:
10959 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010960 // These nodes' second result is a boolean.
10961 if (Op.getResNo() == 0)
10962 break;
10963 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010964 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010965 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10966 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010967 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010968 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010969}
Chris Lattner259e97c2006-01-31 19:43:35 +000010970
Owen Andersonbc146b02010-09-21 20:42:50 +000010971unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10972 unsigned Depth) const {
10973 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10974 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10975 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010976
Owen Andersonbc146b02010-09-21 20:42:50 +000010977 // Fallback case.
10978 return 1;
10979}
10980
Evan Cheng206ee9d2006-07-07 08:33:52 +000010981/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010982/// node is a GlobalAddress + offset.
10983bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010984 const GlobalValue* &GA,
10985 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010986 if (N->getOpcode() == X86ISD::Wrapper) {
10987 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010988 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010989 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010990 return true;
10991 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010992 }
Evan Chengad4196b2008-05-12 19:56:52 +000010993 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010994}
10995
Evan Cheng206ee9d2006-07-07 08:33:52 +000010996/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10997/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10998/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010999/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000011000static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011001 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011002 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011003 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000011004
Eli Friedman7a5e5552009-06-07 06:52:44 +000011005 if (VT.getSizeInBits() != 128)
11006 return SDValue();
11007
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011008 // Don't create instructions with illegal types after legalize types has run.
11009 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11010 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
11011 return SDValue();
11012
Nate Begemanfdea31a2010-03-24 20:49:50 +000011013 SmallVector<SDValue, 16> Elts;
11014 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011015 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000011016
Nate Begemanfdea31a2010-03-24 20:49:50 +000011017 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000011018}
Evan Chengd880b972008-05-09 21:53:03 +000011019
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000011020/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
11021/// generation and convert it from being a bunch of shuffles and extracts
11022/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011023static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
11024 const TargetLowering &TLI) {
11025 SDValue InputVector = N->getOperand(0);
11026
11027 // Only operate on vectors of 4 elements, where the alternative shuffling
11028 // gets to be more expensive.
11029 if (InputVector.getValueType() != MVT::v4i32)
11030 return SDValue();
11031
11032 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
11033 // single use which is a sign-extend or zero-extend, and all elements are
11034 // used.
11035 SmallVector<SDNode *, 4> Uses;
11036 unsigned ExtractedElements = 0;
11037 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
11038 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
11039 if (UI.getUse().getResNo() != InputVector.getResNo())
11040 return SDValue();
11041
11042 SDNode *Extract = *UI;
11043 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
11044 return SDValue();
11045
11046 if (Extract->getValueType(0) != MVT::i32)
11047 return SDValue();
11048 if (!Extract->hasOneUse())
11049 return SDValue();
11050 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
11051 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
11052 return SDValue();
11053 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
11054 return SDValue();
11055
11056 // Record which element was extracted.
11057 ExtractedElements |=
11058 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
11059
11060 Uses.push_back(Extract);
11061 }
11062
11063 // If not all the elements were used, this may not be worthwhile.
11064 if (ExtractedElements != 15)
11065 return SDValue();
11066
11067 // Ok, we've now decided to do the transformation.
11068 DebugLoc dl = InputVector.getDebugLoc();
11069
11070 // Store the value to a temporary stack slot.
11071 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000011072 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
11073 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011074
11075 // Replace each use (extract) with a load of the appropriate element.
11076 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
11077 UE = Uses.end(); UI != UE; ++UI) {
11078 SDNode *Extract = *UI;
11079
Nadav Rotem86694292011-05-17 08:31:57 +000011080 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011081 SDValue Idx = Extract->getOperand(1);
11082 unsigned EltSize =
11083 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
11084 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
11085 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
11086
Nadav Rotem86694292011-05-17 08:31:57 +000011087 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011088 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011089
11090 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000011091 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000011092 ScalarAddr, MachinePointerInfo(),
11093 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011094
11095 // Replace the exact with the load.
11096 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
11097 }
11098
11099 // The replacement was made in place; don't return anything.
11100 return SDValue();
11101}
11102
Chris Lattner83e6c992006-10-04 06:57:07 +000011103/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011104static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000011105 const X86Subtarget *Subtarget) {
11106 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000011107 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000011108 // Get the LHS/RHS of the select.
11109 SDValue LHS = N->getOperand(1);
11110 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011111
Dan Gohman670e5392009-09-21 18:03:22 +000011112 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000011113 // instructions match the semantics of the common C idiom x<y?x:y but not
11114 // x<=y?x:y, because of how they handle negative zero (which can be
11115 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000011116 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000011117 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000011118 Cond.getOpcode() == ISD::SETCC) {
11119 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011120
Chris Lattner47b4ce82009-03-11 05:48:52 +000011121 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000011122 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000011123 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
11124 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011125 switch (CC) {
11126 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011127 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011128 // Converting this to a min would handle NaNs incorrectly, and swapping
11129 // the operands would cause it to handle comparisons between positive
11130 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011131 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011132 if (!UnsafeFPMath &&
11133 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11134 break;
11135 std::swap(LHS, RHS);
11136 }
Dan Gohman670e5392009-09-21 18:03:22 +000011137 Opcode = X86ISD::FMIN;
11138 break;
11139 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011140 // Converting this to a min would handle comparisons between positive
11141 // and negative zero incorrectly.
11142 if (!UnsafeFPMath &&
11143 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11144 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011145 Opcode = X86ISD::FMIN;
11146 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011147 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011148 // Converting this to a min would handle both negative zeros and NaNs
11149 // incorrectly, but we can swap the operands to fix both.
11150 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011151 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011152 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011153 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011154 Opcode = X86ISD::FMIN;
11155 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011156
Dan Gohman670e5392009-09-21 18:03:22 +000011157 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011158 // Converting this to a max would handle comparisons between positive
11159 // and negative zero incorrectly.
11160 if (!UnsafeFPMath &&
11161 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11162 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011163 Opcode = X86ISD::FMAX;
11164 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011165 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011166 // Converting this to a max would handle NaNs incorrectly, and swapping
11167 // the operands would cause it to handle comparisons between positive
11168 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011169 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011170 if (!UnsafeFPMath &&
11171 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11172 break;
11173 std::swap(LHS, RHS);
11174 }
Dan Gohman670e5392009-09-21 18:03:22 +000011175 Opcode = X86ISD::FMAX;
11176 break;
11177 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011178 // Converting this to a max would handle both negative zeros and NaNs
11179 // incorrectly, but we can swap the operands to fix both.
11180 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011181 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011182 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011183 case ISD::SETGE:
11184 Opcode = X86ISD::FMAX;
11185 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011186 }
Dan Gohman670e5392009-09-21 18:03:22 +000011187 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011188 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11189 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011190 switch (CC) {
11191 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011192 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011193 // Converting this to a min would handle comparisons between positive
11194 // and negative zero incorrectly, and swapping the operands would
11195 // cause it to handle NaNs incorrectly.
11196 if (!UnsafeFPMath &&
11197 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011198 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011199 break;
11200 std::swap(LHS, RHS);
11201 }
Dan Gohman670e5392009-09-21 18:03:22 +000011202 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011203 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011204 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011205 // Converting this to a min would handle NaNs incorrectly.
11206 if (!UnsafeFPMath &&
11207 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11208 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011209 Opcode = X86ISD::FMIN;
11210 break;
11211 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011212 // Converting this to a min would handle both negative zeros and NaNs
11213 // incorrectly, but we can swap the operands to fix both.
11214 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011215 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011216 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011217 case ISD::SETGE:
11218 Opcode = X86ISD::FMIN;
11219 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011220
Dan Gohman670e5392009-09-21 18:03:22 +000011221 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011222 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011223 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011224 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011225 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011226 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011227 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011228 // Converting this to a max would handle comparisons between positive
11229 // and negative zero incorrectly, and swapping the operands would
11230 // cause it to handle NaNs incorrectly.
11231 if (!UnsafeFPMath &&
11232 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011233 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011234 break;
11235 std::swap(LHS, RHS);
11236 }
Dan Gohman670e5392009-09-21 18:03:22 +000011237 Opcode = X86ISD::FMAX;
11238 break;
11239 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011240 // Converting this to a max would handle both negative zeros and NaNs
11241 // incorrectly, but we can swap the operands to fix both.
11242 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011243 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011244 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011245 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011246 Opcode = X86ISD::FMAX;
11247 break;
11248 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011249 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011250
Chris Lattner47b4ce82009-03-11 05:48:52 +000011251 if (Opcode)
11252 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011253 }
Eric Christopherfd179292009-08-27 18:07:15 +000011254
Chris Lattnerd1980a52009-03-12 06:52:53 +000011255 // If this is a select between two integer constants, try to do some
11256 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011257 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11258 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011259 // Don't do this for crazy integer types.
11260 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11261 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011262 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011263 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011264
Chris Lattnercee56e72009-03-13 05:53:31 +000011265 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011266 // Efficiently invertible.
11267 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11268 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11269 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11270 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011271 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011272 }
Eric Christopherfd179292009-08-27 18:07:15 +000011273
Chris Lattnerd1980a52009-03-12 06:52:53 +000011274 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011275 if (FalseC->getAPIntValue() == 0 &&
11276 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011277 if (NeedsCondInvert) // Invert the condition if needed.
11278 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11279 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011280
Chris Lattnerd1980a52009-03-12 06:52:53 +000011281 // Zero extend the condition if needed.
11282 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011283
Chris Lattnercee56e72009-03-13 05:53:31 +000011284 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011285 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011286 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011287 }
Eric Christopherfd179292009-08-27 18:07:15 +000011288
Chris Lattner97a29a52009-03-13 05:22:11 +000011289 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011290 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011291 if (NeedsCondInvert) // Invert the condition if needed.
11292 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11293 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011294
Chris Lattner97a29a52009-03-13 05:22:11 +000011295 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011296 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11297 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011298 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011299 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011300 }
Eric Christopherfd179292009-08-27 18:07:15 +000011301
Chris Lattnercee56e72009-03-13 05:53:31 +000011302 // Optimize cases that will turn into an LEA instruction. This requires
11303 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011304 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011305 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011306 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011307
Chris Lattnercee56e72009-03-13 05:53:31 +000011308 bool isFastMultiplier = false;
11309 if (Diff < 10) {
11310 switch ((unsigned char)Diff) {
11311 default: break;
11312 case 1: // result = add base, cond
11313 case 2: // result = lea base( , cond*2)
11314 case 3: // result = lea base(cond, cond*2)
11315 case 4: // result = lea base( , cond*4)
11316 case 5: // result = lea base(cond, cond*4)
11317 case 8: // result = lea base( , cond*8)
11318 case 9: // result = lea base(cond, cond*8)
11319 isFastMultiplier = true;
11320 break;
11321 }
11322 }
Eric Christopherfd179292009-08-27 18:07:15 +000011323
Chris Lattnercee56e72009-03-13 05:53:31 +000011324 if (isFastMultiplier) {
11325 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11326 if (NeedsCondInvert) // Invert the condition if needed.
11327 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11328 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011329
Chris Lattnercee56e72009-03-13 05:53:31 +000011330 // Zero extend the condition if needed.
11331 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11332 Cond);
11333 // Scale the condition by the difference.
11334 if (Diff != 1)
11335 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11336 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011337
Chris Lattnercee56e72009-03-13 05:53:31 +000011338 // Add the base if non-zero.
11339 if (FalseC->getAPIntValue() != 0)
11340 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11341 SDValue(FalseC, 0));
11342 return Cond;
11343 }
Eric Christopherfd179292009-08-27 18:07:15 +000011344 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011345 }
11346 }
Eric Christopherfd179292009-08-27 18:07:15 +000011347
Dan Gohman475871a2008-07-27 21:46:04 +000011348 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011349}
11350
Chris Lattnerd1980a52009-03-12 06:52:53 +000011351/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11352static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11353 TargetLowering::DAGCombinerInfo &DCI) {
11354 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011355
Chris Lattnerd1980a52009-03-12 06:52:53 +000011356 // If the flag operand isn't dead, don't touch this CMOV.
11357 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11358 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011359
Evan Chengb5a55d92011-05-24 01:48:22 +000011360 SDValue FalseOp = N->getOperand(0);
11361 SDValue TrueOp = N->getOperand(1);
11362 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
11363 SDValue Cond = N->getOperand(3);
11364 if (CC == X86::COND_E || CC == X86::COND_NE) {
11365 switch (Cond.getOpcode()) {
11366 default: break;
11367 case X86ISD::BSR:
11368 case X86ISD::BSF:
11369 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
11370 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
11371 return (CC == X86::COND_E) ? FalseOp : TrueOp;
11372 }
11373 }
11374
Chris Lattnerd1980a52009-03-12 06:52:53 +000011375 // If this is a select between two integer constants, try to do some
11376 // optimizations. Note that the operands are ordered the opposite of SELECT
11377 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000011378 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
11379 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011380 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11381 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000011382 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11383 CC = X86::GetOppositeBranchCondition(CC);
11384 std::swap(TrueC, FalseC);
11385 }
Eric Christopherfd179292009-08-27 18:07:15 +000011386
Chris Lattnerd1980a52009-03-12 06:52:53 +000011387 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011388 // This is efficient for any integer data type (including i8/i16) and
11389 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011390 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011391 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11392 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011393
Chris Lattnerd1980a52009-03-12 06:52:53 +000011394 // Zero extend the condition if needed.
11395 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011396
Chris Lattnerd1980a52009-03-12 06:52:53 +000011397 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11398 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011399 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011400 if (N->getNumValues() == 2) // Dead flag value?
11401 return DCI.CombineTo(N, Cond, SDValue());
11402 return Cond;
11403 }
Eric Christopherfd179292009-08-27 18:07:15 +000011404
Chris Lattnercee56e72009-03-13 05:53:31 +000011405 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11406 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011407 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011408 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11409 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011410
Chris Lattner97a29a52009-03-13 05:22:11 +000011411 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011412 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11413 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011414 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11415 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011416
Chris Lattner97a29a52009-03-13 05:22:11 +000011417 if (N->getNumValues() == 2) // Dead flag value?
11418 return DCI.CombineTo(N, Cond, SDValue());
11419 return Cond;
11420 }
Eric Christopherfd179292009-08-27 18:07:15 +000011421
Chris Lattnercee56e72009-03-13 05:53:31 +000011422 // Optimize cases that will turn into an LEA instruction. This requires
11423 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011424 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011425 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011426 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011427
Chris Lattnercee56e72009-03-13 05:53:31 +000011428 bool isFastMultiplier = false;
11429 if (Diff < 10) {
11430 switch ((unsigned char)Diff) {
11431 default: break;
11432 case 1: // result = add base, cond
11433 case 2: // result = lea base( , cond*2)
11434 case 3: // result = lea base(cond, cond*2)
11435 case 4: // result = lea base( , cond*4)
11436 case 5: // result = lea base(cond, cond*4)
11437 case 8: // result = lea base( , cond*8)
11438 case 9: // result = lea base(cond, cond*8)
11439 isFastMultiplier = true;
11440 break;
11441 }
11442 }
Eric Christopherfd179292009-08-27 18:07:15 +000011443
Chris Lattnercee56e72009-03-13 05:53:31 +000011444 if (isFastMultiplier) {
11445 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011446 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11447 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011448 // Zero extend the condition if needed.
11449 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11450 Cond);
11451 // Scale the condition by the difference.
11452 if (Diff != 1)
11453 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11454 DAG.getConstant(Diff, Cond.getValueType()));
11455
11456 // Add the base if non-zero.
11457 if (FalseC->getAPIntValue() != 0)
11458 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11459 SDValue(FalseC, 0));
11460 if (N->getNumValues() == 2) // Dead flag value?
11461 return DCI.CombineTo(N, Cond, SDValue());
11462 return Cond;
11463 }
Eric Christopherfd179292009-08-27 18:07:15 +000011464 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011465 }
11466 }
11467 return SDValue();
11468}
11469
11470
Evan Cheng0b0cd912009-03-28 05:57:29 +000011471/// PerformMulCombine - Optimize a single multiply with constant into two
11472/// in order to implement it with two cheaper instructions, e.g.
11473/// LEA + SHL, LEA + LEA.
11474static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11475 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011476 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11477 return SDValue();
11478
Owen Andersone50ed302009-08-10 22:56:29 +000011479 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011481 return SDValue();
11482
11483 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11484 if (!C)
11485 return SDValue();
11486 uint64_t MulAmt = C->getZExtValue();
11487 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11488 return SDValue();
11489
11490 uint64_t MulAmt1 = 0;
11491 uint64_t MulAmt2 = 0;
11492 if ((MulAmt % 9) == 0) {
11493 MulAmt1 = 9;
11494 MulAmt2 = MulAmt / 9;
11495 } else if ((MulAmt % 5) == 0) {
11496 MulAmt1 = 5;
11497 MulAmt2 = MulAmt / 5;
11498 } else if ((MulAmt % 3) == 0) {
11499 MulAmt1 = 3;
11500 MulAmt2 = MulAmt / 3;
11501 }
11502 if (MulAmt2 &&
11503 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11504 DebugLoc DL = N->getDebugLoc();
11505
11506 if (isPowerOf2_64(MulAmt2) &&
11507 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11508 // If second multiplifer is pow2, issue it first. We want the multiply by
11509 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11510 // is an add.
11511 std::swap(MulAmt1, MulAmt2);
11512
11513 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011514 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011515 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011516 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011517 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011518 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011519 DAG.getConstant(MulAmt1, VT));
11520
Eric Christopherfd179292009-08-27 18:07:15 +000011521 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011522 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011523 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011524 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011525 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011526 DAG.getConstant(MulAmt2, VT));
11527
11528 // Do not add new nodes to DAG combiner worklist.
11529 DCI.CombineTo(N, NewMul, false);
11530 }
11531 return SDValue();
11532}
11533
Evan Chengad9c0a32009-12-15 00:53:42 +000011534static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11535 SDValue N0 = N->getOperand(0);
11536 SDValue N1 = N->getOperand(1);
11537 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11538 EVT VT = N0.getValueType();
11539
11540 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11541 // since the result of setcc_c is all zero's or all ones.
11542 if (N1C && N0.getOpcode() == ISD::AND &&
11543 N0.getOperand(1).getOpcode() == ISD::Constant) {
11544 SDValue N00 = N0.getOperand(0);
11545 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11546 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11547 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11548 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11549 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11550 APInt ShAmt = N1C->getAPIntValue();
11551 Mask = Mask.shl(ShAmt);
11552 if (Mask != 0)
11553 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11554 N00, DAG.getConstant(Mask, VT));
11555 }
11556 }
11557
11558 return SDValue();
11559}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011560
Nate Begeman740ab032009-01-26 00:52:55 +000011561/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11562/// when possible.
11563static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11564 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011565 EVT VT = N->getValueType(0);
11566 if (!VT.isVector() && VT.isInteger() &&
11567 N->getOpcode() == ISD::SHL)
11568 return PerformSHLCombine(N, DAG);
11569
Nate Begeman740ab032009-01-26 00:52:55 +000011570 // On X86 with SSE2 support, we can transform this to a vector shift if
11571 // all elements are shifted by the same amount. We can't do this in legalize
11572 // because the a constant vector is typically transformed to a constant pool
11573 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011574 if (!Subtarget->hasSSE2())
11575 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011576
Owen Anderson825b72b2009-08-11 20:47:22 +000011577 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011578 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011579
Mon P Wang3becd092009-01-28 08:12:05 +000011580 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011581 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011582 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011583 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011584 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11585 unsigned NumElts = VT.getVectorNumElements();
11586 unsigned i = 0;
11587 for (; i != NumElts; ++i) {
11588 SDValue Arg = ShAmtOp.getOperand(i);
11589 if (Arg.getOpcode() == ISD::UNDEF) continue;
11590 BaseShAmt = Arg;
11591 break;
11592 }
11593 for (; i != NumElts; ++i) {
11594 SDValue Arg = ShAmtOp.getOperand(i);
11595 if (Arg.getOpcode() == ISD::UNDEF) continue;
11596 if (Arg != BaseShAmt) {
11597 return SDValue();
11598 }
11599 }
11600 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011601 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011602 SDValue InVec = ShAmtOp.getOperand(0);
11603 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11604 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11605 unsigned i = 0;
11606 for (; i != NumElts; ++i) {
11607 SDValue Arg = InVec.getOperand(i);
11608 if (Arg.getOpcode() == ISD::UNDEF) continue;
11609 BaseShAmt = Arg;
11610 break;
11611 }
11612 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11613 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011614 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011615 if (C->getZExtValue() == SplatIdx)
11616 BaseShAmt = InVec.getOperand(1);
11617 }
11618 }
11619 if (BaseShAmt.getNode() == 0)
11620 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11621 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011622 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011623 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011624
Mon P Wangefa42202009-09-03 19:56:25 +000011625 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011626 if (EltVT.bitsGT(MVT::i32))
11627 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11628 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011629 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011630
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011631 // The shift amount is identical so we can do a vector shift.
11632 SDValue ValOp = N->getOperand(0);
11633 switch (N->getOpcode()) {
11634 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011635 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011636 break;
11637 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011638 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011639 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011640 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011641 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011642 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011643 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011644 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011645 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011646 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011647 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011648 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011649 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011650 break;
11651 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011652 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011653 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011654 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011655 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011656 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011657 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011658 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011659 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011660 break;
11661 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011662 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011663 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011664 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011665 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011666 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011667 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011668 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011669 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011670 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011671 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011672 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011673 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011674 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011675 }
11676 return SDValue();
11677}
11678
Nate Begemanb65c1752010-12-17 22:55:37 +000011679
Stuart Hastings865f0932011-06-03 23:53:54 +000011680// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
11681// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
11682// and friends. Likewise for OR -> CMPNEQSS.
11683static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
11684 TargetLowering::DAGCombinerInfo &DCI,
11685 const X86Subtarget *Subtarget) {
11686 unsigned opcode;
11687
11688 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
11689 // we're requiring SSE2 for both.
11690 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
11691 SDValue N0 = N->getOperand(0);
11692 SDValue N1 = N->getOperand(1);
11693 SDValue CMP0 = N0->getOperand(1);
11694 SDValue CMP1 = N1->getOperand(1);
11695 DebugLoc DL = N->getDebugLoc();
11696
11697 // The SETCCs should both refer to the same CMP.
11698 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
11699 return SDValue();
11700
11701 SDValue CMP00 = CMP0->getOperand(0);
11702 SDValue CMP01 = CMP0->getOperand(1);
11703 EVT VT = CMP00.getValueType();
11704
11705 if (VT == MVT::f32 || VT == MVT::f64) {
11706 bool ExpectingFlags = false;
11707 // Check for any users that want flags:
11708 for (SDNode::use_iterator UI = N->use_begin(),
11709 UE = N->use_end();
11710 !ExpectingFlags && UI != UE; ++UI)
11711 switch (UI->getOpcode()) {
11712 default:
11713 case ISD::BR_CC:
11714 case ISD::BRCOND:
11715 case ISD::SELECT:
11716 ExpectingFlags = true;
11717 break;
11718 case ISD::CopyToReg:
11719 case ISD::SIGN_EXTEND:
11720 case ISD::ZERO_EXTEND:
11721 case ISD::ANY_EXTEND:
11722 break;
11723 }
11724
11725 if (!ExpectingFlags) {
11726 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
11727 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
11728
11729 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
11730 X86::CondCode tmp = cc0;
11731 cc0 = cc1;
11732 cc1 = tmp;
11733 }
11734
11735 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
11736 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
11737 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
11738 X86ISD::NodeType NTOperator = is64BitFP ?
11739 X86ISD::FSETCCsd : X86ISD::FSETCCss;
11740 // FIXME: need symbolic constants for these magic numbers.
11741 // See X86ATTInstPrinter.cpp:printSSECC().
11742 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
11743 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
11744 DAG.getConstant(x86cc, MVT::i8));
11745 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
11746 OnesOrZeroesF);
11747 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
11748 DAG.getConstant(1, MVT::i32));
11749 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
11750 return OneBitOfTruth;
11751 }
11752 }
11753 }
11754 }
11755 return SDValue();
11756}
11757
Nate Begemanb65c1752010-12-17 22:55:37 +000011758static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11759 TargetLowering::DAGCombinerInfo &DCI,
11760 const X86Subtarget *Subtarget) {
11761 if (DCI.isBeforeLegalizeOps())
11762 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011763
Stuart Hastings865f0932011-06-03 23:53:54 +000011764 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11765 if (R.getNode())
11766 return R;
11767
Nate Begemanb65c1752010-12-17 22:55:37 +000011768 // Want to form PANDN nodes, in the hopes of then easily combining them with
11769 // OR and AND nodes to form PBLEND/PSIGN.
11770 EVT VT = N->getValueType(0);
11771 if (VT != MVT::v2i64)
11772 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011773
Nate Begemanb65c1752010-12-17 22:55:37 +000011774 SDValue N0 = N->getOperand(0);
11775 SDValue N1 = N->getOperand(1);
11776 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011777
Nate Begemanb65c1752010-12-17 22:55:37 +000011778 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011779 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011780 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11781 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11782
11783 // Check RHS for vnot
11784 if (N1.getOpcode() == ISD::XOR &&
11785 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11786 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011787
Nate Begemanb65c1752010-12-17 22:55:37 +000011788 return SDValue();
11789}
11790
Evan Cheng760d1942010-01-04 21:22:48 +000011791static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011792 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011793 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011794 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011795 return SDValue();
11796
Stuart Hastings865f0932011-06-03 23:53:54 +000011797 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
11798 if (R.getNode())
11799 return R;
11800
Evan Cheng760d1942010-01-04 21:22:48 +000011801 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011802 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011803 return SDValue();
11804
Evan Cheng760d1942010-01-04 21:22:48 +000011805 SDValue N0 = N->getOperand(0);
11806 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011807
Nate Begemanb65c1752010-12-17 22:55:37 +000011808 // look for psign/blend
11809 if (Subtarget->hasSSSE3()) {
11810 if (VT == MVT::v2i64) {
11811 // Canonicalize pandn to RHS
11812 if (N0.getOpcode() == X86ISD::PANDN)
11813 std::swap(N0, N1);
11814 // or (and (m, x), (pandn m, y))
11815 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11816 SDValue Mask = N1.getOperand(0);
11817 SDValue X = N1.getOperand(1);
11818 SDValue Y;
11819 if (N0.getOperand(0) == Mask)
11820 Y = N0.getOperand(1);
11821 if (N0.getOperand(1) == Mask)
11822 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011823
Nate Begemanb65c1752010-12-17 22:55:37 +000011824 // Check to see if the mask appeared in both the AND and PANDN and
11825 if (!Y.getNode())
11826 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011827
Nate Begemanb65c1752010-12-17 22:55:37 +000011828 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11829 if (Mask.getOpcode() != ISD::BITCAST ||
11830 X.getOpcode() != ISD::BITCAST ||
11831 Y.getOpcode() != ISD::BITCAST)
11832 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011833
Nate Begemanb65c1752010-12-17 22:55:37 +000011834 // Look through mask bitcast.
11835 Mask = Mask.getOperand(0);
11836 EVT MaskVT = Mask.getValueType();
11837
11838 // Validate that the Mask operand is a vector sra node. The sra node
11839 // will be an intrinsic.
11840 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11841 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011842
Nate Begemanb65c1752010-12-17 22:55:37 +000011843 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11844 // there is no psrai.b
11845 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11846 case Intrinsic::x86_sse2_psrai_w:
11847 case Intrinsic::x86_sse2_psrai_d:
11848 break;
11849 default: return SDValue();
11850 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011851
Nate Begemanb65c1752010-12-17 22:55:37 +000011852 // Check that the SRA is all signbits.
11853 SDValue SraC = Mask.getOperand(2);
11854 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11855 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11856 if ((SraAmt + 1) != EltBits)
11857 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011858
Nate Begemanb65c1752010-12-17 22:55:37 +000011859 DebugLoc DL = N->getDebugLoc();
11860
11861 // Now we know we at least have a plendvb with the mask val. See if
11862 // we can form a psignb/w/d.
11863 // psign = x.type == y.type == mask.type && y = sub(0, x);
11864 X = X.getOperand(0);
11865 Y = Y.getOperand(0);
11866 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11867 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11868 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11869 unsigned Opc = 0;
11870 switch (EltBits) {
11871 case 8: Opc = X86ISD::PSIGNB; break;
11872 case 16: Opc = X86ISD::PSIGNW; break;
11873 case 32: Opc = X86ISD::PSIGND; break;
11874 default: break;
11875 }
11876 if (Opc) {
11877 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11878 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11879 }
11880 }
11881 // PBLENDVB only available on SSE 4.1
11882 if (!Subtarget->hasSSE41())
11883 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011884
Nate Begemanb65c1752010-12-17 22:55:37 +000011885 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11886 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11887 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011888 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011889 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11890 }
11891 }
11892 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011893
Nate Begemanb65c1752010-12-17 22:55:37 +000011894 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011895 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11896 std::swap(N0, N1);
11897 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11898 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011899 if (!N0.hasOneUse() || !N1.hasOneUse())
11900 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011901
11902 SDValue ShAmt0 = N0.getOperand(1);
11903 if (ShAmt0.getValueType() != MVT::i8)
11904 return SDValue();
11905 SDValue ShAmt1 = N1.getOperand(1);
11906 if (ShAmt1.getValueType() != MVT::i8)
11907 return SDValue();
11908 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11909 ShAmt0 = ShAmt0.getOperand(0);
11910 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11911 ShAmt1 = ShAmt1.getOperand(0);
11912
11913 DebugLoc DL = N->getDebugLoc();
11914 unsigned Opc = X86ISD::SHLD;
11915 SDValue Op0 = N0.getOperand(0);
11916 SDValue Op1 = N1.getOperand(0);
11917 if (ShAmt0.getOpcode() == ISD::SUB) {
11918 Opc = X86ISD::SHRD;
11919 std::swap(Op0, Op1);
11920 std::swap(ShAmt0, ShAmt1);
11921 }
11922
Evan Cheng8b1190a2010-04-28 01:18:01 +000011923 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011924 if (ShAmt1.getOpcode() == ISD::SUB) {
11925 SDValue Sum = ShAmt1.getOperand(0);
11926 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011927 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11928 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11929 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11930 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011931 return DAG.getNode(Opc, DL, VT,
11932 Op0, Op1,
11933 DAG.getNode(ISD::TRUNCATE, DL,
11934 MVT::i8, ShAmt0));
11935 }
11936 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11937 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11938 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011939 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011940 return DAG.getNode(Opc, DL, VT,
11941 N0.getOperand(0), N1.getOperand(0),
11942 DAG.getNode(ISD::TRUNCATE, DL,
11943 MVT::i8, ShAmt0));
11944 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011945
Evan Cheng760d1942010-01-04 21:22:48 +000011946 return SDValue();
11947}
11948
Chris Lattner149a4e52008-02-22 02:09:43 +000011949/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011950static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011951 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011952 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11953 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011954 // A preferable solution to the general problem is to figure out the right
11955 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011956
11957 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011958 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011959 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011960 if (VT.getSizeInBits() != 64)
11961 return SDValue();
11962
Devang Patel578efa92009-06-05 21:57:13 +000011963 const Function *F = DAG.getMachineFunction().getFunction();
11964 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011965 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011966 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011967 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011968 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011969 isa<LoadSDNode>(St->getValue()) &&
11970 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11971 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011972 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011973 LoadSDNode *Ld = 0;
11974 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011975 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011976 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011977 // Must be a store of a load. We currently handle two cases: the load
11978 // is a direct child, and it's under an intervening TokenFactor. It is
11979 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011980 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011981 Ld = cast<LoadSDNode>(St->getChain());
11982 else if (St->getValue().hasOneUse() &&
11983 ChainVal->getOpcode() == ISD::TokenFactor) {
11984 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011985 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011986 TokenFactorIndex = i;
11987 Ld = cast<LoadSDNode>(St->getValue());
11988 } else
11989 Ops.push_back(ChainVal->getOperand(i));
11990 }
11991 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011992
Evan Cheng536e6672009-03-12 05:59:15 +000011993 if (!Ld || !ISD::isNormalLoad(Ld))
11994 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011995
Evan Cheng536e6672009-03-12 05:59:15 +000011996 // If this is not the MMX case, i.e. we are just turning i64 load/store
11997 // into f64 load/store, avoid the transformation if there are multiple
11998 // uses of the loaded value.
11999 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
12000 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000012001
Evan Cheng536e6672009-03-12 05:59:15 +000012002 DebugLoc LdDL = Ld->getDebugLoc();
12003 DebugLoc StDL = N->getDebugLoc();
12004 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
12005 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
12006 // pair instead.
12007 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012008 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000012009 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
12010 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012011 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012012 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000012013 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000012014 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000012015 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000012016 Ops.size());
12017 }
Evan Cheng536e6672009-03-12 05:59:15 +000012018 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000012019 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012020 St->isVolatile(), St->isNonTemporal(),
12021 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000012022 }
Evan Cheng536e6672009-03-12 05:59:15 +000012023
12024 // Otherwise, lower to two pairs of 32-bit loads / stores.
12025 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012026 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
12027 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012028
Owen Anderson825b72b2009-08-11 20:47:22 +000012029 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012030 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012031 Ld->isVolatile(), Ld->isNonTemporal(),
12032 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000012033 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000012034 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000012035 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012036 MinAlign(Ld->getAlignment(), 4));
12037
12038 SDValue NewChain = LoLd.getValue(1);
12039 if (TokenFactorIndex != -1) {
12040 Ops.push_back(LoLd);
12041 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000012042 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000012043 Ops.size());
12044 }
12045
12046 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000012047 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
12048 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000012049
12050 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012051 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000012052 St->isVolatile(), St->isNonTemporal(),
12053 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000012054 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000012055 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000012056 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000012057 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000012058 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000012059 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000012060 }
Dan Gohman475871a2008-07-27 21:46:04 +000012061 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000012062}
12063
Chris Lattner6cf73262008-01-25 06:14:17 +000012064/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
12065/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012066static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000012067 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
12068 // F[X]OR(0.0, x) -> x
12069 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000012070 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12071 if (C->getValueAPF().isPosZero())
12072 return N->getOperand(1);
12073 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12074 if (C->getValueAPF().isPosZero())
12075 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000012076 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012077}
12078
12079/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000012080static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000012081 // FAND(0.0, x) -> 0.0
12082 // FAND(x, 0.0) -> 0.0
12083 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
12084 if (C->getValueAPF().isPosZero())
12085 return N->getOperand(0);
12086 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
12087 if (C->getValueAPF().isPosZero())
12088 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000012089 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000012090}
12091
Dan Gohmane5af2d32009-01-29 01:59:02 +000012092static SDValue PerformBTCombine(SDNode *N,
12093 SelectionDAG &DAG,
12094 TargetLowering::DAGCombinerInfo &DCI) {
12095 // BT ignores high bits in the bit index operand.
12096 SDValue Op1 = N->getOperand(1);
12097 if (Op1.hasOneUse()) {
12098 unsigned BitWidth = Op1.getValueSizeInBits();
12099 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
12100 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012101 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
12102 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000012103 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000012104 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
12105 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
12106 DCI.CommitTargetLoweringOpt(TLO);
12107 }
12108 return SDValue();
12109}
Chris Lattner83e6c992006-10-04 06:57:07 +000012110
Eli Friedman7a5e5552009-06-07 06:52:44 +000012111static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
12112 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012113 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000012114 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000012115 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000012116 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000012117 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000012118 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012119 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012120 }
12121 return SDValue();
12122}
12123
Evan Cheng2e489c42009-12-16 00:53:11 +000012124static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
12125 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
12126 // (and (i32 x86isd::setcc_carry), 1)
12127 // This eliminates the zext. This transformation is necessary because
12128 // ISD::SETCC is always legalized to i8.
12129 DebugLoc dl = N->getDebugLoc();
12130 SDValue N0 = N->getOperand(0);
12131 EVT VT = N->getValueType(0);
12132 if (N0.getOpcode() == ISD::AND &&
12133 N0.hasOneUse() &&
12134 N0.getOperand(0).hasOneUse()) {
12135 SDValue N00 = N0.getOperand(0);
12136 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
12137 return SDValue();
12138 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12139 if (!C || C->getZExtValue() != 1)
12140 return SDValue();
12141 return DAG.getNode(ISD::AND, dl, VT,
12142 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
12143 N00.getOperand(0), N00.getOperand(1)),
12144 DAG.getConstant(1, VT));
12145 }
12146
12147 return SDValue();
12148}
12149
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012150// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
12151static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
12152 unsigned X86CC = N->getConstantOperandVal(0);
12153 SDValue EFLAG = N->getOperand(1);
12154 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012155
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012156 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
12157 // a zext and produces an all-ones bit which is more useful than 0/1 in some
12158 // cases.
12159 if (X86CC == X86::COND_B)
12160 return DAG.getNode(ISD::AND, DL, MVT::i8,
12161 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
12162 DAG.getConstant(X86CC, MVT::i8), EFLAG),
12163 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012164
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012165 return SDValue();
12166}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012167
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012168static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG, const X86TargetLowering *XTLI) {
12169 DebugLoc dl = N->getDebugLoc();
12170 SDValue Op0 = N->getOperand(0);
12171 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
12172 // a 32-bit target where SSE doesn't support i64->FP operations.
12173 if (Op0.getOpcode() == ISD::LOAD) {
12174 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
12175 EVT VT = Ld->getValueType(0);
12176 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
12177 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
12178 !XTLI->getSubtarget()->is64Bit() &&
12179 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
12180 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0), Ld->getChain(), Op0, DAG);
12181 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
12182 return FILDChain;
12183 }
12184 }
12185 return SDValue();
12186}
12187
Chris Lattner23a01992010-12-20 01:37:09 +000012188// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
12189static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
12190 X86TargetLowering::DAGCombinerInfo &DCI) {
12191 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
12192 // the result is either zero or one (depending on the input carry bit).
12193 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
12194 if (X86::isZeroNode(N->getOperand(0)) &&
12195 X86::isZeroNode(N->getOperand(1)) &&
12196 // We don't have a good way to replace an EFLAGS use, so only do this when
12197 // dead right now.
12198 SDValue(N, 1).use_empty()) {
12199 DebugLoc DL = N->getDebugLoc();
12200 EVT VT = N->getValueType(0);
12201 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
12202 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
12203 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
12204 DAG.getConstant(X86::COND_B,MVT::i8),
12205 N->getOperand(2)),
12206 DAG.getConstant(1, VT));
12207 return DCI.CombineTo(N, Res1, CarryOut);
12208 }
12209
12210 return SDValue();
12211}
12212
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012213// fold (add Y, (sete X, 0)) -> adc 0, Y
12214// (add Y, (setne X, 0)) -> sbb -1, Y
12215// (sub (sete X, 0), Y) -> sbb 0, Y
12216// (sub (setne X, 0), Y) -> adc -1, Y
12217static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
12218 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012219
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012220 // Look through ZExts.
12221 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
12222 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
12223 return SDValue();
12224
12225 SDValue SetCC = Ext.getOperand(0);
12226 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
12227 return SDValue();
12228
12229 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
12230 if (CC != X86::COND_E && CC != X86::COND_NE)
12231 return SDValue();
12232
12233 SDValue Cmp = SetCC.getOperand(1);
12234 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000012235 !X86::isZeroNode(Cmp.getOperand(1)) ||
12236 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012237 return SDValue();
12238
12239 SDValue CmpOp0 = Cmp.getOperand(0);
12240 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
12241 DAG.getConstant(1, CmpOp0.getValueType()));
12242
12243 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
12244 if (CC == X86::COND_NE)
12245 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
12246 DL, OtherVal.getValueType(), OtherVal,
12247 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
12248 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
12249 DL, OtherVal.getValueType(), OtherVal,
12250 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
12251}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012252
Dan Gohman475871a2008-07-27 21:46:04 +000012253SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012254 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012255 SelectionDAG &DAG = DCI.DAG;
12256 switch (N->getOpcode()) {
12257 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012258 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012259 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012260 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012261 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012262 case ISD::ADD:
12263 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012264 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012265 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012266 case ISD::SHL:
12267 case ISD::SRA:
12268 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012269 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012270 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012271 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000012272 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Chris Lattner6cf73262008-01-25 06:14:17 +000012273 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012274 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12275 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012276 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012277 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012278 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012279 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012280 case X86ISD::SHUFPS: // Handle all target specific shuffles
12281 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012282 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012283 case X86ISD::PUNPCKHBW:
12284 case X86ISD::PUNPCKHWD:
12285 case X86ISD::PUNPCKHDQ:
12286 case X86ISD::PUNPCKHQDQ:
12287 case X86ISD::UNPCKHPS:
12288 case X86ISD::UNPCKHPD:
12289 case X86ISD::PUNPCKLBW:
12290 case X86ISD::PUNPCKLWD:
12291 case X86ISD::PUNPCKLDQ:
12292 case X86ISD::PUNPCKLQDQ:
12293 case X86ISD::UNPCKLPS:
12294 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012295 case X86ISD::VUNPCKLPS:
12296 case X86ISD::VUNPCKLPD:
12297 case X86ISD::VUNPCKLPSY:
12298 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012299 case X86ISD::MOVHLPS:
12300 case X86ISD::MOVLHPS:
12301 case X86ISD::PSHUFD:
12302 case X86ISD::PSHUFHW:
12303 case X86ISD::PSHUFLW:
12304 case X86ISD::MOVSS:
12305 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012306 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012307 }
12308
Dan Gohman475871a2008-07-27 21:46:04 +000012309 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012310}
12311
Evan Chenge5b51ac2010-04-17 06:13:15 +000012312/// isTypeDesirableForOp - Return true if the target has native support for
12313/// the specified value type and it is 'desirable' to use the type for the
12314/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12315/// instruction encodings are longer and some i16 instructions are slow.
12316bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12317 if (!isTypeLegal(VT))
12318 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012319 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012320 return true;
12321
12322 switch (Opc) {
12323 default:
12324 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012325 case ISD::LOAD:
12326 case ISD::SIGN_EXTEND:
12327 case ISD::ZERO_EXTEND:
12328 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012329 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012330 case ISD::SRL:
12331 case ISD::SUB:
12332 case ISD::ADD:
12333 case ISD::MUL:
12334 case ISD::AND:
12335 case ISD::OR:
12336 case ISD::XOR:
12337 return false;
12338 }
12339}
12340
12341/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012342/// beneficial for dag combiner to promote the specified node. If true, it
12343/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012344bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012345 EVT VT = Op.getValueType();
12346 if (VT != MVT::i16)
12347 return false;
12348
Evan Cheng4c26e932010-04-19 19:29:22 +000012349 bool Promote = false;
12350 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012351 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012352 default: break;
12353 case ISD::LOAD: {
12354 LoadSDNode *LD = cast<LoadSDNode>(Op);
12355 // If the non-extending load has a single use and it's not live out, then it
12356 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012357 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12358 Op.hasOneUse()*/) {
12359 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12360 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12361 // The only case where we'd want to promote LOAD (rather then it being
12362 // promoted as an operand is when it's only use is liveout.
12363 if (UI->getOpcode() != ISD::CopyToReg)
12364 return false;
12365 }
12366 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012367 Promote = true;
12368 break;
12369 }
12370 case ISD::SIGN_EXTEND:
12371 case ISD::ZERO_EXTEND:
12372 case ISD::ANY_EXTEND:
12373 Promote = true;
12374 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012375 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012376 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012377 SDValue N0 = Op.getOperand(0);
12378 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012379 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012380 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012381 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012382 break;
12383 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012384 case ISD::ADD:
12385 case ISD::MUL:
12386 case ISD::AND:
12387 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012388 case ISD::XOR:
12389 Commute = true;
12390 // fallthrough
12391 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012392 SDValue N0 = Op.getOperand(0);
12393 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012394 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012395 return false;
12396 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012397 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012398 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012399 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012400 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012401 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012402 }
12403 }
12404
12405 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012406 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012407}
12408
Evan Cheng60c07e12006-07-05 22:17:51 +000012409//===----------------------------------------------------------------------===//
12410// X86 Inline Assembly Support
12411//===----------------------------------------------------------------------===//
12412
Chris Lattnerb8105652009-07-20 17:51:36 +000012413bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12414 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012415
12416 std::string AsmStr = IA->getAsmString();
12417
12418 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012419 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012420 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012421
12422 switch (AsmPieces.size()) {
12423 default: return false;
12424 case 1:
12425 AsmStr = AsmPieces[0];
12426 AsmPieces.clear();
12427 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12428
Chris Lattner7a2bdde2011-04-15 05:18:47 +000012429 // FIXME: this should verify that we are targeting a 486 or better. If not,
Evan Cheng55d42002011-01-08 01:24:27 +000012430 // we will turn this bswap into something that will be lowered to logical ops
12431 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12432 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012433 // bswap $0
12434 if (AsmPieces.size() == 2 &&
12435 (AsmPieces[0] == "bswap" ||
12436 AsmPieces[0] == "bswapq" ||
12437 AsmPieces[0] == "bswapl") &&
12438 (AsmPieces[1] == "$0" ||
12439 AsmPieces[1] == "${0:q}")) {
12440 // No need to check constraints, nothing other than the equivalent of
12441 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012442 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12443 if (!Ty || Ty->getBitWidth() % 16 != 0)
12444 return false;
12445 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012446 }
12447 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012448 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012449 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012450 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012451 AsmPieces[1] == "$$8," &&
12452 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012453 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12454 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012455 const std::string &ConstraintsStr = IA->getConstraintString();
12456 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012457 std::sort(AsmPieces.begin(), AsmPieces.end());
12458 if (AsmPieces.size() == 4 &&
12459 AsmPieces[0] == "~{cc}" &&
12460 AsmPieces[1] == "~{dirflag}" &&
12461 AsmPieces[2] == "~{flags}" &&
12462 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012463 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12464 if (!Ty || Ty->getBitWidth() % 16 != 0)
12465 return false;
12466 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012467 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012468 }
12469 break;
12470 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012471 if (CI->getType()->isIntegerTy(32) &&
12472 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12473 SmallVector<StringRef, 4> Words;
12474 SplitString(AsmPieces[0], Words, " \t,");
12475 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12476 Words[2] == "${0:w}") {
12477 Words.clear();
12478 SplitString(AsmPieces[1], Words, " \t,");
12479 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12480 Words[2] == "$0") {
12481 Words.clear();
12482 SplitString(AsmPieces[2], Words, " \t,");
12483 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12484 Words[2] == "${0:w}") {
12485 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012486 const std::string &ConstraintsStr = IA->getConstraintString();
12487 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012488 std::sort(AsmPieces.begin(), AsmPieces.end());
12489 if (AsmPieces.size() == 4 &&
12490 AsmPieces[0] == "~{cc}" &&
12491 AsmPieces[1] == "~{dirflag}" &&
12492 AsmPieces[2] == "~{flags}" &&
12493 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012494 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12495 if (!Ty || Ty->getBitWidth() % 16 != 0)
12496 return false;
12497 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012498 }
12499 }
12500 }
12501 }
12502 }
Evan Cheng55d42002011-01-08 01:24:27 +000012503
12504 if (CI->getType()->isIntegerTy(64)) {
12505 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12506 if (Constraints.size() >= 2 &&
12507 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12508 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12509 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12510 SmallVector<StringRef, 4> Words;
12511 SplitString(AsmPieces[0], Words, " \t");
12512 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012513 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012514 SplitString(AsmPieces[1], Words, " \t");
12515 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12516 Words.clear();
12517 SplitString(AsmPieces[2], Words, " \t,");
12518 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12519 Words[2] == "%edx") {
12520 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12521 if (!Ty || Ty->getBitWidth() % 16 != 0)
12522 return false;
12523 return IntrinsicLowering::LowerToByteSwap(CI);
12524 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012525 }
12526 }
12527 }
12528 }
12529 break;
12530 }
12531 return false;
12532}
12533
12534
12535
Chris Lattnerf4dff842006-07-11 02:54:03 +000012536/// getConstraintType - Given a constraint letter, return the type of
12537/// constraint it is for this target.
12538X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012539X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12540 if (Constraint.size() == 1) {
12541 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012542 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012543 case 'q':
12544 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012545 case 'f':
12546 case 't':
12547 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012548 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012549 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012550 case 'Y':
12551 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012552 case 'a':
12553 case 'b':
12554 case 'c':
12555 case 'd':
12556 case 'S':
12557 case 'D':
12558 case 'A':
12559 return C_Register;
12560 case 'I':
12561 case 'J':
12562 case 'K':
12563 case 'L':
12564 case 'M':
12565 case 'N':
12566 case 'G':
12567 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012568 case 'e':
12569 case 'Z':
12570 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012571 default:
12572 break;
12573 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012574 }
Chris Lattner4234f572007-03-25 02:14:49 +000012575 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012576}
12577
John Thompson44ab89e2010-10-29 17:29:13 +000012578/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012579/// This object must already have been set up with the operand type
12580/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012581TargetLowering::ConstraintWeight
12582 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012583 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012584 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012585 Value *CallOperandVal = info.CallOperandVal;
12586 // If we don't have a value, we can't do a match,
12587 // but allow it at the lowest weight.
12588 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012589 return CW_Default;
12590 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012591 // Look at the constraint type.
12592 switch (*constraint) {
12593 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012594 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12595 case 'R':
12596 case 'q':
12597 case 'Q':
12598 case 'a':
12599 case 'b':
12600 case 'c':
12601 case 'd':
12602 case 'S':
12603 case 'D':
12604 case 'A':
12605 if (CallOperandVal->getType()->isIntegerTy())
12606 weight = CW_SpecificReg;
12607 break;
12608 case 'f':
12609 case 't':
12610 case 'u':
12611 if (type->isFloatingPointTy())
12612 weight = CW_SpecificReg;
12613 break;
12614 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012615 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012616 weight = CW_SpecificReg;
12617 break;
12618 case 'x':
12619 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012620 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012621 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012622 break;
12623 case 'I':
12624 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12625 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012626 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012627 }
12628 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012629 case 'J':
12630 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12631 if (C->getZExtValue() <= 63)
12632 weight = CW_Constant;
12633 }
12634 break;
12635 case 'K':
12636 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12637 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12638 weight = CW_Constant;
12639 }
12640 break;
12641 case 'L':
12642 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12643 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12644 weight = CW_Constant;
12645 }
12646 break;
12647 case 'M':
12648 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12649 if (C->getZExtValue() <= 3)
12650 weight = CW_Constant;
12651 }
12652 break;
12653 case 'N':
12654 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12655 if (C->getZExtValue() <= 0xff)
12656 weight = CW_Constant;
12657 }
12658 break;
12659 case 'G':
12660 case 'C':
12661 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12662 weight = CW_Constant;
12663 }
12664 break;
12665 case 'e':
12666 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12667 if ((C->getSExtValue() >= -0x80000000LL) &&
12668 (C->getSExtValue() <= 0x7fffffffLL))
12669 weight = CW_Constant;
12670 }
12671 break;
12672 case 'Z':
12673 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12674 if (C->getZExtValue() <= 0xffffffff)
12675 weight = CW_Constant;
12676 }
12677 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012678 }
12679 return weight;
12680}
12681
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012682/// LowerXConstraint - try to replace an X constraint, which matches anything,
12683/// with another that has more specific requirements based on the type of the
12684/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012685const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012686LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012687 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12688 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012689 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012690 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012691 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012692 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012693 return "x";
12694 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012695
Chris Lattner5e764232008-04-26 23:02:14 +000012696 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012697}
12698
Chris Lattner48884cd2007-08-25 00:47:38 +000012699/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12700/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012701void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000012702 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012703 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012704 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012705 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012706
Eric Christopher100c8332011-06-02 23:16:42 +000012707 // Only support length 1 constraints for now.
12708 if (Constraint.length() > 1) return;
12709
12710 char ConstraintLetter = Constraint[0];
12711 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012712 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012713 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012714 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012715 if (C->getZExtValue() <= 31) {
12716 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012717 break;
12718 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012719 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012720 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012721 case 'J':
12722 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012723 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012724 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12725 break;
12726 }
12727 }
12728 return;
12729 case 'K':
12730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012731 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012732 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12733 break;
12734 }
12735 }
12736 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012737 case 'N':
12738 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012739 if (C->getZExtValue() <= 255) {
12740 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012741 break;
12742 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012743 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012744 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012745 case 'e': {
12746 // 32-bit signed value
12747 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012748 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12749 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012750 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012751 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012752 break;
12753 }
12754 // FIXME gcc accepts some relocatable values here too, but only in certain
12755 // memory models; it's complicated.
12756 }
12757 return;
12758 }
12759 case 'Z': {
12760 // 32-bit unsigned value
12761 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012762 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12763 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012764 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12765 break;
12766 }
12767 }
12768 // FIXME gcc accepts some relocatable values here too, but only in certain
12769 // memory models; it's complicated.
12770 return;
12771 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012772 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012773 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012774 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012775 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012776 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012777 break;
12778 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012779
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012780 // In any sort of PIC mode addresses need to be computed at runtime by
12781 // adding in a register or some sort of table lookup. These can't
12782 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012783 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012784 return;
12785
Chris Lattnerdc43a882007-05-03 16:52:29 +000012786 // If we are in non-pic codegen mode, we allow the address of a global (with
12787 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012788 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012789 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012790
Chris Lattner49921962009-05-08 18:23:14 +000012791 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12792 while (1) {
12793 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12794 Offset += GA->getOffset();
12795 break;
12796 } else if (Op.getOpcode() == ISD::ADD) {
12797 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12798 Offset += C->getZExtValue();
12799 Op = Op.getOperand(0);
12800 continue;
12801 }
12802 } else if (Op.getOpcode() == ISD::SUB) {
12803 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12804 Offset += -C->getZExtValue();
12805 Op = Op.getOperand(0);
12806 continue;
12807 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012808 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012809
Chris Lattner49921962009-05-08 18:23:14 +000012810 // Otherwise, this isn't something we can handle, reject it.
12811 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012812 }
Eric Christopherfd179292009-08-27 18:07:15 +000012813
Dan Gohman46510a72010-04-15 01:51:59 +000012814 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012815 // If we require an extra load to get this address, as in PIC mode, we
12816 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012817 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12818 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012819 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012820
Devang Patel0d881da2010-07-06 22:08:15 +000012821 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12822 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012823 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012824 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012825 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012826
Gabor Greifba36cb52008-08-28 21:40:38 +000012827 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012828 Ops.push_back(Result);
12829 return;
12830 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012831 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012832}
12833
Chris Lattner259e97c2006-01-31 19:43:35 +000012834std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012835getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012836 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012837 if (Constraint.size() == 1) {
12838 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012839 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012840 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012841 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12842 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012843 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012844 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12845 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12846 X86::R10D,X86::R11D,X86::R12D,
12847 X86::R13D,X86::R14D,X86::R15D,
12848 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012849 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012850 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12851 X86::SI, X86::DI, X86::R8W,X86::R9W,
12852 X86::R10W,X86::R11W,X86::R12W,
12853 X86::R13W,X86::R14W,X86::R15W,
12854 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012855 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012856 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12857 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12858 X86::R10B,X86::R11B,X86::R12B,
12859 X86::R13B,X86::R14B,X86::R15B,
12860 X86::BPL, X86::SPL, 0);
12861
Owen Anderson825b72b2009-08-11 20:47:22 +000012862 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012863 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12864 X86::RSI, X86::RDI, X86::R8, X86::R9,
12865 X86::R10, X86::R11, X86::R12,
12866 X86::R13, X86::R14, X86::R15,
12867 X86::RBP, X86::RSP, 0);
12868
12869 break;
12870 }
Eric Christopherfd179292009-08-27 18:07:15 +000012871 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012872 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012873 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012874 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012875 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012876 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012877 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012878 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012879 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012880 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12881 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012882 }
12883 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012884
Chris Lattner1efa40f2006-02-22 00:56:39 +000012885 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012886}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012887
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012888std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012889X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012890 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012891 // First, see if this is a constraint that directly corresponds to an LLVM
12892 // register class.
12893 if (Constraint.size() == 1) {
12894 // GCC Constraint Letters
12895 switch (Constraint[0]) {
12896 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012897 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012898 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012899 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012900 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012901 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012902 return std::make_pair(0U, X86::GR16RegisterClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000012903 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012904 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012905 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012906 case 'R': // LEGACY_REGS
12907 if (VT == MVT::i8)
12908 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12909 if (VT == MVT::i16)
12910 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12911 if (VT == MVT::i32 || !Subtarget->is64Bit())
12912 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12913 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012914 case 'f': // FP Stack registers.
12915 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12916 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012917 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012918 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012919 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012920 return std::make_pair(0U, X86::RFP64RegisterClass);
12921 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012922 case 'y': // MMX_REGS if MMX allowed.
12923 if (!Subtarget->hasMMX()) break;
12924 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012925 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012926 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012927 // FALL THROUGH.
12928 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012929 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012930
Owen Anderson825b72b2009-08-11 20:47:22 +000012931 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012932 default: break;
12933 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012934 case MVT::f32:
12935 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012936 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012937 case MVT::f64:
12938 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012939 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012940 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012941 case MVT::v16i8:
12942 case MVT::v8i16:
12943 case MVT::v4i32:
12944 case MVT::v2i64:
12945 case MVT::v4f32:
12946 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012947 return std::make_pair(0U, X86::VR128RegisterClass);
12948 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012949 break;
12950 }
12951 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012952
Chris Lattnerf76d1802006-07-31 23:26:50 +000012953 // Use the default implementation in TargetLowering to convert the register
12954 // constraint into a member of a register class.
12955 std::pair<unsigned, const TargetRegisterClass*> Res;
12956 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012957
12958 // Not found as a standard register?
12959 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012960 // Map st(0) -> st(7) -> ST0
12961 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12962 tolower(Constraint[1]) == 's' &&
12963 tolower(Constraint[2]) == 't' &&
12964 Constraint[3] == '(' &&
12965 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12966 Constraint[5] == ')' &&
12967 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012968
Chris Lattner56d77c72009-09-13 22:41:48 +000012969 Res.first = X86::ST0+Constraint[4]-'0';
12970 Res.second = X86::RFP80RegisterClass;
12971 return Res;
12972 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012973
Chris Lattner56d77c72009-09-13 22:41:48 +000012974 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012975 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012976 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012977 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012978 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012979 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012980
12981 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012982 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012983 Res.first = X86::EFLAGS;
12984 Res.second = X86::CCRRegisterClass;
12985 return Res;
12986 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012987
Dale Johannesen330169f2008-11-13 21:52:36 +000012988 // 'A' means EAX + EDX.
12989 if (Constraint == "A") {
12990 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012991 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012992 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012993 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012994 return Res;
12995 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012996
Chris Lattnerf76d1802006-07-31 23:26:50 +000012997 // Otherwise, check to see if this is a register class of the wrong value
12998 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12999 // turn into {ax},{dx}.
13000 if (Res.second->hasType(VT))
13001 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013002
Chris Lattnerf76d1802006-07-31 23:26:50 +000013003 // All of the single-register GCC register classes map their values onto
13004 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
13005 // really want an 8-bit or 32-bit register, map to the appropriate register
13006 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000013007 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000013008 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013009 unsigned DestReg = 0;
13010 switch (Res.first) {
13011 default: break;
13012 case X86::AX: DestReg = X86::AL; break;
13013 case X86::DX: DestReg = X86::DL; break;
13014 case X86::CX: DestReg = X86::CL; break;
13015 case X86::BX: DestReg = X86::BL; break;
13016 }
13017 if (DestReg) {
13018 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013019 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013020 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013021 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013022 unsigned DestReg = 0;
13023 switch (Res.first) {
13024 default: break;
13025 case X86::AX: DestReg = X86::EAX; break;
13026 case X86::DX: DestReg = X86::EDX; break;
13027 case X86::CX: DestReg = X86::ECX; break;
13028 case X86::BX: DestReg = X86::EBX; break;
13029 case X86::SI: DestReg = X86::ESI; break;
13030 case X86::DI: DestReg = X86::EDI; break;
13031 case X86::BP: DestReg = X86::EBP; break;
13032 case X86::SP: DestReg = X86::ESP; break;
13033 }
13034 if (DestReg) {
13035 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013036 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013037 }
Owen Anderson825b72b2009-08-11 20:47:22 +000013038 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000013039 unsigned DestReg = 0;
13040 switch (Res.first) {
13041 default: break;
13042 case X86::AX: DestReg = X86::RAX; break;
13043 case X86::DX: DestReg = X86::RDX; break;
13044 case X86::CX: DestReg = X86::RCX; break;
13045 case X86::BX: DestReg = X86::RBX; break;
13046 case X86::SI: DestReg = X86::RSI; break;
13047 case X86::DI: DestReg = X86::RDI; break;
13048 case X86::BP: DestReg = X86::RBP; break;
13049 case X86::SP: DestReg = X86::RSP; break;
13050 }
13051 if (DestReg) {
13052 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000013053 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000013054 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000013055 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000013056 } else if (Res.second == X86::FR32RegisterClass ||
13057 Res.second == X86::FR64RegisterClass ||
13058 Res.second == X86::VR128RegisterClass) {
13059 // Handle references to XMM physical registers that got mapped into the
13060 // wrong class. This can happen with constraints like {xmm0} where the
13061 // target independent register mapper will just pick the first match it can
13062 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000013063 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013064 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000013065 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000013066 Res.second = X86::FR64RegisterClass;
13067 else if (X86::VR128RegisterClass->hasType(VT))
13068 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000013069 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013070
Chris Lattnerf76d1802006-07-31 23:26:50 +000013071 return Res;
13072}