blob: 9ce0046f12b854ab21a416dba6fec89c65e0ec7b [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.
Duncan Sands03228082008-11-23 15:47:28 +0000223 setBooleanContents(ZeroOrOneBooleanContent);
Eric Christopher7c2cdb12011-03-08 02:42:25 +0000224 setSchedulingPreference(Sched::ILP);
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000226
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000227 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000228 // Setup Windows compiler runtime calls.
229 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000230 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
231 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000232 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000233 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000234 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000235 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
236 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000237 }
238
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000239 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000240 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000241 setUseUnderscoreSetJmp(false);
242 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000243 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000244 // MS runtime is weird: it exports _setjmp, but longjmp!
245 setUseUnderscoreSetJmp(true);
246 setUseUnderscoreLongJmp(false);
247 } else {
248 setUseUnderscoreSetJmp(true);
249 setUseUnderscoreLongJmp(true);
250 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000251
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000252 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000254 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000260
Scott Michelfdc40a02009-02-17 22:15:04 +0000261 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000263 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000265 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
267 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000268
269 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
271 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
272 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
273 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
274 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
275 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000276
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000277 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
278 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
280 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
281 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000282
Evan Cheng25ab6902006-09-08 06:48:29 +0000283 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
285 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000286 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000287 // We have an algorithm for SSE2->double, and we turn this into a
288 // 64-bit FILD followed by conditional FADD for other targets.
289 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000290 // We have an algorithm for SSE2, and we turn this into a 64-bit
291 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000292 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294
295 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
296 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
298 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000299
Devang Patel6a784892009-06-05 18:48:29 +0000300 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000301 // SSE has no i16 to fp conversion, only i32
302 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000304 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
308 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000309 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000310 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
312 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000313 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000314
Dale Johannesen73328d12007-09-19 23:55:34 +0000315 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
316 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
318 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000319
Evan Cheng02568ff2006-01-30 22:13:22 +0000320 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
321 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
323 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000324
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000325 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000327 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000329 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
331 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000332 }
333
334 // Handle FP_TO_UINT by promoting the destination to a larger signed
335 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
337 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
338 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339
Evan Cheng25ab6902006-09-08 06:48:29 +0000340 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
342 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000343 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000344 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000345 // Expand FP_TO_UINT into a select.
346 // FIXME: We would like to use a Custom expander here eventually to do
347 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000349 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000350 // With SSE3 we can use fisttpll to convert to a signed i64; without
351 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000353 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000354
Chris Lattner399610a2006-12-05 18:22:22 +0000355 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000356 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000357 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
358 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000359 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000360 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000361 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000362 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000363 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000364 }
Chris Lattner21f66852005-12-23 05:15:23 +0000365
Dan Gohmanb00ee212008-02-18 19:34:53 +0000366 // Scalar integer divide and remainder are lowered to use operations that
367 // produce two results, to match the available instructions. This exposes
368 // the two-result form to trivial CSE, which is able to combine x/y and x%y
369 // into a single instruction.
370 //
371 // Scalar integer multiply-high is also lowered to use two-result
372 // operations, to match the available instructions. However, plain multiply
373 // (low) operations are left as Legal, as there are single-result
374 // instructions for this in x86. Using the two-result multiply instructions
375 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000376 for (unsigned i = 0, e = 4; i != e; ++i) {
377 MVT VT = IntVTs[i];
378 setOperationAction(ISD::MULHS, VT, Expand);
379 setOperationAction(ISD::MULHU, VT, Expand);
380 setOperationAction(ISD::SDIV, VT, Expand);
381 setOperationAction(ISD::UDIV, VT, Expand);
382 setOperationAction(ISD::SREM, VT, Expand);
383 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000384
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000385 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000386 setOperationAction(ISD::ADDC, VT, Custom);
387 setOperationAction(ISD::ADDE, VT, Custom);
388 setOperationAction(ISD::SUBC, VT, Custom);
389 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000390 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000391
Owen Anderson825b72b2009-08-11 20:47:22 +0000392 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
393 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
394 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
395 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000396 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
398 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
399 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
400 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
401 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
402 setOperationAction(ISD::FREM , MVT::f32 , Expand);
403 setOperationAction(ISD::FREM , MVT::f64 , Expand);
404 setOperationAction(ISD::FREM , MVT::f80 , Expand);
405 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000406
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
408 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000409 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
410 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
412 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000413 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
415 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000416 }
417
Benjamin Kramer1292c222010-12-04 20:32:23 +0000418 if (Subtarget->hasPOPCNT()) {
419 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
420 } else {
421 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
422 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
423 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
424 if (Subtarget->is64Bit())
425 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
426 }
427
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
429 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000430
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000431 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000432 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000433 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000434 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000435 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
437 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
438 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
439 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
440 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000441 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
443 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
444 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
445 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000446 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
448 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000449 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000451
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000452 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
454 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
455 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
456 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000457 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
459 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000460 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000461 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
463 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
464 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
465 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000466 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000467 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000468 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
470 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
471 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000472 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
474 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
475 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000476 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000477
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000478 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000480
Eric Christopher9a9d2752010-07-22 02:48:34 +0000481 // We may not have a libcall for MEMBARRIER so we should lower this.
482 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000483
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000484 // On X86 and X86-64, atomic operations are lowered to locked instructions.
485 // Locked instructions, in turn, have implicit fence semantics (all memory
486 // operations are flushed before issuing the locked instruction, and they
487 // are not buffered), so we can fold away the common pattern of
488 // fence-atomic-fence.
489 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000490
Mon P Wang63307c32008-05-05 19:05:59 +0000491 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000492 for (unsigned i = 0, e = 4; i != e; ++i) {
493 MVT VT = IntVTs[i];
494 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
495 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
496 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000497
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000498 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
500 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
501 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
502 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
503 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
505 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000506 }
507
Evan Cheng3c992d22006-03-07 02:02:57 +0000508 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000509 if (!Subtarget->isTargetDarwin() &&
510 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000511 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000513 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000514
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
516 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
517 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
518 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000519 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000520 setExceptionPointerRegister(X86::RAX);
521 setExceptionSelectorRegister(X86::RDX);
522 } else {
523 setExceptionPointerRegister(X86::EAX);
524 setExceptionSelectorRegister(X86::EDX);
525 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
527 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000528
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000530
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000532
Nate Begemanacc398c2006-01-25 18:21:52 +0000533 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::VASTART , MVT::Other, Custom);
535 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000536 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::VAARG , MVT::Other, Custom);
538 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000539 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::VAARG , MVT::Other, Expand);
541 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000542 }
Evan Chengae642192007-03-02 23:16:35 +0000543
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
545 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000546 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000548 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000550 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000552
Evan Chengc7ce29b2009-02-13 22:36:38 +0000553 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000554 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000555 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
557 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000558
Evan Cheng223547a2006-01-31 22:28:30 +0000559 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::FABS , MVT::f64, Custom);
561 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000562
563 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setOperationAction(ISD::FNEG , MVT::f64, Custom);
565 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000566
Evan Cheng68c47cb2007-01-05 07:55:56 +0000567 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
569 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000570
Evan Chengd25e9e82006-02-02 00:28:23 +0000571 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::FSIN , MVT::f64, Expand);
573 setOperationAction(ISD::FCOS , MVT::f64, Expand);
574 setOperationAction(ISD::FSIN , MVT::f32, Expand);
575 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000576
Chris Lattnera54aa942006-01-29 06:26:08 +0000577 // Expand FP immediates into loads from the stack, except for the special
578 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000579 addLegalFPImmediate(APFloat(+0.0)); // xorpd
580 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000581 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000582 // Use SSE for f32, x87 for f64.
583 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
585 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000586
587 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000589
590 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000592
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000594
595 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
597 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000598
599 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::FSIN , MVT::f32, Expand);
601 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000602
Nate Begemane1795842008-02-14 08:57:00 +0000603 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000604 addLegalFPImmediate(APFloat(+0.0f)); // xorps
605 addLegalFPImmediate(APFloat(+0.0)); // FLD0
606 addLegalFPImmediate(APFloat(+1.0)); // FLD1
607 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
608 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
609
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
612 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000614 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000615 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000616 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
618 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000619
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
621 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
623 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000624
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000625 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
627 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000628 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000629 addLegalFPImmediate(APFloat(+0.0)); // FLD0
630 addLegalFPImmediate(APFloat(+1.0)); // FLD1
631 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
632 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000633 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
634 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
635 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
636 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000637 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000638
Dale Johannesen59a58732007-08-05 18:49:15 +0000639 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000640 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
642 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
643 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000644 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000645 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000646 addLegalFPImmediate(TmpFlt); // FLD0
647 TmpFlt.changeSign();
648 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000649
650 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000651 APFloat TmpFlt2(+1.0);
652 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
653 &ignored);
654 addLegalFPImmediate(TmpFlt2); // FLD1
655 TmpFlt2.changeSign();
656 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
657 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000658
Evan Chengc7ce29b2009-02-13 22:36:38 +0000659 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
661 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000662 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000663 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000664
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000665 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
667 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
668 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FLOG, MVT::f80, Expand);
671 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
672 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
673 setOperationAction(ISD::FEXP, MVT::f80, Expand);
674 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000675
Mon P Wangf007a8b2008-11-06 05:31:54 +0000676 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000677 // (for widening) or expand (for scalarization). Then we will selectively
678 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
680 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
681 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
682 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
683 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
684 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
685 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
686 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
687 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
688 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
689 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
690 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
691 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
692 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
693 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
694 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000697 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
698 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
700 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
701 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
702 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
703 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
704 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
705 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
706 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
707 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
708 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
709 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
710 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
711 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
712 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
713 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
714 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
715 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
716 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000730 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000731 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
733 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
734 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
735 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
736 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
737 setTruncStoreAction((MVT::SimpleValueType)VT,
738 (MVT::SimpleValueType)InnerVT, Expand);
739 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
740 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
741 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000742 }
743
Evan Chengc7ce29b2009-02-13 22:36:38 +0000744 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
745 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000746 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000747 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000748 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000749 }
750
Dale Johannesen0488fb62010-09-30 23:57:10 +0000751 // MMX-sized vectors (other than x86mmx) are expected to be expanded
752 // into smaller operations.
753 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
754 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
755 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
756 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
757 setOperationAction(ISD::AND, MVT::v8i8, Expand);
758 setOperationAction(ISD::AND, MVT::v4i16, Expand);
759 setOperationAction(ISD::AND, MVT::v2i32, Expand);
760 setOperationAction(ISD::AND, MVT::v1i64, Expand);
761 setOperationAction(ISD::OR, MVT::v8i8, Expand);
762 setOperationAction(ISD::OR, MVT::v4i16, Expand);
763 setOperationAction(ISD::OR, MVT::v2i32, Expand);
764 setOperationAction(ISD::OR, MVT::v1i64, Expand);
765 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
766 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
767 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
768 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
769 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
770 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
771 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
772 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
773 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
774 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
775 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
776 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
777 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000778 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
779 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
780 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
781 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000782
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000783 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000785
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
787 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
788 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
789 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
790 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
791 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
792 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
793 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
794 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
796 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
797 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000798 }
799
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000800 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000802
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000803 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
804 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
806 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
807 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
808 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000809
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
811 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
812 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
813 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
814 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
815 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
816 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
817 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
818 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
819 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
820 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
821 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
822 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
823 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
824 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
825 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000826
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
828 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
829 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
830 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000831
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
833 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
834 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
836 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000837
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000838 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
839 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
840 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
841 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
842 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
843
Evan Cheng2c3ae372006-04-12 21:21:57 +0000844 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
846 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000847 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000848 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000849 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000850 // Do not attempt to custom lower non-128-bit vectors
851 if (!VT.is128BitVector())
852 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 setOperationAction(ISD::BUILD_VECTOR,
854 VT.getSimpleVT().SimpleTy, Custom);
855 setOperationAction(ISD::VECTOR_SHUFFLE,
856 VT.getSimpleVT().SimpleTy, Custom);
857 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
858 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000859 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000860
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
862 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
863 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
864 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
865 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
866 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000867
Nate Begemancdd1eec2008-02-12 22:51:28 +0000868 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
870 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000871 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000872
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000873 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
875 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000876 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000877
878 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000879 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000880 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000881
Owen Andersond6662ad2009-08-10 20:46:15 +0000882 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000884 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000886 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000888 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000890 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000892 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000895
Evan Cheng2c3ae372006-04-12 21:21:57 +0000896 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
898 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
899 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
900 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
903 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000904 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000905
Nate Begeman14d12ca2008-02-11 04:19:36 +0000906 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000907 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
908 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
909 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
910 setOperationAction(ISD::FRINT, MVT::f32, Legal);
911 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
912 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
913 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
914 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
915 setOperationAction(ISD::FRINT, MVT::f64, Legal);
916 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
917
Nate Begeman14d12ca2008-02-11 04:19:36 +0000918 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000920
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000921 // Can turn SHL into an integer multiply.
922 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000923 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000924
Nate Begeman14d12ca2008-02-11 04:19:36 +0000925 // i8 and i16 vectors are custom , because the source register and source
926 // source memory operand types are not the same width. f32 vectors are
927 // custom since the immediate controlling the insert encodes additional
928 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
931 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
932 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000933
Owen Anderson825b72b2009-08-11 20:47:22 +0000934 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
935 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000938
939 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000942 }
943 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000944
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000945 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000947
David Greene9b9838d2009-06-29 16:47:10 +0000948 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
950 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
951 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
952 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000953 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000954
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
956 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
957 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
958 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000959
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
961 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
962 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
963 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
964 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
965 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000966
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
968 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
969 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
970 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
971 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
972 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000973
David Greene54d8eba2011-01-27 22:38:56 +0000974 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
975 // insert_vector_elt extract_subvector and extract_vector_elt for
976 // 256-bit types.
977 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
978 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
979 ++i) {
980 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
981 // Do not attempt to custom lower non-256-bit vectors
982 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
983 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000984 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000985 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
986 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000987 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000988 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000989 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000990 }
David Greene54d8eba2011-01-27 22:38:56 +0000991 // Custom-lower insert_subvector and extract_subvector based on
992 // the result type.
993 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
994 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
995 ++i) {
996 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
997 // Do not attempt to custom lower non-256-bit vectors
998 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +0000999 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001000
1001 if (MVT(VT).getSizeInBits() == 128) {
1002 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001003 }
David Greene54d8eba2011-01-27 22:38:56 +00001004 else if (MVT(VT).getSizeInBits() == 256) {
1005 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1006 }
David Greene9b9838d2009-06-29 16:47:10 +00001007 }
1008
David Greene54d8eba2011-01-27 22:38:56 +00001009 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1010 // Don't promote loads because we need them for VPERM vector index versions.
1011
1012 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1013 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
1014 VT++) {
1015 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
1016 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
1017 continue;
1018 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
1019 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
1020 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
1021 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
1022 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
1023 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
1024 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
1025 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
1026 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
1027 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
1028 }
David Greene9b9838d2009-06-29 16:47:10 +00001029 }
1030
Evan Cheng6be2c582006-04-05 23:38:46 +00001031 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001033
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001034
Eli Friedman962f5492010-06-02 19:35:46 +00001035 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1036 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001037 //
Eli Friedman962f5492010-06-02 19:35:46 +00001038 // FIXME: We really should do custom legalization for addition and
1039 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1040 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001041 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1042 // Add/Sub/Mul with overflow operations are custom lowered.
1043 MVT VT = IntVTs[i];
1044 setOperationAction(ISD::SADDO, VT, Custom);
1045 setOperationAction(ISD::UADDO, VT, Custom);
1046 setOperationAction(ISD::SSUBO, VT, Custom);
1047 setOperationAction(ISD::USUBO, VT, Custom);
1048 setOperationAction(ISD::SMULO, VT, Custom);
1049 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001050 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001051
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001052 // There are no 8-bit 3-address imul/mul instructions
1053 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1054 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001055
Evan Chengd54f2d52009-03-31 19:38:51 +00001056 if (!Subtarget->is64Bit()) {
1057 // These libcalls are not available in 32-bit.
1058 setLibcallName(RTLIB::SHL_I128, 0);
1059 setLibcallName(RTLIB::SRL_I128, 0);
1060 setLibcallName(RTLIB::SRA_I128, 0);
1061 }
1062
Evan Cheng206ee9d2006-07-07 08:33:52 +00001063 // We have target-specific dag combine patterns for the following nodes:
1064 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001065 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +00001066 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001067 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001068 setTargetDAGCombine(ISD::SHL);
1069 setTargetDAGCombine(ISD::SRA);
1070 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001071 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001072 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001073 setTargetDAGCombine(ISD::ADD);
1074 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +00001075 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001076 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001077 if (Subtarget->is64Bit())
1078 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001079
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001080 computeRegisterProperties();
1081
Evan Cheng05219282011-01-06 06:52:41 +00001082 // On Darwin, -Os means optimize for size without hurting performance,
1083 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001084 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001085 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001086 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001087 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1088 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1089 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +00001090 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001091 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001092}
1093
Scott Michel5b8f82e2008-03-10 15:42:14 +00001094
Owen Anderson825b72b2009-08-11 20:47:22 +00001095MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1096 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001097}
1098
1099
Evan Cheng29286502008-01-23 23:17:41 +00001100/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1101/// the desired ByVal argument alignment.
1102static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1103 if (MaxAlign == 16)
1104 return;
1105 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1106 if (VTy->getBitWidth() == 128)
1107 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001108 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1109 unsigned EltAlign = 0;
1110 getMaxByValAlign(ATy->getElementType(), EltAlign);
1111 if (EltAlign > MaxAlign)
1112 MaxAlign = EltAlign;
1113 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1114 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1115 unsigned EltAlign = 0;
1116 getMaxByValAlign(STy->getElementType(i), EltAlign);
1117 if (EltAlign > MaxAlign)
1118 MaxAlign = EltAlign;
1119 if (MaxAlign == 16)
1120 break;
1121 }
1122 }
1123 return;
1124}
1125
1126/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1127/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001128/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1129/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001130unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001131 if (Subtarget->is64Bit()) {
1132 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001133 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001134 if (TyAlign > 8)
1135 return TyAlign;
1136 return 8;
1137 }
1138
Evan Cheng29286502008-01-23 23:17:41 +00001139 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001140 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001141 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001142 return Align;
1143}
Chris Lattner2b02a442007-02-25 08:29:00 +00001144
Evan Chengf0df0312008-05-15 08:39:06 +00001145/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001146/// and store operations as a result of memset, memcpy, and memmove
1147/// lowering. If DstAlign is zero that means it's safe to destination
1148/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1149/// means there isn't a need to check it against alignment requirement,
1150/// probably because the source does not need to be loaded. If
1151/// 'NonScalarIntSafe' is true, that means it's safe to return a
1152/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1153/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1154/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001155/// It returns EVT::Other if the type should be determined using generic
1156/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001157EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001158X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1159 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001160 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001161 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001162 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001163 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1164 // linux. This is because the stack realignment code can't handle certain
1165 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001166 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001167 if (NonScalarIntSafe &&
1168 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001169 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001170 (Subtarget->isUnalignedMemAccessFast() ||
1171 ((DstAlign == 0 || DstAlign >= 16) &&
1172 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001173 Subtarget->getStackAlignment() >= 16) {
1174 if (Subtarget->hasSSE2())
1175 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001176 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001177 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001178 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001179 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001180 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001181 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001182 // Do not use f64 to lower memcpy if source is string constant. It's
1183 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001184 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001185 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001186 }
Evan Chengf0df0312008-05-15 08:39:06 +00001187 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001188 return MVT::i64;
1189 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001190}
1191
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001192/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1193/// current function. The returned value is a member of the
1194/// MachineJumpTableInfo::JTEntryKind enum.
1195unsigned X86TargetLowering::getJumpTableEncoding() const {
1196 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1197 // symbol.
1198 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1199 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001200 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001201
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001202 // Otherwise, use the normal jump table encoding heuristics.
1203 return TargetLowering::getJumpTableEncoding();
1204}
1205
Chris Lattnerc64daab2010-01-26 05:02:42 +00001206const MCExpr *
1207X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1208 const MachineBasicBlock *MBB,
1209 unsigned uid,MCContext &Ctx) const{
1210 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1211 Subtarget->isPICStyleGOT());
1212 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1213 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001214 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1215 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001216}
1217
Evan Chengcc415862007-11-09 01:32:10 +00001218/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1219/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001220SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001221 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001222 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001223 // This doesn't have DebugLoc associated with it, but is not really the
1224 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001225 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001226 return Table;
1227}
1228
Chris Lattner589c6f62010-01-26 06:28:43 +00001229/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1230/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1231/// MCExpr.
1232const MCExpr *X86TargetLowering::
1233getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1234 MCContext &Ctx) const {
1235 // X86-64 uses RIP relative addressing based on the jump table label.
1236 if (Subtarget->isPICStyleRIPRel())
1237 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1238
1239 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001240 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001241}
1242
Bill Wendlingb4202b82009-07-01 18:50:55 +00001243/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001244unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001245 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001246}
1247
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001248// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001249std::pair<const TargetRegisterClass*, uint8_t>
1250X86TargetLowering::findRepresentativeClass(EVT VT) const{
1251 const TargetRegisterClass *RRC = 0;
1252 uint8_t Cost = 1;
1253 switch (VT.getSimpleVT().SimpleTy) {
1254 default:
1255 return TargetLowering::findRepresentativeClass(VT);
1256 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1257 RRC = (Subtarget->is64Bit()
1258 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1259 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001260 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001261 RRC = X86::VR64RegisterClass;
1262 break;
1263 case MVT::f32: case MVT::f64:
1264 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1265 case MVT::v4f32: case MVT::v2f64:
1266 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1267 case MVT::v4f64:
1268 RRC = X86::VR128RegisterClass;
1269 break;
1270 }
1271 return std::make_pair(RRC, Cost);
1272}
1273
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001274bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1275 unsigned &Offset) const {
1276 if (!Subtarget->isTargetLinux())
1277 return false;
1278
1279 if (Subtarget->is64Bit()) {
1280 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1281 Offset = 0x28;
1282 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1283 AddressSpace = 256;
1284 else
1285 AddressSpace = 257;
1286 } else {
1287 // %gs:0x14 on i386
1288 Offset = 0x14;
1289 AddressSpace = 256;
1290 }
1291 return true;
1292}
1293
1294
Chris Lattner2b02a442007-02-25 08:29:00 +00001295//===----------------------------------------------------------------------===//
1296// Return Value Calling Convention Implementation
1297//===----------------------------------------------------------------------===//
1298
Chris Lattner59ed56b2007-02-28 04:55:35 +00001299#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001300
Michael J. Spencerec38de22010-10-10 22:04:20 +00001301bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001302X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001303 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001304 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001305 SmallVector<CCValAssign, 16> RVLocs;
1306 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001307 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001308 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001309}
1310
Dan Gohman98ca4f22009-08-05 01:29:28 +00001311SDValue
1312X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001313 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001314 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001315 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001316 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001317 MachineFunction &MF = DAG.getMachineFunction();
1318 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001319
Chris Lattner9774c912007-02-27 05:28:59 +00001320 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1322 RVLocs, *DAG.getContext());
1323 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001324
Evan Chengdcea1632010-02-04 02:40:39 +00001325 // Add the regs to the liveout set for the function.
1326 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1327 for (unsigned i = 0; i != RVLocs.size(); ++i)
1328 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1329 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001330
Dan Gohman475871a2008-07-27 21:46:04 +00001331 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001332
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001334 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1335 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001336 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1337 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001338
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001339 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001340 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1341 CCValAssign &VA = RVLocs[i];
1342 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001343 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001344 EVT ValVT = ValToCopy.getValueType();
1345
Dale Johannesenc4510512010-09-24 19:05:48 +00001346 // If this is x86-64, and we disabled SSE, we can't return FP values,
1347 // or SSE or MMX vectors.
1348 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1349 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001350 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001351 report_fatal_error("SSE register return with SSE disabled");
1352 }
1353 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1354 // llvm-gcc has never done it right and no one has noticed, so this
1355 // should be OK for now.
1356 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001357 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001358 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001359
Chris Lattner447ff682008-03-11 03:23:40 +00001360 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1361 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001362 if (VA.getLocReg() == X86::ST0 ||
1363 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001364 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1365 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001366 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001368 RetOps.push_back(ValToCopy);
1369 // Don't emit a copytoreg.
1370 continue;
1371 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001372
Evan Cheng242b38b2009-02-23 09:03:22 +00001373 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1374 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001375 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001376 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001377 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001379 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1380 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001381 // If we don't have SSE2 available, convert to v4f32 so the generated
1382 // register is legal.
1383 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001384 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001385 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001386 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001387 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001388
Dale Johannesendd64c412009-02-04 00:33:20 +00001389 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001390 Flag = Chain.getValue(1);
1391 }
Dan Gohman61a92132008-04-21 23:59:07 +00001392
1393 // The x86-64 ABI for returning structs by value requires that we copy
1394 // the sret argument into %rax for the return. We saved the argument into
1395 // a virtual register in the entry block, so now we copy the value out
1396 // and into %rax.
1397 if (Subtarget->is64Bit() &&
1398 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1399 MachineFunction &MF = DAG.getMachineFunction();
1400 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1401 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001402 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001403 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001404 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001405
Dale Johannesendd64c412009-02-04 00:33:20 +00001406 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001407 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001408
1409 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001410 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001411 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001412
Chris Lattner447ff682008-03-11 03:23:40 +00001413 RetOps[0] = Chain; // Update chain.
1414
1415 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001416 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001417 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
1419 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001421}
1422
Evan Cheng3d2125c2010-11-30 23:55:39 +00001423bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1424 if (N->getNumValues() != 1)
1425 return false;
1426 if (!N->hasNUsesOfValue(1, 0))
1427 return false;
1428
1429 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001430 if (Copy->getOpcode() != ISD::CopyToReg &&
1431 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001432 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001433
1434 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001435 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001436 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001437 if (UI->getOpcode() != X86ISD::RET_FLAG)
1438 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001439 HasRet = true;
1440 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001441
Evan Cheng1bf891a2010-12-01 22:59:46 +00001442 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001443}
1444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445/// LowerCallResult - Lower the result values of a call into the
1446/// appropriate copies out of appropriate physical registers.
1447///
1448SDValue
1449X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001450 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451 const SmallVectorImpl<ISD::InputArg> &Ins,
1452 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001453 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001454
Chris Lattnere32bbf62007-02-28 07:09:55 +00001455 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001456 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001457 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001459 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001460 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Chris Lattner3085e152007-02-25 08:59:22 +00001462 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001463 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001464 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001465 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001466
Torok Edwin3f142c32009-02-01 18:15:56 +00001467 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001469 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001470 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001471 }
1472
Evan Cheng79fb3b42009-02-20 20:43:02 +00001473 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001474
1475 // If this is a call to a function that returns an fp value on the floating
1476 // point stack, we must guarantee the the value is popped from the stack, so
1477 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1478 // if the return value is not used. We use the FpGET_ST0 instructions
1479 // instead.
1480 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1481 // If we prefer to use the value in xmm registers, copy it out as f80 and
1482 // use a truncate to move it from fp stack reg to xmm reg.
1483 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1484 bool isST0 = VA.getLocReg() == X86::ST0;
1485 unsigned Opc = 0;
1486 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1487 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1488 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1489 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001490 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001491 Ops, 2), 1);
1492 Val = Chain.getValue(0);
1493
1494 // Round the f80 to the right size, which also moves it to the appropriate
1495 // xmm register.
1496 if (CopyVT != VA.getValVT())
1497 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1498 // This truncation won't change the value.
1499 DAG.getIntPtrConstant(1));
1500 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001501 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1502 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1503 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001504 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001505 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001506 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1507 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001508 } else {
1509 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001511 Val = Chain.getValue(0);
1512 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001513 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001514 } else {
1515 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1516 CopyVT, InFlag).getValue(1);
1517 Val = Chain.getValue(0);
1518 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001519 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001520 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001521 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001522
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001524}
1525
1526
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001527//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001528// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001529//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001530// StdCall calling convention seems to be standard for many Windows' API
1531// routines and around. It differs from C calling convention just a little:
1532// callee should clean up the stack, not caller. Symbols should be also
1533// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001534// For info on fast calling convention see Fast Calling Convention (tail call)
1535// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001536
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001538/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1540 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001541 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001542
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001544}
1545
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001546/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001547/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001548static bool
1549ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1550 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001551 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001552
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001554}
1555
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001556/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1557/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001558/// the specific parameter attribute. The copy will be passed as a byval
1559/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001560static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001561CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001562 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1563 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001564 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001565
Dale Johannesendd64c412009-02-04 00:33:20 +00001566 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001567 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001568 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001569}
1570
Chris Lattner29689432010-03-11 00:22:57 +00001571/// IsTailCallConvention - Return true if the calling convention is one that
1572/// supports tail call optimization.
1573static bool IsTailCallConvention(CallingConv::ID CC) {
1574 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1575}
1576
Evan Cheng0c439eb2010-01-27 00:07:07 +00001577/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1578/// a tailcall target by changing its ABI.
1579static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001580 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001581}
1582
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583SDValue
1584X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001585 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001586 const SmallVectorImpl<ISD::InputArg> &Ins,
1587 DebugLoc dl, SelectionDAG &DAG,
1588 const CCValAssign &VA,
1589 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001590 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001591 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001593 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001594 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001595 EVT ValVT;
1596
1597 // If value is passed by pointer we have address passed instead of the value
1598 // itself.
1599 if (VA.getLocInfo() == CCValAssign::Indirect)
1600 ValVT = VA.getLocVT();
1601 else
1602 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001603
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001604 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001605 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001606 // In case of tail call optimization mark all arguments mutable. Since they
1607 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001608 if (Flags.isByVal()) {
1609 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001610 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001611 return DAG.getFrameIndex(FI, getPointerTy());
1612 } else {
1613 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001614 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001615 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1616 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001617 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001618 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001619 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001620}
1621
Dan Gohman475871a2008-07-27 21:46:04 +00001622SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001623X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001624 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 bool isVarArg,
1626 const SmallVectorImpl<ISD::InputArg> &Ins,
1627 DebugLoc dl,
1628 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001629 SmallVectorImpl<SDValue> &InVals)
1630 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001631 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001632 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001633
Gordon Henriksen86737662008-01-05 16:56:59 +00001634 const Function* Fn = MF.getFunction();
1635 if (Fn->hasExternalLinkage() &&
1636 Subtarget->isTargetCygMing() &&
1637 Fn->getName() == "main")
1638 FuncInfo->setForceFramePointer(true);
1639
Evan Cheng1bc78042006-04-26 01:20:17 +00001640 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001641 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001643
Chris Lattner29689432010-03-11 00:22:57 +00001644 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1645 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001646
Chris Lattner638402b2007-02-28 07:00:42 +00001647 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001648 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1650 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001651
1652 // Allocate shadow area for Win64
1653 if (IsWin64) {
1654 CCInfo.AllocateStack(32, 8);
1655 }
1656
Duncan Sands45907662010-10-31 13:21:44 +00001657 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001658
Chris Lattnerf39f7712007-02-28 05:46:49 +00001659 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001660 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001661 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1662 CCValAssign &VA = ArgLocs[i];
1663 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1664 // places.
1665 assert(VA.getValNo() != LastVal &&
1666 "Don't support value assigned to multiple locs yet");
1667 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001668
Chris Lattnerf39f7712007-02-28 05:46:49 +00001669 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001670 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001671 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001673 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001679 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001680 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1681 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001682 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001683 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001684 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001685 RC = X86::VR64RegisterClass;
1686 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001687 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001688
Devang Patel68e6bee2011-02-21 23:21:26 +00001689 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001690 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001691
Chris Lattnerf39f7712007-02-28 05:46:49 +00001692 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1693 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1694 // right size.
1695 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001696 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001697 DAG.getValueType(VA.getValVT()));
1698 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001699 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001700 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001701 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001702 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001703
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001704 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001705 // Handle MMX values passed in XMM regs.
1706 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001707 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1708 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001709 } else
1710 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001711 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001712 } else {
1713 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001715 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001716
1717 // If value is passed via pointer - do a load.
1718 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001719 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1720 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001721
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001723 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001724
Dan Gohman61a92132008-04-21 23:59:07 +00001725 // The x86-64 ABI for returning structs by value requires that we copy
1726 // the sret argument into %rax for the return. Save the argument into
1727 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001728 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001729 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1730 unsigned Reg = FuncInfo->getSRetReturnReg();
1731 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001733 FuncInfo->setSRetReturnReg(Reg);
1734 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001736 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001737 }
1738
Chris Lattnerf39f7712007-02-28 05:46:49 +00001739 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001740 // Align stack specially for tail calls.
1741 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001742 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001743
Evan Cheng1bc78042006-04-26 01:20:17 +00001744 // If the function takes variable number of arguments, make a frame index for
1745 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001746 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001747 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1748 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001749 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 }
1751 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001752 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1753
1754 // FIXME: We should really autogenerate these arrays
1755 static const unsigned GPR64ArgRegsWin64[] = {
1756 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001758 static const unsigned GPR64ArgRegs64Bit[] = {
1759 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1760 };
1761 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001762 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1763 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1764 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001765 const unsigned *GPR64ArgRegs;
1766 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001767
1768 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001769 // The XMM registers which might contain var arg parameters are shadowed
1770 // in their paired GPR. So we only need to save the GPR to their home
1771 // slots.
1772 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001773 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001774 } else {
1775 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1776 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001777
1778 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001779 }
1780 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1781 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001782
Devang Patel578efa92009-06-05 21:57:13 +00001783 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001784 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001785 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001786 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001787 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001788 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001789 // Kernel mode asks for SSE to be disabled, so don't push them
1790 // on the stack.
1791 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001792
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001793 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001794 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001795 // Get to the caller-allocated home save location. Add 8 to account
1796 // for the return address.
1797 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001798 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001799 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001800 // Fixup to set vararg frame on shadow area (4 x i64).
1801 if (NumIntRegs < 4)
1802 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001803 } else {
1804 // For X86-64, if there are vararg parameters that are passed via
1805 // registers, then we must store them to their spots on the stack so they
1806 // may be loaded by deferencing the result of va_next.
1807 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1808 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1809 FuncInfo->setRegSaveFrameIndex(
1810 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001811 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001812 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001813
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001815 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001816 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1817 getPointerTy());
1818 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001819 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001820 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1821 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001822 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001823 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001825 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001826 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001827 MachinePointerInfo::getFixedStack(
1828 FuncInfo->getRegSaveFrameIndex(), Offset),
1829 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001830 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001831 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001832 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001833
Dan Gohmanface41a2009-08-16 21:24:25 +00001834 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1835 // Now store the XMM (fp + vector) parameter registers.
1836 SmallVector<SDValue, 11> SaveXMMOps;
1837 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001838
Devang Patel68e6bee2011-02-21 23:21:26 +00001839 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001840 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1841 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001842
Dan Gohman1e93df62010-04-17 14:41:14 +00001843 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1844 FuncInfo->getRegSaveFrameIndex()));
1845 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1846 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001847
Dan Gohmanface41a2009-08-16 21:24:25 +00001848 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001849 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001850 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001851 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1852 SaveXMMOps.push_back(Val);
1853 }
1854 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1855 MVT::Other,
1856 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001857 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001858
1859 if (!MemOps.empty())
1860 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1861 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001863 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001864
Gordon Henriksen86737662008-01-05 16:56:59 +00001865 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001866 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001867 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001868 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001869 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001870 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001871 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001872 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001873 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001874
Gordon Henriksen86737662008-01-05 16:56:59 +00001875 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001876 // RegSaveFrameIndex is X86-64 only.
1877 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001878 if (CallConv == CallingConv::X86_FastCall ||
1879 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001880 // fastcc functions can't have varargs.
1881 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001882 }
Evan Cheng25caf632006-05-23 21:06:34 +00001883
Dan Gohman98ca4f22009-08-05 01:29:28 +00001884 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001885}
1886
Dan Gohman475871a2008-07-27 21:46:04 +00001887SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001888X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1889 SDValue StackPtr, SDValue Arg,
1890 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001891 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001892 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001893 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001894 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001895 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001896 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001897 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001898
1899 return DAG.getStore(Chain, dl, Arg, PtrOff,
1900 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001901 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001902}
1903
Bill Wendling64e87322009-01-16 19:25:27 +00001904/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001905/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001906SDValue
1907X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001908 SDValue &OutRetAddr, SDValue Chain,
1909 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001910 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001911 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001912 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001913 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001914
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001915 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001916 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1917 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001918 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001919}
1920
1921/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1922/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001923static SDValue
1924EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001925 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001926 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001927 // Store the return address to the appropriate stack slot.
1928 if (!FPDiff) return Chain;
1929 // Calculate the new stack slot for the return address.
1930 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001931 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001932 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001933 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001934 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001936 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001937 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938 return Chain;
1939}
1940
Dan Gohman98ca4f22009-08-05 01:29:28 +00001941SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001942X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001943 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001944 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001945 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001946 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947 const SmallVectorImpl<ISD::InputArg> &Ins,
1948 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001949 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001950 MachineFunction &MF = DAG.getMachineFunction();
1951 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001952 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001954 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001955
Evan Cheng5f941932010-02-05 02:21:12 +00001956 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001957 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001958 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1959 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001960 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001961
1962 // Sibcalls are automatically detected tailcalls which do not require
1963 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001964 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001965 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001966
1967 if (isTailCall)
1968 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001969 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001970
Chris Lattner29689432010-03-11 00:22:57 +00001971 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1972 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001973
Chris Lattner638402b2007-02-28 07:00:42 +00001974 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001975 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001976 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1977 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001978
1979 // Allocate shadow area for Win64
1980 if (IsWin64) {
1981 CCInfo.AllocateStack(32, 8);
1982 }
1983
Duncan Sands45907662010-10-31 13:21:44 +00001984 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Chris Lattner423c5f42007-02-28 05:31:48 +00001986 // Get a count of how many bytes are to be pushed on the stack.
1987 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001988 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001989 // This is a sibcall. The memory operands are available in caller's
1990 // own caller's stack.
1991 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001992 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001993 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001994
Gordon Henriksen86737662008-01-05 16:56:59 +00001995 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001996 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001998 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001999 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2000 FPDiff = NumBytesCallerPushed - NumBytes;
2001
2002 // Set the delta of movement of the returnaddr stackslot.
2003 // But only set if delta is greater than previous delta.
2004 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2005 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2006 }
2007
Evan Chengf22f9b32010-02-06 03:28:46 +00002008 if (!IsSibcall)
2009 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002010
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002012 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002013 if (isTailCall && FPDiff)
2014 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2015 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002016
Dan Gohman475871a2008-07-27 21:46:04 +00002017 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2018 SmallVector<SDValue, 8> MemOpChains;
2019 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002020
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002021 // Walk the register/memloc assignments, inserting copies/loads. In the case
2022 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002023 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2024 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002025 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002026 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002028 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Chris Lattner423c5f42007-02-28 05:31:48 +00002030 // Promote the value if needed.
2031 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002032 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002033 case CCValAssign::Full: break;
2034 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002035 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002036 break;
2037 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002038 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002039 break;
2040 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002041 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2042 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002043 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2045 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002046 } else
2047 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2048 break;
2049 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002050 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002051 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002052 case CCValAssign::Indirect: {
2053 // Store the argument.
2054 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002055 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002056 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002057 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002058 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002059 Arg = SpillSlot;
2060 break;
2061 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002062 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002063
Chris Lattner423c5f42007-02-28 05:31:48 +00002064 if (VA.isRegLoc()) {
2065 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002066 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002067 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2068 // shadow reg if callee is a varargs function.
2069 unsigned ShadowReg = 0;
2070 switch (VA.getLocReg()) {
2071 case X86::XMM0: ShadowReg = X86::RCX; break;
2072 case X86::XMM1: ShadowReg = X86::RDX; break;
2073 case X86::XMM2: ShadowReg = X86::R8; break;
2074 case X86::XMM3: ShadowReg = X86::R9; break;
2075 }
2076 if (ShadowReg)
2077 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2078 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002079 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002080 assert(VA.isMemLoc());
2081 if (StackPtr.getNode() == 0)
2082 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2083 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2084 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002085 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002086 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002087
Evan Cheng32fe1032006-05-25 00:59:30 +00002088 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002090 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002091
Evan Cheng347d5f72006-04-28 21:29:37 +00002092 // Build a sequence of copy-to-reg nodes chained together with token chain
2093 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002094 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002095 // Tail call byval lowering might overwrite argument registers so in case of
2096 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002097 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002098 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002099 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002100 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002101 InFlag = Chain.getValue(1);
2102 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002103
Chris Lattner88e1fd52009-07-09 04:24:46 +00002104 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002105 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2106 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002107 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002108 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2109 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002110 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002111 InFlag);
2112 InFlag = Chain.getValue(1);
2113 } else {
2114 // If we are tail calling and generating PIC/GOT style code load the
2115 // address of the callee into ECX. The value in ecx is used as target of
2116 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2117 // for tail calls on PIC/GOT architectures. Normally we would just put the
2118 // address of GOT into ebx and then call target@PLT. But for tail calls
2119 // ebx would be restored (since ebx is callee saved) before jumping to the
2120 // target@PLT.
2121
2122 // Note: The actual moving to ECX is done further down.
2123 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2124 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2125 !G->getGlobal()->hasProtectedVisibility())
2126 Callee = LowerGlobalAddress(Callee, DAG);
2127 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002128 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002129 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002130 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002131
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002132 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 // From AMD64 ABI document:
2134 // For calls that may call functions that use varargs or stdargs
2135 // (prototype-less calls or calls to functions containing ellipsis (...) in
2136 // the declaration) %al is used as hidden argument to specify the number
2137 // of SSE registers used. The contents of %al do not need to match exactly
2138 // the number of registers, but must be an ubound on the number of SSE
2139 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002140
Gordon Henriksen86737662008-01-05 16:56:59 +00002141 // Count the number of XMM registers allocated.
2142 static const unsigned XMMArgRegs[] = {
2143 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2144 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2145 };
2146 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002147 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002148 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002149
Dale Johannesendd64c412009-02-04 00:33:20 +00002150 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002151 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002152 InFlag = Chain.getValue(1);
2153 }
2154
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002155
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002156 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157 if (isTailCall) {
2158 // Force all the incoming stack arguments to be loaded from the stack
2159 // before any new outgoing arguments are stored to the stack, because the
2160 // outgoing stack slots may alias the incoming argument stack slots, and
2161 // the alias isn't otherwise explicit. This is slightly more conservative
2162 // than necessary, because it means that each store effectively depends
2163 // on every argument instead of just those arguments it would clobber.
2164 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2165
Dan Gohman475871a2008-07-27 21:46:04 +00002166 SmallVector<SDValue, 8> MemOpChains2;
2167 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002168 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002169 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002170 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002171 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002172 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2173 CCValAssign &VA = ArgLocs[i];
2174 if (VA.isRegLoc())
2175 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002176 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002177 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002179 // Create frame index.
2180 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002181 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002182 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002183 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002184
Duncan Sands276dcbd2008-03-21 09:14:45 +00002185 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002186 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002187 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002188 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002189 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002190 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002191 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002192
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2194 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002195 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002196 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002197 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002198 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002199 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002200 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002201 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002202 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002203 }
2204 }
2205
2206 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002207 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002208 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002209
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002210 // Copy arguments to their registers.
2211 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002212 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002213 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002214 InFlag = Chain.getValue(1);
2215 }
Dan Gohman475871a2008-07-27 21:46:04 +00002216 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002219 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002220 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002221 }
2222
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002223 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2224 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2225 // In the 64-bit large code model, we have to make all calls
2226 // through a register, since the call instruction's 32-bit
2227 // pc-relative offset may not be large enough to hold the whole
2228 // address.
2229 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002230 // If the callee is a GlobalAddress node (quite common, every direct call
2231 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2232 // it.
2233
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002234 // We should use extra load for direct calls to dllimported functions in
2235 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002236 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002237 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002238 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002239
Chris Lattner48a7d022009-07-09 05:02:21 +00002240 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2241 // external symbols most go through the PLT in PIC mode. If the symbol
2242 // has hidden or protected visibility, or if it is static or local, then
2243 // we don't need to use the PLT - we can directly call it.
2244 if (Subtarget->isTargetELF() &&
2245 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002246 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002247 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002248 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002249 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2250 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002251 // PC-relative references to external symbols should go through $stub,
2252 // unless we're building with the leopard linker or later, which
2253 // automatically synthesizes these stubs.
2254 OpFlags = X86II::MO_DARWIN_STUB;
2255 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002256
Devang Patel0d881da2010-07-06 22:08:15 +00002257 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002258 G->getOffset(), OpFlags);
2259 }
Bill Wendling056292f2008-09-16 21:48:12 +00002260 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002261 unsigned char OpFlags = 0;
2262
Evan Cheng1bf891a2010-12-01 22:59:46 +00002263 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2264 // external symbols should go through the PLT.
2265 if (Subtarget->isTargetELF() &&
2266 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2267 OpFlags = X86II::MO_PLT;
2268 } else if (Subtarget->isPICStyleStubAny() &&
2269 Subtarget->getDarwinVers() < 9) {
2270 // PC-relative references to external symbols should go through $stub,
2271 // unless we're building with the leopard linker or later, which
2272 // automatically synthesizes these stubs.
2273 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002274 }
Eric Christopherfd179292009-08-27 18:07:15 +00002275
Chris Lattner48a7d022009-07-09 05:02:21 +00002276 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2277 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002278 }
2279
Chris Lattnerd96d0722007-02-25 06:40:16 +00002280 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002281 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002282 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002283
Evan Chengf22f9b32010-02-06 03:28:46 +00002284 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002285 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2286 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002287 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002289
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002290 Ops.push_back(Chain);
2291 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002292
Dan Gohman98ca4f22009-08-05 01:29:28 +00002293 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002294 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002295
Gordon Henriksen86737662008-01-05 16:56:59 +00002296 // Add argument registers to the end of the list so that they are known live
2297 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002298 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2299 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2300 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002301
Evan Cheng586ccac2008-03-18 23:36:35 +00002302 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002303 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002304 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2305
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002306 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002307 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002308 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002309
Gabor Greifba36cb52008-08-28 21:40:38 +00002310 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002311 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002312
Dan Gohman98ca4f22009-08-05 01:29:28 +00002313 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002314 // We used to do:
2315 //// If this is the first return lowered for this function, add the regs
2316 //// to the liveout set for the function.
2317 // This isn't right, although it's probably harmless on x86; liveouts
2318 // should be computed from returns not tail calls. Consider a void
2319 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002320 return DAG.getNode(X86ISD::TC_RETURN, dl,
2321 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002322 }
2323
Dale Johannesenace16102009-02-03 19:33:06 +00002324 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002325 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002326
Chris Lattner2d297092006-05-23 18:50:38 +00002327 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002328 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002329 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002330 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002331 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002332 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002333 // pops the hidden struct pointer, so we have to push it back.
2334 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002335 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002336 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002337 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002338
Gordon Henriksenae636f82008-01-03 16:47:34 +00002339 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002340 if (!IsSibcall) {
2341 Chain = DAG.getCALLSEQ_END(Chain,
2342 DAG.getIntPtrConstant(NumBytes, true),
2343 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2344 true),
2345 InFlag);
2346 InFlag = Chain.getValue(1);
2347 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002348
Chris Lattner3085e152007-02-25 08:59:22 +00002349 // Handle result values, copying them out of physregs into vregs that we
2350 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002351 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2352 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002353}
2354
Evan Cheng25ab6902006-09-08 06:48:29 +00002355
2356//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002357// Fast Calling Convention (tail call) implementation
2358//===----------------------------------------------------------------------===//
2359
2360// Like std call, callee cleans arguments, convention except that ECX is
2361// reserved for storing the tail called function address. Only 2 registers are
2362// free for argument passing (inreg). Tail call optimization is performed
2363// provided:
2364// * tailcallopt is enabled
2365// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002366// On X86_64 architecture with GOT-style position independent code only local
2367// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002368// To keep the stack aligned according to platform abi the function
2369// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2370// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002371// If a tail called function callee has more arguments than the caller the
2372// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002373// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002374// original REtADDR, but before the saved framepointer or the spilled registers
2375// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2376// stack layout:
2377// arg1
2378// arg2
2379// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002380// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002381// move area ]
2382// (possible EBP)
2383// ESI
2384// EDI
2385// local1 ..
2386
2387/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2388/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002389unsigned
2390X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2391 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002392 MachineFunction &MF = DAG.getMachineFunction();
2393 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002394 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002395 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002396 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002397 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002398 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002399 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2400 // Number smaller than 12 so just add the difference.
2401 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2402 } else {
2403 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002404 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002405 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002406 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002407 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002408}
2409
Evan Cheng5f941932010-02-05 02:21:12 +00002410/// MatchingStackOffset - Return true if the given stack call argument is
2411/// already available in the same position (relatively) of the caller's
2412/// incoming argument stack.
2413static
2414bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2415 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2416 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002417 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2418 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002419 if (Arg.getOpcode() == ISD::CopyFromReg) {
2420 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002421 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002422 return false;
2423 MachineInstr *Def = MRI->getVRegDef(VR);
2424 if (!Def)
2425 return false;
2426 if (!Flags.isByVal()) {
2427 if (!TII->isLoadFromStackSlot(Def, FI))
2428 return false;
2429 } else {
2430 unsigned Opcode = Def->getOpcode();
2431 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2432 Def->getOperand(1).isFI()) {
2433 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002434 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002435 } else
2436 return false;
2437 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002438 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2439 if (Flags.isByVal())
2440 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002441 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002442 // define @foo(%struct.X* %A) {
2443 // tail call @bar(%struct.X* byval %A)
2444 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002445 return false;
2446 SDValue Ptr = Ld->getBasePtr();
2447 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2448 if (!FINode)
2449 return false;
2450 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002451 } else
2452 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002453
Evan Cheng4cae1332010-03-05 08:38:04 +00002454 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002455 if (!MFI->isFixedObjectIndex(FI))
2456 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002457 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002458}
2459
Dan Gohman98ca4f22009-08-05 01:29:28 +00002460/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2461/// for tail call optimization. Targets which want to do tail call
2462/// optimization should implement this function.
2463bool
2464X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002465 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002466 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002467 bool isCalleeStructRet,
2468 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002469 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002470 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002471 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002473 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002474 CalleeCC != CallingConv::C)
2475 return false;
2476
Evan Cheng7096ae42010-01-29 06:45:59 +00002477 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002478 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002479 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002480 CallingConv::ID CallerCC = CallerF->getCallingConv();
2481 bool CCMatch = CallerCC == CalleeCC;
2482
Dan Gohman1797ed52010-02-08 20:27:50 +00002483 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002484 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002485 return true;
2486 return false;
2487 }
2488
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002489 // Look for obvious safe cases to perform tail call optimization that do not
2490 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002491
Evan Cheng2c12cb42010-03-26 16:26:03 +00002492 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2493 // emit a special epilogue.
2494 if (RegInfo->needsStackRealignment(MF))
2495 return false;
2496
Eric Christopher90eb4022010-07-22 00:26:08 +00002497 // Do not sibcall optimize vararg calls unless the call site is not passing
2498 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002499 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002500 return false;
2501
Evan Chenga375d472010-03-15 18:54:48 +00002502 // Also avoid sibcall optimization if either caller or callee uses struct
2503 // return semantics.
2504 if (isCalleeStructRet || isCallerStructRet)
2505 return false;
2506
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002507 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2508 // Therefore if it's not used by the call it is not safe to optimize this into
2509 // a sibcall.
2510 bool Unused = false;
2511 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2512 if (!Ins[i].Used) {
2513 Unused = true;
2514 break;
2515 }
2516 }
2517 if (Unused) {
2518 SmallVector<CCValAssign, 16> RVLocs;
2519 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2520 RVLocs, *DAG.getContext());
2521 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002522 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002523 CCValAssign &VA = RVLocs[i];
2524 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2525 return false;
2526 }
2527 }
2528
Evan Cheng13617962010-04-30 01:12:32 +00002529 // If the calling conventions do not match, then we'd better make sure the
2530 // results are returned in the same way as what the caller expects.
2531 if (!CCMatch) {
2532 SmallVector<CCValAssign, 16> RVLocs1;
2533 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2534 RVLocs1, *DAG.getContext());
2535 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2536
2537 SmallVector<CCValAssign, 16> RVLocs2;
2538 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2539 RVLocs2, *DAG.getContext());
2540 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2541
2542 if (RVLocs1.size() != RVLocs2.size())
2543 return false;
2544 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2545 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2546 return false;
2547 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2548 return false;
2549 if (RVLocs1[i].isRegLoc()) {
2550 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2551 return false;
2552 } else {
2553 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2554 return false;
2555 }
2556 }
2557 }
2558
Evan Chenga6bff982010-01-30 01:22:00 +00002559 // If the callee takes no arguments then go on to check the results of the
2560 // call.
2561 if (!Outs.empty()) {
2562 // Check if stack adjustment is needed. For now, do not do this if any
2563 // argument is passed on the stack.
2564 SmallVector<CCValAssign, 16> ArgLocs;
2565 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2566 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002567
2568 // Allocate shadow area for Win64
2569 if (Subtarget->isTargetWin64()) {
2570 CCInfo.AllocateStack(32, 8);
2571 }
2572
Duncan Sands45907662010-10-31 13:21:44 +00002573 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002574 if (CCInfo.getNextStackOffset()) {
2575 MachineFunction &MF = DAG.getMachineFunction();
2576 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2577 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002578
2579 // Check if the arguments are already laid out in the right way as
2580 // the caller's fixed stack objects.
2581 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002582 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2583 const X86InstrInfo *TII =
2584 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002585 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2586 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002587 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002588 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002589 if (VA.getLocInfo() == CCValAssign::Indirect)
2590 return false;
2591 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002592 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2593 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002594 return false;
2595 }
2596 }
2597 }
Evan Cheng9c044672010-05-29 01:35:22 +00002598
2599 // If the tailcall address may be in a register, then make sure it's
2600 // possible to register allocate for it. In 32-bit, the call address can
2601 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002602 // callee-saved registers are restored. These happen to be the same
2603 // registers used to pass 'inreg' arguments so watch out for those.
2604 if (!Subtarget->is64Bit() &&
2605 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002606 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002607 unsigned NumInRegs = 0;
2608 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2609 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002610 if (!VA.isRegLoc())
2611 continue;
2612 unsigned Reg = VA.getLocReg();
2613 switch (Reg) {
2614 default: break;
2615 case X86::EAX: case X86::EDX: case X86::ECX:
2616 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002617 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002618 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002619 }
2620 }
2621 }
Evan Chenga6bff982010-01-30 01:22:00 +00002622 }
Evan Chengb1712452010-01-27 06:25:16 +00002623
Dale Johannesend155d7e2010-10-25 22:17:05 +00002624 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002625 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002626 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2627 return false;
2628
Evan Cheng86809cc2010-02-03 03:28:02 +00002629 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002630}
2631
Dan Gohman3df24e62008-09-03 23:12:08 +00002632FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002633X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2634 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002635}
2636
2637
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002638//===----------------------------------------------------------------------===//
2639// Other Lowering Hooks
2640//===----------------------------------------------------------------------===//
2641
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002642static bool MayFoldLoad(SDValue Op) {
2643 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2644}
2645
2646static bool MayFoldIntoStore(SDValue Op) {
2647 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2648}
2649
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002650static bool isTargetShuffle(unsigned Opcode) {
2651 switch(Opcode) {
2652 default: return false;
2653 case X86ISD::PSHUFD:
2654 case X86ISD::PSHUFHW:
2655 case X86ISD::PSHUFLW:
2656 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002657 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002658 case X86ISD::SHUFPS:
2659 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002660 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002661 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002662 case X86ISD::MOVLPS:
2663 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002664 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002665 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002666 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002667 case X86ISD::MOVSS:
2668 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002669 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002670 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002671 case X86ISD::VUNPCKLPS:
2672 case X86ISD::VUNPCKLPD:
2673 case X86ISD::VUNPCKLPSY:
2674 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002675 case X86ISD::PUNPCKLWD:
2676 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002677 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002678 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002679 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002680 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002681 case X86ISD::PUNPCKHWD:
2682 case X86ISD::PUNPCKHBW:
2683 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002684 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002685 return true;
2686 }
2687 return false;
2688}
2689
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002690static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002691 SDValue V1, SelectionDAG &DAG) {
2692 switch(Opc) {
2693 default: llvm_unreachable("Unknown x86 shuffle node");
2694 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002695 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002696 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002697 return DAG.getNode(Opc, dl, VT, V1);
2698 }
2699
2700 return SDValue();
2701}
2702
2703static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002704 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002705 switch(Opc) {
2706 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002707 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002708 case X86ISD::PSHUFHW:
2709 case X86ISD::PSHUFLW:
2710 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2711 }
2712
2713 return SDValue();
2714}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002715
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002716static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2717 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2718 switch(Opc) {
2719 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002720 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002721 case X86ISD::SHUFPD:
2722 case X86ISD::SHUFPS:
2723 return DAG.getNode(Opc, dl, VT, V1, V2,
2724 DAG.getConstant(TargetMask, MVT::i8));
2725 }
2726 return SDValue();
2727}
2728
2729static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2730 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2731 switch(Opc) {
2732 default: llvm_unreachable("Unknown x86 shuffle node");
2733 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002734 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002735 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002736 case X86ISD::MOVLPS:
2737 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002738 case X86ISD::MOVSS:
2739 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002740 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002741 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002742 case X86ISD::VUNPCKLPS:
2743 case X86ISD::VUNPCKLPD:
2744 case X86ISD::VUNPCKLPSY:
2745 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002746 case X86ISD::PUNPCKLWD:
2747 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002748 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002749 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002750 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002751 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002752 case X86ISD::PUNPCKHWD:
2753 case X86ISD::PUNPCKHBW:
2754 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002755 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002756 return DAG.getNode(Opc, dl, VT, V1, V2);
2757 }
2758 return SDValue();
2759}
2760
Dan Gohmand858e902010-04-17 15:26:15 +00002761SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002762 MachineFunction &MF = DAG.getMachineFunction();
2763 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2764 int ReturnAddrIndex = FuncInfo->getRAIndex();
2765
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002766 if (ReturnAddrIndex == 0) {
2767 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002768 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002769 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002770 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002771 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002772 }
2773
Evan Cheng25ab6902006-09-08 06:48:29 +00002774 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002775}
2776
2777
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002778bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2779 bool hasSymbolicDisplacement) {
2780 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002781 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002782 return false;
2783
2784 // If we don't have a symbolic displacement - we don't have any extra
2785 // restrictions.
2786 if (!hasSymbolicDisplacement)
2787 return true;
2788
2789 // FIXME: Some tweaks might be needed for medium code model.
2790 if (M != CodeModel::Small && M != CodeModel::Kernel)
2791 return false;
2792
2793 // For small code model we assume that latest object is 16MB before end of 31
2794 // bits boundary. We may also accept pretty large negative constants knowing
2795 // that all objects are in the positive half of address space.
2796 if (M == CodeModel::Small && Offset < 16*1024*1024)
2797 return true;
2798
2799 // For kernel code model we know that all object resist in the negative half
2800 // of 32bits address space. We may not accept negative offsets, since they may
2801 // be just off and we may accept pretty large positive ones.
2802 if (M == CodeModel::Kernel && Offset > 0)
2803 return true;
2804
2805 return false;
2806}
2807
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002808/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2809/// specific condition code, returning the condition code and the LHS/RHS of the
2810/// comparison to make.
2811static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2812 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002813 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002814 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2815 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2816 // X > -1 -> X == 0, jump !sign.
2817 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002818 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002819 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2820 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002821 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002822 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002823 // X < 1 -> X <= 0
2824 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002825 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002826 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002827 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002828
Evan Chengd9558e02006-01-06 00:43:03 +00002829 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002830 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002831 case ISD::SETEQ: return X86::COND_E;
2832 case ISD::SETGT: return X86::COND_G;
2833 case ISD::SETGE: return X86::COND_GE;
2834 case ISD::SETLT: return X86::COND_L;
2835 case ISD::SETLE: return X86::COND_LE;
2836 case ISD::SETNE: return X86::COND_NE;
2837 case ISD::SETULT: return X86::COND_B;
2838 case ISD::SETUGT: return X86::COND_A;
2839 case ISD::SETULE: return X86::COND_BE;
2840 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002841 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002843
Chris Lattner4c78e022008-12-23 23:42:27 +00002844 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002845
Chris Lattner4c78e022008-12-23 23:42:27 +00002846 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002847 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2848 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002849 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2850 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002851 }
2852
Chris Lattner4c78e022008-12-23 23:42:27 +00002853 switch (SetCCOpcode) {
2854 default: break;
2855 case ISD::SETOLT:
2856 case ISD::SETOLE:
2857 case ISD::SETUGT:
2858 case ISD::SETUGE:
2859 std::swap(LHS, RHS);
2860 break;
2861 }
2862
2863 // On a floating point condition, the flags are set as follows:
2864 // ZF PF CF op
2865 // 0 | 0 | 0 | X > Y
2866 // 0 | 0 | 1 | X < Y
2867 // 1 | 0 | 0 | X == Y
2868 // 1 | 1 | 1 | unordered
2869 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002870 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002871 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002872 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002873 case ISD::SETOLT: // flipped
2874 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002875 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002876 case ISD::SETOLE: // flipped
2877 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002878 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002879 case ISD::SETUGT: // flipped
2880 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002881 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002882 case ISD::SETUGE: // flipped
2883 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002884 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002885 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002886 case ISD::SETNE: return X86::COND_NE;
2887 case ISD::SETUO: return X86::COND_P;
2888 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002889 case ISD::SETOEQ:
2890 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002891 }
Evan Chengd9558e02006-01-06 00:43:03 +00002892}
2893
Evan Cheng4a460802006-01-11 00:33:36 +00002894/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2895/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002896/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002897static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002898 switch (X86CC) {
2899 default:
2900 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002901 case X86::COND_B:
2902 case X86::COND_BE:
2903 case X86::COND_E:
2904 case X86::COND_P:
2905 case X86::COND_A:
2906 case X86::COND_AE:
2907 case X86::COND_NE:
2908 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002909 return true;
2910 }
2911}
2912
Evan Chengeb2f9692009-10-27 19:56:55 +00002913/// isFPImmLegal - Returns true if the target can instruction select the
2914/// specified FP immediate natively. If false, the legalizer will
2915/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002916bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002917 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2918 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2919 return true;
2920 }
2921 return false;
2922}
2923
Nate Begeman9008ca62009-04-27 18:41:29 +00002924/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2925/// the specified range (L, H].
2926static bool isUndefOrInRange(int Val, int Low, int Hi) {
2927 return (Val < 0) || (Val >= Low && Val < Hi);
2928}
2929
2930/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2931/// specified value.
2932static bool isUndefOrEqual(int Val, int CmpVal) {
2933 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002934 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002936}
2937
Nate Begeman9008ca62009-04-27 18:41:29 +00002938/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2939/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2940/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002941static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002942 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002944 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002945 return (Mask[0] < 2 && Mask[1] < 2);
2946 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002947}
2948
Nate Begeman9008ca62009-04-27 18:41:29 +00002949bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002950 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 N->getMask(M);
2952 return ::isPSHUFDMask(M, N->getValueType(0));
2953}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002954
Nate Begeman9008ca62009-04-27 18:41:29 +00002955/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2956/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002957static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002959 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002960
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 // Lower quadword copied in order or undef.
2962 for (int i = 0; i != 4; ++i)
2963 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002964 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002965
Evan Cheng506d3df2006-03-29 23:07:14 +00002966 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 for (int i = 4; i != 8; ++i)
2968 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002969 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002970
Evan Cheng506d3df2006-03-29 23:07:14 +00002971 return true;
2972}
2973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002975 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 N->getMask(M);
2977 return ::isPSHUFHWMask(M, N->getValueType(0));
2978}
Evan Cheng506d3df2006-03-29 23:07:14 +00002979
Nate Begeman9008ca62009-04-27 18:41:29 +00002980/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2981/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002982static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002984 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002985
Rafael Espindola15684b22009-04-24 12:40:33 +00002986 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 for (int i = 4; i != 8; ++i)
2988 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002989 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002990
Rafael Espindola15684b22009-04-24 12:40:33 +00002991 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002992 for (int i = 0; i != 4; ++i)
2993 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002994 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002995
Rafael Espindola15684b22009-04-24 12:40:33 +00002996 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002997}
2998
Nate Begeman9008ca62009-04-27 18:41:29 +00002999bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003000 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 N->getMask(M);
3002 return ::isPSHUFLWMask(M, N->getValueType(0));
3003}
3004
Nate Begemana09008b2009-10-19 02:17:23 +00003005/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3006/// is suitable for input to PALIGNR.
3007static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3008 bool hasSSSE3) {
3009 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003010
Nate Begemana09008b2009-10-19 02:17:23 +00003011 // Do not handle v2i64 / v2f64 shuffles with palignr.
3012 if (e < 4 || !hasSSSE3)
3013 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003014
Nate Begemana09008b2009-10-19 02:17:23 +00003015 for (i = 0; i != e; ++i)
3016 if (Mask[i] >= 0)
3017 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003018
Nate Begemana09008b2009-10-19 02:17:23 +00003019 // All undef, not a palignr.
3020 if (i == e)
3021 return false;
3022
3023 // Determine if it's ok to perform a palignr with only the LHS, since we
3024 // don't have access to the actual shuffle elements to see if RHS is undef.
3025 bool Unary = Mask[i] < (int)e;
3026 bool NeedsUnary = false;
3027
3028 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003029
Nate Begemana09008b2009-10-19 02:17:23 +00003030 // Check the rest of the elements to see if they are consecutive.
3031 for (++i; i != e; ++i) {
3032 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003033 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003034 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003035
Nate Begemana09008b2009-10-19 02:17:23 +00003036 Unary = Unary && (m < (int)e);
3037 NeedsUnary = NeedsUnary || (m < s);
3038
3039 if (NeedsUnary && !Unary)
3040 return false;
3041 if (Unary && m != ((s+i) & (e-1)))
3042 return false;
3043 if (!Unary && m != (s+i))
3044 return false;
3045 }
3046 return true;
3047}
3048
3049bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3050 SmallVector<int, 8> M;
3051 N->getMask(M);
3052 return ::isPALIGNRMask(M, N->getValueType(0), true);
3053}
3054
Evan Cheng14aed5e2006-03-24 01:18:28 +00003055/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3056/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003057static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 int NumElems = VT.getVectorNumElements();
3059 if (NumElems != 2 && NumElems != 4)
3060 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 int Half = NumElems / 2;
3063 for (int i = 0; i < Half; ++i)
3064 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003065 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003066 for (int i = Half; i < NumElems; ++i)
3067 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003068 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003069
Evan Cheng14aed5e2006-03-24 01:18:28 +00003070 return true;
3071}
3072
Nate Begeman9008ca62009-04-27 18:41:29 +00003073bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3074 SmallVector<int, 8> M;
3075 N->getMask(M);
3076 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003077}
3078
Evan Cheng213d2cf2007-05-17 18:45:50 +00003079/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003080/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3081/// half elements to come from vector 1 (which would equal the dest.) and
3082/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003083static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003084 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003085
3086 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003088
Nate Begeman9008ca62009-04-27 18:41:29 +00003089 int Half = NumElems / 2;
3090 for (int i = 0; i < Half; ++i)
3091 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003092 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 for (int i = Half; i < NumElems; ++i)
3094 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003095 return false;
3096 return true;
3097}
3098
Nate Begeman9008ca62009-04-27 18:41:29 +00003099static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3100 SmallVector<int, 8> M;
3101 N->getMask(M);
3102 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003103}
3104
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003105/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3106/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003107bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3108 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003109 return false;
3110
Evan Cheng2064a2b2006-03-28 06:50:32 +00003111 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3113 isUndefOrEqual(N->getMaskElt(1), 7) &&
3114 isUndefOrEqual(N->getMaskElt(2), 2) &&
3115 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003116}
3117
Nate Begeman0b10b912009-11-07 23:17:15 +00003118/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3119/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3120/// <2, 3, 2, 3>
3121bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3122 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003123
Nate Begeman0b10b912009-11-07 23:17:15 +00003124 if (NumElems != 4)
3125 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003126
Nate Begeman0b10b912009-11-07 23:17:15 +00003127 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3128 isUndefOrEqual(N->getMaskElt(1), 3) &&
3129 isUndefOrEqual(N->getMaskElt(2), 2) &&
3130 isUndefOrEqual(N->getMaskElt(3), 3);
3131}
3132
Evan Cheng5ced1d82006-04-06 23:23:56 +00003133/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3134/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003135bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3136 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003137
Evan Cheng5ced1d82006-04-06 23:23:56 +00003138 if (NumElems != 2 && NumElems != 4)
3139 return false;
3140
Evan Chengc5cdff22006-04-07 21:53:05 +00003141 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003143 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003144
Evan Chengc5cdff22006-04-07 21:53:05 +00003145 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003147 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003148
3149 return true;
3150}
3151
Nate Begeman0b10b912009-11-07 23:17:15 +00003152/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3153/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3154bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003156
David Greenea20244d2011-03-02 17:23:43 +00003157 if ((NumElems != 2 && NumElems != 4)
3158 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003159 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))
Evan Chengc5cdff22006-04-07 21:53:05 +00003163 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003164
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 for (unsigned i = 0; i < NumElems/2; ++i)
3166 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003167 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003168
3169 return true;
3170}
3171
Evan Cheng0038e592006-03-28 00:39:58 +00003172/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3173/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003174static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003175 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003177 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003178 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003179
David Greenea20244d2011-03-02 17:23:43 +00003180 // Handle vector lengths > 128 bits. Define a "section" as a set of
3181 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3182 // sections.
3183 unsigned NumSections = VT.getSizeInBits() / 128;
3184 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3185 unsigned NumSectionElts = NumElts / NumSections;
3186
3187 unsigned Start = 0;
3188 unsigned End = NumSectionElts;
3189 for (unsigned s = 0; s < NumSections; ++s) {
3190 for (unsigned i = Start, j = s * NumSectionElts;
3191 i != End;
3192 i += 2, ++j) {
3193 int BitI = Mask[i];
3194 int BitI1 = Mask[i+1];
3195 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003196 return false;
David Greenea20244d2011-03-02 17:23:43 +00003197 if (V2IsSplat) {
3198 if (!isUndefOrEqual(BitI1, NumElts))
3199 return false;
3200 } else {
3201 if (!isUndefOrEqual(BitI1, j + NumElts))
3202 return false;
3203 }
Evan Cheng39623da2006-04-20 08:58:49 +00003204 }
David Greenea20244d2011-03-02 17:23:43 +00003205 // Process the next 128 bits.
3206 Start += NumSectionElts;
3207 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003208 }
David Greenea20244d2011-03-02 17:23:43 +00003209
Evan Cheng0038e592006-03-28 00:39:58 +00003210 return true;
3211}
3212
Nate Begeman9008ca62009-04-27 18:41:29 +00003213bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3214 SmallVector<int, 8> M;
3215 N->getMask(M);
3216 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003217}
3218
Evan Cheng4fcb9222006-03-28 02:43:26 +00003219/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3220/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003221static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003222 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003224 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003225 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003226
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3228 int BitI = Mask[i];
3229 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003230 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003231 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003232 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003233 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003234 return false;
3235 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003236 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003237 return false;
3238 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003239 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003240 return true;
3241}
3242
Nate Begeman9008ca62009-04-27 18:41:29 +00003243bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3244 SmallVector<int, 8> M;
3245 N->getMask(M);
3246 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003247}
3248
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003249/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3250/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3251/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003252static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003254 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003255 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003256
David Greenea20244d2011-03-02 17:23:43 +00003257 // Handle vector lengths > 128 bits. Define a "section" as a set of
3258 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3259 // sections.
3260 unsigned NumSections = VT.getSizeInBits() / 128;
3261 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3262 unsigned NumSectionElts = NumElems / NumSections;
3263
3264 for (unsigned s = 0; s < NumSections; ++s) {
3265 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3266 i != NumSectionElts * (s + 1);
3267 i += 2, ++j) {
3268 int BitI = Mask[i];
3269 int BitI1 = Mask[i+1];
3270
3271 if (!isUndefOrEqual(BitI, j))
3272 return false;
3273 if (!isUndefOrEqual(BitI1, j))
3274 return false;
3275 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003276 }
David Greenea20244d2011-03-02 17:23:43 +00003277
Rafael Espindola15684b22009-04-24 12:40:33 +00003278 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003279}
3280
Nate Begeman9008ca62009-04-27 18:41:29 +00003281bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3282 SmallVector<int, 8> M;
3283 N->getMask(M);
3284 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3285}
3286
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003287/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3288/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3289/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003290static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003291 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003292 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3293 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003294
Nate Begeman9008ca62009-04-27 18:41:29 +00003295 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3296 int BitI = Mask[i];
3297 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003298 if (!isUndefOrEqual(BitI, j))
3299 return false;
3300 if (!isUndefOrEqual(BitI1, j))
3301 return false;
3302 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003303 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003304}
3305
Nate Begeman9008ca62009-04-27 18:41:29 +00003306bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3307 SmallVector<int, 8> M;
3308 N->getMask(M);
3309 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3310}
3311
Evan Cheng017dcc62006-04-21 01:05:10 +00003312/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3313/// specifies a shuffle of elements that is suitable for input to MOVSS,
3314/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003315static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003316 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003317 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003318
3319 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003320
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003322 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003323
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 for (int i = 1; i < NumElts; ++i)
3325 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003326 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003327
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003328 return true;
3329}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003330
Nate Begeman9008ca62009-04-27 18:41:29 +00003331bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3332 SmallVector<int, 8> M;
3333 N->getMask(M);
3334 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003335}
3336
Evan Cheng017dcc62006-04-21 01:05:10 +00003337/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3338/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003339/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003340static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 bool V2IsSplat = false, bool V2IsUndef = false) {
3342 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003343 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003344 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003345
Nate Begeman9008ca62009-04-27 18:41:29 +00003346 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003347 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003348
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 for (int i = 1; i < NumOps; ++i)
3350 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3351 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3352 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003353 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003354
Evan Cheng39623da2006-04-20 08:58:49 +00003355 return true;
3356}
3357
Nate Begeman9008ca62009-04-27 18:41:29 +00003358static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003359 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003360 SmallVector<int, 8> M;
3361 N->getMask(M);
3362 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003363}
3364
Evan Chengd9539472006-04-14 21:59:03 +00003365/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3366/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003367bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3368 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003369 return false;
3370
3371 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003372 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 int Elt = N->getMaskElt(i);
3374 if (Elt >= 0 && Elt != 1)
3375 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003376 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003377
3378 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003379 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 int Elt = N->getMaskElt(i);
3381 if (Elt >= 0 && Elt != 3)
3382 return false;
3383 if (Elt == 3)
3384 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003385 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003386 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003388 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003389}
3390
3391/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3392/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003393bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3394 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003395 return false;
3396
3397 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 for (unsigned i = 0; i < 2; ++i)
3399 if (N->getMaskElt(i) > 0)
3400 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003401
3402 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003403 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 int Elt = N->getMaskElt(i);
3405 if (Elt >= 0 && Elt != 2)
3406 return false;
3407 if (Elt == 2)
3408 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003409 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003411 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003412}
3413
Evan Cheng0b457f02008-09-25 20:50:48 +00003414/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3415/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003416bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3417 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003418
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 for (int i = 0; i < e; ++i)
3420 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003421 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 for (int i = 0; i < e; ++i)
3423 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003424 return false;
3425 return true;
3426}
3427
David Greenec38a03e2011-02-03 15:50:00 +00003428/// isVEXTRACTF128Index - Return true if the specified
3429/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3430/// suitable for input to VEXTRACTF128.
3431bool X86::isVEXTRACTF128Index(SDNode *N) {
3432 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3433 return false;
3434
3435 // The index should be aligned on a 128-bit boundary.
3436 uint64_t Index =
3437 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3438
3439 unsigned VL = N->getValueType(0).getVectorNumElements();
3440 unsigned VBits = N->getValueType(0).getSizeInBits();
3441 unsigned ElSize = VBits / VL;
3442 bool Result = (Index * ElSize) % 128 == 0;
3443
3444 return Result;
3445}
3446
David Greeneccacdc12011-02-04 16:08:29 +00003447/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3448/// operand specifies a subvector insert that is suitable for input to
3449/// VINSERTF128.
3450bool X86::isVINSERTF128Index(SDNode *N) {
3451 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3452 return false;
3453
3454 // The index should be aligned on a 128-bit boundary.
3455 uint64_t Index =
3456 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3457
3458 unsigned VL = N->getValueType(0).getVectorNumElements();
3459 unsigned VBits = N->getValueType(0).getSizeInBits();
3460 unsigned ElSize = VBits / VL;
3461 bool Result = (Index * ElSize) % 128 == 0;
3462
3463 return Result;
3464}
3465
Evan Cheng63d33002006-03-22 08:01:21 +00003466/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003467/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003468unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3470 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3471
Evan Chengb9df0ca2006-03-22 02:53:00 +00003472 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3473 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 for (int i = 0; i < NumOperands; ++i) {
3475 int Val = SVOp->getMaskElt(NumOperands-i-1);
3476 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003477 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003478 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003479 if (i != NumOperands - 1)
3480 Mask <<= Shift;
3481 }
Evan Cheng63d33002006-03-22 08:01:21 +00003482 return Mask;
3483}
3484
Evan Cheng506d3df2006-03-29 23:07:14 +00003485/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003486/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003487unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003489 unsigned Mask = 0;
3490 // 8 nodes, but we only care about the last 4.
3491 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 int Val = SVOp->getMaskElt(i);
3493 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003494 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003495 if (i != 4)
3496 Mask <<= 2;
3497 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003498 return Mask;
3499}
3500
3501/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003502/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003503unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003504 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003505 unsigned Mask = 0;
3506 // 8 nodes, but we only care about the first 4.
3507 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 int Val = SVOp->getMaskElt(i);
3509 if (Val >= 0)
3510 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003511 if (i != 0)
3512 Mask <<= 2;
3513 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003514 return Mask;
3515}
3516
Nate Begemana09008b2009-10-19 02:17:23 +00003517/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3518/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3519unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3520 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3521 EVT VVT = N->getValueType(0);
3522 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3523 int Val = 0;
3524
3525 unsigned i, e;
3526 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3527 Val = SVOp->getMaskElt(i);
3528 if (Val >= 0)
3529 break;
3530 }
3531 return (Val - i) * EltSize;
3532}
3533
David Greenec38a03e2011-02-03 15:50:00 +00003534/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3535/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3536/// instructions.
3537unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3538 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3539 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3540
3541 uint64_t Index =
3542 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3543
3544 EVT VecVT = N->getOperand(0).getValueType();
3545 EVT ElVT = VecVT.getVectorElementType();
3546
3547 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3548
3549 return Index / NumElemsPerChunk;
3550}
3551
David Greeneccacdc12011-02-04 16:08:29 +00003552/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3553/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3554/// instructions.
3555unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3556 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3557 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3558
3559 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003560 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003561
3562 EVT VecVT = N->getValueType(0);
3563 EVT ElVT = VecVT.getVectorElementType();
3564
3565 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3566
3567 return Index / NumElemsPerChunk;
3568}
3569
Evan Cheng37b73872009-07-30 08:33:02 +00003570/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3571/// constant +0.0.
3572bool X86::isZeroNode(SDValue Elt) {
3573 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003574 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003575 (isa<ConstantFPSDNode>(Elt) &&
3576 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3577}
3578
Nate Begeman9008ca62009-04-27 18:41:29 +00003579/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3580/// their permute mask.
3581static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3582 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003583 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003584 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003586
Nate Begeman5a5ca152009-04-29 05:20:52 +00003587 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 int idx = SVOp->getMaskElt(i);
3589 if (idx < 0)
3590 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003591 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003593 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003595 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003596 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3597 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003598}
3599
Evan Cheng779ccea2007-12-07 21:30:01 +00003600/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3601/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003602static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003603 unsigned NumElems = VT.getVectorNumElements();
3604 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 int idx = Mask[i];
3606 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003607 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003608 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003610 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003612 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003613}
3614
Evan Cheng533a0aa2006-04-19 20:35:22 +00003615/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3616/// match movhlps. The lower half elements should come from upper half of
3617/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003618/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003619static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3620 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003621 return false;
3622 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003624 return false;
3625 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003626 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003627 return false;
3628 return true;
3629}
3630
Evan Cheng5ced1d82006-04-06 23:23:56 +00003631/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003632/// is promoted to a vector. It also returns the LoadSDNode by reference if
3633/// required.
3634static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003635 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3636 return false;
3637 N = N->getOperand(0).getNode();
3638 if (!ISD::isNON_EXTLoad(N))
3639 return false;
3640 if (LD)
3641 *LD = cast<LoadSDNode>(N);
3642 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003643}
3644
Evan Cheng533a0aa2006-04-19 20:35:22 +00003645/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3646/// match movlp{s|d}. The lower half elements should come from lower half of
3647/// V1 (and in order), and the upper half elements should come from the upper
3648/// half of V2 (and in order). And since V1 will become the source of the
3649/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003650static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3651 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003652 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003653 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003654 // Is V2 is a vector load, don't do this transformation. We will try to use
3655 // load folding shufps op.
3656 if (ISD::isNON_EXTLoad(V2))
3657 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003658
Nate Begeman5a5ca152009-04-29 05:20:52 +00003659 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003660
Evan Cheng533a0aa2006-04-19 20:35:22 +00003661 if (NumElems != 2 && NumElems != 4)
3662 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003663 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003664 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003665 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003666 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003667 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003668 return false;
3669 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003670}
3671
Evan Cheng39623da2006-04-20 08:58:49 +00003672/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3673/// all the same.
3674static bool isSplatVector(SDNode *N) {
3675 if (N->getOpcode() != ISD::BUILD_VECTOR)
3676 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003677
Dan Gohman475871a2008-07-27 21:46:04 +00003678 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003679 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3680 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003681 return false;
3682 return true;
3683}
3684
Evan Cheng213d2cf2007-05-17 18:45:50 +00003685/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003686/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003687/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003688static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003689 SDValue V1 = N->getOperand(0);
3690 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003691 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3692 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003694 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003695 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003696 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3697 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003698 if (Opc != ISD::BUILD_VECTOR ||
3699 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003700 return false;
3701 } else if (Idx >= 0) {
3702 unsigned Opc = V1.getOpcode();
3703 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3704 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003705 if (Opc != ISD::BUILD_VECTOR ||
3706 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003707 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003708 }
3709 }
3710 return true;
3711}
3712
3713/// getZeroVector - Returns a vector of specified type with all zero elements.
3714///
Owen Andersone50ed302009-08-10 22:56:29 +00003715static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003716 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003717 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003718
Dale Johannesen0488fb62010-09-30 23:57:10 +00003719 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003720 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003721 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003722 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003723 if (HasSSE2) { // SSE2
3724 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3725 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3726 } else { // SSE1
3727 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3728 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3729 }
3730 } else if (VT.getSizeInBits() == 256) { // AVX
3731 // 256-bit logic and arithmetic instructions in AVX are
3732 // all floating-point, no support for integer ops. Default
3733 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003735 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3736 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003737 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003738 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003739}
3740
Chris Lattner8a594482007-11-25 00:24:49 +00003741/// getOnesVector - Returns a vector of specified type with all bits set.
3742///
Owen Andersone50ed302009-08-10 22:56:29 +00003743static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003744 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003745
Chris Lattner8a594482007-11-25 00:24:49 +00003746 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3747 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003749 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003750 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003751 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003752}
3753
3754
Evan Cheng39623da2006-04-20 08:58:49 +00003755/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3756/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003757static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003758 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003759 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003760
Evan Cheng39623da2006-04-20 08:58:49 +00003761 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003762 SmallVector<int, 8> MaskVec;
3763 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003764
Nate Begeman5a5ca152009-04-29 05:20:52 +00003765 for (unsigned i = 0; i != NumElems; ++i) {
3766 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003767 MaskVec[i] = NumElems;
3768 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003769 }
Evan Cheng39623da2006-04-20 08:58:49 +00003770 }
Evan Cheng39623da2006-04-20 08:58:49 +00003771 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003772 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3773 SVOp->getOperand(1), &MaskVec[0]);
3774 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003775}
3776
Evan Cheng017dcc62006-04-21 01:05:10 +00003777/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3778/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003779static SDValue getMOVL(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;
3783 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003784 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 Mask.push_back(i);
3786 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003787}
3788
Nate Begeman9008ca62009-04-27 18:41:29 +00003789/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003790static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 SDValue V2) {
3792 unsigned NumElems = VT.getVectorNumElements();
3793 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003794 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 Mask.push_back(i);
3796 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003797 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003798 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003799}
3800
Nate Begeman9008ca62009-04-27 18:41:29 +00003801/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003802static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003803 SDValue V2) {
3804 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003805 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003807 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003808 Mask.push_back(i + Half);
3809 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003810 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003812}
3813
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003814/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3815static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003817 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 DebugLoc dl = SV->getDebugLoc();
3819 SDValue V1 = SV->getOperand(0);
3820 int NumElems = VT.getVectorNumElements();
3821 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003822
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 // unpack elements to the correct location
3824 while (NumElems > 4) {
3825 if (EltNo < NumElems/2) {
3826 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3827 } else {
3828 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3829 EltNo -= NumElems/2;
3830 }
3831 NumElems >>= 1;
3832 }
Eric Christopherfd179292009-08-27 18:07:15 +00003833
Nate Begeman9008ca62009-04-27 18:41:29 +00003834 // Perform the splat.
3835 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003836 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003838 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003839}
3840
Evan Chengba05f722006-04-21 23:03:30 +00003841/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003842/// vector of zero or undef vector. This produces a shuffle where the low
3843/// element of V2 is swizzled into the zero/undef vector, landing at element
3844/// 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 +00003845static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003846 bool isZero, bool HasSSE2,
3847 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003848 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003849 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003850 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3851 unsigned NumElems = VT.getVectorNumElements();
3852 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003853 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003854 // If this is the insertion idx, put the low elt of V2 here.
3855 MaskVec.push_back(i == Idx ? NumElems : i);
3856 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003857}
3858
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003859/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3860/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003861SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3862 unsigned Depth) {
3863 if (Depth == 6)
3864 return SDValue(); // Limit search depth.
3865
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003866 SDValue V = SDValue(N, 0);
3867 EVT VT = V.getValueType();
3868 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003869
3870 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3871 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3872 Index = SV->getMaskElt(Index);
3873
3874 if (Index < 0)
3875 return DAG.getUNDEF(VT.getVectorElementType());
3876
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003877 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003878 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003879 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003880 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003881
3882 // Recurse into target specific vector shuffles to find scalars.
3883 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003884 int NumElems = VT.getVectorNumElements();
3885 SmallVector<unsigned, 16> ShuffleMask;
3886 SDValue ImmN;
3887
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003888 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003889 case X86ISD::SHUFPS:
3890 case X86ISD::SHUFPD:
3891 ImmN = N->getOperand(N->getNumOperands()-1);
3892 DecodeSHUFPSMask(NumElems,
3893 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3894 ShuffleMask);
3895 break;
3896 case X86ISD::PUNPCKHBW:
3897 case X86ISD::PUNPCKHWD:
3898 case X86ISD::PUNPCKHDQ:
3899 case X86ISD::PUNPCKHQDQ:
3900 DecodePUNPCKHMask(NumElems, ShuffleMask);
3901 break;
3902 case X86ISD::UNPCKHPS:
3903 case X86ISD::UNPCKHPD:
3904 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3905 break;
3906 case X86ISD::PUNPCKLBW:
3907 case X86ISD::PUNPCKLWD:
3908 case X86ISD::PUNPCKLDQ:
3909 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003910 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003911 break;
3912 case X86ISD::UNPCKLPS:
3913 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003914 case X86ISD::VUNPCKLPS:
3915 case X86ISD::VUNPCKLPD:
3916 case X86ISD::VUNPCKLPSY:
3917 case X86ISD::VUNPCKLPDY:
3918 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003919 break;
3920 case X86ISD::MOVHLPS:
3921 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3922 break;
3923 case X86ISD::MOVLHPS:
3924 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3925 break;
3926 case X86ISD::PSHUFD:
3927 ImmN = N->getOperand(N->getNumOperands()-1);
3928 DecodePSHUFMask(NumElems,
3929 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3930 ShuffleMask);
3931 break;
3932 case X86ISD::PSHUFHW:
3933 ImmN = N->getOperand(N->getNumOperands()-1);
3934 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3935 ShuffleMask);
3936 break;
3937 case X86ISD::PSHUFLW:
3938 ImmN = N->getOperand(N->getNumOperands()-1);
3939 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3940 ShuffleMask);
3941 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003942 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003943 case X86ISD::MOVSD: {
3944 // The index 0 always comes from the first element of the second source,
3945 // this is why MOVSS and MOVSD are used in the first place. The other
3946 // elements come from the other positions of the first source vector.
3947 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003948 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3949 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003950 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003951 default:
3952 assert("not implemented for target shuffle node");
3953 return SDValue();
3954 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003955
3956 Index = ShuffleMask[Index];
3957 if (Index < 0)
3958 return DAG.getUNDEF(VT.getVectorElementType());
3959
3960 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3961 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3962 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003963 }
3964
3965 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003966 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003967 V = V.getOperand(0);
3968 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003969 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003970
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003971 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003972 return SDValue();
3973 }
3974
3975 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3976 return (Index == 0) ? V.getOperand(0)
3977 : DAG.getUNDEF(VT.getVectorElementType());
3978
3979 if (V.getOpcode() == ISD::BUILD_VECTOR)
3980 return V.getOperand(Index);
3981
3982 return SDValue();
3983}
3984
3985/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3986/// shuffle operation which come from a consecutively from a zero. The
3987/// search can start in two diferent directions, from left or right.
3988static
3989unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3990 bool ZerosFromLeft, SelectionDAG &DAG) {
3991 int i = 0;
3992
3993 while (i < NumElems) {
3994 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003995 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003996 if (!(Elt.getNode() &&
3997 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3998 break;
3999 ++i;
4000 }
4001
4002 return i;
4003}
4004
4005/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4006/// MaskE correspond consecutively to elements from one of the vector operands,
4007/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4008static
4009bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4010 int OpIdx, int NumElems, unsigned &OpNum) {
4011 bool SeenV1 = false;
4012 bool SeenV2 = false;
4013
4014 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4015 int Idx = SVOp->getMaskElt(i);
4016 // Ignore undef indicies
4017 if (Idx < 0)
4018 continue;
4019
4020 if (Idx < NumElems)
4021 SeenV1 = true;
4022 else
4023 SeenV2 = true;
4024
4025 // Only accept consecutive elements from the same vector
4026 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4027 return false;
4028 }
4029
4030 OpNum = SeenV1 ? 0 : 1;
4031 return true;
4032}
4033
4034/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4035/// logical left shift of a vector.
4036static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4037 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4038 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4039 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4040 false /* check zeros from right */, DAG);
4041 unsigned OpSrc;
4042
4043 if (!NumZeros)
4044 return false;
4045
4046 // Considering the elements in the mask that are not consecutive zeros,
4047 // check if they consecutively come from only one of the source vectors.
4048 //
4049 // V1 = {X, A, B, C} 0
4050 // \ \ \ /
4051 // vector_shuffle V1, V2 <1, 2, 3, X>
4052 //
4053 if (!isShuffleMaskConsecutive(SVOp,
4054 0, // Mask Start Index
4055 NumElems-NumZeros-1, // Mask End Index
4056 NumZeros, // Where to start looking in the src vector
4057 NumElems, // Number of elements in vector
4058 OpSrc)) // Which source operand ?
4059 return false;
4060
4061 isLeft = false;
4062 ShAmt = NumZeros;
4063 ShVal = SVOp->getOperand(OpSrc);
4064 return true;
4065}
4066
4067/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4068/// logical left shift of a vector.
4069static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4070 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4071 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4072 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4073 true /* check zeros from left */, DAG);
4074 unsigned OpSrc;
4075
4076 if (!NumZeros)
4077 return false;
4078
4079 // Considering the elements in the mask that are not consecutive zeros,
4080 // check if they consecutively come from only one of the source vectors.
4081 //
4082 // 0 { A, B, X, X } = V2
4083 // / \ / /
4084 // vector_shuffle V1, V2 <X, X, 4, 5>
4085 //
4086 if (!isShuffleMaskConsecutive(SVOp,
4087 NumZeros, // Mask Start Index
4088 NumElems-1, // Mask End Index
4089 0, // Where to start looking in the src vector
4090 NumElems, // Number of elements in vector
4091 OpSrc)) // Which source operand ?
4092 return false;
4093
4094 isLeft = true;
4095 ShAmt = NumZeros;
4096 ShVal = SVOp->getOperand(OpSrc);
4097 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004098}
4099
4100/// isVectorShift - Returns true if the shuffle can be implemented as a
4101/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004102static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004103 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004104 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4105 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4106 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004107
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004108 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004109}
4110
Evan Chengc78d3b42006-04-24 18:01:45 +00004111/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4112///
Dan Gohman475871a2008-07-27 21:46:04 +00004113static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004114 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004115 SelectionDAG &DAG,
4116 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004117 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004118 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004119
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004120 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004122 bool First = true;
4123 for (unsigned i = 0; i < 16; ++i) {
4124 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4125 if (ThisIsNonZero && First) {
4126 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004128 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004130 First = false;
4131 }
4132
4133 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004134 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004135 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4136 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004137 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004139 }
4140 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004141 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4142 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4143 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004144 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004146 } else
4147 ThisElt = LastElt;
4148
Gabor Greifba36cb52008-08-28 21:40:38 +00004149 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004151 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004152 }
4153 }
4154
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004155 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004156}
4157
Bill Wendlinga348c562007-03-22 18:42:45 +00004158/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004159///
Dan Gohman475871a2008-07-27 21:46:04 +00004160static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004161 unsigned NumNonZero, unsigned NumZero,
4162 SelectionDAG &DAG,
4163 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004164 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004165 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004166
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004167 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004168 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004169 bool First = true;
4170 for (unsigned i = 0; i < 8; ++i) {
4171 bool isNonZero = (NonZeros & (1 << i)) != 0;
4172 if (isNonZero) {
4173 if (First) {
4174 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004176 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004178 First = false;
4179 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004180 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004182 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004183 }
4184 }
4185
4186 return V;
4187}
4188
Evan Chengf26ffe92008-05-29 08:22:04 +00004189/// getVShift - Return a vector logical shift node.
4190///
Owen Andersone50ed302009-08-10 22:56:29 +00004191static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 unsigned NumBits, SelectionDAG &DAG,
4193 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004194 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004195 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004196 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4197 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004198 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004199 DAG.getConstant(NumBits,
4200 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004201}
4202
Dan Gohman475871a2008-07-27 21:46:04 +00004203SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004204X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004205 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004206
Evan Chengc3630942009-12-09 21:00:30 +00004207 // Check if the scalar load can be widened into a vector load. And if
4208 // the address is "base + cst" see if the cst can be "absorbed" into
4209 // the shuffle mask.
4210 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4211 SDValue Ptr = LD->getBasePtr();
4212 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4213 return SDValue();
4214 EVT PVT = LD->getValueType(0);
4215 if (PVT != MVT::i32 && PVT != MVT::f32)
4216 return SDValue();
4217
4218 int FI = -1;
4219 int64_t Offset = 0;
4220 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4221 FI = FINode->getIndex();
4222 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004223 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004224 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4225 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4226 Offset = Ptr.getConstantOperandVal(1);
4227 Ptr = Ptr.getOperand(0);
4228 } else {
4229 return SDValue();
4230 }
4231
4232 SDValue Chain = LD->getChain();
4233 // Make sure the stack object alignment is at least 16.
4234 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4235 if (DAG.InferPtrAlignment(Ptr) < 16) {
4236 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004237 // Can't change the alignment. FIXME: It's possible to compute
4238 // the exact stack offset and reference FI + adjust offset instead.
4239 // If someone *really* cares about this. That's the way to implement it.
4240 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004241 } else {
4242 MFI->setObjectAlignment(FI, 16);
4243 }
4244 }
4245
4246 // (Offset % 16) must be multiple of 4. Then address is then
4247 // Ptr + (Offset & ~15).
4248 if (Offset < 0)
4249 return SDValue();
4250 if ((Offset % 16) & 3)
4251 return SDValue();
4252 int64_t StartOffset = Offset & ~15;
4253 if (StartOffset)
4254 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4255 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4256
4257 int EltNo = (Offset - StartOffset) >> 2;
4258 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4259 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004260 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4261 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004262 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004263 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004264 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4265 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004266 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004267 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004268 }
4269
4270 return SDValue();
4271}
4272
Michael J. Spencerec38de22010-10-10 22:04:20 +00004273/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4274/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004275/// load which has the same value as a build_vector whose operands are 'elts'.
4276///
4277/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004278///
Nate Begeman1449f292010-03-24 22:19:06 +00004279/// FIXME: we'd also like to handle the case where the last elements are zero
4280/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4281/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004282static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004283 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004284 EVT EltVT = VT.getVectorElementType();
4285 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004286
Nate Begemanfdea31a2010-03-24 20:49:50 +00004287 LoadSDNode *LDBase = NULL;
4288 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004289
Nate Begeman1449f292010-03-24 22:19:06 +00004290 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004291 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004292 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004293 for (unsigned i = 0; i < NumElems; ++i) {
4294 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004295
Nate Begemanfdea31a2010-03-24 20:49:50 +00004296 if (!Elt.getNode() ||
4297 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4298 return SDValue();
4299 if (!LDBase) {
4300 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4301 return SDValue();
4302 LDBase = cast<LoadSDNode>(Elt.getNode());
4303 LastLoadedElt = i;
4304 continue;
4305 }
4306 if (Elt.getOpcode() == ISD::UNDEF)
4307 continue;
4308
4309 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4310 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4311 return SDValue();
4312 LastLoadedElt = i;
4313 }
Nate Begeman1449f292010-03-24 22:19:06 +00004314
4315 // If we have found an entire vector of loads and undefs, then return a large
4316 // load of the entire vector width starting at the base pointer. If we found
4317 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004318 if (LastLoadedElt == NumElems - 1) {
4319 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004320 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004321 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004322 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004323 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004324 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004325 LDBase->isVolatile(), LDBase->isNonTemporal(),
4326 LDBase->getAlignment());
4327 } else if (NumElems == 4 && LastLoadedElt == 1) {
4328 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4329 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004330 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4331 Ops, 2, MVT::i32,
4332 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004333 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004334 }
4335 return SDValue();
4336}
4337
Evan Chengc3630942009-12-09 21:00:30 +00004338SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004339X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004340 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004341
David Greenef125a292011-02-08 19:04:41 +00004342 EVT VT = Op.getValueType();
4343 EVT ExtVT = VT.getVectorElementType();
4344
4345 unsigned NumElems = Op.getNumOperands();
4346
4347 // For AVX-length vectors, build the individual 128-bit pieces and
4348 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004349 if (VT.getSizeInBits() > 256 &&
4350 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004351 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4352 SmallVector<SDValue, 8> V;
4353 V.resize(NumElems);
4354 for (unsigned i = 0; i < NumElems; ++i) {
4355 V[i] = Op.getOperand(i);
4356 }
Owen Anderson95771af2011-02-25 21:41:48 +00004357
David Greenef125a292011-02-08 19:04:41 +00004358 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4359
4360 // Build the lower subvector.
4361 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4362 // Build the upper subvector.
4363 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4364 NumElems/2);
4365
4366 return ConcatVectors(Lower, Upper, DAG);
4367 }
4368
Chris Lattner6e80e442010-08-28 17:15:43 +00004369 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4370 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004371 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4372 // is present, so AllOnes is ignored.
4373 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4374 (Op.getValueType().getSizeInBits() != 256 &&
4375 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004376 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004377 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4378 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004379 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004380 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004381
Gabor Greifba36cb52008-08-28 21:40:38 +00004382 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004383 return getOnesVector(Op.getValueType(), DAG, dl);
4384 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004385 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004386
Owen Andersone50ed302009-08-10 22:56:29 +00004387 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388
Evan Cheng0db9fe62006-04-25 20:13:52 +00004389 unsigned NumZero = 0;
4390 unsigned NumNonZero = 0;
4391 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004392 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004394 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004395 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004396 if (Elt.getOpcode() == ISD::UNDEF)
4397 continue;
4398 Values.insert(Elt);
4399 if (Elt.getOpcode() != ISD::Constant &&
4400 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004401 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004402 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004403 NumZero++;
4404 else {
4405 NonZeros |= (1 << i);
4406 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004407 }
4408 }
4409
Chris Lattner97a2a562010-08-26 05:24:29 +00004410 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4411 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004412 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413
Chris Lattner67f453a2008-03-09 05:42:06 +00004414 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004415 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004416 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004417 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004418
Chris Lattner62098042008-03-09 01:05:04 +00004419 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4420 // the value are obviously zero, truncate the value to i32 and do the
4421 // insertion that way. Only do this if the value is non-constant or if the
4422 // value is a constant being inserted into element 0. It is cheaper to do
4423 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004425 (!IsAllConstants || Idx == 0)) {
4426 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004427 // Handle SSE only.
4428 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4429 EVT VecVT = MVT::v4i32;
4430 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004431
Chris Lattner62098042008-03-09 01:05:04 +00004432 // Truncate the value (which may itself be a constant) to i32, and
4433 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004434 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004435 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004436 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4437 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004438
Chris Lattner62098042008-03-09 01:05:04 +00004439 // Now we have our 32-bit value zero extended in the low element of
4440 // a vector. If Idx != 0, swizzle it into place.
4441 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 SmallVector<int, 4> Mask;
4443 Mask.push_back(Idx);
4444 for (unsigned i = 1; i != VecElts; ++i)
4445 Mask.push_back(i);
4446 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004447 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004449 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004450 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004451 }
4452 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004453
Chris Lattner19f79692008-03-08 22:59:52 +00004454 // If we have a constant or non-constant insertion into the low element of
4455 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4456 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004457 // depending on what the source datatype is.
4458 if (Idx == 0) {
4459 if (NumZero == 0) {
4460 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004461 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4462 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004463 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4464 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4465 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4466 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004467 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4468 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004469 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4470 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004471 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4472 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4473 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004474 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004475 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004476 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004477
4478 // Is it a vector logical left shift?
4479 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004480 X86::isZeroNode(Op.getOperand(0)) &&
4481 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004482 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004483 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004484 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004485 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004486 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004487 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004488
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004489 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004490 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004491
Chris Lattner19f79692008-03-08 22:59:52 +00004492 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4493 // is a non-constant being inserted into an element other than the low one,
4494 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4495 // movd/movss) to move this into the low element, then shuffle it into
4496 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004497 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004498 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004499
Evan Cheng0db9fe62006-04-25 20:13:52 +00004500 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004501 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4502 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004503 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004504 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 MaskVec.push_back(i == Idx ? 0 : 1);
4506 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 }
4508 }
4509
Chris Lattner67f453a2008-03-09 05:42:06 +00004510 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004511 if (Values.size() == 1) {
4512 if (EVTBits == 32) {
4513 // Instead of a shuffle like this:
4514 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4515 // Check if it's possible to issue this instead.
4516 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4517 unsigned Idx = CountTrailingZeros_32(NonZeros);
4518 SDValue Item = Op.getOperand(Idx);
4519 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4520 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4521 }
Dan Gohman475871a2008-07-27 21:46:04 +00004522 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004524
Dan Gohmana3941172007-07-24 22:55:08 +00004525 // A vector full of immediates; various special cases are already
4526 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004527 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004528 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004529
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004530 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004531 if (EVTBits == 64) {
4532 if (NumNonZero == 1) {
4533 // One half is zero or undef.
4534 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004535 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004536 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004537 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4538 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004539 }
Dan Gohman475871a2008-07-27 21:46:04 +00004540 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004541 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004542
4543 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004544 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004545 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004546 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004547 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004548 }
4549
Bill Wendling826f36f2007-03-28 00:57:11 +00004550 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004552 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004553 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 }
4555
4556 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004558 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004559 if (NumElems == 4 && NumZero > 0) {
4560 for (unsigned i = 0; i < 4; ++i) {
4561 bool isZero = !(NonZeros & (1 << i));
4562 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004563 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004564 else
Dale Johannesenace16102009-02-03 19:33:06 +00004565 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 }
4567
4568 for (unsigned i = 0; i < 2; ++i) {
4569 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4570 default: break;
4571 case 0:
4572 V[i] = V[i*2]; // Must be a zero vector.
4573 break;
4574 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004575 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004576 break;
4577 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004578 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579 break;
4580 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004581 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582 break;
4583 }
4584 }
4585
Nate Begeman9008ca62009-04-27 18:41:29 +00004586 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004587 bool Reverse = (NonZeros & 0x3) == 2;
4588 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004589 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004590 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4591 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4593 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004594 }
4595
Nate Begemanfdea31a2010-03-24 20:49:50 +00004596 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4597 // Check for a build vector of consecutive loads.
4598 for (unsigned i = 0; i < NumElems; ++i)
4599 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004600
Nate Begemanfdea31a2010-03-24 20:49:50 +00004601 // Check for elements which are consecutive loads.
4602 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4603 if (LD.getNode())
4604 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004605
4606 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004607 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004608 SDValue Result;
4609 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4610 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4611 else
4612 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004613
Chris Lattner24faf612010-08-28 17:59:08 +00004614 for (unsigned i = 1; i < NumElems; ++i) {
4615 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4616 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004617 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004618 }
4619 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004620 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004621
Chris Lattner6e80e442010-08-28 17:15:43 +00004622 // Otherwise, expand into a number of unpckl*, start by extending each of
4623 // our (non-undef) elements to the full vector width with the element in the
4624 // bottom slot of the vector (which generates no code for SSE).
4625 for (unsigned i = 0; i < NumElems; ++i) {
4626 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4627 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4628 else
4629 V[i] = DAG.getUNDEF(VT);
4630 }
4631
4632 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4634 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4635 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004636 unsigned EltStride = NumElems >> 1;
4637 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004638 for (unsigned i = 0; i < EltStride; ++i) {
4639 // If V[i+EltStride] is undef and this is the first round of mixing,
4640 // then it is safe to just drop this shuffle: V[i] is already in the
4641 // right place, the one element (since it's the first round) being
4642 // inserted as undef can be dropped. This isn't safe for successive
4643 // rounds because they will permute elements within both vectors.
4644 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4645 EltStride == NumElems/2)
4646 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004647
Chris Lattner6e80e442010-08-28 17:15:43 +00004648 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004649 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004650 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004651 }
4652 return V[0];
4653 }
Dan Gohman475871a2008-07-27 21:46:04 +00004654 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655}
4656
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004657SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004658X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004659 // We support concatenate two MMX registers and place them in a MMX
4660 // register. This is better than doing a stack convert.
4661 DebugLoc dl = Op.getDebugLoc();
4662 EVT ResVT = Op.getValueType();
4663 assert(Op.getNumOperands() == 2);
4664 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4665 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4666 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004667 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004668 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4669 InVec = Op.getOperand(1);
4670 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4671 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004672 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004673 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4674 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4675 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004676 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004677 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4678 Mask[0] = 0; Mask[1] = 2;
4679 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4680 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004681 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004682}
4683
Nate Begemanb9a47b82009-02-23 08:49:38 +00004684// v8i16 shuffles - Prefer shuffles in the following order:
4685// 1. [all] pshuflw, pshufhw, optional move
4686// 2. [ssse3] 1 x pshufb
4687// 3. [ssse3] 2 x pshufb + 1 x por
4688// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004689SDValue
4690X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4691 SelectionDAG &DAG) const {
4692 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 SDValue V1 = SVOp->getOperand(0);
4694 SDValue V2 = SVOp->getOperand(1);
4695 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004696 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004697
Nate Begemanb9a47b82009-02-23 08:49:38 +00004698 // Determine if more than 1 of the words in each of the low and high quadwords
4699 // of the result come from the same quadword of one of the two inputs. Undef
4700 // mask values count as coming from any quadword, for better codegen.
4701 SmallVector<unsigned, 4> LoQuad(4);
4702 SmallVector<unsigned, 4> HiQuad(4);
4703 BitVector InputQuads(4);
4704 for (unsigned i = 0; i < 8; ++i) {
4705 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004706 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004707 MaskVals.push_back(EltIdx);
4708 if (EltIdx < 0) {
4709 ++Quad[0];
4710 ++Quad[1];
4711 ++Quad[2];
4712 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004713 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004714 }
4715 ++Quad[EltIdx / 4];
4716 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004717 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004718
Nate Begemanb9a47b82009-02-23 08:49:38 +00004719 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004720 unsigned MaxQuad = 1;
4721 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004722 if (LoQuad[i] > MaxQuad) {
4723 BestLoQuad = i;
4724 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004725 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004726 }
4727
Nate Begemanb9a47b82009-02-23 08:49:38 +00004728 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004729 MaxQuad = 1;
4730 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004731 if (HiQuad[i] > MaxQuad) {
4732 BestHiQuad = i;
4733 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004734 }
4735 }
4736
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004738 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004739 // single pshufb instruction is necessary. If There are more than 2 input
4740 // quads, disable the next transformation since it does not help SSSE3.
4741 bool V1Used = InputQuads[0] || InputQuads[1];
4742 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004743 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004744 if (InputQuads.count() == 2 && V1Used && V2Used) {
4745 BestLoQuad = InputQuads.find_first();
4746 BestHiQuad = InputQuads.find_next(BestLoQuad);
4747 }
4748 if (InputQuads.count() > 2) {
4749 BestLoQuad = -1;
4750 BestHiQuad = -1;
4751 }
4752 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004753
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4755 // the shuffle mask. If a quad is scored as -1, that means that it contains
4756 // words from all 4 input quadwords.
4757 SDValue NewV;
4758 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004759 SmallVector<int, 8> MaskV;
4760 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4761 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004762 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004763 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4764 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4765 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004766
Nate Begemanb9a47b82009-02-23 08:49:38 +00004767 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4768 // source words for the shuffle, to aid later transformations.
4769 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004770 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004771 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004772 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004773 if (idx != (int)i)
4774 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004775 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004776 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 AllWordsInNewV = false;
4778 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004779 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004780
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4782 if (AllWordsInNewV) {
4783 for (int i = 0; i != 8; ++i) {
4784 int idx = MaskVals[i];
4785 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004786 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004787 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004788 if ((idx != i) && idx < 4)
4789 pshufhw = false;
4790 if ((idx != i) && idx > 3)
4791 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004792 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004793 V1 = NewV;
4794 V2Used = false;
4795 BestLoQuad = 0;
4796 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004797 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004798
Nate Begemanb9a47b82009-02-23 08:49:38 +00004799 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4800 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004801 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004802 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4803 unsigned TargetMask = 0;
4804 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004805 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004806 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4807 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4808 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004809 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004810 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004811 }
Eric Christopherfd179292009-08-27 18:07:15 +00004812
Nate Begemanb9a47b82009-02-23 08:49:38 +00004813 // If we have SSSE3, and all words of the result are from 1 input vector,
4814 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4815 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004816 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004818
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004820 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 // mask, and elements that come from V1 in the V2 mask, so that the two
4822 // results can be OR'd together.
4823 bool TwoInputs = V1Used && V2Used;
4824 for (unsigned i = 0; i != 8; ++i) {
4825 int EltIdx = MaskVals[i] * 2;
4826 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4828 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 continue;
4830 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4832 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004833 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004834 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004835 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004836 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004839 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004840
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 // Calculate the shuffle mask for the second input, shuffle it, and
4842 // OR it with the first shuffled input.
4843 pshufbMask.clear();
4844 for (unsigned i = 0; i != 8; ++i) {
4845 int EltIdx = MaskVals[i] * 2;
4846 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4848 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004849 continue;
4850 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4852 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004854 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004855 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004856 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 MVT::v16i8, &pshufbMask[0], 16));
4858 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004859 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004860 }
4861
4862 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4863 // and update MaskVals with new element order.
4864 BitVector InOrder(8);
4865 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004866 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004867 for (int i = 0; i != 4; ++i) {
4868 int idx = MaskVals[i];
4869 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004870 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004871 InOrder.set(i);
4872 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004873 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004874 InOrder.set(i);
4875 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004876 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004877 }
4878 }
4879 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004880 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004882 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004883
4884 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4885 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4886 NewV.getOperand(0),
4887 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4888 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004889 }
Eric Christopherfd179292009-08-27 18:07:15 +00004890
Nate Begemanb9a47b82009-02-23 08:49:38 +00004891 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4892 // and update MaskVals with the new element order.
4893 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004894 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004896 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004897 for (unsigned i = 4; i != 8; ++i) {
4898 int idx = MaskVals[i];
4899 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004900 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004901 InOrder.set(i);
4902 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004903 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004904 InOrder.set(i);
4905 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004906 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004907 }
4908 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004910 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004911
4912 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4913 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4914 NewV.getOperand(0),
4915 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4916 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 }
Eric Christopherfd179292009-08-27 18:07:15 +00004918
Nate Begemanb9a47b82009-02-23 08:49:38 +00004919 // In case BestHi & BestLo were both -1, which means each quadword has a word
4920 // from each of the four input quadwords, calculate the InOrder bitvector now
4921 // before falling through to the insert/extract cleanup.
4922 if (BestLoQuad == -1 && BestHiQuad == -1) {
4923 NewV = V1;
4924 for (int i = 0; i != 8; ++i)
4925 if (MaskVals[i] < 0 || MaskVals[i] == i)
4926 InOrder.set(i);
4927 }
Eric Christopherfd179292009-08-27 18:07:15 +00004928
Nate Begemanb9a47b82009-02-23 08:49:38 +00004929 // The other elements are put in the right place using pextrw and pinsrw.
4930 for (unsigned i = 0; i != 8; ++i) {
4931 if (InOrder[i])
4932 continue;
4933 int EltIdx = MaskVals[i];
4934 if (EltIdx < 0)
4935 continue;
4936 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004938 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004939 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004940 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004941 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004942 DAG.getIntPtrConstant(i));
4943 }
4944 return NewV;
4945}
4946
4947// v16i8 shuffles - Prefer shuffles in the following order:
4948// 1. [ssse3] 1 x pshufb
4949// 2. [ssse3] 2 x pshufb + 1 x por
4950// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4951static
Nate Begeman9008ca62009-04-27 18:41:29 +00004952SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004953 SelectionDAG &DAG,
4954 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 SDValue V1 = SVOp->getOperand(0);
4956 SDValue V2 = SVOp->getOperand(1);
4957 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004958 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004959 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004960
Nate Begemanb9a47b82009-02-23 08:49:38 +00004961 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004962 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004963 // present, fall back to case 3.
4964 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4965 bool V1Only = true;
4966 bool V2Only = true;
4967 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004968 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004969 if (EltIdx < 0)
4970 continue;
4971 if (EltIdx < 16)
4972 V2Only = false;
4973 else
4974 V1Only = false;
4975 }
Eric Christopherfd179292009-08-27 18:07:15 +00004976
Nate Begemanb9a47b82009-02-23 08:49:38 +00004977 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4978 if (TLI.getSubtarget()->hasSSSE3()) {
4979 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004980
Nate Begemanb9a47b82009-02-23 08:49:38 +00004981 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004982 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004983 //
4984 // Otherwise, we have elements from both input vectors, and must zero out
4985 // elements that come from V2 in the first mask, and V1 in the second mask
4986 // so that we can OR them together.
4987 bool TwoInputs = !(V1Only || V2Only);
4988 for (unsigned i = 0; i != 16; ++i) {
4989 int EltIdx = MaskVals[i];
4990 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004991 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004992 continue;
4993 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004994 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004995 }
4996 // If all the elements are from V2, assign it to V1 and return after
4997 // building the first pshufb.
4998 if (V2Only)
4999 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005001 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005002 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005003 if (!TwoInputs)
5004 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005005
Nate Begemanb9a47b82009-02-23 08:49:38 +00005006 // Calculate the shuffle mask for the second input, shuffle it, and
5007 // OR it with the first shuffled input.
5008 pshufbMask.clear();
5009 for (unsigned i = 0; i != 16; ++i) {
5010 int EltIdx = MaskVals[i];
5011 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005012 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005013 continue;
5014 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005015 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005016 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005017 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005018 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005019 MVT::v16i8, &pshufbMask[0], 16));
5020 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 }
Eric Christopherfd179292009-08-27 18:07:15 +00005022
Nate Begemanb9a47b82009-02-23 08:49:38 +00005023 // No SSSE3 - Calculate in place words and then fix all out of place words
5024 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5025 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005026 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5027 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005028 SDValue NewV = V2Only ? V2 : V1;
5029 for (int i = 0; i != 8; ++i) {
5030 int Elt0 = MaskVals[i*2];
5031 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005032
Nate Begemanb9a47b82009-02-23 08:49:38 +00005033 // This word of the result is all undef, skip it.
5034 if (Elt0 < 0 && Elt1 < 0)
5035 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005036
Nate Begemanb9a47b82009-02-23 08:49:38 +00005037 // This word of the result is already in the correct place, skip it.
5038 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5039 continue;
5040 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5041 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005042
Nate Begemanb9a47b82009-02-23 08:49:38 +00005043 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5044 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5045 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005046
5047 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5048 // using a single extract together, load it and store it.
5049 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005050 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005051 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005053 DAG.getIntPtrConstant(i));
5054 continue;
5055 }
5056
Nate Begemanb9a47b82009-02-23 08:49:38 +00005057 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005058 // source byte is not also odd, shift the extracted word left 8 bits
5059 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005060 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005062 DAG.getIntPtrConstant(Elt1 / 2));
5063 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005064 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005065 DAG.getConstant(8,
5066 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005067 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005068 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5069 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005070 }
5071 // If Elt0 is defined, extract it from the appropriate source. If the
5072 // source byte is not also even, shift the extracted word right 8 bits. If
5073 // Elt1 was also defined, OR the extracted values together before
5074 // inserting them in the result.
5075 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005076 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005077 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5078 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005079 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005080 DAG.getConstant(8,
5081 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005082 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5084 DAG.getConstant(0x00FF, MVT::i16));
5085 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005086 : InsElt0;
5087 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005088 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005089 DAG.getIntPtrConstant(i));
5090 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005091 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005092}
5093
Evan Cheng7a831ce2007-12-15 03:00:47 +00005094/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005095/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005096/// done when every pair / quad of shuffle mask elements point to elements in
5097/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005098/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005099static
Nate Begeman9008ca62009-04-27 18:41:29 +00005100SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005101 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005102 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005103 SDValue V1 = SVOp->getOperand(0);
5104 SDValue V2 = SVOp->getOperand(1);
5105 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005106 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005107 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005109 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 case MVT::v4f32: NewVT = MVT::v2f64; break;
5111 case MVT::v4i32: NewVT = MVT::v2i64; break;
5112 case MVT::v8i16: NewVT = MVT::v4i32; break;
5113 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005114 }
5115
Nate Begeman9008ca62009-04-27 18:41:29 +00005116 int Scale = NumElems / NewWidth;
5117 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005118 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005119 int StartIdx = -1;
5120 for (int j = 0; j < Scale; ++j) {
5121 int EltIdx = SVOp->getMaskElt(i+j);
5122 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005123 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005124 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005125 StartIdx = EltIdx - (EltIdx % Scale);
5126 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005127 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005128 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005129 if (StartIdx == -1)
5130 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005131 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005132 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005133 }
5134
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005135 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5136 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005137 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005138}
5139
Evan Chengd880b972008-05-09 21:53:03 +00005140/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005141///
Owen Andersone50ed302009-08-10 22:56:29 +00005142static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005143 SDValue SrcOp, SelectionDAG &DAG,
5144 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005146 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005147 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005148 LD = dyn_cast<LoadSDNode>(SrcOp);
5149 if (!LD) {
5150 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5151 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005152 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005153 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005154 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005155 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005156 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005157 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005158 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005159 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005160 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5161 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5162 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005163 SrcOp.getOperand(0)
5164 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005165 }
5166 }
5167 }
5168
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005169 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005170 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005171 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005172 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005173}
5174
Evan Chengace3c172008-07-22 21:13:36 +00005175/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5176/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005177static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005178LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5179 SDValue V1 = SVOp->getOperand(0);
5180 SDValue V2 = SVOp->getOperand(1);
5181 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005182 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005183
Evan Chengace3c172008-07-22 21:13:36 +00005184 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005185 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005186 SmallVector<int, 8> Mask1(4U, -1);
5187 SmallVector<int, 8> PermMask;
5188 SVOp->getMask(PermMask);
5189
Evan Chengace3c172008-07-22 21:13:36 +00005190 unsigned NumHi = 0;
5191 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005192 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005193 int Idx = PermMask[i];
5194 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005195 Locs[i] = std::make_pair(-1, -1);
5196 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005197 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5198 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005199 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005200 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005201 NumLo++;
5202 } else {
5203 Locs[i] = std::make_pair(1, NumHi);
5204 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005205 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005206 NumHi++;
5207 }
5208 }
5209 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005210
Evan Chengace3c172008-07-22 21:13:36 +00005211 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005212 // If no more than two elements come from either vector. This can be
5213 // implemented with two shuffles. First shuffle gather the elements.
5214 // The second shuffle, which takes the first shuffle as both of its
5215 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005216 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005217
Nate Begeman9008ca62009-04-27 18:41:29 +00005218 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005219
Evan Chengace3c172008-07-22 21:13:36 +00005220 for (unsigned i = 0; i != 4; ++i) {
5221 if (Locs[i].first == -1)
5222 continue;
5223 else {
5224 unsigned Idx = (i < 2) ? 0 : 4;
5225 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005226 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005227 }
5228 }
5229
Nate Begeman9008ca62009-04-27 18:41:29 +00005230 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005231 } else if (NumLo == 3 || NumHi == 3) {
5232 // Otherwise, we must have three elements from one vector, call it X, and
5233 // one element from the other, call it Y. First, use a shufps to build an
5234 // intermediate vector with the one element from Y and the element from X
5235 // that will be in the same half in the final destination (the indexes don't
5236 // matter). Then, use a shufps to build the final vector, taking the half
5237 // containing the element from Y from the intermediate, and the other half
5238 // from X.
5239 if (NumHi == 3) {
5240 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005241 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005242 std::swap(V1, V2);
5243 }
5244
5245 // Find the element from V2.
5246 unsigned HiIndex;
5247 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005248 int Val = PermMask[HiIndex];
5249 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005250 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005251 if (Val >= 4)
5252 break;
5253 }
5254
Nate Begeman9008ca62009-04-27 18:41:29 +00005255 Mask1[0] = PermMask[HiIndex];
5256 Mask1[1] = -1;
5257 Mask1[2] = PermMask[HiIndex^1];
5258 Mask1[3] = -1;
5259 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005260
5261 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005262 Mask1[0] = PermMask[0];
5263 Mask1[1] = PermMask[1];
5264 Mask1[2] = HiIndex & 1 ? 6 : 4;
5265 Mask1[3] = HiIndex & 1 ? 4 : 6;
5266 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005267 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005268 Mask1[0] = HiIndex & 1 ? 2 : 0;
5269 Mask1[1] = HiIndex & 1 ? 0 : 2;
5270 Mask1[2] = PermMask[2];
5271 Mask1[3] = PermMask[3];
5272 if (Mask1[2] >= 0)
5273 Mask1[2] += 4;
5274 if (Mask1[3] >= 0)
5275 Mask1[3] += 4;
5276 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005277 }
Evan Chengace3c172008-07-22 21:13:36 +00005278 }
5279
5280 // Break it into (shuffle shuffle_hi, shuffle_lo).
5281 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005282 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005283 SmallVector<int,8> LoMask(4U, -1);
5284 SmallVector<int,8> HiMask(4U, -1);
5285
5286 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005287 unsigned MaskIdx = 0;
5288 unsigned LoIdx = 0;
5289 unsigned HiIdx = 2;
5290 for (unsigned i = 0; i != 4; ++i) {
5291 if (i == 2) {
5292 MaskPtr = &HiMask;
5293 MaskIdx = 1;
5294 LoIdx = 0;
5295 HiIdx = 2;
5296 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005297 int Idx = PermMask[i];
5298 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005299 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005301 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005302 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005303 LoIdx++;
5304 } else {
5305 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005307 HiIdx++;
5308 }
5309 }
5310
Nate Begeman9008ca62009-04-27 18:41:29 +00005311 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5312 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5313 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005314 for (unsigned i = 0; i != 4; ++i) {
5315 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005316 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005317 } else {
5318 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005320 }
5321 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005322 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005323}
5324
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005325static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005326 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005327 V = V.getOperand(0);
5328 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5329 V = V.getOperand(0);
5330 if (MayFoldLoad(V))
5331 return true;
5332 return false;
5333}
5334
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005335// FIXME: the version above should always be used. Since there's
5336// a bug where several vector shuffles can't be folded because the
5337// DAG is not updated during lowering and a node claims to have two
5338// uses while it only has one, use this version, and let isel match
5339// another instruction if the load really happens to have more than
5340// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005341// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005342static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005343 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005344 V = V.getOperand(0);
5345 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5346 V = V.getOperand(0);
5347 if (ISD::isNormalLoad(V.getNode()))
5348 return true;
5349 return false;
5350}
5351
5352/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5353/// a vector extract, and if both can be later optimized into a single load.
5354/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5355/// here because otherwise a target specific shuffle node is going to be
5356/// emitted for this shuffle, and the optimization not done.
5357/// FIXME: This is probably not the best approach, but fix the problem
5358/// until the right path is decided.
5359static
5360bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5361 const TargetLowering &TLI) {
5362 EVT VT = V.getValueType();
5363 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5364
5365 // Be sure that the vector shuffle is present in a pattern like this:
5366 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5367 if (!V.hasOneUse())
5368 return false;
5369
5370 SDNode *N = *V.getNode()->use_begin();
5371 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5372 return false;
5373
5374 SDValue EltNo = N->getOperand(1);
5375 if (!isa<ConstantSDNode>(EltNo))
5376 return false;
5377
5378 // If the bit convert changed the number of elements, it is unsafe
5379 // to examine the mask.
5380 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005381 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005382 EVT SrcVT = V.getOperand(0).getValueType();
5383 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5384 return false;
5385 V = V.getOperand(0);
5386 HasShuffleIntoBitcast = true;
5387 }
5388
5389 // Select the input vector, guarding against out of range extract vector.
5390 unsigned NumElems = VT.getVectorNumElements();
5391 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5392 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5393 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5394
5395 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005396 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005397 V = V.getOperand(0);
5398
5399 if (ISD::isNormalLoad(V.getNode())) {
5400 // Is the original load suitable?
5401 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5402
5403 // FIXME: avoid the multi-use bug that is preventing lots of
5404 // of foldings to be detected, this is still wrong of course, but
5405 // give the temporary desired behavior, and if it happens that
5406 // the load has real more uses, during isel it will not fold, and
5407 // will generate poor code.
5408 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5409 return false;
5410
5411 if (!HasShuffleIntoBitcast)
5412 return true;
5413
5414 // If there's a bitcast before the shuffle, check if the load type and
5415 // alignment is valid.
5416 unsigned Align = LN0->getAlignment();
5417 unsigned NewAlign =
5418 TLI.getTargetData()->getABITypeAlignment(
5419 VT.getTypeForEVT(*DAG.getContext()));
5420
5421 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5422 return false;
5423 }
5424
5425 return true;
5426}
5427
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005428static
Evan Cheng835580f2010-10-07 20:50:20 +00005429SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5430 EVT VT = Op.getValueType();
5431
5432 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005433 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5434 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005435 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5436 V1, DAG));
5437}
5438
5439static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005440SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5441 bool HasSSE2) {
5442 SDValue V1 = Op.getOperand(0);
5443 SDValue V2 = Op.getOperand(1);
5444 EVT VT = Op.getValueType();
5445
5446 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5447
5448 if (HasSSE2 && VT == MVT::v2f64)
5449 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5450
5451 // v4f32 or v4i32
5452 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5453}
5454
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005455static
5456SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5457 SDValue V1 = Op.getOperand(0);
5458 SDValue V2 = Op.getOperand(1);
5459 EVT VT = Op.getValueType();
5460
5461 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5462 "unsupported shuffle type");
5463
5464 if (V2.getOpcode() == ISD::UNDEF)
5465 V2 = V1;
5466
5467 // v4i32 or v4f32
5468 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5469}
5470
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005471static
5472SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5473 SDValue V1 = Op.getOperand(0);
5474 SDValue V2 = Op.getOperand(1);
5475 EVT VT = Op.getValueType();
5476 unsigned NumElems = VT.getVectorNumElements();
5477
5478 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5479 // operand of these instructions is only memory, so check if there's a
5480 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5481 // same masks.
5482 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005483
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005484 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005485 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005486 CanFoldLoad = true;
5487
5488 // When V1 is a load, it can be folded later into a store in isel, example:
5489 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5490 // turns into:
5491 // (MOVLPSmr addr:$src1, VR128:$src2)
5492 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005493 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005494 CanFoldLoad = true;
5495
Eric Christopher893a8822011-02-20 05:04:42 +00005496 // Both of them can't be memory operations though.
5497 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5498 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005499
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005500 if (CanFoldLoad) {
5501 if (HasSSE2 && NumElems == 2)
5502 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5503
5504 if (NumElems == 4)
5505 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5506 }
5507
5508 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5509 // movl and movlp will both match v2i64, but v2i64 is never matched by
5510 // movl earlier because we make it strict to avoid messing with the movlp load
5511 // folding logic (see the code above getMOVLP call). Match it here then,
5512 // this is horrible, but will stay like this until we move all shuffle
5513 // matching to x86 specific nodes. Note that for the 1st condition all
5514 // types are matched with movsd.
5515 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5516 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5517 else if (HasSSE2)
5518 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5519
5520
5521 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5522
5523 // Invert the operand order and use SHUFPS to match it.
5524 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5525 X86::getShuffleSHUFImmediate(SVOp), DAG);
5526}
5527
David Greenec4db4e52011-02-28 19:06:56 +00005528static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005529 switch(VT.getSimpleVT().SimpleTy) {
5530 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5531 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005532 case MVT::v4f32:
5533 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5534 case MVT::v2f64:
5535 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5536 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5537 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005538 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5539 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5540 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005541 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005542 }
5543 return 0;
5544}
5545
5546static inline unsigned getUNPCKHOpcode(EVT VT) {
5547 switch(VT.getSimpleVT().SimpleTy) {
5548 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5549 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5550 case MVT::v4f32: return X86ISD::UNPCKHPS;
5551 case MVT::v2f64: return X86ISD::UNPCKHPD;
5552 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5553 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5554 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005555 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005556 }
5557 return 0;
5558}
5559
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005560static
5561SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005562 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005563 const X86Subtarget *Subtarget) {
5564 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5565 EVT VT = Op.getValueType();
5566 DebugLoc dl = Op.getDebugLoc();
5567 SDValue V1 = Op.getOperand(0);
5568 SDValue V2 = Op.getOperand(1);
5569
5570 if (isZeroShuffle(SVOp))
5571 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5572
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005573 // Handle splat operations
5574 if (SVOp->isSplat()) {
5575 // Special case, this is the only place now where it's
5576 // allowed to return a vector_shuffle operation without
5577 // using a target specific node, because *hopefully* it
5578 // will be optimized away by the dag combiner.
5579 if (VT.getVectorNumElements() <= 4 &&
5580 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5581 return Op;
5582
5583 // Handle splats by matching through known masks
5584 if (VT.getVectorNumElements() <= 4)
5585 return SDValue();
5586
Evan Cheng835580f2010-10-07 20:50:20 +00005587 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005588 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005589 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005590
5591 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5592 // do it!
5593 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5594 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5595 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005596 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005597 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5598 // FIXME: Figure out a cleaner way to do this.
5599 // Try to make use of movq to zero out the top part.
5600 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5601 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5602 if (NewOp.getNode()) {
5603 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5604 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5605 DAG, Subtarget, dl);
5606 }
5607 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5608 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5609 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5610 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5611 DAG, Subtarget, dl);
5612 }
5613 }
5614 return SDValue();
5615}
5616
Dan Gohman475871a2008-07-27 21:46:04 +00005617SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005618X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005619 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005620 SDValue V1 = Op.getOperand(0);
5621 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005622 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005623 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005625 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005626 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5627 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005628 bool V1IsSplat = false;
5629 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005630 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005631 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005632 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005633 MachineFunction &MF = DAG.getMachineFunction();
5634 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005635
Dale Johannesen0488fb62010-09-30 23:57:10 +00005636 // Shuffle operations on MMX not supported.
5637 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005638 return Op;
5639
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005640 // Vector shuffle lowering takes 3 steps:
5641 //
5642 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5643 // narrowing and commutation of operands should be handled.
5644 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5645 // shuffle nodes.
5646 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5647 // so the shuffle can be broken into other shuffles and the legalizer can
5648 // try the lowering again.
5649 //
5650 // The general ideia is that no vector_shuffle operation should be left to
5651 // be matched during isel, all of them must be converted to a target specific
5652 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005653
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005654 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5655 // narrowing and commutation of operands should be handled. The actual code
5656 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005657 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005658 if (NewOp.getNode())
5659 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005660
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005661 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5662 // unpckh_undef). Only use pshufd if speed is more important than size.
5663 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5664 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005665 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005666 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5667 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5668 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005669
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005670 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005671 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005672 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005673
Dale Johannesen0488fb62010-09-30 23:57:10 +00005674 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005675 return getMOVHighToLow(Op, dl, DAG);
5676
5677 // Use to match splats
5678 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5679 (VT == MVT::v2f64 || VT == MVT::v2i64))
5680 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5681
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005682 if (X86::isPSHUFDMask(SVOp)) {
5683 // The actual implementation will match the mask in the if above and then
5684 // during isel it can match several different instructions, not only pshufd
5685 // as its name says, sad but true, emulate the behavior for now...
5686 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5687 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5688
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005689 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5690
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005691 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005692 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5693
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005694 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005695 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5696 TargetMask, DAG);
5697
5698 if (VT == MVT::v4f32)
5699 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5700 TargetMask, DAG);
5701 }
Eric Christopherfd179292009-08-27 18:07:15 +00005702
Evan Chengf26ffe92008-05-29 08:22:04 +00005703 // Check if this can be converted into a logical shift.
5704 bool isLeft = false;
5705 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005706 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005707 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005708 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005709 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005710 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005711 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005712 EVT EltVT = VT.getVectorElementType();
5713 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005714 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005715 }
Eric Christopherfd179292009-08-27 18:07:15 +00005716
Nate Begeman9008ca62009-04-27 18:41:29 +00005717 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005718 if (V1IsUndef)
5719 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005720 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005721 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005722 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005723 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005724 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5725
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005726 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005727 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5728 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005729 }
Eric Christopherfd179292009-08-27 18:07:15 +00005730
Nate Begeman9008ca62009-04-27 18:41:29 +00005731 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005732 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5733 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005734
Dale Johannesen0488fb62010-09-30 23:57:10 +00005735 if (X86::isMOVHLPSMask(SVOp))
5736 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005737
Dale Johannesen0488fb62010-09-30 23:57:10 +00005738 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5739 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005740
Dale Johannesen0488fb62010-09-30 23:57:10 +00005741 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5742 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005743
Dale Johannesen0488fb62010-09-30 23:57:10 +00005744 if (X86::isMOVLPMask(SVOp))
5745 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005746
Nate Begeman9008ca62009-04-27 18:41:29 +00005747 if (ShouldXformToMOVHLPS(SVOp) ||
5748 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5749 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005750
Evan Chengf26ffe92008-05-29 08:22:04 +00005751 if (isShift) {
5752 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005753 EVT EltVT = VT.getVectorElementType();
5754 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005755 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005756 }
Eric Christopherfd179292009-08-27 18:07:15 +00005757
Evan Cheng9eca5e82006-10-25 21:49:50 +00005758 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005759 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5760 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005761 V1IsSplat = isSplatVector(V1.getNode());
5762 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005763
Chris Lattner8a594482007-11-25 00:24:49 +00005764 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005765 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005766 Op = CommuteVectorShuffle(SVOp, DAG);
5767 SVOp = cast<ShuffleVectorSDNode>(Op);
5768 V1 = SVOp->getOperand(0);
5769 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005770 std::swap(V1IsSplat, V2IsSplat);
5771 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005772 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005773 }
5774
Nate Begeman9008ca62009-04-27 18:41:29 +00005775 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5776 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005777 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005778 return V1;
5779 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5780 // the instruction selector will not match, so get a canonical MOVL with
5781 // swapped operands to undo the commute.
5782 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005783 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005784
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005785 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005786 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5787 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005788
5789 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005790 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005791
Evan Cheng9bbbb982006-10-25 20:48:19 +00005792 if (V2IsSplat) {
5793 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005794 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005795 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005796 SDValue NewMask = NormalizeMask(SVOp, DAG);
5797 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5798 if (NSVOp != SVOp) {
5799 if (X86::isUNPCKLMask(NSVOp, true)) {
5800 return NewMask;
5801 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5802 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005803 }
5804 }
5805 }
5806
Evan Cheng9eca5e82006-10-25 21:49:50 +00005807 if (Commuted) {
5808 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005809 // FIXME: this seems wrong.
5810 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5811 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005812
5813 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005814 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5815 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005816
5817 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005818 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005819 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005820
Nate Begeman9008ca62009-04-27 18:41:29 +00005821 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005822 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005823 return CommuteVectorShuffle(SVOp, DAG);
5824
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005825 // The checks below are all present in isShuffleMaskLegal, but they are
5826 // inlined here right now to enable us to directly emit target specific
5827 // nodes, and remove one by one until they don't return Op anymore.
5828 SmallVector<int, 16> M;
5829 SVOp->getMask(M);
5830
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005831 if (isPALIGNRMask(M, VT, HasSSSE3))
5832 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5833 X86::getShufflePALIGNRImmediate(SVOp),
5834 DAG);
5835
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005836 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5837 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005838 if (VT == MVT::v2f64) {
5839 X86ISD::NodeType Opcode =
5840 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5841 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5842 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005843 if (VT == MVT::v2i64)
5844 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5845 }
5846
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005847 if (isPSHUFHWMask(M, VT))
5848 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5849 X86::getShufflePSHUFHWImmediate(SVOp),
5850 DAG);
5851
5852 if (isPSHUFLWMask(M, VT))
5853 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5854 X86::getShufflePSHUFLWImmediate(SVOp),
5855 DAG);
5856
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005857 if (isSHUFPMask(M, VT)) {
5858 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5859 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5860 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5861 TargetMask, DAG);
5862 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5863 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5864 TargetMask, DAG);
5865 }
5866
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005867 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5868 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005869 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5870 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005871 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5872 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5873 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5874
Evan Cheng14b32e12007-12-11 01:46:18 +00005875 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005876 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005877 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005878 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005879 return NewOp;
5880 }
5881
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005883 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 if (NewOp.getNode())
5885 return NewOp;
5886 }
Eric Christopherfd179292009-08-27 18:07:15 +00005887
Dale Johannesen0488fb62010-09-30 23:57:10 +00005888 // Handle all 4 wide cases with a number of shuffles.
5889 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005890 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005891
Dan Gohman475871a2008-07-27 21:46:04 +00005892 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005893}
5894
Dan Gohman475871a2008-07-27 21:46:04 +00005895SDValue
5896X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005897 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005898 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005899 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005900 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005901 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005902 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005904 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005905 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005906 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005907 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5908 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5909 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005910 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5911 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005912 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005913 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005914 Op.getOperand(0)),
5915 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005917 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005919 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005920 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005922 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5923 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005924 // result has a single use which is a store or a bitcast to i32. And in
5925 // the case of a store, it's not worth it if the index is a constant 0,
5926 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005927 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005928 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005929 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005930 if ((User->getOpcode() != ISD::STORE ||
5931 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5932 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005933 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005935 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005937 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005938 Op.getOperand(0)),
5939 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005940 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005941 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005942 // ExtractPS works with constant index.
5943 if (isa<ConstantSDNode>(Op.getOperand(1)))
5944 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005945 }
Dan Gohman475871a2008-07-27 21:46:04 +00005946 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005947}
5948
5949
Dan Gohman475871a2008-07-27 21:46:04 +00005950SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005951X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5952 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005953 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005954 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005955
David Greene74a579d2011-02-10 16:57:36 +00005956 SDValue Vec = Op.getOperand(0);
5957 EVT VecVT = Vec.getValueType();
5958
5959 // If this is a 256-bit vector result, first extract the 128-bit
5960 // vector and then extract from the 128-bit vector.
5961 if (VecVT.getSizeInBits() > 128) {
5962 DebugLoc dl = Op.getNode()->getDebugLoc();
5963 unsigned NumElems = VecVT.getVectorNumElements();
5964 SDValue Idx = Op.getOperand(1);
5965
5966 if (!isa<ConstantSDNode>(Idx))
5967 return SDValue();
5968
5969 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5970 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5971
5972 // Get the 128-bit vector.
5973 bool Upper = IdxVal >= ExtractNumElems;
5974 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5975
5976 // Extract from it.
5977 SDValue ScaledIdx = Idx;
5978 if (Upper)
5979 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5980 DAG.getConstant(ExtractNumElems,
5981 Idx.getValueType()));
5982 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5983 ScaledIdx);
5984 }
5985
5986 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
5987
Evan Cheng62a3f152008-03-24 21:52:23 +00005988 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005989 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005990 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005991 return Res;
5992 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005993
Owen Andersone50ed302009-08-10 22:56:29 +00005994 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005995 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005996 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005997 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005998 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005999 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006000 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6002 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006003 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006005 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006006 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006007 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006008 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006009 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006010 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006011 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006012 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006013 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006014 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006015 if (Idx == 0)
6016 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006017
Evan Cheng0db9fe62006-04-25 20:13:52 +00006018 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006019 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006020 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006021 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006022 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006023 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006024 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006025 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006026 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6027 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6028 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006029 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006030 if (Idx == 0)
6031 return Op;
6032
6033 // UNPCKHPD the element to the lowest double word, then movsd.
6034 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6035 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006036 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006037 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006038 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006040 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006041 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006042 }
6043
Dan Gohman475871a2008-07-27 21:46:04 +00006044 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006045}
6046
Dan Gohman475871a2008-07-27 21:46:04 +00006047SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006048X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6049 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006050 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006051 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006052 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006053
Dan Gohman475871a2008-07-27 21:46:04 +00006054 SDValue N0 = Op.getOperand(0);
6055 SDValue N1 = Op.getOperand(1);
6056 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006057
Dan Gohman8a55ce42009-09-23 21:02:20 +00006058 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006059 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006060 unsigned Opc;
6061 if (VT == MVT::v8i16)
6062 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006063 else if (VT == MVT::v16i8)
6064 Opc = X86ISD::PINSRB;
6065 else
6066 Opc = X86ISD::PINSRB;
6067
Nate Begeman14d12ca2008-02-11 04:19:36 +00006068 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6069 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006070 if (N1.getValueType() != MVT::i32)
6071 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6072 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006073 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006074 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006075 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006076 // Bits [7:6] of the constant are the source select. This will always be
6077 // zero here. The DAG Combiner may combine an extract_elt index into these
6078 // bits. For example (insert (extract, 3), 2) could be matched by putting
6079 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006080 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006081 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006082 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006083 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006084 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006085 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006086 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006087 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006088 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006089 // PINSR* works with constant index.
6090 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006091 }
Dan Gohman475871a2008-07-27 21:46:04 +00006092 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006093}
6094
Dan Gohman475871a2008-07-27 21:46:04 +00006095SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006096X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006097 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006098 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006099
David Greene6b381262011-02-09 15:32:06 +00006100 DebugLoc dl = Op.getDebugLoc();
6101 SDValue N0 = Op.getOperand(0);
6102 SDValue N1 = Op.getOperand(1);
6103 SDValue N2 = Op.getOperand(2);
6104
6105 // If this is a 256-bit vector result, first insert into a 128-bit
6106 // vector and then insert into the 256-bit vector.
6107 if (VT.getSizeInBits() > 128) {
6108 if (!isa<ConstantSDNode>(N2))
6109 return SDValue();
6110
6111 // Get the 128-bit vector.
6112 unsigned NumElems = VT.getVectorNumElements();
6113 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6114 bool Upper = IdxVal >= NumElems / 2;
6115
6116 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6117
6118 // Insert into it.
6119 SDValue ScaledN2 = N2;
6120 if (Upper)
6121 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006122 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006123 (VT.getSizeInBits() / 128),
6124 N2.getValueType()));
6125 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6126 N1, ScaledN2);
6127
6128 // Insert the 128-bit vector
6129 // FIXME: Why UNDEF?
6130 return Insert128BitVector(N0, Op, N2, DAG, dl);
6131 }
6132
Nate Begeman14d12ca2008-02-11 04:19:36 +00006133 if (Subtarget->hasSSE41())
6134 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6135
Dan Gohman8a55ce42009-09-23 21:02:20 +00006136 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006137 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006138
Dan Gohman8a55ce42009-09-23 21:02:20 +00006139 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006140 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6141 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006142 if (N1.getValueType() != MVT::i32)
6143 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6144 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006145 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006146 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006147 }
Dan Gohman475871a2008-07-27 21:46:04 +00006148 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006149}
6150
Dan Gohman475871a2008-07-27 21:46:04 +00006151SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006152X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006153 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006154 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006155 EVT OpVT = Op.getValueType();
6156
6157 // If this is a 256-bit vector result, first insert into a 128-bit
6158 // vector and then insert into the 256-bit vector.
6159 if (OpVT.getSizeInBits() > 128) {
6160 // Insert into a 128-bit vector.
6161 EVT VT128 = EVT::getVectorVT(*Context,
6162 OpVT.getVectorElementType(),
6163 OpVT.getVectorNumElements() / 2);
6164
6165 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6166
6167 // Insert the 128-bit vector.
6168 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6169 DAG.getConstant(0, MVT::i32),
6170 DAG, dl);
6171 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006172
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006173 if (Op.getValueType() == MVT::v1i64 &&
6174 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006175 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006176
Owen Anderson825b72b2009-08-11 20:47:22 +00006177 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006178 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6179 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006180 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006181 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006182}
6183
David Greene91585092011-01-26 15:38:49 +00006184// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6185// a simple subregister reference or explicit instructions to grab
6186// upper bits of a vector.
6187SDValue
6188X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6189 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006190 DebugLoc dl = Op.getNode()->getDebugLoc();
6191 SDValue Vec = Op.getNode()->getOperand(0);
6192 SDValue Idx = Op.getNode()->getOperand(1);
6193
6194 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6195 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6196 return Extract128BitVector(Vec, Idx, DAG, dl);
6197 }
David Greene91585092011-01-26 15:38:49 +00006198 }
6199 return SDValue();
6200}
6201
David Greenecfe33c42011-01-26 19:13:22 +00006202// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6203// simple superregister reference or explicit instructions to insert
6204// the upper bits of a vector.
6205SDValue
6206X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6207 if (Subtarget->hasAVX()) {
6208 DebugLoc dl = Op.getNode()->getDebugLoc();
6209 SDValue Vec = Op.getNode()->getOperand(0);
6210 SDValue SubVec = Op.getNode()->getOperand(1);
6211 SDValue Idx = Op.getNode()->getOperand(2);
6212
6213 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6214 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006215 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006216 }
6217 }
6218 return SDValue();
6219}
6220
Bill Wendling056292f2008-09-16 21:48:12 +00006221// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6222// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6223// one of the above mentioned nodes. It has to be wrapped because otherwise
6224// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6225// be used to form addressing mode. These wrapped nodes will be selected
6226// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006227SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006228X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006229 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006230
Chris Lattner41621a22009-06-26 19:22:52 +00006231 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6232 // global base reg.
6233 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006234 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006235 CodeModel::Model M = getTargetMachine().getCodeModel();
6236
Chris Lattner4f066492009-07-11 20:29:19 +00006237 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006238 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006239 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006240 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006241 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006242 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006243 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006244
Evan Cheng1606e8e2009-03-13 07:51:59 +00006245 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006246 CP->getAlignment(),
6247 CP->getOffset(), OpFlag);
6248 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006249 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006250 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006251 if (OpFlag) {
6252 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006253 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006254 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006255 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006256 }
6257
6258 return Result;
6259}
6260
Dan Gohmand858e902010-04-17 15:26:15 +00006261SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006262 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006263
Chris Lattner18c59872009-06-27 04:16:01 +00006264 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6265 // global base reg.
6266 unsigned char OpFlag = 0;
6267 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006268 CodeModel::Model M = getTargetMachine().getCodeModel();
6269
Chris Lattner4f066492009-07-11 20:29:19 +00006270 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006271 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006272 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006273 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006274 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006275 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006276 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006277
Chris Lattner18c59872009-06-27 04:16:01 +00006278 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6279 OpFlag);
6280 DebugLoc DL = JT->getDebugLoc();
6281 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006282
Chris Lattner18c59872009-06-27 04:16:01 +00006283 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006284 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006285 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6286 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006287 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006288 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006289
Chris Lattner18c59872009-06-27 04:16:01 +00006290 return Result;
6291}
6292
6293SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006294X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006295 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006296
Chris Lattner18c59872009-06-27 04:16:01 +00006297 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6298 // global base reg.
6299 unsigned char OpFlag = 0;
6300 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006301 CodeModel::Model M = getTargetMachine().getCodeModel();
6302
Chris Lattner4f066492009-07-11 20:29:19 +00006303 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006304 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006305 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006306 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006307 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006308 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006309 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006310
Chris Lattner18c59872009-06-27 04:16:01 +00006311 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006312
Chris Lattner18c59872009-06-27 04:16:01 +00006313 DebugLoc DL = Op.getDebugLoc();
6314 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006315
6316
Chris Lattner18c59872009-06-27 04:16:01 +00006317 // With PIC, the address is actually $g + Offset.
6318 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006319 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006320 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6321 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006322 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006323 Result);
6324 }
Eric Christopherfd179292009-08-27 18:07:15 +00006325
Chris Lattner18c59872009-06-27 04:16:01 +00006326 return Result;
6327}
6328
Dan Gohman475871a2008-07-27 21:46:04 +00006329SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006330X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006331 // Create the TargetBlockAddressAddress node.
6332 unsigned char OpFlags =
6333 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006334 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006335 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006336 DebugLoc dl = Op.getDebugLoc();
6337 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6338 /*isTarget=*/true, OpFlags);
6339
Dan Gohmanf705adb2009-10-30 01:28:02 +00006340 if (Subtarget->isPICStyleRIPRel() &&
6341 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006342 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6343 else
6344 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006345
Dan Gohman29cbade2009-11-20 23:18:13 +00006346 // With PIC, the address is actually $g + Offset.
6347 if (isGlobalRelativeToPICBase(OpFlags)) {
6348 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6349 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6350 Result);
6351 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006352
6353 return Result;
6354}
6355
6356SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006357X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006358 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006359 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006360 // Create the TargetGlobalAddress node, folding in the constant
6361 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006362 unsigned char OpFlags =
6363 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006364 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006365 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006366 if (OpFlags == X86II::MO_NO_FLAG &&
6367 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006368 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006369 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006370 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006371 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006372 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006373 }
Eric Christopherfd179292009-08-27 18:07:15 +00006374
Chris Lattner4f066492009-07-11 20:29:19 +00006375 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006376 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006377 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6378 else
6379 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006380
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006381 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006382 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006383 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6384 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006385 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006386 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006387
Chris Lattner36c25012009-07-10 07:34:39 +00006388 // For globals that require a load from a stub to get the address, emit the
6389 // load.
6390 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006391 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006392 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006393
Dan Gohman6520e202008-10-18 02:06:02 +00006394 // If there was a non-zero offset that we didn't fold, create an explicit
6395 // addition for it.
6396 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006397 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006398 DAG.getConstant(Offset, getPointerTy()));
6399
Evan Cheng0db9fe62006-04-25 20:13:52 +00006400 return Result;
6401}
6402
Evan Chengda43bcf2008-09-24 00:05:32 +00006403SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006404X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006405 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006406 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006407 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006408}
6409
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006410static SDValue
6411GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006412 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006413 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006414 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006415 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006416 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006417 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006418 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006419 GA->getOffset(),
6420 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006421 if (InFlag) {
6422 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006423 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006424 } else {
6425 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006426 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006427 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006428
6429 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006430 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006431
Rafael Espindola15f1b662009-04-24 12:59:40 +00006432 SDValue Flag = Chain.getValue(1);
6433 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006434}
6435
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006436// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006437static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006438LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006439 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006440 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006441 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6442 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006443 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006444 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006445 InFlag = Chain.getValue(1);
6446
Chris Lattnerb903bed2009-06-26 21:20:29 +00006447 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006448}
6449
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006450// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006451static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006452LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006453 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006454 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6455 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006456}
6457
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006458// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6459// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006460static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006461 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006462 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006463 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006464
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006465 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6466 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6467 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006468
Michael J. Spencerec38de22010-10-10 22:04:20 +00006469 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006470 DAG.getIntPtrConstant(0),
6471 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006472
Chris Lattnerb903bed2009-06-26 21:20:29 +00006473 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006474 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6475 // initialexec.
6476 unsigned WrapperKind = X86ISD::Wrapper;
6477 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006478 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006479 } else if (is64Bit) {
6480 assert(model == TLSModel::InitialExec);
6481 OperandFlags = X86II::MO_GOTTPOFF;
6482 WrapperKind = X86ISD::WrapperRIP;
6483 } else {
6484 assert(model == TLSModel::InitialExec);
6485 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006486 }
Eric Christopherfd179292009-08-27 18:07:15 +00006487
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006488 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6489 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006490 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006491 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006492 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006493 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006494
Rafael Espindola9a580232009-02-27 13:37:18 +00006495 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006496 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006497 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006498
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006499 // The address of the thread local variable is the add of the thread
6500 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006501 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006502}
6503
Dan Gohman475871a2008-07-27 21:46:04 +00006504SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006505X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006506
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006507 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006508 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006509
Eric Christopher30ef0e52010-06-03 04:07:48 +00006510 if (Subtarget->isTargetELF()) {
6511 // TODO: implement the "local dynamic" model
6512 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006513
Eric Christopher30ef0e52010-06-03 04:07:48 +00006514 // If GV is an alias then use the aliasee for determining
6515 // thread-localness.
6516 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6517 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006518
6519 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006520 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006521
Eric Christopher30ef0e52010-06-03 04:07:48 +00006522 switch (model) {
6523 case TLSModel::GeneralDynamic:
6524 case TLSModel::LocalDynamic: // not implemented
6525 if (Subtarget->is64Bit())
6526 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6527 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006528
Eric Christopher30ef0e52010-06-03 04:07:48 +00006529 case TLSModel::InitialExec:
6530 case TLSModel::LocalExec:
6531 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6532 Subtarget->is64Bit());
6533 }
6534 } else if (Subtarget->isTargetDarwin()) {
6535 // Darwin only has one model of TLS. Lower to that.
6536 unsigned char OpFlag = 0;
6537 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6538 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006539
Eric Christopher30ef0e52010-06-03 04:07:48 +00006540 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6541 // global base reg.
6542 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6543 !Subtarget->is64Bit();
6544 if (PIC32)
6545 OpFlag = X86II::MO_TLVP_PIC_BASE;
6546 else
6547 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006548 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006549 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006550 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006551 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006552 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006553
Eric Christopher30ef0e52010-06-03 04:07:48 +00006554 // With PIC32, the address is actually $g + Offset.
6555 if (PIC32)
6556 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6557 DAG.getNode(X86ISD::GlobalBaseReg,
6558 DebugLoc(), getPointerTy()),
6559 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006560
Eric Christopher30ef0e52010-06-03 04:07:48 +00006561 // Lowering the machine isd will make sure everything is in the right
6562 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006563 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006564 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006565 SDValue Args[] = { Chain, Offset };
6566 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006567
Eric Christopher30ef0e52010-06-03 04:07:48 +00006568 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6569 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6570 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006571
Eric Christopher30ef0e52010-06-03 04:07:48 +00006572 // And our return value (tls address) is in the standard call return value
6573 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006574 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6575 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006576 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006577
Eric Christopher30ef0e52010-06-03 04:07:48 +00006578 assert(false &&
6579 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006580
Torok Edwinc23197a2009-07-14 16:55:14 +00006581 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006582 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006583}
6584
Evan Cheng0db9fe62006-04-25 20:13:52 +00006585
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006586/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006587/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006588SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006589 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006590 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006591 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006592 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006593 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006594 SDValue ShOpLo = Op.getOperand(0);
6595 SDValue ShOpHi = Op.getOperand(1);
6596 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006597 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006598 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006599 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006600
Dan Gohman475871a2008-07-27 21:46:04 +00006601 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006602 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006603 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6604 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006605 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006606 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6607 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006608 }
Evan Chenge3413162006-01-09 18:33:28 +00006609
Owen Anderson825b72b2009-08-11 20:47:22 +00006610 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6611 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006612 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006614
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006617 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6618 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006619
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006620 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006621 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6622 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006623 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006624 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6625 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006626 }
6627
Dan Gohman475871a2008-07-27 21:46:04 +00006628 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006629 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006630}
Evan Chenga3195e82006-01-12 22:54:21 +00006631
Dan Gohmand858e902010-04-17 15:26:15 +00006632SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6633 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006634 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006635
Dale Johannesen0488fb62010-09-30 23:57:10 +00006636 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006637 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006638
Owen Anderson825b72b2009-08-11 20:47:22 +00006639 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006640 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006641
Eli Friedman36df4992009-05-27 00:47:34 +00006642 // These are really Legal; return the operand so the caller accepts it as
6643 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006645 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006646 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006647 Subtarget->is64Bit()) {
6648 return Op;
6649 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006650
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006651 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006652 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006653 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006654 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006655 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006656 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006657 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006658 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006659 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006660 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6661}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006662
Owen Andersone50ed302009-08-10 22:56:29 +00006663SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006664 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006665 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006666 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006667 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006668 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006669 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006670 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006671 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006672 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006673 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006674
Chris Lattner492a43e2010-09-22 01:28:21 +00006675 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006676
Chris Lattner492a43e2010-09-22 01:28:21 +00006677 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6678 MachineMemOperand *MMO =
6679 DAG.getMachineFunction()
6680 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6681 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006682
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006683 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006684 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6685 X86ISD::FILD, DL,
6686 Tys, Ops, array_lengthof(Ops),
6687 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006689 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006691 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006692
6693 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6694 // shouldn't be necessary except that RFP cannot be live across
6695 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006696 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006697 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6698 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006699 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006700 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006701 SDValue Ops[] = {
6702 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6703 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006704 MachineMemOperand *MMO =
6705 DAG.getMachineFunction()
6706 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006707 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006708
Chris Lattner492a43e2010-09-22 01:28:21 +00006709 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6710 Ops, array_lengthof(Ops),
6711 Op.getValueType(), MMO);
6712 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006713 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006714 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006715 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006716
Evan Cheng0db9fe62006-04-25 20:13:52 +00006717 return Result;
6718}
6719
Bill Wendling8b8a6362009-01-17 03:56:04 +00006720// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006721SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6722 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006723 // This algorithm is not obvious. Here it is in C code, more or less:
6724 /*
6725 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6726 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6727 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006728
Bill Wendling8b8a6362009-01-17 03:56:04 +00006729 // Copy ints to xmm registers.
6730 __m128i xh = _mm_cvtsi32_si128( hi );
6731 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006732
Bill Wendling8b8a6362009-01-17 03:56:04 +00006733 // Combine into low half of a single xmm register.
6734 __m128i x = _mm_unpacklo_epi32( xh, xl );
6735 __m128d d;
6736 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006737
Bill Wendling8b8a6362009-01-17 03:56:04 +00006738 // Merge in appropriate exponents to give the integer bits the right
6739 // magnitude.
6740 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006741
Bill Wendling8b8a6362009-01-17 03:56:04 +00006742 // Subtract away the biases to deal with the IEEE-754 double precision
6743 // implicit 1.
6744 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006745
Bill Wendling8b8a6362009-01-17 03:56:04 +00006746 // All conversions up to here are exact. The correctly rounded result is
6747 // calculated using the current rounding mode using the following
6748 // horizontal add.
6749 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6750 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6751 // store doesn't really need to be here (except
6752 // maybe to zero the other double)
6753 return sd;
6754 }
6755 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006756
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006757 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006758 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006759
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006760 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006761 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006762 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6763 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6764 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6765 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006766 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006767 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006768
Bill Wendling8b8a6362009-01-17 03:56:04 +00006769 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006770 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006771 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006772 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006773 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006774 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006775 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006776
Owen Anderson825b72b2009-08-11 20:47:22 +00006777 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6778 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006779 Op.getOperand(0),
6780 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006781 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6782 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006783 Op.getOperand(0),
6784 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006785 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6786 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006787 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006788 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006789 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006790 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006791 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006792 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006793 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006794 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006795
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006796 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006797 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006798 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6799 DAG.getUNDEF(MVT::v2f64), ShufMask);
6800 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6801 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006802 DAG.getIntPtrConstant(0));
6803}
6804
Bill Wendling8b8a6362009-01-17 03:56:04 +00006805// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006806SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6807 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006808 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006809 // FP constant to bias correct the final result.
6810 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006811 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006812
6813 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006814 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6815 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006816 Op.getOperand(0),
6817 DAG.getIntPtrConstant(0)));
6818
Owen Anderson825b72b2009-08-11 20:47:22 +00006819 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006820 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006821 DAG.getIntPtrConstant(0));
6822
6823 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006824 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006825 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006826 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006827 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006828 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006829 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006830 MVT::v2f64, Bias)));
6831 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006832 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006833 DAG.getIntPtrConstant(0));
6834
6835 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006836 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006837
6838 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006839 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006840
Owen Anderson825b72b2009-08-11 20:47:22 +00006841 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006842 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006843 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006845 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006846 }
6847
6848 // Handle final rounding.
6849 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006850}
6851
Dan Gohmand858e902010-04-17 15:26:15 +00006852SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6853 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006854 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006855 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006856
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006857 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006858 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6859 // the optimization here.
6860 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006861 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006862
Owen Andersone50ed302009-08-10 22:56:29 +00006863 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006864 EVT DstVT = Op.getValueType();
6865 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006866 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006867 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006868 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006869
6870 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006872 if (SrcVT == MVT::i32) {
6873 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6874 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6875 getPointerTy(), StackSlot, WordOff);
6876 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006877 StackSlot, MachinePointerInfo(),
6878 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006879 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006880 OffsetSlot, MachinePointerInfo(),
6881 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006882 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6883 return Fild;
6884 }
6885
6886 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6887 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006888 StackSlot, MachinePointerInfo(),
6889 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006890 // For i64 source, we need to add the appropriate power of 2 if the input
6891 // was negative. This is the same as the optimization in
6892 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6893 // we must be careful to do the computation in x87 extended precision, not
6894 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006895 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6896 MachineMemOperand *MMO =
6897 DAG.getMachineFunction()
6898 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6899 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006900
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006901 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6902 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006903 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6904 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006905
6906 APInt FF(32, 0x5F800000ULL);
6907
6908 // Check whether the sign bit is set.
6909 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6910 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6911 ISD::SETLT);
6912
6913 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6914 SDValue FudgePtr = DAG.getConstantPool(
6915 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6916 getPointerTy());
6917
6918 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6919 SDValue Zero = DAG.getIntPtrConstant(0);
6920 SDValue Four = DAG.getIntPtrConstant(4);
6921 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6922 Zero, Four);
6923 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6924
6925 // Load the value out, extending it from f32 to f80.
6926 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006927 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006928 FudgePtr, MachinePointerInfo::getConstantPool(),
6929 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006930 // Extend everything to 80 bits to force it to be done on x87.
6931 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6932 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006933}
6934
Dan Gohman475871a2008-07-27 21:46:04 +00006935std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006936FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006937 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006938
Owen Andersone50ed302009-08-10 22:56:29 +00006939 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006940
6941 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6943 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006944 }
6945
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6947 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006948 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006950 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006952 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006953 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006954 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006956 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006957 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006958
Evan Cheng87c89352007-10-15 20:11:21 +00006959 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6960 // stack slot.
6961 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006962 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006963 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006964 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006965
Michael J. Spencerec38de22010-10-10 22:04:20 +00006966
6967
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006969 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006970 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006971 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6972 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6973 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006975
Dan Gohman475871a2008-07-27 21:46:04 +00006976 SDValue Chain = DAG.getEntryNode();
6977 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006978 EVT TheVT = Op.getOperand(0).getValueType();
6979 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006980 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006981 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006982 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006983 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006984 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006985 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006986 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006987 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006988
Chris Lattner492a43e2010-09-22 01:28:21 +00006989 MachineMemOperand *MMO =
6990 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6991 MachineMemOperand::MOLoad, MemSize, MemSize);
6992 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6993 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006994 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006995 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006996 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6997 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006998
Chris Lattner07290932010-09-22 01:05:16 +00006999 MachineMemOperand *MMO =
7000 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7001 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007002
Evan Cheng0db9fe62006-04-25 20:13:52 +00007003 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007004 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007005 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7006 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007007
Chris Lattner27a6c732007-11-24 07:07:01 +00007008 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007009}
7010
Dan Gohmand858e902010-04-17 15:26:15 +00007011SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7012 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007013 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007014 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007015
Eli Friedman948e95a2009-05-23 09:59:16 +00007016 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007017 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007018 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7019 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007020
Chris Lattner27a6c732007-11-24 07:07:01 +00007021 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007022 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007023 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007024}
7025
Dan Gohmand858e902010-04-17 15:26:15 +00007026SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7027 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007028 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7029 SDValue FIST = Vals.first, StackSlot = Vals.second;
7030 assert(FIST.getNode() && "Unexpected failure");
7031
7032 // Load the result.
7033 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007034 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007035}
7036
Dan Gohmand858e902010-04-17 15:26:15 +00007037SDValue X86TargetLowering::LowerFABS(SDValue Op,
7038 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007039 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007040 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007041 EVT VT = Op.getValueType();
7042 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007043 if (VT.isVector())
7044 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007045 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007047 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007048 CV.push_back(C);
7049 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007050 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007051 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007052 CV.push_back(C);
7053 CV.push_back(C);
7054 CV.push_back(C);
7055 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007056 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007057 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007058 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007059 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007060 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007061 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007062 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007063}
7064
Dan Gohmand858e902010-04-17 15:26:15 +00007065SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007066 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007067 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007068 EVT VT = Op.getValueType();
7069 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007070 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007071 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007072 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007074 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007075 CV.push_back(C);
7076 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007077 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007078 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007079 CV.push_back(C);
7080 CV.push_back(C);
7081 CV.push_back(C);
7082 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007083 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007084 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007085 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007086 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007087 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007088 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007089 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007090 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007092 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007093 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007094 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007095 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007096 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007097 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007098}
7099
Dan Gohmand858e902010-04-17 15:26:15 +00007100SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007101 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007102 SDValue Op0 = Op.getOperand(0);
7103 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007104 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007105 EVT VT = Op.getValueType();
7106 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007107
7108 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007109 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007110 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007111 SrcVT = VT;
7112 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007113 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007114 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007115 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007116 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007117 }
7118
7119 // At this point the operands and the result should have the same
7120 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007121
Evan Cheng68c47cb2007-01-05 07:55:56 +00007122 // First get the sign bit of second operand.
7123 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 if (SrcVT == 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 Cheng68c47cb2007-01-05 07:55: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 Cheng68c47cb2007-01-05 07:55:56 +00007132 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007133 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007134 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007135 SDValue Mask1 = DAG.getLoad(SrcVT, 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 SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007139
7140 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007141 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007142 // Op0 is MVT::f32, Op1 is MVT::f64.
7143 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7144 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7145 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007146 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007148 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007149 }
7150
Evan Cheng73d6cf12007-01-05 21:37:56 +00007151 // Clear first operand sign bit.
7152 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007153 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007154 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7155 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007156 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007157 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7158 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7159 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7160 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007161 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007162 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007163 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007164 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007165 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007166 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007167 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007168
7169 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007170 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007171}
7172
Dan Gohman076aee32009-03-04 19:44:21 +00007173/// Emit nodes that will be selected as "test Op0,Op0", or something
7174/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007175SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007176 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007177 DebugLoc dl = Op.getDebugLoc();
7178
Dan Gohman31125812009-03-07 01:58:32 +00007179 // CF and OF aren't always set the way we want. Determine which
7180 // of these we need.
7181 bool NeedCF = false;
7182 bool NeedOF = false;
7183 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007184 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007185 case X86::COND_A: case X86::COND_AE:
7186 case X86::COND_B: case X86::COND_BE:
7187 NeedCF = true;
7188 break;
7189 case X86::COND_G: case X86::COND_GE:
7190 case X86::COND_L: case X86::COND_LE:
7191 case X86::COND_O: case X86::COND_NO:
7192 NeedOF = true;
7193 break;
Dan Gohman31125812009-03-07 01:58:32 +00007194 }
7195
Dan Gohman076aee32009-03-04 19:44:21 +00007196 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007197 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7198 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007199 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7200 // Emit a CMP with 0, which is the TEST pattern.
7201 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7202 DAG.getConstant(0, Op.getValueType()));
7203
7204 unsigned Opcode = 0;
7205 unsigned NumOperands = 0;
7206 switch (Op.getNode()->getOpcode()) {
7207 case ISD::ADD:
7208 // Due to an isel shortcoming, be conservative if this add is likely to be
7209 // selected as part of a load-modify-store instruction. When the root node
7210 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7211 // uses of other nodes in the match, such as the ADD in this case. This
7212 // leads to the ADD being left around and reselected, with the result being
7213 // two adds in the output. Alas, even if none our users are stores, that
7214 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7215 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7216 // climbing the DAG back to the root, and it doesn't seem to be worth the
7217 // effort.
7218 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007219 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007220 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7221 goto default_case;
7222
7223 if (ConstantSDNode *C =
7224 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7225 // An add of one will be selected as an INC.
7226 if (C->getAPIntValue() == 1) {
7227 Opcode = X86ISD::INC;
7228 NumOperands = 1;
7229 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007230 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007231
7232 // An add of negative one (subtract of one) will be selected as a DEC.
7233 if (C->getAPIntValue().isAllOnesValue()) {
7234 Opcode = X86ISD::DEC;
7235 NumOperands = 1;
7236 break;
7237 }
Dan Gohman076aee32009-03-04 19:44:21 +00007238 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007239
7240 // Otherwise use a regular EFLAGS-setting add.
7241 Opcode = X86ISD::ADD;
7242 NumOperands = 2;
7243 break;
7244 case ISD::AND: {
7245 // If the primary and result isn't used, don't bother using X86ISD::AND,
7246 // because a TEST instruction will be better.
7247 bool NonFlagUse = false;
7248 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7249 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7250 SDNode *User = *UI;
7251 unsigned UOpNo = UI.getOperandNo();
7252 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7253 // Look pass truncate.
7254 UOpNo = User->use_begin().getOperandNo();
7255 User = *User->use_begin();
7256 }
7257
7258 if (User->getOpcode() != ISD::BRCOND &&
7259 User->getOpcode() != ISD::SETCC &&
7260 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7261 NonFlagUse = true;
7262 break;
7263 }
Dan Gohman076aee32009-03-04 19:44:21 +00007264 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007265
7266 if (!NonFlagUse)
7267 break;
7268 }
7269 // FALL THROUGH
7270 case ISD::SUB:
7271 case ISD::OR:
7272 case ISD::XOR:
7273 // Due to the ISEL shortcoming noted above, be conservative if this op is
7274 // likely to be selected as part of a load-modify-store instruction.
7275 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7276 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7277 if (UI->getOpcode() == ISD::STORE)
7278 goto default_case;
7279
7280 // Otherwise use a regular EFLAGS-setting instruction.
7281 switch (Op.getNode()->getOpcode()) {
7282 default: llvm_unreachable("unexpected operator!");
7283 case ISD::SUB: Opcode = X86ISD::SUB; break;
7284 case ISD::OR: Opcode = X86ISD::OR; break;
7285 case ISD::XOR: Opcode = X86ISD::XOR; break;
7286 case ISD::AND: Opcode = X86ISD::AND; break;
7287 }
7288
7289 NumOperands = 2;
7290 break;
7291 case X86ISD::ADD:
7292 case X86ISD::SUB:
7293 case X86ISD::INC:
7294 case X86ISD::DEC:
7295 case X86ISD::OR:
7296 case X86ISD::XOR:
7297 case X86ISD::AND:
7298 return SDValue(Op.getNode(), 1);
7299 default:
7300 default_case:
7301 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007302 }
7303
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007304 if (Opcode == 0)
7305 // Emit a CMP with 0, which is the TEST pattern.
7306 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7307 DAG.getConstant(0, Op.getValueType()));
7308
7309 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7310 SmallVector<SDValue, 4> Ops;
7311 for (unsigned i = 0; i != NumOperands; ++i)
7312 Ops.push_back(Op.getOperand(i));
7313
7314 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7315 DAG.ReplaceAllUsesWith(Op, New);
7316 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007317}
7318
7319/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7320/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007321SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007322 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007323 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7324 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007325 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007326
7327 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007328 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007329}
7330
Evan Chengd40d03e2010-01-06 19:38:29 +00007331/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7332/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007333SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7334 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007335 SDValue Op0 = And.getOperand(0);
7336 SDValue Op1 = And.getOperand(1);
7337 if (Op0.getOpcode() == ISD::TRUNCATE)
7338 Op0 = Op0.getOperand(0);
7339 if (Op1.getOpcode() == ISD::TRUNCATE)
7340 Op1 = Op1.getOperand(0);
7341
Evan Chengd40d03e2010-01-06 19:38:29 +00007342 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007343 if (Op1.getOpcode() == ISD::SHL)
7344 std::swap(Op0, Op1);
7345 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007346 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7347 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007348 // If we looked past a truncate, check that it's only truncating away
7349 // known zeros.
7350 unsigned BitWidth = Op0.getValueSizeInBits();
7351 unsigned AndBitWidth = And.getValueSizeInBits();
7352 if (BitWidth > AndBitWidth) {
7353 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7354 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7355 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7356 return SDValue();
7357 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007358 LHS = Op1;
7359 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007360 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007361 } else if (Op1.getOpcode() == ISD::Constant) {
7362 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7363 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007364 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7365 LHS = AndLHS.getOperand(0);
7366 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007367 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007368 }
Evan Cheng0488db92007-09-25 01:57:46 +00007369
Evan Chengd40d03e2010-01-06 19:38:29 +00007370 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007371 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007372 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007373 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007374 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007375 // Also promote i16 to i32 for performance / code size reason.
7376 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007377 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007378 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007379
Evan Chengd40d03e2010-01-06 19:38:29 +00007380 // If the operand types disagree, extend the shift amount to match. Since
7381 // BT ignores high bits (like shifts) we can use anyextend.
7382 if (LHS.getValueType() != RHS.getValueType())
7383 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007384
Evan Chengd40d03e2010-01-06 19:38:29 +00007385 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7386 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7387 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7388 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007389 }
7390
Evan Cheng54de3ea2010-01-05 06:52:31 +00007391 return SDValue();
7392}
7393
Dan Gohmand858e902010-04-17 15:26:15 +00007394SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007395 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7396 SDValue Op0 = Op.getOperand(0);
7397 SDValue Op1 = Op.getOperand(1);
7398 DebugLoc dl = Op.getDebugLoc();
7399 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7400
7401 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007402 // Lower (X & (1 << N)) == 0 to BT(X, N).
7403 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7404 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007405 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007406 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007407 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007408 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7409 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7410 if (NewSetCC.getNode())
7411 return NewSetCC;
7412 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007413
Chris Lattner481eebc2010-12-19 21:23:48 +00007414 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7415 // these.
7416 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007417 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7418 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7419 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007420
Chris Lattner481eebc2010-12-19 21:23:48 +00007421 // If the input is a setcc, then reuse the input setcc or use a new one with
7422 // the inverted condition.
7423 if (Op0.getOpcode() == X86ISD::SETCC) {
7424 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7425 bool Invert = (CC == ISD::SETNE) ^
7426 cast<ConstantSDNode>(Op1)->isNullValue();
7427 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007428
Evan Cheng2c755ba2010-02-27 07:36:59 +00007429 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007430 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7431 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7432 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007433 }
7434
Evan Chenge5b51ac2010-04-17 06:13:15 +00007435 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007436 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007437 if (X86CC == X86::COND_INVALID)
7438 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007439
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007440 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007441 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007442 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007443}
7444
Dan Gohmand858e902010-04-17 15:26:15 +00007445SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007446 SDValue Cond;
7447 SDValue Op0 = Op.getOperand(0);
7448 SDValue Op1 = Op.getOperand(1);
7449 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007450 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007451 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7452 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007453 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007454
7455 if (isFP) {
7456 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007457 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7459 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007460 bool Swap = false;
7461
7462 switch (SetCCOpcode) {
7463 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007464 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007465 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007466 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007467 case ISD::SETGT: Swap = true; // Fallthrough
7468 case ISD::SETLT:
7469 case ISD::SETOLT: SSECC = 1; break;
7470 case ISD::SETOGE:
7471 case ISD::SETGE: Swap = true; // Fallthrough
7472 case ISD::SETLE:
7473 case ISD::SETOLE: SSECC = 2; break;
7474 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007475 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007476 case ISD::SETNE: SSECC = 4; break;
7477 case ISD::SETULE: Swap = true;
7478 case ISD::SETUGE: SSECC = 5; break;
7479 case ISD::SETULT: Swap = true;
7480 case ISD::SETUGT: SSECC = 6; break;
7481 case ISD::SETO: SSECC = 7; break;
7482 }
7483 if (Swap)
7484 std::swap(Op0, Op1);
7485
Nate Begemanfb8ead02008-07-25 19:05:58 +00007486 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007487 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007488 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007489 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007490 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7491 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007492 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007493 }
7494 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007495 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007496 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7497 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007498 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007499 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007500 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007501 }
7502 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007503 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007504 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007505
Nate Begeman30a0de92008-07-17 16:51:19 +00007506 // We are handling one of the integer comparisons here. Since SSE only has
7507 // GT and EQ comparisons for integer, swapping operands and multiple
7508 // operations may be required for some comparisons.
7509 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7510 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007511
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007513 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007514 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007515 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007516 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7517 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007519
Nate Begeman30a0de92008-07-17 16:51:19 +00007520 switch (SetCCOpcode) {
7521 default: break;
7522 case ISD::SETNE: Invert = true;
7523 case ISD::SETEQ: Opc = EQOpc; break;
7524 case ISD::SETLT: Swap = true;
7525 case ISD::SETGT: Opc = GTOpc; break;
7526 case ISD::SETGE: Swap = true;
7527 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7528 case ISD::SETULT: Swap = true;
7529 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7530 case ISD::SETUGE: Swap = true;
7531 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7532 }
7533 if (Swap)
7534 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007535
Nate Begeman30a0de92008-07-17 16:51:19 +00007536 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7537 // bits of the inputs before performing those operations.
7538 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007539 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007540 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7541 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007542 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007543 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7544 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007545 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7546 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007547 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007548
Dale Johannesenace16102009-02-03 19:33:06 +00007549 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007550
7551 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007552 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007553 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007554
Nate Begeman30a0de92008-07-17 16:51:19 +00007555 return Result;
7556}
Evan Cheng0488db92007-09-25 01:57:46 +00007557
Evan Cheng370e5342008-12-03 08:38:43 +00007558// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007559static bool isX86LogicalCmp(SDValue Op) {
7560 unsigned Opc = Op.getNode()->getOpcode();
7561 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7562 return true;
7563 if (Op.getResNo() == 1 &&
7564 (Opc == X86ISD::ADD ||
7565 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007566 Opc == X86ISD::ADC ||
7567 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007568 Opc == X86ISD::SMUL ||
7569 Opc == X86ISD::UMUL ||
7570 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007571 Opc == X86ISD::DEC ||
7572 Opc == X86ISD::OR ||
7573 Opc == X86ISD::XOR ||
7574 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007575 return true;
7576
Chris Lattner9637d5b2010-12-05 07:49:54 +00007577 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7578 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007579
Dan Gohman076aee32009-03-04 19:44:21 +00007580 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007581}
7582
Chris Lattnera2b56002010-12-05 01:23:24 +00007583static bool isZero(SDValue V) {
7584 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7585 return C && C->isNullValue();
7586}
7587
Chris Lattner96908b12010-12-05 02:00:51 +00007588static bool isAllOnes(SDValue V) {
7589 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7590 return C && C->isAllOnesValue();
7591}
7592
Dan Gohmand858e902010-04-17 15:26:15 +00007593SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007594 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007595 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007596 SDValue Op1 = Op.getOperand(1);
7597 SDValue Op2 = Op.getOperand(2);
7598 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007599 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007600
Dan Gohman1a492952009-10-20 16:22:37 +00007601 if (Cond.getOpcode() == ISD::SETCC) {
7602 SDValue NewCond = LowerSETCC(Cond, DAG);
7603 if (NewCond.getNode())
7604 Cond = NewCond;
7605 }
Evan Cheng734503b2006-09-11 02:19:56 +00007606
Chris Lattnera2b56002010-12-05 01:23:24 +00007607 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007608 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007609 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007610 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007611 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007612 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7613 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007614 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007615
Chris Lattnera2b56002010-12-05 01:23:24 +00007616 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007617
7618 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007619 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7620 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007621
7622 SDValue CmpOp0 = Cmp.getOperand(0);
7623 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7624 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007625
Chris Lattner96908b12010-12-05 02:00:51 +00007626 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007627 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7628 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007629
Chris Lattner96908b12010-12-05 02:00:51 +00007630 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7631 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007632
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007633 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007634 if (N2C == 0 || !N2C->isNullValue())
7635 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7636 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007637 }
7638 }
7639
Chris Lattnera2b56002010-12-05 01:23:24 +00007640 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007641 if (Cond.getOpcode() == ISD::AND &&
7642 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7643 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007644 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007645 Cond = Cond.getOperand(0);
7646 }
7647
Evan Cheng3f41d662007-10-08 22:16:29 +00007648 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7649 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007650 if (Cond.getOpcode() == X86ISD::SETCC ||
7651 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007652 CC = Cond.getOperand(0);
7653
Dan Gohman475871a2008-07-27 21:46:04 +00007654 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007655 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007656 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007657
Evan Cheng3f41d662007-10-08 22:16:29 +00007658 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007659 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007660 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007661 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007662
Chris Lattnerd1980a52009-03-12 06:52:53 +00007663 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7664 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007665 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007666 addTest = false;
7667 }
7668 }
7669
7670 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007671 // Look pass the truncate.
7672 if (Cond.getOpcode() == ISD::TRUNCATE)
7673 Cond = Cond.getOperand(0);
7674
7675 // We know the result of AND is compared against zero. Try to match
7676 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007677 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007678 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007679 if (NewSetCC.getNode()) {
7680 CC = NewSetCC.getOperand(0);
7681 Cond = NewSetCC.getOperand(1);
7682 addTest = false;
7683 }
7684 }
7685 }
7686
7687 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007688 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007689 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007690 }
7691
Benjamin Kramere915ff32010-12-22 23:09:28 +00007692 // a < b ? -1 : 0 -> RES = ~setcc_carry
7693 // a < b ? 0 : -1 -> RES = setcc_carry
7694 // a >= b ? -1 : 0 -> RES = setcc_carry
7695 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7696 if (Cond.getOpcode() == X86ISD::CMP) {
7697 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7698
7699 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7700 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7701 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7702 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7703 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7704 return DAG.getNOT(DL, Res, Res.getValueType());
7705 return Res;
7706 }
7707 }
7708
Evan Cheng0488db92007-09-25 01:57:46 +00007709 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7710 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007711 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007712 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007713 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007714}
7715
Evan Cheng370e5342008-12-03 08:38:43 +00007716// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7717// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7718// from the AND / OR.
7719static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7720 Opc = Op.getOpcode();
7721 if (Opc != ISD::OR && Opc != ISD::AND)
7722 return false;
7723 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7724 Op.getOperand(0).hasOneUse() &&
7725 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7726 Op.getOperand(1).hasOneUse());
7727}
7728
Evan Cheng961d6d42009-02-02 08:19:07 +00007729// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7730// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007731static bool isXor1OfSetCC(SDValue Op) {
7732 if (Op.getOpcode() != ISD::XOR)
7733 return false;
7734 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7735 if (N1C && N1C->getAPIntValue() == 1) {
7736 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7737 Op.getOperand(0).hasOneUse();
7738 }
7739 return false;
7740}
7741
Dan Gohmand858e902010-04-17 15:26:15 +00007742SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007743 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007744 SDValue Chain = Op.getOperand(0);
7745 SDValue Cond = Op.getOperand(1);
7746 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007747 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007748 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007749
Dan Gohman1a492952009-10-20 16:22:37 +00007750 if (Cond.getOpcode() == ISD::SETCC) {
7751 SDValue NewCond = LowerSETCC(Cond, DAG);
7752 if (NewCond.getNode())
7753 Cond = NewCond;
7754 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007755#if 0
7756 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007757 else if (Cond.getOpcode() == X86ISD::ADD ||
7758 Cond.getOpcode() == X86ISD::SUB ||
7759 Cond.getOpcode() == X86ISD::SMUL ||
7760 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007761 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007762#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007763
Evan Chengad9c0a32009-12-15 00:53:42 +00007764 // Look pass (and (setcc_carry (cmp ...)), 1).
7765 if (Cond.getOpcode() == ISD::AND &&
7766 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7767 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007768 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007769 Cond = Cond.getOperand(0);
7770 }
7771
Evan Cheng3f41d662007-10-08 22:16:29 +00007772 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7773 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007774 if (Cond.getOpcode() == X86ISD::SETCC ||
7775 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007776 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777
Dan Gohman475871a2008-07-27 21:46:04 +00007778 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007779 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007780 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007781 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007782 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007783 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007784 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007785 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007786 default: break;
7787 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007788 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007789 // These can only come from an arithmetic instruction with overflow,
7790 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007791 Cond = Cond.getNode()->getOperand(1);
7792 addTest = false;
7793 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007794 }
Evan Cheng0488db92007-09-25 01:57:46 +00007795 }
Evan Cheng370e5342008-12-03 08:38:43 +00007796 } else {
7797 unsigned CondOpc;
7798 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7799 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007800 if (CondOpc == ISD::OR) {
7801 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7802 // two branches instead of an explicit OR instruction with a
7803 // separate test.
7804 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007805 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007806 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007807 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007808 Chain, Dest, CC, Cmp);
7809 CC = Cond.getOperand(1).getOperand(0);
7810 Cond = Cmp;
7811 addTest = false;
7812 }
7813 } else { // ISD::AND
7814 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7815 // two branches instead of an explicit AND instruction with a
7816 // separate test. However, we only do this if this block doesn't
7817 // have a fall-through edge, because this requires an explicit
7818 // jmp when the condition is false.
7819 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007820 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007821 Op.getNode()->hasOneUse()) {
7822 X86::CondCode CCode =
7823 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7824 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007825 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007826 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007827 // Look for an unconditional branch following this conditional branch.
7828 // We need this because we need to reverse the successors in order
7829 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007830 if (User->getOpcode() == ISD::BR) {
7831 SDValue FalseBB = User->getOperand(1);
7832 SDNode *NewBR =
7833 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007834 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007835 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007836 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007837
Dale Johannesene4d209d2009-02-03 20:21:25 +00007838 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007839 Chain, Dest, CC, Cmp);
7840 X86::CondCode CCode =
7841 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7842 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007843 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007844 Cond = Cmp;
7845 addTest = false;
7846 }
7847 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007848 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007849 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7850 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7851 // It should be transformed during dag combiner except when the condition
7852 // is set by a arithmetics with overflow node.
7853 X86::CondCode CCode =
7854 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7855 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007856 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007857 Cond = Cond.getOperand(0).getOperand(1);
7858 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007859 }
Evan Cheng0488db92007-09-25 01:57:46 +00007860 }
7861
7862 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007863 // Look pass the truncate.
7864 if (Cond.getOpcode() == ISD::TRUNCATE)
7865 Cond = Cond.getOperand(0);
7866
7867 // We know the result of AND is compared against zero. Try to match
7868 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007869 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007870 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7871 if (NewSetCC.getNode()) {
7872 CC = NewSetCC.getOperand(0);
7873 Cond = NewSetCC.getOperand(1);
7874 addTest = false;
7875 }
7876 }
7877 }
7878
7879 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007880 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007881 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007882 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007883 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007884 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007885}
7886
Anton Korobeynikove060b532007-04-17 19:34:00 +00007887
7888// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7889// Calls to _alloca is needed to probe the stack when allocating more than 4k
7890// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7891// that the guard pages used by the OS virtual memory manager are allocated in
7892// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007893SDValue
7894X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007895 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007896 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007897 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007898 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007899
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007900 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007901 SDValue Chain = Op.getOperand(0);
7902 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007903 // FIXME: Ensure alignment here
7904
Dan Gohman475871a2008-07-27 21:46:04 +00007905 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007906
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007908
Dale Johannesendd64c412009-02-04 00:33:20 +00007909 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007910 Flag = Chain.getValue(1);
7911
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007912 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007913
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007914 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007915 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007916
Dale Johannesendd64c412009-02-04 00:33:20 +00007917 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007918
Dan Gohman475871a2008-07-27 21:46:04 +00007919 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007920 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007921}
7922
Dan Gohmand858e902010-04-17 15:26:15 +00007923SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007924 MachineFunction &MF = DAG.getMachineFunction();
7925 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7926
Dan Gohman69de1932008-02-06 22:27:42 +00007927 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007928 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007929
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007930 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007931 // vastart just stores the address of the VarArgsFrameIndex slot into the
7932 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007933 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7934 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007935 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7936 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007937 }
7938
7939 // __va_list_tag:
7940 // gp_offset (0 - 6 * 8)
7941 // fp_offset (48 - 48 + 8 * 16)
7942 // overflow_arg_area (point to parameters coming in memory).
7943 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007944 SmallVector<SDValue, 8> MemOps;
7945 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007946 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007947 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007948 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7949 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007950 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007951 MemOps.push_back(Store);
7952
7953 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007954 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007955 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007956 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007957 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7958 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007959 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007960 MemOps.push_back(Store);
7961
7962 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007963 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007964 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007965 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7966 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007967 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7968 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007969 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007970 MemOps.push_back(Store);
7971
7972 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007973 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007974 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007975 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7976 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007977 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7978 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007979 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007980 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007982}
7983
Dan Gohmand858e902010-04-17 15:26:15 +00007984SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007985 assert(Subtarget->is64Bit() &&
7986 "LowerVAARG only handles 64-bit va_arg!");
7987 assert((Subtarget->isTargetLinux() ||
7988 Subtarget->isTargetDarwin()) &&
7989 "Unhandled target in LowerVAARG");
7990 assert(Op.getNode()->getNumOperands() == 4);
7991 SDValue Chain = Op.getOperand(0);
7992 SDValue SrcPtr = Op.getOperand(1);
7993 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7994 unsigned Align = Op.getConstantOperandVal(3);
7995 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007996
Dan Gohman320afb82010-10-12 18:00:49 +00007997 EVT ArgVT = Op.getNode()->getValueType(0);
7998 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7999 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8000 uint8_t ArgMode;
8001
8002 // Decide which area this value should be read from.
8003 // TODO: Implement the AMD64 ABI in its entirety. This simple
8004 // selection mechanism works only for the basic types.
8005 if (ArgVT == MVT::f80) {
8006 llvm_unreachable("va_arg for f80 not yet implemented");
8007 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8008 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8009 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8010 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8011 } else {
8012 llvm_unreachable("Unhandled argument type in LowerVAARG");
8013 }
8014
8015 if (ArgMode == 2) {
8016 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008017 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008018 !(DAG.getMachineFunction()
8019 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008020 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008021 }
8022
8023 // Insert VAARG_64 node into the DAG
8024 // VAARG_64 returns two values: Variable Argument Address, Chain
8025 SmallVector<SDValue, 11> InstOps;
8026 InstOps.push_back(Chain);
8027 InstOps.push_back(SrcPtr);
8028 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8029 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8030 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8031 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8032 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8033 VTs, &InstOps[0], InstOps.size(),
8034 MVT::i64,
8035 MachinePointerInfo(SV),
8036 /*Align=*/0,
8037 /*Volatile=*/false,
8038 /*ReadMem=*/true,
8039 /*WriteMem=*/true);
8040 Chain = VAARG.getValue(1);
8041
8042 // Load the next argument and return it
8043 return DAG.getLoad(ArgVT, dl,
8044 Chain,
8045 VAARG,
8046 MachinePointerInfo(),
8047 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008048}
8049
Dan Gohmand858e902010-04-17 15:26:15 +00008050SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008051 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008052 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008053 SDValue Chain = Op.getOperand(0);
8054 SDValue DstPtr = Op.getOperand(1);
8055 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008056 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8057 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008058 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008059
Chris Lattnere72f2022010-09-21 05:40:29 +00008060 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008061 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008062 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008063 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008064}
8065
Dan Gohman475871a2008-07-27 21:46:04 +00008066SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008067X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008068 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008069 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008070 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008071 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008072 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008073 case Intrinsic::x86_sse_comieq_ss:
8074 case Intrinsic::x86_sse_comilt_ss:
8075 case Intrinsic::x86_sse_comile_ss:
8076 case Intrinsic::x86_sse_comigt_ss:
8077 case Intrinsic::x86_sse_comige_ss:
8078 case Intrinsic::x86_sse_comineq_ss:
8079 case Intrinsic::x86_sse_ucomieq_ss:
8080 case Intrinsic::x86_sse_ucomilt_ss:
8081 case Intrinsic::x86_sse_ucomile_ss:
8082 case Intrinsic::x86_sse_ucomigt_ss:
8083 case Intrinsic::x86_sse_ucomige_ss:
8084 case Intrinsic::x86_sse_ucomineq_ss:
8085 case Intrinsic::x86_sse2_comieq_sd:
8086 case Intrinsic::x86_sse2_comilt_sd:
8087 case Intrinsic::x86_sse2_comile_sd:
8088 case Intrinsic::x86_sse2_comigt_sd:
8089 case Intrinsic::x86_sse2_comige_sd:
8090 case Intrinsic::x86_sse2_comineq_sd:
8091 case Intrinsic::x86_sse2_ucomieq_sd:
8092 case Intrinsic::x86_sse2_ucomilt_sd:
8093 case Intrinsic::x86_sse2_ucomile_sd:
8094 case Intrinsic::x86_sse2_ucomigt_sd:
8095 case Intrinsic::x86_sse2_ucomige_sd:
8096 case Intrinsic::x86_sse2_ucomineq_sd: {
8097 unsigned Opc = 0;
8098 ISD::CondCode CC = ISD::SETCC_INVALID;
8099 switch (IntNo) {
8100 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008101 case Intrinsic::x86_sse_comieq_ss:
8102 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008103 Opc = X86ISD::COMI;
8104 CC = ISD::SETEQ;
8105 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008106 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008107 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008108 Opc = X86ISD::COMI;
8109 CC = ISD::SETLT;
8110 break;
8111 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008112 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008113 Opc = X86ISD::COMI;
8114 CC = ISD::SETLE;
8115 break;
8116 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008117 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008118 Opc = X86ISD::COMI;
8119 CC = ISD::SETGT;
8120 break;
8121 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008122 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008123 Opc = X86ISD::COMI;
8124 CC = ISD::SETGE;
8125 break;
8126 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008127 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008128 Opc = X86ISD::COMI;
8129 CC = ISD::SETNE;
8130 break;
8131 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008132 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008133 Opc = X86ISD::UCOMI;
8134 CC = ISD::SETEQ;
8135 break;
8136 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008137 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008138 Opc = X86ISD::UCOMI;
8139 CC = ISD::SETLT;
8140 break;
8141 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008142 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008143 Opc = X86ISD::UCOMI;
8144 CC = ISD::SETLE;
8145 break;
8146 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008147 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008148 Opc = X86ISD::UCOMI;
8149 CC = ISD::SETGT;
8150 break;
8151 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008152 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008153 Opc = X86ISD::UCOMI;
8154 CC = ISD::SETGE;
8155 break;
8156 case Intrinsic::x86_sse_ucomineq_ss:
8157 case Intrinsic::x86_sse2_ucomineq_sd:
8158 Opc = X86ISD::UCOMI;
8159 CC = ISD::SETNE;
8160 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008161 }
Evan Cheng734503b2006-09-11 02:19:56 +00008162
Dan Gohman475871a2008-07-27 21:46:04 +00008163 SDValue LHS = Op.getOperand(1);
8164 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008165 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008166 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008167 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8168 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8169 DAG.getConstant(X86CC, MVT::i8), Cond);
8170 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008171 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008172 // ptest and testp intrinsics. The intrinsic these come from are designed to
8173 // return an integer value, not just an instruction so lower it to the ptest
8174 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008175 case Intrinsic::x86_sse41_ptestz:
8176 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008177 case Intrinsic::x86_sse41_ptestnzc:
8178 case Intrinsic::x86_avx_ptestz_256:
8179 case Intrinsic::x86_avx_ptestc_256:
8180 case Intrinsic::x86_avx_ptestnzc_256:
8181 case Intrinsic::x86_avx_vtestz_ps:
8182 case Intrinsic::x86_avx_vtestc_ps:
8183 case Intrinsic::x86_avx_vtestnzc_ps:
8184 case Intrinsic::x86_avx_vtestz_pd:
8185 case Intrinsic::x86_avx_vtestc_pd:
8186 case Intrinsic::x86_avx_vtestnzc_pd:
8187 case Intrinsic::x86_avx_vtestz_ps_256:
8188 case Intrinsic::x86_avx_vtestc_ps_256:
8189 case Intrinsic::x86_avx_vtestnzc_ps_256:
8190 case Intrinsic::x86_avx_vtestz_pd_256:
8191 case Intrinsic::x86_avx_vtestc_pd_256:
8192 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8193 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008194 unsigned X86CC = 0;
8195 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008196 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008197 case Intrinsic::x86_avx_vtestz_ps:
8198 case Intrinsic::x86_avx_vtestz_pd:
8199 case Intrinsic::x86_avx_vtestz_ps_256:
8200 case Intrinsic::x86_avx_vtestz_pd_256:
8201 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008202 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008203 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008204 // ZF = 1
8205 X86CC = X86::COND_E;
8206 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008207 case Intrinsic::x86_avx_vtestc_ps:
8208 case Intrinsic::x86_avx_vtestc_pd:
8209 case Intrinsic::x86_avx_vtestc_ps_256:
8210 case Intrinsic::x86_avx_vtestc_pd_256:
8211 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008212 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008213 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008214 // CF = 1
8215 X86CC = X86::COND_B;
8216 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008217 case Intrinsic::x86_avx_vtestnzc_ps:
8218 case Intrinsic::x86_avx_vtestnzc_pd:
8219 case Intrinsic::x86_avx_vtestnzc_ps_256:
8220 case Intrinsic::x86_avx_vtestnzc_pd_256:
8221 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008222 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008223 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008224 // ZF and CF = 0
8225 X86CC = X86::COND_A;
8226 break;
8227 }
Eric Christopherfd179292009-08-27 18:07:15 +00008228
Eric Christopher71c67532009-07-29 00:28:05 +00008229 SDValue LHS = Op.getOperand(1);
8230 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008231 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8232 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008233 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8234 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8235 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008236 }
Evan Cheng5759f972008-05-04 09:15:50 +00008237
8238 // Fix vector shift instructions where the last operand is a non-immediate
8239 // i32 value.
8240 case Intrinsic::x86_sse2_pslli_w:
8241 case Intrinsic::x86_sse2_pslli_d:
8242 case Intrinsic::x86_sse2_pslli_q:
8243 case Intrinsic::x86_sse2_psrli_w:
8244 case Intrinsic::x86_sse2_psrli_d:
8245 case Intrinsic::x86_sse2_psrli_q:
8246 case Intrinsic::x86_sse2_psrai_w:
8247 case Intrinsic::x86_sse2_psrai_d:
8248 case Intrinsic::x86_mmx_pslli_w:
8249 case Intrinsic::x86_mmx_pslli_d:
8250 case Intrinsic::x86_mmx_pslli_q:
8251 case Intrinsic::x86_mmx_psrli_w:
8252 case Intrinsic::x86_mmx_psrli_d:
8253 case Intrinsic::x86_mmx_psrli_q:
8254 case Intrinsic::x86_mmx_psrai_w:
8255 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008256 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008257 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008258 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008259
8260 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008261 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008262 switch (IntNo) {
8263 case Intrinsic::x86_sse2_pslli_w:
8264 NewIntNo = Intrinsic::x86_sse2_psll_w;
8265 break;
8266 case Intrinsic::x86_sse2_pslli_d:
8267 NewIntNo = Intrinsic::x86_sse2_psll_d;
8268 break;
8269 case Intrinsic::x86_sse2_pslli_q:
8270 NewIntNo = Intrinsic::x86_sse2_psll_q;
8271 break;
8272 case Intrinsic::x86_sse2_psrli_w:
8273 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8274 break;
8275 case Intrinsic::x86_sse2_psrli_d:
8276 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8277 break;
8278 case Intrinsic::x86_sse2_psrli_q:
8279 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8280 break;
8281 case Intrinsic::x86_sse2_psrai_w:
8282 NewIntNo = Intrinsic::x86_sse2_psra_w;
8283 break;
8284 case Intrinsic::x86_sse2_psrai_d:
8285 NewIntNo = Intrinsic::x86_sse2_psra_d;
8286 break;
8287 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008288 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008289 switch (IntNo) {
8290 case Intrinsic::x86_mmx_pslli_w:
8291 NewIntNo = Intrinsic::x86_mmx_psll_w;
8292 break;
8293 case Intrinsic::x86_mmx_pslli_d:
8294 NewIntNo = Intrinsic::x86_mmx_psll_d;
8295 break;
8296 case Intrinsic::x86_mmx_pslli_q:
8297 NewIntNo = Intrinsic::x86_mmx_psll_q;
8298 break;
8299 case Intrinsic::x86_mmx_psrli_w:
8300 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8301 break;
8302 case Intrinsic::x86_mmx_psrli_d:
8303 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8304 break;
8305 case Intrinsic::x86_mmx_psrli_q:
8306 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8307 break;
8308 case Intrinsic::x86_mmx_psrai_w:
8309 NewIntNo = Intrinsic::x86_mmx_psra_w;
8310 break;
8311 case Intrinsic::x86_mmx_psrai_d:
8312 NewIntNo = Intrinsic::x86_mmx_psra_d;
8313 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008314 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008315 }
8316 break;
8317 }
8318 }
Mon P Wangefa42202009-09-03 19:56:25 +00008319
8320 // The vector shift intrinsics with scalars uses 32b shift amounts but
8321 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8322 // to be zero.
8323 SDValue ShOps[4];
8324 ShOps[0] = ShAmt;
8325 ShOps[1] = DAG.getConstant(0, MVT::i32);
8326 if (ShAmtVT == MVT::v4i32) {
8327 ShOps[2] = DAG.getUNDEF(MVT::i32);
8328 ShOps[3] = DAG.getUNDEF(MVT::i32);
8329 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8330 } else {
8331 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008332// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008333 }
8334
Owen Andersone50ed302009-08-10 22:56:29 +00008335 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008336 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008337 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008338 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008339 Op.getOperand(1), ShAmt);
8340 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008341 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008342}
Evan Cheng72261582005-12-20 06:22:03 +00008343
Dan Gohmand858e902010-04-17 15:26:15 +00008344SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8345 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008346 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8347 MFI->setReturnAddressIsTaken(true);
8348
Bill Wendling64e87322009-01-16 19:25:27 +00008349 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008350 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008351
8352 if (Depth > 0) {
8353 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8354 SDValue Offset =
8355 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008356 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008357 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008358 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008359 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008360 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008361 }
8362
8363 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008364 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008365 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008366 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008367}
8368
Dan Gohmand858e902010-04-17 15:26:15 +00008369SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008370 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8371 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008372
Owen Andersone50ed302009-08-10 22:56:29 +00008373 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008374 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008375 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8376 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008377 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008378 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008379 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8380 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008381 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008382 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008383}
8384
Dan Gohman475871a2008-07-27 21:46:04 +00008385SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008386 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008387 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008388}
8389
Dan Gohmand858e902010-04-17 15:26:15 +00008390SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008391 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008392 SDValue Chain = Op.getOperand(0);
8393 SDValue Offset = Op.getOperand(1);
8394 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008395 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008396
Dan Gohmand8816272010-08-11 18:14:00 +00008397 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8398 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8399 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008400 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008401
Dan Gohmand8816272010-08-11 18:14:00 +00008402 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8403 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008404 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008405 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8406 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008407 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008408 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008409
Dale Johannesene4d209d2009-02-03 20:21:25 +00008410 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008411 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008412 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008413}
8414
Dan Gohman475871a2008-07-27 21:46:04 +00008415SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008416 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008417 SDValue Root = Op.getOperand(0);
8418 SDValue Trmp = Op.getOperand(1); // trampoline
8419 SDValue FPtr = Op.getOperand(2); // nested function
8420 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008421 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008422
Dan Gohman69de1932008-02-06 22:27:42 +00008423 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008424
8425 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008426 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008427
8428 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008429 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8430 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008431
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008432 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8433 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008434
8435 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8436
8437 // Load the pointer to the nested function into R11.
8438 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008439 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008440 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008441 Addr, MachinePointerInfo(TrmpAddr),
8442 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008443
Owen Anderson825b72b2009-08-11 20:47:22 +00008444 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8445 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008446 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8447 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008448 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008449
8450 // Load the 'nest' parameter value into R10.
8451 // R10 is specified in X86CallingConv.td
8452 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008453 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8454 DAG.getConstant(10, MVT::i64));
8455 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008456 Addr, MachinePointerInfo(TrmpAddr, 10),
8457 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008458
Owen Anderson825b72b2009-08-11 20:47:22 +00008459 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8460 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008461 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8462 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008463 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008464
8465 // Jump to the nested function.
8466 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008467 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8468 DAG.getConstant(20, MVT::i64));
8469 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008470 Addr, MachinePointerInfo(TrmpAddr, 20),
8471 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008472
8473 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008474 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8475 DAG.getConstant(22, MVT::i64));
8476 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008477 MachinePointerInfo(TrmpAddr, 22),
8478 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008479
Dan Gohman475871a2008-07-27 21:46:04 +00008480 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008481 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008482 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008483 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008484 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008485 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008486 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008487 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008488
8489 switch (CC) {
8490 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008491 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008492 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008493 case CallingConv::X86_StdCall: {
8494 // Pass 'nest' parameter in ECX.
8495 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008496 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008497
8498 // Check that ECX wasn't needed by an 'inreg' parameter.
8499 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008500 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008501
Chris Lattner58d74912008-03-12 17:45:29 +00008502 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008503 unsigned InRegCount = 0;
8504 unsigned Idx = 1;
8505
8506 for (FunctionType::param_iterator I = FTy->param_begin(),
8507 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008508 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008509 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008510 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008511
8512 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008513 report_fatal_error("Nest register in use - reduce number of inreg"
8514 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008515 }
8516 }
8517 break;
8518 }
8519 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008520 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008521 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008522 // Pass 'nest' parameter in EAX.
8523 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008524 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008525 break;
8526 }
8527
Dan Gohman475871a2008-07-27 21:46:04 +00008528 SDValue OutChains[4];
8529 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008530
Owen Anderson825b72b2009-08-11 20:47:22 +00008531 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8532 DAG.getConstant(10, MVT::i32));
8533 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008534
Chris Lattnera62fe662010-02-05 19:20:30 +00008535 // This is storing the opcode for MOV32ri.
8536 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008537 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008538 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008539 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008540 Trmp, MachinePointerInfo(TrmpAddr),
8541 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008542
Owen Anderson825b72b2009-08-11 20:47:22 +00008543 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8544 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008545 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8546 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008547 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008548
Chris Lattnera62fe662010-02-05 19:20:30 +00008549 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8551 DAG.getConstant(5, MVT::i32));
8552 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008553 MachinePointerInfo(TrmpAddr, 5),
8554 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008555
Owen Anderson825b72b2009-08-11 20:47:22 +00008556 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8557 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008558 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8559 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008560 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008561
Dan Gohman475871a2008-07-27 21:46:04 +00008562 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008563 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008564 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008565 }
8566}
8567
Dan Gohmand858e902010-04-17 15:26:15 +00008568SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8569 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008570 /*
8571 The rounding mode is in bits 11:10 of FPSR, and has the following
8572 settings:
8573 00 Round to nearest
8574 01 Round to -inf
8575 10 Round to +inf
8576 11 Round to 0
8577
8578 FLT_ROUNDS, on the other hand, expects the following:
8579 -1 Undefined
8580 0 Round to 0
8581 1 Round to nearest
8582 2 Round to +inf
8583 3 Round to -inf
8584
8585 To perform the conversion, we do:
8586 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8587 */
8588
8589 MachineFunction &MF = DAG.getMachineFunction();
8590 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008591 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008592 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008593 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008594 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008595
8596 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008597 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008598 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008599
Michael J. Spencerec38de22010-10-10 22:04:20 +00008600
Chris Lattner2156b792010-09-22 01:11:26 +00008601 MachineMemOperand *MMO =
8602 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8603 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008604
Chris Lattner2156b792010-09-22 01:11:26 +00008605 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8606 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8607 DAG.getVTList(MVT::Other),
8608 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008609
8610 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008611 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008612 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008613
8614 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008615 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008616 DAG.getNode(ISD::SRL, DL, MVT::i16,
8617 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 CWD, DAG.getConstant(0x800, MVT::i16)),
8619 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008620 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008621 DAG.getNode(ISD::SRL, DL, MVT::i16,
8622 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 CWD, DAG.getConstant(0x400, MVT::i16)),
8624 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008625
Dan Gohman475871a2008-07-27 21:46:04 +00008626 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008627 DAG.getNode(ISD::AND, DL, MVT::i16,
8628 DAG.getNode(ISD::ADD, DL, MVT::i16,
8629 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008630 DAG.getConstant(1, MVT::i16)),
8631 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008632
8633
Duncan Sands83ec4b62008-06-06 12:08:01 +00008634 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008635 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008636}
8637
Dan Gohmand858e902010-04-17 15:26:15 +00008638SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008639 EVT VT = Op.getValueType();
8640 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008641 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008642 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008643
8644 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008645 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008646 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008647 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008648 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008649 }
Evan Cheng18efe262007-12-14 02:13:44 +00008650
Evan Cheng152804e2007-12-14 08:30:15 +00008651 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008652 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008653 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008654
8655 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008656 SDValue Ops[] = {
8657 Op,
8658 DAG.getConstant(NumBits+NumBits-1, OpVT),
8659 DAG.getConstant(X86::COND_E, MVT::i8),
8660 Op.getValue(1)
8661 };
8662 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008663
8664 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008665 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008666
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 if (VT == MVT::i8)
8668 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008669 return Op;
8670}
8671
Dan Gohmand858e902010-04-17 15:26:15 +00008672SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008673 EVT VT = Op.getValueType();
8674 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008675 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008676 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008677
8678 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008679 if (VT == MVT::i8) {
8680 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008681 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008682 }
Evan Cheng152804e2007-12-14 08:30:15 +00008683
8684 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008685 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008686 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008687
8688 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008689 SDValue Ops[] = {
8690 Op,
8691 DAG.getConstant(NumBits, OpVT),
8692 DAG.getConstant(X86::COND_E, MVT::i8),
8693 Op.getValue(1)
8694 };
8695 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008696
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 if (VT == MVT::i8)
8698 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008699 return Op;
8700}
8701
Dan Gohmand858e902010-04-17 15:26:15 +00008702SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008703 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008705 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008706
Mon P Wangaf9b9522008-12-18 21:42:19 +00008707 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8708 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8709 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8710 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8711 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8712 //
8713 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8714 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8715 // return AloBlo + AloBhi + AhiBlo;
8716
8717 SDValue A = Op.getOperand(0);
8718 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008719
Dale Johannesene4d209d2009-02-03 20:21:25 +00008720 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008721 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8722 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008723 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008724 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8725 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008726 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008727 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008728 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008729 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008730 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008731 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008732 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008733 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008734 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008735 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008736 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8737 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008738 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008739 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8740 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008741 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8742 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008743 return Res;
8744}
8745
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008746SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8747 EVT VT = Op.getValueType();
8748 DebugLoc dl = Op.getDebugLoc();
8749 SDValue R = Op.getOperand(0);
8750
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008751 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008752
Nate Begeman51409212010-07-28 00:21:48 +00008753 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8754
8755 if (VT == MVT::v4i32) {
8756 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8757 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8758 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8759
8760 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008761
Nate Begeman51409212010-07-28 00:21:48 +00008762 std::vector<Constant*> CV(4, CI);
8763 Constant *C = ConstantVector::get(CV);
8764 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8765 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008766 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008767 false, false, 16);
8768
8769 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008770 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008771 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8772 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8773 }
8774 if (VT == MVT::v16i8) {
8775 // a = a << 5;
8776 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8777 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8778 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8779
8780 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8781 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8782
8783 std::vector<Constant*> CVM1(16, CM1);
8784 std::vector<Constant*> CVM2(16, CM2);
8785 Constant *C = ConstantVector::get(CVM1);
8786 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8787 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008788 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008789 false, false, 16);
8790
8791 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8792 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8793 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8794 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8795 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008796 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008797 // a += a
8798 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008799
Nate Begeman51409212010-07-28 00:21:48 +00008800 C = ConstantVector::get(CVM2);
8801 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8802 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008803 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008804 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008805
Nate Begeman51409212010-07-28 00:21:48 +00008806 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8807 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8808 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8809 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8810 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008811 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008812 // a += a
8813 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008814
Nate Begeman51409212010-07-28 00:21:48 +00008815 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008816 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008817 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8818 return R;
8819 }
8820 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008821}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008822
Dan Gohmand858e902010-04-17 15:26:15 +00008823SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008824 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8825 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008826 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8827 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008828 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008829 SDValue LHS = N->getOperand(0);
8830 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008831 unsigned BaseOp = 0;
8832 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008833 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008834 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008835 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008836 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008837 // A subtract of one will be selected as a INC. Note that INC doesn't
8838 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8840 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008841 BaseOp = X86ISD::INC;
8842 Cond = X86::COND_O;
8843 break;
8844 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008845 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008846 Cond = X86::COND_O;
8847 break;
8848 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008849 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008850 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008851 break;
8852 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008853 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8854 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +00008855 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
8856 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +00008857 BaseOp = X86ISD::DEC;
8858 Cond = X86::COND_O;
8859 break;
8860 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008861 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008862 Cond = X86::COND_O;
8863 break;
8864 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008865 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008866 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008867 break;
8868 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008869 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008870 Cond = X86::COND_O;
8871 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008872 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8873 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8874 MVT::i32);
8875 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008876
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008877 SDValue SetCC =
8878 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8879 DAG.getConstant(X86::COND_O, MVT::i32),
8880 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008881
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008882 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8883 return Sum;
8884 }
Bill Wendling74c37652008-12-09 22:08:41 +00008885 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008886
Bill Wendling61edeb52008-12-02 01:06:39 +00008887 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008888 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008889 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008890
Bill Wendling61edeb52008-12-02 01:06:39 +00008891 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008892 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8893 DAG.getConstant(Cond, MVT::i32),
8894 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008895
Bill Wendling61edeb52008-12-02 01:06:39 +00008896 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8897 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008898}
8899
Eric Christopher9a9d2752010-07-22 02:48:34 +00008900SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8901 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008902
Eric Christopherb6729dc2010-08-04 23:03:04 +00008903 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008904 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008905 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008906 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008907 SDValue Ops[] = {
8908 DAG.getRegister(X86::ESP, MVT::i32), // Base
8909 DAG.getTargetConstant(1, MVT::i8), // Scale
8910 DAG.getRegister(0, MVT::i32), // Index
8911 DAG.getTargetConstant(0, MVT::i32), // Disp
8912 DAG.getRegister(0, MVT::i32), // Segment.
8913 Zero,
8914 Chain
8915 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008916 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008917 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8918 array_lengthof(Ops));
8919 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008920 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008921
Eric Christopher9a9d2752010-07-22 02:48:34 +00008922 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008923 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008924 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008925
Chris Lattner132929a2010-08-14 17:26:09 +00008926 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8927 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8928 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8929 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008930
Chris Lattner132929a2010-08-14 17:26:09 +00008931 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8932 if (!Op1 && !Op2 && !Op3 && Op4)
8933 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008934
Chris Lattner132929a2010-08-14 17:26:09 +00008935 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8936 if (Op1 && !Op2 && !Op3 && !Op4)
8937 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008938
8939 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008940 // (MFENCE)>;
8941 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008942}
8943
Dan Gohmand858e902010-04-17 15:26:15 +00008944SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008945 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008946 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008947 unsigned Reg = 0;
8948 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008949 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008950 default:
8951 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008952 case MVT::i8: Reg = X86::AL; size = 1; break;
8953 case MVT::i16: Reg = X86::AX; size = 2; break;
8954 case MVT::i32: Reg = X86::EAX; size = 4; break;
8955 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008956 assert(Subtarget->is64Bit() && "Node not type legal!");
8957 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008958 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008959 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008960 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008961 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008962 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008963 Op.getOperand(1),
8964 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008965 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008966 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008967 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008968 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8969 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8970 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008971 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008972 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008973 return cpOut;
8974}
8975
Duncan Sands1607f052008-12-01 11:39:25 +00008976SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008977 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008978 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008979 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008980 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008981 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008982 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008983 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8984 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008985 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008986 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8987 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008988 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008989 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008990 rdx.getValue(1)
8991 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008992 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008993}
8994
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008995SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008996 SelectionDAG &DAG) const {
8997 EVT SrcVT = Op.getOperand(0).getValueType();
8998 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008999 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9000 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009001 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009002 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009003 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009004 // i64 <=> MMX conversions are Legal.
9005 if (SrcVT==MVT::i64 && DstVT.isVector())
9006 return Op;
9007 if (DstVT==MVT::i64 && SrcVT.isVector())
9008 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009009 // MMX <=> MMX conversions are Legal.
9010 if (SrcVT.isVector() && DstVT.isVector())
9011 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009012 // All other conversions need to be expanded.
9013 return SDValue();
9014}
Chris Lattner5b856542010-12-20 00:59:46 +00009015
Dan Gohmand858e902010-04-17 15:26:15 +00009016SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009017 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009018 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009019 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009020 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009021 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009022 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009023 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009024 Node->getOperand(0),
9025 Node->getOperand(1), negOp,
9026 cast<AtomicSDNode>(Node)->getSrcValue(),
9027 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009028}
9029
Chris Lattner5b856542010-12-20 00:59:46 +00009030static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9031 EVT VT = Op.getNode()->getValueType(0);
9032
9033 // Let legalize expand this if it isn't a legal type yet.
9034 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9035 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009036
Chris Lattner5b856542010-12-20 00:59:46 +00009037 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009038
Chris Lattner5b856542010-12-20 00:59:46 +00009039 unsigned Opc;
9040 bool ExtraOp = false;
9041 switch (Op.getOpcode()) {
9042 default: assert(0 && "Invalid code");
9043 case ISD::ADDC: Opc = X86ISD::ADD; break;
9044 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9045 case ISD::SUBC: Opc = X86ISD::SUB; break;
9046 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9047 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009048
Chris Lattner5b856542010-12-20 00:59:46 +00009049 if (!ExtraOp)
9050 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9051 Op.getOperand(1));
9052 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9053 Op.getOperand(1), Op.getOperand(2));
9054}
9055
Evan Cheng0db9fe62006-04-25 20:13:52 +00009056/// LowerOperation - Provide custom lowering hooks for some operations.
9057///
Dan Gohmand858e902010-04-17 15:26:15 +00009058SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009059 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009060 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009061 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009062 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9063 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009064 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009065 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009066 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9067 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9068 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009069 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009070 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009071 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9072 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9073 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009074 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009075 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009076 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009077 case ISD::SHL_PARTS:
9078 case ISD::SRA_PARTS:
9079 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9080 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009081 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009082 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009083 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009084 case ISD::FABS: return LowerFABS(Op, DAG);
9085 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009086 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009087 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009088 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009089 case ISD::SELECT: return LowerSELECT(Op, DAG);
9090 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009091 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009092 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009093 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009094 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009095 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009096 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9097 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009098 case ISD::FRAME_TO_ARGS_OFFSET:
9099 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009100 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009101 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009102 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009103 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009104 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9105 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009106 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009107 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009108 case ISD::SADDO:
9109 case ISD::UADDO:
9110 case ISD::SSUBO:
9111 case ISD::USUBO:
9112 case ISD::SMULO:
9113 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009114 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009115 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009116 case ISD::ADDC:
9117 case ISD::ADDE:
9118 case ISD::SUBC:
9119 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009120 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009121}
9122
Duncan Sands1607f052008-12-01 11:39:25 +00009123void X86TargetLowering::
9124ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009125 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009126 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009127 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009128 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009129
9130 SDValue Chain = Node->getOperand(0);
9131 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009133 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009134 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009135 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009136 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009137 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009138 SDValue Result =
9139 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9140 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009141 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009142 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009143 Results.push_back(Result.getValue(2));
9144}
9145
Duncan Sands126d9072008-07-04 11:47:58 +00009146/// ReplaceNodeResults - Replace a node with an illegal result type
9147/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009148void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9149 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009150 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009151 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009152 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009153 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009154 assert(false && "Do not know how to custom type legalize this operation!");
9155 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009156 case ISD::ADDC:
9157 case ISD::ADDE:
9158 case ISD::SUBC:
9159 case ISD::SUBE:
9160 // We don't want to expand or promote these.
9161 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009162 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009163 std::pair<SDValue,SDValue> Vals =
9164 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009165 SDValue FIST = Vals.first, StackSlot = Vals.second;
9166 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009167 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009168 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009169 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9170 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009171 }
9172 return;
9173 }
9174 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009175 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009176 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009177 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009178 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009179 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009180 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009181 eax.getValue(2));
9182 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9183 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009184 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009185 Results.push_back(edx.getValue(1));
9186 return;
9187 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009188 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009189 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009191 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009192 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9193 DAG.getConstant(0, MVT::i32));
9194 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9195 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009196 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9197 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009198 cpInL.getValue(1));
9199 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009200 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9201 DAG.getConstant(0, MVT::i32));
9202 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9203 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009204 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009205 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009206 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009207 swapInL.getValue(1));
9208 SDValue Ops[] = { swapInH.getValue(0),
9209 N->getOperand(1),
9210 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009211 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009212 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9213 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9214 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009215 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009216 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009217 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009218 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009219 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009220 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009221 Results.push_back(cpOutH.getValue(1));
9222 return;
9223 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009224 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009225 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9226 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009227 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009228 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9229 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009230 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009231 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9232 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009233 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009234 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9235 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009236 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009237 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9238 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009239 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009240 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9241 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009242 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009243 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9244 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009245 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009246}
9247
Evan Cheng72261582005-12-20 06:22:03 +00009248const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9249 switch (Opcode) {
9250 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009251 case X86ISD::BSF: return "X86ISD::BSF";
9252 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009253 case X86ISD::SHLD: return "X86ISD::SHLD";
9254 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009255 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009256 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009257 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009258 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009259 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009260 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009261 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9262 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9263 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009264 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009265 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009266 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009267 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009268 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009269 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009270 case X86ISD::COMI: return "X86ISD::COMI";
9271 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009272 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009273 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009274 case X86ISD::CMOV: return "X86ISD::CMOV";
9275 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009276 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009277 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9278 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009279 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009280 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009281 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009282 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009283 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009284 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9285 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009286 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009287 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009288 case X86ISD::PANDN: return "X86ISD::PANDN";
9289 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9290 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9291 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009292 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009293 case X86ISD::FMAX: return "X86ISD::FMAX";
9294 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009295 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9296 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009297 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009298 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009299 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009300 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009301 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009302 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9303 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009304 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9305 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9306 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9307 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9308 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9309 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009310 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9311 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009312 case X86ISD::VSHL: return "X86ISD::VSHL";
9313 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009314 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9315 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9316 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9317 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9318 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9319 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9320 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9321 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9322 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9323 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009324 case X86ISD::ADD: return "X86ISD::ADD";
9325 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009326 case X86ISD::ADC: return "X86ISD::ADC";
9327 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009328 case X86ISD::SMUL: return "X86ISD::SMUL";
9329 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009330 case X86ISD::INC: return "X86ISD::INC";
9331 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009332 case X86ISD::OR: return "X86ISD::OR";
9333 case X86ISD::XOR: return "X86ISD::XOR";
9334 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009335 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009336 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009337 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009338 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9339 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9340 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9341 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9342 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9343 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9344 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9345 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9346 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009347 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009348 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009349 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009350 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9351 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009352 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9353 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9354 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9355 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9356 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9357 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9358 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9359 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9360 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009361 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9362 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9363 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9364 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009365 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9366 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9367 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9368 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9369 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9370 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9371 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9372 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9373 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9374 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009375 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009376 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009377 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009378 }
9379}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009380
Chris Lattnerc9addb72007-03-30 23:15:24 +00009381// isLegalAddressingMode - Return true if the addressing mode represented
9382// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009383bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009384 const Type *Ty) const {
9385 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009386 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009387 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009388
Chris Lattnerc9addb72007-03-30 23:15:24 +00009389 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009390 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009391 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009392
Chris Lattnerc9addb72007-03-30 23:15:24 +00009393 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009394 unsigned GVFlags =
9395 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009396
Chris Lattnerdfed4132009-07-10 07:38:24 +00009397 // If a reference to this global requires an extra load, we can't fold it.
9398 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009399 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009400
Chris Lattnerdfed4132009-07-10 07:38:24 +00009401 // If BaseGV requires a register for the PIC base, we cannot also have a
9402 // BaseReg specified.
9403 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009404 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009405
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009406 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009407 if ((M != CodeModel::Small || R != Reloc::Static) &&
9408 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009409 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009411
Chris Lattnerc9addb72007-03-30 23:15:24 +00009412 switch (AM.Scale) {
9413 case 0:
9414 case 1:
9415 case 2:
9416 case 4:
9417 case 8:
9418 // These scales always work.
9419 break;
9420 case 3:
9421 case 5:
9422 case 9:
9423 // These scales are formed with basereg+scalereg. Only accept if there is
9424 // no basereg yet.
9425 if (AM.HasBaseReg)
9426 return false;
9427 break;
9428 default: // Other stuff never works.
9429 return false;
9430 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009431
Chris Lattnerc9addb72007-03-30 23:15:24 +00009432 return true;
9433}
9434
9435
Evan Cheng2bd122c2007-10-26 01:56:11 +00009436bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009437 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009438 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009439 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9440 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009441 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009442 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009443 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009444}
9445
Owen Andersone50ed302009-08-10 22:56:29 +00009446bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009447 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009448 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009449 unsigned NumBits1 = VT1.getSizeInBits();
9450 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009451 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009452 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009453 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009454}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009455
Dan Gohman97121ba2009-04-08 00:15:30 +00009456bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009457 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009458 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009459}
9460
Owen Andersone50ed302009-08-10 22:56:29 +00009461bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009462 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009463 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009464}
9465
Owen Andersone50ed302009-08-10 22:56:29 +00009466bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009467 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009468 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009469}
9470
Evan Cheng60c07e12006-07-05 22:17:51 +00009471/// isShuffleMaskLegal - Targets can use this to indicate that they only
9472/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9473/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9474/// are assumed to be legal.
9475bool
Eric Christopherfd179292009-08-27 18:07:15 +00009476X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009477 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009478 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009479 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009480 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009481
Nate Begemana09008b2009-10-19 02:17:23 +00009482 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009483 return (VT.getVectorNumElements() == 2 ||
9484 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9485 isMOVLMask(M, VT) ||
9486 isSHUFPMask(M, VT) ||
9487 isPSHUFDMask(M, VT) ||
9488 isPSHUFHWMask(M, VT) ||
9489 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009490 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009491 isUNPCKLMask(M, VT) ||
9492 isUNPCKHMask(M, VT) ||
9493 isUNPCKL_v_undef_Mask(M, VT) ||
9494 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009495}
9496
Dan Gohman7d8143f2008-04-09 20:09:42 +00009497bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009498X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009499 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009500 unsigned NumElts = VT.getVectorNumElements();
9501 // FIXME: This collection of masks seems suspect.
9502 if (NumElts == 2)
9503 return true;
9504 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9505 return (isMOVLMask(Mask, VT) ||
9506 isCommutedMOVLMask(Mask, VT, true) ||
9507 isSHUFPMask(Mask, VT) ||
9508 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009509 }
9510 return false;
9511}
9512
9513//===----------------------------------------------------------------------===//
9514// X86 Scheduler Hooks
9515//===----------------------------------------------------------------------===//
9516
Mon P Wang63307c32008-05-05 19:05:59 +00009517// private utility function
9518MachineBasicBlock *
9519X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9520 MachineBasicBlock *MBB,
9521 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009522 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009523 unsigned LoadOpc,
9524 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009525 unsigned notOpc,
9526 unsigned EAXreg,
9527 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009528 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009529 // For the atomic bitwise operator, we generate
9530 // thisMBB:
9531 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009532 // ld t1 = [bitinstr.addr]
9533 // op t2 = t1, [bitinstr.val]
9534 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009535 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9536 // bz newMBB
9537 // fallthrough -->nextMBB
9538 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9539 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009540 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009541 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009542
Mon P Wang63307c32008-05-05 19:05:59 +00009543 /// First build the CFG
9544 MachineFunction *F = MBB->getParent();
9545 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009546 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9547 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9548 F->insert(MBBIter, newMBB);
9549 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009550
Dan Gohman14152b42010-07-06 20:24:04 +00009551 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9552 nextMBB->splice(nextMBB->begin(), thisMBB,
9553 llvm::next(MachineBasicBlock::iterator(bInstr)),
9554 thisMBB->end());
9555 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009556
Mon P Wang63307c32008-05-05 19:05:59 +00009557 // Update thisMBB to fall through to newMBB
9558 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009559
Mon P Wang63307c32008-05-05 19:05:59 +00009560 // newMBB jumps to itself and fall through to nextMBB
9561 newMBB->addSuccessor(nextMBB);
9562 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009563
Mon P Wang63307c32008-05-05 19:05:59 +00009564 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009565 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009566 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009567 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009568 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009569 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009570 int numArgs = bInstr->getNumOperands() - 1;
9571 for (int i=0; i < numArgs; ++i)
9572 argOpers[i] = &bInstr->getOperand(i+1);
9573
9574 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009575 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009576 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009577
Dale Johannesen140be2d2008-08-19 18:47:28 +00009578 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009579 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009580 for (int i=0; i <= lastAddrIndx; ++i)
9581 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009582
Dale Johannesen140be2d2008-08-19 18:47:28 +00009583 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009584 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009585 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009587 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009588 tt = t1;
9589
Dale Johannesen140be2d2008-08-19 18:47:28 +00009590 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009591 assert((argOpers[valArgIndx]->isReg() ||
9592 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009593 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009594 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009595 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009596 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009597 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009598 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009599 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009600
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009601 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009602 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009603
Dale Johannesene4d209d2009-02-03 20:21:25 +00009604 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009605 for (int i=0; i <= lastAddrIndx; ++i)
9606 (*MIB).addOperand(*argOpers[i]);
9607 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009608 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009609 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9610 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009611
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009612 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009613 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009614
Mon P Wang63307c32008-05-05 19:05:59 +00009615 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009616 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009617
Dan Gohman14152b42010-07-06 20:24:04 +00009618 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009619 return nextMBB;
9620}
9621
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009622// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009623MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009624X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9625 MachineBasicBlock *MBB,
9626 unsigned regOpcL,
9627 unsigned regOpcH,
9628 unsigned immOpcL,
9629 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009630 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009631 // For the atomic bitwise operator, we generate
9632 // thisMBB (instructions are in pairs, except cmpxchg8b)
9633 // ld t1,t2 = [bitinstr.addr]
9634 // newMBB:
9635 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9636 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009637 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009638 // mov ECX, EBX <- t5, t6
9639 // mov EAX, EDX <- t1, t2
9640 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9641 // mov t3, t4 <- EAX, EDX
9642 // bz newMBB
9643 // result in out1, out2
9644 // fallthrough -->nextMBB
9645
9646 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9647 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009648 const unsigned NotOpc = X86::NOT32r;
9649 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9650 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9651 MachineFunction::iterator MBBIter = MBB;
9652 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009653
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009654 /// First build the CFG
9655 MachineFunction *F = MBB->getParent();
9656 MachineBasicBlock *thisMBB = MBB;
9657 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9658 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9659 F->insert(MBBIter, newMBB);
9660 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009661
Dan Gohman14152b42010-07-06 20:24:04 +00009662 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9663 nextMBB->splice(nextMBB->begin(), thisMBB,
9664 llvm::next(MachineBasicBlock::iterator(bInstr)),
9665 thisMBB->end());
9666 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009667
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009668 // Update thisMBB to fall through to newMBB
9669 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009670
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009671 // newMBB jumps to itself and fall through to nextMBB
9672 newMBB->addSuccessor(nextMBB);
9673 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009674
Dale Johannesene4d209d2009-02-03 20:21:25 +00009675 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009676 // Insert instructions into newMBB based on incoming instruction
9677 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009678 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009679 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009680 MachineOperand& dest1Oper = bInstr->getOperand(0);
9681 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009682 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9683 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009684 argOpers[i] = &bInstr->getOperand(i+2);
9685
Dan Gohman71ea4e52010-05-14 21:01:44 +00009686 // We use some of the operands multiple times, so conservatively just
9687 // clear any kill flags that might be present.
9688 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9689 argOpers[i]->setIsKill(false);
9690 }
9691
Evan Chengad5b52f2010-01-08 19:14:57 +00009692 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009693 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009694
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009695 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009696 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009697 for (int i=0; i <= lastAddrIndx; ++i)
9698 (*MIB).addOperand(*argOpers[i]);
9699 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009700 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009701 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009702 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009703 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009704 MachineOperand newOp3 = *(argOpers[3]);
9705 if (newOp3.isImm())
9706 newOp3.setImm(newOp3.getImm()+4);
9707 else
9708 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009709 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009710 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009711
9712 // t3/4 are defined later, at the bottom of the loop
9713 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9714 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009715 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009716 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009717 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009718 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9719
Evan Cheng306b4ca2010-01-08 23:41:50 +00009720 // The subsequent operations should be using the destination registers of
9721 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009722 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009723 t1 = F->getRegInfo().createVirtualRegister(RC);
9724 t2 = F->getRegInfo().createVirtualRegister(RC);
9725 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9726 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009727 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009728 t1 = dest1Oper.getReg();
9729 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009730 }
9731
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009732 int valArgIndx = lastAddrIndx + 1;
9733 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009734 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009735 "invalid operand");
9736 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9737 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009738 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009739 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009740 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009741 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009742 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009743 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009744 (*MIB).addOperand(*argOpers[valArgIndx]);
9745 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009746 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009747 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009748 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009749 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009750 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009751 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009752 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009753 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009754 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009755 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009756
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009757 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009758 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009759 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009760 MIB.addReg(t2);
9761
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009762 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009763 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009764 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009765 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009766
Dale Johannesene4d209d2009-02-03 20:21:25 +00009767 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009768 for (int i=0; i <= lastAddrIndx; ++i)
9769 (*MIB).addOperand(*argOpers[i]);
9770
9771 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009772 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9773 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009774
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009775 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009776 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009777 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009778 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009779
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009780 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009781 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009782
Dan Gohman14152b42010-07-06 20:24:04 +00009783 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009784 return nextMBB;
9785}
9786
9787// private utility function
9788MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009789X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9790 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009791 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009792 // For the atomic min/max operator, we generate
9793 // thisMBB:
9794 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009795 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009796 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009797 // cmp t1, t2
9798 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009799 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009800 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9801 // bz newMBB
9802 // fallthrough -->nextMBB
9803 //
9804 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9805 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009806 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009807 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009808
Mon P Wang63307c32008-05-05 19:05:59 +00009809 /// First build the CFG
9810 MachineFunction *F = MBB->getParent();
9811 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009812 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9813 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9814 F->insert(MBBIter, newMBB);
9815 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009816
Dan Gohman14152b42010-07-06 20:24:04 +00009817 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9818 nextMBB->splice(nextMBB->begin(), thisMBB,
9819 llvm::next(MachineBasicBlock::iterator(mInstr)),
9820 thisMBB->end());
9821 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009822
Mon P Wang63307c32008-05-05 19:05:59 +00009823 // Update thisMBB to fall through to newMBB
9824 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009825
Mon P Wang63307c32008-05-05 19:05:59 +00009826 // newMBB jumps to newMBB and fall through to nextMBB
9827 newMBB->addSuccessor(nextMBB);
9828 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009829
Dale Johannesene4d209d2009-02-03 20:21:25 +00009830 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009831 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009832 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009833 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009834 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009835 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009836 int numArgs = mInstr->getNumOperands() - 1;
9837 for (int i=0; i < numArgs; ++i)
9838 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009839
Mon P Wang63307c32008-05-05 19:05:59 +00009840 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009841 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009842 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009843
Mon P Wangab3e7472008-05-05 22:56:23 +00009844 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009845 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009846 for (int i=0; i <= lastAddrIndx; ++i)
9847 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009848
Mon P Wang63307c32008-05-05 19:05:59 +00009849 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009850 assert((argOpers[valArgIndx]->isReg() ||
9851 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009852 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009853
9854 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009855 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009856 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009857 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009858 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009859 (*MIB).addOperand(*argOpers[valArgIndx]);
9860
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009861 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009862 MIB.addReg(t1);
9863
Dale Johannesene4d209d2009-02-03 20:21:25 +00009864 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009865 MIB.addReg(t1);
9866 MIB.addReg(t2);
9867
9868 // Generate movc
9869 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009870 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009871 MIB.addReg(t2);
9872 MIB.addReg(t1);
9873
9874 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009875 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009876 for (int i=0; i <= lastAddrIndx; ++i)
9877 (*MIB).addOperand(*argOpers[i]);
9878 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009879 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009880 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9881 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009882
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009883 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009884 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009885
Mon P Wang63307c32008-05-05 19:05:59 +00009886 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009887 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009888
Dan Gohman14152b42010-07-06 20:24:04 +00009889 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009890 return nextMBB;
9891}
9892
Eric Christopherf83a5de2009-08-27 18:08:16 +00009893// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009894// or XMM0_V32I8 in AVX all of this code can be replaced with that
9895// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009896MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009897X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009898 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009899 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9900 "Target must have SSE4.2 or AVX features enabled");
9901
Eric Christopherb120ab42009-08-18 22:50:32 +00009902 DebugLoc dl = MI->getDebugLoc();
9903 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009904 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009905 if (!Subtarget->hasAVX()) {
9906 if (memArg)
9907 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9908 else
9909 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9910 } else {
9911 if (memArg)
9912 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9913 else
9914 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9915 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009916
Eric Christopher41c902f2010-11-30 08:20:21 +00009917 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009918 for (unsigned i = 0; i < numArgs; ++i) {
9919 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009920 if (!(Op.isReg() && Op.isImplicit()))
9921 MIB.addOperand(Op);
9922 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009923 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009924 .addReg(X86::XMM0);
9925
Dan Gohman14152b42010-07-06 20:24:04 +00009926 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009927 return BB;
9928}
9929
9930MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009931X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009932 DebugLoc dl = MI->getDebugLoc();
9933 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009934
Eric Christopher228232b2010-11-30 07:20:12 +00009935 // Address into RAX/EAX, other two args into ECX, EDX.
9936 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9937 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9938 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9939 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009940 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009941
Eric Christopher228232b2010-11-30 07:20:12 +00009942 unsigned ValOps = X86::AddrNumOperands;
9943 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9944 .addReg(MI->getOperand(ValOps).getReg());
9945 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9946 .addReg(MI->getOperand(ValOps+1).getReg());
9947
9948 // The instruction doesn't actually take any operands though.
9949 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009950
Eric Christopher228232b2010-11-30 07:20:12 +00009951 MI->eraseFromParent(); // The pseudo is gone now.
9952 return BB;
9953}
9954
9955MachineBasicBlock *
9956X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009957 DebugLoc dl = MI->getDebugLoc();
9958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009959
Eric Christopher228232b2010-11-30 07:20:12 +00009960 // First arg in ECX, the second in EAX.
9961 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9962 .addReg(MI->getOperand(0).getReg());
9963 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9964 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009965
Eric Christopher228232b2010-11-30 07:20:12 +00009966 // The instruction doesn't actually take any operands though.
9967 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009968
Eric Christopher228232b2010-11-30 07:20:12 +00009969 MI->eraseFromParent(); // The pseudo is gone now.
9970 return BB;
9971}
9972
9973MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009974X86TargetLowering::EmitVAARG64WithCustomInserter(
9975 MachineInstr *MI,
9976 MachineBasicBlock *MBB) const {
9977 // Emit va_arg instruction on X86-64.
9978
9979 // Operands to this pseudo-instruction:
9980 // 0 ) Output : destination address (reg)
9981 // 1-5) Input : va_list address (addr, i64mem)
9982 // 6 ) ArgSize : Size (in bytes) of vararg type
9983 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9984 // 8 ) Align : Alignment of type
9985 // 9 ) EFLAGS (implicit-def)
9986
9987 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9988 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9989
9990 unsigned DestReg = MI->getOperand(0).getReg();
9991 MachineOperand &Base = MI->getOperand(1);
9992 MachineOperand &Scale = MI->getOperand(2);
9993 MachineOperand &Index = MI->getOperand(3);
9994 MachineOperand &Disp = MI->getOperand(4);
9995 MachineOperand &Segment = MI->getOperand(5);
9996 unsigned ArgSize = MI->getOperand(6).getImm();
9997 unsigned ArgMode = MI->getOperand(7).getImm();
9998 unsigned Align = MI->getOperand(8).getImm();
9999
10000 // Memory Reference
10001 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10002 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10003 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10004
10005 // Machine Information
10006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10007 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10008 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10009 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10010 DebugLoc DL = MI->getDebugLoc();
10011
10012 // struct va_list {
10013 // i32 gp_offset
10014 // i32 fp_offset
10015 // i64 overflow_area (address)
10016 // i64 reg_save_area (address)
10017 // }
10018 // sizeof(va_list) = 24
10019 // alignment(va_list) = 8
10020
10021 unsigned TotalNumIntRegs = 6;
10022 unsigned TotalNumXMMRegs = 8;
10023 bool UseGPOffset = (ArgMode == 1);
10024 bool UseFPOffset = (ArgMode == 2);
10025 unsigned MaxOffset = TotalNumIntRegs * 8 +
10026 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10027
10028 /* Align ArgSize to a multiple of 8 */
10029 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10030 bool NeedsAlign = (Align > 8);
10031
10032 MachineBasicBlock *thisMBB = MBB;
10033 MachineBasicBlock *overflowMBB;
10034 MachineBasicBlock *offsetMBB;
10035 MachineBasicBlock *endMBB;
10036
10037 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10038 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10039 unsigned OffsetReg = 0;
10040
10041 if (!UseGPOffset && !UseFPOffset) {
10042 // If we only pull from the overflow region, we don't create a branch.
10043 // We don't need to alter control flow.
10044 OffsetDestReg = 0; // unused
10045 OverflowDestReg = DestReg;
10046
10047 offsetMBB = NULL;
10048 overflowMBB = thisMBB;
10049 endMBB = thisMBB;
10050 } else {
10051 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10052 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10053 // If not, pull from overflow_area. (branch to overflowMBB)
10054 //
10055 // thisMBB
10056 // | .
10057 // | .
10058 // offsetMBB overflowMBB
10059 // | .
10060 // | .
10061 // endMBB
10062
10063 // Registers for the PHI in endMBB
10064 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10065 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10066
10067 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10068 MachineFunction *MF = MBB->getParent();
10069 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10070 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10071 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10072
10073 MachineFunction::iterator MBBIter = MBB;
10074 ++MBBIter;
10075
10076 // Insert the new basic blocks
10077 MF->insert(MBBIter, offsetMBB);
10078 MF->insert(MBBIter, overflowMBB);
10079 MF->insert(MBBIter, endMBB);
10080
10081 // Transfer the remainder of MBB and its successor edges to endMBB.
10082 endMBB->splice(endMBB->begin(), thisMBB,
10083 llvm::next(MachineBasicBlock::iterator(MI)),
10084 thisMBB->end());
10085 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10086
10087 // Make offsetMBB and overflowMBB successors of thisMBB
10088 thisMBB->addSuccessor(offsetMBB);
10089 thisMBB->addSuccessor(overflowMBB);
10090
10091 // endMBB is a successor of both offsetMBB and overflowMBB
10092 offsetMBB->addSuccessor(endMBB);
10093 overflowMBB->addSuccessor(endMBB);
10094
10095 // Load the offset value into a register
10096 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10097 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10098 .addOperand(Base)
10099 .addOperand(Scale)
10100 .addOperand(Index)
10101 .addDisp(Disp, UseFPOffset ? 4 : 0)
10102 .addOperand(Segment)
10103 .setMemRefs(MMOBegin, MMOEnd);
10104
10105 // Check if there is enough room left to pull this argument.
10106 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10107 .addReg(OffsetReg)
10108 .addImm(MaxOffset + 8 - ArgSizeA8);
10109
10110 // Branch to "overflowMBB" if offset >= max
10111 // Fall through to "offsetMBB" otherwise
10112 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10113 .addMBB(overflowMBB);
10114 }
10115
10116 // In offsetMBB, emit code to use the reg_save_area.
10117 if (offsetMBB) {
10118 assert(OffsetReg != 0);
10119
10120 // Read the reg_save_area address.
10121 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10122 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10123 .addOperand(Base)
10124 .addOperand(Scale)
10125 .addOperand(Index)
10126 .addDisp(Disp, 16)
10127 .addOperand(Segment)
10128 .setMemRefs(MMOBegin, MMOEnd);
10129
10130 // Zero-extend the offset
10131 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10132 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10133 .addImm(0)
10134 .addReg(OffsetReg)
10135 .addImm(X86::sub_32bit);
10136
10137 // Add the offset to the reg_save_area to get the final address.
10138 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10139 .addReg(OffsetReg64)
10140 .addReg(RegSaveReg);
10141
10142 // Compute the offset for the next argument
10143 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10144 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10145 .addReg(OffsetReg)
10146 .addImm(UseFPOffset ? 16 : 8);
10147
10148 // Store it back into the va_list.
10149 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10150 .addOperand(Base)
10151 .addOperand(Scale)
10152 .addOperand(Index)
10153 .addDisp(Disp, UseFPOffset ? 4 : 0)
10154 .addOperand(Segment)
10155 .addReg(NextOffsetReg)
10156 .setMemRefs(MMOBegin, MMOEnd);
10157
10158 // Jump to endMBB
10159 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10160 .addMBB(endMBB);
10161 }
10162
10163 //
10164 // Emit code to use overflow area
10165 //
10166
10167 // Load the overflow_area address into a register.
10168 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10169 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10170 .addOperand(Base)
10171 .addOperand(Scale)
10172 .addOperand(Index)
10173 .addDisp(Disp, 8)
10174 .addOperand(Segment)
10175 .setMemRefs(MMOBegin, MMOEnd);
10176
10177 // If we need to align it, do so. Otherwise, just copy the address
10178 // to OverflowDestReg.
10179 if (NeedsAlign) {
10180 // Align the overflow address
10181 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10182 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10183
10184 // aligned_addr = (addr + (align-1)) & ~(align-1)
10185 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10186 .addReg(OverflowAddrReg)
10187 .addImm(Align-1);
10188
10189 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10190 .addReg(TmpReg)
10191 .addImm(~(uint64_t)(Align-1));
10192 } else {
10193 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10194 .addReg(OverflowAddrReg);
10195 }
10196
10197 // Compute the next overflow address after this argument.
10198 // (the overflow address should be kept 8-byte aligned)
10199 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10200 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10201 .addReg(OverflowDestReg)
10202 .addImm(ArgSizeA8);
10203
10204 // Store the new overflow address.
10205 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10206 .addOperand(Base)
10207 .addOperand(Scale)
10208 .addOperand(Index)
10209 .addDisp(Disp, 8)
10210 .addOperand(Segment)
10211 .addReg(NextAddrReg)
10212 .setMemRefs(MMOBegin, MMOEnd);
10213
10214 // If we branched, emit the PHI to the front of endMBB.
10215 if (offsetMBB) {
10216 BuildMI(*endMBB, endMBB->begin(), DL,
10217 TII->get(X86::PHI), DestReg)
10218 .addReg(OffsetDestReg).addMBB(offsetMBB)
10219 .addReg(OverflowDestReg).addMBB(overflowMBB);
10220 }
10221
10222 // Erase the pseudo instruction
10223 MI->eraseFromParent();
10224
10225 return endMBB;
10226}
10227
10228MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010229X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10230 MachineInstr *MI,
10231 MachineBasicBlock *MBB) const {
10232 // Emit code to save XMM registers to the stack. The ABI says that the
10233 // number of registers to save is given in %al, so it's theoretically
10234 // possible to do an indirect jump trick to avoid saving all of them,
10235 // however this code takes a simpler approach and just executes all
10236 // of the stores if %al is non-zero. It's less code, and it's probably
10237 // easier on the hardware branch predictor, and stores aren't all that
10238 // expensive anyway.
10239
10240 // Create the new basic blocks. One block contains all the XMM stores,
10241 // and one block is the final destination regardless of whether any
10242 // stores were performed.
10243 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10244 MachineFunction *F = MBB->getParent();
10245 MachineFunction::iterator MBBIter = MBB;
10246 ++MBBIter;
10247 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10248 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10249 F->insert(MBBIter, XMMSaveMBB);
10250 F->insert(MBBIter, EndMBB);
10251
Dan Gohman14152b42010-07-06 20:24:04 +000010252 // Transfer the remainder of MBB and its successor edges to EndMBB.
10253 EndMBB->splice(EndMBB->begin(), MBB,
10254 llvm::next(MachineBasicBlock::iterator(MI)),
10255 MBB->end());
10256 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10257
Dan Gohmand6708ea2009-08-15 01:38:56 +000010258 // The original block will now fall through to the XMM save block.
10259 MBB->addSuccessor(XMMSaveMBB);
10260 // The XMMSaveMBB will fall through to the end block.
10261 XMMSaveMBB->addSuccessor(EndMBB);
10262
10263 // Now add the instructions.
10264 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10265 DebugLoc DL = MI->getDebugLoc();
10266
10267 unsigned CountReg = MI->getOperand(0).getReg();
10268 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10269 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10270
10271 if (!Subtarget->isTargetWin64()) {
10272 // If %al is 0, branch around the XMM save block.
10273 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010274 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010275 MBB->addSuccessor(EndMBB);
10276 }
10277
10278 // In the XMM save block, save all the XMM argument registers.
10279 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10280 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010281 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010282 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010283 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010284 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010285 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010286 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10287 .addFrameIndex(RegSaveFrameIndex)
10288 .addImm(/*Scale=*/1)
10289 .addReg(/*IndexReg=*/0)
10290 .addImm(/*Disp=*/Offset)
10291 .addReg(/*Segment=*/0)
10292 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010293 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010294 }
10295
Dan Gohman14152b42010-07-06 20:24:04 +000010296 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010297
10298 return EndMBB;
10299}
Mon P Wang63307c32008-05-05 19:05:59 +000010300
Evan Cheng60c07e12006-07-05 22:17:51 +000010301MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010302X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010303 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010304 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10305 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010306
Chris Lattner52600972009-09-02 05:57:00 +000010307 // To "insert" a SELECT_CC instruction, we actually have to insert the
10308 // diamond control-flow pattern. The incoming instruction knows the
10309 // destination vreg to set, the condition code register to branch on, the
10310 // true/false values to select between, and a branch opcode to use.
10311 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10312 MachineFunction::iterator It = BB;
10313 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010314
Chris Lattner52600972009-09-02 05:57:00 +000010315 // thisMBB:
10316 // ...
10317 // TrueVal = ...
10318 // cmpTY ccX, r1, r2
10319 // bCC copy1MBB
10320 // fallthrough --> copy0MBB
10321 MachineBasicBlock *thisMBB = BB;
10322 MachineFunction *F = BB->getParent();
10323 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10324 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010325 F->insert(It, copy0MBB);
10326 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010327
Bill Wendling730c07e2010-06-25 20:48:10 +000010328 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10329 // live into the sink and copy blocks.
10330 const MachineFunction *MF = BB->getParent();
10331 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10332 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010333
Dan Gohman14152b42010-07-06 20:24:04 +000010334 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10335 const MachineOperand &MO = MI->getOperand(I);
10336 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010337 unsigned Reg = MO.getReg();
10338 if (Reg != X86::EFLAGS) continue;
10339 copy0MBB->addLiveIn(Reg);
10340 sinkMBB->addLiveIn(Reg);
10341 }
10342
Dan Gohman14152b42010-07-06 20:24:04 +000010343 // Transfer the remainder of BB and its successor edges to sinkMBB.
10344 sinkMBB->splice(sinkMBB->begin(), BB,
10345 llvm::next(MachineBasicBlock::iterator(MI)),
10346 BB->end());
10347 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10348
10349 // Add the true and fallthrough blocks as its successors.
10350 BB->addSuccessor(copy0MBB);
10351 BB->addSuccessor(sinkMBB);
10352
10353 // Create the conditional branch instruction.
10354 unsigned Opc =
10355 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10356 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10357
Chris Lattner52600972009-09-02 05:57:00 +000010358 // copy0MBB:
10359 // %FalseValue = ...
10360 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010361 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010362
Chris Lattner52600972009-09-02 05:57:00 +000010363 // sinkMBB:
10364 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10365 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010366 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10367 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010368 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10369 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10370
Dan Gohman14152b42010-07-06 20:24:04 +000010371 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010372 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010373}
10374
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010375MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010376X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010377 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010378 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10379 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010380
10381 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10382 // non-trivial part is impdef of ESP.
10383 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10384 // mingw-w64.
10385
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010386 const char *StackProbeSymbol =
10387 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10388
Dan Gohman14152b42010-07-06 20:24:04 +000010389 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010390 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010391 .addReg(X86::EAX, RegState::Implicit)
10392 .addReg(X86::ESP, RegState::Implicit)
10393 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010394 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10395 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010396
Dan Gohman14152b42010-07-06 20:24:04 +000010397 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010398 return BB;
10399}
Chris Lattner52600972009-09-02 05:57:00 +000010400
10401MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010402X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10403 MachineBasicBlock *BB) const {
10404 // This is pretty easy. We're taking the value that we received from
10405 // our load from the relocation, sticking it in either RDI (x86-64)
10406 // or EAX and doing an indirect call. The return value will then
10407 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010408 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010409 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010410 DebugLoc DL = MI->getDebugLoc();
10411 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010412
10413 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010414 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010415
Eric Christopher30ef0e52010-06-03 04:07:48 +000010416 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010417 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10418 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010419 .addReg(X86::RIP)
10420 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010421 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010422 MI->getOperand(3).getTargetFlags())
10423 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010424 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010425 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010426 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010427 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10428 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010429 .addReg(0)
10430 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010431 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010432 MI->getOperand(3).getTargetFlags())
10433 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010434 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010435 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010436 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010437 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10438 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010439 .addReg(TII->getGlobalBaseReg(F))
10440 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010441 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010442 MI->getOperand(3).getTargetFlags())
10443 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010444 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010445 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010446 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010447
Dan Gohman14152b42010-07-06 20:24:04 +000010448 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010449 return BB;
10450}
10451
10452MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010453X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010454 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010455 switch (MI->getOpcode()) {
10456 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010457 case X86::TAILJMPd64:
10458 case X86::TAILJMPr64:
10459 case X86::TAILJMPm64:
10460 assert(!"TAILJMP64 would not be touched here.");
10461 case X86::TCRETURNdi64:
10462 case X86::TCRETURNri64:
10463 case X86::TCRETURNmi64:
10464 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10465 // On AMD64, additional defs should be added before register allocation.
10466 if (!Subtarget->isTargetWin64()) {
10467 MI->addRegisterDefined(X86::RSI);
10468 MI->addRegisterDefined(X86::RDI);
10469 MI->addRegisterDefined(X86::XMM6);
10470 MI->addRegisterDefined(X86::XMM7);
10471 MI->addRegisterDefined(X86::XMM8);
10472 MI->addRegisterDefined(X86::XMM9);
10473 MI->addRegisterDefined(X86::XMM10);
10474 MI->addRegisterDefined(X86::XMM11);
10475 MI->addRegisterDefined(X86::XMM12);
10476 MI->addRegisterDefined(X86::XMM13);
10477 MI->addRegisterDefined(X86::XMM14);
10478 MI->addRegisterDefined(X86::XMM15);
10479 }
10480 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010481 case X86::WIN_ALLOCA:
10482 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010483 case X86::TLSCall_32:
10484 case X86::TLSCall_64:
10485 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010486 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010487 case X86::CMOV_FR32:
10488 case X86::CMOV_FR64:
10489 case X86::CMOV_V4F32:
10490 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010491 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010492 case X86::CMOV_GR16:
10493 case X86::CMOV_GR32:
10494 case X86::CMOV_RFP32:
10495 case X86::CMOV_RFP64:
10496 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010497 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010498
Dale Johannesen849f2142007-07-03 00:53:03 +000010499 case X86::FP32_TO_INT16_IN_MEM:
10500 case X86::FP32_TO_INT32_IN_MEM:
10501 case X86::FP32_TO_INT64_IN_MEM:
10502 case X86::FP64_TO_INT16_IN_MEM:
10503 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010504 case X86::FP64_TO_INT64_IN_MEM:
10505 case X86::FP80_TO_INT16_IN_MEM:
10506 case X86::FP80_TO_INT32_IN_MEM:
10507 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010508 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10509 DebugLoc DL = MI->getDebugLoc();
10510
Evan Cheng60c07e12006-07-05 22:17:51 +000010511 // Change the floating point control register to use "round towards zero"
10512 // mode when truncating to an integer value.
10513 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010514 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010515 addFrameReference(BuildMI(*BB, MI, DL,
10516 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010517
10518 // Load the old value of the high byte of the control word...
10519 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010520 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010521 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010522 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010523
10524 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010525 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010526 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010527
10528 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010529 addFrameReference(BuildMI(*BB, MI, DL,
10530 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010531
10532 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010533 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010534 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010535
10536 // Get the X86 opcode to use.
10537 unsigned Opc;
10538 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010539 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010540 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10541 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10542 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10543 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10544 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10545 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010546 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10547 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10548 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010549 }
10550
10551 X86AddressMode AM;
10552 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010553 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010554 AM.BaseType = X86AddressMode::RegBase;
10555 AM.Base.Reg = Op.getReg();
10556 } else {
10557 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010558 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010559 }
10560 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010561 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010562 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010563 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010564 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010565 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010566 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010567 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010568 AM.GV = Op.getGlobal();
10569 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010570 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010571 }
Dan Gohman14152b42010-07-06 20:24:04 +000010572 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010573 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010574
10575 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010576 addFrameReference(BuildMI(*BB, MI, DL,
10577 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010578
Dan Gohman14152b42010-07-06 20:24:04 +000010579 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010580 return BB;
10581 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010582 // String/text processing lowering.
10583 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010584 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010585 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10586 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010587 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010588 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10589 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010590 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010591 return EmitPCMP(MI, BB, 5, false /* in mem */);
10592 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010593 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010594 return EmitPCMP(MI, BB, 5, true /* in mem */);
10595
Eric Christopher228232b2010-11-30 07:20:12 +000010596 // Thread synchronization.
10597 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010598 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010599 case X86::MWAIT:
10600 return EmitMwait(MI, BB);
10601
Eric Christopherb120ab42009-08-18 22:50:32 +000010602 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010603 case X86::ATOMAND32:
10604 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010605 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010606 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010607 X86::NOT32r, X86::EAX,
10608 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010609 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10611 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010612 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010613 X86::NOT32r, X86::EAX,
10614 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010615 case X86::ATOMXOR32:
10616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010617 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010618 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010619 X86::NOT32r, X86::EAX,
10620 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010621 case X86::ATOMNAND32:
10622 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010623 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010624 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010625 X86::NOT32r, X86::EAX,
10626 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010627 case X86::ATOMMIN32:
10628 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10629 case X86::ATOMMAX32:
10630 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10631 case X86::ATOMUMIN32:
10632 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10633 case X86::ATOMUMAX32:
10634 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010635
10636 case X86::ATOMAND16:
10637 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10638 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010639 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010640 X86::NOT16r, X86::AX,
10641 X86::GR16RegisterClass);
10642 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010644 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010645 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010646 X86::NOT16r, X86::AX,
10647 X86::GR16RegisterClass);
10648 case X86::ATOMXOR16:
10649 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10650 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010651 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010652 X86::NOT16r, X86::AX,
10653 X86::GR16RegisterClass);
10654 case X86::ATOMNAND16:
10655 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10656 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010657 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010658 X86::NOT16r, X86::AX,
10659 X86::GR16RegisterClass, true);
10660 case X86::ATOMMIN16:
10661 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10662 case X86::ATOMMAX16:
10663 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10664 case X86::ATOMUMIN16:
10665 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10666 case X86::ATOMUMAX16:
10667 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10668
10669 case X86::ATOMAND8:
10670 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10671 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010672 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010673 X86::NOT8r, X86::AL,
10674 X86::GR8RegisterClass);
10675 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010676 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010677 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010678 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010679 X86::NOT8r, X86::AL,
10680 X86::GR8RegisterClass);
10681 case X86::ATOMXOR8:
10682 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10683 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010684 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010685 X86::NOT8r, X86::AL,
10686 X86::GR8RegisterClass);
10687 case X86::ATOMNAND8:
10688 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10689 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010690 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010691 X86::NOT8r, X86::AL,
10692 X86::GR8RegisterClass, true);
10693 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010694 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010695 case X86::ATOMAND64:
10696 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010697 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010698 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010699 X86::NOT64r, X86::RAX,
10700 X86::GR64RegisterClass);
10701 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010702 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10703 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010704 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010705 X86::NOT64r, X86::RAX,
10706 X86::GR64RegisterClass);
10707 case X86::ATOMXOR64:
10708 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010709 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010710 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010711 X86::NOT64r, X86::RAX,
10712 X86::GR64RegisterClass);
10713 case X86::ATOMNAND64:
10714 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10715 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010716 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010717 X86::NOT64r, X86::RAX,
10718 X86::GR64RegisterClass, true);
10719 case X86::ATOMMIN64:
10720 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10721 case X86::ATOMMAX64:
10722 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10723 case X86::ATOMUMIN64:
10724 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10725 case X86::ATOMUMAX64:
10726 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010727
10728 // This group does 64-bit operations on a 32-bit host.
10729 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010730 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010731 X86::AND32rr, X86::AND32rr,
10732 X86::AND32ri, X86::AND32ri,
10733 false);
10734 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010735 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010736 X86::OR32rr, X86::OR32rr,
10737 X86::OR32ri, X86::OR32ri,
10738 false);
10739 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010740 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010741 X86::XOR32rr, X86::XOR32rr,
10742 X86::XOR32ri, X86::XOR32ri,
10743 false);
10744 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010745 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010746 X86::AND32rr, X86::AND32rr,
10747 X86::AND32ri, X86::AND32ri,
10748 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010749 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010750 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010751 X86::ADD32rr, X86::ADC32rr,
10752 X86::ADD32ri, X86::ADC32ri,
10753 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010754 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010755 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010756 X86::SUB32rr, X86::SBB32rr,
10757 X86::SUB32ri, X86::SBB32ri,
10758 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010759 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010760 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010761 X86::MOV32rr, X86::MOV32rr,
10762 X86::MOV32ri, X86::MOV32ri,
10763 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010764 case X86::VASTART_SAVE_XMM_REGS:
10765 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010766
10767 case X86::VAARG_64:
10768 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010769 }
10770}
10771
10772//===----------------------------------------------------------------------===//
10773// X86 Optimization Hooks
10774//===----------------------------------------------------------------------===//
10775
Dan Gohman475871a2008-07-27 21:46:04 +000010776void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010777 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010778 APInt &KnownZero,
10779 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010780 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010781 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010782 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010783 assert((Opc >= ISD::BUILTIN_OP_END ||
10784 Opc == ISD::INTRINSIC_WO_CHAIN ||
10785 Opc == ISD::INTRINSIC_W_CHAIN ||
10786 Opc == ISD::INTRINSIC_VOID) &&
10787 "Should use MaskedValueIsZero if you don't know whether Op"
10788 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010789
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010790 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010791 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010792 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010793 case X86ISD::ADD:
10794 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010795 case X86ISD::ADC:
10796 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010797 case X86ISD::SMUL:
10798 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010799 case X86ISD::INC:
10800 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010801 case X86ISD::OR:
10802 case X86ISD::XOR:
10803 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010804 // These nodes' second result is a boolean.
10805 if (Op.getResNo() == 0)
10806 break;
10807 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010808 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010809 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10810 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010811 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010812 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010813}
Chris Lattner259e97c2006-01-31 19:43:35 +000010814
Owen Andersonbc146b02010-09-21 20:42:50 +000010815unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10816 unsigned Depth) const {
10817 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10818 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10819 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010820
Owen Andersonbc146b02010-09-21 20:42:50 +000010821 // Fallback case.
10822 return 1;
10823}
10824
Evan Cheng206ee9d2006-07-07 08:33:52 +000010825/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010826/// node is a GlobalAddress + offset.
10827bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010828 const GlobalValue* &GA,
10829 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010830 if (N->getOpcode() == X86ISD::Wrapper) {
10831 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010832 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010833 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010834 return true;
10835 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010836 }
Evan Chengad4196b2008-05-12 19:56:52 +000010837 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010838}
10839
Evan Cheng206ee9d2006-07-07 08:33:52 +000010840/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10841/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10842/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010843/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010844static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010845 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010846 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010847 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010848
Eli Friedman7a5e5552009-06-07 06:52:44 +000010849 if (VT.getSizeInBits() != 128)
10850 return SDValue();
10851
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010852 // Don't create instructions with illegal types after legalize types has run.
10853 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10854 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10855 return SDValue();
10856
Nate Begemanfdea31a2010-03-24 20:49:50 +000010857 SmallVector<SDValue, 16> Elts;
10858 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010859 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010860
Nate Begemanfdea31a2010-03-24 20:49:50 +000010861 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010862}
Evan Chengd880b972008-05-09 21:53:03 +000010863
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010864/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10865/// generation and convert it from being a bunch of shuffles and extracts
10866/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010867static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10868 const TargetLowering &TLI) {
10869 SDValue InputVector = N->getOperand(0);
10870
10871 // Only operate on vectors of 4 elements, where the alternative shuffling
10872 // gets to be more expensive.
10873 if (InputVector.getValueType() != MVT::v4i32)
10874 return SDValue();
10875
10876 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10877 // single use which is a sign-extend or zero-extend, and all elements are
10878 // used.
10879 SmallVector<SDNode *, 4> Uses;
10880 unsigned ExtractedElements = 0;
10881 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10882 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10883 if (UI.getUse().getResNo() != InputVector.getResNo())
10884 return SDValue();
10885
10886 SDNode *Extract = *UI;
10887 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10888 return SDValue();
10889
10890 if (Extract->getValueType(0) != MVT::i32)
10891 return SDValue();
10892 if (!Extract->hasOneUse())
10893 return SDValue();
10894 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10895 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10896 return SDValue();
10897 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10898 return SDValue();
10899
10900 // Record which element was extracted.
10901 ExtractedElements |=
10902 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10903
10904 Uses.push_back(Extract);
10905 }
10906
10907 // If not all the elements were used, this may not be worthwhile.
10908 if (ExtractedElements != 15)
10909 return SDValue();
10910
10911 // Ok, we've now decided to do the transformation.
10912 DebugLoc dl = InputVector.getDebugLoc();
10913
10914 // Store the value to a temporary stack slot.
10915 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010916 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10917 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010918
10919 // Replace each use (extract) with a load of the appropriate element.
10920 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10921 UE = Uses.end(); UI != UE; ++UI) {
10922 SDNode *Extract = *UI;
10923
10924 // Compute the element's address.
10925 SDValue Idx = Extract->getOperand(1);
10926 unsigned EltSize =
10927 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10928 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10929 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10930
Eric Christopher90eb4022010-07-22 00:26:08 +000010931 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010932 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010933
10934 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010935 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010936 ScalarAddr, MachinePointerInfo(),
10937 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010938
10939 // Replace the exact with the load.
10940 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10941 }
10942
10943 // The replacement was made in place; don't return anything.
10944 return SDValue();
10945}
10946
Chris Lattner83e6c992006-10-04 06:57:07 +000010947/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010948static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010949 const X86Subtarget *Subtarget) {
10950 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010951 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010952 // Get the LHS/RHS of the select.
10953 SDValue LHS = N->getOperand(1);
10954 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010955
Dan Gohman670e5392009-09-21 18:03:22 +000010956 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010957 // instructions match the semantics of the common C idiom x<y?x:y but not
10958 // x<=y?x:y, because of how they handle negative zero (which can be
10959 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010960 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010961 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010962 Cond.getOpcode() == ISD::SETCC) {
10963 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010964
Chris Lattner47b4ce82009-03-11 05:48:52 +000010965 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010966 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010967 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10968 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010969 switch (CC) {
10970 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010971 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010972 // Converting this to a min would handle NaNs incorrectly, and swapping
10973 // the operands would cause it to handle comparisons between positive
10974 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010975 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010976 if (!UnsafeFPMath &&
10977 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10978 break;
10979 std::swap(LHS, RHS);
10980 }
Dan Gohman670e5392009-09-21 18:03:22 +000010981 Opcode = X86ISD::FMIN;
10982 break;
10983 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010984 // Converting this to a min would handle comparisons between positive
10985 // and negative zero incorrectly.
10986 if (!UnsafeFPMath &&
10987 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10988 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010989 Opcode = X86ISD::FMIN;
10990 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010991 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010992 // Converting this to a min would handle both negative zeros and NaNs
10993 // incorrectly, but we can swap the operands to fix both.
10994 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010995 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010996 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010997 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010998 Opcode = X86ISD::FMIN;
10999 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011000
Dan Gohman670e5392009-09-21 18:03:22 +000011001 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011002 // Converting this to a max would handle comparisons between positive
11003 // and negative zero incorrectly.
11004 if (!UnsafeFPMath &&
11005 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11006 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011007 Opcode = X86ISD::FMAX;
11008 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011009 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011010 // Converting this to a max would handle NaNs incorrectly, and swapping
11011 // the operands would cause it to handle comparisons between positive
11012 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011013 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011014 if (!UnsafeFPMath &&
11015 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11016 break;
11017 std::swap(LHS, RHS);
11018 }
Dan Gohman670e5392009-09-21 18:03:22 +000011019 Opcode = X86ISD::FMAX;
11020 break;
11021 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011022 // Converting this to a max would handle both negative zeros and NaNs
11023 // incorrectly, but we can swap the operands to fix both.
11024 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011025 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011026 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011027 case ISD::SETGE:
11028 Opcode = X86ISD::FMAX;
11029 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011030 }
Dan Gohman670e5392009-09-21 18:03:22 +000011031 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011032 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11033 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011034 switch (CC) {
11035 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011036 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011037 // Converting this to a min would handle comparisons between positive
11038 // and negative zero incorrectly, and swapping the operands would
11039 // cause it to handle NaNs incorrectly.
11040 if (!UnsafeFPMath &&
11041 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011042 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011043 break;
11044 std::swap(LHS, RHS);
11045 }
Dan Gohman670e5392009-09-21 18:03:22 +000011046 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011047 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011048 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011049 // Converting this to a min would handle NaNs incorrectly.
11050 if (!UnsafeFPMath &&
11051 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11052 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011053 Opcode = X86ISD::FMIN;
11054 break;
11055 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011056 // Converting this to a min would handle both negative zeros and NaNs
11057 // incorrectly, but we can swap the operands to fix both.
11058 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011059 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011060 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011061 case ISD::SETGE:
11062 Opcode = X86ISD::FMIN;
11063 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011064
Dan Gohman670e5392009-09-21 18:03:22 +000011065 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011066 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011067 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011068 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011069 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011070 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011071 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011072 // Converting this to a max would handle comparisons between positive
11073 // and negative zero incorrectly, and swapping the operands would
11074 // cause it to handle NaNs incorrectly.
11075 if (!UnsafeFPMath &&
11076 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011077 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011078 break;
11079 std::swap(LHS, RHS);
11080 }
Dan Gohman670e5392009-09-21 18:03:22 +000011081 Opcode = X86ISD::FMAX;
11082 break;
11083 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011084 // Converting this to a max would handle both negative zeros and NaNs
11085 // incorrectly, but we can swap the operands to fix both.
11086 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011087 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011088 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011089 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011090 Opcode = X86ISD::FMAX;
11091 break;
11092 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011093 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011094
Chris Lattner47b4ce82009-03-11 05:48:52 +000011095 if (Opcode)
11096 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011097 }
Eric Christopherfd179292009-08-27 18:07:15 +000011098
Chris Lattnerd1980a52009-03-12 06:52:53 +000011099 // If this is a select between two integer constants, try to do some
11100 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011101 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11102 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011103 // Don't do this for crazy integer types.
11104 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11105 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011106 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011107 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011108
Chris Lattnercee56e72009-03-13 05:53:31 +000011109 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011110 // Efficiently invertible.
11111 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11112 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11113 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11114 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011115 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011116 }
Eric Christopherfd179292009-08-27 18:07:15 +000011117
Chris Lattnerd1980a52009-03-12 06:52:53 +000011118 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011119 if (FalseC->getAPIntValue() == 0 &&
11120 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011121 if (NeedsCondInvert) // Invert the condition if needed.
11122 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11123 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011124
Chris Lattnerd1980a52009-03-12 06:52:53 +000011125 // Zero extend the condition if needed.
11126 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011127
Chris Lattnercee56e72009-03-13 05:53:31 +000011128 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011129 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011130 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011131 }
Eric Christopherfd179292009-08-27 18:07:15 +000011132
Chris Lattner97a29a52009-03-13 05:22:11 +000011133 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011134 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011135 if (NeedsCondInvert) // Invert the condition if needed.
11136 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11137 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011138
Chris Lattner97a29a52009-03-13 05:22:11 +000011139 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011140 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11141 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011142 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011143 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011144 }
Eric Christopherfd179292009-08-27 18:07:15 +000011145
Chris Lattnercee56e72009-03-13 05:53:31 +000011146 // Optimize cases that will turn into an LEA instruction. This requires
11147 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011148 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011149 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011150 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011151
Chris Lattnercee56e72009-03-13 05:53:31 +000011152 bool isFastMultiplier = false;
11153 if (Diff < 10) {
11154 switch ((unsigned char)Diff) {
11155 default: break;
11156 case 1: // result = add base, cond
11157 case 2: // result = lea base( , cond*2)
11158 case 3: // result = lea base(cond, cond*2)
11159 case 4: // result = lea base( , cond*4)
11160 case 5: // result = lea base(cond, cond*4)
11161 case 8: // result = lea base( , cond*8)
11162 case 9: // result = lea base(cond, cond*8)
11163 isFastMultiplier = true;
11164 break;
11165 }
11166 }
Eric Christopherfd179292009-08-27 18:07:15 +000011167
Chris Lattnercee56e72009-03-13 05:53:31 +000011168 if (isFastMultiplier) {
11169 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11170 if (NeedsCondInvert) // Invert the condition if needed.
11171 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11172 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011173
Chris Lattnercee56e72009-03-13 05:53:31 +000011174 // Zero extend the condition if needed.
11175 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11176 Cond);
11177 // Scale the condition by the difference.
11178 if (Diff != 1)
11179 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11180 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011181
Chris Lattnercee56e72009-03-13 05:53:31 +000011182 // Add the base if non-zero.
11183 if (FalseC->getAPIntValue() != 0)
11184 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11185 SDValue(FalseC, 0));
11186 return Cond;
11187 }
Eric Christopherfd179292009-08-27 18:07:15 +000011188 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011189 }
11190 }
Eric Christopherfd179292009-08-27 18:07:15 +000011191
Dan Gohman475871a2008-07-27 21:46:04 +000011192 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011193}
11194
Chris Lattnerd1980a52009-03-12 06:52:53 +000011195/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11196static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11197 TargetLowering::DAGCombinerInfo &DCI) {
11198 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011199
Chris Lattnerd1980a52009-03-12 06:52:53 +000011200 // If the flag operand isn't dead, don't touch this CMOV.
11201 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11202 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011203
Chris Lattnerd1980a52009-03-12 06:52:53 +000011204 // If this is a select between two integer constants, try to do some
11205 // optimizations. Note that the operands are ordered the opposite of SELECT
11206 // operands.
11207 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11208 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11209 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11210 // larger than FalseC (the false value).
11211 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011212
Chris Lattnerd1980a52009-03-12 06:52:53 +000011213 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11214 CC = X86::GetOppositeBranchCondition(CC);
11215 std::swap(TrueC, FalseC);
11216 }
Eric Christopherfd179292009-08-27 18:07:15 +000011217
Chris Lattnerd1980a52009-03-12 06:52:53 +000011218 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011219 // This is efficient for any integer data type (including i8/i16) and
11220 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011221 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11222 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011223 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11224 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011225
Chris Lattnerd1980a52009-03-12 06:52:53 +000011226 // Zero extend the condition if needed.
11227 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011228
Chris Lattnerd1980a52009-03-12 06:52:53 +000011229 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11230 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011231 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011232 if (N->getNumValues() == 2) // Dead flag value?
11233 return DCI.CombineTo(N, Cond, SDValue());
11234 return Cond;
11235 }
Eric Christopherfd179292009-08-27 18:07:15 +000011236
Chris Lattnercee56e72009-03-13 05:53:31 +000011237 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11238 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011239 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11240 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011241 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11242 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011243
Chris Lattner97a29a52009-03-13 05:22:11 +000011244 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011245 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11246 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011247 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11248 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011249
Chris Lattner97a29a52009-03-13 05:22:11 +000011250 if (N->getNumValues() == 2) // Dead flag value?
11251 return DCI.CombineTo(N, Cond, SDValue());
11252 return Cond;
11253 }
Eric Christopherfd179292009-08-27 18:07:15 +000011254
Chris Lattnercee56e72009-03-13 05:53:31 +000011255 // Optimize cases that will turn into an LEA instruction. This requires
11256 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011257 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011258 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011259 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011260
Chris Lattnercee56e72009-03-13 05:53:31 +000011261 bool isFastMultiplier = false;
11262 if (Diff < 10) {
11263 switch ((unsigned char)Diff) {
11264 default: break;
11265 case 1: // result = add base, cond
11266 case 2: // result = lea base( , cond*2)
11267 case 3: // result = lea base(cond, cond*2)
11268 case 4: // result = lea base( , cond*4)
11269 case 5: // result = lea base(cond, cond*4)
11270 case 8: // result = lea base( , cond*8)
11271 case 9: // result = lea base(cond, cond*8)
11272 isFastMultiplier = true;
11273 break;
11274 }
11275 }
Eric Christopherfd179292009-08-27 18:07:15 +000011276
Chris Lattnercee56e72009-03-13 05:53:31 +000011277 if (isFastMultiplier) {
11278 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11279 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011280 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11281 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011282 // Zero extend the condition if needed.
11283 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11284 Cond);
11285 // Scale the condition by the difference.
11286 if (Diff != 1)
11287 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11288 DAG.getConstant(Diff, Cond.getValueType()));
11289
11290 // Add the base if non-zero.
11291 if (FalseC->getAPIntValue() != 0)
11292 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11293 SDValue(FalseC, 0));
11294 if (N->getNumValues() == 2) // Dead flag value?
11295 return DCI.CombineTo(N, Cond, SDValue());
11296 return Cond;
11297 }
Eric Christopherfd179292009-08-27 18:07:15 +000011298 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011299 }
11300 }
11301 return SDValue();
11302}
11303
11304
Evan Cheng0b0cd912009-03-28 05:57:29 +000011305/// PerformMulCombine - Optimize a single multiply with constant into two
11306/// in order to implement it with two cheaper instructions, e.g.
11307/// LEA + SHL, LEA + LEA.
11308static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11309 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011310 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11311 return SDValue();
11312
Owen Andersone50ed302009-08-10 22:56:29 +000011313 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011314 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011315 return SDValue();
11316
11317 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11318 if (!C)
11319 return SDValue();
11320 uint64_t MulAmt = C->getZExtValue();
11321 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11322 return SDValue();
11323
11324 uint64_t MulAmt1 = 0;
11325 uint64_t MulAmt2 = 0;
11326 if ((MulAmt % 9) == 0) {
11327 MulAmt1 = 9;
11328 MulAmt2 = MulAmt / 9;
11329 } else if ((MulAmt % 5) == 0) {
11330 MulAmt1 = 5;
11331 MulAmt2 = MulAmt / 5;
11332 } else if ((MulAmt % 3) == 0) {
11333 MulAmt1 = 3;
11334 MulAmt2 = MulAmt / 3;
11335 }
11336 if (MulAmt2 &&
11337 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11338 DebugLoc DL = N->getDebugLoc();
11339
11340 if (isPowerOf2_64(MulAmt2) &&
11341 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11342 // If second multiplifer is pow2, issue it first. We want the multiply by
11343 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11344 // is an add.
11345 std::swap(MulAmt1, MulAmt2);
11346
11347 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011348 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011349 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011350 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011351 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011352 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011353 DAG.getConstant(MulAmt1, VT));
11354
Eric Christopherfd179292009-08-27 18:07:15 +000011355 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011356 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011357 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011358 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011359 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011360 DAG.getConstant(MulAmt2, VT));
11361
11362 // Do not add new nodes to DAG combiner worklist.
11363 DCI.CombineTo(N, NewMul, false);
11364 }
11365 return SDValue();
11366}
11367
Evan Chengad9c0a32009-12-15 00:53:42 +000011368static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11369 SDValue N0 = N->getOperand(0);
11370 SDValue N1 = N->getOperand(1);
11371 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11372 EVT VT = N0.getValueType();
11373
11374 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11375 // since the result of setcc_c is all zero's or all ones.
11376 if (N1C && N0.getOpcode() == ISD::AND &&
11377 N0.getOperand(1).getOpcode() == ISD::Constant) {
11378 SDValue N00 = N0.getOperand(0);
11379 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11380 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11381 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11382 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11383 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11384 APInt ShAmt = N1C->getAPIntValue();
11385 Mask = Mask.shl(ShAmt);
11386 if (Mask != 0)
11387 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11388 N00, DAG.getConstant(Mask, VT));
11389 }
11390 }
11391
11392 return SDValue();
11393}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011394
Nate Begeman740ab032009-01-26 00:52:55 +000011395/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11396/// when possible.
11397static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11398 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011399 EVT VT = N->getValueType(0);
11400 if (!VT.isVector() && VT.isInteger() &&
11401 N->getOpcode() == ISD::SHL)
11402 return PerformSHLCombine(N, DAG);
11403
Nate Begeman740ab032009-01-26 00:52:55 +000011404 // On X86 with SSE2 support, we can transform this to a vector shift if
11405 // all elements are shifted by the same amount. We can't do this in legalize
11406 // because the a constant vector is typically transformed to a constant pool
11407 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011408 if (!Subtarget->hasSSE2())
11409 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011410
Owen Anderson825b72b2009-08-11 20:47:22 +000011411 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011412 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011413
Mon P Wang3becd092009-01-28 08:12:05 +000011414 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011415 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011416 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011417 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011418 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11419 unsigned NumElts = VT.getVectorNumElements();
11420 unsigned i = 0;
11421 for (; i != NumElts; ++i) {
11422 SDValue Arg = ShAmtOp.getOperand(i);
11423 if (Arg.getOpcode() == ISD::UNDEF) continue;
11424 BaseShAmt = Arg;
11425 break;
11426 }
11427 for (; i != NumElts; ++i) {
11428 SDValue Arg = ShAmtOp.getOperand(i);
11429 if (Arg.getOpcode() == ISD::UNDEF) continue;
11430 if (Arg != BaseShAmt) {
11431 return SDValue();
11432 }
11433 }
11434 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011435 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011436 SDValue InVec = ShAmtOp.getOperand(0);
11437 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11438 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11439 unsigned i = 0;
11440 for (; i != NumElts; ++i) {
11441 SDValue Arg = InVec.getOperand(i);
11442 if (Arg.getOpcode() == ISD::UNDEF) continue;
11443 BaseShAmt = Arg;
11444 break;
11445 }
11446 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011448 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011449 if (C->getZExtValue() == SplatIdx)
11450 BaseShAmt = InVec.getOperand(1);
11451 }
11452 }
11453 if (BaseShAmt.getNode() == 0)
11454 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11455 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011456 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011457 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011458
Mon P Wangefa42202009-09-03 19:56:25 +000011459 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011460 if (EltVT.bitsGT(MVT::i32))
11461 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11462 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011463 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011464
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011465 // The shift amount is identical so we can do a vector shift.
11466 SDValue ValOp = N->getOperand(0);
11467 switch (N->getOpcode()) {
11468 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011469 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011470 break;
11471 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011472 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011473 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011474 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011475 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011476 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011477 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011478 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011479 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011481 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011482 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011483 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011484 break;
11485 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011486 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011487 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011488 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011489 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011490 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011491 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011492 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011493 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011494 break;
11495 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011497 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011499 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011500 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011501 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011502 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011503 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011504 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011505 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011506 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011507 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011508 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011509 }
11510 return SDValue();
11511}
11512
Nate Begemanb65c1752010-12-17 22:55:37 +000011513
11514static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11515 TargetLowering::DAGCombinerInfo &DCI,
11516 const X86Subtarget *Subtarget) {
11517 if (DCI.isBeforeLegalizeOps())
11518 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011519
Nate Begemanb65c1752010-12-17 22:55:37 +000011520 // Want to form PANDN nodes, in the hopes of then easily combining them with
11521 // OR and AND nodes to form PBLEND/PSIGN.
11522 EVT VT = N->getValueType(0);
11523 if (VT != MVT::v2i64)
11524 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011525
Nate Begemanb65c1752010-12-17 22:55:37 +000011526 SDValue N0 = N->getOperand(0);
11527 SDValue N1 = N->getOperand(1);
11528 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011529
Nate Begemanb65c1752010-12-17 22:55:37 +000011530 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011531 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011532 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11533 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11534
11535 // Check RHS for vnot
11536 if (N1.getOpcode() == ISD::XOR &&
11537 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11538 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011539
Nate Begemanb65c1752010-12-17 22:55:37 +000011540 return SDValue();
11541}
11542
Evan Cheng760d1942010-01-04 21:22:48 +000011543static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011544 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011545 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011546 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011547 return SDValue();
11548
Evan Cheng760d1942010-01-04 21:22:48 +000011549 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011550 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011551 return SDValue();
11552
Evan Cheng760d1942010-01-04 21:22:48 +000011553 SDValue N0 = N->getOperand(0);
11554 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011555
Nate Begemanb65c1752010-12-17 22:55:37 +000011556 // look for psign/blend
11557 if (Subtarget->hasSSSE3()) {
11558 if (VT == MVT::v2i64) {
11559 // Canonicalize pandn to RHS
11560 if (N0.getOpcode() == X86ISD::PANDN)
11561 std::swap(N0, N1);
11562 // or (and (m, x), (pandn m, y))
11563 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11564 SDValue Mask = N1.getOperand(0);
11565 SDValue X = N1.getOperand(1);
11566 SDValue Y;
11567 if (N0.getOperand(0) == Mask)
11568 Y = N0.getOperand(1);
11569 if (N0.getOperand(1) == Mask)
11570 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011571
Nate Begemanb65c1752010-12-17 22:55:37 +000011572 // Check to see if the mask appeared in both the AND and PANDN and
11573 if (!Y.getNode())
11574 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011575
Nate Begemanb65c1752010-12-17 22:55:37 +000011576 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11577 if (Mask.getOpcode() != ISD::BITCAST ||
11578 X.getOpcode() != ISD::BITCAST ||
11579 Y.getOpcode() != ISD::BITCAST)
11580 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011581
Nate Begemanb65c1752010-12-17 22:55:37 +000011582 // Look through mask bitcast.
11583 Mask = Mask.getOperand(0);
11584 EVT MaskVT = Mask.getValueType();
11585
11586 // Validate that the Mask operand is a vector sra node. The sra node
11587 // will be an intrinsic.
11588 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11589 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011590
Nate Begemanb65c1752010-12-17 22:55:37 +000011591 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11592 // there is no psrai.b
11593 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11594 case Intrinsic::x86_sse2_psrai_w:
11595 case Intrinsic::x86_sse2_psrai_d:
11596 break;
11597 default: return SDValue();
11598 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011599
Nate Begemanb65c1752010-12-17 22:55:37 +000011600 // Check that the SRA is all signbits.
11601 SDValue SraC = Mask.getOperand(2);
11602 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11603 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11604 if ((SraAmt + 1) != EltBits)
11605 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011606
Nate Begemanb65c1752010-12-17 22:55:37 +000011607 DebugLoc DL = N->getDebugLoc();
11608
11609 // Now we know we at least have a plendvb with the mask val. See if
11610 // we can form a psignb/w/d.
11611 // psign = x.type == y.type == mask.type && y = sub(0, x);
11612 X = X.getOperand(0);
11613 Y = Y.getOperand(0);
11614 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11615 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11616 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11617 unsigned Opc = 0;
11618 switch (EltBits) {
11619 case 8: Opc = X86ISD::PSIGNB; break;
11620 case 16: Opc = X86ISD::PSIGNW; break;
11621 case 32: Opc = X86ISD::PSIGND; break;
11622 default: break;
11623 }
11624 if (Opc) {
11625 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11626 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11627 }
11628 }
11629 // PBLENDVB only available on SSE 4.1
11630 if (!Subtarget->hasSSE41())
11631 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011632
Nate Begemanb65c1752010-12-17 22:55:37 +000011633 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11634 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11635 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011636 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011637 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11638 }
11639 }
11640 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011641
Nate Begemanb65c1752010-12-17 22:55:37 +000011642 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011643 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11644 std::swap(N0, N1);
11645 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11646 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011647 if (!N0.hasOneUse() || !N1.hasOneUse())
11648 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011649
11650 SDValue ShAmt0 = N0.getOperand(1);
11651 if (ShAmt0.getValueType() != MVT::i8)
11652 return SDValue();
11653 SDValue ShAmt1 = N1.getOperand(1);
11654 if (ShAmt1.getValueType() != MVT::i8)
11655 return SDValue();
11656 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11657 ShAmt0 = ShAmt0.getOperand(0);
11658 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11659 ShAmt1 = ShAmt1.getOperand(0);
11660
11661 DebugLoc DL = N->getDebugLoc();
11662 unsigned Opc = X86ISD::SHLD;
11663 SDValue Op0 = N0.getOperand(0);
11664 SDValue Op1 = N1.getOperand(0);
11665 if (ShAmt0.getOpcode() == ISD::SUB) {
11666 Opc = X86ISD::SHRD;
11667 std::swap(Op0, Op1);
11668 std::swap(ShAmt0, ShAmt1);
11669 }
11670
Evan Cheng8b1190a2010-04-28 01:18:01 +000011671 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011672 if (ShAmt1.getOpcode() == ISD::SUB) {
11673 SDValue Sum = ShAmt1.getOperand(0);
11674 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011675 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11676 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11677 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11678 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011679 return DAG.getNode(Opc, DL, VT,
11680 Op0, Op1,
11681 DAG.getNode(ISD::TRUNCATE, DL,
11682 MVT::i8, ShAmt0));
11683 }
11684 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11685 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11686 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011687 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011688 return DAG.getNode(Opc, DL, VT,
11689 N0.getOperand(0), N1.getOperand(0),
11690 DAG.getNode(ISD::TRUNCATE, DL,
11691 MVT::i8, ShAmt0));
11692 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011693
Evan Cheng760d1942010-01-04 21:22:48 +000011694 return SDValue();
11695}
11696
Chris Lattner149a4e52008-02-22 02:09:43 +000011697/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011698static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011699 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011700 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11701 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011702 // A preferable solution to the general problem is to figure out the right
11703 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011704
11705 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011706 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011707 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011708 if (VT.getSizeInBits() != 64)
11709 return SDValue();
11710
Devang Patel578efa92009-06-05 21:57:13 +000011711 const Function *F = DAG.getMachineFunction().getFunction();
11712 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011713 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011714 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011715 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011716 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011717 isa<LoadSDNode>(St->getValue()) &&
11718 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11719 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011720 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011721 LoadSDNode *Ld = 0;
11722 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011723 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011724 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011725 // Must be a store of a load. We currently handle two cases: the load
11726 // is a direct child, and it's under an intervening TokenFactor. It is
11727 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011728 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011729 Ld = cast<LoadSDNode>(St->getChain());
11730 else if (St->getValue().hasOneUse() &&
11731 ChainVal->getOpcode() == ISD::TokenFactor) {
11732 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011733 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011734 TokenFactorIndex = i;
11735 Ld = cast<LoadSDNode>(St->getValue());
11736 } else
11737 Ops.push_back(ChainVal->getOperand(i));
11738 }
11739 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011740
Evan Cheng536e6672009-03-12 05:59:15 +000011741 if (!Ld || !ISD::isNormalLoad(Ld))
11742 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011743
Evan Cheng536e6672009-03-12 05:59:15 +000011744 // If this is not the MMX case, i.e. we are just turning i64 load/store
11745 // into f64 load/store, avoid the transformation if there are multiple
11746 // uses of the loaded value.
11747 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11748 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011749
Evan Cheng536e6672009-03-12 05:59:15 +000011750 DebugLoc LdDL = Ld->getDebugLoc();
11751 DebugLoc StDL = N->getDebugLoc();
11752 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11753 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11754 // pair instead.
11755 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011756 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011757 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11758 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011759 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011760 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011761 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011762 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011763 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011764 Ops.size());
11765 }
Evan Cheng536e6672009-03-12 05:59:15 +000011766 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011767 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011768 St->isVolatile(), St->isNonTemporal(),
11769 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011770 }
Evan Cheng536e6672009-03-12 05:59:15 +000011771
11772 // Otherwise, lower to two pairs of 32-bit loads / stores.
11773 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011774 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11775 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011776
Owen Anderson825b72b2009-08-11 20:47:22 +000011777 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011778 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011779 Ld->isVolatile(), Ld->isNonTemporal(),
11780 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011781 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011782 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011783 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011784 MinAlign(Ld->getAlignment(), 4));
11785
11786 SDValue NewChain = LoLd.getValue(1);
11787 if (TokenFactorIndex != -1) {
11788 Ops.push_back(LoLd);
11789 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011790 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011791 Ops.size());
11792 }
11793
11794 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011795 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11796 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011797
11798 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011799 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011800 St->isVolatile(), St->isNonTemporal(),
11801 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011802 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011803 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011804 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011805 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011806 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011807 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011808 }
Dan Gohman475871a2008-07-27 21:46:04 +000011809 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011810}
11811
Chris Lattner6cf73262008-01-25 06:14:17 +000011812/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11813/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011814static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011815 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11816 // F[X]OR(0.0, x) -> x
11817 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011818 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11819 if (C->getValueAPF().isPosZero())
11820 return N->getOperand(1);
11821 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11822 if (C->getValueAPF().isPosZero())
11823 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011824 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011825}
11826
11827/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011828static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011829 // FAND(0.0, x) -> 0.0
11830 // FAND(x, 0.0) -> 0.0
11831 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11832 if (C->getValueAPF().isPosZero())
11833 return N->getOperand(0);
11834 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11835 if (C->getValueAPF().isPosZero())
11836 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011837 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011838}
11839
Dan Gohmane5af2d32009-01-29 01:59:02 +000011840static SDValue PerformBTCombine(SDNode *N,
11841 SelectionDAG &DAG,
11842 TargetLowering::DAGCombinerInfo &DCI) {
11843 // BT ignores high bits in the bit index operand.
11844 SDValue Op1 = N->getOperand(1);
11845 if (Op1.hasOneUse()) {
11846 unsigned BitWidth = Op1.getValueSizeInBits();
11847 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11848 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011849 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11850 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011851 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011852 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11853 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11854 DCI.CommitTargetLoweringOpt(TLO);
11855 }
11856 return SDValue();
11857}
Chris Lattner83e6c992006-10-04 06:57:07 +000011858
Eli Friedman7a5e5552009-06-07 06:52:44 +000011859static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11860 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011861 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011862 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011863 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011864 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011865 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011866 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011867 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011868 }
11869 return SDValue();
11870}
11871
Evan Cheng2e489c42009-12-16 00:53:11 +000011872static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11873 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11874 // (and (i32 x86isd::setcc_carry), 1)
11875 // This eliminates the zext. This transformation is necessary because
11876 // ISD::SETCC is always legalized to i8.
11877 DebugLoc dl = N->getDebugLoc();
11878 SDValue N0 = N->getOperand(0);
11879 EVT VT = N->getValueType(0);
11880 if (N0.getOpcode() == ISD::AND &&
11881 N0.hasOneUse() &&
11882 N0.getOperand(0).hasOneUse()) {
11883 SDValue N00 = N0.getOperand(0);
11884 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11885 return SDValue();
11886 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11887 if (!C || C->getZExtValue() != 1)
11888 return SDValue();
11889 return DAG.getNode(ISD::AND, dl, VT,
11890 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11891 N00.getOperand(0), N00.getOperand(1)),
11892 DAG.getConstant(1, VT));
11893 }
11894
11895 return SDValue();
11896}
11897
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011898// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11899static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11900 unsigned X86CC = N->getConstantOperandVal(0);
11901 SDValue EFLAG = N->getOperand(1);
11902 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011903
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011904 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11905 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11906 // cases.
11907 if (X86CC == X86::COND_B)
11908 return DAG.getNode(ISD::AND, DL, MVT::i8,
11909 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11910 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11911 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011912
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011913 return SDValue();
11914}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011915
Chris Lattner23a01992010-12-20 01:37:09 +000011916// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11917static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11918 X86TargetLowering::DAGCombinerInfo &DCI) {
11919 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11920 // the result is either zero or one (depending on the input carry bit).
11921 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11922 if (X86::isZeroNode(N->getOperand(0)) &&
11923 X86::isZeroNode(N->getOperand(1)) &&
11924 // We don't have a good way to replace an EFLAGS use, so only do this when
11925 // dead right now.
11926 SDValue(N, 1).use_empty()) {
11927 DebugLoc DL = N->getDebugLoc();
11928 EVT VT = N->getValueType(0);
11929 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11930 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11931 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11932 DAG.getConstant(X86::COND_B,MVT::i8),
11933 N->getOperand(2)),
11934 DAG.getConstant(1, VT));
11935 return DCI.CombineTo(N, Res1, CarryOut);
11936 }
11937
11938 return SDValue();
11939}
11940
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011941// fold (add Y, (sete X, 0)) -> adc 0, Y
11942// (add Y, (setne X, 0)) -> sbb -1, Y
11943// (sub (sete X, 0), Y) -> sbb 0, Y
11944// (sub (setne X, 0), Y) -> adc -1, Y
11945static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11946 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011947
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011948 // Look through ZExts.
11949 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11950 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11951 return SDValue();
11952
11953 SDValue SetCC = Ext.getOperand(0);
11954 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11955 return SDValue();
11956
11957 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11958 if (CC != X86::COND_E && CC != X86::COND_NE)
11959 return SDValue();
11960
11961 SDValue Cmp = SetCC.getOperand(1);
11962 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011963 !X86::isZeroNode(Cmp.getOperand(1)) ||
11964 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011965 return SDValue();
11966
11967 SDValue CmpOp0 = Cmp.getOperand(0);
11968 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11969 DAG.getConstant(1, CmpOp0.getValueType()));
11970
11971 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11972 if (CC == X86::COND_NE)
11973 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11974 DL, OtherVal.getValueType(), OtherVal,
11975 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11976 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11977 DL, OtherVal.getValueType(), OtherVal,
11978 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11979}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011980
Dan Gohman475871a2008-07-27 21:46:04 +000011981SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011982 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011983 SelectionDAG &DAG = DCI.DAG;
11984 switch (N->getOpcode()) {
11985 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011986 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011987 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011988 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011989 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011990 case ISD::ADD:
11991 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011992 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011993 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011994 case ISD::SHL:
11995 case ISD::SRA:
11996 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011997 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011998 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011999 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012000 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012001 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12002 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012003 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012004 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012005 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012006 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012007 case X86ISD::SHUFPS: // Handle all target specific shuffles
12008 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012009 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012010 case X86ISD::PUNPCKHBW:
12011 case X86ISD::PUNPCKHWD:
12012 case X86ISD::PUNPCKHDQ:
12013 case X86ISD::PUNPCKHQDQ:
12014 case X86ISD::UNPCKHPS:
12015 case X86ISD::UNPCKHPD:
12016 case X86ISD::PUNPCKLBW:
12017 case X86ISD::PUNPCKLWD:
12018 case X86ISD::PUNPCKLDQ:
12019 case X86ISD::PUNPCKLQDQ:
12020 case X86ISD::UNPCKLPS:
12021 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012022 case X86ISD::VUNPCKLPS:
12023 case X86ISD::VUNPCKLPD:
12024 case X86ISD::VUNPCKLPSY:
12025 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012026 case X86ISD::MOVHLPS:
12027 case X86ISD::MOVLHPS:
12028 case X86ISD::PSHUFD:
12029 case X86ISD::PSHUFHW:
12030 case X86ISD::PSHUFLW:
12031 case X86ISD::MOVSS:
12032 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012033 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012034 }
12035
Dan Gohman475871a2008-07-27 21:46:04 +000012036 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012037}
12038
Evan Chenge5b51ac2010-04-17 06:13:15 +000012039/// isTypeDesirableForOp - Return true if the target has native support for
12040/// the specified value type and it is 'desirable' to use the type for the
12041/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12042/// instruction encodings are longer and some i16 instructions are slow.
12043bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12044 if (!isTypeLegal(VT))
12045 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012046 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012047 return true;
12048
12049 switch (Opc) {
12050 default:
12051 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012052 case ISD::LOAD:
12053 case ISD::SIGN_EXTEND:
12054 case ISD::ZERO_EXTEND:
12055 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012056 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012057 case ISD::SRL:
12058 case ISD::SUB:
12059 case ISD::ADD:
12060 case ISD::MUL:
12061 case ISD::AND:
12062 case ISD::OR:
12063 case ISD::XOR:
12064 return false;
12065 }
12066}
12067
12068/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012069/// beneficial for dag combiner to promote the specified node. If true, it
12070/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012071bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012072 EVT VT = Op.getValueType();
12073 if (VT != MVT::i16)
12074 return false;
12075
Evan Cheng4c26e932010-04-19 19:29:22 +000012076 bool Promote = false;
12077 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012078 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012079 default: break;
12080 case ISD::LOAD: {
12081 LoadSDNode *LD = cast<LoadSDNode>(Op);
12082 // If the non-extending load has a single use and it's not live out, then it
12083 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012084 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12085 Op.hasOneUse()*/) {
12086 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12087 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12088 // The only case where we'd want to promote LOAD (rather then it being
12089 // promoted as an operand is when it's only use is liveout.
12090 if (UI->getOpcode() != ISD::CopyToReg)
12091 return false;
12092 }
12093 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012094 Promote = true;
12095 break;
12096 }
12097 case ISD::SIGN_EXTEND:
12098 case ISD::ZERO_EXTEND:
12099 case ISD::ANY_EXTEND:
12100 Promote = true;
12101 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012102 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012103 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012104 SDValue N0 = Op.getOperand(0);
12105 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012106 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012107 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012108 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012109 break;
12110 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012111 case ISD::ADD:
12112 case ISD::MUL:
12113 case ISD::AND:
12114 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012115 case ISD::XOR:
12116 Commute = true;
12117 // fallthrough
12118 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012119 SDValue N0 = Op.getOperand(0);
12120 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012121 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012122 return false;
12123 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012124 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012125 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012126 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012127 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012128 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012129 }
12130 }
12131
12132 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012133 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012134}
12135
Evan Cheng60c07e12006-07-05 22:17:51 +000012136//===----------------------------------------------------------------------===//
12137// X86 Inline Assembly Support
12138//===----------------------------------------------------------------------===//
12139
Chris Lattnerb8105652009-07-20 17:51:36 +000012140bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12141 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012142
12143 std::string AsmStr = IA->getAsmString();
12144
12145 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012146 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012147 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012148
12149 switch (AsmPieces.size()) {
12150 default: return false;
12151 case 1:
12152 AsmStr = AsmPieces[0];
12153 AsmPieces.clear();
12154 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12155
Evan Cheng55d42002011-01-08 01:24:27 +000012156 // FIXME: this should verify that we are targetting a 486 or better. If not,
12157 // we will turn this bswap into something that will be lowered to logical ops
12158 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12159 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012160 // bswap $0
12161 if (AsmPieces.size() == 2 &&
12162 (AsmPieces[0] == "bswap" ||
12163 AsmPieces[0] == "bswapq" ||
12164 AsmPieces[0] == "bswapl") &&
12165 (AsmPieces[1] == "$0" ||
12166 AsmPieces[1] == "${0:q}")) {
12167 // No need to check constraints, nothing other than the equivalent of
12168 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012169 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12170 if (!Ty || Ty->getBitWidth() % 16 != 0)
12171 return false;
12172 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012173 }
12174 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012175 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012176 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012177 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012178 AsmPieces[1] == "$$8," &&
12179 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012180 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12181 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012182 const std::string &ConstraintsStr = IA->getConstraintString();
12183 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012184 std::sort(AsmPieces.begin(), AsmPieces.end());
12185 if (AsmPieces.size() == 4 &&
12186 AsmPieces[0] == "~{cc}" &&
12187 AsmPieces[1] == "~{dirflag}" &&
12188 AsmPieces[2] == "~{flags}" &&
12189 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012190 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12191 if (!Ty || Ty->getBitWidth() % 16 != 0)
12192 return false;
12193 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012194 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012195 }
12196 break;
12197 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012198 if (CI->getType()->isIntegerTy(32) &&
12199 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12200 SmallVector<StringRef, 4> Words;
12201 SplitString(AsmPieces[0], Words, " \t,");
12202 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12203 Words[2] == "${0:w}") {
12204 Words.clear();
12205 SplitString(AsmPieces[1], Words, " \t,");
12206 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12207 Words[2] == "$0") {
12208 Words.clear();
12209 SplitString(AsmPieces[2], Words, " \t,");
12210 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12211 Words[2] == "${0:w}") {
12212 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012213 const std::string &ConstraintsStr = IA->getConstraintString();
12214 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012215 std::sort(AsmPieces.begin(), AsmPieces.end());
12216 if (AsmPieces.size() == 4 &&
12217 AsmPieces[0] == "~{cc}" &&
12218 AsmPieces[1] == "~{dirflag}" &&
12219 AsmPieces[2] == "~{flags}" &&
12220 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012221 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12222 if (!Ty || Ty->getBitWidth() % 16 != 0)
12223 return false;
12224 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012225 }
12226 }
12227 }
12228 }
12229 }
Evan Cheng55d42002011-01-08 01:24:27 +000012230
12231 if (CI->getType()->isIntegerTy(64)) {
12232 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12233 if (Constraints.size() >= 2 &&
12234 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12235 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12236 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12237 SmallVector<StringRef, 4> Words;
12238 SplitString(AsmPieces[0], Words, " \t");
12239 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012240 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012241 SplitString(AsmPieces[1], Words, " \t");
12242 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12243 Words.clear();
12244 SplitString(AsmPieces[2], Words, " \t,");
12245 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12246 Words[2] == "%edx") {
12247 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12248 if (!Ty || Ty->getBitWidth() % 16 != 0)
12249 return false;
12250 return IntrinsicLowering::LowerToByteSwap(CI);
12251 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012252 }
12253 }
12254 }
12255 }
12256 break;
12257 }
12258 return false;
12259}
12260
12261
12262
Chris Lattnerf4dff842006-07-11 02:54:03 +000012263/// getConstraintType - Given a constraint letter, return the type of
12264/// constraint it is for this target.
12265X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012266X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12267 if (Constraint.size() == 1) {
12268 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012269 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012270 case 'q':
12271 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012272 case 'f':
12273 case 't':
12274 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012275 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012276 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012277 case 'Y':
12278 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012279 case 'a':
12280 case 'b':
12281 case 'c':
12282 case 'd':
12283 case 'S':
12284 case 'D':
12285 case 'A':
12286 return C_Register;
12287 case 'I':
12288 case 'J':
12289 case 'K':
12290 case 'L':
12291 case 'M':
12292 case 'N':
12293 case 'G':
12294 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012295 case 'e':
12296 case 'Z':
12297 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012298 default:
12299 break;
12300 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012301 }
Chris Lattner4234f572007-03-25 02:14:49 +000012302 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012303}
12304
John Thompson44ab89e2010-10-29 17:29:13 +000012305/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012306/// This object must already have been set up with the operand type
12307/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012308TargetLowering::ConstraintWeight
12309 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012310 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012311 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012312 Value *CallOperandVal = info.CallOperandVal;
12313 // If we don't have a value, we can't do a match,
12314 // but allow it at the lowest weight.
12315 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012316 return CW_Default;
12317 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012318 // Look at the constraint type.
12319 switch (*constraint) {
12320 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012321 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12322 case 'R':
12323 case 'q':
12324 case 'Q':
12325 case 'a':
12326 case 'b':
12327 case 'c':
12328 case 'd':
12329 case 'S':
12330 case 'D':
12331 case 'A':
12332 if (CallOperandVal->getType()->isIntegerTy())
12333 weight = CW_SpecificReg;
12334 break;
12335 case 'f':
12336 case 't':
12337 case 'u':
12338 if (type->isFloatingPointTy())
12339 weight = CW_SpecificReg;
12340 break;
12341 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012342 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012343 weight = CW_SpecificReg;
12344 break;
12345 case 'x':
12346 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012347 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012348 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012349 break;
12350 case 'I':
12351 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12352 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012353 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012354 }
12355 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012356 case 'J':
12357 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12358 if (C->getZExtValue() <= 63)
12359 weight = CW_Constant;
12360 }
12361 break;
12362 case 'K':
12363 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12364 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12365 weight = CW_Constant;
12366 }
12367 break;
12368 case 'L':
12369 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12370 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12371 weight = CW_Constant;
12372 }
12373 break;
12374 case 'M':
12375 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12376 if (C->getZExtValue() <= 3)
12377 weight = CW_Constant;
12378 }
12379 break;
12380 case 'N':
12381 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12382 if (C->getZExtValue() <= 0xff)
12383 weight = CW_Constant;
12384 }
12385 break;
12386 case 'G':
12387 case 'C':
12388 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12389 weight = CW_Constant;
12390 }
12391 break;
12392 case 'e':
12393 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12394 if ((C->getSExtValue() >= -0x80000000LL) &&
12395 (C->getSExtValue() <= 0x7fffffffLL))
12396 weight = CW_Constant;
12397 }
12398 break;
12399 case 'Z':
12400 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12401 if (C->getZExtValue() <= 0xffffffff)
12402 weight = CW_Constant;
12403 }
12404 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012405 }
12406 return weight;
12407}
12408
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012409/// LowerXConstraint - try to replace an X constraint, which matches anything,
12410/// with another that has more specific requirements based on the type of the
12411/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012412const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012413LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012414 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12415 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012416 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012417 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012418 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012419 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012420 return "x";
12421 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012422
Chris Lattner5e764232008-04-26 23:02:14 +000012423 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012424}
12425
Chris Lattner48884cd2007-08-25 00:47:38 +000012426/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12427/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012428void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012429 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012430 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012431 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012432 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012433
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012434 switch (Constraint) {
12435 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012436 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012437 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012438 if (C->getZExtValue() <= 31) {
12439 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012440 break;
12441 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012442 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012443 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012444 case 'J':
12445 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012446 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012447 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12448 break;
12449 }
12450 }
12451 return;
12452 case 'K':
12453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012454 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012455 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12456 break;
12457 }
12458 }
12459 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012460 case 'N':
12461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012462 if (C->getZExtValue() <= 255) {
12463 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012464 break;
12465 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012466 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012467 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012468 case 'e': {
12469 // 32-bit signed value
12470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012471 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12472 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012473 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012474 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012475 break;
12476 }
12477 // FIXME gcc accepts some relocatable values here too, but only in certain
12478 // memory models; it's complicated.
12479 }
12480 return;
12481 }
12482 case 'Z': {
12483 // 32-bit unsigned value
12484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012485 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12486 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012487 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12488 break;
12489 }
12490 }
12491 // FIXME gcc accepts some relocatable values here too, but only in certain
12492 // memory models; it's complicated.
12493 return;
12494 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012495 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012496 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012497 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012498 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012499 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012500 break;
12501 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012502
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012503 // In any sort of PIC mode addresses need to be computed at runtime by
12504 // adding in a register or some sort of table lookup. These can't
12505 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012506 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012507 return;
12508
Chris Lattnerdc43a882007-05-03 16:52:29 +000012509 // If we are in non-pic codegen mode, we allow the address of a global (with
12510 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012511 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012512 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012513
Chris Lattner49921962009-05-08 18:23:14 +000012514 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12515 while (1) {
12516 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12517 Offset += GA->getOffset();
12518 break;
12519 } else if (Op.getOpcode() == ISD::ADD) {
12520 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12521 Offset += C->getZExtValue();
12522 Op = Op.getOperand(0);
12523 continue;
12524 }
12525 } else if (Op.getOpcode() == ISD::SUB) {
12526 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12527 Offset += -C->getZExtValue();
12528 Op = Op.getOperand(0);
12529 continue;
12530 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012531 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012532
Chris Lattner49921962009-05-08 18:23:14 +000012533 // Otherwise, this isn't something we can handle, reject it.
12534 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012535 }
Eric Christopherfd179292009-08-27 18:07:15 +000012536
Dan Gohman46510a72010-04-15 01:51:59 +000012537 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012538 // If we require an extra load to get this address, as in PIC mode, we
12539 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012540 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12541 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012542 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012543
Devang Patel0d881da2010-07-06 22:08:15 +000012544 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12545 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012546 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012547 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012548 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012549
Gabor Greifba36cb52008-08-28 21:40:38 +000012550 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012551 Ops.push_back(Result);
12552 return;
12553 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012554 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012555}
12556
Chris Lattner259e97c2006-01-31 19:43:35 +000012557std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012558getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012559 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012560 if (Constraint.size() == 1) {
12561 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012562 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012563 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012564 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12565 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012566 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012567 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12568 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12569 X86::R10D,X86::R11D,X86::R12D,
12570 X86::R13D,X86::R14D,X86::R15D,
12571 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012572 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012573 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12574 X86::SI, X86::DI, X86::R8W,X86::R9W,
12575 X86::R10W,X86::R11W,X86::R12W,
12576 X86::R13W,X86::R14W,X86::R15W,
12577 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012578 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012579 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12580 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12581 X86::R10B,X86::R11B,X86::R12B,
12582 X86::R13B,X86::R14B,X86::R15B,
12583 X86::BPL, X86::SPL, 0);
12584
Owen Anderson825b72b2009-08-11 20:47:22 +000012585 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012586 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12587 X86::RSI, X86::RDI, X86::R8, X86::R9,
12588 X86::R10, X86::R11, X86::R12,
12589 X86::R13, X86::R14, X86::R15,
12590 X86::RBP, X86::RSP, 0);
12591
12592 break;
12593 }
Eric Christopherfd179292009-08-27 18:07:15 +000012594 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012595 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012596 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012597 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012598 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012599 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012600 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012601 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012602 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012603 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12604 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012605 }
12606 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012607
Chris Lattner1efa40f2006-02-22 00:56:39 +000012608 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012609}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012610
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012611std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012612X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012613 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012614 // First, see if this is a constraint that directly corresponds to an LLVM
12615 // register class.
12616 if (Constraint.size() == 1) {
12617 // GCC Constraint Letters
12618 switch (Constraint[0]) {
12619 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012620 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012621 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012622 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012623 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012624 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012625 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012626 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012627 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012628 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012629 case 'R': // LEGACY_REGS
12630 if (VT == MVT::i8)
12631 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12632 if (VT == MVT::i16)
12633 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12634 if (VT == MVT::i32 || !Subtarget->is64Bit())
12635 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12636 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012637 case 'f': // FP Stack registers.
12638 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12639 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012640 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012641 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012642 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012643 return std::make_pair(0U, X86::RFP64RegisterClass);
12644 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012645 case 'y': // MMX_REGS if MMX allowed.
12646 if (!Subtarget->hasMMX()) break;
12647 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012648 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012649 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012650 // FALL THROUGH.
12651 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012652 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012653
Owen Anderson825b72b2009-08-11 20:47:22 +000012654 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012655 default: break;
12656 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012657 case MVT::f32:
12658 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012659 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012660 case MVT::f64:
12661 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012662 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012663 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012664 case MVT::v16i8:
12665 case MVT::v8i16:
12666 case MVT::v4i32:
12667 case MVT::v2i64:
12668 case MVT::v4f32:
12669 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012670 return std::make_pair(0U, X86::VR128RegisterClass);
12671 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012672 break;
12673 }
12674 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012675
Chris Lattnerf76d1802006-07-31 23:26:50 +000012676 // Use the default implementation in TargetLowering to convert the register
12677 // constraint into a member of a register class.
12678 std::pair<unsigned, const TargetRegisterClass*> Res;
12679 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012680
12681 // Not found as a standard register?
12682 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012683 // Map st(0) -> st(7) -> ST0
12684 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12685 tolower(Constraint[1]) == 's' &&
12686 tolower(Constraint[2]) == 't' &&
12687 Constraint[3] == '(' &&
12688 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12689 Constraint[5] == ')' &&
12690 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012691
Chris Lattner56d77c72009-09-13 22:41:48 +000012692 Res.first = X86::ST0+Constraint[4]-'0';
12693 Res.second = X86::RFP80RegisterClass;
12694 return Res;
12695 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012696
Chris Lattner56d77c72009-09-13 22:41:48 +000012697 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012698 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012699 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012700 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012701 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012702 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012703
12704 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012705 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012706 Res.first = X86::EFLAGS;
12707 Res.second = X86::CCRRegisterClass;
12708 return Res;
12709 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012710
Dale Johannesen330169f2008-11-13 21:52:36 +000012711 // 'A' means EAX + EDX.
12712 if (Constraint == "A") {
12713 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012714 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012715 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012716 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012717 return Res;
12718 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012719
Chris Lattnerf76d1802006-07-31 23:26:50 +000012720 // Otherwise, check to see if this is a register class of the wrong value
12721 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12722 // turn into {ax},{dx}.
12723 if (Res.second->hasType(VT))
12724 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012725
Chris Lattnerf76d1802006-07-31 23:26:50 +000012726 // All of the single-register GCC register classes map their values onto
12727 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12728 // really want an 8-bit or 32-bit register, map to the appropriate register
12729 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012730 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012731 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012732 unsigned DestReg = 0;
12733 switch (Res.first) {
12734 default: break;
12735 case X86::AX: DestReg = X86::AL; break;
12736 case X86::DX: DestReg = X86::DL; break;
12737 case X86::CX: DestReg = X86::CL; break;
12738 case X86::BX: DestReg = X86::BL; break;
12739 }
12740 if (DestReg) {
12741 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012742 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012743 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012744 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012745 unsigned DestReg = 0;
12746 switch (Res.first) {
12747 default: break;
12748 case X86::AX: DestReg = X86::EAX; break;
12749 case X86::DX: DestReg = X86::EDX; break;
12750 case X86::CX: DestReg = X86::ECX; break;
12751 case X86::BX: DestReg = X86::EBX; break;
12752 case X86::SI: DestReg = X86::ESI; break;
12753 case X86::DI: DestReg = X86::EDI; break;
12754 case X86::BP: DestReg = X86::EBP; break;
12755 case X86::SP: DestReg = X86::ESP; break;
12756 }
12757 if (DestReg) {
12758 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012759 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012760 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012761 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012762 unsigned DestReg = 0;
12763 switch (Res.first) {
12764 default: break;
12765 case X86::AX: DestReg = X86::RAX; break;
12766 case X86::DX: DestReg = X86::RDX; break;
12767 case X86::CX: DestReg = X86::RCX; break;
12768 case X86::BX: DestReg = X86::RBX; break;
12769 case X86::SI: DestReg = X86::RSI; break;
12770 case X86::DI: DestReg = X86::RDI; break;
12771 case X86::BP: DestReg = X86::RBP; break;
12772 case X86::SP: DestReg = X86::RSP; break;
12773 }
12774 if (DestReg) {
12775 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012776 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012777 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012778 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012779 } else if (Res.second == X86::FR32RegisterClass ||
12780 Res.second == X86::FR64RegisterClass ||
12781 Res.second == X86::VR128RegisterClass) {
12782 // Handle references to XMM physical registers that got mapped into the
12783 // wrong class. This can happen with constraints like {xmm0} where the
12784 // target independent register mapper will just pick the first match it can
12785 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012786 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012787 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012788 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012789 Res.second = X86::FR64RegisterClass;
12790 else if (X86::VR128RegisterClass->hasType(VT))
12791 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012792 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012793
Chris Lattnerf76d1802006-07-31 23:26:50 +000012794 return Res;
12795}