blob: bbd3a230e37f3f0e6f73e7150bb4cde98627993c [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"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Evan Cheng10e86422008-04-25 19:11:04 +000058// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000059static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000060 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000061
David Greenea5f26012011-02-07 19:36:54 +000062static SDValue Insert128BitVector(SDValue Result,
63 SDValue Vec,
64 SDValue Idx,
65 SelectionDAG &DAG,
66 DebugLoc dl);
David Greenef125a292011-02-08 19:04:41 +000067
David Greenea5f26012011-02-07 19:36:54 +000068static SDValue Extract128BitVector(SDValue Vec,
69 SDValue Idx,
70 SelectionDAG &DAG,
71 DebugLoc dl);
72
David Greenef125a292011-02-08 19:04:41 +000073static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG);
74
75
David Greenea5f26012011-02-07 19:36:54 +000076/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
77/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000078/// simple subregister reference. Idx is an index in the 128 bits we
79/// want. It need not be aligned to a 128-bit bounday. That makes
80/// lowering EXTRACT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +000081static SDValue Extract128BitVector(SDValue Vec,
82 SDValue Idx,
83 SelectionDAG &DAG,
84 DebugLoc dl) {
85 EVT VT = Vec.getValueType();
86 assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
87
88 EVT ElVT = VT.getVectorElementType();
89
90 int Factor = VT.getSizeInBits() / 128;
91
92 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(),
93 ElVT,
94 VT.getVectorNumElements() / Factor);
95
96 // Extract from UNDEF is UNDEF.
97 if (Vec.getOpcode() == ISD::UNDEF)
98 return DAG.getNode(ISD::UNDEF, dl, ResultVT);
99
100 if (isa<ConstantSDNode>(Idx)) {
101 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
102
103 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
104 // we can match to VEXTRACTF128.
105 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
106
107 // This is the index of the first element of the 128-bit chunk
108 // we want.
109 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
110 * ElemsPerChunk);
111
112 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
113
114 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
115 VecIdx);
116
117 return Result;
118 }
119
120 return SDValue();
121}
122
123/// Generate a DAG to put 128-bits into a vector > 128 bits. This
124/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000125/// simple superregister reference. Idx is an index in the 128 bits
126/// we want. It need not be aligned to a 128-bit bounday. That makes
127/// lowering INSERT_VECTOR_ELT operations easier.
David Greenea5f26012011-02-07 19:36:54 +0000128static SDValue Insert128BitVector(SDValue Result,
129 SDValue Vec,
130 SDValue Idx,
131 SelectionDAG &DAG,
132 DebugLoc dl) {
133 if (isa<ConstantSDNode>(Idx)) {
134 EVT VT = Vec.getValueType();
135 assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
136
137 EVT ElVT = VT.getVectorElementType();
138
139 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
140
141 EVT ResultVT = Result.getValueType();
142
143 // Insert the relevant 128 bits.
144 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
145
146 // This is the index of the first element of the 128-bit chunk
147 // we want.
148 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
149 * ElemsPerChunk);
150
151 SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
152
153 Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
154 VecIdx);
155 return Result;
156 }
157
158 return SDValue();
159}
160
David Greenef125a292011-02-08 19:04:41 +0000161/// Given two vectors, concat them.
162static SDValue ConcatVectors(SDValue Lower, SDValue Upper, SelectionDAG &DAG) {
163 DebugLoc dl = Lower.getDebugLoc();
164
165 assert(Lower.getValueType() == Upper.getValueType() && "Mismatched vectors!");
166
167 EVT VT = EVT::getVectorVT(*DAG.getContext(),
168 Lower.getValueType().getVectorElementType(),
169 Lower.getValueType().getVectorNumElements() * 2);
170
171 // TODO: Generalize to arbitrary vector length (this assumes 256-bit vectors).
172 assert(VT.getSizeInBits() == 256 && "Unsupported vector concat!");
173
174 // Insert the upper subvector.
175 SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Upper,
176 DAG.getConstant(
177 // This is half the length of the result
178 // vector. Start inserting the upper 128
179 // bits here.
180 Lower.getValueType().getVectorNumElements(),
181 MVT::i32),
182 DAG, dl);
183
184 // Insert the lower subvector.
185 Vec = Insert128BitVector(Vec, Lower, DAG.getConstant(0, MVT::i32), DAG, dl);
186 return Vec;
187}
188
Chris Lattnerf0144122009-07-28 03:13:23 +0000189static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000190 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
191 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000192
Evan Cheng2bffee22011-02-01 01:14:13 +0000193 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000194 if (is64Bit)
195 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000196 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000197 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000198
Evan Cheng2bffee22011-02-01 01:14:13 +0000199 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000200 if (is64Bit)
201 return new X8664_ELFTargetObjectFile(TM);
202 return new X8632_ELFTargetObjectFile(TM);
203 }
Evan Cheng2bffee22011-02-01 01:14:13 +0000204 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000205 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000206 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000207}
208
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000209X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000211 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000212 X86ScalarSSEf64 = Subtarget->hasXMMInt();
213 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000215
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000216 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000217 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000218
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000220 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221
222 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000224 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +0000225 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000227
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000228 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000229 // Setup Windows compiler runtime calls.
230 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000231 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
232 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000233 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000234 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000235 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000236 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
237 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000238 }
239
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000240 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000241 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000242 setUseUnderscoreSetJmp(false);
243 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000244 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000245 // MS runtime is weird: it exports _setjmp, but longjmp!
246 setUseUnderscoreSetJmp(true);
247 setUseUnderscoreLongJmp(false);
248 } else {
249 setUseUnderscoreSetJmp(true);
250 setUseUnderscoreLongJmp(true);
251 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000252
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000253 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000255 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000259
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000261
Scott Michelfdc40a02009-02-17 22:15:04 +0000262 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000264 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000266 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
268 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000269
270 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
272 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
273 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
274 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
275 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
276 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000277
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000278 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
279 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
281 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
282 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000283
Evan Cheng25ab6902006-09-08 06:48:29 +0000284 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
286 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000287 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000288 // We have an algorithm for SSE2->double, and we turn this into a
289 // 64-bit FILD followed by conditional FADD for other targets.
290 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000291 // We have an algorithm for SSE2, and we turn this into a 64-bit
292 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000293 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000294 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000295
296 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
297 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
299 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000300
Devang Patel6a784892009-06-05 18:48:29 +0000301 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000302 // SSE has no i16 to fp conversion, only i32
303 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000305 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000307 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
309 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000310 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000311 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
313 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000314 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000315
Dale Johannesen73328d12007-09-19 23:55:34 +0000316 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
317 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
319 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000320
Evan Cheng02568ff2006-01-30 22:13:22 +0000321 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
322 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
324 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000325
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000326 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000328 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000330 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
332 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000333 }
334
335 // Handle FP_TO_UINT by promoting the destination to a larger signed
336 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
338 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
339 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000340
Evan Cheng25ab6902006-09-08 06:48:29 +0000341 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
343 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000344 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000345 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 // Expand FP_TO_UINT into a select.
347 // FIXME: We would like to use a Custom expander here eventually to do
348 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000350 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000351 // With SSE3 we can use fisttpll to convert to a signed i64; without
352 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000354 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000355
Chris Lattner399610a2006-12-05 18:22:22 +0000356 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000357 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000358 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
359 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000360 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000361 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000362 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000364 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000365 }
Chris Lattner21f66852005-12-23 05:15:23 +0000366
Dan Gohmanb00ee212008-02-18 19:34:53 +0000367 // Scalar integer divide and remainder are lowered to use operations that
368 // produce two results, to match the available instructions. This exposes
369 // the two-result form to trivial CSE, which is able to combine x/y and x%y
370 // into a single instruction.
371 //
372 // Scalar integer multiply-high is also lowered to use two-result
373 // operations, to match the available instructions. However, plain multiply
374 // (low) operations are left as Legal, as there are single-result
375 // instructions for this in x86. Using the two-result multiply instructions
376 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000377 for (unsigned i = 0, e = 4; i != e; ++i) {
378 MVT VT = IntVTs[i];
379 setOperationAction(ISD::MULHS, VT, Expand);
380 setOperationAction(ISD::MULHU, VT, Expand);
381 setOperationAction(ISD::SDIV, VT, Expand);
382 setOperationAction(ISD::UDIV, VT, Expand);
383 setOperationAction(ISD::SREM, VT, Expand);
384 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000385
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000386 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000387 setOperationAction(ISD::ADDC, VT, Custom);
388 setOperationAction(ISD::ADDE, VT, Custom);
389 setOperationAction(ISD::SUBC, VT, Custom);
390 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000391 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000392
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
394 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
395 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
396 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000397 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
399 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
400 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
401 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
402 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
403 setOperationAction(ISD::FREM , MVT::f32 , Expand);
404 setOperationAction(ISD::FREM , MVT::f64 , Expand);
405 setOperationAction(ISD::FREM , MVT::f80 , Expand);
406 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
409 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000410 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
411 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
413 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000414 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 }
418
Benjamin Kramer1292c222010-12-04 20:32:23 +0000419 if (Subtarget->hasPOPCNT()) {
420 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
421 } else {
422 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
423 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
424 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
425 if (Subtarget->is64Bit())
426 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
427 }
428
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
430 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000431
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000433 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000434 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000435 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000436 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
438 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
439 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
440 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
441 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000442 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
444 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
445 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
446 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000447 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
449 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000450 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000452
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000453 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
455 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
456 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
457 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000458 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
460 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000461 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000462 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
464 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
465 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
466 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000467 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000469 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
471 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
472 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000473 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
475 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
476 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000477 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000478
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000479 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000481
Eric Christopher9a9d2752010-07-22 02:48:34 +0000482 // We may not have a libcall for MEMBARRIER so we should lower this.
483 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000484
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000485 // On X86 and X86-64, atomic operations are lowered to locked instructions.
486 // Locked instructions, in turn, have implicit fence semantics (all memory
487 // operations are flushed before issuing the locked instruction, and they
488 // are not buffered), so we can fold away the common pattern of
489 // fence-atomic-fence.
490 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000491
Mon P Wang63307c32008-05-05 19:05:59 +0000492 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000493 for (unsigned i = 0, e = 4; i != e; ++i) {
494 MVT VT = IntVTs[i];
495 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
496 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
497 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000498
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000499 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
501 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
506 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000507 }
508
Evan Cheng3c992d22006-03-07 02:02:57 +0000509 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000510 if (!Subtarget->isTargetDarwin() &&
511 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000512 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000514 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000515
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
517 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
518 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
519 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000520 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000521 setExceptionPointerRegister(X86::RAX);
522 setExceptionSelectorRegister(X86::RDX);
523 } else {
524 setExceptionPointerRegister(X86::EAX);
525 setExceptionSelectorRegister(X86::EDX);
526 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
528 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000529
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000531
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000533
Nate Begemanacc398c2006-01-25 18:21:52 +0000534 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::VASTART , MVT::Other, Custom);
536 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000537 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::VAARG , MVT::Other, Custom);
539 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000540 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::VAARG , MVT::Other, Expand);
542 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000543 }
Evan Chengae642192007-03-02 23:16:35 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
546 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000547 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000549 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000551 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000553
Evan Chengc7ce29b2009-02-13 22:36:38 +0000554 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000555 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000556 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
558 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000559
Evan Cheng223547a2006-01-31 22:28:30 +0000560 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::FABS , MVT::f64, Custom);
562 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000563
564 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::FNEG , MVT::f64, Custom);
566 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000567
Evan Cheng68c47cb2007-01-05 07:55:56 +0000568 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
570 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000571
Evan Chengd25e9e82006-02-02 00:28:23 +0000572 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::FSIN , MVT::f64, Expand);
574 setOperationAction(ISD::FCOS , MVT::f64, Expand);
575 setOperationAction(ISD::FSIN , MVT::f32, Expand);
576 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000577
Chris Lattnera54aa942006-01-29 06:26:08 +0000578 // Expand FP immediates into loads from the stack, except for the special
579 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000580 addLegalFPImmediate(APFloat(+0.0)); // xorpd
581 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000582 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000583 // Use SSE for f32, x87 for f64.
584 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
586 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000587
588 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000590
591 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000593
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000595
596 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
598 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000599
600 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::FSIN , MVT::f32, Expand);
602 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603
Nate Begemane1795842008-02-14 08:57:00 +0000604 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000605 addLegalFPImmediate(APFloat(+0.0f)); // xorps
606 addLegalFPImmediate(APFloat(+0.0)); // FLD0
607 addLegalFPImmediate(APFloat(+1.0)); // FLD1
608 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
609 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
610
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000611 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
613 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000614 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000615 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
619 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000620
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
622 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
623 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
624 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000625
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
628 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000629 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000630 addLegalFPImmediate(APFloat(+0.0)); // FLD0
631 addLegalFPImmediate(APFloat(+1.0)); // FLD1
632 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
633 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
635 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
636 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
637 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000639
Dale Johannesen59a58732007-08-05 18:49:15 +0000640 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000641 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
643 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
644 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000645 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000646 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000647 addLegalFPImmediate(TmpFlt); // FLD0
648 TmpFlt.changeSign();
649 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000650
651 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000652 APFloat TmpFlt2(+1.0);
653 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
654 &ignored);
655 addLegalFPImmediate(TmpFlt2); // FLD1
656 TmpFlt2.changeSign();
657 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
658 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000659
Evan Chengc7ce29b2009-02-13 22:36:38 +0000660 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
662 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000663 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000664 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000665
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000666 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
668 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
669 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::FLOG, MVT::f80, Expand);
672 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
673 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
674 setOperationAction(ISD::FEXP, MVT::f80, Expand);
675 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000676
Mon P Wangf007a8b2008-11-06 05:31:54 +0000677 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000678 // (for widening) or expand (for scalarization). Then we will selectively
679 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
681 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
682 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000698 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
699 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000731 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000732 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
736 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
737 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
738 setTruncStoreAction((MVT::SimpleValueType)VT,
739 (MVT::SimpleValueType)InnerVT, Expand);
740 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
741 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
742 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000743 }
744
Evan Chengc7ce29b2009-02-13 22:36:38 +0000745 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
746 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000747 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000748 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000749 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000750 }
751
Dale Johannesen0488fb62010-09-30 23:57:10 +0000752 // MMX-sized vectors (other than x86mmx) are expected to be expanded
753 // into smaller operations.
754 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
755 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
756 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
757 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
758 setOperationAction(ISD::AND, MVT::v8i8, Expand);
759 setOperationAction(ISD::AND, MVT::v4i16, Expand);
760 setOperationAction(ISD::AND, MVT::v2i32, Expand);
761 setOperationAction(ISD::AND, MVT::v1i64, Expand);
762 setOperationAction(ISD::OR, MVT::v8i8, Expand);
763 setOperationAction(ISD::OR, MVT::v4i16, Expand);
764 setOperationAction(ISD::OR, MVT::v2i32, Expand);
765 setOperationAction(ISD::OR, MVT::v1i64, Expand);
766 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
767 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
768 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
769 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
770 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
771 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
772 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
773 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
774 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
775 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
776 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
777 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
778 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000779 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
780 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
781 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
782 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000783
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000784 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000786
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
788 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
789 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
790 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
791 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
792 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
793 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
794 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
795 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
796 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
797 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
798 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000799 }
800
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000801 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000803
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000804 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
805 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
807 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
808 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
809 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000810
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
812 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
813 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
814 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
815 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
816 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
817 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
818 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
819 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
820 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
821 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
822 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
823 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
824 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
825 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
826 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000827
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
829 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
830 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
831 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
834 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
836 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
837 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000838
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000839 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
840 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
841 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
842 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
843 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
844
Evan Cheng2c3ae372006-04-12 21:21:57 +0000845 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
847 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000848 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000849 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000850 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000851 // Do not attempt to custom lower non-128-bit vectors
852 if (!VT.is128BitVector())
853 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000854 setOperationAction(ISD::BUILD_VECTOR,
855 VT.getSimpleVT().SimpleTy, Custom);
856 setOperationAction(ISD::VECTOR_SHUFFLE,
857 VT.getSimpleVT().SimpleTy, Custom);
858 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
859 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000860 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
863 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
864 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
865 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
866 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
867 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000868
Nate Begemancdd1eec2008-02-12 22:51:28 +0000869 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
871 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000872 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000873
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000874 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
876 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000877 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000878
879 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000880 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000881 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000882
Owen Andersond6662ad2009-08-10 20:46:15 +0000883 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000885 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000887 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000889 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000891 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000893 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000894
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000896
Evan Cheng2c3ae372006-04-12 21:21:57 +0000897 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
899 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
900 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
901 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000902
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
904 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000905 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000906
Nate Begeman14d12ca2008-02-11 04:19:36 +0000907 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000908 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
909 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
910 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
911 setOperationAction(ISD::FRINT, MVT::f32, Legal);
912 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
913 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
915 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
916 setOperationAction(ISD::FRINT, MVT::f64, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
918
Nate Begeman14d12ca2008-02-11 04:19:36 +0000919 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000921
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000922 // Can turn SHL into an integer multiply.
923 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000924 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000925
Nate Begeman14d12ca2008-02-11 04:19:36 +0000926 // i8 and i16 vectors are custom , because the source register and source
927 // source memory operand types are not the same width. f32 vectors are
928 // custom since the immediate controlling the insert encodes additional
929 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000939
940 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000943 }
944 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000945
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000946 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000948
David Greene9b9838d2009-06-29 16:47:10 +0000949 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
951 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
952 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
953 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000954 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000955
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
957 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
958 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
959 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000960
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
962 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
963 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
964 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
965 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
966 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000967
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
969 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
970 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
971 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
972 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
973 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000974
David Greene54d8eba2011-01-27 22:38:56 +0000975 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
976 // insert_vector_elt extract_subvector and extract_vector_elt for
977 // 256-bit types.
978 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
979 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
980 ++i) {
981 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
982 // Do not attempt to custom lower non-256-bit vectors
983 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
984 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000985 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000986 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
987 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000988 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000989 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000990 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000991 }
David Greene54d8eba2011-01-27 22:38:56 +0000992 // Custom-lower insert_subvector and extract_subvector based on
993 // the result type.
994 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
995 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
996 ++i) {
997 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
998 // Do not attempt to custom lower non-256-bit vectors
999 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +00001000 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001001
1002 if (MVT(VT).getSizeInBits() == 128) {
1003 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001004 }
David Greene54d8eba2011-01-27 22:38:56 +00001005 else if (MVT(VT).getSizeInBits() == 256) {
1006 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1007 }
David Greene9b9838d2009-06-29 16:47:10 +00001008 }
1009
David Greene54d8eba2011-01-27 22:38:56 +00001010 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1011 // Don't promote loads because we need them for VPERM vector index versions.
1012
1013 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1014 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1015 VT++) {
1016 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1017 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1018 continue;
1019 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1020 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1021 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1022 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1023 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1024 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1025 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1026 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1027 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1028 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1029 }
David Greene9b9838d2009-06-29 16:47:10 +00001030 }
1031
Evan Cheng6be2c582006-04-05 23:38:46 +00001032 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001034
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001035
Eli Friedman962f5492010-06-02 19:35:46 +00001036 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1037 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001038 //
Eli Friedman962f5492010-06-02 19:35:46 +00001039 // FIXME: We really should do custom legalization for addition and
1040 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1041 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001042 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1043 // Add/Sub/Mul with overflow operations are custom lowered.
1044 MVT VT = IntVTs[i];
1045 setOperationAction(ISD::SADDO, VT, Custom);
1046 setOperationAction(ISD::UADDO, VT, Custom);
1047 setOperationAction(ISD::SSUBO, VT, Custom);
1048 setOperationAction(ISD::USUBO, VT, Custom);
1049 setOperationAction(ISD::SMULO, VT, Custom);
1050 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001051 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001052
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001053 // There are no 8-bit 3-address imul/mul instructions
1054 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1055 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001056
Evan Chengd54f2d52009-03-31 19:38:51 +00001057 if (!Subtarget->is64Bit()) {
1058 // These libcalls are not available in 32-bit.
1059 setLibcallName(RTLIB::SHL_I128, 0);
1060 setLibcallName(RTLIB::SRL_I128, 0);
1061 setLibcallName(RTLIB::SRA_I128, 0);
1062 }
1063
Evan Cheng206ee9d2006-07-07 08:33:52 +00001064 // We have target-specific dag combine patterns for the following nodes:
1065 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001066 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001067 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001068 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001069 setTargetDAGCombine(ISD::SHL);
1070 setTargetDAGCombine(ISD::SRA);
1071 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001072 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001073 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001074 setTargetDAGCombine(ISD::ADD);
1075 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001076 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001077 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001078 if (Subtarget->is64Bit())
1079 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001080
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001081 computeRegisterProperties();
1082
Evan Cheng05219282011-01-06 06:52:41 +00001083 // On Darwin, -Os means optimize for size without hurting performance,
1084 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001085 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001086 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001087 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001088 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1089 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1090 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001091 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001092 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001093}
1094
Scott Michel5b8f82e2008-03-10 15:42:14 +00001095
Owen Anderson825b72b2009-08-11 20:47:22 +00001096MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1097 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001098}
1099
1100
Evan Cheng29286502008-01-23 23:17:41 +00001101/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1102/// the desired ByVal argument alignment.
1103static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1104 if (MaxAlign == 16)
1105 return;
1106 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1107 if (VTy->getBitWidth() == 128)
1108 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001109 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1110 unsigned EltAlign = 0;
1111 getMaxByValAlign(ATy->getElementType(), EltAlign);
1112 if (EltAlign > MaxAlign)
1113 MaxAlign = EltAlign;
1114 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1115 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1116 unsigned EltAlign = 0;
1117 getMaxByValAlign(STy->getElementType(i), EltAlign);
1118 if (EltAlign > MaxAlign)
1119 MaxAlign = EltAlign;
1120 if (MaxAlign == 16)
1121 break;
1122 }
1123 }
1124 return;
1125}
1126
1127/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1128/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001129/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1130/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001131unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001132 if (Subtarget->is64Bit()) {
1133 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001134 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001135 if (TyAlign > 8)
1136 return TyAlign;
1137 return 8;
1138 }
1139
Evan Cheng29286502008-01-23 23:17:41 +00001140 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001141 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001142 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001143 return Align;
1144}
Chris Lattner2b02a442007-02-25 08:29:00 +00001145
Evan Chengf0df0312008-05-15 08:39:06 +00001146/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001147/// and store operations as a result of memset, memcpy, and memmove
1148/// lowering. If DstAlign is zero that means it's safe to destination
1149/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1150/// means there isn't a need to check it against alignment requirement,
1151/// probably because the source does not need to be loaded. If
1152/// 'NonScalarIntSafe' is true, that means it's safe to return a
1153/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1154/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1155/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001156/// It returns EVT::Other if the type should be determined using generic
1157/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001158EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001159X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1160 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001161 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001162 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001163 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001164 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1165 // linux. This is because the stack realignment code can't handle certain
1166 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001167 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001168 if (NonScalarIntSafe &&
1169 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001170 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001171 (Subtarget->isUnalignedMemAccessFast() ||
1172 ((DstAlign == 0 || DstAlign >= 16) &&
1173 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001174 Subtarget->getStackAlignment() >= 16) {
1175 if (Subtarget->hasSSE2())
1176 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001177 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001178 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001179 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001180 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001181 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001182 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001183 // Do not use f64 to lower memcpy if source is string constant. It's
1184 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001185 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001186 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001187 }
Evan Chengf0df0312008-05-15 08:39:06 +00001188 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001189 return MVT::i64;
1190 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001191}
1192
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001193/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1194/// current function. The returned value is a member of the
1195/// MachineJumpTableInfo::JTEntryKind enum.
1196unsigned X86TargetLowering::getJumpTableEncoding() const {
1197 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1198 // symbol.
1199 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1200 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001201 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001202
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001203 // Otherwise, use the normal jump table encoding heuristics.
1204 return TargetLowering::getJumpTableEncoding();
1205}
1206
Chris Lattnerc64daab2010-01-26 05:02:42 +00001207const MCExpr *
1208X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1209 const MachineBasicBlock *MBB,
1210 unsigned uid,MCContext &Ctx) const{
1211 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1212 Subtarget->isPICStyleGOT());
1213 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1214 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001215 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1216 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001217}
1218
Evan Chengcc415862007-11-09 01:32:10 +00001219/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1220/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001221SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001222 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001223 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001224 // This doesn't have DebugLoc associated with it, but is not really the
1225 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001226 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001227 return Table;
1228}
1229
Chris Lattner589c6f62010-01-26 06:28:43 +00001230/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1231/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1232/// MCExpr.
1233const MCExpr *X86TargetLowering::
1234getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1235 MCContext &Ctx) const {
1236 // X86-64 uses RIP relative addressing based on the jump table label.
1237 if (Subtarget->isPICStyleRIPRel())
1238 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1239
1240 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001241 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001242}
1243
Bill Wendlingb4202b82009-07-01 18:50:55 +00001244/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001245unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001246 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001247}
1248
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001249// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001250std::pair<const TargetRegisterClass*, uint8_t>
1251X86TargetLowering::findRepresentativeClass(EVT VT) const{
1252 const TargetRegisterClass *RRC = 0;
1253 uint8_t Cost = 1;
1254 switch (VT.getSimpleVT().SimpleTy) {
1255 default:
1256 return TargetLowering::findRepresentativeClass(VT);
1257 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1258 RRC = (Subtarget->is64Bit()
1259 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1260 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001261 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001262 RRC = X86::VR64RegisterClass;
1263 break;
1264 case MVT::f32: case MVT::f64:
1265 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1266 case MVT::v4f32: case MVT::v2f64:
1267 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1268 case MVT::v4f64:
1269 RRC = X86::VR128RegisterClass;
1270 break;
1271 }
1272 return std::make_pair(RRC, Cost);
1273}
1274
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001275// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001276unsigned
1277X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1278 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001279 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001280
1281 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001282 switch (RC->getID()) {
1283 default:
1284 return 0;
1285 case X86::GR32RegClassID:
1286 return 4 - FPDiff;
1287 case X86::GR64RegClassID:
1288 return 8 - FPDiff;
1289 case X86::VR128RegClassID:
1290 return Subtarget->is64Bit() ? 10 : 4;
1291 case X86::VR64RegClassID:
1292 return 4;
1293 }
1294}
1295
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001296bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1297 unsigned &Offset) const {
1298 if (!Subtarget->isTargetLinux())
1299 return false;
1300
1301 if (Subtarget->is64Bit()) {
1302 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1303 Offset = 0x28;
1304 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1305 AddressSpace = 256;
1306 else
1307 AddressSpace = 257;
1308 } else {
1309 // %gs:0x14 on i386
1310 Offset = 0x14;
1311 AddressSpace = 256;
1312 }
1313 return true;
1314}
1315
1316
Chris Lattner2b02a442007-02-25 08:29:00 +00001317//===----------------------------------------------------------------------===//
1318// Return Value Calling Convention Implementation
1319//===----------------------------------------------------------------------===//
1320
Chris Lattner59ed56b2007-02-28 04:55:35 +00001321#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001322
Michael J. Spencerec38de22010-10-10 22:04:20 +00001323bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001324X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001325 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001326 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001327 SmallVector<CCValAssign, 16> RVLocs;
1328 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001329 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001330 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001331}
1332
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333SDValue
1334X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001335 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001337 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001338 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001339 MachineFunction &MF = DAG.getMachineFunction();
1340 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Chris Lattner9774c912007-02-27 05:28:59 +00001342 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1344 RVLocs, *DAG.getContext());
1345 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Evan Chengdcea1632010-02-04 02:40:39 +00001347 // Add the regs to the liveout set for the function.
1348 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1349 for (unsigned i = 0; i != RVLocs.size(); ++i)
1350 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1351 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001354
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001356 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1357 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001358 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1359 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001360
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001361 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001362 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1363 CCValAssign &VA = RVLocs[i];
1364 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001365 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001366 EVT ValVT = ValToCopy.getValueType();
1367
Dale Johannesenc4510512010-09-24 19:05:48 +00001368 // If this is x86-64, and we disabled SSE, we can't return FP values,
1369 // or SSE or MMX vectors.
1370 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1371 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001372 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001373 report_fatal_error("SSE register return with SSE disabled");
1374 }
1375 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1376 // llvm-gcc has never done it right and no one has noticed, so this
1377 // should be OK for now.
1378 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001379 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001380 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001381
Chris Lattner447ff682008-03-11 03:23:40 +00001382 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1383 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001384 if (VA.getLocReg() == X86::ST0 ||
1385 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001386 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1387 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001388 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001390 RetOps.push_back(ValToCopy);
1391 // Don't emit a copytoreg.
1392 continue;
1393 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001394
Evan Cheng242b38b2009-02-23 09:03:22 +00001395 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1396 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001397 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001398 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001399 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001401 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1402 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001403 // If we don't have SSE2 available, convert to v4f32 so the generated
1404 // register is legal.
1405 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001407 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001408 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001409 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001410
Dale Johannesendd64c412009-02-04 00:33:20 +00001411 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001412 Flag = Chain.getValue(1);
1413 }
Dan Gohman61a92132008-04-21 23:59:07 +00001414
1415 // The x86-64 ABI for returning structs by value requires that we copy
1416 // the sret argument into %rax for the return. We saved the argument into
1417 // a virtual register in the entry block, so now we copy the value out
1418 // and into %rax.
1419 if (Subtarget->is64Bit() &&
1420 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1421 MachineFunction &MF = DAG.getMachineFunction();
1422 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1423 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001424 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001425 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001426 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001427
Dale Johannesendd64c412009-02-04 00:33:20 +00001428 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001429 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001430
1431 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001432 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001433 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001434
Chris Lattner447ff682008-03-11 03:23:40 +00001435 RetOps[0] = Chain; // Update chain.
1436
1437 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001438 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001439 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
1441 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001443}
1444
Evan Cheng3d2125c2010-11-30 23:55:39 +00001445bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1446 if (N->getNumValues() != 1)
1447 return false;
1448 if (!N->hasNUsesOfValue(1, 0))
1449 return false;
1450
1451 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001452 if (Copy->getOpcode() != ISD::CopyToReg &&
1453 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001454 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001455
1456 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001457 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001458 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001459 if (UI->getOpcode() != X86ISD::RET_FLAG)
1460 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001461 HasRet = true;
1462 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001463
Evan Cheng1bf891a2010-12-01 22:59:46 +00001464 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001465}
1466
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467/// LowerCallResult - Lower the result values of a call into the
1468/// appropriate copies out of appropriate physical registers.
1469///
1470SDValue
1471X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001472 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001473 const SmallVectorImpl<ISD::InputArg> &Ins,
1474 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001475 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001476
Chris Lattnere32bbf62007-02-28 07:09:55 +00001477 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001478 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001479 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001480 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001481 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001483
Chris Lattner3085e152007-02-25 08:59:22 +00001484 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001485 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001486 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001487 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001488
Torok Edwin3f142c32009-02-01 18:15:56 +00001489 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001490 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001491 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001492 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001493 }
1494
Evan Cheng79fb3b42009-02-20 20:43:02 +00001495 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001496
1497 // If this is a call to a function that returns an fp value on the floating
1498 // point stack, we must guarantee the the value is popped from the stack, so
1499 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1500 // if the return value is not used. We use the FpGET_ST0 instructions
1501 // instead.
1502 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1503 // If we prefer to use the value in xmm registers, copy it out as f80 and
1504 // use a truncate to move it from fp stack reg to xmm reg.
1505 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1506 bool isST0 = VA.getLocReg() == X86::ST0;
1507 unsigned Opc = 0;
1508 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1509 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1510 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1511 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001512 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001513 Ops, 2), 1);
1514 Val = Chain.getValue(0);
1515
1516 // Round the f80 to the right size, which also moves it to the appropriate
1517 // xmm register.
1518 if (CopyVT != VA.getValVT())
1519 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1520 // This truncation won't change the value.
1521 DAG.getIntPtrConstant(1));
1522 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001523 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1524 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1525 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001527 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1529 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001530 } else {
1531 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001533 Val = Chain.getValue(0);
1534 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001535 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001536 } else {
1537 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1538 CopyVT, InFlag).getValue(1);
1539 Val = Chain.getValue(0);
1540 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001541 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001543 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001544
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001546}
1547
1548
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001549//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001550// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001551//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001552// StdCall calling convention seems to be standard for many Windows' API
1553// routines and around. It differs from C calling convention just a little:
1554// callee should clean up the stack, not caller. Symbols should be also
1555// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001556// For info on fast calling convention see Fast Calling Convention (tail call)
1557// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001558
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001560/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1562 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001563 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001564
Dan Gohman98ca4f22009-08-05 01:29:28 +00001565 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001566}
1567
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001568/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001569/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570static bool
1571ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1572 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001573 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001574
Dan Gohman98ca4f22009-08-05 01:29:28 +00001575 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001576}
1577
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001578/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1579/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001580/// the specific parameter attribute. The copy will be passed as a byval
1581/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001582static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001583CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001584 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1585 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001586 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001587
Dale Johannesendd64c412009-02-04 00:33:20 +00001588 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001589 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001590 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001591}
1592
Chris Lattner29689432010-03-11 00:22:57 +00001593/// IsTailCallConvention - Return true if the calling convention is one that
1594/// supports tail call optimization.
1595static bool IsTailCallConvention(CallingConv::ID CC) {
1596 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1597}
1598
Evan Cheng0c439eb2010-01-27 00:07:07 +00001599/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1600/// a tailcall target by changing its ABI.
1601static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001602 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001603}
1604
Dan Gohman98ca4f22009-08-05 01:29:28 +00001605SDValue
1606X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001607 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608 const SmallVectorImpl<ISD::InputArg> &Ins,
1609 DebugLoc dl, SelectionDAG &DAG,
1610 const CCValAssign &VA,
1611 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001612 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001613 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001614 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001615 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001616 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001617 EVT ValVT;
1618
1619 // If value is passed by pointer we have address passed instead of the value
1620 // itself.
1621 if (VA.getLocInfo() == CCValAssign::Indirect)
1622 ValVT = VA.getLocVT();
1623 else
1624 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001625
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001626 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001627 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001628 // In case of tail call optimization mark all arguments mutable. Since they
1629 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001630 if (Flags.isByVal()) {
1631 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001632 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001633 return DAG.getFrameIndex(FI, getPointerTy());
1634 } else {
1635 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001636 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001637 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1638 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001639 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001640 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001641 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001642}
1643
Dan Gohman475871a2008-07-27 21:46:04 +00001644SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001646 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 bool isVarArg,
1648 const SmallVectorImpl<ISD::InputArg> &Ins,
1649 DebugLoc dl,
1650 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001651 SmallVectorImpl<SDValue> &InVals)
1652 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001653 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
Gordon Henriksen86737662008-01-05 16:56:59 +00001656 const Function* Fn = MF.getFunction();
1657 if (Fn->hasExternalLinkage() &&
1658 Subtarget->isTargetCygMing() &&
1659 Fn->getName() == "main")
1660 FuncInfo->setForceFramePointer(true);
1661
Evan Cheng1bc78042006-04-26 01:20:17 +00001662 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001664 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001665
Chris Lattner29689432010-03-11 00:22:57 +00001666 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1667 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001668
Chris Lattner638402b2007-02-28 07:00:42 +00001669 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001670 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1672 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001673
1674 // Allocate shadow area for Win64
1675 if (IsWin64) {
1676 CCInfo.AllocateStack(32, 8);
1677 }
1678
Duncan Sands45907662010-10-31 13:21:44 +00001679 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001680
Chris Lattnerf39f7712007-02-28 05:46:49 +00001681 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001682 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001683 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1684 CCValAssign &VA = ArgLocs[i];
1685 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1686 // places.
1687 assert(VA.getValNo() != LastVal &&
1688 "Don't support value assigned to multiple locs yet");
1689 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001690
Chris Lattnerf39f7712007-02-28 05:46:49 +00001691 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001692 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001693 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001695 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001696 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001699 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001701 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001702 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1703 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001704 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001705 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001706 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001707 RC = X86::VR64RegisterClass;
1708 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001709 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001710
Devang Patel68e6bee2011-02-21 23:21:26 +00001711 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001713
Chris Lattnerf39f7712007-02-28 05:46:49 +00001714 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1715 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1716 // right size.
1717 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001718 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001719 DAG.getValueType(VA.getValVT()));
1720 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001721 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001722 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001723 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001724 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001725
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001726 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001727 // Handle MMX values passed in XMM regs.
1728 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001729 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1730 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001731 } else
1732 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001733 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001734 } else {
1735 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001737 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001738
1739 // If value is passed via pointer - do a load.
1740 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001741 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1742 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001743
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001745 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001746
Dan Gohman61a92132008-04-21 23:59:07 +00001747 // The x86-64 ABI for returning structs by value requires that we copy
1748 // the sret argument into %rax for the return. Save the argument into
1749 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001750 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001751 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1752 unsigned Reg = FuncInfo->getSRetReturnReg();
1753 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001755 FuncInfo->setSRetReturnReg(Reg);
1756 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001759 }
1760
Chris Lattnerf39f7712007-02-28 05:46:49 +00001761 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001762 // Align stack specially for tail calls.
1763 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001764 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001765
Evan Cheng1bc78042006-04-26 01:20:17 +00001766 // If the function takes variable number of arguments, make a frame index for
1767 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001769 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1770 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001771 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001772 }
1773 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001774 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1775
1776 // FIXME: We should really autogenerate these arrays
1777 static const unsigned GPR64ArgRegsWin64[] = {
1778 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001779 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001780 static const unsigned GPR64ArgRegs64Bit[] = {
1781 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1782 };
1783 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001784 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1785 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1786 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001787 const unsigned *GPR64ArgRegs;
1788 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001789
1790 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001791 // The XMM registers which might contain var arg parameters are shadowed
1792 // in their paired GPR. So we only need to save the GPR to their home
1793 // slots.
1794 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001795 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001796 } else {
1797 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1798 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001799
1800 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001801 }
1802 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1803 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001804
Devang Patel578efa92009-06-05 21:57:13 +00001805 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001806 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001807 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001808 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001809 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001810 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001811 // Kernel mode asks for SSE to be disabled, so don't push them
1812 // on the stack.
1813 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001814
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001815 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001816 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001817 // Get to the caller-allocated home save location. Add 8 to account
1818 // for the return address.
1819 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001820 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001821 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001822 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1823 } else {
1824 // For X86-64, if there are vararg parameters that are passed via
1825 // registers, then we must store them to their spots on the stack so they
1826 // may be loaded by deferencing the result of va_next.
1827 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1828 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1829 FuncInfo->setRegSaveFrameIndex(
1830 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001831 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001832 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001833
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001836 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1837 getPointerTy());
1838 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001840 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1841 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001842 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001843 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001845 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001846 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001847 MachinePointerInfo::getFixedStack(
1848 FuncInfo->getRegSaveFrameIndex(), Offset),
1849 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001851 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001853
Dan Gohmanface41a2009-08-16 21:24:25 +00001854 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1855 // Now store the XMM (fp + vector) parameter registers.
1856 SmallVector<SDValue, 11> SaveXMMOps;
1857 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001858
Devang Patel68e6bee2011-02-21 23:21:26 +00001859 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001860 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1861 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001862
Dan Gohman1e93df62010-04-17 14:41:14 +00001863 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1864 FuncInfo->getRegSaveFrameIndex()));
1865 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1866 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001867
Dan Gohmanface41a2009-08-16 21:24:25 +00001868 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001869 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001870 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001871 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1872 SaveXMMOps.push_back(Val);
1873 }
1874 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1875 MVT::Other,
1876 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001878
1879 if (!MemOps.empty())
1880 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1881 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001883 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001884
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001886 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001887 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001888 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001889 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001890 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001891 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001892 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001893 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001894
Gordon Henriksen86737662008-01-05 16:56:59 +00001895 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001896 // RegSaveFrameIndex is X86-64 only.
1897 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001898 if (CallConv == CallingConv::X86_FastCall ||
1899 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001900 // fastcc functions can't have varargs.
1901 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001902 }
Evan Cheng25caf632006-05-23 21:06:34 +00001903
Dan Gohman98ca4f22009-08-05 01:29:28 +00001904 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001905}
1906
Dan Gohman475871a2008-07-27 21:46:04 +00001907SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1909 SDValue StackPtr, SDValue Arg,
1910 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001911 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001912 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001913 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001915 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001916 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001917 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001918
1919 return DAG.getStore(Chain, dl, Arg, PtrOff,
1920 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001921 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001922}
1923
Bill Wendling64e87322009-01-16 19:25:27 +00001924/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001925/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001926SDValue
1927X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001928 SDValue &OutRetAddr, SDValue Chain,
1929 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001930 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001931 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001932 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001933 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001934
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001935 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001936 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1937 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001938 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001939}
1940
1941/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1942/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001943static SDValue
1944EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001946 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001947 // Store the return address to the appropriate stack slot.
1948 if (!FPDiff) return Chain;
1949 // Calculate the new stack slot for the return address.
1950 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001951 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001952 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001955 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001956 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001957 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001958 return Chain;
1959}
1960
Dan Gohman98ca4f22009-08-05 01:29:28 +00001961SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001962X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001963 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001964 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001966 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967 const SmallVectorImpl<ISD::InputArg> &Ins,
1968 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001969 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001970 MachineFunction &MF = DAG.getMachineFunction();
1971 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001972 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001973 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001974 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975
Evan Cheng5f941932010-02-05 02:21:12 +00001976 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001977 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001978 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1979 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001980 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001981
1982 // Sibcalls are automatically detected tailcalls which do not require
1983 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001984 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001985 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001986
1987 if (isTailCall)
1988 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001989 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001990
Chris Lattner29689432010-03-11 00:22:57 +00001991 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1992 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001993
Chris Lattner638402b2007-02-28 07:00:42 +00001994 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001995 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1997 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001998
1999 // Allocate shadow area for Win64
2000 if (IsWin64) {
2001 CCInfo.AllocateStack(32, 8);
2002 }
2003
Duncan Sands45907662010-10-31 13:21:44 +00002004 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Chris Lattner423c5f42007-02-28 05:31:48 +00002006 // Get a count of how many bytes are to be pushed on the stack.
2007 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002008 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002009 // This is a sibcall. The memory operands are available in caller's
2010 // own caller's stack.
2011 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002012 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002013 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002014
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002016 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002018 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002019 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2020 FPDiff = NumBytesCallerPushed - NumBytes;
2021
2022 // Set the delta of movement of the returnaddr stackslot.
2023 // But only set if delta is greater than previous delta.
2024 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2025 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2026 }
2027
Evan Chengf22f9b32010-02-06 03:28:46 +00002028 if (!IsSibcall)
2029 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002030
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002032 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002033 if (isTailCall && FPDiff)
2034 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2035 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002036
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2038 SmallVector<SDValue, 8> MemOpChains;
2039 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002040
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002041 // Walk the register/memloc assignments, inserting copies/loads. In the case
2042 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002043 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2044 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002045 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002046 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002048 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002049
Chris Lattner423c5f42007-02-28 05:31:48 +00002050 // Promote the value if needed.
2051 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002052 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002053 case CCValAssign::Full: break;
2054 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002055 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002056 break;
2057 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002058 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002059 break;
2060 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002061 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2062 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002063 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002064 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2065 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002066 } else
2067 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2068 break;
2069 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002070 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002071 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002072 case CCValAssign::Indirect: {
2073 // Store the argument.
2074 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002075 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002076 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002077 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002078 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002079 Arg = SpillSlot;
2080 break;
2081 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002083
Chris Lattner423c5f42007-02-28 05:31:48 +00002084 if (VA.isRegLoc()) {
2085 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002086 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002087 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2088 // shadow reg if callee is a varargs function.
2089 unsigned ShadowReg = 0;
2090 switch (VA.getLocReg()) {
2091 case X86::XMM0: ShadowReg = X86::RCX; break;
2092 case X86::XMM1: ShadowReg = X86::RDX; break;
2093 case X86::XMM2: ShadowReg = X86::R8; break;
2094 case X86::XMM3: ShadowReg = X86::R9; break;
2095 }
2096 if (ShadowReg)
2097 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2098 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002099 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002100 assert(VA.isMemLoc());
2101 if (StackPtr.getNode() == 0)
2102 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2103 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2104 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002105 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002107
Evan Cheng32fe1032006-05-25 00:59:30 +00002108 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002110 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002111
Evan Cheng347d5f72006-04-28 21:29:37 +00002112 // Build a sequence of copy-to-reg nodes chained together with token chain
2113 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002114 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002115 // Tail call byval lowering might overwrite argument registers so in case of
2116 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002118 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002119 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002120 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002121 InFlag = Chain.getValue(1);
2122 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002123
Chris Lattner88e1fd52009-07-09 04:24:46 +00002124 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002125 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2126 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002127 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002128 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2129 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002130 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002131 InFlag);
2132 InFlag = Chain.getValue(1);
2133 } else {
2134 // If we are tail calling and generating PIC/GOT style code load the
2135 // address of the callee into ECX. The value in ecx is used as target of
2136 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2137 // for tail calls on PIC/GOT architectures. Normally we would just put the
2138 // address of GOT into ebx and then call target@PLT. But for tail calls
2139 // ebx would be restored (since ebx is callee saved) before jumping to the
2140 // target@PLT.
2141
2142 // Note: The actual moving to ECX is done further down.
2143 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2144 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2145 !G->getGlobal()->hasProtectedVisibility())
2146 Callee = LowerGlobalAddress(Callee, DAG);
2147 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002148 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002149 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002150 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002151
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002152 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 // From AMD64 ABI document:
2154 // For calls that may call functions that use varargs or stdargs
2155 // (prototype-less calls or calls to functions containing ellipsis (...) in
2156 // the declaration) %al is used as hidden argument to specify the number
2157 // of SSE registers used. The contents of %al do not need to match exactly
2158 // the number of registers, but must be an ubound on the number of SSE
2159 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002160
Gordon Henriksen86737662008-01-05 16:56:59 +00002161 // Count the number of XMM registers allocated.
2162 static const unsigned XMMArgRegs[] = {
2163 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2164 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2165 };
2166 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002167 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002168 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002169
Dale Johannesendd64c412009-02-04 00:33:20 +00002170 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002172 InFlag = Chain.getValue(1);
2173 }
2174
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002175
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002176 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177 if (isTailCall) {
2178 // Force all the incoming stack arguments to be loaded from the stack
2179 // before any new outgoing arguments are stored to the stack, because the
2180 // outgoing stack slots may alias the incoming argument stack slots, and
2181 // the alias isn't otherwise explicit. This is slightly more conservative
2182 // than necessary, because it means that each store effectively depends
2183 // on every argument instead of just those arguments it would clobber.
2184 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2185
Dan Gohman475871a2008-07-27 21:46:04 +00002186 SmallVector<SDValue, 8> MemOpChains2;
2187 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002189 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002190 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002191 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002192 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2193 CCValAssign &VA = ArgLocs[i];
2194 if (VA.isRegLoc())
2195 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002196 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002197 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002198 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002199 // Create frame index.
2200 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002201 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002202 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002203 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002204
Duncan Sands276dcbd2008-03-21 09:14:45 +00002205 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002206 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002207 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002208 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002209 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002210 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002211 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002212
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2214 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002215 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002216 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002217 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002218 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002220 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002221 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002222 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 }
2224 }
2225
2226 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002228 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002229
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002230 // Copy arguments to their registers.
2231 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002232 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002233 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002234 InFlag = Chain.getValue(1);
2235 }
Dan Gohman475871a2008-07-27 21:46:04 +00002236 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002237
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002239 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002240 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002241 }
2242
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002243 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2244 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2245 // In the 64-bit large code model, we have to make all calls
2246 // through a register, since the call instruction's 32-bit
2247 // pc-relative offset may not be large enough to hold the whole
2248 // address.
2249 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002250 // If the callee is a GlobalAddress node (quite common, every direct call
2251 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2252 // it.
2253
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002254 // We should use extra load for direct calls to dllimported functions in
2255 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002256 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002257 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002258 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002259
Chris Lattner48a7d022009-07-09 05:02:21 +00002260 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2261 // external symbols most go through the PLT in PIC mode. If the symbol
2262 // has hidden or protected visibility, or if it is static or local, then
2263 // we don't need to use the PLT - we can directly call it.
2264 if (Subtarget->isTargetELF() &&
2265 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002266 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002267 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002268 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002269 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2270 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002271 // PC-relative references to external symbols should go through $stub,
2272 // unless we're building with the leopard linker or later, which
2273 // automatically synthesizes these stubs.
2274 OpFlags = X86II::MO_DARWIN_STUB;
2275 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002276
Devang Patel0d881da2010-07-06 22:08:15 +00002277 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002278 G->getOffset(), OpFlags);
2279 }
Bill Wendling056292f2008-09-16 21:48:12 +00002280 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002281 unsigned char OpFlags = 0;
2282
Evan Cheng1bf891a2010-12-01 22:59:46 +00002283 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2284 // external symbols should go through the PLT.
2285 if (Subtarget->isTargetELF() &&
2286 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2287 OpFlags = X86II::MO_PLT;
2288 } else if (Subtarget->isPICStyleStubAny() &&
2289 Subtarget->getDarwinVers() < 9) {
2290 // PC-relative references to external symbols should go through $stub,
2291 // unless we're building with the leopard linker or later, which
2292 // automatically synthesizes these stubs.
2293 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002294 }
Eric Christopherfd179292009-08-27 18:07:15 +00002295
Chris Lattner48a7d022009-07-09 05:02:21 +00002296 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2297 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002298 }
2299
Chris Lattnerd96d0722007-02-25 06:40:16 +00002300 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002301 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002302 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002303
Evan Chengf22f9b32010-02-06 03:28:46 +00002304 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002305 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2306 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002307 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002308 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002309
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002310 Ops.push_back(Chain);
2311 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002312
Dan Gohman98ca4f22009-08-05 01:29:28 +00002313 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002314 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002315
Gordon Henriksen86737662008-01-05 16:56:59 +00002316 // Add argument registers to the end of the list so that they are known live
2317 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002318 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2319 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2320 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002321
Evan Cheng586ccac2008-03-18 23:36:35 +00002322 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002324 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2325
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002326 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002327 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002329
Gabor Greifba36cb52008-08-28 21:40:38 +00002330 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002331 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002332
Dan Gohman98ca4f22009-08-05 01:29:28 +00002333 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002334 // We used to do:
2335 //// If this is the first return lowered for this function, add the regs
2336 //// to the liveout set for the function.
2337 // This isn't right, although it's probably harmless on x86; liveouts
2338 // should be computed from returns not tail calls. Consider a void
2339 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002340 return DAG.getNode(X86ISD::TC_RETURN, dl,
2341 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002342 }
2343
Dale Johannesenace16102009-02-03 19:33:06 +00002344 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002345 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002346
Chris Lattner2d297092006-05-23 18:50:38 +00002347 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002348 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002349 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002350 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002351 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002352 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002353 // pops the hidden struct pointer, so we have to push it back.
2354 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002355 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002356 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002357 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Gordon Henriksenae636f82008-01-03 16:47:34 +00002359 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002360 if (!IsSibcall) {
2361 Chain = DAG.getCALLSEQ_END(Chain,
2362 DAG.getIntPtrConstant(NumBytes, true),
2363 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2364 true),
2365 InFlag);
2366 InFlag = Chain.getValue(1);
2367 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002368
Chris Lattner3085e152007-02-25 08:59:22 +00002369 // Handle result values, copying them out of physregs into vregs that we
2370 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002371 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2372 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002373}
2374
Evan Cheng25ab6902006-09-08 06:48:29 +00002375
2376//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002377// Fast Calling Convention (tail call) implementation
2378//===----------------------------------------------------------------------===//
2379
2380// Like std call, callee cleans arguments, convention except that ECX is
2381// reserved for storing the tail called function address. Only 2 registers are
2382// free for argument passing (inreg). Tail call optimization is performed
2383// provided:
2384// * tailcallopt is enabled
2385// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002386// On X86_64 architecture with GOT-style position independent code only local
2387// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002388// To keep the stack aligned according to platform abi the function
2389// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2390// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002391// If a tail called function callee has more arguments than the caller the
2392// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002393// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002394// original REtADDR, but before the saved framepointer or the spilled registers
2395// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2396// stack layout:
2397// arg1
2398// arg2
2399// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002400// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002401// move area ]
2402// (possible EBP)
2403// ESI
2404// EDI
2405// local1 ..
2406
2407/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2408/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002409unsigned
2410X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2411 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002412 MachineFunction &MF = DAG.getMachineFunction();
2413 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002414 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002415 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002416 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002417 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002418 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002419 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2420 // Number smaller than 12 so just add the difference.
2421 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2422 } else {
2423 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002424 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002425 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002426 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002427 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002428}
2429
Evan Cheng5f941932010-02-05 02:21:12 +00002430/// MatchingStackOffset - Return true if the given stack call argument is
2431/// already available in the same position (relatively) of the caller's
2432/// incoming argument stack.
2433static
2434bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2435 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2436 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002437 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2438 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002439 if (Arg.getOpcode() == ISD::CopyFromReg) {
2440 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002441 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002442 return false;
2443 MachineInstr *Def = MRI->getVRegDef(VR);
2444 if (!Def)
2445 return false;
2446 if (!Flags.isByVal()) {
2447 if (!TII->isLoadFromStackSlot(Def, FI))
2448 return false;
2449 } else {
2450 unsigned Opcode = Def->getOpcode();
2451 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2452 Def->getOperand(1).isFI()) {
2453 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002454 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002455 } else
2456 return false;
2457 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002458 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2459 if (Flags.isByVal())
2460 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002461 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002462 // define @foo(%struct.X* %A) {
2463 // tail call @bar(%struct.X* byval %A)
2464 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002465 return false;
2466 SDValue Ptr = Ld->getBasePtr();
2467 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2468 if (!FINode)
2469 return false;
2470 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002471 } else
2472 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002473
Evan Cheng4cae1332010-03-05 08:38:04 +00002474 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002475 if (!MFI->isFixedObjectIndex(FI))
2476 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002477 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002478}
2479
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2481/// for tail call optimization. Targets which want to do tail call
2482/// optimization should implement this function.
2483bool
2484X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002485 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002487 bool isCalleeStructRet,
2488 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002489 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002490 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002491 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002492 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002493 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002494 CalleeCC != CallingConv::C)
2495 return false;
2496
Evan Cheng7096ae42010-01-29 06:45:59 +00002497 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002498 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002499 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002500 CallingConv::ID CallerCC = CallerF->getCallingConv();
2501 bool CCMatch = CallerCC == CalleeCC;
2502
Dan Gohman1797ed52010-02-08 20:27:50 +00002503 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002504 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002505 return true;
2506 return false;
2507 }
2508
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002509 // Look for obvious safe cases to perform tail call optimization that do not
2510 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002511
Evan Cheng2c12cb42010-03-26 16:26:03 +00002512 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2513 // emit a special epilogue.
2514 if (RegInfo->needsStackRealignment(MF))
2515 return false;
2516
Eric Christopher90eb4022010-07-22 00:26:08 +00002517 // Do not sibcall optimize vararg calls unless the call site is not passing
2518 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002519 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002520 return false;
2521
Evan Chenga375d472010-03-15 18:54:48 +00002522 // Also avoid sibcall optimization if either caller or callee uses struct
2523 // return semantics.
2524 if (isCalleeStructRet || isCallerStructRet)
2525 return false;
2526
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002527 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2528 // Therefore if it's not used by the call it is not safe to optimize this into
2529 // a sibcall.
2530 bool Unused = false;
2531 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2532 if (!Ins[i].Used) {
2533 Unused = true;
2534 break;
2535 }
2536 }
2537 if (Unused) {
2538 SmallVector<CCValAssign, 16> RVLocs;
2539 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2540 RVLocs, *DAG.getContext());
2541 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002542 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002543 CCValAssign &VA = RVLocs[i];
2544 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2545 return false;
2546 }
2547 }
2548
Evan Cheng13617962010-04-30 01:12:32 +00002549 // If the calling conventions do not match, then we'd better make sure the
2550 // results are returned in the same way as what the caller expects.
2551 if (!CCMatch) {
2552 SmallVector<CCValAssign, 16> RVLocs1;
2553 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2554 RVLocs1, *DAG.getContext());
2555 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2556
2557 SmallVector<CCValAssign, 16> RVLocs2;
2558 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2559 RVLocs2, *DAG.getContext());
2560 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2561
2562 if (RVLocs1.size() != RVLocs2.size())
2563 return false;
2564 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2565 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2566 return false;
2567 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2568 return false;
2569 if (RVLocs1[i].isRegLoc()) {
2570 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2571 return false;
2572 } else {
2573 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2574 return false;
2575 }
2576 }
2577 }
2578
Evan Chenga6bff982010-01-30 01:22:00 +00002579 // If the callee takes no arguments then go on to check the results of the
2580 // call.
2581 if (!Outs.empty()) {
2582 // Check if stack adjustment is needed. For now, do not do this if any
2583 // argument is passed on the stack.
2584 SmallVector<CCValAssign, 16> ArgLocs;
2585 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2586 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002587
2588 // Allocate shadow area for Win64
2589 if (Subtarget->isTargetWin64()) {
2590 CCInfo.AllocateStack(32, 8);
2591 }
2592
Duncan Sands45907662010-10-31 13:21:44 +00002593 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002594 if (CCInfo.getNextStackOffset()) {
2595 MachineFunction &MF = DAG.getMachineFunction();
2596 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2597 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002598
2599 // Check if the arguments are already laid out in the right way as
2600 // the caller's fixed stack objects.
2601 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002602 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2603 const X86InstrInfo *TII =
2604 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002605 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2606 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002607 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002608 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002609 if (VA.getLocInfo() == CCValAssign::Indirect)
2610 return false;
2611 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002612 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2613 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002614 return false;
2615 }
2616 }
2617 }
Evan Cheng9c044672010-05-29 01:35:22 +00002618
2619 // If the tailcall address may be in a register, then make sure it's
2620 // possible to register allocate for it. In 32-bit, the call address can
2621 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002622 // callee-saved registers are restored. These happen to be the same
2623 // registers used to pass 'inreg' arguments so watch out for those.
2624 if (!Subtarget->is64Bit() &&
2625 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002626 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002627 unsigned NumInRegs = 0;
2628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2629 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002630 if (!VA.isRegLoc())
2631 continue;
2632 unsigned Reg = VA.getLocReg();
2633 switch (Reg) {
2634 default: break;
2635 case X86::EAX: case X86::EDX: case X86::ECX:
2636 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002637 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002638 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002639 }
2640 }
2641 }
Evan Chenga6bff982010-01-30 01:22:00 +00002642 }
Evan Chengb1712452010-01-27 06:25:16 +00002643
Dale Johannesend155d7e2010-10-25 22:17:05 +00002644 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002645 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002646 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2647 return false;
2648
Evan Cheng86809cc2010-02-03 03:28:02 +00002649 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002650}
2651
Dan Gohman3df24e62008-09-03 23:12:08 +00002652FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002653X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2654 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002655}
2656
2657
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002658//===----------------------------------------------------------------------===//
2659// Other Lowering Hooks
2660//===----------------------------------------------------------------------===//
2661
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002662static bool MayFoldLoad(SDValue Op) {
2663 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2664}
2665
2666static bool MayFoldIntoStore(SDValue Op) {
2667 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2668}
2669
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002670static bool isTargetShuffle(unsigned Opcode) {
2671 switch(Opcode) {
2672 default: return false;
2673 case X86ISD::PSHUFD:
2674 case X86ISD::PSHUFHW:
2675 case X86ISD::PSHUFLW:
2676 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002677 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002678 case X86ISD::SHUFPS:
2679 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002680 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002681 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002682 case X86ISD::MOVLPS:
2683 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002684 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002685 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002686 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002687 case X86ISD::MOVSS:
2688 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002689 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002690 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002691 case X86ISD::VUNPCKLPS:
2692 case X86ISD::VUNPCKLPD:
2693 case X86ISD::VUNPCKLPSY:
2694 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002695 case X86ISD::PUNPCKLWD:
2696 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002697 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002698 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002699 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002700 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002701 case X86ISD::PUNPCKHWD:
2702 case X86ISD::PUNPCKHBW:
2703 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002704 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002705 return true;
2706 }
2707 return false;
2708}
2709
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002710static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002711 SDValue V1, SelectionDAG &DAG) {
2712 switch(Opc) {
2713 default: llvm_unreachable("Unknown x86 shuffle node");
2714 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 Lopes5023ef22010-08-31 22:22:11 +00002717 return DAG.getNode(Opc, dl, VT, V1);
2718 }
2719
2720 return SDValue();
2721}
2722
2723static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002724 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002725 switch(Opc) {
2726 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002727 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002728 case X86ISD::PSHUFHW:
2729 case X86ISD::PSHUFLW:
2730 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2731 }
2732
2733 return SDValue();
2734}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002735
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002736static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2737 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2738 switch(Opc) {
2739 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002740 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002741 case X86ISD::SHUFPD:
2742 case X86ISD::SHUFPS:
2743 return DAG.getNode(Opc, dl, VT, V1, V2,
2744 DAG.getConstant(TargetMask, MVT::i8));
2745 }
2746 return SDValue();
2747}
2748
2749static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2750 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2751 switch(Opc) {
2752 default: llvm_unreachable("Unknown x86 shuffle node");
2753 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002754 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002755 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002756 case X86ISD::MOVLPS:
2757 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002758 case X86ISD::MOVSS:
2759 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002760 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002761 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002762 case X86ISD::VUNPCKLPS:
2763 case X86ISD::VUNPCKLPD:
2764 case X86ISD::VUNPCKLPSY:
2765 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002766 case X86ISD::PUNPCKLWD:
2767 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002768 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002769 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002770 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002771 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002772 case X86ISD::PUNPCKHWD:
2773 case X86ISD::PUNPCKHBW:
2774 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002775 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002776 return DAG.getNode(Opc, dl, VT, V1, V2);
2777 }
2778 return SDValue();
2779}
2780
Dan Gohmand858e902010-04-17 15:26:15 +00002781SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002782 MachineFunction &MF = DAG.getMachineFunction();
2783 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2784 int ReturnAddrIndex = FuncInfo->getRAIndex();
2785
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002786 if (ReturnAddrIndex == 0) {
2787 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002788 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002789 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002790 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002791 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002792 }
2793
Evan Cheng25ab6902006-09-08 06:48:29 +00002794 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002795}
2796
2797
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002798bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2799 bool hasSymbolicDisplacement) {
2800 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002801 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002802 return false;
2803
2804 // If we don't have a symbolic displacement - we don't have any extra
2805 // restrictions.
2806 if (!hasSymbolicDisplacement)
2807 return true;
2808
2809 // FIXME: Some tweaks might be needed for medium code model.
2810 if (M != CodeModel::Small && M != CodeModel::Kernel)
2811 return false;
2812
2813 // For small code model we assume that latest object is 16MB before end of 31
2814 // bits boundary. We may also accept pretty large negative constants knowing
2815 // that all objects are in the positive half of address space.
2816 if (M == CodeModel::Small && Offset < 16*1024*1024)
2817 return true;
2818
2819 // For kernel code model we know that all object resist in the negative half
2820 // of 32bits address space. We may not accept negative offsets, since they may
2821 // be just off and we may accept pretty large positive ones.
2822 if (M == CodeModel::Kernel && Offset > 0)
2823 return true;
2824
2825 return false;
2826}
2827
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002828/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2829/// specific condition code, returning the condition code and the LHS/RHS of the
2830/// comparison to make.
2831static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2832 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002833 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002834 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2835 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2836 // X > -1 -> X == 0, jump !sign.
2837 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002838 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002839 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2840 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002841 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002842 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002843 // X < 1 -> X <= 0
2844 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002845 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002846 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002847 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002848
Evan Chengd9558e02006-01-06 00:43:03 +00002849 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002850 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002851 case ISD::SETEQ: return X86::COND_E;
2852 case ISD::SETGT: return X86::COND_G;
2853 case ISD::SETGE: return X86::COND_GE;
2854 case ISD::SETLT: return X86::COND_L;
2855 case ISD::SETLE: return X86::COND_LE;
2856 case ISD::SETNE: return X86::COND_NE;
2857 case ISD::SETULT: return X86::COND_B;
2858 case ISD::SETUGT: return X86::COND_A;
2859 case ISD::SETULE: return X86::COND_BE;
2860 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002861 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002862 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002863
Chris Lattner4c78e022008-12-23 23:42:27 +00002864 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002865
Chris Lattner4c78e022008-12-23 23:42:27 +00002866 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002867 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2868 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002869 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2870 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002871 }
2872
Chris Lattner4c78e022008-12-23 23:42:27 +00002873 switch (SetCCOpcode) {
2874 default: break;
2875 case ISD::SETOLT:
2876 case ISD::SETOLE:
2877 case ISD::SETUGT:
2878 case ISD::SETUGE:
2879 std::swap(LHS, RHS);
2880 break;
2881 }
2882
2883 // On a floating point condition, the flags are set as follows:
2884 // ZF PF CF op
2885 // 0 | 0 | 0 | X > Y
2886 // 0 | 0 | 1 | X < Y
2887 // 1 | 0 | 0 | X == Y
2888 // 1 | 1 | 1 | unordered
2889 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002890 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002891 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002892 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002893 case ISD::SETOLT: // flipped
2894 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002895 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002896 case ISD::SETOLE: // flipped
2897 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002898 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002899 case ISD::SETUGT: // flipped
2900 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002901 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002902 case ISD::SETUGE: // flipped
2903 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002904 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002905 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002906 case ISD::SETNE: return X86::COND_NE;
2907 case ISD::SETUO: return X86::COND_P;
2908 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002909 case ISD::SETOEQ:
2910 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002911 }
Evan Chengd9558e02006-01-06 00:43:03 +00002912}
2913
Evan Cheng4a460802006-01-11 00:33:36 +00002914/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2915/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002916/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002917static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002918 switch (X86CC) {
2919 default:
2920 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002921 case X86::COND_B:
2922 case X86::COND_BE:
2923 case X86::COND_E:
2924 case X86::COND_P:
2925 case X86::COND_A:
2926 case X86::COND_AE:
2927 case X86::COND_NE:
2928 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002929 return true;
2930 }
2931}
2932
Evan Chengeb2f9692009-10-27 19:56:55 +00002933/// isFPImmLegal - Returns true if the target can instruction select the
2934/// specified FP immediate natively. If false, the legalizer will
2935/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002936bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002937 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2938 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2939 return true;
2940 }
2941 return false;
2942}
2943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2945/// the specified range (L, H].
2946static bool isUndefOrInRange(int Val, int Low, int Hi) {
2947 return (Val < 0) || (Val >= Low && Val < Hi);
2948}
2949
2950/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2951/// specified value.
2952static bool isUndefOrEqual(int Val, int CmpVal) {
2953 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002954 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002956}
2957
Nate Begeman9008ca62009-04-27 18:41:29 +00002958/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2959/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2960/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002961static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002962 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002963 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002964 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002965 return (Mask[0] < 2 && Mask[1] < 2);
2966 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002967}
2968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002970 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002971 N->getMask(M);
2972 return ::isPSHUFDMask(M, N->getValueType(0));
2973}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002974
Nate Begeman9008ca62009-04-27 18:41:29 +00002975/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2976/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002977static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002979 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002980
Nate Begeman9008ca62009-04-27 18:41:29 +00002981 // Lower quadword copied in order or undef.
2982 for (int i = 0; i != 4; ++i)
2983 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002984 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002985
Evan Cheng506d3df2006-03-29 23:07:14 +00002986 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 for (int i = 4; i != 8; ++i)
2988 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002989 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002990
Evan Cheng506d3df2006-03-29 23:07:14 +00002991 return true;
2992}
2993
Nate Begeman9008ca62009-04-27 18:41:29 +00002994bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002995 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002996 N->getMask(M);
2997 return ::isPSHUFHWMask(M, N->getValueType(0));
2998}
Evan Cheng506d3df2006-03-29 23:07:14 +00002999
Nate Begeman9008ca62009-04-27 18:41:29 +00003000/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3001/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003002static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003004 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003005
Rafael Espindola15684b22009-04-24 12:40:33 +00003006 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 for (int i = 4; i != 8; ++i)
3008 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003009 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003010
Rafael Espindola15684b22009-04-24 12:40:33 +00003011 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003012 for (int i = 0; i != 4; ++i)
3013 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003014 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003015
Rafael Espindola15684b22009-04-24 12:40:33 +00003016 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003017}
3018
Nate Begeman9008ca62009-04-27 18:41:29 +00003019bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003020 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 N->getMask(M);
3022 return ::isPSHUFLWMask(M, N->getValueType(0));
3023}
3024
Nate Begemana09008b2009-10-19 02:17:23 +00003025/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3026/// is suitable for input to PALIGNR.
3027static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3028 bool hasSSSE3) {
3029 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003030
Nate Begemana09008b2009-10-19 02:17:23 +00003031 // Do not handle v2i64 / v2f64 shuffles with palignr.
3032 if (e < 4 || !hasSSSE3)
3033 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003034
Nate Begemana09008b2009-10-19 02:17:23 +00003035 for (i = 0; i != e; ++i)
3036 if (Mask[i] >= 0)
3037 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003038
Nate Begemana09008b2009-10-19 02:17:23 +00003039 // All undef, not a palignr.
3040 if (i == e)
3041 return false;
3042
3043 // Determine if it's ok to perform a palignr with only the LHS, since we
3044 // don't have access to the actual shuffle elements to see if RHS is undef.
3045 bool Unary = Mask[i] < (int)e;
3046 bool NeedsUnary = false;
3047
3048 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003049
Nate Begemana09008b2009-10-19 02:17:23 +00003050 // Check the rest of the elements to see if they are consecutive.
3051 for (++i; i != e; ++i) {
3052 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003053 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003054 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003055
Nate Begemana09008b2009-10-19 02:17:23 +00003056 Unary = Unary && (m < (int)e);
3057 NeedsUnary = NeedsUnary || (m < s);
3058
3059 if (NeedsUnary && !Unary)
3060 return false;
3061 if (Unary && m != ((s+i) & (e-1)))
3062 return false;
3063 if (!Unary && m != (s+i))
3064 return false;
3065 }
3066 return true;
3067}
3068
3069bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3070 SmallVector<int, 8> M;
3071 N->getMask(M);
3072 return ::isPALIGNRMask(M, N->getValueType(0), true);
3073}
3074
Evan Cheng14aed5e2006-03-24 01:18:28 +00003075/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3076/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003077static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 int NumElems = VT.getVectorNumElements();
3079 if (NumElems != 2 && NumElems != 4)
3080 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003081
Nate Begeman9008ca62009-04-27 18:41:29 +00003082 int Half = NumElems / 2;
3083 for (int i = 0; i < Half; ++i)
3084 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003085 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 for (int i = Half; i < NumElems; ++i)
3087 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003088 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003089
Evan Cheng14aed5e2006-03-24 01:18:28 +00003090 return true;
3091}
3092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3094 SmallVector<int, 8> M;
3095 N->getMask(M);
3096 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003097}
3098
Evan Cheng213d2cf2007-05-17 18:45:50 +00003099/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003100/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3101/// half elements to come from vector 1 (which would equal the dest.) and
3102/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003103static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003105
3106 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003108
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 int Half = NumElems / 2;
3110 for (int i = 0; i < Half; ++i)
3111 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003112 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 for (int i = Half; i < NumElems; ++i)
3114 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003115 return false;
3116 return true;
3117}
3118
Nate Begeman9008ca62009-04-27 18:41:29 +00003119static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3120 SmallVector<int, 8> M;
3121 N->getMask(M);
3122 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003123}
3124
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003125/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3126/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003127bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3128 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003129 return false;
3130
Evan Cheng2064a2b2006-03-28 06:50:32 +00003131 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3133 isUndefOrEqual(N->getMaskElt(1), 7) &&
3134 isUndefOrEqual(N->getMaskElt(2), 2) &&
3135 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003136}
3137
Nate Begeman0b10b912009-11-07 23:17:15 +00003138/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3139/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3140/// <2, 3, 2, 3>
3141bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3142 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003143
Nate Begeman0b10b912009-11-07 23:17:15 +00003144 if (NumElems != 4)
3145 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003146
Nate Begeman0b10b912009-11-07 23:17:15 +00003147 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3148 isUndefOrEqual(N->getMaskElt(1), 3) &&
3149 isUndefOrEqual(N->getMaskElt(2), 2) &&
3150 isUndefOrEqual(N->getMaskElt(3), 3);
3151}
3152
Evan Cheng5ced1d82006-04-06 23:23:56 +00003153/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3154/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003155bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3156 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157
Evan Cheng5ced1d82006-04-06 23:23:56 +00003158 if (NumElems != 2 && NumElems != 4)
3159 return false;
3160
Evan Chengc5cdff22006-04-07 21:53:05 +00003161 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003163 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003164
Evan Chengc5cdff22006-04-07 21:53:05 +00003165 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003167 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003168
3169 return true;
3170}
3171
Nate Begeman0b10b912009-11-07 23:17:15 +00003172/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3173/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3174bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003176
Evan Cheng5ced1d82006-04-06 23:23:56 +00003177 if (NumElems != 2 && NumElems != 4)
3178 return false;
3179
Evan Chengc5cdff22006-04-07 21:53:05 +00003180 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003181 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003182 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 for (unsigned i = 0; i < NumElems/2; ++i)
3185 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003186 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003187
3188 return true;
3189}
3190
Evan Cheng0038e592006-03-28 00:39:58 +00003191/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3192/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003193static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003194 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003196 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003197 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003198
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3200 int BitI = Mask[i];
3201 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003202 if (!isUndefOrEqual(BitI, j))
3203 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003204 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003205 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003206 return false;
3207 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003208 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003209 return false;
3210 }
Evan Cheng0038e592006-03-28 00:39:58 +00003211 }
Evan Cheng0038e592006-03-28 00:39:58 +00003212 return true;
3213}
3214
Nate Begeman9008ca62009-04-27 18:41:29 +00003215bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3216 SmallVector<int, 8> M;
3217 N->getMask(M);
3218 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003219}
3220
Evan Cheng4fcb9222006-03-28 02:43:26 +00003221/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3222/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003223static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003224 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003226 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003227 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003228
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3230 int BitI = Mask[i];
3231 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003232 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003233 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003234 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003235 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003236 return false;
3237 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003238 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003239 return false;
3240 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003241 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003242 return true;
3243}
3244
Nate Begeman9008ca62009-04-27 18:41:29 +00003245bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3246 SmallVector<int, 8> M;
3247 N->getMask(M);
3248 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003249}
3250
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003251/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3252/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3253/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003254static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003256 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003257 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003258
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3260 int BitI = Mask[i];
3261 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003262 if (!isUndefOrEqual(BitI, j))
3263 return false;
3264 if (!isUndefOrEqual(BitI1, j))
3265 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003266 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003267 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003268}
3269
Nate Begeman9008ca62009-04-27 18:41:29 +00003270bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3271 SmallVector<int, 8> M;
3272 N->getMask(M);
3273 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3274}
3275
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003276/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3277/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3278/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003279static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003280 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003281 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3282 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003283
Nate Begeman9008ca62009-04-27 18:41:29 +00003284 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3285 int BitI = Mask[i];
3286 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003287 if (!isUndefOrEqual(BitI, j))
3288 return false;
3289 if (!isUndefOrEqual(BitI1, j))
3290 return false;
3291 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003292 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003293}
3294
Nate Begeman9008ca62009-04-27 18:41:29 +00003295bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3296 SmallVector<int, 8> M;
3297 N->getMask(M);
3298 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3299}
3300
Evan Cheng017dcc62006-04-21 01:05:10 +00003301/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3302/// specifies a shuffle of elements that is suitable for input to MOVSS,
3303/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003304static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003305 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003306 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003307
3308 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003309
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003311 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003312
Nate Begeman9008ca62009-04-27 18:41:29 +00003313 for (int i = 1; i < NumElts; ++i)
3314 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003315 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003316
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003317 return true;
3318}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003319
Nate Begeman9008ca62009-04-27 18:41:29 +00003320bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3321 SmallVector<int, 8> M;
3322 N->getMask(M);
3323 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003324}
3325
Evan Cheng017dcc62006-04-21 01:05:10 +00003326/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3327/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003328/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003329static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003330 bool V2IsSplat = false, bool V2IsUndef = false) {
3331 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003332 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003333 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003334
Nate Begeman9008ca62009-04-27 18:41:29 +00003335 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003336 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003337
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 for (int i = 1; i < NumOps; ++i)
3339 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3340 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3341 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003342 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003343
Evan Cheng39623da2006-04-20 08:58:49 +00003344 return true;
3345}
3346
Nate Begeman9008ca62009-04-27 18:41:29 +00003347static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003348 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 SmallVector<int, 8> M;
3350 N->getMask(M);
3351 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003352}
3353
Evan Chengd9539472006-04-14 21:59:03 +00003354/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3355/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003356bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3357 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003358 return false;
3359
3360 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003361 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 int Elt = N->getMaskElt(i);
3363 if (Elt >= 0 && Elt != 1)
3364 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003365 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003366
3367 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003368 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 int Elt = N->getMaskElt(i);
3370 if (Elt >= 0 && Elt != 3)
3371 return false;
3372 if (Elt == 3)
3373 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003374 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003375 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003376 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003377 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003378}
3379
3380/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3381/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003382bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3383 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003384 return false;
3385
3386 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 for (unsigned i = 0; i < 2; ++i)
3388 if (N->getMaskElt(i) > 0)
3389 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003390
3391 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003392 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 int Elt = N->getMaskElt(i);
3394 if (Elt >= 0 && Elt != 2)
3395 return false;
3396 if (Elt == 2)
3397 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003398 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003400 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003401}
3402
Evan Cheng0b457f02008-09-25 20:50:48 +00003403/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3404/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003405bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3406 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003407
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 for (int i = 0; i < e; ++i)
3409 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003410 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003411 for (int i = 0; i < e; ++i)
3412 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003413 return false;
3414 return true;
3415}
3416
David Greenec38a03e2011-02-03 15:50:00 +00003417/// isVEXTRACTF128Index - Return true if the specified
3418/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3419/// suitable for input to VEXTRACTF128.
3420bool X86::isVEXTRACTF128Index(SDNode *N) {
3421 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3422 return false;
3423
3424 // The index should be aligned on a 128-bit boundary.
3425 uint64_t Index =
3426 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3427
3428 unsigned VL = N->getValueType(0).getVectorNumElements();
3429 unsigned VBits = N->getValueType(0).getSizeInBits();
3430 unsigned ElSize = VBits / VL;
3431 bool Result = (Index * ElSize) % 128 == 0;
3432
3433 return Result;
3434}
3435
David Greeneccacdc12011-02-04 16:08:29 +00003436/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3437/// operand specifies a subvector insert that is suitable for input to
3438/// VINSERTF128.
3439bool X86::isVINSERTF128Index(SDNode *N) {
3440 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3441 return false;
3442
3443 // The index should be aligned on a 128-bit boundary.
3444 uint64_t Index =
3445 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3446
3447 unsigned VL = N->getValueType(0).getVectorNumElements();
3448 unsigned VBits = N->getValueType(0).getSizeInBits();
3449 unsigned ElSize = VBits / VL;
3450 bool Result = (Index * ElSize) % 128 == 0;
3451
3452 return Result;
3453}
3454
Evan Cheng63d33002006-03-22 08:01:21 +00003455/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003456/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003457unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3459 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3460
Evan Chengb9df0ca2006-03-22 02:53:00 +00003461 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3462 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 for (int i = 0; i < NumOperands; ++i) {
3464 int Val = SVOp->getMaskElt(NumOperands-i-1);
3465 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003466 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003467 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003468 if (i != NumOperands - 1)
3469 Mask <<= Shift;
3470 }
Evan Cheng63d33002006-03-22 08:01:21 +00003471 return Mask;
3472}
3473
Evan Cheng506d3df2006-03-29 23:07:14 +00003474/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003475/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003476unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003478 unsigned Mask = 0;
3479 // 8 nodes, but we only care about the last 4.
3480 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 int Val = SVOp->getMaskElt(i);
3482 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003483 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003484 if (i != 4)
3485 Mask <<= 2;
3486 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003487 return Mask;
3488}
3489
3490/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003491/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003492unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003494 unsigned Mask = 0;
3495 // 8 nodes, but we only care about the first 4.
3496 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 int Val = SVOp->getMaskElt(i);
3498 if (Val >= 0)
3499 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003500 if (i != 0)
3501 Mask <<= 2;
3502 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003503 return Mask;
3504}
3505
Nate Begemana09008b2009-10-19 02:17:23 +00003506/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3507/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3508unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3509 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3510 EVT VVT = N->getValueType(0);
3511 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3512 int Val = 0;
3513
3514 unsigned i, e;
3515 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3516 Val = SVOp->getMaskElt(i);
3517 if (Val >= 0)
3518 break;
3519 }
3520 return (Val - i) * EltSize;
3521}
3522
David Greenec38a03e2011-02-03 15:50:00 +00003523/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3524/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3525/// instructions.
3526unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3527 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3528 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3529
3530 uint64_t Index =
3531 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3532
3533 EVT VecVT = N->getOperand(0).getValueType();
3534 EVT ElVT = VecVT.getVectorElementType();
3535
3536 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3537
3538 return Index / NumElemsPerChunk;
3539}
3540
David Greeneccacdc12011-02-04 16:08:29 +00003541/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3542/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3543/// instructions.
3544unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3545 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3546 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3547
3548 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003549 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003550
3551 EVT VecVT = N->getValueType(0);
3552 EVT ElVT = VecVT.getVectorElementType();
3553
3554 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3555
3556 return Index / NumElemsPerChunk;
3557}
3558
Evan Cheng37b73872009-07-30 08:33:02 +00003559/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3560/// constant +0.0.
3561bool X86::isZeroNode(SDValue Elt) {
3562 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003563 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003564 (isa<ConstantFPSDNode>(Elt) &&
3565 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3566}
3567
Nate Begeman9008ca62009-04-27 18:41:29 +00003568/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3569/// their permute mask.
3570static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3571 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003572 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003573 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003574 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003575
Nate Begeman5a5ca152009-04-29 05:20:52 +00003576 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003577 int idx = SVOp->getMaskElt(i);
3578 if (idx < 0)
3579 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003580 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003582 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003584 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3586 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003587}
3588
Evan Cheng779ccea2007-12-07 21:30:01 +00003589/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3590/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003591static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003592 unsigned NumElems = VT.getVectorNumElements();
3593 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 int idx = Mask[i];
3595 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003596 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003597 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003598 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003599 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003601 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003602}
3603
Evan Cheng533a0aa2006-04-19 20:35:22 +00003604/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3605/// match movhlps. The lower half elements should come from upper half of
3606/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003607/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003608static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3609 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003610 return false;
3611 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003613 return false;
3614 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003616 return false;
3617 return true;
3618}
3619
Evan Cheng5ced1d82006-04-06 23:23:56 +00003620/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003621/// is promoted to a vector. It also returns the LoadSDNode by reference if
3622/// required.
3623static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003624 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3625 return false;
3626 N = N->getOperand(0).getNode();
3627 if (!ISD::isNON_EXTLoad(N))
3628 return false;
3629 if (LD)
3630 *LD = cast<LoadSDNode>(N);
3631 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003632}
3633
Evan Cheng533a0aa2006-04-19 20:35:22 +00003634/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3635/// match movlp{s|d}. The lower half elements should come from lower half of
3636/// V1 (and in order), and the upper half elements should come from the upper
3637/// half of V2 (and in order). And since V1 will become the source of the
3638/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003639static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3640 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003641 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003642 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003643 // Is V2 is a vector load, don't do this transformation. We will try to use
3644 // load folding shufps op.
3645 if (ISD::isNON_EXTLoad(V2))
3646 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003647
Nate Begeman5a5ca152009-04-29 05:20:52 +00003648 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003649
Evan Cheng533a0aa2006-04-19 20:35:22 +00003650 if (NumElems != 2 && NumElems != 4)
3651 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003652 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003653 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003654 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003655 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003657 return false;
3658 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003659}
3660
Evan Cheng39623da2006-04-20 08:58:49 +00003661/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3662/// all the same.
3663static bool isSplatVector(SDNode *N) {
3664 if (N->getOpcode() != ISD::BUILD_VECTOR)
3665 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003666
Dan Gohman475871a2008-07-27 21:46:04 +00003667 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003668 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3669 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003670 return false;
3671 return true;
3672}
3673
Evan Cheng213d2cf2007-05-17 18:45:50 +00003674/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003675/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003676/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003677static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003678 SDValue V1 = N->getOperand(0);
3679 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003680 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3681 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003683 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003685 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3686 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003687 if (Opc != ISD::BUILD_VECTOR ||
3688 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 return false;
3690 } else if (Idx >= 0) {
3691 unsigned Opc = V1.getOpcode();
3692 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3693 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003694 if (Opc != ISD::BUILD_VECTOR ||
3695 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003696 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003697 }
3698 }
3699 return true;
3700}
3701
3702/// getZeroVector - Returns a vector of specified type with all zero elements.
3703///
Owen Andersone50ed302009-08-10 22:56:29 +00003704static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003705 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003706 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003707
Dale Johannesen0488fb62010-09-30 23:57:10 +00003708 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003709 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003710 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003711 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003712 if (HasSSE2) { // SSE2
3713 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3714 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3715 } else { // SSE1
3716 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3717 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3718 }
3719 } else if (VT.getSizeInBits() == 256) { // AVX
3720 // 256-bit logic and arithmetic instructions in AVX are
3721 // all floating-point, no support for integer ops. Default
3722 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003724 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3725 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003726 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003727 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003728}
3729
Chris Lattner8a594482007-11-25 00:24:49 +00003730/// getOnesVector - Returns a vector of specified type with all bits set.
3731///
Owen Andersone50ed302009-08-10 22:56:29 +00003732static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003733 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003734
Chris Lattner8a594482007-11-25 00:24:49 +00003735 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3736 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003738 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003739 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003740 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003741}
3742
3743
Evan Cheng39623da2006-04-20 08:58:49 +00003744/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3745/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003746static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003747 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003748 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003749
Evan Cheng39623da2006-04-20 08:58:49 +00003750 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003751 SmallVector<int, 8> MaskVec;
3752 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003753
Nate Begeman5a5ca152009-04-29 05:20:52 +00003754 for (unsigned i = 0; i != NumElems; ++i) {
3755 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003756 MaskVec[i] = NumElems;
3757 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003758 }
Evan Cheng39623da2006-04-20 08:58:49 +00003759 }
Evan Cheng39623da2006-04-20 08:58:49 +00003760 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003761 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3762 SVOp->getOperand(1), &MaskVec[0]);
3763 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003764}
3765
Evan Cheng017dcc62006-04-21 01:05:10 +00003766/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3767/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003768static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003769 SDValue V2) {
3770 unsigned NumElems = VT.getVectorNumElements();
3771 SmallVector<int, 8> Mask;
3772 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003773 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003774 Mask.push_back(i);
3775 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003776}
3777
Nate Begeman9008ca62009-04-27 18:41:29 +00003778/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003779static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 SDValue V2) {
3781 unsigned NumElems = VT.getVectorNumElements();
3782 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003783 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003784 Mask.push_back(i);
3785 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003786 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003787 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003788}
3789
Nate Begeman9008ca62009-04-27 18:41:29 +00003790/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003791static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 SDValue V2) {
3793 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003794 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003796 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003797 Mask.push_back(i + Half);
3798 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003799 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003801}
3802
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003803/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3804static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003806 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003807 DebugLoc dl = SV->getDebugLoc();
3808 SDValue V1 = SV->getOperand(0);
3809 int NumElems = VT.getVectorNumElements();
3810 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003811
Nate Begeman9008ca62009-04-27 18:41:29 +00003812 // unpack elements to the correct location
3813 while (NumElems > 4) {
3814 if (EltNo < NumElems/2) {
3815 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3816 } else {
3817 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3818 EltNo -= NumElems/2;
3819 }
3820 NumElems >>= 1;
3821 }
Eric Christopherfd179292009-08-27 18:07:15 +00003822
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 // Perform the splat.
3824 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003825 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003827 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003828}
3829
Evan Chengba05f722006-04-21 23:03:30 +00003830/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003831/// vector of zero or undef vector. This produces a shuffle where the low
3832/// element of V2 is swizzled into the zero/undef vector, landing at element
3833/// 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 +00003834static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003835 bool isZero, bool HasSSE2,
3836 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003837 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003838 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003839 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3840 unsigned NumElems = VT.getVectorNumElements();
3841 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003842 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 // If this is the insertion idx, put the low elt of V2 here.
3844 MaskVec.push_back(i == Idx ? NumElems : i);
3845 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003846}
3847
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003848/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3849/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003850SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3851 unsigned Depth) {
3852 if (Depth == 6)
3853 return SDValue(); // Limit search depth.
3854
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003855 SDValue V = SDValue(N, 0);
3856 EVT VT = V.getValueType();
3857 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003858
3859 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3860 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3861 Index = SV->getMaskElt(Index);
3862
3863 if (Index < 0)
3864 return DAG.getUNDEF(VT.getVectorElementType());
3865
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003866 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003867 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003868 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003869 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003870
3871 // Recurse into target specific vector shuffles to find scalars.
3872 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003873 int NumElems = VT.getVectorNumElements();
3874 SmallVector<unsigned, 16> ShuffleMask;
3875 SDValue ImmN;
3876
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003877 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003878 case X86ISD::SHUFPS:
3879 case X86ISD::SHUFPD:
3880 ImmN = N->getOperand(N->getNumOperands()-1);
3881 DecodeSHUFPSMask(NumElems,
3882 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3883 ShuffleMask);
3884 break;
3885 case X86ISD::PUNPCKHBW:
3886 case X86ISD::PUNPCKHWD:
3887 case X86ISD::PUNPCKHDQ:
3888 case X86ISD::PUNPCKHQDQ:
3889 DecodePUNPCKHMask(NumElems, ShuffleMask);
3890 break;
3891 case X86ISD::UNPCKHPS:
3892 case X86ISD::UNPCKHPD:
3893 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3894 break;
3895 case X86ISD::PUNPCKLBW:
3896 case X86ISD::PUNPCKLWD:
3897 case X86ISD::PUNPCKLDQ:
3898 case X86ISD::PUNPCKLQDQ:
3899 DecodePUNPCKLMask(NumElems, ShuffleMask);
3900 break;
3901 case X86ISD::UNPCKLPS:
3902 case X86ISD::UNPCKLPD:
3903 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3904 break;
3905 case X86ISD::MOVHLPS:
3906 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3907 break;
3908 case X86ISD::MOVLHPS:
3909 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3910 break;
3911 case X86ISD::PSHUFD:
3912 ImmN = N->getOperand(N->getNumOperands()-1);
3913 DecodePSHUFMask(NumElems,
3914 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3915 ShuffleMask);
3916 break;
3917 case X86ISD::PSHUFHW:
3918 ImmN = N->getOperand(N->getNumOperands()-1);
3919 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3920 ShuffleMask);
3921 break;
3922 case X86ISD::PSHUFLW:
3923 ImmN = N->getOperand(N->getNumOperands()-1);
3924 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3925 ShuffleMask);
3926 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003927 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003928 case X86ISD::MOVSD: {
3929 // The index 0 always comes from the first element of the second source,
3930 // this is why MOVSS and MOVSD are used in the first place. The other
3931 // elements come from the other positions of the first source vector.
3932 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003933 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3934 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003935 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003936 default:
3937 assert("not implemented for target shuffle node");
3938 return SDValue();
3939 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003940
3941 Index = ShuffleMask[Index];
3942 if (Index < 0)
3943 return DAG.getUNDEF(VT.getVectorElementType());
3944
3945 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3946 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3947 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003948 }
3949
3950 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003951 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003952 V = V.getOperand(0);
3953 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003954 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003955
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003956 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003957 return SDValue();
3958 }
3959
3960 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3961 return (Index == 0) ? V.getOperand(0)
3962 : DAG.getUNDEF(VT.getVectorElementType());
3963
3964 if (V.getOpcode() == ISD::BUILD_VECTOR)
3965 return V.getOperand(Index);
3966
3967 return SDValue();
3968}
3969
3970/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3971/// shuffle operation which come from a consecutively from a zero. The
3972/// search can start in two diferent directions, from left or right.
3973static
3974unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3975 bool ZerosFromLeft, SelectionDAG &DAG) {
3976 int i = 0;
3977
3978 while (i < NumElems) {
3979 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003980 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003981 if (!(Elt.getNode() &&
3982 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3983 break;
3984 ++i;
3985 }
3986
3987 return i;
3988}
3989
3990/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3991/// MaskE correspond consecutively to elements from one of the vector operands,
3992/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3993static
3994bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3995 int OpIdx, int NumElems, unsigned &OpNum) {
3996 bool SeenV1 = false;
3997 bool SeenV2 = false;
3998
3999 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4000 int Idx = SVOp->getMaskElt(i);
4001 // Ignore undef indicies
4002 if (Idx < 0)
4003 continue;
4004
4005 if (Idx < NumElems)
4006 SeenV1 = true;
4007 else
4008 SeenV2 = true;
4009
4010 // Only accept consecutive elements from the same vector
4011 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4012 return false;
4013 }
4014
4015 OpNum = SeenV1 ? 0 : 1;
4016 return true;
4017}
4018
4019/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4020/// logical left shift of a vector.
4021static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4022 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4023 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4024 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4025 false /* check zeros from right */, DAG);
4026 unsigned OpSrc;
4027
4028 if (!NumZeros)
4029 return false;
4030
4031 // Considering the elements in the mask that are not consecutive zeros,
4032 // check if they consecutively come from only one of the source vectors.
4033 //
4034 // V1 = {X, A, B, C} 0
4035 // \ \ \ /
4036 // vector_shuffle V1, V2 <1, 2, 3, X>
4037 //
4038 if (!isShuffleMaskConsecutive(SVOp,
4039 0, // Mask Start Index
4040 NumElems-NumZeros-1, // Mask End Index
4041 NumZeros, // Where to start looking in the src vector
4042 NumElems, // Number of elements in vector
4043 OpSrc)) // Which source operand ?
4044 return false;
4045
4046 isLeft = false;
4047 ShAmt = NumZeros;
4048 ShVal = SVOp->getOperand(OpSrc);
4049 return true;
4050}
4051
4052/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4053/// logical left shift of a vector.
4054static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4055 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4056 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4057 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4058 true /* check zeros from left */, DAG);
4059 unsigned OpSrc;
4060
4061 if (!NumZeros)
4062 return false;
4063
4064 // Considering the elements in the mask that are not consecutive zeros,
4065 // check if they consecutively come from only one of the source vectors.
4066 //
4067 // 0 { A, B, X, X } = V2
4068 // / \ / /
4069 // vector_shuffle V1, V2 <X, X, 4, 5>
4070 //
4071 if (!isShuffleMaskConsecutive(SVOp,
4072 NumZeros, // Mask Start Index
4073 NumElems-1, // Mask End Index
4074 0, // Where to start looking in the src vector
4075 NumElems, // Number of elements in vector
4076 OpSrc)) // Which source operand ?
4077 return false;
4078
4079 isLeft = true;
4080 ShAmt = NumZeros;
4081 ShVal = SVOp->getOperand(OpSrc);
4082 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004083}
4084
4085/// isVectorShift - Returns true if the shuffle can be implemented as a
4086/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004087static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004088 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004089 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4090 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4091 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004092
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004093 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004094}
4095
Evan Chengc78d3b42006-04-24 18:01:45 +00004096/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4097///
Dan Gohman475871a2008-07-27 21:46:04 +00004098static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004099 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004100 SelectionDAG &DAG,
4101 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004102 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004103 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004104
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004105 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004106 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004107 bool First = true;
4108 for (unsigned i = 0; i < 16; ++i) {
4109 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4110 if (ThisIsNonZero && First) {
4111 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004112 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004113 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004114 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004115 First = false;
4116 }
4117
4118 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004119 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004120 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4121 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004122 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004124 }
4125 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004126 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4127 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4128 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004129 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004131 } else
4132 ThisElt = LastElt;
4133
Gabor Greifba36cb52008-08-28 21:40:38 +00004134 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004136 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004137 }
4138 }
4139
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004140 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004141}
4142
Bill Wendlinga348c562007-03-22 18:42:45 +00004143/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004144///
Dan Gohman475871a2008-07-27 21:46:04 +00004145static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004146 unsigned NumNonZero, unsigned NumZero,
4147 SelectionDAG &DAG,
4148 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004149 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004150 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004151
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004152 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004153 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004154 bool First = true;
4155 for (unsigned i = 0; i < 8; ++i) {
4156 bool isNonZero = (NonZeros & (1 << i)) != 0;
4157 if (isNonZero) {
4158 if (First) {
4159 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004161 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004163 First = false;
4164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004165 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004166 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004167 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004168 }
4169 }
4170
4171 return V;
4172}
4173
Evan Chengf26ffe92008-05-29 08:22:04 +00004174/// getVShift - Return a vector logical shift node.
4175///
Owen Andersone50ed302009-08-10 22:56:29 +00004176static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004177 unsigned NumBits, SelectionDAG &DAG,
4178 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004179 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004180 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004181 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4182 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004183 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00004184 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00004185}
4186
Dan Gohman475871a2008-07-27 21:46:04 +00004187SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004188X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004189 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004190
Evan Chengc3630942009-12-09 21:00:30 +00004191 // Check if the scalar load can be widened into a vector load. And if
4192 // the address is "base + cst" see if the cst can be "absorbed" into
4193 // the shuffle mask.
4194 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4195 SDValue Ptr = LD->getBasePtr();
4196 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4197 return SDValue();
4198 EVT PVT = LD->getValueType(0);
4199 if (PVT != MVT::i32 && PVT != MVT::f32)
4200 return SDValue();
4201
4202 int FI = -1;
4203 int64_t Offset = 0;
4204 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4205 FI = FINode->getIndex();
4206 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004207 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004208 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4209 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4210 Offset = Ptr.getConstantOperandVal(1);
4211 Ptr = Ptr.getOperand(0);
4212 } else {
4213 return SDValue();
4214 }
4215
4216 SDValue Chain = LD->getChain();
4217 // Make sure the stack object alignment is at least 16.
4218 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4219 if (DAG.InferPtrAlignment(Ptr) < 16) {
4220 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004221 // Can't change the alignment. FIXME: It's possible to compute
4222 // the exact stack offset and reference FI + adjust offset instead.
4223 // If someone *really* cares about this. That's the way to implement it.
4224 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004225 } else {
4226 MFI->setObjectAlignment(FI, 16);
4227 }
4228 }
4229
4230 // (Offset % 16) must be multiple of 4. Then address is then
4231 // Ptr + (Offset & ~15).
4232 if (Offset < 0)
4233 return SDValue();
4234 if ((Offset % 16) & 3)
4235 return SDValue();
4236 int64_t StartOffset = Offset & ~15;
4237 if (StartOffset)
4238 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4239 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4240
4241 int EltNo = (Offset - StartOffset) >> 2;
4242 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4243 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004244 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4245 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004246 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004247 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004248 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4249 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004250 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004251 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004252 }
4253
4254 return SDValue();
4255}
4256
Michael J. Spencerec38de22010-10-10 22:04:20 +00004257/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4258/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004259/// load which has the same value as a build_vector whose operands are 'elts'.
4260///
4261/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004262///
Nate Begeman1449f292010-03-24 22:19:06 +00004263/// FIXME: we'd also like to handle the case where the last elements are zero
4264/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4265/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004266static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004267 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004268 EVT EltVT = VT.getVectorElementType();
4269 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004270
Nate Begemanfdea31a2010-03-24 20:49:50 +00004271 LoadSDNode *LDBase = NULL;
4272 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004273
Nate Begeman1449f292010-03-24 22:19:06 +00004274 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004275 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004276 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004277 for (unsigned i = 0; i < NumElems; ++i) {
4278 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004279
Nate Begemanfdea31a2010-03-24 20:49:50 +00004280 if (!Elt.getNode() ||
4281 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4282 return SDValue();
4283 if (!LDBase) {
4284 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4285 return SDValue();
4286 LDBase = cast<LoadSDNode>(Elt.getNode());
4287 LastLoadedElt = i;
4288 continue;
4289 }
4290 if (Elt.getOpcode() == ISD::UNDEF)
4291 continue;
4292
4293 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4294 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4295 return SDValue();
4296 LastLoadedElt = i;
4297 }
Nate Begeman1449f292010-03-24 22:19:06 +00004298
4299 // If we have found an entire vector of loads and undefs, then return a large
4300 // load of the entire vector width starting at the base pointer. If we found
4301 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004302 if (LastLoadedElt == NumElems - 1) {
4303 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004304 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004305 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004306 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004307 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004308 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004309 LDBase->isVolatile(), LDBase->isNonTemporal(),
4310 LDBase->getAlignment());
4311 } else if (NumElems == 4 && LastLoadedElt == 1) {
4312 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4313 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004314 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4315 Ops, 2, MVT::i32,
4316 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004317 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004318 }
4319 return SDValue();
4320}
4321
Evan Chengc3630942009-12-09 21:00:30 +00004322SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004323X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004324 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004325
David Greenef125a292011-02-08 19:04:41 +00004326 EVT VT = Op.getValueType();
4327 EVT ExtVT = VT.getVectorElementType();
4328
4329 unsigned NumElems = Op.getNumOperands();
4330
4331 // For AVX-length vectors, build the individual 128-bit pieces and
4332 // use shuffles to put them in place.
4333 if (VT.getSizeInBits() > 256 &&
4334 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004335 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4336 SmallVector<SDValue, 8> V;
4337 V.resize(NumElems);
4338 for (unsigned i = 0; i < NumElems; ++i) {
4339 V[i] = Op.getOperand(i);
4340 }
4341
4342 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4343
4344 // Build the lower subvector.
4345 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4346 // Build the upper subvector.
4347 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4348 NumElems/2);
4349
4350 return ConcatVectors(Lower, Upper, DAG);
4351 }
4352
Chris Lattner6e80e442010-08-28 17:15:43 +00004353 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4354 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004355 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4356 // is present, so AllOnes is ignored.
4357 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4358 (Op.getValueType().getSizeInBits() != 256 &&
4359 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004360 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004361 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4362 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004363 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004364 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004365
Gabor Greifba36cb52008-08-28 21:40:38 +00004366 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004367 return getOnesVector(Op.getValueType(), DAG, dl);
4368 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004369 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370
Owen Andersone50ed302009-08-10 22:56:29 +00004371 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004372
Evan Cheng0db9fe62006-04-25 20:13:52 +00004373 unsigned NumZero = 0;
4374 unsigned NumNonZero = 0;
4375 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004376 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004377 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004378 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004379 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004380 if (Elt.getOpcode() == ISD::UNDEF)
4381 continue;
4382 Values.insert(Elt);
4383 if (Elt.getOpcode() != ISD::Constant &&
4384 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004385 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004386 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004387 NumZero++;
4388 else {
4389 NonZeros |= (1 << i);
4390 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004391 }
4392 }
4393
Chris Lattner97a2a562010-08-26 05:24:29 +00004394 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4395 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004396 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004397
Chris Lattner67f453a2008-03-09 05:42:06 +00004398 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004399 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004401 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004402
Chris Lattner62098042008-03-09 01:05:04 +00004403 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4404 // the value are obviously zero, truncate the value to i32 and do the
4405 // insertion that way. Only do this if the value is non-constant or if the
4406 // value is a constant being inserted into element 0. It is cheaper to do
4407 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004408 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004409 (!IsAllConstants || Idx == 0)) {
4410 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004411 // Handle SSE only.
4412 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4413 EVT VecVT = MVT::v4i32;
4414 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004415
Chris Lattner62098042008-03-09 01:05:04 +00004416 // Truncate the value (which may itself be a constant) to i32, and
4417 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004418 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004419 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004420 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4421 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004422
Chris Lattner62098042008-03-09 01:05:04 +00004423 // Now we have our 32-bit value zero extended in the low element of
4424 // a vector. If Idx != 0, swizzle it into place.
4425 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 SmallVector<int, 4> Mask;
4427 Mask.push_back(Idx);
4428 for (unsigned i = 1; i != VecElts; ++i)
4429 Mask.push_back(i);
4430 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004431 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004433 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004434 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004435 }
4436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Chris Lattner19f79692008-03-08 22:59:52 +00004438 // If we have a constant or non-constant insertion into the low element of
4439 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4440 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004441 // depending on what the source datatype is.
4442 if (Idx == 0) {
4443 if (NumZero == 0) {
4444 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004445 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4446 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004447 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4448 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4449 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4450 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004451 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4452 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004453 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4454 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004455 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4456 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4457 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004458 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004459 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004460 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004461
4462 // Is it a vector logical left shift?
4463 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004464 X86::isZeroNode(Op.getOperand(0)) &&
4465 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004466 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004467 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004468 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004469 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004470 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004471 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004472
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004473 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004474 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004475
Chris Lattner19f79692008-03-08 22:59:52 +00004476 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4477 // is a non-constant being inserted into an element other than the low one,
4478 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4479 // movd/movss) to move this into the low element, then shuffle it into
4480 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004482 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004483
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004485 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4486 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004488 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004489 MaskVec.push_back(i == Idx ? 0 : 1);
4490 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004491 }
4492 }
4493
Chris Lattner67f453a2008-03-09 05:42:06 +00004494 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004495 if (Values.size() == 1) {
4496 if (EVTBits == 32) {
4497 // Instead of a shuffle like this:
4498 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4499 // Check if it's possible to issue this instead.
4500 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4501 unsigned Idx = CountTrailingZeros_32(NonZeros);
4502 SDValue Item = Op.getOperand(Idx);
4503 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4504 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4505 }
Dan Gohman475871a2008-07-27 21:46:04 +00004506 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004508
Dan Gohmana3941172007-07-24 22:55:08 +00004509 // A vector full of immediates; various special cases are already
4510 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004511 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004512 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004513
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004514 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004515 if (EVTBits == 64) {
4516 if (NumNonZero == 1) {
4517 // One half is zero or undef.
4518 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004519 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004520 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004521 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4522 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004523 }
Dan Gohman475871a2008-07-27 21:46:04 +00004524 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004525 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004526
4527 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004528 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004529 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004530 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004531 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004532 }
4533
Bill Wendling826f36f2007-03-28 00:57:11 +00004534 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004535 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004536 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004537 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004538 }
4539
4540 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004541 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004542 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004543 if (NumElems == 4 && NumZero > 0) {
4544 for (unsigned i = 0; i < 4; ++i) {
4545 bool isZero = !(NonZeros & (1 << i));
4546 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004547 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004548 else
Dale Johannesenace16102009-02-03 19:33:06 +00004549 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004550 }
4551
4552 for (unsigned i = 0; i < 2; ++i) {
4553 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4554 default: break;
4555 case 0:
4556 V[i] = V[i*2]; // Must be a zero vector.
4557 break;
4558 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004559 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004560 break;
4561 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004562 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 break;
4564 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004565 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 break;
4567 }
4568 }
4569
Nate Begeman9008ca62009-04-27 18:41:29 +00004570 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 bool Reverse = (NonZeros & 0x3) == 2;
4572 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004573 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004574 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4575 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004576 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4577 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004578 }
4579
Nate Begemanfdea31a2010-03-24 20:49:50 +00004580 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4581 // Check for a build vector of consecutive loads.
4582 for (unsigned i = 0; i < NumElems; ++i)
4583 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004584
Nate Begemanfdea31a2010-03-24 20:49:50 +00004585 // Check for elements which are consecutive loads.
4586 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4587 if (LD.getNode())
4588 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004589
4590 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004591 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004592 SDValue Result;
4593 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4594 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4595 else
4596 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004597
Chris Lattner24faf612010-08-28 17:59:08 +00004598 for (unsigned i = 1; i < NumElems; ++i) {
4599 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4600 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004602 }
4603 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004605
Chris Lattner6e80e442010-08-28 17:15:43 +00004606 // Otherwise, expand into a number of unpckl*, start by extending each of
4607 // our (non-undef) elements to the full vector width with the element in the
4608 // bottom slot of the vector (which generates no code for SSE).
4609 for (unsigned i = 0; i < NumElems; ++i) {
4610 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4611 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4612 else
4613 V[i] = DAG.getUNDEF(VT);
4614 }
4615
4616 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4618 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4619 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004620 unsigned EltStride = NumElems >> 1;
4621 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004622 for (unsigned i = 0; i < EltStride; ++i) {
4623 // If V[i+EltStride] is undef and this is the first round of mixing,
4624 // then it is safe to just drop this shuffle: V[i] is already in the
4625 // right place, the one element (since it's the first round) being
4626 // inserted as undef can be dropped. This isn't safe for successive
4627 // rounds because they will permute elements within both vectors.
4628 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4629 EltStride == NumElems/2)
4630 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004631
Chris Lattner6e80e442010-08-28 17:15:43 +00004632 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004633 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004634 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004635 }
4636 return V[0];
4637 }
Dan Gohman475871a2008-07-27 21:46:04 +00004638 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639}
4640
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004641SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004642X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004643 // We support concatenate two MMX registers and place them in a MMX
4644 // register. This is better than doing a stack convert.
4645 DebugLoc dl = Op.getDebugLoc();
4646 EVT ResVT = Op.getValueType();
4647 assert(Op.getNumOperands() == 2);
4648 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4649 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4650 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004651 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004652 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4653 InVec = Op.getOperand(1);
4654 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4655 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004656 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004657 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4658 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4659 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004660 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004661 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4662 Mask[0] = 0; Mask[1] = 2;
4663 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4664 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004665 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004666}
4667
Nate Begemanb9a47b82009-02-23 08:49:38 +00004668// v8i16 shuffles - Prefer shuffles in the following order:
4669// 1. [all] pshuflw, pshufhw, optional move
4670// 2. [ssse3] 1 x pshufb
4671// 3. [ssse3] 2 x pshufb + 1 x por
4672// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004673SDValue
4674X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4675 SelectionDAG &DAG) const {
4676 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 SDValue V1 = SVOp->getOperand(0);
4678 SDValue V2 = SVOp->getOperand(1);
4679 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004680 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004681
Nate Begemanb9a47b82009-02-23 08:49:38 +00004682 // Determine if more than 1 of the words in each of the low and high quadwords
4683 // of the result come from the same quadword of one of the two inputs. Undef
4684 // mask values count as coming from any quadword, for better codegen.
4685 SmallVector<unsigned, 4> LoQuad(4);
4686 SmallVector<unsigned, 4> HiQuad(4);
4687 BitVector InputQuads(4);
4688 for (unsigned i = 0; i < 8; ++i) {
4689 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004691 MaskVals.push_back(EltIdx);
4692 if (EltIdx < 0) {
4693 ++Quad[0];
4694 ++Quad[1];
4695 ++Quad[2];
4696 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004697 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004698 }
4699 ++Quad[EltIdx / 4];
4700 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004701 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004702
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004704 unsigned MaxQuad = 1;
4705 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004706 if (LoQuad[i] > MaxQuad) {
4707 BestLoQuad = i;
4708 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004709 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004710 }
4711
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004713 MaxQuad = 1;
4714 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 if (HiQuad[i] > MaxQuad) {
4716 BestHiQuad = i;
4717 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004718 }
4719 }
4720
Nate Begemanb9a47b82009-02-23 08:49:38 +00004721 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004722 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004723 // single pshufb instruction is necessary. If There are more than 2 input
4724 // quads, disable the next transformation since it does not help SSSE3.
4725 bool V1Used = InputQuads[0] || InputQuads[1];
4726 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004727 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004728 if (InputQuads.count() == 2 && V1Used && V2Used) {
4729 BestLoQuad = InputQuads.find_first();
4730 BestHiQuad = InputQuads.find_next(BestLoQuad);
4731 }
4732 if (InputQuads.count() > 2) {
4733 BestLoQuad = -1;
4734 BestHiQuad = -1;
4735 }
4736 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004737
Nate Begemanb9a47b82009-02-23 08:49:38 +00004738 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4739 // the shuffle mask. If a quad is scored as -1, that means that it contains
4740 // words from all 4 input quadwords.
4741 SDValue NewV;
4742 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004743 SmallVector<int, 8> MaskV;
4744 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4745 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004746 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004747 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4748 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4749 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004750
Nate Begemanb9a47b82009-02-23 08:49:38 +00004751 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4752 // source words for the shuffle, to aid later transformations.
4753 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004754 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004755 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004756 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004757 if (idx != (int)i)
4758 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004759 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004760 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004761 AllWordsInNewV = false;
4762 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004763 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004764
Nate Begemanb9a47b82009-02-23 08:49:38 +00004765 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4766 if (AllWordsInNewV) {
4767 for (int i = 0; i != 8; ++i) {
4768 int idx = MaskVals[i];
4769 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004770 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004771 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 if ((idx != i) && idx < 4)
4773 pshufhw = false;
4774 if ((idx != i) && idx > 3)
4775 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004776 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 V1 = NewV;
4778 V2Used = false;
4779 BestLoQuad = 0;
4780 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004781 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004782
Nate Begemanb9a47b82009-02-23 08:49:38 +00004783 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4784 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004785 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004786 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4787 unsigned TargetMask = 0;
4788 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004790 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4791 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4792 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004793 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004794 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004795 }
Eric Christopherfd179292009-08-27 18:07:15 +00004796
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797 // If we have SSSE3, and all words of the result are from 1 input vector,
4798 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4799 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004800 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004801 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004802
Nate Begemanb9a47b82009-02-23 08:49:38 +00004803 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004804 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 // mask, and elements that come from V1 in the V2 mask, so that the two
4806 // results can be OR'd together.
4807 bool TwoInputs = V1Used && V2Used;
4808 for (unsigned i = 0; i != 8; ++i) {
4809 int EltIdx = MaskVals[i] * 2;
4810 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004811 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4812 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 continue;
4814 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4816 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004818 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004819 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004820 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004822 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004823 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004824
Nate Begemanb9a47b82009-02-23 08:49:38 +00004825 // Calculate the shuffle mask for the second input, shuffle it, and
4826 // OR it with the first shuffled input.
4827 pshufbMask.clear();
4828 for (unsigned i = 0; i != 8; ++i) {
4829 int EltIdx = MaskVals[i] * 2;
4830 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4832 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004833 continue;
4834 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4836 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004838 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004839 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004840 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 MVT::v16i8, &pshufbMask[0], 16));
4842 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004843 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004844 }
4845
4846 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4847 // and update MaskVals with new element order.
4848 BitVector InOrder(8);
4849 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004850 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004851 for (int i = 0; i != 4; ++i) {
4852 int idx = MaskVals[i];
4853 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004854 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 InOrder.set(i);
4856 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004857 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 InOrder.set(i);
4859 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004860 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004861 }
4862 }
4863 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004865 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004866 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004867
4868 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4869 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4870 NewV.getOperand(0),
4871 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4872 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004873 }
Eric Christopherfd179292009-08-27 18:07:15 +00004874
Nate Begemanb9a47b82009-02-23 08:49:38 +00004875 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4876 // and update MaskVals with the new element order.
4877 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004880 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004881 for (unsigned i = 4; i != 8; ++i) {
4882 int idx = MaskVals[i];
4883 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 InOrder.set(i);
4886 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004887 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004888 InOrder.set(i);
4889 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004890 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 }
4892 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004893 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004894 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004895
4896 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4897 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4898 NewV.getOperand(0),
4899 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4900 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004901 }
Eric Christopherfd179292009-08-27 18:07:15 +00004902
Nate Begemanb9a47b82009-02-23 08:49:38 +00004903 // In case BestHi & BestLo were both -1, which means each quadword has a word
4904 // from each of the four input quadwords, calculate the InOrder bitvector now
4905 // before falling through to the insert/extract cleanup.
4906 if (BestLoQuad == -1 && BestHiQuad == -1) {
4907 NewV = V1;
4908 for (int i = 0; i != 8; ++i)
4909 if (MaskVals[i] < 0 || MaskVals[i] == i)
4910 InOrder.set(i);
4911 }
Eric Christopherfd179292009-08-27 18:07:15 +00004912
Nate Begemanb9a47b82009-02-23 08:49:38 +00004913 // The other elements are put in the right place using pextrw and pinsrw.
4914 for (unsigned i = 0; i != 8; ++i) {
4915 if (InOrder[i])
4916 continue;
4917 int EltIdx = MaskVals[i];
4918 if (EltIdx < 0)
4919 continue;
4920 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004921 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004922 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004923 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004924 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004925 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 DAG.getIntPtrConstant(i));
4927 }
4928 return NewV;
4929}
4930
4931// v16i8 shuffles - Prefer shuffles in the following order:
4932// 1. [ssse3] 1 x pshufb
4933// 2. [ssse3] 2 x pshufb + 1 x por
4934// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4935static
Nate Begeman9008ca62009-04-27 18:41:29 +00004936SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004937 SelectionDAG &DAG,
4938 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004939 SDValue V1 = SVOp->getOperand(0);
4940 SDValue V2 = SVOp->getOperand(1);
4941 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004944
Nate Begemanb9a47b82009-02-23 08:49:38 +00004945 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004946 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004947 // present, fall back to case 3.
4948 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4949 bool V1Only = true;
4950 bool V2Only = true;
4951 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004952 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004953 if (EltIdx < 0)
4954 continue;
4955 if (EltIdx < 16)
4956 V2Only = false;
4957 else
4958 V1Only = false;
4959 }
Eric Christopherfd179292009-08-27 18:07:15 +00004960
Nate Begemanb9a47b82009-02-23 08:49:38 +00004961 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4962 if (TLI.getSubtarget()->hasSSSE3()) {
4963 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004964
Nate Begemanb9a47b82009-02-23 08:49:38 +00004965 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004966 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004967 //
4968 // Otherwise, we have elements from both input vectors, and must zero out
4969 // elements that come from V2 in the first mask, and V1 in the second mask
4970 // so that we can OR them together.
4971 bool TwoInputs = !(V1Only || V2Only);
4972 for (unsigned i = 0; i != 16; ++i) {
4973 int EltIdx = MaskVals[i];
4974 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004975 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004976 continue;
4977 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004978 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004979 }
4980 // If all the elements are from V2, assign it to V1 and return after
4981 // building the first pshufb.
4982 if (V2Only)
4983 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004984 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004985 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004986 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004987 if (!TwoInputs)
4988 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004989
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 // Calculate the shuffle mask for the second input, shuffle it, and
4991 // OR it with the first shuffled input.
4992 pshufbMask.clear();
4993 for (unsigned i = 0; i != 16; ++i) {
4994 int EltIdx = MaskVals[i];
4995 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004996 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004997 continue;
4998 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004999 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005000 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005001 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005002 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005003 MVT::v16i8, &pshufbMask[0], 16));
5004 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005005 }
Eric Christopherfd179292009-08-27 18:07:15 +00005006
Nate Begemanb9a47b82009-02-23 08:49:38 +00005007 // No SSSE3 - Calculate in place words and then fix all out of place words
5008 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5009 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005010 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5011 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005012 SDValue NewV = V2Only ? V2 : V1;
5013 for (int i = 0; i != 8; ++i) {
5014 int Elt0 = MaskVals[i*2];
5015 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005016
Nate Begemanb9a47b82009-02-23 08:49:38 +00005017 // This word of the result is all undef, skip it.
5018 if (Elt0 < 0 && Elt1 < 0)
5019 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005020
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 // This word of the result is already in the correct place, skip it.
5022 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5023 continue;
5024 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5025 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005026
Nate Begemanb9a47b82009-02-23 08:49:38 +00005027 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5028 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5029 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005030
5031 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5032 // using a single extract together, load it and store it.
5033 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005034 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005035 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005036 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005037 DAG.getIntPtrConstant(i));
5038 continue;
5039 }
5040
Nate Begemanb9a47b82009-02-23 08:49:38 +00005041 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005042 // source byte is not also odd, shift the extracted word left 8 bits
5043 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005044 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005045 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005046 DAG.getIntPtrConstant(Elt1 / 2));
5047 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005049 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005050 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5052 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005053 }
5054 // If Elt0 is defined, extract it from the appropriate source. If the
5055 // source byte is not also even, shift the extracted word right 8 bits. If
5056 // Elt1 was also defined, OR the extracted values together before
5057 // inserting them in the result.
5058 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005059 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005060 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5061 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005062 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005063 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005064 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5066 DAG.getConstant(0x00FF, MVT::i16));
5067 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 : InsElt0;
5069 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005070 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005071 DAG.getIntPtrConstant(i));
5072 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005073 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005074}
5075
Evan Cheng7a831ce2007-12-15 03:00:47 +00005076/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005077/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005078/// done when every pair / quad of shuffle mask elements point to elements in
5079/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005080/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005081static
Nate Begeman9008ca62009-04-27 18:41:29 +00005082SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005083 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005084 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005085 SDValue V1 = SVOp->getOperand(0);
5086 SDValue V2 = SVOp->getOperand(1);
5087 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005088 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005089 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005090 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005091 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005092 case MVT::v4f32: NewVT = MVT::v2f64; break;
5093 case MVT::v4i32: NewVT = MVT::v2i64; break;
5094 case MVT::v8i16: NewVT = MVT::v4i32; break;
5095 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005096 }
5097
Nate Begeman9008ca62009-04-27 18:41:29 +00005098 int Scale = NumElems / NewWidth;
5099 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005100 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005101 int StartIdx = -1;
5102 for (int j = 0; j < Scale; ++j) {
5103 int EltIdx = SVOp->getMaskElt(i+j);
5104 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005105 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005106 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005107 StartIdx = EltIdx - (EltIdx % Scale);
5108 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005109 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005110 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005111 if (StartIdx == -1)
5112 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005113 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005115 }
5116
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005117 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5118 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005119 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005120}
5121
Evan Chengd880b972008-05-09 21:53:03 +00005122/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005123///
Owen Andersone50ed302009-08-10 22:56:29 +00005124static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 SDValue SrcOp, SelectionDAG &DAG,
5126 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005127 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005128 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005129 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005130 LD = dyn_cast<LoadSDNode>(SrcOp);
5131 if (!LD) {
5132 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5133 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005134 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005135 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005136 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005137 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005138 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005139 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005140 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005141 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005142 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5143 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5144 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005145 SrcOp.getOperand(0)
5146 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005147 }
5148 }
5149 }
5150
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005151 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005152 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005153 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005154 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005155}
5156
Evan Chengace3c172008-07-22 21:13:36 +00005157/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5158/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005159static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005160LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5161 SDValue V1 = SVOp->getOperand(0);
5162 SDValue V2 = SVOp->getOperand(1);
5163 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005164 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005165
Evan Chengace3c172008-07-22 21:13:36 +00005166 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005167 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005168 SmallVector<int, 8> Mask1(4U, -1);
5169 SmallVector<int, 8> PermMask;
5170 SVOp->getMask(PermMask);
5171
Evan Chengace3c172008-07-22 21:13:36 +00005172 unsigned NumHi = 0;
5173 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005174 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005175 int Idx = PermMask[i];
5176 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005177 Locs[i] = std::make_pair(-1, -1);
5178 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005179 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5180 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005181 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005182 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005183 NumLo++;
5184 } else {
5185 Locs[i] = std::make_pair(1, NumHi);
5186 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005187 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005188 NumHi++;
5189 }
5190 }
5191 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005192
Evan Chengace3c172008-07-22 21:13:36 +00005193 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005194 // If no more than two elements come from either vector. This can be
5195 // implemented with two shuffles. First shuffle gather the elements.
5196 // The second shuffle, which takes the first shuffle as both of its
5197 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005198 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005199
Nate Begeman9008ca62009-04-27 18:41:29 +00005200 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005201
Evan Chengace3c172008-07-22 21:13:36 +00005202 for (unsigned i = 0; i != 4; ++i) {
5203 if (Locs[i].first == -1)
5204 continue;
5205 else {
5206 unsigned Idx = (i < 2) ? 0 : 4;
5207 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005208 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005209 }
5210 }
5211
Nate Begeman9008ca62009-04-27 18:41:29 +00005212 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005213 } else if (NumLo == 3 || NumHi == 3) {
5214 // Otherwise, we must have three elements from one vector, call it X, and
5215 // one element from the other, call it Y. First, use a shufps to build an
5216 // intermediate vector with the one element from Y and the element from X
5217 // that will be in the same half in the final destination (the indexes don't
5218 // matter). Then, use a shufps to build the final vector, taking the half
5219 // containing the element from Y from the intermediate, and the other half
5220 // from X.
5221 if (NumHi == 3) {
5222 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005223 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005224 std::swap(V1, V2);
5225 }
5226
5227 // Find the element from V2.
5228 unsigned HiIndex;
5229 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005230 int Val = PermMask[HiIndex];
5231 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005232 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005233 if (Val >= 4)
5234 break;
5235 }
5236
Nate Begeman9008ca62009-04-27 18:41:29 +00005237 Mask1[0] = PermMask[HiIndex];
5238 Mask1[1] = -1;
5239 Mask1[2] = PermMask[HiIndex^1];
5240 Mask1[3] = -1;
5241 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005242
5243 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005244 Mask1[0] = PermMask[0];
5245 Mask1[1] = PermMask[1];
5246 Mask1[2] = HiIndex & 1 ? 6 : 4;
5247 Mask1[3] = HiIndex & 1 ? 4 : 6;
5248 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005249 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 Mask1[0] = HiIndex & 1 ? 2 : 0;
5251 Mask1[1] = HiIndex & 1 ? 0 : 2;
5252 Mask1[2] = PermMask[2];
5253 Mask1[3] = PermMask[3];
5254 if (Mask1[2] >= 0)
5255 Mask1[2] += 4;
5256 if (Mask1[3] >= 0)
5257 Mask1[3] += 4;
5258 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005259 }
Evan Chengace3c172008-07-22 21:13:36 +00005260 }
5261
5262 // Break it into (shuffle shuffle_hi, shuffle_lo).
5263 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005264 SmallVector<int,8> LoMask(4U, -1);
5265 SmallVector<int,8> HiMask(4U, -1);
5266
5267 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005268 unsigned MaskIdx = 0;
5269 unsigned LoIdx = 0;
5270 unsigned HiIdx = 2;
5271 for (unsigned i = 0; i != 4; ++i) {
5272 if (i == 2) {
5273 MaskPtr = &HiMask;
5274 MaskIdx = 1;
5275 LoIdx = 0;
5276 HiIdx = 2;
5277 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005278 int Idx = PermMask[i];
5279 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005280 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005282 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005284 LoIdx++;
5285 } else {
5286 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005288 HiIdx++;
5289 }
5290 }
5291
Nate Begeman9008ca62009-04-27 18:41:29 +00005292 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5293 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5294 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005295 for (unsigned i = 0; i != 4; ++i) {
5296 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005298 } else {
5299 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005301 }
5302 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005303 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005304}
5305
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005306static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005307 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005308 V = V.getOperand(0);
5309 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5310 V = V.getOperand(0);
5311 if (MayFoldLoad(V))
5312 return true;
5313 return false;
5314}
5315
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005316// FIXME: the version above should always be used. Since there's
5317// a bug where several vector shuffles can't be folded because the
5318// DAG is not updated during lowering and a node claims to have two
5319// uses while it only has one, use this version, and let isel match
5320// another instruction if the load really happens to have more than
5321// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005322// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005323static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005324 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005325 V = V.getOperand(0);
5326 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5327 V = V.getOperand(0);
5328 if (ISD::isNormalLoad(V.getNode()))
5329 return true;
5330 return false;
5331}
5332
5333/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5334/// a vector extract, and if both can be later optimized into a single load.
5335/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5336/// here because otherwise a target specific shuffle node is going to be
5337/// emitted for this shuffle, and the optimization not done.
5338/// FIXME: This is probably not the best approach, but fix the problem
5339/// until the right path is decided.
5340static
5341bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5342 const TargetLowering &TLI) {
5343 EVT VT = V.getValueType();
5344 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5345
5346 // Be sure that the vector shuffle is present in a pattern like this:
5347 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5348 if (!V.hasOneUse())
5349 return false;
5350
5351 SDNode *N = *V.getNode()->use_begin();
5352 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5353 return false;
5354
5355 SDValue EltNo = N->getOperand(1);
5356 if (!isa<ConstantSDNode>(EltNo))
5357 return false;
5358
5359 // If the bit convert changed the number of elements, it is unsafe
5360 // to examine the mask.
5361 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005362 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005363 EVT SrcVT = V.getOperand(0).getValueType();
5364 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5365 return false;
5366 V = V.getOperand(0);
5367 HasShuffleIntoBitcast = true;
5368 }
5369
5370 // Select the input vector, guarding against out of range extract vector.
5371 unsigned NumElems = VT.getVectorNumElements();
5372 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5373 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5374 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5375
5376 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005377 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005378 V = V.getOperand(0);
5379
5380 if (ISD::isNormalLoad(V.getNode())) {
5381 // Is the original load suitable?
5382 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5383
5384 // FIXME: avoid the multi-use bug that is preventing lots of
5385 // of foldings to be detected, this is still wrong of course, but
5386 // give the temporary desired behavior, and if it happens that
5387 // the load has real more uses, during isel it will not fold, and
5388 // will generate poor code.
5389 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5390 return false;
5391
5392 if (!HasShuffleIntoBitcast)
5393 return true;
5394
5395 // If there's a bitcast before the shuffle, check if the load type and
5396 // alignment is valid.
5397 unsigned Align = LN0->getAlignment();
5398 unsigned NewAlign =
5399 TLI.getTargetData()->getABITypeAlignment(
5400 VT.getTypeForEVT(*DAG.getContext()));
5401
5402 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5403 return false;
5404 }
5405
5406 return true;
5407}
5408
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005409static
Evan Cheng835580f2010-10-07 20:50:20 +00005410SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5411 EVT VT = Op.getValueType();
5412
5413 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005414 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5415 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005416 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5417 V1, DAG));
5418}
5419
5420static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005421SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5422 bool HasSSE2) {
5423 SDValue V1 = Op.getOperand(0);
5424 SDValue V2 = Op.getOperand(1);
5425 EVT VT = Op.getValueType();
5426
5427 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5428
5429 if (HasSSE2 && VT == MVT::v2f64)
5430 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5431
5432 // v4f32 or v4i32
5433 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5434}
5435
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005436static
5437SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5438 SDValue V1 = Op.getOperand(0);
5439 SDValue V2 = Op.getOperand(1);
5440 EVT VT = Op.getValueType();
5441
5442 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5443 "unsupported shuffle type");
5444
5445 if (V2.getOpcode() == ISD::UNDEF)
5446 V2 = V1;
5447
5448 // v4i32 or v4f32
5449 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5450}
5451
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005452static
5453SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5454 SDValue V1 = Op.getOperand(0);
5455 SDValue V2 = Op.getOperand(1);
5456 EVT VT = Op.getValueType();
5457 unsigned NumElems = VT.getVectorNumElements();
5458
5459 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5460 // operand of these instructions is only memory, so check if there's a
5461 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5462 // same masks.
5463 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005464
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005465 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005466 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005467 CanFoldLoad = true;
5468
5469 // When V1 is a load, it can be folded later into a store in isel, example:
5470 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5471 // turns into:
5472 // (MOVLPSmr addr:$src1, VR128:$src2)
5473 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005474 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005475 CanFoldLoad = true;
5476
Eric Christopher893a8822011-02-20 05:04:42 +00005477 // Both of them can't be memory operations though.
5478 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5479 CanFoldLoad = false;
5480
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005481 if (CanFoldLoad) {
5482 if (HasSSE2 && NumElems == 2)
5483 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5484
5485 if (NumElems == 4)
5486 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5487 }
5488
5489 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5490 // movl and movlp will both match v2i64, but v2i64 is never matched by
5491 // movl earlier because we make it strict to avoid messing with the movlp load
5492 // folding logic (see the code above getMOVLP call). Match it here then,
5493 // this is horrible, but will stay like this until we move all shuffle
5494 // matching to x86 specific nodes. Note that for the 1st condition all
5495 // types are matched with movsd.
5496 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5497 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5498 else if (HasSSE2)
5499 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5500
5501
5502 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5503
5504 // Invert the operand order and use SHUFPS to match it.
5505 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5506 X86::getShuffleSHUFImmediate(SVOp), DAG);
5507}
5508
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005509static inline unsigned getUNPCKLOpcode(EVT VT) {
5510 switch(VT.getSimpleVT().SimpleTy) {
5511 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5512 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5513 case MVT::v4f32: return X86ISD::UNPCKLPS;
5514 case MVT::v2f64: return X86ISD::UNPCKLPD;
5515 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5516 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5517 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005518 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005519 }
5520 return 0;
5521}
5522
5523static inline unsigned getUNPCKHOpcode(EVT VT) {
5524 switch(VT.getSimpleVT().SimpleTy) {
5525 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5526 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5527 case MVT::v4f32: return X86ISD::UNPCKHPS;
5528 case MVT::v2f64: return X86ISD::UNPCKHPD;
5529 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5530 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5531 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005532 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005533 }
5534 return 0;
5535}
5536
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005537static
5538SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005539 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005540 const X86Subtarget *Subtarget) {
5541 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5542 EVT VT = Op.getValueType();
5543 DebugLoc dl = Op.getDebugLoc();
5544 SDValue V1 = Op.getOperand(0);
5545 SDValue V2 = Op.getOperand(1);
5546
5547 if (isZeroShuffle(SVOp))
5548 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5549
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005550 // Handle splat operations
5551 if (SVOp->isSplat()) {
5552 // Special case, this is the only place now where it's
5553 // allowed to return a vector_shuffle operation without
5554 // using a target specific node, because *hopefully* it
5555 // will be optimized away by the dag combiner.
5556 if (VT.getVectorNumElements() <= 4 &&
5557 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5558 return Op;
5559
5560 // Handle splats by matching through known masks
5561 if (VT.getVectorNumElements() <= 4)
5562 return SDValue();
5563
Evan Cheng835580f2010-10-07 20:50:20 +00005564 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005565 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005566 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005567
5568 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5569 // do it!
5570 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5571 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5572 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005573 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005574 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5575 // FIXME: Figure out a cleaner way to do this.
5576 // Try to make use of movq to zero out the top part.
5577 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5578 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5579 if (NewOp.getNode()) {
5580 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5581 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5582 DAG, Subtarget, dl);
5583 }
5584 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5585 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5586 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5587 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5588 DAG, Subtarget, dl);
5589 }
5590 }
5591 return SDValue();
5592}
5593
Dan Gohman475871a2008-07-27 21:46:04 +00005594SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005595X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005596 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005597 SDValue V1 = Op.getOperand(0);
5598 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005599 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005600 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005601 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005602 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005603 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5604 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005605 bool V1IsSplat = false;
5606 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005607 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005608 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005609 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005610 MachineFunction &MF = DAG.getMachineFunction();
5611 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005612
Dale Johannesen0488fb62010-09-30 23:57:10 +00005613 // Shuffle operations on MMX not supported.
5614 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005615 return Op;
5616
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005617 // Vector shuffle lowering takes 3 steps:
5618 //
5619 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5620 // narrowing and commutation of operands should be handled.
5621 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5622 // shuffle nodes.
5623 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5624 // so the shuffle can be broken into other shuffles and the legalizer can
5625 // try the lowering again.
5626 //
5627 // The general ideia is that no vector_shuffle operation should be left to
5628 // be matched during isel, all of them must be converted to a target specific
5629 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005630
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005631 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5632 // narrowing and commutation of operands should be handled. The actual code
5633 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005634 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005635 if (NewOp.getNode())
5636 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005637
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005638 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5639 // unpckh_undef). Only use pshufd if speed is more important than size.
5640 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5641 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5642 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5643 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5644 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5645 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005646
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005647 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005648 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005649 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005650
Dale Johannesen0488fb62010-09-30 23:57:10 +00005651 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005652 return getMOVHighToLow(Op, dl, DAG);
5653
5654 // Use to match splats
5655 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5656 (VT == MVT::v2f64 || VT == MVT::v2i64))
5657 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5658
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005659 if (X86::isPSHUFDMask(SVOp)) {
5660 // The actual implementation will match the mask in the if above and then
5661 // during isel it can match several different instructions, not only pshufd
5662 // as its name says, sad but true, emulate the behavior for now...
5663 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5664 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5665
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005666 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5667
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005668 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005669 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5670
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005671 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005672 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5673 TargetMask, DAG);
5674
5675 if (VT == MVT::v4f32)
5676 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5677 TargetMask, DAG);
5678 }
Eric Christopherfd179292009-08-27 18:07:15 +00005679
Evan Chengf26ffe92008-05-29 08:22:04 +00005680 // Check if this can be converted into a logical shift.
5681 bool isLeft = false;
5682 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005683 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005684 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005685 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005686 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005687 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005688 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005689 EVT EltVT = VT.getVectorElementType();
5690 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005691 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005692 }
Eric Christopherfd179292009-08-27 18:07:15 +00005693
Nate Begeman9008ca62009-04-27 18:41:29 +00005694 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005695 if (V1IsUndef)
5696 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005697 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005698 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005699 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005700 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005701 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5702
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005703 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005704 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5705 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005706 }
Eric Christopherfd179292009-08-27 18:07:15 +00005707
Nate Begeman9008ca62009-04-27 18:41:29 +00005708 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005709 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5710 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005711
Dale Johannesen0488fb62010-09-30 23:57:10 +00005712 if (X86::isMOVHLPSMask(SVOp))
5713 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005714
Dale Johannesen0488fb62010-09-30 23:57:10 +00005715 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5716 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005717
Dale Johannesen0488fb62010-09-30 23:57:10 +00005718 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5719 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005720
Dale Johannesen0488fb62010-09-30 23:57:10 +00005721 if (X86::isMOVLPMask(SVOp))
5722 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005723
Nate Begeman9008ca62009-04-27 18:41:29 +00005724 if (ShouldXformToMOVHLPS(SVOp) ||
5725 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5726 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005727
Evan Chengf26ffe92008-05-29 08:22:04 +00005728 if (isShift) {
5729 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005730 EVT EltVT = VT.getVectorElementType();
5731 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005732 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005733 }
Eric Christopherfd179292009-08-27 18:07:15 +00005734
Evan Cheng9eca5e82006-10-25 21:49:50 +00005735 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005736 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5737 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005738 V1IsSplat = isSplatVector(V1.getNode());
5739 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005740
Chris Lattner8a594482007-11-25 00:24:49 +00005741 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005742 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005743 Op = CommuteVectorShuffle(SVOp, DAG);
5744 SVOp = cast<ShuffleVectorSDNode>(Op);
5745 V1 = SVOp->getOperand(0);
5746 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005747 std::swap(V1IsSplat, V2IsSplat);
5748 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005749 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005750 }
5751
Nate Begeman9008ca62009-04-27 18:41:29 +00005752 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5753 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005754 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005755 return V1;
5756 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5757 // the instruction selector will not match, so get a canonical MOVL with
5758 // swapped operands to undo the commute.
5759 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005760 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005761
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005762 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005763 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005764
5765 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005766 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005767
Evan Cheng9bbbb982006-10-25 20:48:19 +00005768 if (V2IsSplat) {
5769 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005770 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005771 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005772 SDValue NewMask = NormalizeMask(SVOp, DAG);
5773 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5774 if (NSVOp != SVOp) {
5775 if (X86::isUNPCKLMask(NSVOp, true)) {
5776 return NewMask;
5777 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5778 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005779 }
5780 }
5781 }
5782
Evan Cheng9eca5e82006-10-25 21:49:50 +00005783 if (Commuted) {
5784 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005785 // FIXME: this seems wrong.
5786 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5787 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005788
5789 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005790 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005791
5792 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005793 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005794 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005795
Nate Begeman9008ca62009-04-27 18:41:29 +00005796 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005797 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005798 return CommuteVectorShuffle(SVOp, DAG);
5799
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005800 // The checks below are all present in isShuffleMaskLegal, but they are
5801 // inlined here right now to enable us to directly emit target specific
5802 // nodes, and remove one by one until they don't return Op anymore.
5803 SmallVector<int, 16> M;
5804 SVOp->getMask(M);
5805
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005806 if (isPALIGNRMask(M, VT, HasSSSE3))
5807 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5808 X86::getShufflePALIGNRImmediate(SVOp),
5809 DAG);
5810
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005811 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5812 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5813 if (VT == MVT::v2f64)
5814 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5815 if (VT == MVT::v2i64)
5816 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5817 }
5818
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005819 if (isPSHUFHWMask(M, VT))
5820 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5821 X86::getShufflePSHUFHWImmediate(SVOp),
5822 DAG);
5823
5824 if (isPSHUFLWMask(M, VT))
5825 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5826 X86::getShufflePSHUFLWImmediate(SVOp),
5827 DAG);
5828
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005829 if (isSHUFPMask(M, VT)) {
5830 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5831 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5832 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5833 TargetMask, DAG);
5834 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5835 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5836 TargetMask, DAG);
5837 }
5838
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005839 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5840 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5841 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5842 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5843 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5844 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5845
Evan Cheng14b32e12007-12-11 01:46:18 +00005846 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005847 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005848 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005849 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005850 return NewOp;
5851 }
5852
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005854 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 if (NewOp.getNode())
5856 return NewOp;
5857 }
Eric Christopherfd179292009-08-27 18:07:15 +00005858
Dale Johannesen0488fb62010-09-30 23:57:10 +00005859 // Handle all 4 wide cases with a number of shuffles.
5860 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005861 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005862
Dan Gohman475871a2008-07-27 21:46:04 +00005863 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005864}
5865
Dan Gohman475871a2008-07-27 21:46:04 +00005866SDValue
5867X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005868 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005869 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005870 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005871 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005873 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005875 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005876 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005877 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005878 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5879 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5880 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005881 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5882 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005883 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005884 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005885 Op.getOperand(0)),
5886 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005888 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005890 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005891 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005892 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005893 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5894 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005895 // result has a single use which is a store or a bitcast to i32. And in
5896 // the case of a store, it's not worth it if the index is a constant 0,
5897 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005898 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005899 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005900 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005901 if ((User->getOpcode() != ISD::STORE ||
5902 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5903 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005904 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005906 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005907 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005908 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005909 Op.getOperand(0)),
5910 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005911 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005912 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005913 // ExtractPS works with constant index.
5914 if (isa<ConstantSDNode>(Op.getOperand(1)))
5915 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005916 }
Dan Gohman475871a2008-07-27 21:46:04 +00005917 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005918}
5919
5920
Dan Gohman475871a2008-07-27 21:46:04 +00005921SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005922X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5923 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005924 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005925 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005926
David Greene74a579d2011-02-10 16:57:36 +00005927 SDValue Vec = Op.getOperand(0);
5928 EVT VecVT = Vec.getValueType();
5929
5930 // If this is a 256-bit vector result, first extract the 128-bit
5931 // vector and then extract from the 128-bit vector.
5932 if (VecVT.getSizeInBits() > 128) {
5933 DebugLoc dl = Op.getNode()->getDebugLoc();
5934 unsigned NumElems = VecVT.getVectorNumElements();
5935 SDValue Idx = Op.getOperand(1);
5936
5937 if (!isa<ConstantSDNode>(Idx))
5938 return SDValue();
5939
5940 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5941 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5942
5943 // Get the 128-bit vector.
5944 bool Upper = IdxVal >= ExtractNumElems;
5945 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5946
5947 // Extract from it.
5948 SDValue ScaledIdx = Idx;
5949 if (Upper)
5950 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5951 DAG.getConstant(ExtractNumElems,
5952 Idx.getValueType()));
5953 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5954 ScaledIdx);
5955 }
5956
5957 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
5958
Evan Cheng62a3f152008-03-24 21:52:23 +00005959 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005961 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005962 return Res;
5963 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005964
Owen Andersone50ed302009-08-10 22:56:29 +00005965 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005966 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005967 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005968 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005969 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005970 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005971 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005972 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5973 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005974 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005975 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005976 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005977 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005978 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005979 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005980 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005981 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005982 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005983 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005984 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005985 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986 if (Idx == 0)
5987 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005988
Evan Cheng0db9fe62006-04-25 20:13:52 +00005989 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005990 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005991 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005992 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005993 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005994 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005995 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005996 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005997 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5998 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5999 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006000 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006001 if (Idx == 0)
6002 return Op;
6003
6004 // UNPCKHPD the element to the lowest double word, then movsd.
6005 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6006 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006007 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006008 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006009 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006010 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006011 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006012 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006013 }
6014
Dan Gohman475871a2008-07-27 21:46:04 +00006015 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006016}
6017
Dan Gohman475871a2008-07-27 21:46:04 +00006018SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006019X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6020 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006021 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006022 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006023 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006024
Dan Gohman475871a2008-07-27 21:46:04 +00006025 SDValue N0 = Op.getOperand(0);
6026 SDValue N1 = Op.getOperand(1);
6027 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006028
Dan Gohman8a55ce42009-09-23 21:02:20 +00006029 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006030 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006031 unsigned Opc;
6032 if (VT == MVT::v8i16)
6033 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006034 else if (VT == MVT::v16i8)
6035 Opc = X86ISD::PINSRB;
6036 else
6037 Opc = X86ISD::PINSRB;
6038
Nate Begeman14d12ca2008-02-11 04:19:36 +00006039 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6040 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006041 if (N1.getValueType() != MVT::i32)
6042 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6043 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006044 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006045 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006046 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006047 // Bits [7:6] of the constant are the source select. This will always be
6048 // zero here. The DAG Combiner may combine an extract_elt index into these
6049 // bits. For example (insert (extract, 3), 2) could be matched by putting
6050 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006051 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006052 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006053 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006054 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006055 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006056 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006058 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006059 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006060 // PINSR* works with constant index.
6061 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006062 }
Dan Gohman475871a2008-07-27 21:46:04 +00006063 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006064}
6065
Dan Gohman475871a2008-07-27 21:46:04 +00006066SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006067X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006068 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006069 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006070
David Greene6b381262011-02-09 15:32:06 +00006071 DebugLoc dl = Op.getDebugLoc();
6072 SDValue N0 = Op.getOperand(0);
6073 SDValue N1 = Op.getOperand(1);
6074 SDValue N2 = Op.getOperand(2);
6075
6076 // If this is a 256-bit vector result, first insert into a 128-bit
6077 // vector and then insert into the 256-bit vector.
6078 if (VT.getSizeInBits() > 128) {
6079 if (!isa<ConstantSDNode>(N2))
6080 return SDValue();
6081
6082 // Get the 128-bit vector.
6083 unsigned NumElems = VT.getVectorNumElements();
6084 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6085 bool Upper = IdxVal >= NumElems / 2;
6086
6087 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6088
6089 // Insert into it.
6090 SDValue ScaledN2 = N2;
6091 if (Upper)
6092 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
6093 DAG.getConstant(NumElems /
6094 (VT.getSizeInBits() / 128),
6095 N2.getValueType()));
6096 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6097 N1, ScaledN2);
6098
6099 // Insert the 128-bit vector
6100 // FIXME: Why UNDEF?
6101 return Insert128BitVector(N0, Op, N2, DAG, dl);
6102 }
6103
Nate Begeman14d12ca2008-02-11 04:19:36 +00006104 if (Subtarget->hasSSE41())
6105 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6106
Dan Gohman8a55ce42009-09-23 21:02:20 +00006107 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006108 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006109
Dan Gohman8a55ce42009-09-23 21:02:20 +00006110 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006111 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6112 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006113 if (N1.getValueType() != MVT::i32)
6114 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6115 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006116 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006117 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006118 }
Dan Gohman475871a2008-07-27 21:46:04 +00006119 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006120}
6121
Dan Gohman475871a2008-07-27 21:46:04 +00006122SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006123X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006124 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006125 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006126 EVT OpVT = Op.getValueType();
6127
6128 // If this is a 256-bit vector result, first insert into a 128-bit
6129 // vector and then insert into the 256-bit vector.
6130 if (OpVT.getSizeInBits() > 128) {
6131 // Insert into a 128-bit vector.
6132 EVT VT128 = EVT::getVectorVT(*Context,
6133 OpVT.getVectorElementType(),
6134 OpVT.getVectorNumElements() / 2);
6135
6136 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6137
6138 // Insert the 128-bit vector.
6139 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6140 DAG.getConstant(0, MVT::i32),
6141 DAG, dl);
6142 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006143
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006144 if (Op.getValueType() == MVT::v1i64 &&
6145 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006147
Owen Anderson825b72b2009-08-11 20:47:22 +00006148 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006149 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6150 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006151 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006152 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006153}
6154
David Greene91585092011-01-26 15:38:49 +00006155// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6156// a simple subregister reference or explicit instructions to grab
6157// upper bits of a vector.
6158SDValue
6159X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6160 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006161 DebugLoc dl = Op.getNode()->getDebugLoc();
6162 SDValue Vec = Op.getNode()->getOperand(0);
6163 SDValue Idx = Op.getNode()->getOperand(1);
6164
6165 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6166 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6167 return Extract128BitVector(Vec, Idx, DAG, dl);
6168 }
David Greene91585092011-01-26 15:38:49 +00006169 }
6170 return SDValue();
6171}
6172
David Greenecfe33c42011-01-26 19:13:22 +00006173// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6174// simple superregister reference or explicit instructions to insert
6175// the upper bits of a vector.
6176SDValue
6177X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6178 if (Subtarget->hasAVX()) {
6179 DebugLoc dl = Op.getNode()->getDebugLoc();
6180 SDValue Vec = Op.getNode()->getOperand(0);
6181 SDValue SubVec = Op.getNode()->getOperand(1);
6182 SDValue Idx = Op.getNode()->getOperand(2);
6183
6184 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6185 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006186 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006187 }
6188 }
6189 return SDValue();
6190}
6191
Bill Wendling056292f2008-09-16 21:48:12 +00006192// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6193// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6194// one of the above mentioned nodes. It has to be wrapped because otherwise
6195// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6196// be used to form addressing mode. These wrapped nodes will be selected
6197// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006198SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006199X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006200 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006201
Chris Lattner41621a22009-06-26 19:22:52 +00006202 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6203 // global base reg.
6204 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006205 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006206 CodeModel::Model M = getTargetMachine().getCodeModel();
6207
Chris Lattner4f066492009-07-11 20:29:19 +00006208 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006209 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006210 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006211 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006212 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006213 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006214 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006215
Evan Cheng1606e8e2009-03-13 07:51:59 +00006216 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006217 CP->getAlignment(),
6218 CP->getOffset(), OpFlag);
6219 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006220 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006221 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006222 if (OpFlag) {
6223 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006224 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006225 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006226 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006227 }
6228
6229 return Result;
6230}
6231
Dan Gohmand858e902010-04-17 15:26:15 +00006232SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006233 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006234
Chris Lattner18c59872009-06-27 04:16:01 +00006235 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6236 // global base reg.
6237 unsigned char OpFlag = 0;
6238 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006239 CodeModel::Model M = getTargetMachine().getCodeModel();
6240
Chris Lattner4f066492009-07-11 20:29:19 +00006241 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006242 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006243 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006244 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006245 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006246 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006247 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006248
Chris Lattner18c59872009-06-27 04:16:01 +00006249 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6250 OpFlag);
6251 DebugLoc DL = JT->getDebugLoc();
6252 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006253
Chris Lattner18c59872009-06-27 04:16:01 +00006254 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006255 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006256 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6257 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006258 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006259 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006260
Chris Lattner18c59872009-06-27 04:16:01 +00006261 return Result;
6262}
6263
6264SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006265X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006266 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006267
Chris Lattner18c59872009-06-27 04:16:01 +00006268 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6269 // global base reg.
6270 unsigned char OpFlag = 0;
6271 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006272 CodeModel::Model M = getTargetMachine().getCodeModel();
6273
Chris Lattner4f066492009-07-11 20:29:19 +00006274 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006275 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006276 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006277 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006278 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006279 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006280 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006281
Chris Lattner18c59872009-06-27 04:16:01 +00006282 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006283
Chris Lattner18c59872009-06-27 04:16:01 +00006284 DebugLoc DL = Op.getDebugLoc();
6285 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006286
6287
Chris Lattner18c59872009-06-27 04:16:01 +00006288 // With PIC, the address is actually $g + Offset.
6289 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006290 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006291 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6292 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006293 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006294 Result);
6295 }
Eric Christopherfd179292009-08-27 18:07:15 +00006296
Chris Lattner18c59872009-06-27 04:16:01 +00006297 return Result;
6298}
6299
Dan Gohman475871a2008-07-27 21:46:04 +00006300SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006301X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006302 // Create the TargetBlockAddressAddress node.
6303 unsigned char OpFlags =
6304 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006305 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006306 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006307 DebugLoc dl = Op.getDebugLoc();
6308 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6309 /*isTarget=*/true, OpFlags);
6310
Dan Gohmanf705adb2009-10-30 01:28:02 +00006311 if (Subtarget->isPICStyleRIPRel() &&
6312 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006313 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6314 else
6315 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006316
Dan Gohman29cbade2009-11-20 23:18:13 +00006317 // With PIC, the address is actually $g + Offset.
6318 if (isGlobalRelativeToPICBase(OpFlags)) {
6319 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6320 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6321 Result);
6322 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006323
6324 return Result;
6325}
6326
6327SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006328X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006329 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006330 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006331 // Create the TargetGlobalAddress node, folding in the constant
6332 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006333 unsigned char OpFlags =
6334 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006335 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006336 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006337 if (OpFlags == X86II::MO_NO_FLAG &&
6338 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006339 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006340 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006341 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006342 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006343 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006344 }
Eric Christopherfd179292009-08-27 18:07:15 +00006345
Chris Lattner4f066492009-07-11 20:29:19 +00006346 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006347 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006348 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6349 else
6350 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006351
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006352 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006353 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006354 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6355 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006356 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006357 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006358
Chris Lattner36c25012009-07-10 07:34:39 +00006359 // For globals that require a load from a stub to get the address, emit the
6360 // load.
6361 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006362 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006363 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006364
Dan Gohman6520e202008-10-18 02:06:02 +00006365 // If there was a non-zero offset that we didn't fold, create an explicit
6366 // addition for it.
6367 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006368 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006369 DAG.getConstant(Offset, getPointerTy()));
6370
Evan Cheng0db9fe62006-04-25 20:13:52 +00006371 return Result;
6372}
6373
Evan Chengda43bcf2008-09-24 00:05:32 +00006374SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006375X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006376 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006377 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006378 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006379}
6380
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006381static SDValue
6382GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006383 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006384 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006385 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006386 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006387 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006388 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006389 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006390 GA->getOffset(),
6391 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006392 if (InFlag) {
6393 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006394 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006395 } else {
6396 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006397 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006398 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006399
6400 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006401 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006402
Rafael Espindola15f1b662009-04-24 12:59:40 +00006403 SDValue Flag = Chain.getValue(1);
6404 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006405}
6406
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006407// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006408static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006409LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006410 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006411 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006412 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6413 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006414 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006415 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006416 InFlag = Chain.getValue(1);
6417
Chris Lattnerb903bed2009-06-26 21:20:29 +00006418 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006419}
6420
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006421// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006422static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006423LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006424 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006425 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6426 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006427}
6428
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006429// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6430// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006431static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006432 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006433 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006434 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006435
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006436 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6437 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6438 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006439
Michael J. Spencerec38de22010-10-10 22:04:20 +00006440 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006441 DAG.getIntPtrConstant(0),
6442 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006443
Chris Lattnerb903bed2009-06-26 21:20:29 +00006444 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006445 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6446 // initialexec.
6447 unsigned WrapperKind = X86ISD::Wrapper;
6448 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006449 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006450 } else if (is64Bit) {
6451 assert(model == TLSModel::InitialExec);
6452 OperandFlags = X86II::MO_GOTTPOFF;
6453 WrapperKind = X86ISD::WrapperRIP;
6454 } else {
6455 assert(model == TLSModel::InitialExec);
6456 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006457 }
Eric Christopherfd179292009-08-27 18:07:15 +00006458
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006459 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6460 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006461 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006462 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006463 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006464 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006465
Rafael Espindola9a580232009-02-27 13:37:18 +00006466 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006467 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006468 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006469
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006470 // The address of the thread local variable is the add of the thread
6471 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006472 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006473}
6474
Dan Gohman475871a2008-07-27 21:46:04 +00006475SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006476X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006477
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006478 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006479 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006480
Eric Christopher30ef0e52010-06-03 04:07:48 +00006481 if (Subtarget->isTargetELF()) {
6482 // TODO: implement the "local dynamic" model
6483 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006484
Eric Christopher30ef0e52010-06-03 04:07:48 +00006485 // If GV is an alias then use the aliasee for determining
6486 // thread-localness.
6487 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6488 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006489
6490 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006491 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006492
Eric Christopher30ef0e52010-06-03 04:07:48 +00006493 switch (model) {
6494 case TLSModel::GeneralDynamic:
6495 case TLSModel::LocalDynamic: // not implemented
6496 if (Subtarget->is64Bit())
6497 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6498 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006499
Eric Christopher30ef0e52010-06-03 04:07:48 +00006500 case TLSModel::InitialExec:
6501 case TLSModel::LocalExec:
6502 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6503 Subtarget->is64Bit());
6504 }
6505 } else if (Subtarget->isTargetDarwin()) {
6506 // Darwin only has one model of TLS. Lower to that.
6507 unsigned char OpFlag = 0;
6508 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6509 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006510
Eric Christopher30ef0e52010-06-03 04:07:48 +00006511 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6512 // global base reg.
6513 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6514 !Subtarget->is64Bit();
6515 if (PIC32)
6516 OpFlag = X86II::MO_TLVP_PIC_BASE;
6517 else
6518 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006519 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006520 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006521 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006522 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006523 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006524
Eric Christopher30ef0e52010-06-03 04:07:48 +00006525 // With PIC32, the address is actually $g + Offset.
6526 if (PIC32)
6527 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6528 DAG.getNode(X86ISD::GlobalBaseReg,
6529 DebugLoc(), getPointerTy()),
6530 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006531
Eric Christopher30ef0e52010-06-03 04:07:48 +00006532 // Lowering the machine isd will make sure everything is in the right
6533 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006534 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006535 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006536 SDValue Args[] = { Chain, Offset };
6537 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006538
Eric Christopher30ef0e52010-06-03 04:07:48 +00006539 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6540 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6541 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006542
Eric Christopher30ef0e52010-06-03 04:07:48 +00006543 // And our return value (tls address) is in the standard call return value
6544 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006545 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6546 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006547 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006548
Eric Christopher30ef0e52010-06-03 04:07:48 +00006549 assert(false &&
6550 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006551
Torok Edwinc23197a2009-07-14 16:55:14 +00006552 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006553 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006554}
6555
Evan Cheng0db9fe62006-04-25 20:13:52 +00006556
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006557/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006558/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006559SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006560 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006561 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006562 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006563 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006564 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006565 SDValue ShOpLo = Op.getOperand(0);
6566 SDValue ShOpHi = Op.getOperand(1);
6567 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006568 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006569 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006570 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006571
Dan Gohman475871a2008-07-27 21:46:04 +00006572 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006573 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006574 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6575 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006576 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006577 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6578 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006579 }
Evan Chenge3413162006-01-09 18:33:28 +00006580
Owen Anderson825b72b2009-08-11 20:47:22 +00006581 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6582 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006583 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006584 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006585
Dan Gohman475871a2008-07-27 21:46:04 +00006586 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006587 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006588 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6589 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006590
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006591 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006592 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6593 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006594 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006595 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6596 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006597 }
6598
Dan Gohman475871a2008-07-27 21:46:04 +00006599 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006600 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006601}
Evan Chenga3195e82006-01-12 22:54:21 +00006602
Dan Gohmand858e902010-04-17 15:26:15 +00006603SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6604 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006605 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006606
Dale Johannesen0488fb62010-09-30 23:57:10 +00006607 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006608 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006609
Owen Anderson825b72b2009-08-11 20:47:22 +00006610 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006611 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006612
Eli Friedman36df4992009-05-27 00:47:34 +00006613 // These are really Legal; return the operand so the caller accepts it as
6614 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006615 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006616 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006617 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006618 Subtarget->is64Bit()) {
6619 return Op;
6620 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006621
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006622 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006623 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006624 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006625 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006626 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006627 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006628 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006629 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006630 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006631 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6632}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006633
Owen Andersone50ed302009-08-10 22:56:29 +00006634SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006635 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006636 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006637 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006638 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006639 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006640 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006641 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006642 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006643 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006645
Chris Lattner492a43e2010-09-22 01:28:21 +00006646 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006647
Chris Lattner492a43e2010-09-22 01:28:21 +00006648 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6649 MachineMemOperand *MMO =
6650 DAG.getMachineFunction()
6651 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6652 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006653
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006654 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006655 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6656 X86ISD::FILD, DL,
6657 Tys, Ops, array_lengthof(Ops),
6658 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006659
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006660 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006662 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663
6664 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6665 // shouldn't be necessary except that RFP cannot be live across
6666 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006667 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006668 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6669 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006670 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006671 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006672 SDValue Ops[] = {
6673 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6674 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006675 MachineMemOperand *MMO =
6676 DAG.getMachineFunction()
6677 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006678 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006679
Chris Lattner492a43e2010-09-22 01:28:21 +00006680 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6681 Ops, array_lengthof(Ops),
6682 Op.getValueType(), MMO);
6683 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006684 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006685 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006686 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006687
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688 return Result;
6689}
6690
Bill Wendling8b8a6362009-01-17 03:56:04 +00006691// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006692SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6693 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006694 // This algorithm is not obvious. Here it is in C code, more or less:
6695 /*
6696 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6697 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6698 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006699
Bill Wendling8b8a6362009-01-17 03:56:04 +00006700 // Copy ints to xmm registers.
6701 __m128i xh = _mm_cvtsi32_si128( hi );
6702 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006703
Bill Wendling8b8a6362009-01-17 03:56:04 +00006704 // Combine into low half of a single xmm register.
6705 __m128i x = _mm_unpacklo_epi32( xh, xl );
6706 __m128d d;
6707 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006708
Bill Wendling8b8a6362009-01-17 03:56:04 +00006709 // Merge in appropriate exponents to give the integer bits the right
6710 // magnitude.
6711 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006712
Bill Wendling8b8a6362009-01-17 03:56:04 +00006713 // Subtract away the biases to deal with the IEEE-754 double precision
6714 // implicit 1.
6715 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006716
Bill Wendling8b8a6362009-01-17 03:56:04 +00006717 // All conversions up to here are exact. The correctly rounded result is
6718 // calculated using the current rounding mode using the following
6719 // horizontal add.
6720 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6721 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6722 // store doesn't really need to be here (except
6723 // maybe to zero the other double)
6724 return sd;
6725 }
6726 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006727
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006728 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006729 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006730
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006731 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006732 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006733 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6734 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6735 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6736 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006737 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006738 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006739
Bill Wendling8b8a6362009-01-17 03:56:04 +00006740 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006741 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006742 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006743 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006744 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006745 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006746 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006747
Owen Anderson825b72b2009-08-11 20:47:22 +00006748 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6749 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006750 Op.getOperand(0),
6751 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006752 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6753 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006754 Op.getOperand(0),
6755 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6757 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006758 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006759 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006760 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006761 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006762 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006763 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006764 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006765 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006766
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006767 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006768 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006769 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6770 DAG.getUNDEF(MVT::v2f64), ShufMask);
6771 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6772 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006773 DAG.getIntPtrConstant(0));
6774}
6775
Bill Wendling8b8a6362009-01-17 03:56:04 +00006776// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006777SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6778 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006779 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006780 // FP constant to bias correct the final result.
6781 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006783
6784 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006785 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6786 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006787 Op.getOperand(0),
6788 DAG.getIntPtrConstant(0)));
6789
Owen Anderson825b72b2009-08-11 20:47:22 +00006790 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006791 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006792 DAG.getIntPtrConstant(0));
6793
6794 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006795 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006796 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006797 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006798 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006799 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006800 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006801 MVT::v2f64, Bias)));
6802 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006803 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006804 DAG.getIntPtrConstant(0));
6805
6806 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006808
6809 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006810 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006811
Owen Anderson825b72b2009-08-11 20:47:22 +00006812 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006813 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006814 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006815 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006816 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006817 }
6818
6819 // Handle final rounding.
6820 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006821}
6822
Dan Gohmand858e902010-04-17 15:26:15 +00006823SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6824 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006825 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006826 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006827
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006828 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006829 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6830 // the optimization here.
6831 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006832 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006833
Owen Andersone50ed302009-08-10 22:56:29 +00006834 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006835 EVT DstVT = Op.getValueType();
6836 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006837 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006838 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006839 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006840
6841 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006843 if (SrcVT == MVT::i32) {
6844 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6845 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6846 getPointerTy(), StackSlot, WordOff);
6847 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006848 StackSlot, MachinePointerInfo(),
6849 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006850 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006851 OffsetSlot, MachinePointerInfo(),
6852 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006853 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6854 return Fild;
6855 }
6856
6857 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6858 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006859 StackSlot, MachinePointerInfo(),
6860 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006861 // For i64 source, we need to add the appropriate power of 2 if the input
6862 // was negative. This is the same as the optimization in
6863 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6864 // we must be careful to do the computation in x87 extended precision, not
6865 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006866 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6867 MachineMemOperand *MMO =
6868 DAG.getMachineFunction()
6869 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6870 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006871
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006872 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6873 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006874 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6875 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006876
6877 APInt FF(32, 0x5F800000ULL);
6878
6879 // Check whether the sign bit is set.
6880 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6881 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6882 ISD::SETLT);
6883
6884 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6885 SDValue FudgePtr = DAG.getConstantPool(
6886 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6887 getPointerTy());
6888
6889 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6890 SDValue Zero = DAG.getIntPtrConstant(0);
6891 SDValue Four = DAG.getIntPtrConstant(4);
6892 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6893 Zero, Four);
6894 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6895
6896 // Load the value out, extending it from f32 to f80.
6897 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006898 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006899 FudgePtr, MachinePointerInfo::getConstantPool(),
6900 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006901 // Extend everything to 80 bits to force it to be done on x87.
6902 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6903 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006904}
6905
Dan Gohman475871a2008-07-27 21:46:04 +00006906std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006907FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006908 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006909
Owen Andersone50ed302009-08-10 22:56:29 +00006910 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006911
6912 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6914 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006915 }
6916
Owen Anderson825b72b2009-08-11 20:47:22 +00006917 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6918 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006919 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006920
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006921 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006923 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006924 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006925 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006927 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006928 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006929
Evan Cheng87c89352007-10-15 20:11:21 +00006930 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6931 // stack slot.
6932 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006933 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006934 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006935 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006936
Michael J. Spencerec38de22010-10-10 22:04:20 +00006937
6938
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006941 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6943 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6944 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006945 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006946
Dan Gohman475871a2008-07-27 21:46:04 +00006947 SDValue Chain = DAG.getEntryNode();
6948 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006949 EVT TheVT = Op.getOperand(0).getValueType();
6950 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006952 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006953 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006954 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006956 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006957 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006958 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006959
Chris Lattner492a43e2010-09-22 01:28:21 +00006960 MachineMemOperand *MMO =
6961 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6962 MachineMemOperand::MOLoad, MemSize, MemSize);
6963 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6964 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006965 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006966 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006967 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6968 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006969
Chris Lattner07290932010-09-22 01:05:16 +00006970 MachineMemOperand *MMO =
6971 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6972 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006973
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006975 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006976 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6977 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006978
Chris Lattner27a6c732007-11-24 07:07:01 +00006979 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006980}
6981
Dan Gohmand858e902010-04-17 15:26:15 +00006982SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6983 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006984 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006985 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006986
Eli Friedman948e95a2009-05-23 09:59:16 +00006987 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006988 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006989 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6990 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006991
Chris Lattner27a6c732007-11-24 07:07:01 +00006992 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006993 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006994 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006995}
6996
Dan Gohmand858e902010-04-17 15:26:15 +00006997SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6998 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006999 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7000 SDValue FIST = Vals.first, StackSlot = Vals.second;
7001 assert(FIST.getNode() && "Unexpected failure");
7002
7003 // Load the result.
7004 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007005 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007006}
7007
Dan Gohmand858e902010-04-17 15:26:15 +00007008SDValue X86TargetLowering::LowerFABS(SDValue Op,
7009 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007010 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007011 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007012 EVT VT = Op.getValueType();
7013 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007014 if (VT.isVector())
7015 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007016 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007017 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007018 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007019 CV.push_back(C);
7020 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007021 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007022 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007023 CV.push_back(C);
7024 CV.push_back(C);
7025 CV.push_back(C);
7026 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007028 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007029 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007030 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007031 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007032 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007033 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007034}
7035
Dan Gohmand858e902010-04-17 15:26:15 +00007036SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007037 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007038 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007039 EVT VT = Op.getValueType();
7040 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007041 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007042 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007043 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007045 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007046 CV.push_back(C);
7047 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007048 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007049 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007050 CV.push_back(C);
7051 CV.push_back(C);
7052 CV.push_back(C);
7053 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007054 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007055 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007056 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007057 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007058 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007059 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007060 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007061 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007063 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007064 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007065 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007066 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007067 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007068 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007069}
7070
Dan Gohmand858e902010-04-17 15:26:15 +00007071SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007072 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007073 SDValue Op0 = Op.getOperand(0);
7074 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007075 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007076 EVT VT = Op.getValueType();
7077 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007078
7079 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007080 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007081 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007082 SrcVT = VT;
7083 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007084 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007085 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007086 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007087 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007088 }
7089
7090 // At this point the operands and the result should have the same
7091 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007092
Evan Cheng68c47cb2007-01-05 07:55:56 +00007093 // First get the sign bit of second operand.
7094 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007096 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7097 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007098 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007099 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7100 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7101 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7102 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007103 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007104 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007105 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007106 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007107 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007108 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007109 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007110
7111 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007112 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 // Op0 is MVT::f32, Op1 is MVT::f64.
7114 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7115 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7116 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007117 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007119 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007120 }
7121
Evan Cheng73d6cf12007-01-05 21:37:56 +00007122 // Clear first operand sign bit.
7123 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007125 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7126 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007127 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007128 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7129 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7130 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7131 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007132 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007133 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007134 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007135 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007136 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007137 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007138 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007139
7140 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007141 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007142}
7143
Dan Gohman076aee32009-03-04 19:44:21 +00007144/// Emit nodes that will be selected as "test Op0,Op0", or something
7145/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007146SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007147 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007148 DebugLoc dl = Op.getDebugLoc();
7149
Dan Gohman31125812009-03-07 01:58:32 +00007150 // CF and OF aren't always set the way we want. Determine which
7151 // of these we need.
7152 bool NeedCF = false;
7153 bool NeedOF = false;
7154 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007155 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007156 case X86::COND_A: case X86::COND_AE:
7157 case X86::COND_B: case X86::COND_BE:
7158 NeedCF = true;
7159 break;
7160 case X86::COND_G: case X86::COND_GE:
7161 case X86::COND_L: case X86::COND_LE:
7162 case X86::COND_O: case X86::COND_NO:
7163 NeedOF = true;
7164 break;
Dan Gohman31125812009-03-07 01:58:32 +00007165 }
7166
Dan Gohman076aee32009-03-04 19:44:21 +00007167 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007168 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7169 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007170 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7171 // Emit a CMP with 0, which is the TEST pattern.
7172 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7173 DAG.getConstant(0, Op.getValueType()));
7174
7175 unsigned Opcode = 0;
7176 unsigned NumOperands = 0;
7177 switch (Op.getNode()->getOpcode()) {
7178 case ISD::ADD:
7179 // Due to an isel shortcoming, be conservative if this add is likely to be
7180 // selected as part of a load-modify-store instruction. When the root node
7181 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7182 // uses of other nodes in the match, such as the ADD in this case. This
7183 // leads to the ADD being left around and reselected, with the result being
7184 // two adds in the output. Alas, even if none our users are stores, that
7185 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7186 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7187 // climbing the DAG back to the root, and it doesn't seem to be worth the
7188 // effort.
7189 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007190 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007191 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7192 goto default_case;
7193
7194 if (ConstantSDNode *C =
7195 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7196 // An add of one will be selected as an INC.
7197 if (C->getAPIntValue() == 1) {
7198 Opcode = X86ISD::INC;
7199 NumOperands = 1;
7200 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007201 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007202
7203 // An add of negative one (subtract of one) will be selected as a DEC.
7204 if (C->getAPIntValue().isAllOnesValue()) {
7205 Opcode = X86ISD::DEC;
7206 NumOperands = 1;
7207 break;
7208 }
Dan Gohman076aee32009-03-04 19:44:21 +00007209 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007210
7211 // Otherwise use a regular EFLAGS-setting add.
7212 Opcode = X86ISD::ADD;
7213 NumOperands = 2;
7214 break;
7215 case ISD::AND: {
7216 // If the primary and result isn't used, don't bother using X86ISD::AND,
7217 // because a TEST instruction will be better.
7218 bool NonFlagUse = false;
7219 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7220 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7221 SDNode *User = *UI;
7222 unsigned UOpNo = UI.getOperandNo();
7223 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7224 // Look pass truncate.
7225 UOpNo = User->use_begin().getOperandNo();
7226 User = *User->use_begin();
7227 }
7228
7229 if (User->getOpcode() != ISD::BRCOND &&
7230 User->getOpcode() != ISD::SETCC &&
7231 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7232 NonFlagUse = true;
7233 break;
7234 }
Dan Gohman076aee32009-03-04 19:44:21 +00007235 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007236
7237 if (!NonFlagUse)
7238 break;
7239 }
7240 // FALL THROUGH
7241 case ISD::SUB:
7242 case ISD::OR:
7243 case ISD::XOR:
7244 // Due to the ISEL shortcoming noted above, be conservative if this op is
7245 // likely to be selected as part of a load-modify-store instruction.
7246 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7247 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7248 if (UI->getOpcode() == ISD::STORE)
7249 goto default_case;
7250
7251 // Otherwise use a regular EFLAGS-setting instruction.
7252 switch (Op.getNode()->getOpcode()) {
7253 default: llvm_unreachable("unexpected operator!");
7254 case ISD::SUB: Opcode = X86ISD::SUB; break;
7255 case ISD::OR: Opcode = X86ISD::OR; break;
7256 case ISD::XOR: Opcode = X86ISD::XOR; break;
7257 case ISD::AND: Opcode = X86ISD::AND; break;
7258 }
7259
7260 NumOperands = 2;
7261 break;
7262 case X86ISD::ADD:
7263 case X86ISD::SUB:
7264 case X86ISD::INC:
7265 case X86ISD::DEC:
7266 case X86ISD::OR:
7267 case X86ISD::XOR:
7268 case X86ISD::AND:
7269 return SDValue(Op.getNode(), 1);
7270 default:
7271 default_case:
7272 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007273 }
7274
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007275 if (Opcode == 0)
7276 // Emit a CMP with 0, which is the TEST pattern.
7277 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7278 DAG.getConstant(0, Op.getValueType()));
7279
7280 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7281 SmallVector<SDValue, 4> Ops;
7282 for (unsigned i = 0; i != NumOperands; ++i)
7283 Ops.push_back(Op.getOperand(i));
7284
7285 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7286 DAG.ReplaceAllUsesWith(Op, New);
7287 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007288}
7289
7290/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7291/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007292SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007293 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007294 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7295 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007296 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007297
7298 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007299 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007300}
7301
Evan Chengd40d03e2010-01-06 19:38:29 +00007302/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7303/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007304SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7305 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007306 SDValue Op0 = And.getOperand(0);
7307 SDValue Op1 = And.getOperand(1);
7308 if (Op0.getOpcode() == ISD::TRUNCATE)
7309 Op0 = Op0.getOperand(0);
7310 if (Op1.getOpcode() == ISD::TRUNCATE)
7311 Op1 = Op1.getOperand(0);
7312
Evan Chengd40d03e2010-01-06 19:38:29 +00007313 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007314 if (Op1.getOpcode() == ISD::SHL)
7315 std::swap(Op0, Op1);
7316 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007317 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7318 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007319 // If we looked past a truncate, check that it's only truncating away
7320 // known zeros.
7321 unsigned BitWidth = Op0.getValueSizeInBits();
7322 unsigned AndBitWidth = And.getValueSizeInBits();
7323 if (BitWidth > AndBitWidth) {
7324 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7325 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7326 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7327 return SDValue();
7328 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007329 LHS = Op1;
7330 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007331 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007332 } else if (Op1.getOpcode() == ISD::Constant) {
7333 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7334 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007335 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7336 LHS = AndLHS.getOperand(0);
7337 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007338 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007339 }
Evan Cheng0488db92007-09-25 01:57:46 +00007340
Evan Chengd40d03e2010-01-06 19:38:29 +00007341 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007342 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007343 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007344 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007345 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007346 // Also promote i16 to i32 for performance / code size reason.
7347 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007348 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007349 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007350
Evan Chengd40d03e2010-01-06 19:38:29 +00007351 // If the operand types disagree, extend the shift amount to match. Since
7352 // BT ignores high bits (like shifts) we can use anyextend.
7353 if (LHS.getValueType() != RHS.getValueType())
7354 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007355
Evan Chengd40d03e2010-01-06 19:38:29 +00007356 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7357 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7358 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7359 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007360 }
7361
Evan Cheng54de3ea2010-01-05 06:52:31 +00007362 return SDValue();
7363}
7364
Dan Gohmand858e902010-04-17 15:26:15 +00007365SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007366 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7367 SDValue Op0 = Op.getOperand(0);
7368 SDValue Op1 = Op.getOperand(1);
7369 DebugLoc dl = Op.getDebugLoc();
7370 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7371
7372 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007373 // Lower (X & (1 << N)) == 0 to BT(X, N).
7374 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7375 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007376 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007377 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007378 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007379 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7380 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7381 if (NewSetCC.getNode())
7382 return NewSetCC;
7383 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007384
Chris Lattner481eebc2010-12-19 21:23:48 +00007385 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7386 // these.
7387 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007388 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7389 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7390 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007391
Chris Lattner481eebc2010-12-19 21:23:48 +00007392 // If the input is a setcc, then reuse the input setcc or use a new one with
7393 // the inverted condition.
7394 if (Op0.getOpcode() == X86ISD::SETCC) {
7395 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7396 bool Invert = (CC == ISD::SETNE) ^
7397 cast<ConstantSDNode>(Op1)->isNullValue();
7398 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007399
Evan Cheng2c755ba2010-02-27 07:36:59 +00007400 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007401 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7402 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7403 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007404 }
7405
Evan Chenge5b51ac2010-04-17 06:13:15 +00007406 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007407 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007408 if (X86CC == X86::COND_INVALID)
7409 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007410
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007411 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007412 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007413 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007414}
7415
Dan Gohmand858e902010-04-17 15:26:15 +00007416SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007417 SDValue Cond;
7418 SDValue Op0 = Op.getOperand(0);
7419 SDValue Op1 = Op.getOperand(1);
7420 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007421 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007422 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7423 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007424 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007425
7426 if (isFP) {
7427 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007428 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007429 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7430 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007431 bool Swap = false;
7432
7433 switch (SetCCOpcode) {
7434 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007435 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007436 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007437 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007438 case ISD::SETGT: Swap = true; // Fallthrough
7439 case ISD::SETLT:
7440 case ISD::SETOLT: SSECC = 1; break;
7441 case ISD::SETOGE:
7442 case ISD::SETGE: Swap = true; // Fallthrough
7443 case ISD::SETLE:
7444 case ISD::SETOLE: SSECC = 2; break;
7445 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007446 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007447 case ISD::SETNE: SSECC = 4; break;
7448 case ISD::SETULE: Swap = true;
7449 case ISD::SETUGE: SSECC = 5; break;
7450 case ISD::SETULT: Swap = true;
7451 case ISD::SETUGT: SSECC = 6; break;
7452 case ISD::SETO: SSECC = 7; break;
7453 }
7454 if (Swap)
7455 std::swap(Op0, Op1);
7456
Nate Begemanfb8ead02008-07-25 19:05:58 +00007457 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007458 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007459 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007460 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7462 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007463 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007464 }
7465 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007466 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7468 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007469 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007470 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007471 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007472 }
7473 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007474 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007476
Nate Begeman30a0de92008-07-17 16:51:19 +00007477 // We are handling one of the integer comparisons here. Since SSE only has
7478 // GT and EQ comparisons for integer, swapping operands and multiple
7479 // operations may be required for some comparisons.
7480 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7481 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007482
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007484 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007486 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007487 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7488 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007490
Nate Begeman30a0de92008-07-17 16:51:19 +00007491 switch (SetCCOpcode) {
7492 default: break;
7493 case ISD::SETNE: Invert = true;
7494 case ISD::SETEQ: Opc = EQOpc; break;
7495 case ISD::SETLT: Swap = true;
7496 case ISD::SETGT: Opc = GTOpc; break;
7497 case ISD::SETGE: Swap = true;
7498 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7499 case ISD::SETULT: Swap = true;
7500 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7501 case ISD::SETUGE: Swap = true;
7502 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7503 }
7504 if (Swap)
7505 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007506
Nate Begeman30a0de92008-07-17 16:51:19 +00007507 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7508 // bits of the inputs before performing those operations.
7509 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007510 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007511 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7512 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007513 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007514 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7515 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007516 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7517 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007519
Dale Johannesenace16102009-02-03 19:33:06 +00007520 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007521
7522 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007523 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007524 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007525
Nate Begeman30a0de92008-07-17 16:51:19 +00007526 return Result;
7527}
Evan Cheng0488db92007-09-25 01:57:46 +00007528
Evan Cheng370e5342008-12-03 08:38:43 +00007529// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007530static bool isX86LogicalCmp(SDValue Op) {
7531 unsigned Opc = Op.getNode()->getOpcode();
7532 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7533 return true;
7534 if (Op.getResNo() == 1 &&
7535 (Opc == X86ISD::ADD ||
7536 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007537 Opc == X86ISD::ADC ||
7538 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007539 Opc == X86ISD::SMUL ||
7540 Opc == X86ISD::UMUL ||
7541 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007542 Opc == X86ISD::DEC ||
7543 Opc == X86ISD::OR ||
7544 Opc == X86ISD::XOR ||
7545 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007546 return true;
7547
Chris Lattner9637d5b2010-12-05 07:49:54 +00007548 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7549 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007550
Dan Gohman076aee32009-03-04 19:44:21 +00007551 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007552}
7553
Chris Lattnera2b56002010-12-05 01:23:24 +00007554static bool isZero(SDValue V) {
7555 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7556 return C && C->isNullValue();
7557}
7558
Chris Lattner96908b12010-12-05 02:00:51 +00007559static bool isAllOnes(SDValue V) {
7560 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7561 return C && C->isAllOnesValue();
7562}
7563
Dan Gohmand858e902010-04-17 15:26:15 +00007564SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007565 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007566 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007567 SDValue Op1 = Op.getOperand(1);
7568 SDValue Op2 = Op.getOperand(2);
7569 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007570 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007571
Dan Gohman1a492952009-10-20 16:22:37 +00007572 if (Cond.getOpcode() == ISD::SETCC) {
7573 SDValue NewCond = LowerSETCC(Cond, DAG);
7574 if (NewCond.getNode())
7575 Cond = NewCond;
7576 }
Evan Cheng734503b2006-09-11 02:19:56 +00007577
Chris Lattnera2b56002010-12-05 01:23:24 +00007578 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007579 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007580 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007581 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007582 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007583 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7584 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007585 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007586
Chris Lattnera2b56002010-12-05 01:23:24 +00007587 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007588
7589 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007590 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7591 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007592
7593 SDValue CmpOp0 = Cmp.getOperand(0);
7594 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7595 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007596
Chris Lattner96908b12010-12-05 02:00:51 +00007597 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007598 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7599 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007600
Chris Lattner96908b12010-12-05 02:00:51 +00007601 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7602 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007603
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007604 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007605 if (N2C == 0 || !N2C->isNullValue())
7606 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7607 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007608 }
7609 }
7610
Chris Lattnera2b56002010-12-05 01:23:24 +00007611 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007612 if (Cond.getOpcode() == ISD::AND &&
7613 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7614 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007615 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007616 Cond = Cond.getOperand(0);
7617 }
7618
Evan Cheng3f41d662007-10-08 22:16:29 +00007619 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7620 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007621 if (Cond.getOpcode() == X86ISD::SETCC ||
7622 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007623 CC = Cond.getOperand(0);
7624
Dan Gohman475871a2008-07-27 21:46:04 +00007625 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007626 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007627 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007628
Evan Cheng3f41d662007-10-08 22:16:29 +00007629 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007630 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007631 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007632 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007633
Chris Lattnerd1980a52009-03-12 06:52:53 +00007634 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7635 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007636 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007637 addTest = false;
7638 }
7639 }
7640
7641 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007642 // Look pass the truncate.
7643 if (Cond.getOpcode() == ISD::TRUNCATE)
7644 Cond = Cond.getOperand(0);
7645
7646 // We know the result of AND is compared against zero. Try to match
7647 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007648 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007649 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007650 if (NewSetCC.getNode()) {
7651 CC = NewSetCC.getOperand(0);
7652 Cond = NewSetCC.getOperand(1);
7653 addTest = false;
7654 }
7655 }
7656 }
7657
7658 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007659 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007660 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007661 }
7662
Benjamin Kramere915ff32010-12-22 23:09:28 +00007663 // a < b ? -1 : 0 -> RES = ~setcc_carry
7664 // a < b ? 0 : -1 -> RES = setcc_carry
7665 // a >= b ? -1 : 0 -> RES = setcc_carry
7666 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7667 if (Cond.getOpcode() == X86ISD::CMP) {
7668 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7669
7670 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7671 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7672 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7673 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7674 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7675 return DAG.getNOT(DL, Res, Res.getValueType());
7676 return Res;
7677 }
7678 }
7679
Evan Cheng0488db92007-09-25 01:57:46 +00007680 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7681 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007682 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007683 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007684 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007685}
7686
Evan Cheng370e5342008-12-03 08:38:43 +00007687// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7688// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7689// from the AND / OR.
7690static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7691 Opc = Op.getOpcode();
7692 if (Opc != ISD::OR && Opc != ISD::AND)
7693 return false;
7694 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7695 Op.getOperand(0).hasOneUse() &&
7696 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7697 Op.getOperand(1).hasOneUse());
7698}
7699
Evan Cheng961d6d42009-02-02 08:19:07 +00007700// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7701// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007702static bool isXor1OfSetCC(SDValue Op) {
7703 if (Op.getOpcode() != ISD::XOR)
7704 return false;
7705 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7706 if (N1C && N1C->getAPIntValue() == 1) {
7707 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7708 Op.getOperand(0).hasOneUse();
7709 }
7710 return false;
7711}
7712
Dan Gohmand858e902010-04-17 15:26:15 +00007713SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007714 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007715 SDValue Chain = Op.getOperand(0);
7716 SDValue Cond = Op.getOperand(1);
7717 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007718 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007719 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007720
Dan Gohman1a492952009-10-20 16:22:37 +00007721 if (Cond.getOpcode() == ISD::SETCC) {
7722 SDValue NewCond = LowerSETCC(Cond, DAG);
7723 if (NewCond.getNode())
7724 Cond = NewCond;
7725 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007726#if 0
7727 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007728 else if (Cond.getOpcode() == X86ISD::ADD ||
7729 Cond.getOpcode() == X86ISD::SUB ||
7730 Cond.getOpcode() == X86ISD::SMUL ||
7731 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007732 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007733#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007734
Evan Chengad9c0a32009-12-15 00:53:42 +00007735 // Look pass (and (setcc_carry (cmp ...)), 1).
7736 if (Cond.getOpcode() == ISD::AND &&
7737 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7738 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007739 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007740 Cond = Cond.getOperand(0);
7741 }
7742
Evan Cheng3f41d662007-10-08 22:16:29 +00007743 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7744 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007745 if (Cond.getOpcode() == X86ISD::SETCC ||
7746 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007747 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007748
Dan Gohman475871a2008-07-27 21:46:04 +00007749 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007750 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007751 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007752 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007753 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007754 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007755 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007756 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007757 default: break;
7758 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007759 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007760 // These can only come from an arithmetic instruction with overflow,
7761 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007762 Cond = Cond.getNode()->getOperand(1);
7763 addTest = false;
7764 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007765 }
Evan Cheng0488db92007-09-25 01:57:46 +00007766 }
Evan Cheng370e5342008-12-03 08:38:43 +00007767 } else {
7768 unsigned CondOpc;
7769 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7770 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007771 if (CondOpc == ISD::OR) {
7772 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7773 // two branches instead of an explicit OR instruction with a
7774 // separate test.
7775 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007776 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007777 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007778 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007779 Chain, Dest, CC, Cmp);
7780 CC = Cond.getOperand(1).getOperand(0);
7781 Cond = Cmp;
7782 addTest = false;
7783 }
7784 } else { // ISD::AND
7785 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7786 // two branches instead of an explicit AND instruction with a
7787 // separate test. However, we only do this if this block doesn't
7788 // have a fall-through edge, because this requires an explicit
7789 // jmp when the condition is false.
7790 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007791 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007792 Op.getNode()->hasOneUse()) {
7793 X86::CondCode CCode =
7794 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7795 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007797 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007798 // Look for an unconditional branch following this conditional branch.
7799 // We need this because we need to reverse the successors in order
7800 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007801 if (User->getOpcode() == ISD::BR) {
7802 SDValue FalseBB = User->getOperand(1);
7803 SDNode *NewBR =
7804 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007805 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007806 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007807 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007808
Dale Johannesene4d209d2009-02-03 20:21:25 +00007809 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007810 Chain, Dest, CC, Cmp);
7811 X86::CondCode CCode =
7812 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7813 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007814 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007815 Cond = Cmp;
7816 addTest = false;
7817 }
7818 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007819 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007820 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7821 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7822 // It should be transformed during dag combiner except when the condition
7823 // is set by a arithmetics with overflow node.
7824 X86::CondCode CCode =
7825 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7826 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007827 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007828 Cond = Cond.getOperand(0).getOperand(1);
7829 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007830 }
Evan Cheng0488db92007-09-25 01:57:46 +00007831 }
7832
7833 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007834 // Look pass the truncate.
7835 if (Cond.getOpcode() == ISD::TRUNCATE)
7836 Cond = Cond.getOperand(0);
7837
7838 // We know the result of AND is compared against zero. Try to match
7839 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007840 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007841 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7842 if (NewSetCC.getNode()) {
7843 CC = NewSetCC.getOperand(0);
7844 Cond = NewSetCC.getOperand(1);
7845 addTest = false;
7846 }
7847 }
7848 }
7849
7850 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007851 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007852 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007853 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007854 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007855 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007856}
7857
Anton Korobeynikove060b532007-04-17 19:34:00 +00007858
7859// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7860// Calls to _alloca is needed to probe the stack when allocating more than 4k
7861// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7862// that the guard pages used by the OS virtual memory manager are allocated in
7863// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007864SDValue
7865X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007866 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007867 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007868 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007869 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007870
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007871 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007872 SDValue Chain = Op.getOperand(0);
7873 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007874 // FIXME: Ensure alignment here
7875
Dan Gohman475871a2008-07-27 21:46:04 +00007876 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007877
Owen Anderson825b72b2009-08-11 20:47:22 +00007878 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007879
Dale Johannesendd64c412009-02-04 00:33:20 +00007880 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007881 Flag = Chain.getValue(1);
7882
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007883 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007884
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007885 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007886 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007887
Dale Johannesendd64c412009-02-04 00:33:20 +00007888 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007889
Dan Gohman475871a2008-07-27 21:46:04 +00007890 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007891 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007892}
7893
Dan Gohmand858e902010-04-17 15:26:15 +00007894SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007895 MachineFunction &MF = DAG.getMachineFunction();
7896 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7897
Dan Gohman69de1932008-02-06 22:27:42 +00007898 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007899 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007900
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007901 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007902 // vastart just stores the address of the VarArgsFrameIndex slot into the
7903 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007904 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7905 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007906 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7907 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007908 }
7909
7910 // __va_list_tag:
7911 // gp_offset (0 - 6 * 8)
7912 // fp_offset (48 - 48 + 8 * 16)
7913 // overflow_arg_area (point to parameters coming in memory).
7914 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007915 SmallVector<SDValue, 8> MemOps;
7916 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007917 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007918 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007919 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7920 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007921 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007922 MemOps.push_back(Store);
7923
7924 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007925 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007926 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007927 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007928 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7929 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007930 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007931 MemOps.push_back(Store);
7932
7933 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007934 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007935 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007936 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7937 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007938 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7939 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007940 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007941 MemOps.push_back(Store);
7942
7943 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007944 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007945 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007946 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7947 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007948 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7949 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007950 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007951 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007952 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007953}
7954
Dan Gohmand858e902010-04-17 15:26:15 +00007955SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007956 assert(Subtarget->is64Bit() &&
7957 "LowerVAARG only handles 64-bit va_arg!");
7958 assert((Subtarget->isTargetLinux() ||
7959 Subtarget->isTargetDarwin()) &&
7960 "Unhandled target in LowerVAARG");
7961 assert(Op.getNode()->getNumOperands() == 4);
7962 SDValue Chain = Op.getOperand(0);
7963 SDValue SrcPtr = Op.getOperand(1);
7964 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7965 unsigned Align = Op.getConstantOperandVal(3);
7966 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007967
Dan Gohman320afb82010-10-12 18:00:49 +00007968 EVT ArgVT = Op.getNode()->getValueType(0);
7969 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7970 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7971 uint8_t ArgMode;
7972
7973 // Decide which area this value should be read from.
7974 // TODO: Implement the AMD64 ABI in its entirety. This simple
7975 // selection mechanism works only for the basic types.
7976 if (ArgVT == MVT::f80) {
7977 llvm_unreachable("va_arg for f80 not yet implemented");
7978 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7979 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7980 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7981 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7982 } else {
7983 llvm_unreachable("Unhandled argument type in LowerVAARG");
7984 }
7985
7986 if (ArgMode == 2) {
7987 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007988 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007989 !(DAG.getMachineFunction()
7990 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007991 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007992 }
7993
7994 // Insert VAARG_64 node into the DAG
7995 // VAARG_64 returns two values: Variable Argument Address, Chain
7996 SmallVector<SDValue, 11> InstOps;
7997 InstOps.push_back(Chain);
7998 InstOps.push_back(SrcPtr);
7999 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8000 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8001 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8002 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8003 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8004 VTs, &InstOps[0], InstOps.size(),
8005 MVT::i64,
8006 MachinePointerInfo(SV),
8007 /*Align=*/0,
8008 /*Volatile=*/false,
8009 /*ReadMem=*/true,
8010 /*WriteMem=*/true);
8011 Chain = VAARG.getValue(1);
8012
8013 // Load the next argument and return it
8014 return DAG.getLoad(ArgVT, dl,
8015 Chain,
8016 VAARG,
8017 MachinePointerInfo(),
8018 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008019}
8020
Dan Gohmand858e902010-04-17 15:26:15 +00008021SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008022 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008023 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008024 SDValue Chain = Op.getOperand(0);
8025 SDValue DstPtr = Op.getOperand(1);
8026 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008027 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8028 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008029 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008030
Chris Lattnere72f2022010-09-21 05:40:29 +00008031 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008032 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008033 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008034 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008035}
8036
Dan Gohman475871a2008-07-27 21:46:04 +00008037SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008038X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008039 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008040 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008041 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008042 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008043 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008044 case Intrinsic::x86_sse_comieq_ss:
8045 case Intrinsic::x86_sse_comilt_ss:
8046 case Intrinsic::x86_sse_comile_ss:
8047 case Intrinsic::x86_sse_comigt_ss:
8048 case Intrinsic::x86_sse_comige_ss:
8049 case Intrinsic::x86_sse_comineq_ss:
8050 case Intrinsic::x86_sse_ucomieq_ss:
8051 case Intrinsic::x86_sse_ucomilt_ss:
8052 case Intrinsic::x86_sse_ucomile_ss:
8053 case Intrinsic::x86_sse_ucomigt_ss:
8054 case Intrinsic::x86_sse_ucomige_ss:
8055 case Intrinsic::x86_sse_ucomineq_ss:
8056 case Intrinsic::x86_sse2_comieq_sd:
8057 case Intrinsic::x86_sse2_comilt_sd:
8058 case Intrinsic::x86_sse2_comile_sd:
8059 case Intrinsic::x86_sse2_comigt_sd:
8060 case Intrinsic::x86_sse2_comige_sd:
8061 case Intrinsic::x86_sse2_comineq_sd:
8062 case Intrinsic::x86_sse2_ucomieq_sd:
8063 case Intrinsic::x86_sse2_ucomilt_sd:
8064 case Intrinsic::x86_sse2_ucomile_sd:
8065 case Intrinsic::x86_sse2_ucomigt_sd:
8066 case Intrinsic::x86_sse2_ucomige_sd:
8067 case Intrinsic::x86_sse2_ucomineq_sd: {
8068 unsigned Opc = 0;
8069 ISD::CondCode CC = ISD::SETCC_INVALID;
8070 switch (IntNo) {
8071 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008072 case Intrinsic::x86_sse_comieq_ss:
8073 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008074 Opc = X86ISD::COMI;
8075 CC = ISD::SETEQ;
8076 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008077 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008078 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008079 Opc = X86ISD::COMI;
8080 CC = ISD::SETLT;
8081 break;
8082 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008083 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008084 Opc = X86ISD::COMI;
8085 CC = ISD::SETLE;
8086 break;
8087 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008088 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008089 Opc = X86ISD::COMI;
8090 CC = ISD::SETGT;
8091 break;
8092 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008093 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008094 Opc = X86ISD::COMI;
8095 CC = ISD::SETGE;
8096 break;
8097 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008098 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008099 Opc = X86ISD::COMI;
8100 CC = ISD::SETNE;
8101 break;
8102 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008103 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008104 Opc = X86ISD::UCOMI;
8105 CC = ISD::SETEQ;
8106 break;
8107 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008108 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008109 Opc = X86ISD::UCOMI;
8110 CC = ISD::SETLT;
8111 break;
8112 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008113 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008114 Opc = X86ISD::UCOMI;
8115 CC = ISD::SETLE;
8116 break;
8117 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008118 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008119 Opc = X86ISD::UCOMI;
8120 CC = ISD::SETGT;
8121 break;
8122 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008123 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008124 Opc = X86ISD::UCOMI;
8125 CC = ISD::SETGE;
8126 break;
8127 case Intrinsic::x86_sse_ucomineq_ss:
8128 case Intrinsic::x86_sse2_ucomineq_sd:
8129 Opc = X86ISD::UCOMI;
8130 CC = ISD::SETNE;
8131 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008132 }
Evan Cheng734503b2006-09-11 02:19:56 +00008133
Dan Gohman475871a2008-07-27 21:46:04 +00008134 SDValue LHS = Op.getOperand(1);
8135 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008136 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008137 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008138 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8139 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8140 DAG.getConstant(X86CC, MVT::i8), Cond);
8141 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008142 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008143 // ptest and testp intrinsics. The intrinsic these come from are designed to
8144 // return an integer value, not just an instruction so lower it to the ptest
8145 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008146 case Intrinsic::x86_sse41_ptestz:
8147 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008148 case Intrinsic::x86_sse41_ptestnzc:
8149 case Intrinsic::x86_avx_ptestz_256:
8150 case Intrinsic::x86_avx_ptestc_256:
8151 case Intrinsic::x86_avx_ptestnzc_256:
8152 case Intrinsic::x86_avx_vtestz_ps:
8153 case Intrinsic::x86_avx_vtestc_ps:
8154 case Intrinsic::x86_avx_vtestnzc_ps:
8155 case Intrinsic::x86_avx_vtestz_pd:
8156 case Intrinsic::x86_avx_vtestc_pd:
8157 case Intrinsic::x86_avx_vtestnzc_pd:
8158 case Intrinsic::x86_avx_vtestz_ps_256:
8159 case Intrinsic::x86_avx_vtestc_ps_256:
8160 case Intrinsic::x86_avx_vtestnzc_ps_256:
8161 case Intrinsic::x86_avx_vtestz_pd_256:
8162 case Intrinsic::x86_avx_vtestc_pd_256:
8163 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8164 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008165 unsigned X86CC = 0;
8166 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008167 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008168 case Intrinsic::x86_avx_vtestz_ps:
8169 case Intrinsic::x86_avx_vtestz_pd:
8170 case Intrinsic::x86_avx_vtestz_ps_256:
8171 case Intrinsic::x86_avx_vtestz_pd_256:
8172 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008173 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008174 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008175 // ZF = 1
8176 X86CC = X86::COND_E;
8177 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008178 case Intrinsic::x86_avx_vtestc_ps:
8179 case Intrinsic::x86_avx_vtestc_pd:
8180 case Intrinsic::x86_avx_vtestc_ps_256:
8181 case Intrinsic::x86_avx_vtestc_pd_256:
8182 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008183 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008184 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008185 // CF = 1
8186 X86CC = X86::COND_B;
8187 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008188 case Intrinsic::x86_avx_vtestnzc_ps:
8189 case Intrinsic::x86_avx_vtestnzc_pd:
8190 case Intrinsic::x86_avx_vtestnzc_ps_256:
8191 case Intrinsic::x86_avx_vtestnzc_pd_256:
8192 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008193 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008194 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008195 // ZF and CF = 0
8196 X86CC = X86::COND_A;
8197 break;
8198 }
Eric Christopherfd179292009-08-27 18:07:15 +00008199
Eric Christopher71c67532009-07-29 00:28:05 +00008200 SDValue LHS = Op.getOperand(1);
8201 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008202 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8203 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008204 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8205 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8206 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008207 }
Evan Cheng5759f972008-05-04 09:15:50 +00008208
8209 // Fix vector shift instructions where the last operand is a non-immediate
8210 // i32 value.
8211 case Intrinsic::x86_sse2_pslli_w:
8212 case Intrinsic::x86_sse2_pslli_d:
8213 case Intrinsic::x86_sse2_pslli_q:
8214 case Intrinsic::x86_sse2_psrli_w:
8215 case Intrinsic::x86_sse2_psrli_d:
8216 case Intrinsic::x86_sse2_psrli_q:
8217 case Intrinsic::x86_sse2_psrai_w:
8218 case Intrinsic::x86_sse2_psrai_d:
8219 case Intrinsic::x86_mmx_pslli_w:
8220 case Intrinsic::x86_mmx_pslli_d:
8221 case Intrinsic::x86_mmx_pslli_q:
8222 case Intrinsic::x86_mmx_psrli_w:
8223 case Intrinsic::x86_mmx_psrli_d:
8224 case Intrinsic::x86_mmx_psrli_q:
8225 case Intrinsic::x86_mmx_psrai_w:
8226 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008227 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008228 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008229 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008230
8231 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008232 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008233 switch (IntNo) {
8234 case Intrinsic::x86_sse2_pslli_w:
8235 NewIntNo = Intrinsic::x86_sse2_psll_w;
8236 break;
8237 case Intrinsic::x86_sse2_pslli_d:
8238 NewIntNo = Intrinsic::x86_sse2_psll_d;
8239 break;
8240 case Intrinsic::x86_sse2_pslli_q:
8241 NewIntNo = Intrinsic::x86_sse2_psll_q;
8242 break;
8243 case Intrinsic::x86_sse2_psrli_w:
8244 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8245 break;
8246 case Intrinsic::x86_sse2_psrli_d:
8247 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8248 break;
8249 case Intrinsic::x86_sse2_psrli_q:
8250 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8251 break;
8252 case Intrinsic::x86_sse2_psrai_w:
8253 NewIntNo = Intrinsic::x86_sse2_psra_w;
8254 break;
8255 case Intrinsic::x86_sse2_psrai_d:
8256 NewIntNo = Intrinsic::x86_sse2_psra_d;
8257 break;
8258 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008259 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008260 switch (IntNo) {
8261 case Intrinsic::x86_mmx_pslli_w:
8262 NewIntNo = Intrinsic::x86_mmx_psll_w;
8263 break;
8264 case Intrinsic::x86_mmx_pslli_d:
8265 NewIntNo = Intrinsic::x86_mmx_psll_d;
8266 break;
8267 case Intrinsic::x86_mmx_pslli_q:
8268 NewIntNo = Intrinsic::x86_mmx_psll_q;
8269 break;
8270 case Intrinsic::x86_mmx_psrli_w:
8271 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8272 break;
8273 case Intrinsic::x86_mmx_psrli_d:
8274 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8275 break;
8276 case Intrinsic::x86_mmx_psrli_q:
8277 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8278 break;
8279 case Intrinsic::x86_mmx_psrai_w:
8280 NewIntNo = Intrinsic::x86_mmx_psra_w;
8281 break;
8282 case Intrinsic::x86_mmx_psrai_d:
8283 NewIntNo = Intrinsic::x86_mmx_psra_d;
8284 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008285 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008286 }
8287 break;
8288 }
8289 }
Mon P Wangefa42202009-09-03 19:56:25 +00008290
8291 // The vector shift intrinsics with scalars uses 32b shift amounts but
8292 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8293 // to be zero.
8294 SDValue ShOps[4];
8295 ShOps[0] = ShAmt;
8296 ShOps[1] = DAG.getConstant(0, MVT::i32);
8297 if (ShAmtVT == MVT::v4i32) {
8298 ShOps[2] = DAG.getUNDEF(MVT::i32);
8299 ShOps[3] = DAG.getUNDEF(MVT::i32);
8300 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8301 } else {
8302 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008303// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008304 }
8305
Owen Andersone50ed302009-08-10 22:56:29 +00008306 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008307 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008308 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008309 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008310 Op.getOperand(1), ShAmt);
8311 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008312 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008313}
Evan Cheng72261582005-12-20 06:22:03 +00008314
Dan Gohmand858e902010-04-17 15:26:15 +00008315SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8316 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008317 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8318 MFI->setReturnAddressIsTaken(true);
8319
Bill Wendling64e87322009-01-16 19:25:27 +00008320 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008321 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008322
8323 if (Depth > 0) {
8324 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8325 SDValue Offset =
8326 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008327 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008328 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008329 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008330 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008331 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008332 }
8333
8334 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008335 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008336 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008337 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008338}
8339
Dan Gohmand858e902010-04-17 15:26:15 +00008340SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008341 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8342 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008343
Owen Andersone50ed302009-08-10 22:56:29 +00008344 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008345 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008346 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8347 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008348 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008349 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008350 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8351 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008352 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008353 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008354}
8355
Dan Gohman475871a2008-07-27 21:46:04 +00008356SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008357 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008358 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008359}
8360
Dan Gohmand858e902010-04-17 15:26:15 +00008361SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008362 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008363 SDValue Chain = Op.getOperand(0);
8364 SDValue Offset = Op.getOperand(1);
8365 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008366 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008367
Dan Gohmand8816272010-08-11 18:14:00 +00008368 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8369 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8370 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008371 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008372
Dan Gohmand8816272010-08-11 18:14:00 +00008373 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8374 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008375 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008376 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8377 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008378 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008379 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008380
Dale Johannesene4d209d2009-02-03 20:21:25 +00008381 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008382 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008383 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008384}
8385
Dan Gohman475871a2008-07-27 21:46:04 +00008386SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008387 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008388 SDValue Root = Op.getOperand(0);
8389 SDValue Trmp = Op.getOperand(1); // trampoline
8390 SDValue FPtr = Op.getOperand(2); // nested function
8391 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008392 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008393
Dan Gohman69de1932008-02-06 22:27:42 +00008394 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008395
8396 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008397 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008398
8399 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008400 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8401 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008402
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008403 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8404 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008405
8406 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8407
8408 // Load the pointer to the nested function into R11.
8409 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008410 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008411 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008412 Addr, MachinePointerInfo(TrmpAddr),
8413 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008414
Owen Anderson825b72b2009-08-11 20:47:22 +00008415 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8416 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008417 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8418 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008419 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008420
8421 // Load the 'nest' parameter value into R10.
8422 // R10 is specified in X86CallingConv.td
8423 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008424 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8425 DAG.getConstant(10, MVT::i64));
8426 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008427 Addr, MachinePointerInfo(TrmpAddr, 10),
8428 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008429
Owen Anderson825b72b2009-08-11 20:47:22 +00008430 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8431 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008432 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8433 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008434 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008435
8436 // Jump to the nested function.
8437 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008438 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8439 DAG.getConstant(20, MVT::i64));
8440 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008441 Addr, MachinePointerInfo(TrmpAddr, 20),
8442 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008443
8444 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008445 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8446 DAG.getConstant(22, MVT::i64));
8447 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008448 MachinePointerInfo(TrmpAddr, 22),
8449 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008450
Dan Gohman475871a2008-07-27 21:46:04 +00008451 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008452 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008453 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008454 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008455 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008456 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008457 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008458 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008459
8460 switch (CC) {
8461 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008462 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008463 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008464 case CallingConv::X86_StdCall: {
8465 // Pass 'nest' parameter in ECX.
8466 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008467 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008468
8469 // Check that ECX wasn't needed by an 'inreg' parameter.
8470 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008471 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008472
Chris Lattner58d74912008-03-12 17:45:29 +00008473 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008474 unsigned InRegCount = 0;
8475 unsigned Idx = 1;
8476
8477 for (FunctionType::param_iterator I = FTy->param_begin(),
8478 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008479 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008480 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008481 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008482
8483 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008484 report_fatal_error("Nest register in use - reduce number of inreg"
8485 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008486 }
8487 }
8488 break;
8489 }
8490 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008491 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008492 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008493 // Pass 'nest' parameter in EAX.
8494 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008495 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008496 break;
8497 }
8498
Dan Gohman475871a2008-07-27 21:46:04 +00008499 SDValue OutChains[4];
8500 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008501
Owen Anderson825b72b2009-08-11 20:47:22 +00008502 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8503 DAG.getConstant(10, MVT::i32));
8504 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008505
Chris Lattnera62fe662010-02-05 19:20:30 +00008506 // This is storing the opcode for MOV32ri.
8507 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008508 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008509 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008510 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008511 Trmp, MachinePointerInfo(TrmpAddr),
8512 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008513
Owen Anderson825b72b2009-08-11 20:47:22 +00008514 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8515 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008516 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8517 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008518 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008519
Chris Lattnera62fe662010-02-05 19:20:30 +00008520 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008521 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8522 DAG.getConstant(5, MVT::i32));
8523 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008524 MachinePointerInfo(TrmpAddr, 5),
8525 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008526
Owen Anderson825b72b2009-08-11 20:47:22 +00008527 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8528 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008529 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8530 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008531 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008532
Dan Gohman475871a2008-07-27 21:46:04 +00008533 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008534 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008535 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008536 }
8537}
8538
Dan Gohmand858e902010-04-17 15:26:15 +00008539SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8540 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008541 /*
8542 The rounding mode is in bits 11:10 of FPSR, and has the following
8543 settings:
8544 00 Round to nearest
8545 01 Round to -inf
8546 10 Round to +inf
8547 11 Round to 0
8548
8549 FLT_ROUNDS, on the other hand, expects the following:
8550 -1 Undefined
8551 0 Round to 0
8552 1 Round to nearest
8553 2 Round to +inf
8554 3 Round to -inf
8555
8556 To perform the conversion, we do:
8557 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8558 */
8559
8560 MachineFunction &MF = DAG.getMachineFunction();
8561 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008562 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008563 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008564 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008565 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008566
8567 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008568 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008569 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008570
Michael J. Spencerec38de22010-10-10 22:04:20 +00008571
Chris Lattner2156b792010-09-22 01:11:26 +00008572 MachineMemOperand *MMO =
8573 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8574 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008575
Chris Lattner2156b792010-09-22 01:11:26 +00008576 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8577 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8578 DAG.getVTList(MVT::Other),
8579 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008580
8581 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008582 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008583 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008584
8585 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008586 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008587 DAG.getNode(ISD::SRL, DL, MVT::i16,
8588 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008589 CWD, DAG.getConstant(0x800, MVT::i16)),
8590 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008591 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008592 DAG.getNode(ISD::SRL, DL, MVT::i16,
8593 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008594 CWD, DAG.getConstant(0x400, MVT::i16)),
8595 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008596
Dan Gohman475871a2008-07-27 21:46:04 +00008597 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008598 DAG.getNode(ISD::AND, DL, MVT::i16,
8599 DAG.getNode(ISD::ADD, DL, MVT::i16,
8600 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008601 DAG.getConstant(1, MVT::i16)),
8602 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008603
8604
Duncan Sands83ec4b62008-06-06 12:08:01 +00008605 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008606 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008607}
8608
Dan Gohmand858e902010-04-17 15:26:15 +00008609SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008610 EVT VT = Op.getValueType();
8611 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008612 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008613 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008614
8615 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008616 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008617 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008619 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008620 }
Evan Cheng18efe262007-12-14 02:13:44 +00008621
Evan Cheng152804e2007-12-14 08:30:15 +00008622 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008624 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008625
8626 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008627 SDValue Ops[] = {
8628 Op,
8629 DAG.getConstant(NumBits+NumBits-1, OpVT),
8630 DAG.getConstant(X86::COND_E, MVT::i8),
8631 Op.getValue(1)
8632 };
8633 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008634
8635 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008636 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008637
Owen Anderson825b72b2009-08-11 20:47:22 +00008638 if (VT == MVT::i8)
8639 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008640 return Op;
8641}
8642
Dan Gohmand858e902010-04-17 15:26:15 +00008643SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008644 EVT VT = Op.getValueType();
8645 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008646 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008647 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008648
8649 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008650 if (VT == MVT::i8) {
8651 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008652 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008653 }
Evan Cheng152804e2007-12-14 08:30:15 +00008654
8655 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008656 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008657 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008658
8659 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008660 SDValue Ops[] = {
8661 Op,
8662 DAG.getConstant(NumBits, OpVT),
8663 DAG.getConstant(X86::COND_E, MVT::i8),
8664 Op.getValue(1)
8665 };
8666 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008667
Owen Anderson825b72b2009-08-11 20:47:22 +00008668 if (VT == MVT::i8)
8669 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008670 return Op;
8671}
8672
Dan Gohmand858e902010-04-17 15:26:15 +00008673SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008674 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008675 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008676 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008677
Mon P Wangaf9b9522008-12-18 21:42:19 +00008678 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8679 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8680 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8681 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8682 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8683 //
8684 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8685 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8686 // return AloBlo + AloBhi + AhiBlo;
8687
8688 SDValue A = Op.getOperand(0);
8689 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008690
Dale Johannesene4d209d2009-02-03 20:21:25 +00008691 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8693 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008694 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8696 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008697 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008698 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008699 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008700 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008701 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008702 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008703 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008705 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008706 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008707 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8708 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008709 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8711 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008712 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8713 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008714 return Res;
8715}
8716
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008717SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8718 EVT VT = Op.getValueType();
8719 DebugLoc dl = Op.getDebugLoc();
8720 SDValue R = Op.getOperand(0);
8721
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008722 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008723
Nate Begeman51409212010-07-28 00:21:48 +00008724 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8725
8726 if (VT == MVT::v4i32) {
8727 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8728 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8729 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8730
8731 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008732
Nate Begeman51409212010-07-28 00:21:48 +00008733 std::vector<Constant*> CV(4, CI);
8734 Constant *C = ConstantVector::get(CV);
8735 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8736 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008737 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008738 false, false, 16);
8739
8740 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008741 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008742 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8743 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8744 }
8745 if (VT == MVT::v16i8) {
8746 // a = a << 5;
8747 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8748 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8749 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8750
8751 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8752 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8753
8754 std::vector<Constant*> CVM1(16, CM1);
8755 std::vector<Constant*> CVM2(16, CM2);
8756 Constant *C = ConstantVector::get(CVM1);
8757 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8758 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008759 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008760 false, false, 16);
8761
8762 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8763 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8764 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8765 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8766 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008767 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008768 // a += a
8769 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008770
Nate Begeman51409212010-07-28 00:21:48 +00008771 C = ConstantVector::get(CVM2);
8772 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8773 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008774 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008775 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008776
Nate Begeman51409212010-07-28 00:21:48 +00008777 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8778 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8779 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8780 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8781 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008782 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008783 // a += a
8784 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008785
Nate Begeman51409212010-07-28 00:21:48 +00008786 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008787 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008788 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8789 return R;
8790 }
8791 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008792}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008793
Dan Gohmand858e902010-04-17 15:26:15 +00008794SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008795 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8796 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008797 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8798 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008799 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008800 SDValue LHS = N->getOperand(0);
8801 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008802 unsigned BaseOp = 0;
8803 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008804 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008805 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008806 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008807 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008808 // A subtract of one will be selected as a INC. Note that INC doesn't
8809 // set CF, so we can't do this for UADDO.
8810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8811 if (C->getAPIntValue() == 1) {
8812 BaseOp = X86ISD::INC;
8813 Cond = X86::COND_O;
8814 break;
8815 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008816 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008817 Cond = X86::COND_O;
8818 break;
8819 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008820 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008821 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008822 break;
8823 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008824 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8825 // set CF, so we can't do this for USUBO.
8826 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8827 if (C->getAPIntValue() == 1) {
8828 BaseOp = X86ISD::DEC;
8829 Cond = X86::COND_O;
8830 break;
8831 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008832 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008833 Cond = X86::COND_O;
8834 break;
8835 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008836 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008837 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008838 break;
8839 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008840 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008841 Cond = X86::COND_O;
8842 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008843 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8844 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8845 MVT::i32);
8846 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008847
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008848 SDValue SetCC =
8849 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8850 DAG.getConstant(X86::COND_O, MVT::i32),
8851 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008852
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008853 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8854 return Sum;
8855 }
Bill Wendling74c37652008-12-09 22:08:41 +00008856 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008857
Bill Wendling61edeb52008-12-02 01:06:39 +00008858 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008860 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008861
Bill Wendling61edeb52008-12-02 01:06:39 +00008862 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008863 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8864 DAG.getConstant(Cond, MVT::i32),
8865 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008866
Bill Wendling61edeb52008-12-02 01:06:39 +00008867 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8868 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008869}
8870
Eric Christopher9a9d2752010-07-22 02:48:34 +00008871SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8872 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008873
Eric Christopherb6729dc2010-08-04 23:03:04 +00008874 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008875 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008876 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008877 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008878 SDValue Ops[] = {
8879 DAG.getRegister(X86::ESP, MVT::i32), // Base
8880 DAG.getTargetConstant(1, MVT::i8), // Scale
8881 DAG.getRegister(0, MVT::i32), // Index
8882 DAG.getTargetConstant(0, MVT::i32), // Disp
8883 DAG.getRegister(0, MVT::i32), // Segment.
8884 Zero,
8885 Chain
8886 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008887 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008888 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8889 array_lengthof(Ops));
8890 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008891 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008892
Eric Christopher9a9d2752010-07-22 02:48:34 +00008893 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008894 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008895 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008896
Chris Lattner132929a2010-08-14 17:26:09 +00008897 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8898 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8899 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8900 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008901
Chris Lattner132929a2010-08-14 17:26:09 +00008902 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8903 if (!Op1 && !Op2 && !Op3 && Op4)
8904 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008905
Chris Lattner132929a2010-08-14 17:26:09 +00008906 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8907 if (Op1 && !Op2 && !Op3 && !Op4)
8908 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008909
8910 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008911 // (MFENCE)>;
8912 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008913}
8914
Dan Gohmand858e902010-04-17 15:26:15 +00008915SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008916 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008917 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008918 unsigned Reg = 0;
8919 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008920 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008921 default:
8922 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008923 case MVT::i8: Reg = X86::AL; size = 1; break;
8924 case MVT::i16: Reg = X86::AX; size = 2; break;
8925 case MVT::i32: Reg = X86::EAX; size = 4; break;
8926 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008927 assert(Subtarget->is64Bit() && "Node not type legal!");
8928 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008929 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008930 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008931 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008932 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008933 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008934 Op.getOperand(1),
8935 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008936 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008937 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008938 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008939 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8940 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8941 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008942 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008943 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008944 return cpOut;
8945}
8946
Duncan Sands1607f052008-12-01 11:39:25 +00008947SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008948 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008949 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008950 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008951 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008952 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008953 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008954 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8955 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008956 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008957 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8958 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008959 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008960 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008961 rdx.getValue(1)
8962 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008963 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008964}
8965
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008966SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008967 SelectionDAG &DAG) const {
8968 EVT SrcVT = Op.getOperand(0).getValueType();
8969 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008970 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8971 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008972 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008973 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008974 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008975 // i64 <=> MMX conversions are Legal.
8976 if (SrcVT==MVT::i64 && DstVT.isVector())
8977 return Op;
8978 if (DstVT==MVT::i64 && SrcVT.isVector())
8979 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008980 // MMX <=> MMX conversions are Legal.
8981 if (SrcVT.isVector() && DstVT.isVector())
8982 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008983 // All other conversions need to be expanded.
8984 return SDValue();
8985}
Chris Lattner5b856542010-12-20 00:59:46 +00008986
Dan Gohmand858e902010-04-17 15:26:15 +00008987SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008988 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008989 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008990 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008991 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008992 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008993 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008994 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008995 Node->getOperand(0),
8996 Node->getOperand(1), negOp,
8997 cast<AtomicSDNode>(Node)->getSrcValue(),
8998 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008999}
9000
Chris Lattner5b856542010-12-20 00:59:46 +00009001static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9002 EVT VT = Op.getNode()->getValueType(0);
9003
9004 // Let legalize expand this if it isn't a legal type yet.
9005 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9006 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009007
Chris Lattner5b856542010-12-20 00:59:46 +00009008 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009009
Chris Lattner5b856542010-12-20 00:59:46 +00009010 unsigned Opc;
9011 bool ExtraOp = false;
9012 switch (Op.getOpcode()) {
9013 default: assert(0 && "Invalid code");
9014 case ISD::ADDC: Opc = X86ISD::ADD; break;
9015 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9016 case ISD::SUBC: Opc = X86ISD::SUB; break;
9017 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9018 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009019
Chris Lattner5b856542010-12-20 00:59:46 +00009020 if (!ExtraOp)
9021 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9022 Op.getOperand(1));
9023 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9024 Op.getOperand(1), Op.getOperand(2));
9025}
9026
Evan Cheng0db9fe62006-04-25 20:13:52 +00009027/// LowerOperation - Provide custom lowering hooks for some operations.
9028///
Dan Gohmand858e902010-04-17 15:26:15 +00009029SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009030 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009031 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009032 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009033 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9034 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009035 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009036 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009037 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9038 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9039 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009040 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009041 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009042 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9043 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9044 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009045 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009046 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009047 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009048 case ISD::SHL_PARTS:
9049 case ISD::SRA_PARTS:
9050 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9051 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009052 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009053 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009054 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009055 case ISD::FABS: return LowerFABS(Op, DAG);
9056 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009057 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009058 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009059 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009060 case ISD::SELECT: return LowerSELECT(Op, DAG);
9061 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009062 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009063 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009064 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009065 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009066 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009067 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9068 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009069 case ISD::FRAME_TO_ARGS_OFFSET:
9070 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009071 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009072 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009073 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009074 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009075 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9076 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009077 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009078 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009079 case ISD::SADDO:
9080 case ISD::UADDO:
9081 case ISD::SSUBO:
9082 case ISD::USUBO:
9083 case ISD::SMULO:
9084 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009085 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009086 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009087 case ISD::ADDC:
9088 case ISD::ADDE:
9089 case ISD::SUBC:
9090 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009091 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009092}
9093
Duncan Sands1607f052008-12-01 11:39:25 +00009094void X86TargetLowering::
9095ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009096 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009097 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009098 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009099 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009100
9101 SDValue Chain = Node->getOperand(0);
9102 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009103 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009104 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009105 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009106 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009107 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009108 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009109 SDValue Result =
9110 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9111 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009112 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009113 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009114 Results.push_back(Result.getValue(2));
9115}
9116
Duncan Sands126d9072008-07-04 11:47:58 +00009117/// ReplaceNodeResults - Replace a node with an illegal result type
9118/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009119void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9120 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009121 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009122 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009123 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009124 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009125 assert(false && "Do not know how to custom type legalize this operation!");
9126 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009127 case ISD::ADDC:
9128 case ISD::ADDE:
9129 case ISD::SUBC:
9130 case ISD::SUBE:
9131 // We don't want to expand or promote these.
9132 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009133 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009134 std::pair<SDValue,SDValue> Vals =
9135 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009136 SDValue FIST = Vals.first, StackSlot = Vals.second;
9137 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009138 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009139 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009140 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9141 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009142 }
9143 return;
9144 }
9145 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009146 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009147 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009148 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009149 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009150 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009151 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009152 eax.getValue(2));
9153 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9154 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009155 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009156 Results.push_back(edx.getValue(1));
9157 return;
9158 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009159 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009160 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009162 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009163 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9164 DAG.getConstant(0, MVT::i32));
9165 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9166 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009167 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9168 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009169 cpInL.getValue(1));
9170 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009171 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9172 DAG.getConstant(0, MVT::i32));
9173 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9174 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009175 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009176 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009177 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009178 swapInL.getValue(1));
9179 SDValue Ops[] = { swapInH.getValue(0),
9180 N->getOperand(1),
9181 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009182 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009183 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9184 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9185 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009186 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009187 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009188 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009189 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009190 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009191 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009192 Results.push_back(cpOutH.getValue(1));
9193 return;
9194 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009195 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009196 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9197 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009198 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009199 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9200 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009201 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009202 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9203 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009204 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009205 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9206 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009207 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009208 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9209 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009210 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009211 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9212 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009213 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009214 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9215 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009216 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009217}
9218
Evan Cheng72261582005-12-20 06:22:03 +00009219const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9220 switch (Opcode) {
9221 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009222 case X86ISD::BSF: return "X86ISD::BSF";
9223 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009224 case X86ISD::SHLD: return "X86ISD::SHLD";
9225 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009226 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009227 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009228 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009229 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009230 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009231 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009232 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9233 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9234 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009235 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009236 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009237 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009238 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009239 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009240 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009241 case X86ISD::COMI: return "X86ISD::COMI";
9242 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009243 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009244 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009245 case X86ISD::CMOV: return "X86ISD::CMOV";
9246 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009247 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009248 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9249 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009250 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009251 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009252 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009253 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009254 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009255 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9256 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009257 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009258 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009259 case X86ISD::PANDN: return "X86ISD::PANDN";
9260 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9261 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9262 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009263 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009264 case X86ISD::FMAX: return "X86ISD::FMAX";
9265 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009266 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9267 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009268 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009269 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009270 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009271 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009272 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009273 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9274 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009275 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9276 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9277 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9278 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9279 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9280 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009281 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9282 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009283 case X86ISD::VSHL: return "X86ISD::VSHL";
9284 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009285 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9286 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9287 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9288 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9289 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9290 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9291 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9292 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9293 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9294 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009295 case X86ISD::ADD: return "X86ISD::ADD";
9296 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009297 case X86ISD::ADC: return "X86ISD::ADC";
9298 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009299 case X86ISD::SMUL: return "X86ISD::SMUL";
9300 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009301 case X86ISD::INC: return "X86ISD::INC";
9302 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009303 case X86ISD::OR: return "X86ISD::OR";
9304 case X86ISD::XOR: return "X86ISD::XOR";
9305 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009306 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009307 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009308 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009309 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9310 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9311 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9312 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9313 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9314 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9315 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9316 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9317 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009318 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009319 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009320 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009321 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9322 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009323 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9324 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9325 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9326 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9327 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9328 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9329 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9330 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9331 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009332 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9333 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9334 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9335 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009336 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9337 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9338 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9339 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9340 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9341 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9342 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9343 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9344 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9345 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009346 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009347 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009348 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009349 }
9350}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009351
Chris Lattnerc9addb72007-03-30 23:15:24 +00009352// isLegalAddressingMode - Return true if the addressing mode represented
9353// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009354bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009355 const Type *Ty) const {
9356 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009357 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009358 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009359
Chris Lattnerc9addb72007-03-30 23:15:24 +00009360 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009361 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009362 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009363
Chris Lattnerc9addb72007-03-30 23:15:24 +00009364 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009365 unsigned GVFlags =
9366 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009367
Chris Lattnerdfed4132009-07-10 07:38:24 +00009368 // If a reference to this global requires an extra load, we can't fold it.
9369 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009370 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009371
Chris Lattnerdfed4132009-07-10 07:38:24 +00009372 // If BaseGV requires a register for the PIC base, we cannot also have a
9373 // BaseReg specified.
9374 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009375 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009376
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009377 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009378 if ((M != CodeModel::Small || R != Reloc::Static) &&
9379 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009380 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009381 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009382
Chris Lattnerc9addb72007-03-30 23:15:24 +00009383 switch (AM.Scale) {
9384 case 0:
9385 case 1:
9386 case 2:
9387 case 4:
9388 case 8:
9389 // These scales always work.
9390 break;
9391 case 3:
9392 case 5:
9393 case 9:
9394 // These scales are formed with basereg+scalereg. Only accept if there is
9395 // no basereg yet.
9396 if (AM.HasBaseReg)
9397 return false;
9398 break;
9399 default: // Other stuff never works.
9400 return false;
9401 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009402
Chris Lattnerc9addb72007-03-30 23:15:24 +00009403 return true;
9404}
9405
9406
Evan Cheng2bd122c2007-10-26 01:56:11 +00009407bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009408 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009409 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009410 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9411 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009412 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009413 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009414 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009415}
9416
Owen Andersone50ed302009-08-10 22:56:29 +00009417bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009418 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009419 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009420 unsigned NumBits1 = VT1.getSizeInBits();
9421 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009422 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009423 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009424 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009425}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009426
Dan Gohman97121ba2009-04-08 00:15:30 +00009427bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009428 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009429 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009430}
9431
Owen Andersone50ed302009-08-10 22:56:29 +00009432bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009433 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009434 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009435}
9436
Owen Andersone50ed302009-08-10 22:56:29 +00009437bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009438 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009439 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009440}
9441
Evan Cheng60c07e12006-07-05 22:17:51 +00009442/// isShuffleMaskLegal - Targets can use this to indicate that they only
9443/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9444/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9445/// are assumed to be legal.
9446bool
Eric Christopherfd179292009-08-27 18:07:15 +00009447X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009448 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009449 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009450 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009451 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009452
Nate Begemana09008b2009-10-19 02:17:23 +00009453 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009454 return (VT.getVectorNumElements() == 2 ||
9455 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9456 isMOVLMask(M, VT) ||
9457 isSHUFPMask(M, VT) ||
9458 isPSHUFDMask(M, VT) ||
9459 isPSHUFHWMask(M, VT) ||
9460 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009461 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009462 isUNPCKLMask(M, VT) ||
9463 isUNPCKHMask(M, VT) ||
9464 isUNPCKL_v_undef_Mask(M, VT) ||
9465 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009466}
9467
Dan Gohman7d8143f2008-04-09 20:09:42 +00009468bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009469X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009470 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009471 unsigned NumElts = VT.getVectorNumElements();
9472 // FIXME: This collection of masks seems suspect.
9473 if (NumElts == 2)
9474 return true;
9475 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9476 return (isMOVLMask(Mask, VT) ||
9477 isCommutedMOVLMask(Mask, VT, true) ||
9478 isSHUFPMask(Mask, VT) ||
9479 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009480 }
9481 return false;
9482}
9483
9484//===----------------------------------------------------------------------===//
9485// X86 Scheduler Hooks
9486//===----------------------------------------------------------------------===//
9487
Mon P Wang63307c32008-05-05 19:05:59 +00009488// private utility function
9489MachineBasicBlock *
9490X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9491 MachineBasicBlock *MBB,
9492 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009493 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009494 unsigned LoadOpc,
9495 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009496 unsigned notOpc,
9497 unsigned EAXreg,
9498 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009499 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009500 // For the atomic bitwise operator, we generate
9501 // thisMBB:
9502 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009503 // ld t1 = [bitinstr.addr]
9504 // op t2 = t1, [bitinstr.val]
9505 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009506 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9507 // bz newMBB
9508 // fallthrough -->nextMBB
9509 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9510 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009511 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009512 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009513
Mon P Wang63307c32008-05-05 19:05:59 +00009514 /// First build the CFG
9515 MachineFunction *F = MBB->getParent();
9516 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009517 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9518 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9519 F->insert(MBBIter, newMBB);
9520 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009521
Dan Gohman14152b42010-07-06 20:24:04 +00009522 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9523 nextMBB->splice(nextMBB->begin(), thisMBB,
9524 llvm::next(MachineBasicBlock::iterator(bInstr)),
9525 thisMBB->end());
9526 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009527
Mon P Wang63307c32008-05-05 19:05:59 +00009528 // Update thisMBB to fall through to newMBB
9529 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009530
Mon P Wang63307c32008-05-05 19:05:59 +00009531 // newMBB jumps to itself and fall through to nextMBB
9532 newMBB->addSuccessor(nextMBB);
9533 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009534
Mon P Wang63307c32008-05-05 19:05:59 +00009535 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009536 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009537 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009538 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009539 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009540 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009541 int numArgs = bInstr->getNumOperands() - 1;
9542 for (int i=0; i < numArgs; ++i)
9543 argOpers[i] = &bInstr->getOperand(i+1);
9544
9545 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009546 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009547 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009548
Dale Johannesen140be2d2008-08-19 18:47:28 +00009549 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009550 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009551 for (int i=0; i <= lastAddrIndx; ++i)
9552 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009553
Dale Johannesen140be2d2008-08-19 18:47:28 +00009554 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009555 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009556 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009558 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009559 tt = t1;
9560
Dale Johannesen140be2d2008-08-19 18:47:28 +00009561 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009562 assert((argOpers[valArgIndx]->isReg() ||
9563 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009564 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009565 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009566 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009567 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009568 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009569 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009570 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009571
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009572 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009573 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009574
Dale Johannesene4d209d2009-02-03 20:21:25 +00009575 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009576 for (int i=0; i <= lastAddrIndx; ++i)
9577 (*MIB).addOperand(*argOpers[i]);
9578 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009579 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009580 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9581 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009582
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009583 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009584 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009585
Mon P Wang63307c32008-05-05 19:05:59 +00009586 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009587 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009588
Dan Gohman14152b42010-07-06 20:24:04 +00009589 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009590 return nextMBB;
9591}
9592
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009593// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009594MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009595X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9596 MachineBasicBlock *MBB,
9597 unsigned regOpcL,
9598 unsigned regOpcH,
9599 unsigned immOpcL,
9600 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009601 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009602 // For the atomic bitwise operator, we generate
9603 // thisMBB (instructions are in pairs, except cmpxchg8b)
9604 // ld t1,t2 = [bitinstr.addr]
9605 // newMBB:
9606 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9607 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009608 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009609 // mov ECX, EBX <- t5, t6
9610 // mov EAX, EDX <- t1, t2
9611 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9612 // mov t3, t4 <- EAX, EDX
9613 // bz newMBB
9614 // result in out1, out2
9615 // fallthrough -->nextMBB
9616
9617 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9618 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009619 const unsigned NotOpc = X86::NOT32r;
9620 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9621 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9622 MachineFunction::iterator MBBIter = MBB;
9623 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009624
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009625 /// First build the CFG
9626 MachineFunction *F = MBB->getParent();
9627 MachineBasicBlock *thisMBB = MBB;
9628 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9629 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9630 F->insert(MBBIter, newMBB);
9631 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009632
Dan Gohman14152b42010-07-06 20:24:04 +00009633 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9634 nextMBB->splice(nextMBB->begin(), thisMBB,
9635 llvm::next(MachineBasicBlock::iterator(bInstr)),
9636 thisMBB->end());
9637 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009638
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009639 // Update thisMBB to fall through to newMBB
9640 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009641
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009642 // newMBB jumps to itself and fall through to nextMBB
9643 newMBB->addSuccessor(nextMBB);
9644 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009645
Dale Johannesene4d209d2009-02-03 20:21:25 +00009646 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009647 // Insert instructions into newMBB based on incoming instruction
9648 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009649 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009650 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009651 MachineOperand& dest1Oper = bInstr->getOperand(0);
9652 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009653 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9654 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009655 argOpers[i] = &bInstr->getOperand(i+2);
9656
Dan Gohman71ea4e52010-05-14 21:01:44 +00009657 // We use some of the operands multiple times, so conservatively just
9658 // clear any kill flags that might be present.
9659 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9660 argOpers[i]->setIsKill(false);
9661 }
9662
Evan Chengad5b52f2010-01-08 19:14:57 +00009663 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009664 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009665
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009666 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009667 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009668 for (int i=0; i <= lastAddrIndx; ++i)
9669 (*MIB).addOperand(*argOpers[i]);
9670 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009671 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009672 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009673 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009674 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009675 MachineOperand newOp3 = *(argOpers[3]);
9676 if (newOp3.isImm())
9677 newOp3.setImm(newOp3.getImm()+4);
9678 else
9679 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009680 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009681 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009682
9683 // t3/4 are defined later, at the bottom of the loop
9684 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9685 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009686 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009687 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009688 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009689 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9690
Evan Cheng306b4ca2010-01-08 23:41:50 +00009691 // The subsequent operations should be using the destination registers of
9692 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009693 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009694 t1 = F->getRegInfo().createVirtualRegister(RC);
9695 t2 = F->getRegInfo().createVirtualRegister(RC);
9696 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9697 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009698 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009699 t1 = dest1Oper.getReg();
9700 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009701 }
9702
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009703 int valArgIndx = lastAddrIndx + 1;
9704 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009705 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009706 "invalid operand");
9707 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9708 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009709 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009710 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009711 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009712 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009713 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009714 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009715 (*MIB).addOperand(*argOpers[valArgIndx]);
9716 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009717 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009718 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009719 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009720 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009721 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009722 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009723 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009724 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009725 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009726 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009727
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009728 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009729 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009730 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009731 MIB.addReg(t2);
9732
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009733 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009734 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009735 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009736 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009737
Dale Johannesene4d209d2009-02-03 20:21:25 +00009738 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009739 for (int i=0; i <= lastAddrIndx; ++i)
9740 (*MIB).addOperand(*argOpers[i]);
9741
9742 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009743 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9744 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009745
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009746 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009747 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009748 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009749 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009750
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009751 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009752 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009753
Dan Gohman14152b42010-07-06 20:24:04 +00009754 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009755 return nextMBB;
9756}
9757
9758// private utility function
9759MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009760X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9761 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009762 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009763 // For the atomic min/max operator, we generate
9764 // thisMBB:
9765 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009766 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009767 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009768 // cmp t1, t2
9769 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009770 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009771 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9772 // bz newMBB
9773 // fallthrough -->nextMBB
9774 //
9775 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9776 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009777 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009778 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009779
Mon P Wang63307c32008-05-05 19:05:59 +00009780 /// First build the CFG
9781 MachineFunction *F = MBB->getParent();
9782 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009783 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9784 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9785 F->insert(MBBIter, newMBB);
9786 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009787
Dan Gohman14152b42010-07-06 20:24:04 +00009788 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9789 nextMBB->splice(nextMBB->begin(), thisMBB,
9790 llvm::next(MachineBasicBlock::iterator(mInstr)),
9791 thisMBB->end());
9792 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009793
Mon P Wang63307c32008-05-05 19:05:59 +00009794 // Update thisMBB to fall through to newMBB
9795 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009796
Mon P Wang63307c32008-05-05 19:05:59 +00009797 // newMBB jumps to newMBB and fall through to nextMBB
9798 newMBB->addSuccessor(nextMBB);
9799 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009800
Dale Johannesene4d209d2009-02-03 20:21:25 +00009801 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009802 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009803 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009804 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009805 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009806 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009807 int numArgs = mInstr->getNumOperands() - 1;
9808 for (int i=0; i < numArgs; ++i)
9809 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009810
Mon P Wang63307c32008-05-05 19:05:59 +00009811 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009812 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009813 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009814
Mon P Wangab3e7472008-05-05 22:56:23 +00009815 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009816 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009817 for (int i=0; i <= lastAddrIndx; ++i)
9818 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009819
Mon P Wang63307c32008-05-05 19:05:59 +00009820 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009821 assert((argOpers[valArgIndx]->isReg() ||
9822 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009823 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009824
9825 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009826 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009827 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009828 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009829 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009830 (*MIB).addOperand(*argOpers[valArgIndx]);
9831
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009832 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009833 MIB.addReg(t1);
9834
Dale Johannesene4d209d2009-02-03 20:21:25 +00009835 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009836 MIB.addReg(t1);
9837 MIB.addReg(t2);
9838
9839 // Generate movc
9840 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009841 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009842 MIB.addReg(t2);
9843 MIB.addReg(t1);
9844
9845 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009847 for (int i=0; i <= lastAddrIndx; ++i)
9848 (*MIB).addOperand(*argOpers[i]);
9849 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009850 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009851 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9852 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009853
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009854 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009855 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009856
Mon P Wang63307c32008-05-05 19:05:59 +00009857 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009858 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009859
Dan Gohman14152b42010-07-06 20:24:04 +00009860 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009861 return nextMBB;
9862}
9863
Eric Christopherf83a5de2009-08-27 18:08:16 +00009864// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009865// or XMM0_V32I8 in AVX all of this code can be replaced with that
9866// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009867MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009868X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009869 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009870 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9871 "Target must have SSE4.2 or AVX features enabled");
9872
Eric Christopherb120ab42009-08-18 22:50:32 +00009873 DebugLoc dl = MI->getDebugLoc();
9874 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009875 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009876 if (!Subtarget->hasAVX()) {
9877 if (memArg)
9878 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9879 else
9880 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9881 } else {
9882 if (memArg)
9883 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9884 else
9885 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9886 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009887
Eric Christopher41c902f2010-11-30 08:20:21 +00009888 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009889 for (unsigned i = 0; i < numArgs; ++i) {
9890 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009891 if (!(Op.isReg() && Op.isImplicit()))
9892 MIB.addOperand(Op);
9893 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009894 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009895 .addReg(X86::XMM0);
9896
Dan Gohman14152b42010-07-06 20:24:04 +00009897 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009898 return BB;
9899}
9900
9901MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009902X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009903 DebugLoc dl = MI->getDebugLoc();
9904 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009905
Eric Christopher228232b2010-11-30 07:20:12 +00009906 // Address into RAX/EAX, other two args into ECX, EDX.
9907 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9908 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9909 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9910 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009911 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009912
Eric Christopher228232b2010-11-30 07:20:12 +00009913 unsigned ValOps = X86::AddrNumOperands;
9914 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9915 .addReg(MI->getOperand(ValOps).getReg());
9916 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9917 .addReg(MI->getOperand(ValOps+1).getReg());
9918
9919 // The instruction doesn't actually take any operands though.
9920 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009921
Eric Christopher228232b2010-11-30 07:20:12 +00009922 MI->eraseFromParent(); // The pseudo is gone now.
9923 return BB;
9924}
9925
9926MachineBasicBlock *
9927X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009928 DebugLoc dl = MI->getDebugLoc();
9929 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009930
Eric Christopher228232b2010-11-30 07:20:12 +00009931 // First arg in ECX, the second in EAX.
9932 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9933 .addReg(MI->getOperand(0).getReg());
9934 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9935 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009936
Eric Christopher228232b2010-11-30 07:20:12 +00009937 // The instruction doesn't actually take any operands though.
9938 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009939
Eric Christopher228232b2010-11-30 07:20:12 +00009940 MI->eraseFromParent(); // The pseudo is gone now.
9941 return BB;
9942}
9943
9944MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009945X86TargetLowering::EmitVAARG64WithCustomInserter(
9946 MachineInstr *MI,
9947 MachineBasicBlock *MBB) const {
9948 // Emit va_arg instruction on X86-64.
9949
9950 // Operands to this pseudo-instruction:
9951 // 0 ) Output : destination address (reg)
9952 // 1-5) Input : va_list address (addr, i64mem)
9953 // 6 ) ArgSize : Size (in bytes) of vararg type
9954 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9955 // 8 ) Align : Alignment of type
9956 // 9 ) EFLAGS (implicit-def)
9957
9958 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9959 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9960
9961 unsigned DestReg = MI->getOperand(0).getReg();
9962 MachineOperand &Base = MI->getOperand(1);
9963 MachineOperand &Scale = MI->getOperand(2);
9964 MachineOperand &Index = MI->getOperand(3);
9965 MachineOperand &Disp = MI->getOperand(4);
9966 MachineOperand &Segment = MI->getOperand(5);
9967 unsigned ArgSize = MI->getOperand(6).getImm();
9968 unsigned ArgMode = MI->getOperand(7).getImm();
9969 unsigned Align = MI->getOperand(8).getImm();
9970
9971 // Memory Reference
9972 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9973 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9974 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9975
9976 // Machine Information
9977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9978 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9979 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9980 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9981 DebugLoc DL = MI->getDebugLoc();
9982
9983 // struct va_list {
9984 // i32 gp_offset
9985 // i32 fp_offset
9986 // i64 overflow_area (address)
9987 // i64 reg_save_area (address)
9988 // }
9989 // sizeof(va_list) = 24
9990 // alignment(va_list) = 8
9991
9992 unsigned TotalNumIntRegs = 6;
9993 unsigned TotalNumXMMRegs = 8;
9994 bool UseGPOffset = (ArgMode == 1);
9995 bool UseFPOffset = (ArgMode == 2);
9996 unsigned MaxOffset = TotalNumIntRegs * 8 +
9997 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9998
9999 /* Align ArgSize to a multiple of 8 */
10000 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10001 bool NeedsAlign = (Align > 8);
10002
10003 MachineBasicBlock *thisMBB = MBB;
10004 MachineBasicBlock *overflowMBB;
10005 MachineBasicBlock *offsetMBB;
10006 MachineBasicBlock *endMBB;
10007
10008 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10009 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10010 unsigned OffsetReg = 0;
10011
10012 if (!UseGPOffset && !UseFPOffset) {
10013 // If we only pull from the overflow region, we don't create a branch.
10014 // We don't need to alter control flow.
10015 OffsetDestReg = 0; // unused
10016 OverflowDestReg = DestReg;
10017
10018 offsetMBB = NULL;
10019 overflowMBB = thisMBB;
10020 endMBB = thisMBB;
10021 } else {
10022 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10023 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10024 // If not, pull from overflow_area. (branch to overflowMBB)
10025 //
10026 // thisMBB
10027 // | .
10028 // | .
10029 // offsetMBB overflowMBB
10030 // | .
10031 // | .
10032 // endMBB
10033
10034 // Registers for the PHI in endMBB
10035 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10036 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10037
10038 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10039 MachineFunction *MF = MBB->getParent();
10040 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10041 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10042 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10043
10044 MachineFunction::iterator MBBIter = MBB;
10045 ++MBBIter;
10046
10047 // Insert the new basic blocks
10048 MF->insert(MBBIter, offsetMBB);
10049 MF->insert(MBBIter, overflowMBB);
10050 MF->insert(MBBIter, endMBB);
10051
10052 // Transfer the remainder of MBB and its successor edges to endMBB.
10053 endMBB->splice(endMBB->begin(), thisMBB,
10054 llvm::next(MachineBasicBlock::iterator(MI)),
10055 thisMBB->end());
10056 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10057
10058 // Make offsetMBB and overflowMBB successors of thisMBB
10059 thisMBB->addSuccessor(offsetMBB);
10060 thisMBB->addSuccessor(overflowMBB);
10061
10062 // endMBB is a successor of both offsetMBB and overflowMBB
10063 offsetMBB->addSuccessor(endMBB);
10064 overflowMBB->addSuccessor(endMBB);
10065
10066 // Load the offset value into a register
10067 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10068 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10069 .addOperand(Base)
10070 .addOperand(Scale)
10071 .addOperand(Index)
10072 .addDisp(Disp, UseFPOffset ? 4 : 0)
10073 .addOperand(Segment)
10074 .setMemRefs(MMOBegin, MMOEnd);
10075
10076 // Check if there is enough room left to pull this argument.
10077 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10078 .addReg(OffsetReg)
10079 .addImm(MaxOffset + 8 - ArgSizeA8);
10080
10081 // Branch to "overflowMBB" if offset >= max
10082 // Fall through to "offsetMBB" otherwise
10083 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10084 .addMBB(overflowMBB);
10085 }
10086
10087 // In offsetMBB, emit code to use the reg_save_area.
10088 if (offsetMBB) {
10089 assert(OffsetReg != 0);
10090
10091 // Read the reg_save_area address.
10092 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10093 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10094 .addOperand(Base)
10095 .addOperand(Scale)
10096 .addOperand(Index)
10097 .addDisp(Disp, 16)
10098 .addOperand(Segment)
10099 .setMemRefs(MMOBegin, MMOEnd);
10100
10101 // Zero-extend the offset
10102 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10103 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10104 .addImm(0)
10105 .addReg(OffsetReg)
10106 .addImm(X86::sub_32bit);
10107
10108 // Add the offset to the reg_save_area to get the final address.
10109 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10110 .addReg(OffsetReg64)
10111 .addReg(RegSaveReg);
10112
10113 // Compute the offset for the next argument
10114 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10115 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10116 .addReg(OffsetReg)
10117 .addImm(UseFPOffset ? 16 : 8);
10118
10119 // Store it back into the va_list.
10120 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10121 .addOperand(Base)
10122 .addOperand(Scale)
10123 .addOperand(Index)
10124 .addDisp(Disp, UseFPOffset ? 4 : 0)
10125 .addOperand(Segment)
10126 .addReg(NextOffsetReg)
10127 .setMemRefs(MMOBegin, MMOEnd);
10128
10129 // Jump to endMBB
10130 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10131 .addMBB(endMBB);
10132 }
10133
10134 //
10135 // Emit code to use overflow area
10136 //
10137
10138 // Load the overflow_area address into a register.
10139 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10140 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10141 .addOperand(Base)
10142 .addOperand(Scale)
10143 .addOperand(Index)
10144 .addDisp(Disp, 8)
10145 .addOperand(Segment)
10146 .setMemRefs(MMOBegin, MMOEnd);
10147
10148 // If we need to align it, do so. Otherwise, just copy the address
10149 // to OverflowDestReg.
10150 if (NeedsAlign) {
10151 // Align the overflow address
10152 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10153 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10154
10155 // aligned_addr = (addr + (align-1)) & ~(align-1)
10156 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10157 .addReg(OverflowAddrReg)
10158 .addImm(Align-1);
10159
10160 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10161 .addReg(TmpReg)
10162 .addImm(~(uint64_t)(Align-1));
10163 } else {
10164 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10165 .addReg(OverflowAddrReg);
10166 }
10167
10168 // Compute the next overflow address after this argument.
10169 // (the overflow address should be kept 8-byte aligned)
10170 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10171 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10172 .addReg(OverflowDestReg)
10173 .addImm(ArgSizeA8);
10174
10175 // Store the new overflow address.
10176 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10177 .addOperand(Base)
10178 .addOperand(Scale)
10179 .addOperand(Index)
10180 .addDisp(Disp, 8)
10181 .addOperand(Segment)
10182 .addReg(NextAddrReg)
10183 .setMemRefs(MMOBegin, MMOEnd);
10184
10185 // If we branched, emit the PHI to the front of endMBB.
10186 if (offsetMBB) {
10187 BuildMI(*endMBB, endMBB->begin(), DL,
10188 TII->get(X86::PHI), DestReg)
10189 .addReg(OffsetDestReg).addMBB(offsetMBB)
10190 .addReg(OverflowDestReg).addMBB(overflowMBB);
10191 }
10192
10193 // Erase the pseudo instruction
10194 MI->eraseFromParent();
10195
10196 return endMBB;
10197}
10198
10199MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010200X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10201 MachineInstr *MI,
10202 MachineBasicBlock *MBB) const {
10203 // Emit code to save XMM registers to the stack. The ABI says that the
10204 // number of registers to save is given in %al, so it's theoretically
10205 // possible to do an indirect jump trick to avoid saving all of them,
10206 // however this code takes a simpler approach and just executes all
10207 // of the stores if %al is non-zero. It's less code, and it's probably
10208 // easier on the hardware branch predictor, and stores aren't all that
10209 // expensive anyway.
10210
10211 // Create the new basic blocks. One block contains all the XMM stores,
10212 // and one block is the final destination regardless of whether any
10213 // stores were performed.
10214 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10215 MachineFunction *F = MBB->getParent();
10216 MachineFunction::iterator MBBIter = MBB;
10217 ++MBBIter;
10218 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10219 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10220 F->insert(MBBIter, XMMSaveMBB);
10221 F->insert(MBBIter, EndMBB);
10222
Dan Gohman14152b42010-07-06 20:24:04 +000010223 // Transfer the remainder of MBB and its successor edges to EndMBB.
10224 EndMBB->splice(EndMBB->begin(), MBB,
10225 llvm::next(MachineBasicBlock::iterator(MI)),
10226 MBB->end());
10227 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10228
Dan Gohmand6708ea2009-08-15 01:38:56 +000010229 // The original block will now fall through to the XMM save block.
10230 MBB->addSuccessor(XMMSaveMBB);
10231 // The XMMSaveMBB will fall through to the end block.
10232 XMMSaveMBB->addSuccessor(EndMBB);
10233
10234 // Now add the instructions.
10235 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10236 DebugLoc DL = MI->getDebugLoc();
10237
10238 unsigned CountReg = MI->getOperand(0).getReg();
10239 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10240 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10241
10242 if (!Subtarget->isTargetWin64()) {
10243 // If %al is 0, branch around the XMM save block.
10244 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010245 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010246 MBB->addSuccessor(EndMBB);
10247 }
10248
10249 // In the XMM save block, save all the XMM argument registers.
10250 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10251 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010252 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010253 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010254 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010255 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010256 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010257 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10258 .addFrameIndex(RegSaveFrameIndex)
10259 .addImm(/*Scale=*/1)
10260 .addReg(/*IndexReg=*/0)
10261 .addImm(/*Disp=*/Offset)
10262 .addReg(/*Segment=*/0)
10263 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010264 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010265 }
10266
Dan Gohman14152b42010-07-06 20:24:04 +000010267 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010268
10269 return EndMBB;
10270}
Mon P Wang63307c32008-05-05 19:05:59 +000010271
Evan Cheng60c07e12006-07-05 22:17:51 +000010272MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010273X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010274 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010275 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10276 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010277
Chris Lattner52600972009-09-02 05:57:00 +000010278 // To "insert" a SELECT_CC instruction, we actually have to insert the
10279 // diamond control-flow pattern. The incoming instruction knows the
10280 // destination vreg to set, the condition code register to branch on, the
10281 // true/false values to select between, and a branch opcode to use.
10282 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10283 MachineFunction::iterator It = BB;
10284 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010285
Chris Lattner52600972009-09-02 05:57:00 +000010286 // thisMBB:
10287 // ...
10288 // TrueVal = ...
10289 // cmpTY ccX, r1, r2
10290 // bCC copy1MBB
10291 // fallthrough --> copy0MBB
10292 MachineBasicBlock *thisMBB = BB;
10293 MachineFunction *F = BB->getParent();
10294 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10295 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010296 F->insert(It, copy0MBB);
10297 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010298
Bill Wendling730c07e2010-06-25 20:48:10 +000010299 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10300 // live into the sink and copy blocks.
10301 const MachineFunction *MF = BB->getParent();
10302 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10303 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010304
Dan Gohman14152b42010-07-06 20:24:04 +000010305 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10306 const MachineOperand &MO = MI->getOperand(I);
10307 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010308 unsigned Reg = MO.getReg();
10309 if (Reg != X86::EFLAGS) continue;
10310 copy0MBB->addLiveIn(Reg);
10311 sinkMBB->addLiveIn(Reg);
10312 }
10313
Dan Gohman14152b42010-07-06 20:24:04 +000010314 // Transfer the remainder of BB and its successor edges to sinkMBB.
10315 sinkMBB->splice(sinkMBB->begin(), BB,
10316 llvm::next(MachineBasicBlock::iterator(MI)),
10317 BB->end());
10318 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10319
10320 // Add the true and fallthrough blocks as its successors.
10321 BB->addSuccessor(copy0MBB);
10322 BB->addSuccessor(sinkMBB);
10323
10324 // Create the conditional branch instruction.
10325 unsigned Opc =
10326 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10327 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10328
Chris Lattner52600972009-09-02 05:57:00 +000010329 // copy0MBB:
10330 // %FalseValue = ...
10331 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010332 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010333
Chris Lattner52600972009-09-02 05:57:00 +000010334 // sinkMBB:
10335 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10336 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010337 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10338 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010339 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10340 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10341
Dan Gohman14152b42010-07-06 20:24:04 +000010342 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010343 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010344}
10345
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010346MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010347X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010348 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010349 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10350 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010351
10352 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10353 // non-trivial part is impdef of ESP.
10354 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10355 // mingw-w64.
10356
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010357 const char *StackProbeSymbol =
10358 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10359
Dan Gohman14152b42010-07-06 20:24:04 +000010360 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010361 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010362 .addReg(X86::EAX, RegState::Implicit)
10363 .addReg(X86::ESP, RegState::Implicit)
10364 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010365 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10366 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010367
Dan Gohman14152b42010-07-06 20:24:04 +000010368 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010369 return BB;
10370}
Chris Lattner52600972009-09-02 05:57:00 +000010371
10372MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010373X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10374 MachineBasicBlock *BB) const {
10375 // This is pretty easy. We're taking the value that we received from
10376 // our load from the relocation, sticking it in either RDI (x86-64)
10377 // or EAX and doing an indirect call. The return value will then
10378 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010379 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010380 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010381 DebugLoc DL = MI->getDebugLoc();
10382 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010383
10384 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010385 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010386
Eric Christopher30ef0e52010-06-03 04:07:48 +000010387 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010388 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10389 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010390 .addReg(X86::RIP)
10391 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010392 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010393 MI->getOperand(3).getTargetFlags())
10394 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010395 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010396 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010397 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010398 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10399 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010400 .addReg(0)
10401 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010402 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010403 MI->getOperand(3).getTargetFlags())
10404 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010405 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010406 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010407 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010408 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10409 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010410 .addReg(TII->getGlobalBaseReg(F))
10411 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010412 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010413 MI->getOperand(3).getTargetFlags())
10414 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010415 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010416 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010417 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010418
Dan Gohman14152b42010-07-06 20:24:04 +000010419 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010420 return BB;
10421}
10422
10423MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010424X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010425 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010426 switch (MI->getOpcode()) {
10427 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010428 case X86::TAILJMPd64:
10429 case X86::TAILJMPr64:
10430 case X86::TAILJMPm64:
10431 assert(!"TAILJMP64 would not be touched here.");
10432 case X86::TCRETURNdi64:
10433 case X86::TCRETURNri64:
10434 case X86::TCRETURNmi64:
10435 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10436 // On AMD64, additional defs should be added before register allocation.
10437 if (!Subtarget->isTargetWin64()) {
10438 MI->addRegisterDefined(X86::RSI);
10439 MI->addRegisterDefined(X86::RDI);
10440 MI->addRegisterDefined(X86::XMM6);
10441 MI->addRegisterDefined(X86::XMM7);
10442 MI->addRegisterDefined(X86::XMM8);
10443 MI->addRegisterDefined(X86::XMM9);
10444 MI->addRegisterDefined(X86::XMM10);
10445 MI->addRegisterDefined(X86::XMM11);
10446 MI->addRegisterDefined(X86::XMM12);
10447 MI->addRegisterDefined(X86::XMM13);
10448 MI->addRegisterDefined(X86::XMM14);
10449 MI->addRegisterDefined(X86::XMM15);
10450 }
10451 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010452 case X86::WIN_ALLOCA:
10453 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010454 case X86::TLSCall_32:
10455 case X86::TLSCall_64:
10456 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010457 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010458 case X86::CMOV_FR32:
10459 case X86::CMOV_FR64:
10460 case X86::CMOV_V4F32:
10461 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010462 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010463 case X86::CMOV_GR16:
10464 case X86::CMOV_GR32:
10465 case X86::CMOV_RFP32:
10466 case X86::CMOV_RFP64:
10467 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010468 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010469
Dale Johannesen849f2142007-07-03 00:53:03 +000010470 case X86::FP32_TO_INT16_IN_MEM:
10471 case X86::FP32_TO_INT32_IN_MEM:
10472 case X86::FP32_TO_INT64_IN_MEM:
10473 case X86::FP64_TO_INT16_IN_MEM:
10474 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010475 case X86::FP64_TO_INT64_IN_MEM:
10476 case X86::FP80_TO_INT16_IN_MEM:
10477 case X86::FP80_TO_INT32_IN_MEM:
10478 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010479 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10480 DebugLoc DL = MI->getDebugLoc();
10481
Evan Cheng60c07e12006-07-05 22:17:51 +000010482 // Change the floating point control register to use "round towards zero"
10483 // mode when truncating to an integer value.
10484 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010485 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010486 addFrameReference(BuildMI(*BB, MI, DL,
10487 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010488
10489 // Load the old value of the high byte of the control word...
10490 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010491 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010492 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010493 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010494
10495 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010496 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010497 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010498
10499 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010500 addFrameReference(BuildMI(*BB, MI, DL,
10501 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010502
10503 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010504 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010505 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010506
10507 // Get the X86 opcode to use.
10508 unsigned Opc;
10509 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010510 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010511 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10512 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10513 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10514 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10515 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10516 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010517 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10518 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10519 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010520 }
10521
10522 X86AddressMode AM;
10523 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010524 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010525 AM.BaseType = X86AddressMode::RegBase;
10526 AM.Base.Reg = Op.getReg();
10527 } else {
10528 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010529 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010530 }
10531 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010532 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010533 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010534 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010535 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010536 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010537 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010538 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010539 AM.GV = Op.getGlobal();
10540 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010541 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010542 }
Dan Gohman14152b42010-07-06 20:24:04 +000010543 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010544 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010545
10546 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010547 addFrameReference(BuildMI(*BB, MI, DL,
10548 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010549
Dan Gohman14152b42010-07-06 20:24:04 +000010550 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010551 return BB;
10552 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010553 // String/text processing lowering.
10554 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010555 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010556 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10557 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010558 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010559 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10560 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010561 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010562 return EmitPCMP(MI, BB, 5, false /* in mem */);
10563 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010564 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010565 return EmitPCMP(MI, BB, 5, true /* in mem */);
10566
Eric Christopher228232b2010-11-30 07:20:12 +000010567 // Thread synchronization.
10568 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010569 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010570 case X86::MWAIT:
10571 return EmitMwait(MI, BB);
10572
Eric Christopherb120ab42009-08-18 22:50:32 +000010573 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010574 case X86::ATOMAND32:
10575 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010576 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010577 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010578 X86::NOT32r, X86::EAX,
10579 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010580 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010581 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10582 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010583 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010584 X86::NOT32r, X86::EAX,
10585 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010586 case X86::ATOMXOR32:
10587 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010588 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010589 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010590 X86::NOT32r, X86::EAX,
10591 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010592 case X86::ATOMNAND32:
10593 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010594 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010595 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010596 X86::NOT32r, X86::EAX,
10597 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010598 case X86::ATOMMIN32:
10599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10600 case X86::ATOMMAX32:
10601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10602 case X86::ATOMUMIN32:
10603 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10604 case X86::ATOMUMAX32:
10605 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010606
10607 case X86::ATOMAND16:
10608 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10609 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010610 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010611 X86::NOT16r, X86::AX,
10612 X86::GR16RegisterClass);
10613 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010614 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010615 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010616 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010617 X86::NOT16r, X86::AX,
10618 X86::GR16RegisterClass);
10619 case X86::ATOMXOR16:
10620 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10621 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010622 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010623 X86::NOT16r, X86::AX,
10624 X86::GR16RegisterClass);
10625 case X86::ATOMNAND16:
10626 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10627 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010628 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010629 X86::NOT16r, X86::AX,
10630 X86::GR16RegisterClass, true);
10631 case X86::ATOMMIN16:
10632 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10633 case X86::ATOMMAX16:
10634 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10635 case X86::ATOMUMIN16:
10636 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10637 case X86::ATOMUMAX16:
10638 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10639
10640 case X86::ATOMAND8:
10641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10642 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010643 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010644 X86::NOT8r, X86::AL,
10645 X86::GR8RegisterClass);
10646 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010647 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010648 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010649 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010650 X86::NOT8r, X86::AL,
10651 X86::GR8RegisterClass);
10652 case X86::ATOMXOR8:
10653 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10654 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010655 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010656 X86::NOT8r, X86::AL,
10657 X86::GR8RegisterClass);
10658 case X86::ATOMNAND8:
10659 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10660 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010661 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010662 X86::NOT8r, X86::AL,
10663 X86::GR8RegisterClass, true);
10664 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010665 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010666 case X86::ATOMAND64:
10667 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010668 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010669 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010670 X86::NOT64r, X86::RAX,
10671 X86::GR64RegisterClass);
10672 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010673 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10674 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010675 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010676 X86::NOT64r, X86::RAX,
10677 X86::GR64RegisterClass);
10678 case X86::ATOMXOR64:
10679 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010680 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010681 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010682 X86::NOT64r, X86::RAX,
10683 X86::GR64RegisterClass);
10684 case X86::ATOMNAND64:
10685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10686 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010687 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010688 X86::NOT64r, X86::RAX,
10689 X86::GR64RegisterClass, true);
10690 case X86::ATOMMIN64:
10691 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10692 case X86::ATOMMAX64:
10693 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10694 case X86::ATOMUMIN64:
10695 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10696 case X86::ATOMUMAX64:
10697 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010698
10699 // This group does 64-bit operations on a 32-bit host.
10700 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010701 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010702 X86::AND32rr, X86::AND32rr,
10703 X86::AND32ri, X86::AND32ri,
10704 false);
10705 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010706 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010707 X86::OR32rr, X86::OR32rr,
10708 X86::OR32ri, X86::OR32ri,
10709 false);
10710 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010711 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010712 X86::XOR32rr, X86::XOR32rr,
10713 X86::XOR32ri, X86::XOR32ri,
10714 false);
10715 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010716 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010717 X86::AND32rr, X86::AND32rr,
10718 X86::AND32ri, X86::AND32ri,
10719 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010720 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010721 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010722 X86::ADD32rr, X86::ADC32rr,
10723 X86::ADD32ri, X86::ADC32ri,
10724 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010725 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010726 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010727 X86::SUB32rr, X86::SBB32rr,
10728 X86::SUB32ri, X86::SBB32ri,
10729 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010730 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010731 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010732 X86::MOV32rr, X86::MOV32rr,
10733 X86::MOV32ri, X86::MOV32ri,
10734 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010735 case X86::VASTART_SAVE_XMM_REGS:
10736 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010737
10738 case X86::VAARG_64:
10739 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010740 }
10741}
10742
10743//===----------------------------------------------------------------------===//
10744// X86 Optimization Hooks
10745//===----------------------------------------------------------------------===//
10746
Dan Gohman475871a2008-07-27 21:46:04 +000010747void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010748 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010749 APInt &KnownZero,
10750 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010751 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010752 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010753 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010754 assert((Opc >= ISD::BUILTIN_OP_END ||
10755 Opc == ISD::INTRINSIC_WO_CHAIN ||
10756 Opc == ISD::INTRINSIC_W_CHAIN ||
10757 Opc == ISD::INTRINSIC_VOID) &&
10758 "Should use MaskedValueIsZero if you don't know whether Op"
10759 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010760
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010761 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010762 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010763 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010764 case X86ISD::ADD:
10765 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010766 case X86ISD::ADC:
10767 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010768 case X86ISD::SMUL:
10769 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010770 case X86ISD::INC:
10771 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010772 case X86ISD::OR:
10773 case X86ISD::XOR:
10774 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010775 // These nodes' second result is a boolean.
10776 if (Op.getResNo() == 0)
10777 break;
10778 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010779 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010780 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10781 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010782 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010783 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010784}
Chris Lattner259e97c2006-01-31 19:43:35 +000010785
Owen Andersonbc146b02010-09-21 20:42:50 +000010786unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10787 unsigned Depth) const {
10788 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10789 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10790 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010791
Owen Andersonbc146b02010-09-21 20:42:50 +000010792 // Fallback case.
10793 return 1;
10794}
10795
Evan Cheng206ee9d2006-07-07 08:33:52 +000010796/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010797/// node is a GlobalAddress + offset.
10798bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010799 const GlobalValue* &GA,
10800 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010801 if (N->getOpcode() == X86ISD::Wrapper) {
10802 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010803 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010804 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010805 return true;
10806 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010807 }
Evan Chengad4196b2008-05-12 19:56:52 +000010808 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010809}
10810
Evan Cheng206ee9d2006-07-07 08:33:52 +000010811/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10812/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10813/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010814/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010815static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010816 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010817 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010818 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010819
Eli Friedman7a5e5552009-06-07 06:52:44 +000010820 if (VT.getSizeInBits() != 128)
10821 return SDValue();
10822
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010823 // Don't create instructions with illegal types after legalize types has run.
10824 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10825 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10826 return SDValue();
10827
Nate Begemanfdea31a2010-03-24 20:49:50 +000010828 SmallVector<SDValue, 16> Elts;
10829 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010830 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010831
Nate Begemanfdea31a2010-03-24 20:49:50 +000010832 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010833}
Evan Chengd880b972008-05-09 21:53:03 +000010834
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010835/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10836/// generation and convert it from being a bunch of shuffles and extracts
10837/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010838static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10839 const TargetLowering &TLI) {
10840 SDValue InputVector = N->getOperand(0);
10841
10842 // Only operate on vectors of 4 elements, where the alternative shuffling
10843 // gets to be more expensive.
10844 if (InputVector.getValueType() != MVT::v4i32)
10845 return SDValue();
10846
10847 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10848 // single use which is a sign-extend or zero-extend, and all elements are
10849 // used.
10850 SmallVector<SDNode *, 4> Uses;
10851 unsigned ExtractedElements = 0;
10852 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10853 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10854 if (UI.getUse().getResNo() != InputVector.getResNo())
10855 return SDValue();
10856
10857 SDNode *Extract = *UI;
10858 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10859 return SDValue();
10860
10861 if (Extract->getValueType(0) != MVT::i32)
10862 return SDValue();
10863 if (!Extract->hasOneUse())
10864 return SDValue();
10865 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10866 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10867 return SDValue();
10868 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10869 return SDValue();
10870
10871 // Record which element was extracted.
10872 ExtractedElements |=
10873 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10874
10875 Uses.push_back(Extract);
10876 }
10877
10878 // If not all the elements were used, this may not be worthwhile.
10879 if (ExtractedElements != 15)
10880 return SDValue();
10881
10882 // Ok, we've now decided to do the transformation.
10883 DebugLoc dl = InputVector.getDebugLoc();
10884
10885 // Store the value to a temporary stack slot.
10886 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010887 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10888 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010889
10890 // Replace each use (extract) with a load of the appropriate element.
10891 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10892 UE = Uses.end(); UI != UE; ++UI) {
10893 SDNode *Extract = *UI;
10894
10895 // Compute the element's address.
10896 SDValue Idx = Extract->getOperand(1);
10897 unsigned EltSize =
10898 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10899 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10900 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10901
Eric Christopher90eb4022010-07-22 00:26:08 +000010902 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010903 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010904
10905 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010906 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010907 ScalarAddr, MachinePointerInfo(),
10908 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010909
10910 // Replace the exact with the load.
10911 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10912 }
10913
10914 // The replacement was made in place; don't return anything.
10915 return SDValue();
10916}
10917
Chris Lattner83e6c992006-10-04 06:57:07 +000010918/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010919static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010920 const X86Subtarget *Subtarget) {
10921 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010922 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010923 // Get the LHS/RHS of the select.
10924 SDValue LHS = N->getOperand(1);
10925 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010926
Dan Gohman670e5392009-09-21 18:03:22 +000010927 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010928 // instructions match the semantics of the common C idiom x<y?x:y but not
10929 // x<=y?x:y, because of how they handle negative zero (which can be
10930 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010931 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010932 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010933 Cond.getOpcode() == ISD::SETCC) {
10934 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010935
Chris Lattner47b4ce82009-03-11 05:48:52 +000010936 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010937 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010938 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10939 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010940 switch (CC) {
10941 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010942 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010943 // Converting this to a min would handle NaNs incorrectly, and swapping
10944 // the operands would cause it to handle comparisons between positive
10945 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010946 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010947 if (!UnsafeFPMath &&
10948 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10949 break;
10950 std::swap(LHS, RHS);
10951 }
Dan Gohman670e5392009-09-21 18:03:22 +000010952 Opcode = X86ISD::FMIN;
10953 break;
10954 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010955 // Converting this to a min would handle comparisons between positive
10956 // and negative zero incorrectly.
10957 if (!UnsafeFPMath &&
10958 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10959 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010960 Opcode = X86ISD::FMIN;
10961 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010962 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010963 // Converting this to a min would handle both negative zeros and NaNs
10964 // incorrectly, but we can swap the operands to fix both.
10965 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010966 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010967 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010968 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010969 Opcode = X86ISD::FMIN;
10970 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010971
Dan Gohman670e5392009-09-21 18:03:22 +000010972 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010973 // Converting this to a max would handle comparisons between positive
10974 // and negative zero incorrectly.
10975 if (!UnsafeFPMath &&
10976 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10977 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010978 Opcode = X86ISD::FMAX;
10979 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010980 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010981 // Converting this to a max would handle NaNs incorrectly, and swapping
10982 // the operands would cause it to handle comparisons between positive
10983 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010984 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010985 if (!UnsafeFPMath &&
10986 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10987 break;
10988 std::swap(LHS, RHS);
10989 }
Dan Gohman670e5392009-09-21 18:03:22 +000010990 Opcode = X86ISD::FMAX;
10991 break;
10992 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010993 // Converting this to a max would handle both negative zeros and NaNs
10994 // incorrectly, but we can swap the operands to fix both.
10995 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010996 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010997 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010998 case ISD::SETGE:
10999 Opcode = X86ISD::FMAX;
11000 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011001 }
Dan Gohman670e5392009-09-21 18:03:22 +000011002 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011003 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11004 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011005 switch (CC) {
11006 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011007 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011008 // Converting this to a min would handle comparisons between positive
11009 // and negative zero incorrectly, and swapping the operands would
11010 // cause it to handle NaNs incorrectly.
11011 if (!UnsafeFPMath &&
11012 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011013 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011014 break;
11015 std::swap(LHS, RHS);
11016 }
Dan Gohman670e5392009-09-21 18:03:22 +000011017 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011018 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011019 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011020 // Converting this to a min would handle NaNs incorrectly.
11021 if (!UnsafeFPMath &&
11022 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11023 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011024 Opcode = X86ISD::FMIN;
11025 break;
11026 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011027 // Converting this to a min would handle both negative zeros and NaNs
11028 // incorrectly, but we can swap the operands to fix both.
11029 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011030 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011031 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011032 case ISD::SETGE:
11033 Opcode = X86ISD::FMIN;
11034 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011035
Dan Gohman670e5392009-09-21 18:03:22 +000011036 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011037 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011038 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011039 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011040 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011041 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011042 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011043 // Converting this to a max would handle comparisons between positive
11044 // and negative zero incorrectly, and swapping the operands would
11045 // cause it to handle NaNs incorrectly.
11046 if (!UnsafeFPMath &&
11047 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011048 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011049 break;
11050 std::swap(LHS, RHS);
11051 }
Dan Gohman670e5392009-09-21 18:03:22 +000011052 Opcode = X86ISD::FMAX;
11053 break;
11054 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011055 // Converting this to a max would handle both negative zeros and NaNs
11056 // incorrectly, but we can swap the operands to fix both.
11057 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011058 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011059 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011060 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011061 Opcode = X86ISD::FMAX;
11062 break;
11063 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011064 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011065
Chris Lattner47b4ce82009-03-11 05:48:52 +000011066 if (Opcode)
11067 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011068 }
Eric Christopherfd179292009-08-27 18:07:15 +000011069
Chris Lattnerd1980a52009-03-12 06:52:53 +000011070 // If this is a select between two integer constants, try to do some
11071 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011072 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11073 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011074 // Don't do this for crazy integer types.
11075 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11076 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011077 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011078 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011079
Chris Lattnercee56e72009-03-13 05:53:31 +000011080 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011081 // Efficiently invertible.
11082 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11083 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11084 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11085 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011086 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011087 }
Eric Christopherfd179292009-08-27 18:07:15 +000011088
Chris Lattnerd1980a52009-03-12 06:52:53 +000011089 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011090 if (FalseC->getAPIntValue() == 0 &&
11091 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011092 if (NeedsCondInvert) // Invert the condition if needed.
11093 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11094 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011095
Chris Lattnerd1980a52009-03-12 06:52:53 +000011096 // Zero extend the condition if needed.
11097 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011098
Chris Lattnercee56e72009-03-13 05:53:31 +000011099 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011100 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011101 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011102 }
Eric Christopherfd179292009-08-27 18:07:15 +000011103
Chris Lattner97a29a52009-03-13 05:22:11 +000011104 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011105 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011106 if (NeedsCondInvert) // Invert the condition if needed.
11107 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11108 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011109
Chris Lattner97a29a52009-03-13 05:22:11 +000011110 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011111 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11112 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011113 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011114 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011115 }
Eric Christopherfd179292009-08-27 18:07:15 +000011116
Chris Lattnercee56e72009-03-13 05:53:31 +000011117 // Optimize cases that will turn into an LEA instruction. This requires
11118 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011119 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011120 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011121 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011122
Chris Lattnercee56e72009-03-13 05:53:31 +000011123 bool isFastMultiplier = false;
11124 if (Diff < 10) {
11125 switch ((unsigned char)Diff) {
11126 default: break;
11127 case 1: // result = add base, cond
11128 case 2: // result = lea base( , cond*2)
11129 case 3: // result = lea base(cond, cond*2)
11130 case 4: // result = lea base( , cond*4)
11131 case 5: // result = lea base(cond, cond*4)
11132 case 8: // result = lea base( , cond*8)
11133 case 9: // result = lea base(cond, cond*8)
11134 isFastMultiplier = true;
11135 break;
11136 }
11137 }
Eric Christopherfd179292009-08-27 18:07:15 +000011138
Chris Lattnercee56e72009-03-13 05:53:31 +000011139 if (isFastMultiplier) {
11140 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11141 if (NeedsCondInvert) // Invert the condition if needed.
11142 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11143 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011144
Chris Lattnercee56e72009-03-13 05:53:31 +000011145 // Zero extend the condition if needed.
11146 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11147 Cond);
11148 // Scale the condition by the difference.
11149 if (Diff != 1)
11150 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11151 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011152
Chris Lattnercee56e72009-03-13 05:53:31 +000011153 // Add the base if non-zero.
11154 if (FalseC->getAPIntValue() != 0)
11155 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11156 SDValue(FalseC, 0));
11157 return Cond;
11158 }
Eric Christopherfd179292009-08-27 18:07:15 +000011159 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011160 }
11161 }
Eric Christopherfd179292009-08-27 18:07:15 +000011162
Dan Gohman475871a2008-07-27 21:46:04 +000011163 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011164}
11165
Chris Lattnerd1980a52009-03-12 06:52:53 +000011166/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11167static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11168 TargetLowering::DAGCombinerInfo &DCI) {
11169 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011170
Chris Lattnerd1980a52009-03-12 06:52:53 +000011171 // If the flag operand isn't dead, don't touch this CMOV.
11172 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11173 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011174
Chris Lattnerd1980a52009-03-12 06:52:53 +000011175 // If this is a select between two integer constants, try to do some
11176 // optimizations. Note that the operands are ordered the opposite of SELECT
11177 // operands.
11178 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11179 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11180 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11181 // larger than FalseC (the false value).
11182 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011183
Chris Lattnerd1980a52009-03-12 06:52:53 +000011184 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11185 CC = X86::GetOppositeBranchCondition(CC);
11186 std::swap(TrueC, FalseC);
11187 }
Eric Christopherfd179292009-08-27 18:07:15 +000011188
Chris Lattnerd1980a52009-03-12 06:52:53 +000011189 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011190 // This is efficient for any integer data type (including i8/i16) and
11191 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011192 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11193 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011194 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11195 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011196
Chris Lattnerd1980a52009-03-12 06:52:53 +000011197 // Zero extend the condition if needed.
11198 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011199
Chris Lattnerd1980a52009-03-12 06:52:53 +000011200 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11201 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011202 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011203 if (N->getNumValues() == 2) // Dead flag value?
11204 return DCI.CombineTo(N, Cond, SDValue());
11205 return Cond;
11206 }
Eric Christopherfd179292009-08-27 18:07:15 +000011207
Chris Lattnercee56e72009-03-13 05:53:31 +000011208 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11209 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011210 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11211 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011212 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11213 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011214
Chris Lattner97a29a52009-03-13 05:22:11 +000011215 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011216 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11217 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011218 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11219 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011220
Chris Lattner97a29a52009-03-13 05:22:11 +000011221 if (N->getNumValues() == 2) // Dead flag value?
11222 return DCI.CombineTo(N, Cond, SDValue());
11223 return Cond;
11224 }
Eric Christopherfd179292009-08-27 18:07:15 +000011225
Chris Lattnercee56e72009-03-13 05:53:31 +000011226 // Optimize cases that will turn into an LEA instruction. This requires
11227 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011228 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011229 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011231
Chris Lattnercee56e72009-03-13 05:53:31 +000011232 bool isFastMultiplier = false;
11233 if (Diff < 10) {
11234 switch ((unsigned char)Diff) {
11235 default: break;
11236 case 1: // result = add base, cond
11237 case 2: // result = lea base( , cond*2)
11238 case 3: // result = lea base(cond, cond*2)
11239 case 4: // result = lea base( , cond*4)
11240 case 5: // result = lea base(cond, cond*4)
11241 case 8: // result = lea base( , cond*8)
11242 case 9: // result = lea base(cond, cond*8)
11243 isFastMultiplier = true;
11244 break;
11245 }
11246 }
Eric Christopherfd179292009-08-27 18:07:15 +000011247
Chris Lattnercee56e72009-03-13 05:53:31 +000011248 if (isFastMultiplier) {
11249 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11250 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011251 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11252 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011253 // Zero extend the condition if needed.
11254 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11255 Cond);
11256 // Scale the condition by the difference.
11257 if (Diff != 1)
11258 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11259 DAG.getConstant(Diff, Cond.getValueType()));
11260
11261 // Add the base if non-zero.
11262 if (FalseC->getAPIntValue() != 0)
11263 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11264 SDValue(FalseC, 0));
11265 if (N->getNumValues() == 2) // Dead flag value?
11266 return DCI.CombineTo(N, Cond, SDValue());
11267 return Cond;
11268 }
Eric Christopherfd179292009-08-27 18:07:15 +000011269 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011270 }
11271 }
11272 return SDValue();
11273}
11274
11275
Evan Cheng0b0cd912009-03-28 05:57:29 +000011276/// PerformMulCombine - Optimize a single multiply with constant into two
11277/// in order to implement it with two cheaper instructions, e.g.
11278/// LEA + SHL, LEA + LEA.
11279static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11280 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011281 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11282 return SDValue();
11283
Owen Andersone50ed302009-08-10 22:56:29 +000011284 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011285 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011286 return SDValue();
11287
11288 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11289 if (!C)
11290 return SDValue();
11291 uint64_t MulAmt = C->getZExtValue();
11292 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11293 return SDValue();
11294
11295 uint64_t MulAmt1 = 0;
11296 uint64_t MulAmt2 = 0;
11297 if ((MulAmt % 9) == 0) {
11298 MulAmt1 = 9;
11299 MulAmt2 = MulAmt / 9;
11300 } else if ((MulAmt % 5) == 0) {
11301 MulAmt1 = 5;
11302 MulAmt2 = MulAmt / 5;
11303 } else if ((MulAmt % 3) == 0) {
11304 MulAmt1 = 3;
11305 MulAmt2 = MulAmt / 3;
11306 }
11307 if (MulAmt2 &&
11308 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11309 DebugLoc DL = N->getDebugLoc();
11310
11311 if (isPowerOf2_64(MulAmt2) &&
11312 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11313 // If second multiplifer is pow2, issue it first. We want the multiply by
11314 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11315 // is an add.
11316 std::swap(MulAmt1, MulAmt2);
11317
11318 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011319 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011320 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011321 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011322 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011323 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011324 DAG.getConstant(MulAmt1, VT));
11325
Eric Christopherfd179292009-08-27 18:07:15 +000011326 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011327 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011328 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011329 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011330 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011331 DAG.getConstant(MulAmt2, VT));
11332
11333 // Do not add new nodes to DAG combiner worklist.
11334 DCI.CombineTo(N, NewMul, false);
11335 }
11336 return SDValue();
11337}
11338
Evan Chengad9c0a32009-12-15 00:53:42 +000011339static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11340 SDValue N0 = N->getOperand(0);
11341 SDValue N1 = N->getOperand(1);
11342 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11343 EVT VT = N0.getValueType();
11344
11345 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11346 // since the result of setcc_c is all zero's or all ones.
11347 if (N1C && N0.getOpcode() == ISD::AND &&
11348 N0.getOperand(1).getOpcode() == ISD::Constant) {
11349 SDValue N00 = N0.getOperand(0);
11350 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11351 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11352 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11353 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11354 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11355 APInt ShAmt = N1C->getAPIntValue();
11356 Mask = Mask.shl(ShAmt);
11357 if (Mask != 0)
11358 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11359 N00, DAG.getConstant(Mask, VT));
11360 }
11361 }
11362
11363 return SDValue();
11364}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011365
Nate Begeman740ab032009-01-26 00:52:55 +000011366/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11367/// when possible.
11368static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11369 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011370 EVT VT = N->getValueType(0);
11371 if (!VT.isVector() && VT.isInteger() &&
11372 N->getOpcode() == ISD::SHL)
11373 return PerformSHLCombine(N, DAG);
11374
Nate Begeman740ab032009-01-26 00:52:55 +000011375 // On X86 with SSE2 support, we can transform this to a vector shift if
11376 // all elements are shifted by the same amount. We can't do this in legalize
11377 // because the a constant vector is typically transformed to a constant pool
11378 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011379 if (!Subtarget->hasSSE2())
11380 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011381
Owen Anderson825b72b2009-08-11 20:47:22 +000011382 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011383 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011384
Mon P Wang3becd092009-01-28 08:12:05 +000011385 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011386 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011387 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011388 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011389 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11390 unsigned NumElts = VT.getVectorNumElements();
11391 unsigned i = 0;
11392 for (; i != NumElts; ++i) {
11393 SDValue Arg = ShAmtOp.getOperand(i);
11394 if (Arg.getOpcode() == ISD::UNDEF) continue;
11395 BaseShAmt = Arg;
11396 break;
11397 }
11398 for (; i != NumElts; ++i) {
11399 SDValue Arg = ShAmtOp.getOperand(i);
11400 if (Arg.getOpcode() == ISD::UNDEF) continue;
11401 if (Arg != BaseShAmt) {
11402 return SDValue();
11403 }
11404 }
11405 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011406 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011407 SDValue InVec = ShAmtOp.getOperand(0);
11408 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11409 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11410 unsigned i = 0;
11411 for (; i != NumElts; ++i) {
11412 SDValue Arg = InVec.getOperand(i);
11413 if (Arg.getOpcode() == ISD::UNDEF) continue;
11414 BaseShAmt = Arg;
11415 break;
11416 }
11417 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11418 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011419 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011420 if (C->getZExtValue() == SplatIdx)
11421 BaseShAmt = InVec.getOperand(1);
11422 }
11423 }
11424 if (BaseShAmt.getNode() == 0)
11425 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11426 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011427 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011428 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011429
Mon P Wangefa42202009-09-03 19:56:25 +000011430 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011431 if (EltVT.bitsGT(MVT::i32))
11432 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11433 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011434 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011435
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011436 // The shift amount is identical so we can do a vector shift.
11437 SDValue ValOp = N->getOperand(0);
11438 switch (N->getOpcode()) {
11439 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011440 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011441 break;
11442 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011443 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011444 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011445 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011446 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011447 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011448 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011449 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011450 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011451 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011452 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011453 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011454 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011455 break;
11456 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011457 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011458 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011459 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011460 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011461 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011462 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011463 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011464 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011465 break;
11466 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011467 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011468 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011469 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011470 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011471 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011472 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011473 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011474 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011475 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011476 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011477 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011478 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011479 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011480 }
11481 return SDValue();
11482}
11483
Nate Begemanb65c1752010-12-17 22:55:37 +000011484
11485static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11486 TargetLowering::DAGCombinerInfo &DCI,
11487 const X86Subtarget *Subtarget) {
11488 if (DCI.isBeforeLegalizeOps())
11489 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011490
Nate Begemanb65c1752010-12-17 22:55:37 +000011491 // Want to form PANDN nodes, in the hopes of then easily combining them with
11492 // OR and AND nodes to form PBLEND/PSIGN.
11493 EVT VT = N->getValueType(0);
11494 if (VT != MVT::v2i64)
11495 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011496
Nate Begemanb65c1752010-12-17 22:55:37 +000011497 SDValue N0 = N->getOperand(0);
11498 SDValue N1 = N->getOperand(1);
11499 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011500
Nate Begemanb65c1752010-12-17 22:55:37 +000011501 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011502 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011503 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11504 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11505
11506 // Check RHS for vnot
11507 if (N1.getOpcode() == ISD::XOR &&
11508 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11509 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011510
Nate Begemanb65c1752010-12-17 22:55:37 +000011511 return SDValue();
11512}
11513
Evan Cheng760d1942010-01-04 21:22:48 +000011514static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011515 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011516 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011517 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011518 return SDValue();
11519
Evan Cheng760d1942010-01-04 21:22:48 +000011520 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011521 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011522 return SDValue();
11523
Evan Cheng760d1942010-01-04 21:22:48 +000011524 SDValue N0 = N->getOperand(0);
11525 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011526
Nate Begemanb65c1752010-12-17 22:55:37 +000011527 // look for psign/blend
11528 if (Subtarget->hasSSSE3()) {
11529 if (VT == MVT::v2i64) {
11530 // Canonicalize pandn to RHS
11531 if (N0.getOpcode() == X86ISD::PANDN)
11532 std::swap(N0, N1);
11533 // or (and (m, x), (pandn m, y))
11534 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11535 SDValue Mask = N1.getOperand(0);
11536 SDValue X = N1.getOperand(1);
11537 SDValue Y;
11538 if (N0.getOperand(0) == Mask)
11539 Y = N0.getOperand(1);
11540 if (N0.getOperand(1) == Mask)
11541 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011542
Nate Begemanb65c1752010-12-17 22:55:37 +000011543 // Check to see if the mask appeared in both the AND and PANDN and
11544 if (!Y.getNode())
11545 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011546
Nate Begemanb65c1752010-12-17 22:55:37 +000011547 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11548 if (Mask.getOpcode() != ISD::BITCAST ||
11549 X.getOpcode() != ISD::BITCAST ||
11550 Y.getOpcode() != ISD::BITCAST)
11551 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011552
Nate Begemanb65c1752010-12-17 22:55:37 +000011553 // Look through mask bitcast.
11554 Mask = Mask.getOperand(0);
11555 EVT MaskVT = Mask.getValueType();
11556
11557 // Validate that the Mask operand is a vector sra node. The sra node
11558 // will be an intrinsic.
11559 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11560 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011561
Nate Begemanb65c1752010-12-17 22:55:37 +000011562 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11563 // there is no psrai.b
11564 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11565 case Intrinsic::x86_sse2_psrai_w:
11566 case Intrinsic::x86_sse2_psrai_d:
11567 break;
11568 default: return SDValue();
11569 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011570
Nate Begemanb65c1752010-12-17 22:55:37 +000011571 // Check that the SRA is all signbits.
11572 SDValue SraC = Mask.getOperand(2);
11573 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11574 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11575 if ((SraAmt + 1) != EltBits)
11576 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011577
Nate Begemanb65c1752010-12-17 22:55:37 +000011578 DebugLoc DL = N->getDebugLoc();
11579
11580 // Now we know we at least have a plendvb with the mask val. See if
11581 // we can form a psignb/w/d.
11582 // psign = x.type == y.type == mask.type && y = sub(0, x);
11583 X = X.getOperand(0);
11584 Y = Y.getOperand(0);
11585 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11586 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11587 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11588 unsigned Opc = 0;
11589 switch (EltBits) {
11590 case 8: Opc = X86ISD::PSIGNB; break;
11591 case 16: Opc = X86ISD::PSIGNW; break;
11592 case 32: Opc = X86ISD::PSIGND; break;
11593 default: break;
11594 }
11595 if (Opc) {
11596 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11597 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11598 }
11599 }
11600 // PBLENDVB only available on SSE 4.1
11601 if (!Subtarget->hasSSE41())
11602 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011603
Nate Begemanb65c1752010-12-17 22:55:37 +000011604 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11605 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11606 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011607 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011608 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11609 }
11610 }
11611 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011612
Nate Begemanb65c1752010-12-17 22:55:37 +000011613 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011614 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11615 std::swap(N0, N1);
11616 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11617 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011618 if (!N0.hasOneUse() || !N1.hasOneUse())
11619 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011620
11621 SDValue ShAmt0 = N0.getOperand(1);
11622 if (ShAmt0.getValueType() != MVT::i8)
11623 return SDValue();
11624 SDValue ShAmt1 = N1.getOperand(1);
11625 if (ShAmt1.getValueType() != MVT::i8)
11626 return SDValue();
11627 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11628 ShAmt0 = ShAmt0.getOperand(0);
11629 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11630 ShAmt1 = ShAmt1.getOperand(0);
11631
11632 DebugLoc DL = N->getDebugLoc();
11633 unsigned Opc = X86ISD::SHLD;
11634 SDValue Op0 = N0.getOperand(0);
11635 SDValue Op1 = N1.getOperand(0);
11636 if (ShAmt0.getOpcode() == ISD::SUB) {
11637 Opc = X86ISD::SHRD;
11638 std::swap(Op0, Op1);
11639 std::swap(ShAmt0, ShAmt1);
11640 }
11641
Evan Cheng8b1190a2010-04-28 01:18:01 +000011642 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011643 if (ShAmt1.getOpcode() == ISD::SUB) {
11644 SDValue Sum = ShAmt1.getOperand(0);
11645 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011646 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11647 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11648 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11649 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011650 return DAG.getNode(Opc, DL, VT,
11651 Op0, Op1,
11652 DAG.getNode(ISD::TRUNCATE, DL,
11653 MVT::i8, ShAmt0));
11654 }
11655 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11656 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11657 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011658 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011659 return DAG.getNode(Opc, DL, VT,
11660 N0.getOperand(0), N1.getOperand(0),
11661 DAG.getNode(ISD::TRUNCATE, DL,
11662 MVT::i8, ShAmt0));
11663 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011664
Evan Cheng760d1942010-01-04 21:22:48 +000011665 return SDValue();
11666}
11667
Chris Lattner149a4e52008-02-22 02:09:43 +000011668/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011669static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011670 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011671 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11672 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011673 // A preferable solution to the general problem is to figure out the right
11674 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011675
11676 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011677 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011678 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011679 if (VT.getSizeInBits() != 64)
11680 return SDValue();
11681
Devang Patel578efa92009-06-05 21:57:13 +000011682 const Function *F = DAG.getMachineFunction().getFunction();
11683 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011684 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011685 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011686 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011687 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011688 isa<LoadSDNode>(St->getValue()) &&
11689 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11690 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011691 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011692 LoadSDNode *Ld = 0;
11693 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011694 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011695 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011696 // Must be a store of a load. We currently handle two cases: the load
11697 // is a direct child, and it's under an intervening TokenFactor. It is
11698 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011699 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011700 Ld = cast<LoadSDNode>(St->getChain());
11701 else if (St->getValue().hasOneUse() &&
11702 ChainVal->getOpcode() == ISD::TokenFactor) {
11703 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011704 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011705 TokenFactorIndex = i;
11706 Ld = cast<LoadSDNode>(St->getValue());
11707 } else
11708 Ops.push_back(ChainVal->getOperand(i));
11709 }
11710 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011711
Evan Cheng536e6672009-03-12 05:59:15 +000011712 if (!Ld || !ISD::isNormalLoad(Ld))
11713 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011714
Evan Cheng536e6672009-03-12 05:59:15 +000011715 // If this is not the MMX case, i.e. we are just turning i64 load/store
11716 // into f64 load/store, avoid the transformation if there are multiple
11717 // uses of the loaded value.
11718 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11719 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011720
Evan Cheng536e6672009-03-12 05:59:15 +000011721 DebugLoc LdDL = Ld->getDebugLoc();
11722 DebugLoc StDL = N->getDebugLoc();
11723 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11724 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11725 // pair instead.
11726 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011727 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011728 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11729 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011730 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011731 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011732 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011733 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011734 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011735 Ops.size());
11736 }
Evan Cheng536e6672009-03-12 05:59:15 +000011737 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011738 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011739 St->isVolatile(), St->isNonTemporal(),
11740 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011741 }
Evan Cheng536e6672009-03-12 05:59:15 +000011742
11743 // Otherwise, lower to two pairs of 32-bit loads / stores.
11744 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011745 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11746 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011747
Owen Anderson825b72b2009-08-11 20:47:22 +000011748 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011749 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011750 Ld->isVolatile(), Ld->isNonTemporal(),
11751 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011752 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011753 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011754 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011755 MinAlign(Ld->getAlignment(), 4));
11756
11757 SDValue NewChain = LoLd.getValue(1);
11758 if (TokenFactorIndex != -1) {
11759 Ops.push_back(LoLd);
11760 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011761 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011762 Ops.size());
11763 }
11764
11765 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011766 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11767 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011768
11769 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011770 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011771 St->isVolatile(), St->isNonTemporal(),
11772 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011773 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011774 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011775 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011776 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011777 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011778 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011779 }
Dan Gohman475871a2008-07-27 21:46:04 +000011780 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011781}
11782
Chris Lattner6cf73262008-01-25 06:14:17 +000011783/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11784/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011785static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011786 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11787 // F[X]OR(0.0, x) -> x
11788 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011789 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11790 if (C->getValueAPF().isPosZero())
11791 return N->getOperand(1);
11792 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11793 if (C->getValueAPF().isPosZero())
11794 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011795 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011796}
11797
11798/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011799static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011800 // FAND(0.0, x) -> 0.0
11801 // FAND(x, 0.0) -> 0.0
11802 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11803 if (C->getValueAPF().isPosZero())
11804 return N->getOperand(0);
11805 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11806 if (C->getValueAPF().isPosZero())
11807 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011808 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011809}
11810
Dan Gohmane5af2d32009-01-29 01:59:02 +000011811static SDValue PerformBTCombine(SDNode *N,
11812 SelectionDAG &DAG,
11813 TargetLowering::DAGCombinerInfo &DCI) {
11814 // BT ignores high bits in the bit index operand.
11815 SDValue Op1 = N->getOperand(1);
11816 if (Op1.hasOneUse()) {
11817 unsigned BitWidth = Op1.getValueSizeInBits();
11818 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11819 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011820 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11821 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011822 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011823 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11824 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11825 DCI.CommitTargetLoweringOpt(TLO);
11826 }
11827 return SDValue();
11828}
Chris Lattner83e6c992006-10-04 06:57:07 +000011829
Eli Friedman7a5e5552009-06-07 06:52:44 +000011830static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11831 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011832 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011833 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011834 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011835 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011836 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011837 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011838 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011839 }
11840 return SDValue();
11841}
11842
Evan Cheng2e489c42009-12-16 00:53:11 +000011843static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11844 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11845 // (and (i32 x86isd::setcc_carry), 1)
11846 // This eliminates the zext. This transformation is necessary because
11847 // ISD::SETCC is always legalized to i8.
11848 DebugLoc dl = N->getDebugLoc();
11849 SDValue N0 = N->getOperand(0);
11850 EVT VT = N->getValueType(0);
11851 if (N0.getOpcode() == ISD::AND &&
11852 N0.hasOneUse() &&
11853 N0.getOperand(0).hasOneUse()) {
11854 SDValue N00 = N0.getOperand(0);
11855 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11856 return SDValue();
11857 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11858 if (!C || C->getZExtValue() != 1)
11859 return SDValue();
11860 return DAG.getNode(ISD::AND, dl, VT,
11861 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11862 N00.getOperand(0), N00.getOperand(1)),
11863 DAG.getConstant(1, VT));
11864 }
11865
11866 return SDValue();
11867}
11868
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011869// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11870static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11871 unsigned X86CC = N->getConstantOperandVal(0);
11872 SDValue EFLAG = N->getOperand(1);
11873 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011874
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011875 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11876 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11877 // cases.
11878 if (X86CC == X86::COND_B)
11879 return DAG.getNode(ISD::AND, DL, MVT::i8,
11880 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11881 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11882 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011883
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011884 return SDValue();
11885}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011886
Chris Lattner23a01992010-12-20 01:37:09 +000011887// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11888static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11889 X86TargetLowering::DAGCombinerInfo &DCI) {
11890 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11891 // the result is either zero or one (depending on the input carry bit).
11892 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11893 if (X86::isZeroNode(N->getOperand(0)) &&
11894 X86::isZeroNode(N->getOperand(1)) &&
11895 // We don't have a good way to replace an EFLAGS use, so only do this when
11896 // dead right now.
11897 SDValue(N, 1).use_empty()) {
11898 DebugLoc DL = N->getDebugLoc();
11899 EVT VT = N->getValueType(0);
11900 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11901 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11902 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11903 DAG.getConstant(X86::COND_B,MVT::i8),
11904 N->getOperand(2)),
11905 DAG.getConstant(1, VT));
11906 return DCI.CombineTo(N, Res1, CarryOut);
11907 }
11908
11909 return SDValue();
11910}
11911
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011912// fold (add Y, (sete X, 0)) -> adc 0, Y
11913// (add Y, (setne X, 0)) -> sbb -1, Y
11914// (sub (sete X, 0), Y) -> sbb 0, Y
11915// (sub (setne X, 0), Y) -> adc -1, Y
11916static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11917 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011918
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011919 // Look through ZExts.
11920 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11921 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11922 return SDValue();
11923
11924 SDValue SetCC = Ext.getOperand(0);
11925 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11926 return SDValue();
11927
11928 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11929 if (CC != X86::COND_E && CC != X86::COND_NE)
11930 return SDValue();
11931
11932 SDValue Cmp = SetCC.getOperand(1);
11933 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011934 !X86::isZeroNode(Cmp.getOperand(1)) ||
11935 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011936 return SDValue();
11937
11938 SDValue CmpOp0 = Cmp.getOperand(0);
11939 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11940 DAG.getConstant(1, CmpOp0.getValueType()));
11941
11942 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11943 if (CC == X86::COND_NE)
11944 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11945 DL, OtherVal.getValueType(), OtherVal,
11946 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11947 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11948 DL, OtherVal.getValueType(), OtherVal,
11949 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11950}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011951
Dan Gohman475871a2008-07-27 21:46:04 +000011952SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011953 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011954 SelectionDAG &DAG = DCI.DAG;
11955 switch (N->getOpcode()) {
11956 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011957 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011958 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011959 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011960 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011961 case ISD::ADD:
11962 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011963 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011964 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011965 case ISD::SHL:
11966 case ISD::SRA:
11967 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011968 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011969 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011970 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011971 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011972 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11973 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011974 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011975 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011976 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011977 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011978 case X86ISD::SHUFPS: // Handle all target specific shuffles
11979 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011980 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011981 case X86ISD::PUNPCKHBW:
11982 case X86ISD::PUNPCKHWD:
11983 case X86ISD::PUNPCKHDQ:
11984 case X86ISD::PUNPCKHQDQ:
11985 case X86ISD::UNPCKHPS:
11986 case X86ISD::UNPCKHPD:
11987 case X86ISD::PUNPCKLBW:
11988 case X86ISD::PUNPCKLWD:
11989 case X86ISD::PUNPCKLDQ:
11990 case X86ISD::PUNPCKLQDQ:
11991 case X86ISD::UNPCKLPS:
11992 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000011993 case X86ISD::VUNPCKLPS:
11994 case X86ISD::VUNPCKLPD:
11995 case X86ISD::VUNPCKLPSY:
11996 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011997 case X86ISD::MOVHLPS:
11998 case X86ISD::MOVLHPS:
11999 case X86ISD::PSHUFD:
12000 case X86ISD::PSHUFHW:
12001 case X86ISD::PSHUFLW:
12002 case X86ISD::MOVSS:
12003 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012004 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012005 }
12006
Dan Gohman475871a2008-07-27 21:46:04 +000012007 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012008}
12009
Evan Chenge5b51ac2010-04-17 06:13:15 +000012010/// isTypeDesirableForOp - Return true if the target has native support for
12011/// the specified value type and it is 'desirable' to use the type for the
12012/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12013/// instruction encodings are longer and some i16 instructions are slow.
12014bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12015 if (!isTypeLegal(VT))
12016 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012017 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012018 return true;
12019
12020 switch (Opc) {
12021 default:
12022 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012023 case ISD::LOAD:
12024 case ISD::SIGN_EXTEND:
12025 case ISD::ZERO_EXTEND:
12026 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012027 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012028 case ISD::SRL:
12029 case ISD::SUB:
12030 case ISD::ADD:
12031 case ISD::MUL:
12032 case ISD::AND:
12033 case ISD::OR:
12034 case ISD::XOR:
12035 return false;
12036 }
12037}
12038
12039/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012040/// beneficial for dag combiner to promote the specified node. If true, it
12041/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012042bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012043 EVT VT = Op.getValueType();
12044 if (VT != MVT::i16)
12045 return false;
12046
Evan Cheng4c26e932010-04-19 19:29:22 +000012047 bool Promote = false;
12048 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012049 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012050 default: break;
12051 case ISD::LOAD: {
12052 LoadSDNode *LD = cast<LoadSDNode>(Op);
12053 // If the non-extending load has a single use and it's not live out, then it
12054 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012055 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12056 Op.hasOneUse()*/) {
12057 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12058 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12059 // The only case where we'd want to promote LOAD (rather then it being
12060 // promoted as an operand is when it's only use is liveout.
12061 if (UI->getOpcode() != ISD::CopyToReg)
12062 return false;
12063 }
12064 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012065 Promote = true;
12066 break;
12067 }
12068 case ISD::SIGN_EXTEND:
12069 case ISD::ZERO_EXTEND:
12070 case ISD::ANY_EXTEND:
12071 Promote = true;
12072 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012073 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012074 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012075 SDValue N0 = Op.getOperand(0);
12076 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012077 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012078 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012079 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012080 break;
12081 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012082 case ISD::ADD:
12083 case ISD::MUL:
12084 case ISD::AND:
12085 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012086 case ISD::XOR:
12087 Commute = true;
12088 // fallthrough
12089 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012090 SDValue N0 = Op.getOperand(0);
12091 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012092 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012093 return false;
12094 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012095 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012096 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012097 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012098 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012099 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012100 }
12101 }
12102
12103 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012104 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012105}
12106
Evan Cheng60c07e12006-07-05 22:17:51 +000012107//===----------------------------------------------------------------------===//
12108// X86 Inline Assembly Support
12109//===----------------------------------------------------------------------===//
12110
Chris Lattnerb8105652009-07-20 17:51:36 +000012111bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12112 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012113
12114 std::string AsmStr = IA->getAsmString();
12115
12116 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012117 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012118 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012119
12120 switch (AsmPieces.size()) {
12121 default: return false;
12122 case 1:
12123 AsmStr = AsmPieces[0];
12124 AsmPieces.clear();
12125 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12126
Evan Cheng55d42002011-01-08 01:24:27 +000012127 // FIXME: this should verify that we are targetting a 486 or better. If not,
12128 // we will turn this bswap into something that will be lowered to logical ops
12129 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12130 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012131 // bswap $0
12132 if (AsmPieces.size() == 2 &&
12133 (AsmPieces[0] == "bswap" ||
12134 AsmPieces[0] == "bswapq" ||
12135 AsmPieces[0] == "bswapl") &&
12136 (AsmPieces[1] == "$0" ||
12137 AsmPieces[1] == "${0:q}")) {
12138 // No need to check constraints, nothing other than the equivalent of
12139 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012140 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12141 if (!Ty || Ty->getBitWidth() % 16 != 0)
12142 return false;
12143 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012144 }
12145 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012146 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012147 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012148 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012149 AsmPieces[1] == "$$8," &&
12150 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012151 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12152 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012153 const std::string &ConstraintsStr = IA->getConstraintString();
12154 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012155 std::sort(AsmPieces.begin(), AsmPieces.end());
12156 if (AsmPieces.size() == 4 &&
12157 AsmPieces[0] == "~{cc}" &&
12158 AsmPieces[1] == "~{dirflag}" &&
12159 AsmPieces[2] == "~{flags}" &&
12160 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012161 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12162 if (!Ty || Ty->getBitWidth() % 16 != 0)
12163 return false;
12164 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012165 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012166 }
12167 break;
12168 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012169 if (CI->getType()->isIntegerTy(32) &&
12170 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12171 SmallVector<StringRef, 4> Words;
12172 SplitString(AsmPieces[0], Words, " \t,");
12173 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12174 Words[2] == "${0:w}") {
12175 Words.clear();
12176 SplitString(AsmPieces[1], Words, " \t,");
12177 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12178 Words[2] == "$0") {
12179 Words.clear();
12180 SplitString(AsmPieces[2], Words, " \t,");
12181 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12182 Words[2] == "${0:w}") {
12183 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012184 const std::string &ConstraintsStr = IA->getConstraintString();
12185 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012186 std::sort(AsmPieces.begin(), AsmPieces.end());
12187 if (AsmPieces.size() == 4 &&
12188 AsmPieces[0] == "~{cc}" &&
12189 AsmPieces[1] == "~{dirflag}" &&
12190 AsmPieces[2] == "~{flags}" &&
12191 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012192 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12193 if (!Ty || Ty->getBitWidth() % 16 != 0)
12194 return false;
12195 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012196 }
12197 }
12198 }
12199 }
12200 }
Evan Cheng55d42002011-01-08 01:24:27 +000012201
12202 if (CI->getType()->isIntegerTy(64)) {
12203 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12204 if (Constraints.size() >= 2 &&
12205 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12206 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12207 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12208 SmallVector<StringRef, 4> Words;
12209 SplitString(AsmPieces[0], Words, " \t");
12210 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012211 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012212 SplitString(AsmPieces[1], Words, " \t");
12213 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12214 Words.clear();
12215 SplitString(AsmPieces[2], Words, " \t,");
12216 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12217 Words[2] == "%edx") {
12218 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12219 if (!Ty || Ty->getBitWidth() % 16 != 0)
12220 return false;
12221 return IntrinsicLowering::LowerToByteSwap(CI);
12222 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012223 }
12224 }
12225 }
12226 }
12227 break;
12228 }
12229 return false;
12230}
12231
12232
12233
Chris Lattnerf4dff842006-07-11 02:54:03 +000012234/// getConstraintType - Given a constraint letter, return the type of
12235/// constraint it is for this target.
12236X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012237X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12238 if (Constraint.size() == 1) {
12239 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012240 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012241 case 'q':
12242 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012243 case 'f':
12244 case 't':
12245 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012246 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012247 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012248 case 'Y':
12249 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012250 case 'a':
12251 case 'b':
12252 case 'c':
12253 case 'd':
12254 case 'S':
12255 case 'D':
12256 case 'A':
12257 return C_Register;
12258 case 'I':
12259 case 'J':
12260 case 'K':
12261 case 'L':
12262 case 'M':
12263 case 'N':
12264 case 'G':
12265 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012266 case 'e':
12267 case 'Z':
12268 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012269 default:
12270 break;
12271 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012272 }
Chris Lattner4234f572007-03-25 02:14:49 +000012273 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012274}
12275
John Thompson44ab89e2010-10-29 17:29:13 +000012276/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012277/// This object must already have been set up with the operand type
12278/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012279TargetLowering::ConstraintWeight
12280 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012281 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012282 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012283 Value *CallOperandVal = info.CallOperandVal;
12284 // If we don't have a value, we can't do a match,
12285 // but allow it at the lowest weight.
12286 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012287 return CW_Default;
12288 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012289 // Look at the constraint type.
12290 switch (*constraint) {
12291 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012292 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12293 case 'R':
12294 case 'q':
12295 case 'Q':
12296 case 'a':
12297 case 'b':
12298 case 'c':
12299 case 'd':
12300 case 'S':
12301 case 'D':
12302 case 'A':
12303 if (CallOperandVal->getType()->isIntegerTy())
12304 weight = CW_SpecificReg;
12305 break;
12306 case 'f':
12307 case 't':
12308 case 'u':
12309 if (type->isFloatingPointTy())
12310 weight = CW_SpecificReg;
12311 break;
12312 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012313 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012314 weight = CW_SpecificReg;
12315 break;
12316 case 'x':
12317 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012318 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012319 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012320 break;
12321 case 'I':
12322 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12323 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012324 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012325 }
12326 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012327 case 'J':
12328 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12329 if (C->getZExtValue() <= 63)
12330 weight = CW_Constant;
12331 }
12332 break;
12333 case 'K':
12334 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12335 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12336 weight = CW_Constant;
12337 }
12338 break;
12339 case 'L':
12340 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12341 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12342 weight = CW_Constant;
12343 }
12344 break;
12345 case 'M':
12346 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12347 if (C->getZExtValue() <= 3)
12348 weight = CW_Constant;
12349 }
12350 break;
12351 case 'N':
12352 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12353 if (C->getZExtValue() <= 0xff)
12354 weight = CW_Constant;
12355 }
12356 break;
12357 case 'G':
12358 case 'C':
12359 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12360 weight = CW_Constant;
12361 }
12362 break;
12363 case 'e':
12364 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12365 if ((C->getSExtValue() >= -0x80000000LL) &&
12366 (C->getSExtValue() <= 0x7fffffffLL))
12367 weight = CW_Constant;
12368 }
12369 break;
12370 case 'Z':
12371 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12372 if (C->getZExtValue() <= 0xffffffff)
12373 weight = CW_Constant;
12374 }
12375 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012376 }
12377 return weight;
12378}
12379
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012380/// LowerXConstraint - try to replace an X constraint, which matches anything,
12381/// with another that has more specific requirements based on the type of the
12382/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012383const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012384LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012385 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12386 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012387 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012388 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012389 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012390 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012391 return "x";
12392 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012393
Chris Lattner5e764232008-04-26 23:02:14 +000012394 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012395}
12396
Chris Lattner48884cd2007-08-25 00:47:38 +000012397/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12398/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012399void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012400 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012401 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012402 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012403 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012404
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012405 switch (Constraint) {
12406 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012407 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012408 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012409 if (C->getZExtValue() <= 31) {
12410 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012411 break;
12412 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012413 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012414 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012415 case 'J':
12416 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012417 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012418 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12419 break;
12420 }
12421 }
12422 return;
12423 case 'K':
12424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012425 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012426 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12427 break;
12428 }
12429 }
12430 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012431 case 'N':
12432 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012433 if (C->getZExtValue() <= 255) {
12434 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012435 break;
12436 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012437 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012438 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012439 case 'e': {
12440 // 32-bit signed value
12441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012442 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12443 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012444 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012445 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012446 break;
12447 }
12448 // FIXME gcc accepts some relocatable values here too, but only in certain
12449 // memory models; it's complicated.
12450 }
12451 return;
12452 }
12453 case 'Z': {
12454 // 32-bit unsigned value
12455 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012456 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12457 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012458 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12459 break;
12460 }
12461 }
12462 // FIXME gcc accepts some relocatable values here too, but only in certain
12463 // memory models; it's complicated.
12464 return;
12465 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012466 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012467 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012468 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012469 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012470 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012471 break;
12472 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012473
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012474 // In any sort of PIC mode addresses need to be computed at runtime by
12475 // adding in a register or some sort of table lookup. These can't
12476 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012477 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012478 return;
12479
Chris Lattnerdc43a882007-05-03 16:52:29 +000012480 // If we are in non-pic codegen mode, we allow the address of a global (with
12481 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012482 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012483 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012484
Chris Lattner49921962009-05-08 18:23:14 +000012485 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12486 while (1) {
12487 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12488 Offset += GA->getOffset();
12489 break;
12490 } else if (Op.getOpcode() == ISD::ADD) {
12491 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12492 Offset += C->getZExtValue();
12493 Op = Op.getOperand(0);
12494 continue;
12495 }
12496 } else if (Op.getOpcode() == ISD::SUB) {
12497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12498 Offset += -C->getZExtValue();
12499 Op = Op.getOperand(0);
12500 continue;
12501 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012502 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012503
Chris Lattner49921962009-05-08 18:23:14 +000012504 // Otherwise, this isn't something we can handle, reject it.
12505 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012506 }
Eric Christopherfd179292009-08-27 18:07:15 +000012507
Dan Gohman46510a72010-04-15 01:51:59 +000012508 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012509 // If we require an extra load to get this address, as in PIC mode, we
12510 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012511 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12512 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012513 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012514
Devang Patel0d881da2010-07-06 22:08:15 +000012515 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12516 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012517 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012518 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012519 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012520
Gabor Greifba36cb52008-08-28 21:40:38 +000012521 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012522 Ops.push_back(Result);
12523 return;
12524 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012525 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012526}
12527
Chris Lattner259e97c2006-01-31 19:43:35 +000012528std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012529getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012530 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012531 if (Constraint.size() == 1) {
12532 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012533 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012534 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012535 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12536 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012537 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012538 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12539 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12540 X86::R10D,X86::R11D,X86::R12D,
12541 X86::R13D,X86::R14D,X86::R15D,
12542 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012543 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012544 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12545 X86::SI, X86::DI, X86::R8W,X86::R9W,
12546 X86::R10W,X86::R11W,X86::R12W,
12547 X86::R13W,X86::R14W,X86::R15W,
12548 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012549 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012550 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12551 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12552 X86::R10B,X86::R11B,X86::R12B,
12553 X86::R13B,X86::R14B,X86::R15B,
12554 X86::BPL, X86::SPL, 0);
12555
Owen Anderson825b72b2009-08-11 20:47:22 +000012556 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012557 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12558 X86::RSI, X86::RDI, X86::R8, X86::R9,
12559 X86::R10, X86::R11, X86::R12,
12560 X86::R13, X86::R14, X86::R15,
12561 X86::RBP, X86::RSP, 0);
12562
12563 break;
12564 }
Eric Christopherfd179292009-08-27 18:07:15 +000012565 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012566 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012567 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012568 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012569 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012570 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012571 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012572 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012573 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012574 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12575 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012576 }
12577 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012578
Chris Lattner1efa40f2006-02-22 00:56:39 +000012579 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012580}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012581
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012582std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012583X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012584 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012585 // First, see if this is a constraint that directly corresponds to an LLVM
12586 // register class.
12587 if (Constraint.size() == 1) {
12588 // GCC Constraint Letters
12589 switch (Constraint[0]) {
12590 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012591 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012592 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012593 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012594 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012595 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012596 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012597 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012598 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012599 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012600 case 'R': // LEGACY_REGS
12601 if (VT == MVT::i8)
12602 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12603 if (VT == MVT::i16)
12604 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12605 if (VT == MVT::i32 || !Subtarget->is64Bit())
12606 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12607 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012608 case 'f': // FP Stack registers.
12609 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12610 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012611 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012612 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012613 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012614 return std::make_pair(0U, X86::RFP64RegisterClass);
12615 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012616 case 'y': // MMX_REGS if MMX allowed.
12617 if (!Subtarget->hasMMX()) break;
12618 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012619 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012620 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012621 // FALL THROUGH.
12622 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012623 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012624
Owen Anderson825b72b2009-08-11 20:47:22 +000012625 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012626 default: break;
12627 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012628 case MVT::f32:
12629 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012630 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012631 case MVT::f64:
12632 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012633 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012634 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012635 case MVT::v16i8:
12636 case MVT::v8i16:
12637 case MVT::v4i32:
12638 case MVT::v2i64:
12639 case MVT::v4f32:
12640 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012641 return std::make_pair(0U, X86::VR128RegisterClass);
12642 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012643 break;
12644 }
12645 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012646
Chris Lattnerf76d1802006-07-31 23:26:50 +000012647 // Use the default implementation in TargetLowering to convert the register
12648 // constraint into a member of a register class.
12649 std::pair<unsigned, const TargetRegisterClass*> Res;
12650 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012651
12652 // Not found as a standard register?
12653 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012654 // Map st(0) -> st(7) -> ST0
12655 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12656 tolower(Constraint[1]) == 's' &&
12657 tolower(Constraint[2]) == 't' &&
12658 Constraint[3] == '(' &&
12659 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12660 Constraint[5] == ')' &&
12661 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012662
Chris Lattner56d77c72009-09-13 22:41:48 +000012663 Res.first = X86::ST0+Constraint[4]-'0';
12664 Res.second = X86::RFP80RegisterClass;
12665 return Res;
12666 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012667
Chris Lattner56d77c72009-09-13 22:41:48 +000012668 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012669 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012670 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012671 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012672 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012673 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012674
12675 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012676 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012677 Res.first = X86::EFLAGS;
12678 Res.second = X86::CCRRegisterClass;
12679 return Res;
12680 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012681
Dale Johannesen330169f2008-11-13 21:52:36 +000012682 // 'A' means EAX + EDX.
12683 if (Constraint == "A") {
12684 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012685 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012686 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012687 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012688 return Res;
12689 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012690
Chris Lattnerf76d1802006-07-31 23:26:50 +000012691 // Otherwise, check to see if this is a register class of the wrong value
12692 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12693 // turn into {ax},{dx}.
12694 if (Res.second->hasType(VT))
12695 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012696
Chris Lattnerf76d1802006-07-31 23:26:50 +000012697 // All of the single-register GCC register classes map their values onto
12698 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12699 // really want an 8-bit or 32-bit register, map to the appropriate register
12700 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012701 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012702 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012703 unsigned DestReg = 0;
12704 switch (Res.first) {
12705 default: break;
12706 case X86::AX: DestReg = X86::AL; break;
12707 case X86::DX: DestReg = X86::DL; break;
12708 case X86::CX: DestReg = X86::CL; break;
12709 case X86::BX: DestReg = X86::BL; break;
12710 }
12711 if (DestReg) {
12712 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012713 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012714 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012715 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012716 unsigned DestReg = 0;
12717 switch (Res.first) {
12718 default: break;
12719 case X86::AX: DestReg = X86::EAX; break;
12720 case X86::DX: DestReg = X86::EDX; break;
12721 case X86::CX: DestReg = X86::ECX; break;
12722 case X86::BX: DestReg = X86::EBX; break;
12723 case X86::SI: DestReg = X86::ESI; break;
12724 case X86::DI: DestReg = X86::EDI; break;
12725 case X86::BP: DestReg = X86::EBP; break;
12726 case X86::SP: DestReg = X86::ESP; break;
12727 }
12728 if (DestReg) {
12729 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012730 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012731 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012732 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012733 unsigned DestReg = 0;
12734 switch (Res.first) {
12735 default: break;
12736 case X86::AX: DestReg = X86::RAX; break;
12737 case X86::DX: DestReg = X86::RDX; break;
12738 case X86::CX: DestReg = X86::RCX; break;
12739 case X86::BX: DestReg = X86::RBX; break;
12740 case X86::SI: DestReg = X86::RSI; break;
12741 case X86::DI: DestReg = X86::RDI; break;
12742 case X86::BP: DestReg = X86::RBP; break;
12743 case X86::SP: DestReg = X86::RSP; break;
12744 }
12745 if (DestReg) {
12746 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012747 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012748 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012749 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012750 } else if (Res.second == X86::FR32RegisterClass ||
12751 Res.second == X86::FR64RegisterClass ||
12752 Res.second == X86::VR128RegisterClass) {
12753 // Handle references to XMM physical registers that got mapped into the
12754 // wrong class. This can happen with constraints like {xmm0} where the
12755 // target independent register mapper will just pick the first match it can
12756 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012757 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012758 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012759 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012760 Res.second = X86::FR64RegisterClass;
12761 else if (X86::VR128RegisterClass->hasType(VT))
12762 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012763 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012764
Chris Lattnerf76d1802006-07-31 23:26:50 +000012765 return Res;
12766}