blob: 22b9da59ad33176e48dfe19391e9ae76f11368ee [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);
Evan Cheng211ffa12010-05-19 20:19:50 +0000224 setSchedulingPreference(Sched::RegPressure);
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(),
Mon P Wang20adc9d2010-04-04 03:10:48 +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) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001747 if (!IsWin64 && (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));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001800 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1801 } else {
1802 // For X86-64, if there are vararg parameters that are passed via
1803 // registers, then we must store them to their spots on the stack so they
1804 // may be loaded by deferencing the result of va_next.
1805 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1806 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1807 FuncInfo->setRegSaveFrameIndex(
1808 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001809 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001810 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001811
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001813 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001814 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1815 getPointerTy());
1816 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001817 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001818 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1819 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001820 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001821 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001824 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001825 MachinePointerInfo::getFixedStack(
1826 FuncInfo->getRegSaveFrameIndex(), Offset),
1827 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001829 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001830 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001831
Dan Gohmanface41a2009-08-16 21:24:25 +00001832 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1833 // Now store the XMM (fp + vector) parameter registers.
1834 SmallVector<SDValue, 11> SaveXMMOps;
1835 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001836
Devang Patel68e6bee2011-02-21 23:21:26 +00001837 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001838 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1839 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001840
Dan Gohman1e93df62010-04-17 14:41:14 +00001841 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1842 FuncInfo->getRegSaveFrameIndex()));
1843 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1844 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001845
Dan Gohmanface41a2009-08-16 21:24:25 +00001846 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001847 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001848 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001849 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1850 SaveXMMOps.push_back(Val);
1851 }
1852 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1853 MVT::Other,
1854 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001855 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001856
1857 if (!MemOps.empty())
1858 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1859 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001864 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001865 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001866 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001867 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001868 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001869 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001870 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001871 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001872
Gordon Henriksen86737662008-01-05 16:56:59 +00001873 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001874 // RegSaveFrameIndex is X86-64 only.
1875 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001876 if (CallConv == CallingConv::X86_FastCall ||
1877 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001878 // fastcc functions can't have varargs.
1879 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 }
Evan Cheng25caf632006-05-23 21:06:34 +00001881
Dan Gohman98ca4f22009-08-05 01:29:28 +00001882 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001883}
1884
Dan Gohman475871a2008-07-27 21:46:04 +00001885SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001886X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1887 SDValue StackPtr, SDValue Arg,
1888 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001889 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001890 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001891 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001892 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001893 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001894 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001895 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001896
1897 return DAG.getStore(Chain, dl, Arg, PtrOff,
1898 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001899 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001900}
1901
Bill Wendling64e87322009-01-16 19:25:27 +00001902/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001903/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001904SDValue
1905X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001906 SDValue &OutRetAddr, SDValue Chain,
1907 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001908 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001909 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001910 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001911 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001912
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001913 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001914 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1915 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001916 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001917}
1918
1919/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1920/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001921static SDValue
1922EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001923 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001924 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001925 // Store the return address to the appropriate stack slot.
1926 if (!FPDiff) return Chain;
1927 // Calculate the new stack slot for the return address.
1928 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001929 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001930 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001932 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001933 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001934 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001935 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936 return Chain;
1937}
1938
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001940X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001941 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001942 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001944 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001945 const SmallVectorImpl<ISD::InputArg> &Ins,
1946 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001947 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 MachineFunction &MF = DAG.getMachineFunction();
1949 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001950 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001951 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001952 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953
Evan Cheng5f941932010-02-05 02:21:12 +00001954 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001955 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001956 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1957 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001958 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001959
1960 // Sibcalls are automatically detected tailcalls which do not require
1961 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001962 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001963 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001964
1965 if (isTailCall)
1966 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001967 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001968
Chris Lattner29689432010-03-11 00:22:57 +00001969 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1970 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971
Chris Lattner638402b2007-02-28 07:00:42 +00001972 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001973 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1975 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001976
1977 // Allocate shadow area for Win64
1978 if (IsWin64) {
1979 CCInfo.AllocateStack(32, 8);
1980 }
1981
Duncan Sands45907662010-10-31 13:21:44 +00001982 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Chris Lattner423c5f42007-02-28 05:31:48 +00001984 // Get a count of how many bytes are to be pushed on the stack.
1985 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001986 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001987 // This is a sibcall. The memory operands are available in caller's
1988 // own caller's stack.
1989 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001990 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001991 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001992
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001994 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001995 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001996 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1998 FPDiff = NumBytesCallerPushed - NumBytes;
1999
2000 // Set the delta of movement of the returnaddr stackslot.
2001 // But only set if delta is greater than previous delta.
2002 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2003 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2004 }
2005
Evan Chengf22f9b32010-02-06 03:28:46 +00002006 if (!IsSibcall)
2007 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002008
Dan Gohman475871a2008-07-27 21:46:04 +00002009 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002010 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002011 if (isTailCall && FPDiff)
2012 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2013 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002014
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2016 SmallVector<SDValue, 8> MemOpChains;
2017 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002018
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002019 // Walk the register/memloc assignments, inserting copies/loads. In the case
2020 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002021 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2022 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002023 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002024 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002025 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002026 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002027
Chris Lattner423c5f42007-02-28 05:31:48 +00002028 // Promote the value if needed.
2029 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002030 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002031 case CCValAssign::Full: break;
2032 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002033 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002034 break;
2035 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002036 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002037 break;
2038 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002039 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2040 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002041 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2043 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002044 } else
2045 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2046 break;
2047 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002048 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002049 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002050 case CCValAssign::Indirect: {
2051 // Store the argument.
2052 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002053 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002054 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002055 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002056 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002057 Arg = SpillSlot;
2058 break;
2059 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002061
Chris Lattner423c5f42007-02-28 05:31:48 +00002062 if (VA.isRegLoc()) {
2063 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002064 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002065 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2066 // shadow reg if callee is a varargs function.
2067 unsigned ShadowReg = 0;
2068 switch (VA.getLocReg()) {
2069 case X86::XMM0: ShadowReg = X86::RCX; break;
2070 case X86::XMM1: ShadowReg = X86::RDX; break;
2071 case X86::XMM2: ShadowReg = X86::R8; break;
2072 case X86::XMM3: ShadowReg = X86::R9; break;
2073 }
2074 if (ShadowReg)
2075 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2076 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002077 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002078 assert(VA.isMemLoc());
2079 if (StackPtr.getNode() == 0)
2080 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2081 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2082 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002083 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002085
Evan Cheng32fe1032006-05-25 00:59:30 +00002086 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002087 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002088 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002089
Evan Cheng347d5f72006-04-28 21:29:37 +00002090 // Build a sequence of copy-to-reg nodes chained together with token chain
2091 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002092 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002093 // Tail call byval lowering might overwrite argument registers so in case of
2094 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002096 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002097 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002098 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002099 InFlag = Chain.getValue(1);
2100 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002101
Chris Lattner88e1fd52009-07-09 04:24:46 +00002102 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002103 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2104 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002106 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2107 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002108 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002109 InFlag);
2110 InFlag = Chain.getValue(1);
2111 } else {
2112 // If we are tail calling and generating PIC/GOT style code load the
2113 // address of the callee into ECX. The value in ecx is used as target of
2114 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2115 // for tail calls on PIC/GOT architectures. Normally we would just put the
2116 // address of GOT into ebx and then call target@PLT. But for tail calls
2117 // ebx would be restored (since ebx is callee saved) before jumping to the
2118 // target@PLT.
2119
2120 // Note: The actual moving to ECX is done further down.
2121 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2122 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2123 !G->getGlobal()->hasProtectedVisibility())
2124 Callee = LowerGlobalAddress(Callee, DAG);
2125 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002126 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002127 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002128 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002129
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002130 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002131 // From AMD64 ABI document:
2132 // For calls that may call functions that use varargs or stdargs
2133 // (prototype-less calls or calls to functions containing ellipsis (...) in
2134 // the declaration) %al is used as hidden argument to specify the number
2135 // of SSE registers used. The contents of %al do not need to match exactly
2136 // the number of registers, but must be an ubound on the number of SSE
2137 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002138
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 // Count the number of XMM registers allocated.
2140 static const unsigned XMMArgRegs[] = {
2141 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2142 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2143 };
2144 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002145 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002146 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002147
Dale Johannesendd64c412009-02-04 00:33:20 +00002148 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002150 InFlag = Chain.getValue(1);
2151 }
2152
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002153
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002154 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002155 if (isTailCall) {
2156 // Force all the incoming stack arguments to be loaded from the stack
2157 // before any new outgoing arguments are stored to the stack, because the
2158 // outgoing stack slots may alias the incoming argument stack slots, and
2159 // the alias isn't otherwise explicit. This is slightly more conservative
2160 // than necessary, because it means that each store effectively depends
2161 // on every argument instead of just those arguments it would clobber.
2162 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2163
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SmallVector<SDValue, 8> MemOpChains2;
2165 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002166 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002167 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002168 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002169 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002170 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2171 CCValAssign &VA = ArgLocs[i];
2172 if (VA.isRegLoc())
2173 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002174 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002175 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002177 // Create frame index.
2178 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002179 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002180 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002181 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002182
Duncan Sands276dcbd2008-03-21 09:14:45 +00002183 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002184 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002185 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002186 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002187 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002188 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002189 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002190
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2192 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002193 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002195 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002196 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002198 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002199 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002200 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002201 }
2202 }
2203
2204 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002206 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002207
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002208 // Copy arguments to their registers.
2209 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002210 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002211 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002212 InFlag = Chain.getValue(1);
2213 }
Dan Gohman475871a2008-07-27 21:46:04 +00002214 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002215
Gordon Henriksen86737662008-01-05 16:56:59 +00002216 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002217 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002218 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002219 }
2220
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002221 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2222 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2223 // In the 64-bit large code model, we have to make all calls
2224 // through a register, since the call instruction's 32-bit
2225 // pc-relative offset may not be large enough to hold the whole
2226 // address.
2227 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002228 // If the callee is a GlobalAddress node (quite common, every direct call
2229 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2230 // it.
2231
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002232 // We should use extra load for direct calls to dllimported functions in
2233 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002234 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002235 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002236 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002237
Chris Lattner48a7d022009-07-09 05:02:21 +00002238 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2239 // external symbols most go through the PLT in PIC mode. If the symbol
2240 // has hidden or protected visibility, or if it is static or local, then
2241 // we don't need to use the PLT - we can directly call it.
2242 if (Subtarget->isTargetELF() &&
2243 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002244 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002245 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002246 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002247 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2248 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002249 // PC-relative references to external symbols should go through $stub,
2250 // unless we're building with the leopard linker or later, which
2251 // automatically synthesizes these stubs.
2252 OpFlags = X86II::MO_DARWIN_STUB;
2253 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002254
Devang Patel0d881da2010-07-06 22:08:15 +00002255 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002256 G->getOffset(), OpFlags);
2257 }
Bill Wendling056292f2008-09-16 21:48:12 +00002258 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002259 unsigned char OpFlags = 0;
2260
Evan Cheng1bf891a2010-12-01 22:59:46 +00002261 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2262 // external symbols should go through the PLT.
2263 if (Subtarget->isTargetELF() &&
2264 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2265 OpFlags = X86II::MO_PLT;
2266 } else if (Subtarget->isPICStyleStubAny() &&
2267 Subtarget->getDarwinVers() < 9) {
2268 // PC-relative references to external symbols should go through $stub,
2269 // unless we're building with the leopard linker or later, which
2270 // automatically synthesizes these stubs.
2271 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002272 }
Eric Christopherfd179292009-08-27 18:07:15 +00002273
Chris Lattner48a7d022009-07-09 05:02:21 +00002274 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2275 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002276 }
2277
Chris Lattnerd96d0722007-02-25 06:40:16 +00002278 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002279 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002280 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002281
Evan Chengf22f9b32010-02-06 03:28:46 +00002282 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002283 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2284 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002285 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002286 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002287
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002288 Ops.push_back(Chain);
2289 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002290
Dan Gohman98ca4f22009-08-05 01:29:28 +00002291 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002293
Gordon Henriksen86737662008-01-05 16:56:59 +00002294 // Add argument registers to the end of the list so that they are known live
2295 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002296 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2297 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2298 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002299
Evan Cheng586ccac2008-03-18 23:36:35 +00002300 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002301 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002302 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2303
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002304 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002305 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002306 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002307
Gabor Greifba36cb52008-08-28 21:40:38 +00002308 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002309 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002310
Dan Gohman98ca4f22009-08-05 01:29:28 +00002311 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002312 // We used to do:
2313 //// If this is the first return lowered for this function, add the regs
2314 //// to the liveout set for the function.
2315 // This isn't right, although it's probably harmless on x86; liveouts
2316 // should be computed from returns not tail calls. Consider a void
2317 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002318 return DAG.getNode(X86ISD::TC_RETURN, dl,
2319 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002320 }
2321
Dale Johannesenace16102009-02-03 19:33:06 +00002322 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002323 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002324
Chris Lattner2d297092006-05-23 18:50:38 +00002325 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002326 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002327 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002328 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002329 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002330 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002331 // pops the hidden struct pointer, so we have to push it back.
2332 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002333 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002334 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002335 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002336
Gordon Henriksenae636f82008-01-03 16:47:34 +00002337 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002338 if (!IsSibcall) {
2339 Chain = DAG.getCALLSEQ_END(Chain,
2340 DAG.getIntPtrConstant(NumBytes, true),
2341 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2342 true),
2343 InFlag);
2344 InFlag = Chain.getValue(1);
2345 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002346
Chris Lattner3085e152007-02-25 08:59:22 +00002347 // Handle result values, copying them out of physregs into vregs that we
2348 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002349 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2350 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002351}
2352
Evan Cheng25ab6902006-09-08 06:48:29 +00002353
2354//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002355// Fast Calling Convention (tail call) implementation
2356//===----------------------------------------------------------------------===//
2357
2358// Like std call, callee cleans arguments, convention except that ECX is
2359// reserved for storing the tail called function address. Only 2 registers are
2360// free for argument passing (inreg). Tail call optimization is performed
2361// provided:
2362// * tailcallopt is enabled
2363// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002364// On X86_64 architecture with GOT-style position independent code only local
2365// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002366// To keep the stack aligned according to platform abi the function
2367// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2368// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002369// If a tail called function callee has more arguments than the caller the
2370// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002371// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002372// original REtADDR, but before the saved framepointer or the spilled registers
2373// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2374// stack layout:
2375// arg1
2376// arg2
2377// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002378// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002379// move area ]
2380// (possible EBP)
2381// ESI
2382// EDI
2383// local1 ..
2384
2385/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2386/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002387unsigned
2388X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2389 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002390 MachineFunction &MF = DAG.getMachineFunction();
2391 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002392 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002393 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002394 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002395 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002396 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002397 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2398 // Number smaller than 12 so just add the difference.
2399 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2400 } else {
2401 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002402 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002403 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002404 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002405 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002406}
2407
Evan Cheng5f941932010-02-05 02:21:12 +00002408/// MatchingStackOffset - Return true if the given stack call argument is
2409/// already available in the same position (relatively) of the caller's
2410/// incoming argument stack.
2411static
2412bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2413 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2414 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002415 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2416 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002417 if (Arg.getOpcode() == ISD::CopyFromReg) {
2418 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002419 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002420 return false;
2421 MachineInstr *Def = MRI->getVRegDef(VR);
2422 if (!Def)
2423 return false;
2424 if (!Flags.isByVal()) {
2425 if (!TII->isLoadFromStackSlot(Def, FI))
2426 return false;
2427 } else {
2428 unsigned Opcode = Def->getOpcode();
2429 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2430 Def->getOperand(1).isFI()) {
2431 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002432 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002433 } else
2434 return false;
2435 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002436 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2437 if (Flags.isByVal())
2438 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002439 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002440 // define @foo(%struct.X* %A) {
2441 // tail call @bar(%struct.X* byval %A)
2442 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002443 return false;
2444 SDValue Ptr = Ld->getBasePtr();
2445 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2446 if (!FINode)
2447 return false;
2448 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002449 } else
2450 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002451
Evan Cheng4cae1332010-03-05 08:38:04 +00002452 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002453 if (!MFI->isFixedObjectIndex(FI))
2454 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002455 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002456}
2457
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2459/// for tail call optimization. Targets which want to do tail call
2460/// optimization should implement this function.
2461bool
2462X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002463 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002465 bool isCalleeStructRet,
2466 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002467 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002468 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002469 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002471 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002472 CalleeCC != CallingConv::C)
2473 return false;
2474
Evan Cheng7096ae42010-01-29 06:45:59 +00002475 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002476 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002477 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002478 CallingConv::ID CallerCC = CallerF->getCallingConv();
2479 bool CCMatch = CallerCC == CalleeCC;
2480
Dan Gohman1797ed52010-02-08 20:27:50 +00002481 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002482 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002483 return true;
2484 return false;
2485 }
2486
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002487 // Look for obvious safe cases to perform tail call optimization that do not
2488 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002489
Evan Cheng2c12cb42010-03-26 16:26:03 +00002490 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2491 // emit a special epilogue.
2492 if (RegInfo->needsStackRealignment(MF))
2493 return false;
2494
Eric Christopher90eb4022010-07-22 00:26:08 +00002495 // Do not sibcall optimize vararg calls unless the call site is not passing
2496 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002497 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002498 return false;
2499
Evan Chenga375d472010-03-15 18:54:48 +00002500 // Also avoid sibcall optimization if either caller or callee uses struct
2501 // return semantics.
2502 if (isCalleeStructRet || isCallerStructRet)
2503 return false;
2504
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002505 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2506 // Therefore if it's not used by the call it is not safe to optimize this into
2507 // a sibcall.
2508 bool Unused = false;
2509 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2510 if (!Ins[i].Used) {
2511 Unused = true;
2512 break;
2513 }
2514 }
2515 if (Unused) {
2516 SmallVector<CCValAssign, 16> RVLocs;
2517 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2518 RVLocs, *DAG.getContext());
2519 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002520 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002521 CCValAssign &VA = RVLocs[i];
2522 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2523 return false;
2524 }
2525 }
2526
Evan Cheng13617962010-04-30 01:12:32 +00002527 // If the calling conventions do not match, then we'd better make sure the
2528 // results are returned in the same way as what the caller expects.
2529 if (!CCMatch) {
2530 SmallVector<CCValAssign, 16> RVLocs1;
2531 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2532 RVLocs1, *DAG.getContext());
2533 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2534
2535 SmallVector<CCValAssign, 16> RVLocs2;
2536 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2537 RVLocs2, *DAG.getContext());
2538 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2539
2540 if (RVLocs1.size() != RVLocs2.size())
2541 return false;
2542 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2543 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2544 return false;
2545 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2546 return false;
2547 if (RVLocs1[i].isRegLoc()) {
2548 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2549 return false;
2550 } else {
2551 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2552 return false;
2553 }
2554 }
2555 }
2556
Evan Chenga6bff982010-01-30 01:22:00 +00002557 // If the callee takes no arguments then go on to check the results of the
2558 // call.
2559 if (!Outs.empty()) {
2560 // Check if stack adjustment is needed. For now, do not do this if any
2561 // argument is passed on the stack.
2562 SmallVector<CCValAssign, 16> ArgLocs;
2563 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2564 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002565
2566 // Allocate shadow area for Win64
2567 if (Subtarget->isTargetWin64()) {
2568 CCInfo.AllocateStack(32, 8);
2569 }
2570
Duncan Sands45907662010-10-31 13:21:44 +00002571 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002572 if (CCInfo.getNextStackOffset()) {
2573 MachineFunction &MF = DAG.getMachineFunction();
2574 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2575 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002576
2577 // Check if the arguments are already laid out in the right way as
2578 // the caller's fixed stack objects.
2579 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002580 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2581 const X86InstrInfo *TII =
2582 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002583 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2584 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002585 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002586 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002587 if (VA.getLocInfo() == CCValAssign::Indirect)
2588 return false;
2589 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002590 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2591 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002592 return false;
2593 }
2594 }
2595 }
Evan Cheng9c044672010-05-29 01:35:22 +00002596
2597 // If the tailcall address may be in a register, then make sure it's
2598 // possible to register allocate for it. In 32-bit, the call address can
2599 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002600 // callee-saved registers are restored. These happen to be the same
2601 // registers used to pass 'inreg' arguments so watch out for those.
2602 if (!Subtarget->is64Bit() &&
2603 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002604 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002605 unsigned NumInRegs = 0;
2606 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2607 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002608 if (!VA.isRegLoc())
2609 continue;
2610 unsigned Reg = VA.getLocReg();
2611 switch (Reg) {
2612 default: break;
2613 case X86::EAX: case X86::EDX: case X86::ECX:
2614 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002615 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002616 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002617 }
2618 }
2619 }
Evan Chenga6bff982010-01-30 01:22:00 +00002620 }
Evan Chengb1712452010-01-27 06:25:16 +00002621
Dale Johannesend155d7e2010-10-25 22:17:05 +00002622 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002623 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002624 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2625 return false;
2626
Evan Cheng86809cc2010-02-03 03:28:02 +00002627 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002628}
2629
Dan Gohman3df24e62008-09-03 23:12:08 +00002630FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002631X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2632 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002633}
2634
2635
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002636//===----------------------------------------------------------------------===//
2637// Other Lowering Hooks
2638//===----------------------------------------------------------------------===//
2639
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002640static bool MayFoldLoad(SDValue Op) {
2641 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2642}
2643
2644static bool MayFoldIntoStore(SDValue Op) {
2645 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2646}
2647
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002648static bool isTargetShuffle(unsigned Opcode) {
2649 switch(Opcode) {
2650 default: return false;
2651 case X86ISD::PSHUFD:
2652 case X86ISD::PSHUFHW:
2653 case X86ISD::PSHUFLW:
2654 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002655 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002656 case X86ISD::SHUFPS:
2657 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002658 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002659 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002660 case X86ISD::MOVLPS:
2661 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002662 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002663 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002664 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002665 case X86ISD::MOVSS:
2666 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002667 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002668 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002669 case X86ISD::VUNPCKLPS:
2670 case X86ISD::VUNPCKLPD:
2671 case X86ISD::VUNPCKLPSY:
2672 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002673 case X86ISD::PUNPCKLWD:
2674 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002675 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002676 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002677 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002678 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002679 case X86ISD::PUNPCKHWD:
2680 case X86ISD::PUNPCKHBW:
2681 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002682 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002683 return true;
2684 }
2685 return false;
2686}
2687
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002688static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002689 SDValue V1, SelectionDAG &DAG) {
2690 switch(Opc) {
2691 default: llvm_unreachable("Unknown x86 shuffle node");
2692 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002693 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002694 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002695 return DAG.getNode(Opc, dl, VT, V1);
2696 }
2697
2698 return SDValue();
2699}
2700
2701static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002702 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002703 switch(Opc) {
2704 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002705 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002706 case X86ISD::PSHUFHW:
2707 case X86ISD::PSHUFLW:
2708 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2709 }
2710
2711 return SDValue();
2712}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002713
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002714static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2715 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2716 switch(Opc) {
2717 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002718 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002719 case X86ISD::SHUFPD:
2720 case X86ISD::SHUFPS:
2721 return DAG.getNode(Opc, dl, VT, V1, V2,
2722 DAG.getConstant(TargetMask, MVT::i8));
2723 }
2724 return SDValue();
2725}
2726
2727static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2728 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2729 switch(Opc) {
2730 default: llvm_unreachable("Unknown x86 shuffle node");
2731 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002732 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002733 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002734 case X86ISD::MOVLPS:
2735 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002736 case X86ISD::MOVSS:
2737 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002738 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002739 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002740 case X86ISD::VUNPCKLPS:
2741 case X86ISD::VUNPCKLPD:
2742 case X86ISD::VUNPCKLPSY:
2743 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002744 case X86ISD::PUNPCKLWD:
2745 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002746 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002747 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002748 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002749 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002750 case X86ISD::PUNPCKHWD:
2751 case X86ISD::PUNPCKHBW:
2752 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002753 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002754 return DAG.getNode(Opc, dl, VT, V1, V2);
2755 }
2756 return SDValue();
2757}
2758
Dan Gohmand858e902010-04-17 15:26:15 +00002759SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002760 MachineFunction &MF = DAG.getMachineFunction();
2761 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2762 int ReturnAddrIndex = FuncInfo->getRAIndex();
2763
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002764 if (ReturnAddrIndex == 0) {
2765 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002766 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002767 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002768 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002769 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002770 }
2771
Evan Cheng25ab6902006-09-08 06:48:29 +00002772 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002773}
2774
2775
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002776bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2777 bool hasSymbolicDisplacement) {
2778 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002779 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002780 return false;
2781
2782 // If we don't have a symbolic displacement - we don't have any extra
2783 // restrictions.
2784 if (!hasSymbolicDisplacement)
2785 return true;
2786
2787 // FIXME: Some tweaks might be needed for medium code model.
2788 if (M != CodeModel::Small && M != CodeModel::Kernel)
2789 return false;
2790
2791 // For small code model we assume that latest object is 16MB before end of 31
2792 // bits boundary. We may also accept pretty large negative constants knowing
2793 // that all objects are in the positive half of address space.
2794 if (M == CodeModel::Small && Offset < 16*1024*1024)
2795 return true;
2796
2797 // For kernel code model we know that all object resist in the negative half
2798 // of 32bits address space. We may not accept negative offsets, since they may
2799 // be just off and we may accept pretty large positive ones.
2800 if (M == CodeModel::Kernel && Offset > 0)
2801 return true;
2802
2803 return false;
2804}
2805
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002806/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2807/// specific condition code, returning the condition code and the LHS/RHS of the
2808/// comparison to make.
2809static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2810 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002811 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002812 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2813 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2814 // X > -1 -> X == 0, jump !sign.
2815 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002816 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002817 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2818 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002819 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002820 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002821 // X < 1 -> X <= 0
2822 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002823 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002824 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002825 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002826
Evan Chengd9558e02006-01-06 00:43:03 +00002827 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002828 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002829 case ISD::SETEQ: return X86::COND_E;
2830 case ISD::SETGT: return X86::COND_G;
2831 case ISD::SETGE: return X86::COND_GE;
2832 case ISD::SETLT: return X86::COND_L;
2833 case ISD::SETLE: return X86::COND_LE;
2834 case ISD::SETNE: return X86::COND_NE;
2835 case ISD::SETULT: return X86::COND_B;
2836 case ISD::SETUGT: return X86::COND_A;
2837 case ISD::SETULE: return X86::COND_BE;
2838 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002839 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002840 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002841
Chris Lattner4c78e022008-12-23 23:42:27 +00002842 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002843
Chris Lattner4c78e022008-12-23 23:42:27 +00002844 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002845 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2846 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002847 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2848 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002849 }
2850
Chris Lattner4c78e022008-12-23 23:42:27 +00002851 switch (SetCCOpcode) {
2852 default: break;
2853 case ISD::SETOLT:
2854 case ISD::SETOLE:
2855 case ISD::SETUGT:
2856 case ISD::SETUGE:
2857 std::swap(LHS, RHS);
2858 break;
2859 }
2860
2861 // On a floating point condition, the flags are set as follows:
2862 // ZF PF CF op
2863 // 0 | 0 | 0 | X > Y
2864 // 0 | 0 | 1 | X < Y
2865 // 1 | 0 | 0 | X == Y
2866 // 1 | 1 | 1 | unordered
2867 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002868 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002869 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002870 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002871 case ISD::SETOLT: // flipped
2872 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002873 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002874 case ISD::SETOLE: // flipped
2875 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002876 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002877 case ISD::SETUGT: // flipped
2878 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002879 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002880 case ISD::SETUGE: // flipped
2881 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002882 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002883 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002884 case ISD::SETNE: return X86::COND_NE;
2885 case ISD::SETUO: return X86::COND_P;
2886 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002887 case ISD::SETOEQ:
2888 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002889 }
Evan Chengd9558e02006-01-06 00:43:03 +00002890}
2891
Evan Cheng4a460802006-01-11 00:33:36 +00002892/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2893/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002894/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002895static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002896 switch (X86CC) {
2897 default:
2898 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002899 case X86::COND_B:
2900 case X86::COND_BE:
2901 case X86::COND_E:
2902 case X86::COND_P:
2903 case X86::COND_A:
2904 case X86::COND_AE:
2905 case X86::COND_NE:
2906 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002907 return true;
2908 }
2909}
2910
Evan Chengeb2f9692009-10-27 19:56:55 +00002911/// isFPImmLegal - Returns true if the target can instruction select the
2912/// specified FP immediate natively. If false, the legalizer will
2913/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002914bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002915 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2916 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2917 return true;
2918 }
2919 return false;
2920}
2921
Nate Begeman9008ca62009-04-27 18:41:29 +00002922/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2923/// the specified range (L, H].
2924static bool isUndefOrInRange(int Val, int Low, int Hi) {
2925 return (Val < 0) || (Val >= Low && Val < Hi);
2926}
2927
2928/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2929/// specified value.
2930static bool isUndefOrEqual(int Val, int CmpVal) {
2931 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002932 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002933 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002934}
2935
Nate Begeman9008ca62009-04-27 18:41:29 +00002936/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2937/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2938/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002939static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002940 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002941 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002942 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 return (Mask[0] < 2 && Mask[1] < 2);
2944 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002945}
2946
Nate Begeman9008ca62009-04-27 18:41:29 +00002947bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002948 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 N->getMask(M);
2950 return ::isPSHUFDMask(M, N->getValueType(0));
2951}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002952
Nate Begeman9008ca62009-04-27 18:41:29 +00002953/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2954/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002955static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002957 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002958
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 // Lower quadword copied in order or undef.
2960 for (int i = 0; i != 4; ++i)
2961 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002962 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002963
Evan Cheng506d3df2006-03-29 23:07:14 +00002964 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002965 for (int i = 4; i != 8; ++i)
2966 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002967 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002968
Evan Cheng506d3df2006-03-29 23:07:14 +00002969 return true;
2970}
2971
Nate Begeman9008ca62009-04-27 18:41:29 +00002972bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002973 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002974 N->getMask(M);
2975 return ::isPSHUFHWMask(M, N->getValueType(0));
2976}
Evan Cheng506d3df2006-03-29 23:07:14 +00002977
Nate Begeman9008ca62009-04-27 18:41:29 +00002978/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2979/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002980static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002982 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002983
Rafael Espindola15684b22009-04-24 12:40:33 +00002984 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002985 for (int i = 4; i != 8; ++i)
2986 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002987 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002988
Rafael Espindola15684b22009-04-24 12:40:33 +00002989 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 for (int i = 0; i != 4; ++i)
2991 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002992 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002993
Rafael Espindola15684b22009-04-24 12:40:33 +00002994 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002995}
2996
Nate Begeman9008ca62009-04-27 18:41:29 +00002997bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002998 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002999 N->getMask(M);
3000 return ::isPSHUFLWMask(M, N->getValueType(0));
3001}
3002
Nate Begemana09008b2009-10-19 02:17:23 +00003003/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3004/// is suitable for input to PALIGNR.
3005static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3006 bool hasSSSE3) {
3007 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003008
Nate Begemana09008b2009-10-19 02:17:23 +00003009 // Do not handle v2i64 / v2f64 shuffles with palignr.
3010 if (e < 4 || !hasSSSE3)
3011 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003012
Nate Begemana09008b2009-10-19 02:17:23 +00003013 for (i = 0; i != e; ++i)
3014 if (Mask[i] >= 0)
3015 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003016
Nate Begemana09008b2009-10-19 02:17:23 +00003017 // All undef, not a palignr.
3018 if (i == e)
3019 return false;
3020
3021 // Determine if it's ok to perform a palignr with only the LHS, since we
3022 // don't have access to the actual shuffle elements to see if RHS is undef.
3023 bool Unary = Mask[i] < (int)e;
3024 bool NeedsUnary = false;
3025
3026 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003027
Nate Begemana09008b2009-10-19 02:17:23 +00003028 // Check the rest of the elements to see if they are consecutive.
3029 for (++i; i != e; ++i) {
3030 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003031 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003032 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003033
Nate Begemana09008b2009-10-19 02:17:23 +00003034 Unary = Unary && (m < (int)e);
3035 NeedsUnary = NeedsUnary || (m < s);
3036
3037 if (NeedsUnary && !Unary)
3038 return false;
3039 if (Unary && m != ((s+i) & (e-1)))
3040 return false;
3041 if (!Unary && m != (s+i))
3042 return false;
3043 }
3044 return true;
3045}
3046
3047bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3048 SmallVector<int, 8> M;
3049 N->getMask(M);
3050 return ::isPALIGNRMask(M, N->getValueType(0), true);
3051}
3052
Evan Cheng14aed5e2006-03-24 01:18:28 +00003053/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3054/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003055static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 int NumElems = VT.getVectorNumElements();
3057 if (NumElems != 2 && NumElems != 4)
3058 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003059
Nate Begeman9008ca62009-04-27 18:41:29 +00003060 int Half = NumElems / 2;
3061 for (int i = 0; i < Half; ++i)
3062 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003063 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 for (int i = Half; i < NumElems; ++i)
3065 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003066 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003067
Evan Cheng14aed5e2006-03-24 01:18:28 +00003068 return true;
3069}
3070
Nate Begeman9008ca62009-04-27 18:41:29 +00003071bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3072 SmallVector<int, 8> M;
3073 N->getMask(M);
3074 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003075}
3076
Evan Cheng213d2cf2007-05-17 18:45:50 +00003077/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003078/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3079/// half elements to come from vector 1 (which would equal the dest.) and
3080/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003081static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003082 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003083
3084 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003085 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003086
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 int Half = NumElems / 2;
3088 for (int i = 0; i < Half; ++i)
3089 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003090 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 for (int i = Half; i < NumElems; ++i)
3092 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003093 return false;
3094 return true;
3095}
3096
Nate Begeman9008ca62009-04-27 18:41:29 +00003097static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3098 SmallVector<int, 8> M;
3099 N->getMask(M);
3100 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003101}
3102
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003103/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3104/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003105bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3106 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003107 return false;
3108
Evan Cheng2064a2b2006-03-28 06:50:32 +00003109 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003110 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3111 isUndefOrEqual(N->getMaskElt(1), 7) &&
3112 isUndefOrEqual(N->getMaskElt(2), 2) &&
3113 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003114}
3115
Nate Begeman0b10b912009-11-07 23:17:15 +00003116/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3117/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3118/// <2, 3, 2, 3>
3119bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3120 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003121
Nate Begeman0b10b912009-11-07 23:17:15 +00003122 if (NumElems != 4)
3123 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003124
Nate Begeman0b10b912009-11-07 23:17:15 +00003125 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3126 isUndefOrEqual(N->getMaskElt(1), 3) &&
3127 isUndefOrEqual(N->getMaskElt(2), 2) &&
3128 isUndefOrEqual(N->getMaskElt(3), 3);
3129}
3130
Evan Cheng5ced1d82006-04-06 23:23:56 +00003131/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3132/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003133bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3134 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003135
Evan Cheng5ced1d82006-04-06 23:23:56 +00003136 if (NumElems != 2 && NumElems != 4)
3137 return false;
3138
Evan Chengc5cdff22006-04-07 21:53:05 +00003139 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003141 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003142
Evan Chengc5cdff22006-04-07 21:53:05 +00003143 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003144 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003145 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003146
3147 return true;
3148}
3149
Nate Begeman0b10b912009-11-07 23:17:15 +00003150/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3151/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3152bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003154
David Greenea20244d2011-03-02 17:23:43 +00003155 if ((NumElems != 2 && NumElems != 4)
3156 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157 return false;
3158
Evan Chengc5cdff22006-04-07 21:53:05 +00003159 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003161 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163 for (unsigned i = 0; i < NumElems/2; ++i)
3164 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003165 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003166
3167 return true;
3168}
3169
Evan Cheng0038e592006-03-28 00:39:58 +00003170/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3171/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003172static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003173 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003175 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003176 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003177
David Greenea20244d2011-03-02 17:23:43 +00003178 // Handle vector lengths > 128 bits. Define a "section" as a set of
3179 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3180 // sections.
3181 unsigned NumSections = VT.getSizeInBits() / 128;
3182 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3183 unsigned NumSectionElts = NumElts / NumSections;
3184
3185 unsigned Start = 0;
3186 unsigned End = NumSectionElts;
3187 for (unsigned s = 0; s < NumSections; ++s) {
3188 for (unsigned i = Start, j = s * NumSectionElts;
3189 i != End;
3190 i += 2, ++j) {
3191 int BitI = Mask[i];
3192 int BitI1 = Mask[i+1];
3193 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003194 return false;
David Greenea20244d2011-03-02 17:23:43 +00003195 if (V2IsSplat) {
3196 if (!isUndefOrEqual(BitI1, NumElts))
3197 return false;
3198 } else {
3199 if (!isUndefOrEqual(BitI1, j + NumElts))
3200 return false;
3201 }
Evan Cheng39623da2006-04-20 08:58:49 +00003202 }
David Greenea20244d2011-03-02 17:23:43 +00003203 // Process the next 128 bits.
3204 Start += NumSectionElts;
3205 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003206 }
David Greenea20244d2011-03-02 17:23:43 +00003207
Evan Cheng0038e592006-03-28 00:39:58 +00003208 return true;
3209}
3210
Nate Begeman9008ca62009-04-27 18:41:29 +00003211bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3212 SmallVector<int, 8> M;
3213 N->getMask(M);
3214 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003215}
3216
Evan Cheng4fcb9222006-03-28 02:43:26 +00003217/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3218/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003219static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003220 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003221 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003222 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003223 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003224
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3226 int BitI = Mask[i];
3227 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003228 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003229 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003230 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003231 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003232 return false;
3233 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003234 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003235 return false;
3236 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003237 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003238 return true;
3239}
3240
Nate Begeman9008ca62009-04-27 18:41:29 +00003241bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3242 SmallVector<int, 8> M;
3243 N->getMask(M);
3244 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003245}
3246
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003247/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3248/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3249/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003250static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003252 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003253 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003254
David Greenea20244d2011-03-02 17:23:43 +00003255 // Handle vector lengths > 128 bits. Define a "section" as a set of
3256 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3257 // sections.
3258 unsigned NumSections = VT.getSizeInBits() / 128;
3259 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3260 unsigned NumSectionElts = NumElems / NumSections;
3261
3262 for (unsigned s = 0; s < NumSections; ++s) {
3263 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3264 i != NumSectionElts * (s + 1);
3265 i += 2, ++j) {
3266 int BitI = Mask[i];
3267 int BitI1 = Mask[i+1];
3268
3269 if (!isUndefOrEqual(BitI, j))
3270 return false;
3271 if (!isUndefOrEqual(BitI1, j))
3272 return false;
3273 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003274 }
David Greenea20244d2011-03-02 17:23:43 +00003275
Rafael Espindola15684b22009-04-24 12:40:33 +00003276 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003277}
3278
Nate Begeman9008ca62009-04-27 18:41:29 +00003279bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3280 SmallVector<int, 8> M;
3281 N->getMask(M);
3282 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3283}
3284
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003285/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3286/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3287/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003288static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003290 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3291 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003292
Nate Begeman9008ca62009-04-27 18:41:29 +00003293 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3294 int BitI = Mask[i];
3295 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003296 if (!isUndefOrEqual(BitI, j))
3297 return false;
3298 if (!isUndefOrEqual(BitI1, j))
3299 return false;
3300 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003301 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003302}
3303
Nate Begeman9008ca62009-04-27 18:41:29 +00003304bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3305 SmallVector<int, 8> M;
3306 N->getMask(M);
3307 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3308}
3309
Evan Cheng017dcc62006-04-21 01:05:10 +00003310/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3311/// specifies a shuffle of elements that is suitable for input to MOVSS,
3312/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003313static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003314 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003315 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003316
3317 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003318
Nate Begeman9008ca62009-04-27 18:41:29 +00003319 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003320 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003321
Nate Begeman9008ca62009-04-27 18:41:29 +00003322 for (int i = 1; i < NumElts; ++i)
3323 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003324 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003325
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003326 return true;
3327}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003328
Nate Begeman9008ca62009-04-27 18:41:29 +00003329bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3330 SmallVector<int, 8> M;
3331 N->getMask(M);
3332 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003333}
3334
Evan Cheng017dcc62006-04-21 01:05:10 +00003335/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3336/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003337/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003338static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003339 bool V2IsSplat = false, bool V2IsUndef = false) {
3340 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003341 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003342 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003343
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003345 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003346
Nate Begeman9008ca62009-04-27 18:41:29 +00003347 for (int i = 1; i < NumOps; ++i)
3348 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3349 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3350 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003351 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003352
Evan Cheng39623da2006-04-20 08:58:49 +00003353 return true;
3354}
3355
Nate Begeman9008ca62009-04-27 18:41:29 +00003356static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003357 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003358 SmallVector<int, 8> M;
3359 N->getMask(M);
3360 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003361}
3362
Evan Chengd9539472006-04-14 21:59:03 +00003363/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3364/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003365bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3366 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003367 return false;
3368
3369 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003370 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 int Elt = N->getMaskElt(i);
3372 if (Elt >= 0 && Elt != 1)
3373 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003374 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003375
3376 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003377 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003378 int Elt = N->getMaskElt(i);
3379 if (Elt >= 0 && Elt != 3)
3380 return false;
3381 if (Elt == 3)
3382 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003383 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003384 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003386 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003387}
3388
3389/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3390/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003391bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3392 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003393 return false;
3394
3395 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 for (unsigned i = 0; i < 2; ++i)
3397 if (N->getMaskElt(i) > 0)
3398 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003399
3400 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003401 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 int Elt = N->getMaskElt(i);
3403 if (Elt >= 0 && Elt != 2)
3404 return false;
3405 if (Elt == 2)
3406 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003407 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003409 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003410}
3411
Evan Cheng0b457f02008-09-25 20:50:48 +00003412/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3413/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003414bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3415 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003416
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 for (int i = 0; i < e; ++i)
3418 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003419 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 for (int i = 0; i < e; ++i)
3421 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003422 return false;
3423 return true;
3424}
3425
David Greenec38a03e2011-02-03 15:50:00 +00003426/// isVEXTRACTF128Index - Return true if the specified
3427/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3428/// suitable for input to VEXTRACTF128.
3429bool X86::isVEXTRACTF128Index(SDNode *N) {
3430 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3431 return false;
3432
3433 // The index should be aligned on a 128-bit boundary.
3434 uint64_t Index =
3435 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3436
3437 unsigned VL = N->getValueType(0).getVectorNumElements();
3438 unsigned VBits = N->getValueType(0).getSizeInBits();
3439 unsigned ElSize = VBits / VL;
3440 bool Result = (Index * ElSize) % 128 == 0;
3441
3442 return Result;
3443}
3444
David Greeneccacdc12011-02-04 16:08:29 +00003445/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3446/// operand specifies a subvector insert that is suitable for input to
3447/// VINSERTF128.
3448bool X86::isVINSERTF128Index(SDNode *N) {
3449 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3450 return false;
3451
3452 // The index should be aligned on a 128-bit boundary.
3453 uint64_t Index =
3454 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3455
3456 unsigned VL = N->getValueType(0).getVectorNumElements();
3457 unsigned VBits = N->getValueType(0).getSizeInBits();
3458 unsigned ElSize = VBits / VL;
3459 bool Result = (Index * ElSize) % 128 == 0;
3460
3461 return Result;
3462}
3463
Evan Cheng63d33002006-03-22 08:01:21 +00003464/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003465/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003466unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3468 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3469
Evan Chengb9df0ca2006-03-22 02:53:00 +00003470 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3471 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 for (int i = 0; i < NumOperands; ++i) {
3473 int Val = SVOp->getMaskElt(NumOperands-i-1);
3474 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003475 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003476 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003477 if (i != NumOperands - 1)
3478 Mask <<= Shift;
3479 }
Evan Cheng63d33002006-03-22 08:01:21 +00003480 return Mask;
3481}
3482
Evan Cheng506d3df2006-03-29 23:07:14 +00003483/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003484/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003485unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003487 unsigned Mask = 0;
3488 // 8 nodes, but we only care about the last 4.
3489 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003490 int Val = SVOp->getMaskElt(i);
3491 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003492 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003493 if (i != 4)
3494 Mask <<= 2;
3495 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003496 return Mask;
3497}
3498
3499/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003500/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003501unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003503 unsigned Mask = 0;
3504 // 8 nodes, but we only care about the first 4.
3505 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003506 int Val = SVOp->getMaskElt(i);
3507 if (Val >= 0)
3508 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003509 if (i != 0)
3510 Mask <<= 2;
3511 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003512 return Mask;
3513}
3514
Nate Begemana09008b2009-10-19 02:17:23 +00003515/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3516/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3517unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3518 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3519 EVT VVT = N->getValueType(0);
3520 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3521 int Val = 0;
3522
3523 unsigned i, e;
3524 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3525 Val = SVOp->getMaskElt(i);
3526 if (Val >= 0)
3527 break;
3528 }
3529 return (Val - i) * EltSize;
3530}
3531
David Greenec38a03e2011-02-03 15:50:00 +00003532/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3533/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3534/// instructions.
3535unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3536 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3537 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3538
3539 uint64_t Index =
3540 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3541
3542 EVT VecVT = N->getOperand(0).getValueType();
3543 EVT ElVT = VecVT.getVectorElementType();
3544
3545 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3546
3547 return Index / NumElemsPerChunk;
3548}
3549
David Greeneccacdc12011-02-04 16:08:29 +00003550/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3551/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3552/// instructions.
3553unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3554 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3555 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3556
3557 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003558 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003559
3560 EVT VecVT = N->getValueType(0);
3561 EVT ElVT = VecVT.getVectorElementType();
3562
3563 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3564
3565 return Index / NumElemsPerChunk;
3566}
3567
Evan Cheng37b73872009-07-30 08:33:02 +00003568/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3569/// constant +0.0.
3570bool X86::isZeroNode(SDValue Elt) {
3571 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003572 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003573 (isa<ConstantFPSDNode>(Elt) &&
3574 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3575}
3576
Nate Begeman9008ca62009-04-27 18:41:29 +00003577/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3578/// their permute mask.
3579static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3580 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003581 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003582 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003584
Nate Begeman5a5ca152009-04-29 05:20:52 +00003585 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003586 int idx = SVOp->getMaskElt(i);
3587 if (idx < 0)
3588 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003589 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003590 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003591 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003593 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003594 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3595 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003596}
3597
Evan Cheng779ccea2007-12-07 21:30:01 +00003598/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3599/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003600static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003601 unsigned NumElems = VT.getVectorNumElements();
3602 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 int idx = Mask[i];
3604 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003605 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003606 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003608 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003610 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003611}
3612
Evan Cheng533a0aa2006-04-19 20:35:22 +00003613/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3614/// match movhlps. The lower half elements should come from upper half of
3615/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003616/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003617static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3618 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003619 return false;
3620 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003621 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003622 return false;
3623 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003624 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003625 return false;
3626 return true;
3627}
3628
Evan Cheng5ced1d82006-04-06 23:23:56 +00003629/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003630/// is promoted to a vector. It also returns the LoadSDNode by reference if
3631/// required.
3632static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003633 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3634 return false;
3635 N = N->getOperand(0).getNode();
3636 if (!ISD::isNON_EXTLoad(N))
3637 return false;
3638 if (LD)
3639 *LD = cast<LoadSDNode>(N);
3640 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003641}
3642
Evan Cheng533a0aa2006-04-19 20:35:22 +00003643/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3644/// match movlp{s|d}. The lower half elements should come from lower half of
3645/// V1 (and in order), and the upper half elements should come from the upper
3646/// half of V2 (and in order). And since V1 will become the source of the
3647/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003648static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3649 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003650 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003651 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003652 // Is V2 is a vector load, don't do this transformation. We will try to use
3653 // load folding shufps op.
3654 if (ISD::isNON_EXTLoad(V2))
3655 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003656
Nate Begeman5a5ca152009-04-29 05:20:52 +00003657 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003658
Evan Cheng533a0aa2006-04-19 20:35:22 +00003659 if (NumElems != 2 && NumElems != 4)
3660 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003661 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003662 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003663 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003664 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003665 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003666 return false;
3667 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003668}
3669
Evan Cheng39623da2006-04-20 08:58:49 +00003670/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3671/// all the same.
3672static bool isSplatVector(SDNode *N) {
3673 if (N->getOpcode() != ISD::BUILD_VECTOR)
3674 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003675
Dan Gohman475871a2008-07-27 21:46:04 +00003676 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003677 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3678 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003679 return false;
3680 return true;
3681}
3682
Evan Cheng213d2cf2007-05-17 18:45:50 +00003683/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003684/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003685/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003686static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003687 SDValue V1 = N->getOperand(0);
3688 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003689 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3690 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003691 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003692 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003694 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3695 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003696 if (Opc != ISD::BUILD_VECTOR ||
3697 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 return false;
3699 } else if (Idx >= 0) {
3700 unsigned Opc = V1.getOpcode();
3701 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3702 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003703 if (Opc != ISD::BUILD_VECTOR ||
3704 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003705 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003706 }
3707 }
3708 return true;
3709}
3710
3711/// getZeroVector - Returns a vector of specified type with all zero elements.
3712///
Owen Andersone50ed302009-08-10 22:56:29 +00003713static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003714 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003716
Dale Johannesen0488fb62010-09-30 23:57:10 +00003717 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003718 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003719 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003720 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003721 if (HasSSE2) { // SSE2
3722 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3723 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3724 } else { // SSE1
3725 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3726 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3727 }
3728 } else if (VT.getSizeInBits() == 256) { // AVX
3729 // 256-bit logic and arithmetic instructions in AVX are
3730 // all floating-point, no support for integer ops. Default
3731 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003733 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3734 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003735 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003736 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003737}
3738
Chris Lattner8a594482007-11-25 00:24:49 +00003739/// getOnesVector - Returns a vector of specified type with all bits set.
3740///
Owen Andersone50ed302009-08-10 22:56:29 +00003741static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003742 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003743
Chris Lattner8a594482007-11-25 00:24:49 +00003744 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3745 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003747 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003748 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003749 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003750}
3751
3752
Evan Cheng39623da2006-04-20 08:58:49 +00003753/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3754/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003755static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003756 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003757 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003758
Evan Cheng39623da2006-04-20 08:58:49 +00003759 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003760 SmallVector<int, 8> MaskVec;
3761 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003762
Nate Begeman5a5ca152009-04-29 05:20:52 +00003763 for (unsigned i = 0; i != NumElems; ++i) {
3764 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003765 MaskVec[i] = NumElems;
3766 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003767 }
Evan Cheng39623da2006-04-20 08:58:49 +00003768 }
Evan Cheng39623da2006-04-20 08:58:49 +00003769 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003770 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3771 SVOp->getOperand(1), &MaskVec[0]);
3772 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003773}
3774
Evan Cheng017dcc62006-04-21 01:05:10 +00003775/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3776/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003777static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003778 SDValue V2) {
3779 unsigned NumElems = VT.getVectorNumElements();
3780 SmallVector<int, 8> Mask;
3781 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003782 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003783 Mask.push_back(i);
3784 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003785}
3786
Nate Begeman9008ca62009-04-27 18:41:29 +00003787/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003788static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003789 SDValue V2) {
3790 unsigned NumElems = VT.getVectorNumElements();
3791 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003792 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 Mask.push_back(i);
3794 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003795 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003797}
3798
Nate Begeman9008ca62009-04-27 18:41:29 +00003799/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003800static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003801 SDValue V2) {
3802 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003803 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003805 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 Mask.push_back(i + Half);
3807 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003808 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003809 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003810}
3811
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003812/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3813static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003815 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003816 DebugLoc dl = SV->getDebugLoc();
3817 SDValue V1 = SV->getOperand(0);
3818 int NumElems = VT.getVectorNumElements();
3819 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003820
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 // unpack elements to the correct location
3822 while (NumElems > 4) {
3823 if (EltNo < NumElems/2) {
3824 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3825 } else {
3826 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3827 EltNo -= NumElems/2;
3828 }
3829 NumElems >>= 1;
3830 }
Eric Christopherfd179292009-08-27 18:07:15 +00003831
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 // Perform the splat.
3833 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003834 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003835 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003836 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003837}
3838
Evan Chengba05f722006-04-21 23:03:30 +00003839/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003840/// vector of zero or undef vector. This produces a shuffle where the low
3841/// element of V2 is swizzled into the zero/undef vector, landing at element
3842/// 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 +00003843static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003844 bool isZero, bool HasSSE2,
3845 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003846 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003847 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3849 unsigned NumElems = VT.getVectorNumElements();
3850 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003851 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003852 // If this is the insertion idx, put the low elt of V2 here.
3853 MaskVec.push_back(i == Idx ? NumElems : i);
3854 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003855}
3856
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003857/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3858/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003859SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3860 unsigned Depth) {
3861 if (Depth == 6)
3862 return SDValue(); // Limit search depth.
3863
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003864 SDValue V = SDValue(N, 0);
3865 EVT VT = V.getValueType();
3866 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003867
3868 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3869 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3870 Index = SV->getMaskElt(Index);
3871
3872 if (Index < 0)
3873 return DAG.getUNDEF(VT.getVectorElementType());
3874
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003875 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003876 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003877 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003878 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003879
3880 // Recurse into target specific vector shuffles to find scalars.
3881 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003882 int NumElems = VT.getVectorNumElements();
3883 SmallVector<unsigned, 16> ShuffleMask;
3884 SDValue ImmN;
3885
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003886 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003887 case X86ISD::SHUFPS:
3888 case X86ISD::SHUFPD:
3889 ImmN = N->getOperand(N->getNumOperands()-1);
3890 DecodeSHUFPSMask(NumElems,
3891 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3892 ShuffleMask);
3893 break;
3894 case X86ISD::PUNPCKHBW:
3895 case X86ISD::PUNPCKHWD:
3896 case X86ISD::PUNPCKHDQ:
3897 case X86ISD::PUNPCKHQDQ:
3898 DecodePUNPCKHMask(NumElems, ShuffleMask);
3899 break;
3900 case X86ISD::UNPCKHPS:
3901 case X86ISD::UNPCKHPD:
3902 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3903 break;
3904 case X86ISD::PUNPCKLBW:
3905 case X86ISD::PUNPCKLWD:
3906 case X86ISD::PUNPCKLDQ:
3907 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003908 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003909 break;
3910 case X86ISD::UNPCKLPS:
3911 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003912 case X86ISD::VUNPCKLPS:
3913 case X86ISD::VUNPCKLPD:
3914 case X86ISD::VUNPCKLPSY:
3915 case X86ISD::VUNPCKLPDY:
3916 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003917 break;
3918 case X86ISD::MOVHLPS:
3919 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3920 break;
3921 case X86ISD::MOVLHPS:
3922 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3923 break;
3924 case X86ISD::PSHUFD:
3925 ImmN = N->getOperand(N->getNumOperands()-1);
3926 DecodePSHUFMask(NumElems,
3927 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3928 ShuffleMask);
3929 break;
3930 case X86ISD::PSHUFHW:
3931 ImmN = N->getOperand(N->getNumOperands()-1);
3932 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3933 ShuffleMask);
3934 break;
3935 case X86ISD::PSHUFLW:
3936 ImmN = N->getOperand(N->getNumOperands()-1);
3937 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3938 ShuffleMask);
3939 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003940 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003941 case X86ISD::MOVSD: {
3942 // The index 0 always comes from the first element of the second source,
3943 // this is why MOVSS and MOVSD are used in the first place. The other
3944 // elements come from the other positions of the first source vector.
3945 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003946 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3947 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003948 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003949 default:
3950 assert("not implemented for target shuffle node");
3951 return SDValue();
3952 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003953
3954 Index = ShuffleMask[Index];
3955 if (Index < 0)
3956 return DAG.getUNDEF(VT.getVectorElementType());
3957
3958 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3959 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3960 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003961 }
3962
3963 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003964 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003965 V = V.getOperand(0);
3966 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003967 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003968
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003969 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003970 return SDValue();
3971 }
3972
3973 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3974 return (Index == 0) ? V.getOperand(0)
3975 : DAG.getUNDEF(VT.getVectorElementType());
3976
3977 if (V.getOpcode() == ISD::BUILD_VECTOR)
3978 return V.getOperand(Index);
3979
3980 return SDValue();
3981}
3982
3983/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3984/// shuffle operation which come from a consecutively from a zero. The
3985/// search can start in two diferent directions, from left or right.
3986static
3987unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3988 bool ZerosFromLeft, SelectionDAG &DAG) {
3989 int i = 0;
3990
3991 while (i < NumElems) {
3992 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003993 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003994 if (!(Elt.getNode() &&
3995 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3996 break;
3997 ++i;
3998 }
3999
4000 return i;
4001}
4002
4003/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4004/// MaskE correspond consecutively to elements from one of the vector operands,
4005/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4006static
4007bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4008 int OpIdx, int NumElems, unsigned &OpNum) {
4009 bool SeenV1 = false;
4010 bool SeenV2 = false;
4011
4012 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4013 int Idx = SVOp->getMaskElt(i);
4014 // Ignore undef indicies
4015 if (Idx < 0)
4016 continue;
4017
4018 if (Idx < NumElems)
4019 SeenV1 = true;
4020 else
4021 SeenV2 = true;
4022
4023 // Only accept consecutive elements from the same vector
4024 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4025 return false;
4026 }
4027
4028 OpNum = SeenV1 ? 0 : 1;
4029 return true;
4030}
4031
4032/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4033/// logical left shift of a vector.
4034static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4035 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4036 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4037 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4038 false /* check zeros from right */, DAG);
4039 unsigned OpSrc;
4040
4041 if (!NumZeros)
4042 return false;
4043
4044 // Considering the elements in the mask that are not consecutive zeros,
4045 // check if they consecutively come from only one of the source vectors.
4046 //
4047 // V1 = {X, A, B, C} 0
4048 // \ \ \ /
4049 // vector_shuffle V1, V2 <1, 2, 3, X>
4050 //
4051 if (!isShuffleMaskConsecutive(SVOp,
4052 0, // Mask Start Index
4053 NumElems-NumZeros-1, // Mask End Index
4054 NumZeros, // Where to start looking in the src vector
4055 NumElems, // Number of elements in vector
4056 OpSrc)) // Which source operand ?
4057 return false;
4058
4059 isLeft = false;
4060 ShAmt = NumZeros;
4061 ShVal = SVOp->getOperand(OpSrc);
4062 return true;
4063}
4064
4065/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4066/// logical left shift of a vector.
4067static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4068 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4069 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4070 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4071 true /* check zeros from left */, DAG);
4072 unsigned OpSrc;
4073
4074 if (!NumZeros)
4075 return false;
4076
4077 // Considering the elements in the mask that are not consecutive zeros,
4078 // check if they consecutively come from only one of the source vectors.
4079 //
4080 // 0 { A, B, X, X } = V2
4081 // / \ / /
4082 // vector_shuffle V1, V2 <X, X, 4, 5>
4083 //
4084 if (!isShuffleMaskConsecutive(SVOp,
4085 NumZeros, // Mask Start Index
4086 NumElems-1, // Mask End Index
4087 0, // Where to start looking in the src vector
4088 NumElems, // Number of elements in vector
4089 OpSrc)) // Which source operand ?
4090 return false;
4091
4092 isLeft = true;
4093 ShAmt = NumZeros;
4094 ShVal = SVOp->getOperand(OpSrc);
4095 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004096}
4097
4098/// isVectorShift - Returns true if the shuffle can be implemented as a
4099/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004100static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004101 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004102 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4103 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4104 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004105
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004106 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004107}
4108
Evan Chengc78d3b42006-04-24 18:01:45 +00004109/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4110///
Dan Gohman475871a2008-07-27 21:46:04 +00004111static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004112 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004113 SelectionDAG &DAG,
4114 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004115 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004116 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004117
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004118 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004119 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004120 bool First = true;
4121 for (unsigned i = 0; i < 16; ++i) {
4122 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4123 if (ThisIsNonZero && First) {
4124 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004126 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004128 First = false;
4129 }
4130
4131 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004132 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004133 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4134 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004135 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004136 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004137 }
4138 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4140 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4141 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004142 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004144 } else
4145 ThisElt = LastElt;
4146
Gabor Greifba36cb52008-08-28 21:40:38 +00004147 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004149 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004150 }
4151 }
4152
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004153 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004154}
4155
Bill Wendlinga348c562007-03-22 18:42:45 +00004156/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004157///
Dan Gohman475871a2008-07-27 21:46:04 +00004158static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004159 unsigned NumNonZero, unsigned NumZero,
4160 SelectionDAG &DAG,
4161 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004162 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004163 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004164
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004165 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004167 bool First = true;
4168 for (unsigned i = 0; i < 8; ++i) {
4169 bool isNonZero = (NonZeros & (1 << i)) != 0;
4170 if (isNonZero) {
4171 if (First) {
4172 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004174 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004176 First = false;
4177 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004178 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004180 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004181 }
4182 }
4183
4184 return V;
4185}
4186
Evan Chengf26ffe92008-05-29 08:22:04 +00004187/// getVShift - Return a vector logical shift node.
4188///
Owen Andersone50ed302009-08-10 22:56:29 +00004189static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004190 unsigned NumBits, SelectionDAG &DAG,
4191 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004192 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004193 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004194 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4195 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004196 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004197 DAG.getConstant(NumBits,
4198 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004199}
4200
Dan Gohman475871a2008-07-27 21:46:04 +00004201SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004202X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004203 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004204
Evan Chengc3630942009-12-09 21:00:30 +00004205 // Check if the scalar load can be widened into a vector load. And if
4206 // the address is "base + cst" see if the cst can be "absorbed" into
4207 // the shuffle mask.
4208 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4209 SDValue Ptr = LD->getBasePtr();
4210 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4211 return SDValue();
4212 EVT PVT = LD->getValueType(0);
4213 if (PVT != MVT::i32 && PVT != MVT::f32)
4214 return SDValue();
4215
4216 int FI = -1;
4217 int64_t Offset = 0;
4218 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4219 FI = FINode->getIndex();
4220 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004221 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004222 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4223 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4224 Offset = Ptr.getConstantOperandVal(1);
4225 Ptr = Ptr.getOperand(0);
4226 } else {
4227 return SDValue();
4228 }
4229
4230 SDValue Chain = LD->getChain();
4231 // Make sure the stack object alignment is at least 16.
4232 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4233 if (DAG.InferPtrAlignment(Ptr) < 16) {
4234 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004235 // Can't change the alignment. FIXME: It's possible to compute
4236 // the exact stack offset and reference FI + adjust offset instead.
4237 // If someone *really* cares about this. That's the way to implement it.
4238 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004239 } else {
4240 MFI->setObjectAlignment(FI, 16);
4241 }
4242 }
4243
4244 // (Offset % 16) must be multiple of 4. Then address is then
4245 // Ptr + (Offset & ~15).
4246 if (Offset < 0)
4247 return SDValue();
4248 if ((Offset % 16) & 3)
4249 return SDValue();
4250 int64_t StartOffset = Offset & ~15;
4251 if (StartOffset)
4252 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4253 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4254
4255 int EltNo = (Offset - StartOffset) >> 2;
4256 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4257 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004258 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4259 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004260 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004261 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004262 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4263 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004264 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004265 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004266 }
4267
4268 return SDValue();
4269}
4270
Michael J. Spencerec38de22010-10-10 22:04:20 +00004271/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4272/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004273/// load which has the same value as a build_vector whose operands are 'elts'.
4274///
4275/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004276///
Nate Begeman1449f292010-03-24 22:19:06 +00004277/// FIXME: we'd also like to handle the case where the last elements are zero
4278/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4279/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004280static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004281 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004282 EVT EltVT = VT.getVectorElementType();
4283 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004284
Nate Begemanfdea31a2010-03-24 20:49:50 +00004285 LoadSDNode *LDBase = NULL;
4286 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004287
Nate Begeman1449f292010-03-24 22:19:06 +00004288 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004289 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004290 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004291 for (unsigned i = 0; i < NumElems; ++i) {
4292 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004293
Nate Begemanfdea31a2010-03-24 20:49:50 +00004294 if (!Elt.getNode() ||
4295 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4296 return SDValue();
4297 if (!LDBase) {
4298 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4299 return SDValue();
4300 LDBase = cast<LoadSDNode>(Elt.getNode());
4301 LastLoadedElt = i;
4302 continue;
4303 }
4304 if (Elt.getOpcode() == ISD::UNDEF)
4305 continue;
4306
4307 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4308 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4309 return SDValue();
4310 LastLoadedElt = i;
4311 }
Nate Begeman1449f292010-03-24 22:19:06 +00004312
4313 // If we have found an entire vector of loads and undefs, then return a large
4314 // load of the entire vector width starting at the base pointer. If we found
4315 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004316 if (LastLoadedElt == NumElems - 1) {
4317 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004318 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004319 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004320 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004321 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004322 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004323 LDBase->isVolatile(), LDBase->isNonTemporal(),
4324 LDBase->getAlignment());
4325 } else if (NumElems == 4 && LastLoadedElt == 1) {
4326 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4327 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004328 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4329 Ops, 2, MVT::i32,
4330 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004331 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004332 }
4333 return SDValue();
4334}
4335
Evan Chengc3630942009-12-09 21:00:30 +00004336SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004337X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004338 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004339
David Greenef125a292011-02-08 19:04:41 +00004340 EVT VT = Op.getValueType();
4341 EVT ExtVT = VT.getVectorElementType();
4342
4343 unsigned NumElems = Op.getNumOperands();
4344
4345 // For AVX-length vectors, build the individual 128-bit pieces and
4346 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004347 if (VT.getSizeInBits() > 256 &&
4348 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004349 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4350 SmallVector<SDValue, 8> V;
4351 V.resize(NumElems);
4352 for (unsigned i = 0; i < NumElems; ++i) {
4353 V[i] = Op.getOperand(i);
4354 }
Owen Anderson95771af2011-02-25 21:41:48 +00004355
David Greenef125a292011-02-08 19:04:41 +00004356 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4357
4358 // Build the lower subvector.
4359 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4360 // Build the upper subvector.
4361 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4362 NumElems/2);
4363
4364 return ConcatVectors(Lower, Upper, DAG);
4365 }
4366
Chris Lattner6e80e442010-08-28 17:15:43 +00004367 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4368 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004369 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4370 // is present, so AllOnes is ignored.
4371 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4372 (Op.getValueType().getSizeInBits() != 256 &&
4373 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004374 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004375 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4376 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004377 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004378 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004379
Gabor Greifba36cb52008-08-28 21:40:38 +00004380 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004381 return getOnesVector(Op.getValueType(), DAG, dl);
4382 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004383 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384
Owen Andersone50ed302009-08-10 22:56:29 +00004385 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004386
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 unsigned NumZero = 0;
4388 unsigned NumNonZero = 0;
4389 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004390 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004391 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004392 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004394 if (Elt.getOpcode() == ISD::UNDEF)
4395 continue;
4396 Values.insert(Elt);
4397 if (Elt.getOpcode() != ISD::Constant &&
4398 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004399 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004400 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004401 NumZero++;
4402 else {
4403 NonZeros |= (1 << i);
4404 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004405 }
4406 }
4407
Chris Lattner97a2a562010-08-26 05:24:29 +00004408 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4409 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004410 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004411
Chris Lattner67f453a2008-03-09 05:42:06 +00004412 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004413 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004414 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004415 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004416
Chris Lattner62098042008-03-09 01:05:04 +00004417 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4418 // the value are obviously zero, truncate the value to i32 and do the
4419 // insertion that way. Only do this if the value is non-constant or if the
4420 // value is a constant being inserted into element 0. It is cheaper to do
4421 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004423 (!IsAllConstants || Idx == 0)) {
4424 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004425 // Handle SSE only.
4426 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4427 EVT VecVT = MVT::v4i32;
4428 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004429
Chris Lattner62098042008-03-09 01:05:04 +00004430 // Truncate the value (which may itself be a constant) to i32, and
4431 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004433 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004434 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4435 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004436
Chris Lattner62098042008-03-09 01:05:04 +00004437 // Now we have our 32-bit value zero extended in the low element of
4438 // a vector. If Idx != 0, swizzle it into place.
4439 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 SmallVector<int, 4> Mask;
4441 Mask.push_back(Idx);
4442 for (unsigned i = 1; i != VecElts; ++i)
4443 Mask.push_back(i);
4444 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004445 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004447 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004448 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004449 }
4450 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004451
Chris Lattner19f79692008-03-08 22:59:52 +00004452 // If we have a constant or non-constant insertion into the low element of
4453 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4454 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004455 // depending on what the source datatype is.
4456 if (Idx == 0) {
4457 if (NumZero == 0) {
4458 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004459 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4460 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004461 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4462 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4463 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4464 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004465 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4466 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004467 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4468 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004469 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4470 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4471 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004472 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004473 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004474 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004475
4476 // Is it a vector logical left shift?
4477 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004478 X86::isZeroNode(Op.getOperand(0)) &&
4479 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004480 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004481 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004482 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004483 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004484 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004485 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004486
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004487 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004488 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004489
Chris Lattner19f79692008-03-08 22:59:52 +00004490 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4491 // is a non-constant being inserted into an element other than the low one,
4492 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4493 // movd/movss) to move this into the low element, then shuffle it into
4494 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004495 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004496 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004497
Evan Cheng0db9fe62006-04-25 20:13:52 +00004498 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004499 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4500 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004502 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004503 MaskVec.push_back(i == Idx ? 0 : 1);
4504 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004505 }
4506 }
4507
Chris Lattner67f453a2008-03-09 05:42:06 +00004508 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004509 if (Values.size() == 1) {
4510 if (EVTBits == 32) {
4511 // Instead of a shuffle like this:
4512 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4513 // Check if it's possible to issue this instead.
4514 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4515 unsigned Idx = CountTrailingZeros_32(NonZeros);
4516 SDValue Item = Op.getOperand(Idx);
4517 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4518 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4519 }
Dan Gohman475871a2008-07-27 21:46:04 +00004520 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004521 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004522
Dan Gohmana3941172007-07-24 22:55:08 +00004523 // A vector full of immediates; various special cases are already
4524 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004525 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004526 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004527
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004528 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004529 if (EVTBits == 64) {
4530 if (NumNonZero == 1) {
4531 // One half is zero or undef.
4532 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004533 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004534 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004535 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4536 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004537 }
Dan Gohman475871a2008-07-27 21:46:04 +00004538 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004539 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004540
4541 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004542 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004543 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004544 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004545 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004546 }
4547
Bill Wendling826f36f2007-03-28 00:57:11 +00004548 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004549 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004550 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004551 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 }
4553
4554 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004555 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004556 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557 if (NumElems == 4 && NumZero > 0) {
4558 for (unsigned i = 0; i < 4; ++i) {
4559 bool isZero = !(NonZeros & (1 << i));
4560 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004561 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004562 else
Dale Johannesenace16102009-02-03 19:33:06 +00004563 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004564 }
4565
4566 for (unsigned i = 0; i < 2; ++i) {
4567 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4568 default: break;
4569 case 0:
4570 V[i] = V[i*2]; // Must be a zero vector.
4571 break;
4572 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004573 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004574 break;
4575 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004576 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004577 break;
4578 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004579 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004580 break;
4581 }
4582 }
4583
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004585 bool Reverse = (NonZeros & 0x3) == 2;
4586 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004587 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004588 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4589 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004590 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4591 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592 }
4593
Nate Begemanfdea31a2010-03-24 20:49:50 +00004594 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4595 // Check for a build vector of consecutive loads.
4596 for (unsigned i = 0; i < NumElems; ++i)
4597 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004598
Nate Begemanfdea31a2010-03-24 20:49:50 +00004599 // Check for elements which are consecutive loads.
4600 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4601 if (LD.getNode())
4602 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004603
4604 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004605 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004606 SDValue Result;
4607 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4608 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4609 else
4610 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004611
Chris Lattner24faf612010-08-28 17:59:08 +00004612 for (unsigned i = 1; i < NumElems; ++i) {
4613 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4614 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004615 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004616 }
4617 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004619
Chris Lattner6e80e442010-08-28 17:15:43 +00004620 // Otherwise, expand into a number of unpckl*, start by extending each of
4621 // our (non-undef) elements to the full vector width with the element in the
4622 // bottom slot of the vector (which generates no code for SSE).
4623 for (unsigned i = 0; i < NumElems; ++i) {
4624 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4625 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4626 else
4627 V[i] = DAG.getUNDEF(VT);
4628 }
4629
4630 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004631 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4632 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4633 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004634 unsigned EltStride = NumElems >> 1;
4635 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004636 for (unsigned i = 0; i < EltStride; ++i) {
4637 // If V[i+EltStride] is undef and this is the first round of mixing,
4638 // then it is safe to just drop this shuffle: V[i] is already in the
4639 // right place, the one element (since it's the first round) being
4640 // inserted as undef can be dropped. This isn't safe for successive
4641 // rounds because they will permute elements within both vectors.
4642 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4643 EltStride == NumElems/2)
4644 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004645
Chris Lattner6e80e442010-08-28 17:15:43 +00004646 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004647 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004648 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649 }
4650 return V[0];
4651 }
Dan Gohman475871a2008-07-27 21:46:04 +00004652 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653}
4654
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004655SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004656X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004657 // We support concatenate two MMX registers and place them in a MMX
4658 // register. This is better than doing a stack convert.
4659 DebugLoc dl = Op.getDebugLoc();
4660 EVT ResVT = Op.getValueType();
4661 assert(Op.getNumOperands() == 2);
4662 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4663 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4664 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004665 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004666 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4667 InVec = Op.getOperand(1);
4668 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4669 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004670 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004671 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4672 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4673 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004674 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004675 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4676 Mask[0] = 0; Mask[1] = 2;
4677 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4678 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004679 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004680}
4681
Nate Begemanb9a47b82009-02-23 08:49:38 +00004682// v8i16 shuffles - Prefer shuffles in the following order:
4683// 1. [all] pshuflw, pshufhw, optional move
4684// 2. [ssse3] 1 x pshufb
4685// 3. [ssse3] 2 x pshufb + 1 x por
4686// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004687SDValue
4688X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4689 SelectionDAG &DAG) const {
4690 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 SDValue V1 = SVOp->getOperand(0);
4692 SDValue V2 = SVOp->getOperand(1);
4693 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004694 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004695
Nate Begemanb9a47b82009-02-23 08:49:38 +00004696 // Determine if more than 1 of the words in each of the low and high quadwords
4697 // of the result come from the same quadword of one of the two inputs. Undef
4698 // mask values count as coming from any quadword, for better codegen.
4699 SmallVector<unsigned, 4> LoQuad(4);
4700 SmallVector<unsigned, 4> HiQuad(4);
4701 BitVector InputQuads(4);
4702 for (unsigned i = 0; i < 8; ++i) {
4703 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004704 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004705 MaskVals.push_back(EltIdx);
4706 if (EltIdx < 0) {
4707 ++Quad[0];
4708 ++Quad[1];
4709 ++Quad[2];
4710 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004711 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004712 }
4713 ++Quad[EltIdx / 4];
4714 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004715 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004716
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004718 unsigned MaxQuad = 1;
4719 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004720 if (LoQuad[i] > MaxQuad) {
4721 BestLoQuad = i;
4722 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004723 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004724 }
4725
Nate Begemanb9a47b82009-02-23 08:49:38 +00004726 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004727 MaxQuad = 1;
4728 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 if (HiQuad[i] > MaxQuad) {
4730 BestHiQuad = i;
4731 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004732 }
4733 }
4734
Nate Begemanb9a47b82009-02-23 08:49:38 +00004735 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004736 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004737 // single pshufb instruction is necessary. If There are more than 2 input
4738 // quads, disable the next transformation since it does not help SSSE3.
4739 bool V1Used = InputQuads[0] || InputQuads[1];
4740 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004741 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004742 if (InputQuads.count() == 2 && V1Used && V2Used) {
4743 BestLoQuad = InputQuads.find_first();
4744 BestHiQuad = InputQuads.find_next(BestLoQuad);
4745 }
4746 if (InputQuads.count() > 2) {
4747 BestLoQuad = -1;
4748 BestHiQuad = -1;
4749 }
4750 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004751
Nate Begemanb9a47b82009-02-23 08:49:38 +00004752 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4753 // the shuffle mask. If a quad is scored as -1, that means that it contains
4754 // words from all 4 input quadwords.
4755 SDValue NewV;
4756 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004757 SmallVector<int, 8> MaskV;
4758 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4759 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004760 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004761 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4762 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4763 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004764
Nate Begemanb9a47b82009-02-23 08:49:38 +00004765 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4766 // source words for the shuffle, to aid later transformations.
4767 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004768 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004769 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004770 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004771 if (idx != (int)i)
4772 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004773 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004774 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004775 AllWordsInNewV = false;
4776 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004777 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004778
Nate Begemanb9a47b82009-02-23 08:49:38 +00004779 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4780 if (AllWordsInNewV) {
4781 for (int i = 0; i != 8; ++i) {
4782 int idx = MaskVals[i];
4783 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004784 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004785 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004786 if ((idx != i) && idx < 4)
4787 pshufhw = false;
4788 if ((idx != i) && idx > 3)
4789 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004790 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 V1 = NewV;
4792 V2Used = false;
4793 BestLoQuad = 0;
4794 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004795 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004796
Nate Begemanb9a47b82009-02-23 08:49:38 +00004797 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4798 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004799 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004800 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4801 unsigned TargetMask = 0;
4802 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004804 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4805 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4806 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004807 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004808 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004809 }
Eric Christopherfd179292009-08-27 18:07:15 +00004810
Nate Begemanb9a47b82009-02-23 08:49:38 +00004811 // If we have SSSE3, and all words of the result are from 1 input vector,
4812 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4813 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004814 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004816
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004818 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 // mask, and elements that come from V1 in the V2 mask, so that the two
4820 // results can be OR'd together.
4821 bool TwoInputs = V1Used && V2Used;
4822 for (unsigned i = 0; i != 8; ++i) {
4823 int EltIdx = MaskVals[i] * 2;
4824 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4826 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004827 continue;
4828 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4830 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004832 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004833 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004834 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004837 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Nate Begemanb9a47b82009-02-23 08:49:38 +00004839 // Calculate the shuffle mask for the second input, shuffle it, and
4840 // OR it with the first shuffled input.
4841 pshufbMask.clear();
4842 for (unsigned i = 0; i != 8; ++i) {
4843 int EltIdx = MaskVals[i] * 2;
4844 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4846 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 continue;
4848 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4850 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004851 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004852 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004853 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004854 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004855 MVT::v16i8, &pshufbMask[0], 16));
4856 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004857 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 }
4859
4860 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4861 // and update MaskVals with new element order.
4862 BitVector InOrder(8);
4863 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004865 for (int i = 0; i != 4; ++i) {
4866 int idx = MaskVals[i];
4867 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004868 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004869 InOrder.set(i);
4870 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004871 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004872 InOrder.set(i);
4873 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004874 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004875 }
4876 }
4877 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004878 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004879 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004880 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004881
4882 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4883 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4884 NewV.getOperand(0),
4885 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4886 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004887 }
Eric Christopherfd179292009-08-27 18:07:15 +00004888
Nate Begemanb9a47b82009-02-23 08:49:38 +00004889 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4890 // and update MaskVals with the new element order.
4891 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004892 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004893 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004894 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 for (unsigned i = 4; i != 8; ++i) {
4896 int idx = MaskVals[i];
4897 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004898 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004899 InOrder.set(i);
4900 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004901 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004902 InOrder.set(i);
4903 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004904 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004905 }
4906 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004908 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004909
4910 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4911 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4912 NewV.getOperand(0),
4913 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4914 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004915 }
Eric Christopherfd179292009-08-27 18:07:15 +00004916
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 // In case BestHi & BestLo were both -1, which means each quadword has a word
4918 // from each of the four input quadwords, calculate the InOrder bitvector now
4919 // before falling through to the insert/extract cleanup.
4920 if (BestLoQuad == -1 && BestHiQuad == -1) {
4921 NewV = V1;
4922 for (int i = 0; i != 8; ++i)
4923 if (MaskVals[i] < 0 || MaskVals[i] == i)
4924 InOrder.set(i);
4925 }
Eric Christopherfd179292009-08-27 18:07:15 +00004926
Nate Begemanb9a47b82009-02-23 08:49:38 +00004927 // The other elements are put in the right place using pextrw and pinsrw.
4928 for (unsigned i = 0; i != 8; ++i) {
4929 if (InOrder[i])
4930 continue;
4931 int EltIdx = MaskVals[i];
4932 if (EltIdx < 0)
4933 continue;
4934 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004936 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004938 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004939 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004940 DAG.getIntPtrConstant(i));
4941 }
4942 return NewV;
4943}
4944
4945// v16i8 shuffles - Prefer shuffles in the following order:
4946// 1. [ssse3] 1 x pshufb
4947// 2. [ssse3] 2 x pshufb + 1 x por
4948// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4949static
Nate Begeman9008ca62009-04-27 18:41:29 +00004950SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004951 SelectionDAG &DAG,
4952 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004953 SDValue V1 = SVOp->getOperand(0);
4954 SDValue V2 = SVOp->getOperand(1);
4955 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004956 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004957 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004958
Nate Begemanb9a47b82009-02-23 08:49:38 +00004959 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004960 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004961 // present, fall back to case 3.
4962 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4963 bool V1Only = true;
4964 bool V2Only = true;
4965 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004966 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004967 if (EltIdx < 0)
4968 continue;
4969 if (EltIdx < 16)
4970 V2Only = false;
4971 else
4972 V1Only = false;
4973 }
Eric Christopherfd179292009-08-27 18:07:15 +00004974
Nate Begemanb9a47b82009-02-23 08:49:38 +00004975 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4976 if (TLI.getSubtarget()->hasSSSE3()) {
4977 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004978
Nate Begemanb9a47b82009-02-23 08:49:38 +00004979 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004980 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004981 //
4982 // Otherwise, we have elements from both input vectors, and must zero out
4983 // elements that come from V2 in the first mask, and V1 in the second mask
4984 // so that we can OR them together.
4985 bool TwoInputs = !(V1Only || V2Only);
4986 for (unsigned i = 0; i != 16; ++i) {
4987 int EltIdx = MaskVals[i];
4988 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004989 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004990 continue;
4991 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004992 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004993 }
4994 // If all the elements are from V2, assign it to V1 and return after
4995 // building the first pshufb.
4996 if (V2Only)
4997 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004999 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005001 if (!TwoInputs)
5002 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005003
Nate Begemanb9a47b82009-02-23 08:49:38 +00005004 // Calculate the shuffle mask for the second input, shuffle it, and
5005 // OR it with the first shuffled input.
5006 pshufbMask.clear();
5007 for (unsigned i = 0; i != 16; ++i) {
5008 int EltIdx = MaskVals[i];
5009 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005010 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 continue;
5012 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005013 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005014 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005015 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005016 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005017 MVT::v16i8, &pshufbMask[0], 16));
5018 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005019 }
Eric Christopherfd179292009-08-27 18:07:15 +00005020
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 // No SSSE3 - Calculate in place words and then fix all out of place words
5022 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5023 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005024 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5025 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005026 SDValue NewV = V2Only ? V2 : V1;
5027 for (int i = 0; i != 8; ++i) {
5028 int Elt0 = MaskVals[i*2];
5029 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005030
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 // This word of the result is all undef, skip it.
5032 if (Elt0 < 0 && Elt1 < 0)
5033 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005034
Nate Begemanb9a47b82009-02-23 08:49:38 +00005035 // This word of the result is already in the correct place, skip it.
5036 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5037 continue;
5038 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5039 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005040
Nate Begemanb9a47b82009-02-23 08:49:38 +00005041 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5042 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5043 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005044
5045 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5046 // using a single extract together, load it and store it.
5047 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005049 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005050 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005051 DAG.getIntPtrConstant(i));
5052 continue;
5053 }
5054
Nate Begemanb9a47b82009-02-23 08:49:38 +00005055 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005056 // source byte is not also odd, shift the extracted word left 8 bits
5057 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005058 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005059 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005060 DAG.getIntPtrConstant(Elt1 / 2));
5061 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005062 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005063 DAG.getConstant(8,
5064 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005065 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005066 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5067 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005068 }
5069 // If Elt0 is defined, extract it from the appropriate source. If the
5070 // source byte is not also even, shift the extracted word right 8 bits. If
5071 // Elt1 was also defined, OR the extracted values together before
5072 // inserting them in the result.
5073 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005075 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5076 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005077 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005078 DAG.getConstant(8,
5079 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005080 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005081 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5082 DAG.getConstant(0x00FF, MVT::i16));
5083 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005084 : InsElt0;
5085 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005086 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005087 DAG.getIntPtrConstant(i));
5088 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005089 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005090}
5091
Evan Cheng7a831ce2007-12-15 03:00:47 +00005092/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005093/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005094/// done when every pair / quad of shuffle mask elements point to elements in
5095/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005096/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005097static
Nate Begeman9008ca62009-04-27 18:41:29 +00005098SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005099 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005100 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005101 SDValue V1 = SVOp->getOperand(0);
5102 SDValue V2 = SVOp->getOperand(1);
5103 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005104 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005105 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005106 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005107 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 case MVT::v4f32: NewVT = MVT::v2f64; break;
5109 case MVT::v4i32: NewVT = MVT::v2i64; break;
5110 case MVT::v8i16: NewVT = MVT::v4i32; break;
5111 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005112 }
5113
Nate Begeman9008ca62009-04-27 18:41:29 +00005114 int Scale = NumElems / NewWidth;
5115 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005116 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005117 int StartIdx = -1;
5118 for (int j = 0; j < Scale; ++j) {
5119 int EltIdx = SVOp->getMaskElt(i+j);
5120 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005121 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005122 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005123 StartIdx = EltIdx - (EltIdx % Scale);
5124 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005125 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005126 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005127 if (StartIdx == -1)
5128 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005129 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005130 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005131 }
5132
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005133 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5134 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005135 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005136}
5137
Evan Chengd880b972008-05-09 21:53:03 +00005138/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005139///
Owen Andersone50ed302009-08-10 22:56:29 +00005140static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005141 SDValue SrcOp, SelectionDAG &DAG,
5142 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005144 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005145 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005146 LD = dyn_cast<LoadSDNode>(SrcOp);
5147 if (!LD) {
5148 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5149 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005150 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005151 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005152 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005153 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005154 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005155 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005156 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005157 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005158 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5159 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5160 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005161 SrcOp.getOperand(0)
5162 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005163 }
5164 }
5165 }
5166
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005167 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005168 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005169 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005170 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005171}
5172
Evan Chengace3c172008-07-22 21:13:36 +00005173/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5174/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005175static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005176LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5177 SDValue V1 = SVOp->getOperand(0);
5178 SDValue V2 = SVOp->getOperand(1);
5179 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005180 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005181
Evan Chengace3c172008-07-22 21:13:36 +00005182 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005183 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005184 SmallVector<int, 8> Mask1(4U, -1);
5185 SmallVector<int, 8> PermMask;
5186 SVOp->getMask(PermMask);
5187
Evan Chengace3c172008-07-22 21:13:36 +00005188 unsigned NumHi = 0;
5189 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005190 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005191 int Idx = PermMask[i];
5192 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005193 Locs[i] = std::make_pair(-1, -1);
5194 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005195 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5196 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005197 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005198 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005199 NumLo++;
5200 } else {
5201 Locs[i] = std::make_pair(1, NumHi);
5202 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005203 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005204 NumHi++;
5205 }
5206 }
5207 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005208
Evan Chengace3c172008-07-22 21:13:36 +00005209 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005210 // If no more than two elements come from either vector. This can be
5211 // implemented with two shuffles. First shuffle gather the elements.
5212 // The second shuffle, which takes the first shuffle as both of its
5213 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005215
Nate Begeman9008ca62009-04-27 18:41:29 +00005216 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005217
Evan Chengace3c172008-07-22 21:13:36 +00005218 for (unsigned i = 0; i != 4; ++i) {
5219 if (Locs[i].first == -1)
5220 continue;
5221 else {
5222 unsigned Idx = (i < 2) ? 0 : 4;
5223 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005224 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005225 }
5226 }
5227
Nate Begeman9008ca62009-04-27 18:41:29 +00005228 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005229 } else if (NumLo == 3 || NumHi == 3) {
5230 // Otherwise, we must have three elements from one vector, call it X, and
5231 // one element from the other, call it Y. First, use a shufps to build an
5232 // intermediate vector with the one element from Y and the element from X
5233 // that will be in the same half in the final destination (the indexes don't
5234 // matter). Then, use a shufps to build the final vector, taking the half
5235 // containing the element from Y from the intermediate, and the other half
5236 // from X.
5237 if (NumHi == 3) {
5238 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005239 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005240 std::swap(V1, V2);
5241 }
5242
5243 // Find the element from V2.
5244 unsigned HiIndex;
5245 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005246 int Val = PermMask[HiIndex];
5247 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005248 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005249 if (Val >= 4)
5250 break;
5251 }
5252
Nate Begeman9008ca62009-04-27 18:41:29 +00005253 Mask1[0] = PermMask[HiIndex];
5254 Mask1[1] = -1;
5255 Mask1[2] = PermMask[HiIndex^1];
5256 Mask1[3] = -1;
5257 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005258
5259 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005260 Mask1[0] = PermMask[0];
5261 Mask1[1] = PermMask[1];
5262 Mask1[2] = HiIndex & 1 ? 6 : 4;
5263 Mask1[3] = HiIndex & 1 ? 4 : 6;
5264 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005265 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005266 Mask1[0] = HiIndex & 1 ? 2 : 0;
5267 Mask1[1] = HiIndex & 1 ? 0 : 2;
5268 Mask1[2] = PermMask[2];
5269 Mask1[3] = PermMask[3];
5270 if (Mask1[2] >= 0)
5271 Mask1[2] += 4;
5272 if (Mask1[3] >= 0)
5273 Mask1[3] += 4;
5274 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005275 }
Evan Chengace3c172008-07-22 21:13:36 +00005276 }
5277
5278 // Break it into (shuffle shuffle_hi, shuffle_lo).
5279 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005280 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 SmallVector<int,8> LoMask(4U, -1);
5282 SmallVector<int,8> HiMask(4U, -1);
5283
5284 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005285 unsigned MaskIdx = 0;
5286 unsigned LoIdx = 0;
5287 unsigned HiIdx = 2;
5288 for (unsigned i = 0; i != 4; ++i) {
5289 if (i == 2) {
5290 MaskPtr = &HiMask;
5291 MaskIdx = 1;
5292 LoIdx = 0;
5293 HiIdx = 2;
5294 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005295 int Idx = PermMask[i];
5296 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005297 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005298 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005299 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005300 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005301 LoIdx++;
5302 } else {
5303 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005305 HiIdx++;
5306 }
5307 }
5308
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5310 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5311 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005312 for (unsigned i = 0; i != 4; ++i) {
5313 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005314 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005315 } else {
5316 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005317 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005318 }
5319 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005321}
5322
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005323static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005324 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005325 V = V.getOperand(0);
5326 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5327 V = V.getOperand(0);
5328 if (MayFoldLoad(V))
5329 return true;
5330 return false;
5331}
5332
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005333// FIXME: the version above should always be used. Since there's
5334// a bug where several vector shuffles can't be folded because the
5335// DAG is not updated during lowering and a node claims to have two
5336// uses while it only has one, use this version, and let isel match
5337// another instruction if the load really happens to have more than
5338// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005339// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005340static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005341 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005342 V = V.getOperand(0);
5343 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5344 V = V.getOperand(0);
5345 if (ISD::isNormalLoad(V.getNode()))
5346 return true;
5347 return false;
5348}
5349
5350/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5351/// a vector extract, and if both can be later optimized into a single load.
5352/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5353/// here because otherwise a target specific shuffle node is going to be
5354/// emitted for this shuffle, and the optimization not done.
5355/// FIXME: This is probably not the best approach, but fix the problem
5356/// until the right path is decided.
5357static
5358bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5359 const TargetLowering &TLI) {
5360 EVT VT = V.getValueType();
5361 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5362
5363 // Be sure that the vector shuffle is present in a pattern like this:
5364 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5365 if (!V.hasOneUse())
5366 return false;
5367
5368 SDNode *N = *V.getNode()->use_begin();
5369 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5370 return false;
5371
5372 SDValue EltNo = N->getOperand(1);
5373 if (!isa<ConstantSDNode>(EltNo))
5374 return false;
5375
5376 // If the bit convert changed the number of elements, it is unsafe
5377 // to examine the mask.
5378 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005379 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005380 EVT SrcVT = V.getOperand(0).getValueType();
5381 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5382 return false;
5383 V = V.getOperand(0);
5384 HasShuffleIntoBitcast = true;
5385 }
5386
5387 // Select the input vector, guarding against out of range extract vector.
5388 unsigned NumElems = VT.getVectorNumElements();
5389 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5390 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5391 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5392
5393 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005394 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005395 V = V.getOperand(0);
5396
5397 if (ISD::isNormalLoad(V.getNode())) {
5398 // Is the original load suitable?
5399 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5400
5401 // FIXME: avoid the multi-use bug that is preventing lots of
5402 // of foldings to be detected, this is still wrong of course, but
5403 // give the temporary desired behavior, and if it happens that
5404 // the load has real more uses, during isel it will not fold, and
5405 // will generate poor code.
5406 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5407 return false;
5408
5409 if (!HasShuffleIntoBitcast)
5410 return true;
5411
5412 // If there's a bitcast before the shuffle, check if the load type and
5413 // alignment is valid.
5414 unsigned Align = LN0->getAlignment();
5415 unsigned NewAlign =
5416 TLI.getTargetData()->getABITypeAlignment(
5417 VT.getTypeForEVT(*DAG.getContext()));
5418
5419 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5420 return false;
5421 }
5422
5423 return true;
5424}
5425
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005426static
Evan Cheng835580f2010-10-07 20:50:20 +00005427SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5428 EVT VT = Op.getValueType();
5429
5430 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005431 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5432 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005433 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5434 V1, DAG));
5435}
5436
5437static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005438SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5439 bool HasSSE2) {
5440 SDValue V1 = Op.getOperand(0);
5441 SDValue V2 = Op.getOperand(1);
5442 EVT VT = Op.getValueType();
5443
5444 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5445
5446 if (HasSSE2 && VT == MVT::v2f64)
5447 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5448
5449 // v4f32 or v4i32
5450 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5451}
5452
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005453static
5454SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5455 SDValue V1 = Op.getOperand(0);
5456 SDValue V2 = Op.getOperand(1);
5457 EVT VT = Op.getValueType();
5458
5459 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5460 "unsupported shuffle type");
5461
5462 if (V2.getOpcode() == ISD::UNDEF)
5463 V2 = V1;
5464
5465 // v4i32 or v4f32
5466 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5467}
5468
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005469static
5470SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5471 SDValue V1 = Op.getOperand(0);
5472 SDValue V2 = Op.getOperand(1);
5473 EVT VT = Op.getValueType();
5474 unsigned NumElems = VT.getVectorNumElements();
5475
5476 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5477 // operand of these instructions is only memory, so check if there's a
5478 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5479 // same masks.
5480 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005481
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005482 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005483 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005484 CanFoldLoad = true;
5485
5486 // When V1 is a load, it can be folded later into a store in isel, example:
5487 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5488 // turns into:
5489 // (MOVLPSmr addr:$src1, VR128:$src2)
5490 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005491 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005492 CanFoldLoad = true;
5493
Eric Christopher893a8822011-02-20 05:04:42 +00005494 // Both of them can't be memory operations though.
5495 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5496 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005497
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005498 if (CanFoldLoad) {
5499 if (HasSSE2 && NumElems == 2)
5500 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5501
5502 if (NumElems == 4)
5503 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5504 }
5505
5506 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5507 // movl and movlp will both match v2i64, but v2i64 is never matched by
5508 // movl earlier because we make it strict to avoid messing with the movlp load
5509 // folding logic (see the code above getMOVLP call). Match it here then,
5510 // this is horrible, but will stay like this until we move all shuffle
5511 // matching to x86 specific nodes. Note that for the 1st condition all
5512 // types are matched with movsd.
5513 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5514 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5515 else if (HasSSE2)
5516 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5517
5518
5519 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5520
5521 // Invert the operand order and use SHUFPS to match it.
5522 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5523 X86::getShuffleSHUFImmediate(SVOp), DAG);
5524}
5525
David Greenec4db4e52011-02-28 19:06:56 +00005526static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005527 switch(VT.getSimpleVT().SimpleTy) {
5528 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5529 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005530 case MVT::v4f32:
5531 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5532 case MVT::v2f64:
5533 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5534 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5535 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005536 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5537 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5538 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005539 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005540 }
5541 return 0;
5542}
5543
5544static inline unsigned getUNPCKHOpcode(EVT VT) {
5545 switch(VT.getSimpleVT().SimpleTy) {
5546 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5547 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5548 case MVT::v4f32: return X86ISD::UNPCKHPS;
5549 case MVT::v2f64: return X86ISD::UNPCKHPD;
5550 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5551 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5552 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005553 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005554 }
5555 return 0;
5556}
5557
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005558static
5559SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005560 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005561 const X86Subtarget *Subtarget) {
5562 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5563 EVT VT = Op.getValueType();
5564 DebugLoc dl = Op.getDebugLoc();
5565 SDValue V1 = Op.getOperand(0);
5566 SDValue V2 = Op.getOperand(1);
5567
5568 if (isZeroShuffle(SVOp))
5569 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5570
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005571 // Handle splat operations
5572 if (SVOp->isSplat()) {
5573 // Special case, this is the only place now where it's
5574 // allowed to return a vector_shuffle operation without
5575 // using a target specific node, because *hopefully* it
5576 // will be optimized away by the dag combiner.
5577 if (VT.getVectorNumElements() <= 4 &&
5578 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5579 return Op;
5580
5581 // Handle splats by matching through known masks
5582 if (VT.getVectorNumElements() <= 4)
5583 return SDValue();
5584
Evan Cheng835580f2010-10-07 20:50:20 +00005585 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005586 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005587 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005588
5589 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5590 // do it!
5591 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5592 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5593 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005594 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005595 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5596 // FIXME: Figure out a cleaner way to do this.
5597 // Try to make use of movq to zero out the top part.
5598 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5599 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5600 if (NewOp.getNode()) {
5601 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5602 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5603 DAG, Subtarget, dl);
5604 }
5605 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5606 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5607 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5608 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5609 DAG, Subtarget, dl);
5610 }
5611 }
5612 return SDValue();
5613}
5614
Dan Gohman475871a2008-07-27 21:46:04 +00005615SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005616X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005617 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005618 SDValue V1 = Op.getOperand(0);
5619 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005620 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005621 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005622 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005624 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5625 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005626 bool V1IsSplat = false;
5627 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005628 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005629 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005630 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005631 MachineFunction &MF = DAG.getMachineFunction();
5632 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005633
Dale Johannesen0488fb62010-09-30 23:57:10 +00005634 // Shuffle operations on MMX not supported.
5635 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005636 return Op;
5637
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005638 // Vector shuffle lowering takes 3 steps:
5639 //
5640 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5641 // narrowing and commutation of operands should be handled.
5642 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5643 // shuffle nodes.
5644 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5645 // so the shuffle can be broken into other shuffles and the legalizer can
5646 // try the lowering again.
5647 //
5648 // The general ideia is that no vector_shuffle operation should be left to
5649 // be matched during isel, all of them must be converted to a target specific
5650 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005651
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005652 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5653 // narrowing and commutation of operands should be handled. The actual code
5654 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005655 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005656 if (NewOp.getNode())
5657 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005658
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005659 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5660 // unpckh_undef). Only use pshufd if speed is more important than size.
5661 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5662 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005663 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005664 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5665 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5666 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005667
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005668 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005669 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005670 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005671
Dale Johannesen0488fb62010-09-30 23:57:10 +00005672 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005673 return getMOVHighToLow(Op, dl, DAG);
5674
5675 // Use to match splats
5676 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5677 (VT == MVT::v2f64 || VT == MVT::v2i64))
5678 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5679
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005680 if (X86::isPSHUFDMask(SVOp)) {
5681 // The actual implementation will match the mask in the if above and then
5682 // during isel it can match several different instructions, not only pshufd
5683 // as its name says, sad but true, emulate the behavior for now...
5684 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5685 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5686
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005687 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5688
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005689 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005690 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5691
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005692 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005693 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5694 TargetMask, DAG);
5695
5696 if (VT == MVT::v4f32)
5697 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5698 TargetMask, DAG);
5699 }
Eric Christopherfd179292009-08-27 18:07:15 +00005700
Evan Chengf26ffe92008-05-29 08:22:04 +00005701 // Check if this can be converted into a logical shift.
5702 bool isLeft = false;
5703 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005704 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005705 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005706 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005707 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005708 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005709 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005710 EVT EltVT = VT.getVectorElementType();
5711 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005712 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005713 }
Eric Christopherfd179292009-08-27 18:07:15 +00005714
Nate Begeman9008ca62009-04-27 18:41:29 +00005715 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005716 if (V1IsUndef)
5717 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005718 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005719 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005720 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005721 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005722 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5723
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005724 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005725 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5726 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005727 }
Eric Christopherfd179292009-08-27 18:07:15 +00005728
Nate Begeman9008ca62009-04-27 18:41:29 +00005729 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005730 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5731 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005732
Dale Johannesen0488fb62010-09-30 23:57:10 +00005733 if (X86::isMOVHLPSMask(SVOp))
5734 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005735
Dale Johannesen0488fb62010-09-30 23:57:10 +00005736 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5737 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005738
Dale Johannesen0488fb62010-09-30 23:57:10 +00005739 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5740 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005741
Dale Johannesen0488fb62010-09-30 23:57:10 +00005742 if (X86::isMOVLPMask(SVOp))
5743 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744
Nate Begeman9008ca62009-04-27 18:41:29 +00005745 if (ShouldXformToMOVHLPS(SVOp) ||
5746 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5747 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748
Evan Chengf26ffe92008-05-29 08:22:04 +00005749 if (isShift) {
5750 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005751 EVT EltVT = VT.getVectorElementType();
5752 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005753 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005754 }
Eric Christopherfd179292009-08-27 18:07:15 +00005755
Evan Cheng9eca5e82006-10-25 21:49:50 +00005756 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005757 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5758 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005759 V1IsSplat = isSplatVector(V1.getNode());
5760 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005761
Chris Lattner8a594482007-11-25 00:24:49 +00005762 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005763 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005764 Op = CommuteVectorShuffle(SVOp, DAG);
5765 SVOp = cast<ShuffleVectorSDNode>(Op);
5766 V1 = SVOp->getOperand(0);
5767 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005768 std::swap(V1IsSplat, V2IsSplat);
5769 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005770 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005771 }
5772
Nate Begeman9008ca62009-04-27 18:41:29 +00005773 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5774 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005775 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005776 return V1;
5777 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5778 // the instruction selector will not match, so get a canonical MOVL with
5779 // swapped operands to undo the commute.
5780 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005781 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005782
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005783 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005784 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5785 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005786
5787 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005788 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005789
Evan Cheng9bbbb982006-10-25 20:48:19 +00005790 if (V2IsSplat) {
5791 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005792 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005793 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005794 SDValue NewMask = NormalizeMask(SVOp, DAG);
5795 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5796 if (NSVOp != SVOp) {
5797 if (X86::isUNPCKLMask(NSVOp, true)) {
5798 return NewMask;
5799 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5800 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005801 }
5802 }
5803 }
5804
Evan Cheng9eca5e82006-10-25 21:49:50 +00005805 if (Commuted) {
5806 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005807 // FIXME: this seems wrong.
5808 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5809 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005810
5811 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005812 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5813 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005814
5815 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005816 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005817 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005818
Nate Begeman9008ca62009-04-27 18:41:29 +00005819 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005820 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005821 return CommuteVectorShuffle(SVOp, DAG);
5822
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005823 // The checks below are all present in isShuffleMaskLegal, but they are
5824 // inlined here right now to enable us to directly emit target specific
5825 // nodes, and remove one by one until they don't return Op anymore.
5826 SmallVector<int, 16> M;
5827 SVOp->getMask(M);
5828
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005829 if (isPALIGNRMask(M, VT, HasSSSE3))
5830 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5831 X86::getShufflePALIGNRImmediate(SVOp),
5832 DAG);
5833
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005834 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5835 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005836 if (VT == MVT::v2f64) {
5837 X86ISD::NodeType Opcode =
5838 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5839 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5840 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005841 if (VT == MVT::v2i64)
5842 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5843 }
5844
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005845 if (isPSHUFHWMask(M, VT))
5846 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5847 X86::getShufflePSHUFHWImmediate(SVOp),
5848 DAG);
5849
5850 if (isPSHUFLWMask(M, VT))
5851 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5852 X86::getShufflePSHUFLWImmediate(SVOp),
5853 DAG);
5854
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005855 if (isSHUFPMask(M, VT)) {
5856 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5857 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5858 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5859 TargetMask, DAG);
5860 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5861 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5862 TargetMask, DAG);
5863 }
5864
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005865 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5866 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005867 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5868 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005869 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5870 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5871 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5872
Evan Cheng14b32e12007-12-11 01:46:18 +00005873 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005875 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005876 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005877 return NewOp;
5878 }
5879
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005881 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 if (NewOp.getNode())
5883 return NewOp;
5884 }
Eric Christopherfd179292009-08-27 18:07:15 +00005885
Dale Johannesen0488fb62010-09-30 23:57:10 +00005886 // Handle all 4 wide cases with a number of shuffles.
5887 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005888 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005889
Dan Gohman475871a2008-07-27 21:46:04 +00005890 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005891}
5892
Dan Gohman475871a2008-07-27 21:46:04 +00005893SDValue
5894X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005895 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005896 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005897 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005898 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005900 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005901 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005902 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005903 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005904 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005905 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5906 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5907 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005908 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5909 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005912 Op.getOperand(0)),
5913 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005914 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005915 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005917 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005918 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005920 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5921 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005922 // result has a single use which is a store or a bitcast to i32. And in
5923 // the case of a store, it's not worth it if the index is a constant 0,
5924 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005925 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005926 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005927 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005928 if ((User->getOpcode() != ISD::STORE ||
5929 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5930 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005931 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005933 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005935 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005936 Op.getOperand(0)),
5937 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005938 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005940 // ExtractPS works with constant index.
5941 if (isa<ConstantSDNode>(Op.getOperand(1)))
5942 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005943 }
Dan Gohman475871a2008-07-27 21:46:04 +00005944 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005945}
5946
5947
Dan Gohman475871a2008-07-27 21:46:04 +00005948SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005949X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5950 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005951 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005952 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005953
David Greene74a579d2011-02-10 16:57:36 +00005954 SDValue Vec = Op.getOperand(0);
5955 EVT VecVT = Vec.getValueType();
5956
5957 // If this is a 256-bit vector result, first extract the 128-bit
5958 // vector and then extract from the 128-bit vector.
5959 if (VecVT.getSizeInBits() > 128) {
5960 DebugLoc dl = Op.getNode()->getDebugLoc();
5961 unsigned NumElems = VecVT.getVectorNumElements();
5962 SDValue Idx = Op.getOperand(1);
5963
5964 if (!isa<ConstantSDNode>(Idx))
5965 return SDValue();
5966
5967 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5968 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5969
5970 // Get the 128-bit vector.
5971 bool Upper = IdxVal >= ExtractNumElems;
5972 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5973
5974 // Extract from it.
5975 SDValue ScaledIdx = Idx;
5976 if (Upper)
5977 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5978 DAG.getConstant(ExtractNumElems,
5979 Idx.getValueType()));
5980 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5981 ScaledIdx);
5982 }
5983
5984 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
5985
Evan Cheng62a3f152008-03-24 21:52:23 +00005986 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005987 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005988 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005989 return Res;
5990 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005991
Owen Andersone50ed302009-08-10 22:56:29 +00005992 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005993 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005994 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005995 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005996 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005997 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005998 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6000 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006001 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006003 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006004 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006005 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006006 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006007 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006008 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006009 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006010 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006011 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006012 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006013 if (Idx == 0)
6014 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006015
Evan Cheng0db9fe62006-04-25 20:13:52 +00006016 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006017 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006018 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006019 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006020 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006021 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006022 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006023 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006024 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6025 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6026 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006027 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006028 if (Idx == 0)
6029 return Op;
6030
6031 // UNPCKHPD the element to the lowest double word, then movsd.
6032 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6033 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006034 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006035 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006036 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006037 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006038 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006039 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006040 }
6041
Dan Gohman475871a2008-07-27 21:46:04 +00006042 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006043}
6044
Dan Gohman475871a2008-07-27 21:46:04 +00006045SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006046X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6047 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006048 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006049 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006050 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006051
Dan Gohman475871a2008-07-27 21:46:04 +00006052 SDValue N0 = Op.getOperand(0);
6053 SDValue N1 = Op.getOperand(1);
6054 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006055
Dan Gohman8a55ce42009-09-23 21:02:20 +00006056 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006057 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006058 unsigned Opc;
6059 if (VT == MVT::v8i16)
6060 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006061 else if (VT == MVT::v16i8)
6062 Opc = X86ISD::PINSRB;
6063 else
6064 Opc = X86ISD::PINSRB;
6065
Nate Begeman14d12ca2008-02-11 04:19:36 +00006066 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6067 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 if (N1.getValueType() != MVT::i32)
6069 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6070 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006071 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006072 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006073 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006074 // Bits [7:6] of the constant are the source select. This will always be
6075 // zero here. The DAG Combiner may combine an extract_elt index into these
6076 // bits. For example (insert (extract, 3), 2) could be matched by putting
6077 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006078 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006079 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006080 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006081 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006082 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006083 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006084 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006085 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006086 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006087 // PINSR* works with constant index.
6088 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006089 }
Dan Gohman475871a2008-07-27 21:46:04 +00006090 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006091}
6092
Dan Gohman475871a2008-07-27 21:46:04 +00006093SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006094X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006095 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006096 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006097
David Greene6b381262011-02-09 15:32:06 +00006098 DebugLoc dl = Op.getDebugLoc();
6099 SDValue N0 = Op.getOperand(0);
6100 SDValue N1 = Op.getOperand(1);
6101 SDValue N2 = Op.getOperand(2);
6102
6103 // If this is a 256-bit vector result, first insert into a 128-bit
6104 // vector and then insert into the 256-bit vector.
6105 if (VT.getSizeInBits() > 128) {
6106 if (!isa<ConstantSDNode>(N2))
6107 return SDValue();
6108
6109 // Get the 128-bit vector.
6110 unsigned NumElems = VT.getVectorNumElements();
6111 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6112 bool Upper = IdxVal >= NumElems / 2;
6113
6114 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6115
6116 // Insert into it.
6117 SDValue ScaledN2 = N2;
6118 if (Upper)
6119 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006120 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006121 (VT.getSizeInBits() / 128),
6122 N2.getValueType()));
6123 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6124 N1, ScaledN2);
6125
6126 // Insert the 128-bit vector
6127 // FIXME: Why UNDEF?
6128 return Insert128BitVector(N0, Op, N2, DAG, dl);
6129 }
6130
Nate Begeman14d12ca2008-02-11 04:19:36 +00006131 if (Subtarget->hasSSE41())
6132 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6133
Dan Gohman8a55ce42009-09-23 21:02:20 +00006134 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006135 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006136
Dan Gohman8a55ce42009-09-23 21:02:20 +00006137 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006138 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6139 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006140 if (N1.getValueType() != MVT::i32)
6141 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6142 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006143 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006144 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006145 }
Dan Gohman475871a2008-07-27 21:46:04 +00006146 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006147}
6148
Dan Gohman475871a2008-07-27 21:46:04 +00006149SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006150X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006151 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006152 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006153 EVT OpVT = Op.getValueType();
6154
6155 // If this is a 256-bit vector result, first insert into a 128-bit
6156 // vector and then insert into the 256-bit vector.
6157 if (OpVT.getSizeInBits() > 128) {
6158 // Insert into a 128-bit vector.
6159 EVT VT128 = EVT::getVectorVT(*Context,
6160 OpVT.getVectorElementType(),
6161 OpVT.getVectorNumElements() / 2);
6162
6163 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6164
6165 // Insert the 128-bit vector.
6166 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6167 DAG.getConstant(0, MVT::i32),
6168 DAG, dl);
6169 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006170
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006171 if (Op.getValueType() == MVT::v1i64 &&
6172 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006173 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006174
Owen Anderson825b72b2009-08-11 20:47:22 +00006175 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006176 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6177 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006178 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006179 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006180}
6181
David Greene91585092011-01-26 15:38:49 +00006182// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6183// a simple subregister reference or explicit instructions to grab
6184// upper bits of a vector.
6185SDValue
6186X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6187 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006188 DebugLoc dl = Op.getNode()->getDebugLoc();
6189 SDValue Vec = Op.getNode()->getOperand(0);
6190 SDValue Idx = Op.getNode()->getOperand(1);
6191
6192 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6193 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6194 return Extract128BitVector(Vec, Idx, DAG, dl);
6195 }
David Greene91585092011-01-26 15:38:49 +00006196 }
6197 return SDValue();
6198}
6199
David Greenecfe33c42011-01-26 19:13:22 +00006200// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6201// simple superregister reference or explicit instructions to insert
6202// the upper bits of a vector.
6203SDValue
6204X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6205 if (Subtarget->hasAVX()) {
6206 DebugLoc dl = Op.getNode()->getDebugLoc();
6207 SDValue Vec = Op.getNode()->getOperand(0);
6208 SDValue SubVec = Op.getNode()->getOperand(1);
6209 SDValue Idx = Op.getNode()->getOperand(2);
6210
6211 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6212 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006213 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006214 }
6215 }
6216 return SDValue();
6217}
6218
Bill Wendling056292f2008-09-16 21:48:12 +00006219// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6220// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6221// one of the above mentioned nodes. It has to be wrapped because otherwise
6222// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6223// be used to form addressing mode. These wrapped nodes will be selected
6224// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006225SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006226X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006227 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006228
Chris Lattner41621a22009-06-26 19:22:52 +00006229 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6230 // global base reg.
6231 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006232 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006233 CodeModel::Model M = getTargetMachine().getCodeModel();
6234
Chris Lattner4f066492009-07-11 20:29:19 +00006235 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006236 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006237 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006238 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006239 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006240 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006241 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006242
Evan Cheng1606e8e2009-03-13 07:51:59 +00006243 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006244 CP->getAlignment(),
6245 CP->getOffset(), OpFlag);
6246 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006247 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006248 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006249 if (OpFlag) {
6250 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006251 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006252 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006253 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006254 }
6255
6256 return Result;
6257}
6258
Dan Gohmand858e902010-04-17 15:26:15 +00006259SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006260 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006261
Chris Lattner18c59872009-06-27 04:16:01 +00006262 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6263 // global base reg.
6264 unsigned char OpFlag = 0;
6265 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006266 CodeModel::Model M = getTargetMachine().getCodeModel();
6267
Chris Lattner4f066492009-07-11 20:29:19 +00006268 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006269 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006270 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006271 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006272 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006273 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006274 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006275
Chris Lattner18c59872009-06-27 04:16:01 +00006276 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6277 OpFlag);
6278 DebugLoc DL = JT->getDebugLoc();
6279 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006280
Chris Lattner18c59872009-06-27 04:16:01 +00006281 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006282 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006283 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6284 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006285 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006286 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006287
Chris Lattner18c59872009-06-27 04:16:01 +00006288 return Result;
6289}
6290
6291SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006292X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006293 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006294
Chris Lattner18c59872009-06-27 04:16:01 +00006295 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6296 // global base reg.
6297 unsigned char OpFlag = 0;
6298 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006299 CodeModel::Model M = getTargetMachine().getCodeModel();
6300
Chris Lattner4f066492009-07-11 20:29:19 +00006301 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006302 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006303 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006304 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006305 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006306 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006307 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006308
Chris Lattner18c59872009-06-27 04:16:01 +00006309 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006310
Chris Lattner18c59872009-06-27 04:16:01 +00006311 DebugLoc DL = Op.getDebugLoc();
6312 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006313
6314
Chris Lattner18c59872009-06-27 04:16:01 +00006315 // With PIC, the address is actually $g + Offset.
6316 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006317 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006318 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6319 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006320 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006321 Result);
6322 }
Eric Christopherfd179292009-08-27 18:07:15 +00006323
Chris Lattner18c59872009-06-27 04:16:01 +00006324 return Result;
6325}
6326
Dan Gohman475871a2008-07-27 21:46:04 +00006327SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006328X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006329 // Create the TargetBlockAddressAddress node.
6330 unsigned char OpFlags =
6331 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006332 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006333 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006334 DebugLoc dl = Op.getDebugLoc();
6335 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6336 /*isTarget=*/true, OpFlags);
6337
Dan Gohmanf705adb2009-10-30 01:28:02 +00006338 if (Subtarget->isPICStyleRIPRel() &&
6339 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006340 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6341 else
6342 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006343
Dan Gohman29cbade2009-11-20 23:18:13 +00006344 // With PIC, the address is actually $g + Offset.
6345 if (isGlobalRelativeToPICBase(OpFlags)) {
6346 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6347 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6348 Result);
6349 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006350
6351 return Result;
6352}
6353
6354SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006355X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006356 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006357 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006358 // Create the TargetGlobalAddress node, folding in the constant
6359 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006360 unsigned char OpFlags =
6361 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006362 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006363 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006364 if (OpFlags == X86II::MO_NO_FLAG &&
6365 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006366 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006367 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006368 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006369 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006370 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006371 }
Eric Christopherfd179292009-08-27 18:07:15 +00006372
Chris Lattner4f066492009-07-11 20:29:19 +00006373 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006374 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006375 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6376 else
6377 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006378
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006379 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006380 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006381 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6382 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006383 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006385
Chris Lattner36c25012009-07-10 07:34:39 +00006386 // For globals that require a load from a stub to get the address, emit the
6387 // load.
6388 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006389 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006390 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006391
Dan Gohman6520e202008-10-18 02:06:02 +00006392 // If there was a non-zero offset that we didn't fold, create an explicit
6393 // addition for it.
6394 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006395 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006396 DAG.getConstant(Offset, getPointerTy()));
6397
Evan Cheng0db9fe62006-04-25 20:13:52 +00006398 return Result;
6399}
6400
Evan Chengda43bcf2008-09-24 00:05:32 +00006401SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006402X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006403 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006404 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006405 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006406}
6407
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006408static SDValue
6409GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006410 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006411 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006412 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006413 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006414 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006415 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006416 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006417 GA->getOffset(),
6418 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006419 if (InFlag) {
6420 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006421 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006422 } else {
6423 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006424 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006425 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006426
6427 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006428 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006429
Rafael Espindola15f1b662009-04-24 12:59:40 +00006430 SDValue Flag = Chain.getValue(1);
6431 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006432}
6433
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006434// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006435static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006436LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006437 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006438 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006439 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6440 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006441 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006442 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006443 InFlag = Chain.getValue(1);
6444
Chris Lattnerb903bed2009-06-26 21:20:29 +00006445 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006446}
6447
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006448// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006449static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006450LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006451 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006452 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6453 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006454}
6455
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006456// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6457// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006458static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006459 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006460 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006461 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006462
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006463 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6464 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6465 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006466
Michael J. Spencerec38de22010-10-10 22:04:20 +00006467 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006468 DAG.getIntPtrConstant(0),
6469 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006470
Chris Lattnerb903bed2009-06-26 21:20:29 +00006471 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006472 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6473 // initialexec.
6474 unsigned WrapperKind = X86ISD::Wrapper;
6475 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006476 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006477 } else if (is64Bit) {
6478 assert(model == TLSModel::InitialExec);
6479 OperandFlags = X86II::MO_GOTTPOFF;
6480 WrapperKind = X86ISD::WrapperRIP;
6481 } else {
6482 assert(model == TLSModel::InitialExec);
6483 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006484 }
Eric Christopherfd179292009-08-27 18:07:15 +00006485
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006486 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6487 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006488 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006489 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006490 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006491 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006492
Rafael Espindola9a580232009-02-27 13:37:18 +00006493 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006494 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006495 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006496
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006497 // The address of the thread local variable is the add of the thread
6498 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006499 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006500}
6501
Dan Gohman475871a2008-07-27 21:46:04 +00006502SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006503X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006504
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006505 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006506 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006507
Eric Christopher30ef0e52010-06-03 04:07:48 +00006508 if (Subtarget->isTargetELF()) {
6509 // TODO: implement the "local dynamic" model
6510 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006511
Eric Christopher30ef0e52010-06-03 04:07:48 +00006512 // If GV is an alias then use the aliasee for determining
6513 // thread-localness.
6514 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6515 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006516
6517 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006518 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006519
Eric Christopher30ef0e52010-06-03 04:07:48 +00006520 switch (model) {
6521 case TLSModel::GeneralDynamic:
6522 case TLSModel::LocalDynamic: // not implemented
6523 if (Subtarget->is64Bit())
6524 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6525 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006526
Eric Christopher30ef0e52010-06-03 04:07:48 +00006527 case TLSModel::InitialExec:
6528 case TLSModel::LocalExec:
6529 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6530 Subtarget->is64Bit());
6531 }
6532 } else if (Subtarget->isTargetDarwin()) {
6533 // Darwin only has one model of TLS. Lower to that.
6534 unsigned char OpFlag = 0;
6535 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6536 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006537
Eric Christopher30ef0e52010-06-03 04:07:48 +00006538 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6539 // global base reg.
6540 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6541 !Subtarget->is64Bit();
6542 if (PIC32)
6543 OpFlag = X86II::MO_TLVP_PIC_BASE;
6544 else
6545 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006546 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006547 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006548 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006549 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006550 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006551
Eric Christopher30ef0e52010-06-03 04:07:48 +00006552 // With PIC32, the address is actually $g + Offset.
6553 if (PIC32)
6554 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6555 DAG.getNode(X86ISD::GlobalBaseReg,
6556 DebugLoc(), getPointerTy()),
6557 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006558
Eric Christopher30ef0e52010-06-03 04:07:48 +00006559 // Lowering the machine isd will make sure everything is in the right
6560 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006561 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006562 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006563 SDValue Args[] = { Chain, Offset };
6564 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006565
Eric Christopher30ef0e52010-06-03 04:07:48 +00006566 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6567 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6568 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006569
Eric Christopher30ef0e52010-06-03 04:07:48 +00006570 // And our return value (tls address) is in the standard call return value
6571 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006572 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6573 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006574 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006575
Eric Christopher30ef0e52010-06-03 04:07:48 +00006576 assert(false &&
6577 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006578
Torok Edwinc23197a2009-07-14 16:55:14 +00006579 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006580 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006581}
6582
Evan Cheng0db9fe62006-04-25 20:13:52 +00006583
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006584/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006585/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006586SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006587 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006588 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006589 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006590 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006591 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue ShOpLo = Op.getOperand(0);
6593 SDValue ShOpHi = Op.getOperand(1);
6594 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006595 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006596 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006597 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006598
Dan Gohman475871a2008-07-27 21:46:04 +00006599 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006600 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006601 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6602 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006603 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006604 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6605 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006606 }
Evan Chenge3413162006-01-09 18:33:28 +00006607
Owen Anderson825b72b2009-08-11 20:47:22 +00006608 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6609 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006610 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006611 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006612
Dan Gohman475871a2008-07-27 21:46:04 +00006613 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006614 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6616 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006617
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006618 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006619 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6620 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006621 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006622 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6623 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006624 }
6625
Dan Gohman475871a2008-07-27 21:46:04 +00006626 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006627 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006628}
Evan Chenga3195e82006-01-12 22:54:21 +00006629
Dan Gohmand858e902010-04-17 15:26:15 +00006630SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6631 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006632 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006633
Dale Johannesen0488fb62010-09-30 23:57:10 +00006634 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006635 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006636
Owen Anderson825b72b2009-08-11 20:47:22 +00006637 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006638 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006639
Eli Friedman36df4992009-05-27 00:47:34 +00006640 // These are really Legal; return the operand so the caller accepts it as
6641 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006642 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006643 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006645 Subtarget->is64Bit()) {
6646 return Op;
6647 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006648
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006649 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006650 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006651 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006652 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006653 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006654 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006655 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006656 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006657 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006658 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6659}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006660
Owen Andersone50ed302009-08-10 22:56:29 +00006661SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006662 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006663 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006664 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006665 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006666 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006667 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006668 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006669 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006670 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006671 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006672
Chris Lattner492a43e2010-09-22 01:28:21 +00006673 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006674
Chris Lattner492a43e2010-09-22 01:28:21 +00006675 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6676 MachineMemOperand *MMO =
6677 DAG.getMachineFunction()
6678 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6679 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006680
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006681 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006682 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6683 X86ISD::FILD, DL,
6684 Tys, Ops, array_lengthof(Ops),
6685 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006687 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006689 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690
6691 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6692 // shouldn't be necessary except that RFP cannot be live across
6693 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006694 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006695 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6696 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006697 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006698 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006699 SDValue Ops[] = {
6700 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6701 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006702 MachineMemOperand *MMO =
6703 DAG.getMachineFunction()
6704 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006705 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006706
Chris Lattner492a43e2010-09-22 01:28:21 +00006707 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6708 Ops, array_lengthof(Ops),
6709 Op.getValueType(), MMO);
6710 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006711 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006712 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006713 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006714
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715 return Result;
6716}
6717
Bill Wendling8b8a6362009-01-17 03:56:04 +00006718// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006719SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6720 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006721 // This algorithm is not obvious. Here it is in C code, more or less:
6722 /*
6723 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6724 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6725 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006726
Bill Wendling8b8a6362009-01-17 03:56:04 +00006727 // Copy ints to xmm registers.
6728 __m128i xh = _mm_cvtsi32_si128( hi );
6729 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006730
Bill Wendling8b8a6362009-01-17 03:56:04 +00006731 // Combine into low half of a single xmm register.
6732 __m128i x = _mm_unpacklo_epi32( xh, xl );
6733 __m128d d;
6734 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006735
Bill Wendling8b8a6362009-01-17 03:56:04 +00006736 // Merge in appropriate exponents to give the integer bits the right
6737 // magnitude.
6738 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006739
Bill Wendling8b8a6362009-01-17 03:56:04 +00006740 // Subtract away the biases to deal with the IEEE-754 double precision
6741 // implicit 1.
6742 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006743
Bill Wendling8b8a6362009-01-17 03:56:04 +00006744 // All conversions up to here are exact. The correctly rounded result is
6745 // calculated using the current rounding mode using the following
6746 // horizontal add.
6747 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6748 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6749 // store doesn't really need to be here (except
6750 // maybe to zero the other double)
6751 return sd;
6752 }
6753 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006754
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006755 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006756 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006757
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006758 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006759 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006760 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6761 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6762 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6763 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006764 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006765 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006766
Bill Wendling8b8a6362009-01-17 03:56:04 +00006767 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006768 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006769 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
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, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006772 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006773 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006774
Owen Anderson825b72b2009-08-11 20:47:22 +00006775 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6776 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006777 Op.getOperand(0),
6778 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006779 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6780 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006781 Op.getOperand(0),
6782 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006783 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6784 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006785 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006786 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006787 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006788 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006789 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006790 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006791 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006792 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006793
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006794 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006795 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006796 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6797 DAG.getUNDEF(MVT::v2f64), ShufMask);
6798 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6799 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006800 DAG.getIntPtrConstant(0));
6801}
6802
Bill Wendling8b8a6362009-01-17 03:56:04 +00006803// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006804SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6805 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006806 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006807 // FP constant to bias correct the final result.
6808 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006810
6811 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006812 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6813 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006814 Op.getOperand(0),
6815 DAG.getIntPtrConstant(0)));
6816
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006818 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006819 DAG.getIntPtrConstant(0));
6820
6821 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006822 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006823 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006824 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006826 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006827 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006828 MVT::v2f64, Bias)));
6829 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006830 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006831 DAG.getIntPtrConstant(0));
6832
6833 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006834 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006835
6836 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006837 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006838
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006840 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006841 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006843 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006844 }
6845
6846 // Handle final rounding.
6847 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006848}
6849
Dan Gohmand858e902010-04-17 15:26:15 +00006850SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6851 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006852 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006853 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006854
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006855 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006856 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6857 // the optimization here.
6858 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006859 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006860
Owen Andersone50ed302009-08-10 22:56:29 +00006861 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006862 EVT DstVT = Op.getValueType();
6863 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006864 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006865 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006866 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006867
6868 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006870 if (SrcVT == MVT::i32) {
6871 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6872 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6873 getPointerTy(), StackSlot, WordOff);
6874 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006875 StackSlot, MachinePointerInfo(),
6876 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006877 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006878 OffsetSlot, MachinePointerInfo(),
6879 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006880 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6881 return Fild;
6882 }
6883
6884 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6885 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006886 StackSlot, MachinePointerInfo(),
6887 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006888 // For i64 source, we need to add the appropriate power of 2 if the input
6889 // was negative. This is the same as the optimization in
6890 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6891 // we must be careful to do the computation in x87 extended precision, not
6892 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006893 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6894 MachineMemOperand *MMO =
6895 DAG.getMachineFunction()
6896 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6897 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006898
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006899 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6900 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006901 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6902 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006903
6904 APInt FF(32, 0x5F800000ULL);
6905
6906 // Check whether the sign bit is set.
6907 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6908 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6909 ISD::SETLT);
6910
6911 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6912 SDValue FudgePtr = DAG.getConstantPool(
6913 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6914 getPointerTy());
6915
6916 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6917 SDValue Zero = DAG.getIntPtrConstant(0);
6918 SDValue Four = DAG.getIntPtrConstant(4);
6919 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6920 Zero, Four);
6921 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6922
6923 // Load the value out, extending it from f32 to f80.
6924 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006925 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006926 FudgePtr, MachinePointerInfo::getConstantPool(),
6927 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006928 // Extend everything to 80 bits to force it to be done on x87.
6929 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6930 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006931}
6932
Dan Gohman475871a2008-07-27 21:46:04 +00006933std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006934FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006935 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006936
Owen Andersone50ed302009-08-10 22:56:29 +00006937 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006938
6939 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6941 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006942 }
6943
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6945 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006946 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006948 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006949 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006950 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006951 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006952 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006953 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006954 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006955 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006956
Evan Cheng87c89352007-10-15 20:11:21 +00006957 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6958 // stack slot.
6959 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006960 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006961 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006962 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006963
Michael J. Spencerec38de22010-10-10 22:04:20 +00006964
6965
Evan Cheng0db9fe62006-04-25 20:13:52 +00006966 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006967 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006968 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006969 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6970 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6971 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006972 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006973
Dan Gohman475871a2008-07-27 21:46:04 +00006974 SDValue Chain = DAG.getEntryNode();
6975 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006976 EVT TheVT = Op.getOperand(0).getValueType();
6977 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006978 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006979 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006980 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006981 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006982 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006983 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006984 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006985 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006986
Chris Lattner492a43e2010-09-22 01:28:21 +00006987 MachineMemOperand *MMO =
6988 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6989 MachineMemOperand::MOLoad, MemSize, MemSize);
6990 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6991 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006992 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006993 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006994 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6995 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006996
Chris Lattner07290932010-09-22 01:05:16 +00006997 MachineMemOperand *MMO =
6998 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6999 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007000
Evan Cheng0db9fe62006-04-25 20:13:52 +00007001 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007002 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007003 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7004 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007005
Chris Lattner27a6c732007-11-24 07:07:01 +00007006 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007007}
7008
Dan Gohmand858e902010-04-17 15:26:15 +00007009SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7010 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007011 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007012 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007013
Eli Friedman948e95a2009-05-23 09:59:16 +00007014 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007016 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7017 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007018
Chris Lattner27a6c732007-11-24 07:07:01 +00007019 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007020 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007021 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007022}
7023
Dan Gohmand858e902010-04-17 15:26:15 +00007024SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7025 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007026 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7027 SDValue FIST = Vals.first, StackSlot = Vals.second;
7028 assert(FIST.getNode() && "Unexpected failure");
7029
7030 // Load the result.
7031 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007032 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007033}
7034
Dan Gohmand858e902010-04-17 15:26:15 +00007035SDValue X86TargetLowering::LowerFABS(SDValue Op,
7036 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007037 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007038 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007039 EVT VT = Op.getValueType();
7040 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007041 if (VT.isVector())
7042 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007043 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007044 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007045 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007046 CV.push_back(C);
7047 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007048 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007049 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007050 CV.push_back(C);
7051 CV.push_back(C);
7052 CV.push_back(C);
7053 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007054 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007055 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007056 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007057 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007058 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007059 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007060 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007061}
7062
Dan Gohmand858e902010-04-17 15:26:15 +00007063SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007064 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007065 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007066 EVT VT = Op.getValueType();
7067 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007068 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007069 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007070 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007071 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007072 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007073 CV.push_back(C);
7074 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007075 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007076 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007077 CV.push_back(C);
7078 CV.push_back(C);
7079 CV.push_back(C);
7080 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007081 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007082 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007083 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007084 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007085 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007086 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007087 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007088 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007089 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007090 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007091 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007092 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007093 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007094 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007095 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007096}
7097
Dan Gohmand858e902010-04-17 15:26:15 +00007098SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007099 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007100 SDValue Op0 = Op.getOperand(0);
7101 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007102 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007103 EVT VT = Op.getValueType();
7104 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007105
7106 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007107 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007108 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007109 SrcVT = VT;
7110 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007111 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007112 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007113 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007114 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007115 }
7116
7117 // At this point the operands and the result should have the same
7118 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007119
Evan Cheng68c47cb2007-01-05 07:55:56 +00007120 // First get the sign bit of second operand.
7121 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007122 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007123 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7124 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007125 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007126 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7127 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7128 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7129 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007130 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007131 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007132 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007133 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007134 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007135 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007136 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007137
7138 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007139 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 // Op0 is MVT::f32, Op1 is MVT::f64.
7141 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7142 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7143 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007144 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007145 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007146 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007147 }
7148
Evan Cheng73d6cf12007-01-05 21:37:56 +00007149 // Clear first operand sign bit.
7150 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007151 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007152 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7153 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007154 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007155 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7156 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7157 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7158 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007159 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007160 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007161 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007162 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007163 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007164 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007165 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007166
7167 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007168 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007169}
7170
Dan Gohman076aee32009-03-04 19:44:21 +00007171/// Emit nodes that will be selected as "test Op0,Op0", or something
7172/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007173SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007174 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007175 DebugLoc dl = Op.getDebugLoc();
7176
Dan Gohman31125812009-03-07 01:58:32 +00007177 // CF and OF aren't always set the way we want. Determine which
7178 // of these we need.
7179 bool NeedCF = false;
7180 bool NeedOF = false;
7181 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007182 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007183 case X86::COND_A: case X86::COND_AE:
7184 case X86::COND_B: case X86::COND_BE:
7185 NeedCF = true;
7186 break;
7187 case X86::COND_G: case X86::COND_GE:
7188 case X86::COND_L: case X86::COND_LE:
7189 case X86::COND_O: case X86::COND_NO:
7190 NeedOF = true;
7191 break;
Dan Gohman31125812009-03-07 01:58:32 +00007192 }
7193
Dan Gohman076aee32009-03-04 19:44:21 +00007194 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007195 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7196 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007197 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7198 // Emit a CMP with 0, which is the TEST pattern.
7199 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7200 DAG.getConstant(0, Op.getValueType()));
7201
7202 unsigned Opcode = 0;
7203 unsigned NumOperands = 0;
7204 switch (Op.getNode()->getOpcode()) {
7205 case ISD::ADD:
7206 // Due to an isel shortcoming, be conservative if this add is likely to be
7207 // selected as part of a load-modify-store instruction. When the root node
7208 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7209 // uses of other nodes in the match, such as the ADD in this case. This
7210 // leads to the ADD being left around and reselected, with the result being
7211 // two adds in the output. Alas, even if none our users are stores, that
7212 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7213 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7214 // climbing the DAG back to the root, and it doesn't seem to be worth the
7215 // effort.
7216 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007217 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007218 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7219 goto default_case;
7220
7221 if (ConstantSDNode *C =
7222 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7223 // An add of one will be selected as an INC.
7224 if (C->getAPIntValue() == 1) {
7225 Opcode = X86ISD::INC;
7226 NumOperands = 1;
7227 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007228 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007229
7230 // An add of negative one (subtract of one) will be selected as a DEC.
7231 if (C->getAPIntValue().isAllOnesValue()) {
7232 Opcode = X86ISD::DEC;
7233 NumOperands = 1;
7234 break;
7235 }
Dan Gohman076aee32009-03-04 19:44:21 +00007236 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007237
7238 // Otherwise use a regular EFLAGS-setting add.
7239 Opcode = X86ISD::ADD;
7240 NumOperands = 2;
7241 break;
7242 case ISD::AND: {
7243 // If the primary and result isn't used, don't bother using X86ISD::AND,
7244 // because a TEST instruction will be better.
7245 bool NonFlagUse = false;
7246 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7247 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7248 SDNode *User = *UI;
7249 unsigned UOpNo = UI.getOperandNo();
7250 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7251 // Look pass truncate.
7252 UOpNo = User->use_begin().getOperandNo();
7253 User = *User->use_begin();
7254 }
7255
7256 if (User->getOpcode() != ISD::BRCOND &&
7257 User->getOpcode() != ISD::SETCC &&
7258 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7259 NonFlagUse = true;
7260 break;
7261 }
Dan Gohman076aee32009-03-04 19:44:21 +00007262 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007263
7264 if (!NonFlagUse)
7265 break;
7266 }
7267 // FALL THROUGH
7268 case ISD::SUB:
7269 case ISD::OR:
7270 case ISD::XOR:
7271 // Due to the ISEL shortcoming noted above, be conservative if this op is
7272 // likely to be selected as part of a load-modify-store instruction.
7273 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7274 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7275 if (UI->getOpcode() == ISD::STORE)
7276 goto default_case;
7277
7278 // Otherwise use a regular EFLAGS-setting instruction.
7279 switch (Op.getNode()->getOpcode()) {
7280 default: llvm_unreachable("unexpected operator!");
7281 case ISD::SUB: Opcode = X86ISD::SUB; break;
7282 case ISD::OR: Opcode = X86ISD::OR; break;
7283 case ISD::XOR: Opcode = X86ISD::XOR; break;
7284 case ISD::AND: Opcode = X86ISD::AND; break;
7285 }
7286
7287 NumOperands = 2;
7288 break;
7289 case X86ISD::ADD:
7290 case X86ISD::SUB:
7291 case X86ISD::INC:
7292 case X86ISD::DEC:
7293 case X86ISD::OR:
7294 case X86ISD::XOR:
7295 case X86ISD::AND:
7296 return SDValue(Op.getNode(), 1);
7297 default:
7298 default_case:
7299 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007300 }
7301
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007302 if (Opcode == 0)
7303 // Emit a CMP with 0, which is the TEST pattern.
7304 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7305 DAG.getConstant(0, Op.getValueType()));
7306
7307 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7308 SmallVector<SDValue, 4> Ops;
7309 for (unsigned i = 0; i != NumOperands; ++i)
7310 Ops.push_back(Op.getOperand(i));
7311
7312 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7313 DAG.ReplaceAllUsesWith(Op, New);
7314 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007315}
7316
7317/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7318/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007319SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007320 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007321 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7322 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007323 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007324
7325 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007326 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007327}
7328
Evan Chengd40d03e2010-01-06 19:38:29 +00007329/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7330/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007331SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7332 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007333 SDValue Op0 = And.getOperand(0);
7334 SDValue Op1 = And.getOperand(1);
7335 if (Op0.getOpcode() == ISD::TRUNCATE)
7336 Op0 = Op0.getOperand(0);
7337 if (Op1.getOpcode() == ISD::TRUNCATE)
7338 Op1 = Op1.getOperand(0);
7339
Evan Chengd40d03e2010-01-06 19:38:29 +00007340 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007341 if (Op1.getOpcode() == ISD::SHL)
7342 std::swap(Op0, Op1);
7343 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007344 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7345 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007346 // If we looked past a truncate, check that it's only truncating away
7347 // known zeros.
7348 unsigned BitWidth = Op0.getValueSizeInBits();
7349 unsigned AndBitWidth = And.getValueSizeInBits();
7350 if (BitWidth > AndBitWidth) {
7351 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7352 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7353 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7354 return SDValue();
7355 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007356 LHS = Op1;
7357 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007358 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007359 } else if (Op1.getOpcode() == ISD::Constant) {
7360 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7361 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007362 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7363 LHS = AndLHS.getOperand(0);
7364 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007365 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007366 }
Evan Cheng0488db92007-09-25 01:57:46 +00007367
Evan Chengd40d03e2010-01-06 19:38:29 +00007368 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007369 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007370 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007371 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007372 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007373 // Also promote i16 to i32 for performance / code size reason.
7374 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007375 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007376 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007377
Evan Chengd40d03e2010-01-06 19:38:29 +00007378 // If the operand types disagree, extend the shift amount to match. Since
7379 // BT ignores high bits (like shifts) we can use anyextend.
7380 if (LHS.getValueType() != RHS.getValueType())
7381 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007382
Evan Chengd40d03e2010-01-06 19:38:29 +00007383 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7384 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7385 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7386 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007387 }
7388
Evan Cheng54de3ea2010-01-05 06:52:31 +00007389 return SDValue();
7390}
7391
Dan Gohmand858e902010-04-17 15:26:15 +00007392SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007393 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7394 SDValue Op0 = Op.getOperand(0);
7395 SDValue Op1 = Op.getOperand(1);
7396 DebugLoc dl = Op.getDebugLoc();
7397 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7398
7399 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007400 // Lower (X & (1 << N)) == 0 to BT(X, N).
7401 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7402 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007403 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007404 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007405 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007406 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7407 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7408 if (NewSetCC.getNode())
7409 return NewSetCC;
7410 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007411
Chris Lattner481eebc2010-12-19 21:23:48 +00007412 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7413 // these.
7414 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007415 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7416 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7417 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007418
Chris Lattner481eebc2010-12-19 21:23:48 +00007419 // If the input is a setcc, then reuse the input setcc or use a new one with
7420 // the inverted condition.
7421 if (Op0.getOpcode() == X86ISD::SETCC) {
7422 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7423 bool Invert = (CC == ISD::SETNE) ^
7424 cast<ConstantSDNode>(Op1)->isNullValue();
7425 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007426
Evan Cheng2c755ba2010-02-27 07:36:59 +00007427 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007428 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7429 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7430 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007431 }
7432
Evan Chenge5b51ac2010-04-17 06:13:15 +00007433 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007434 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007435 if (X86CC == X86::COND_INVALID)
7436 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007437
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007438 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007439 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007440 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007441}
7442
Dan Gohmand858e902010-04-17 15:26:15 +00007443SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007444 SDValue Cond;
7445 SDValue Op0 = Op.getOperand(0);
7446 SDValue Op1 = Op.getOperand(1);
7447 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007448 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007449 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7450 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007451 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007452
7453 if (isFP) {
7454 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007455 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7457 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007458 bool Swap = false;
7459
7460 switch (SetCCOpcode) {
7461 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007462 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007463 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007464 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007465 case ISD::SETGT: Swap = true; // Fallthrough
7466 case ISD::SETLT:
7467 case ISD::SETOLT: SSECC = 1; break;
7468 case ISD::SETOGE:
7469 case ISD::SETGE: Swap = true; // Fallthrough
7470 case ISD::SETLE:
7471 case ISD::SETOLE: SSECC = 2; break;
7472 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007473 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007474 case ISD::SETNE: SSECC = 4; break;
7475 case ISD::SETULE: Swap = true;
7476 case ISD::SETUGE: SSECC = 5; break;
7477 case ISD::SETULT: Swap = true;
7478 case ISD::SETUGT: SSECC = 6; break;
7479 case ISD::SETO: SSECC = 7; break;
7480 }
7481 if (Swap)
7482 std::swap(Op0, Op1);
7483
Nate Begemanfb8ead02008-07-25 19:05:58 +00007484 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007485 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007486 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007487 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7489 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007490 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007491 }
7492 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007493 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7495 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007496 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007497 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007498 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007499 }
7500 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007501 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007503
Nate Begeman30a0de92008-07-17 16:51:19 +00007504 // We are handling one of the integer comparisons here. Since SSE only has
7505 // GT and EQ comparisons for integer, swapping operands and multiple
7506 // operations may be required for some comparisons.
7507 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7508 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007509
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007511 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007513 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007514 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7515 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007517
Nate Begeman30a0de92008-07-17 16:51:19 +00007518 switch (SetCCOpcode) {
7519 default: break;
7520 case ISD::SETNE: Invert = true;
7521 case ISD::SETEQ: Opc = EQOpc; break;
7522 case ISD::SETLT: Swap = true;
7523 case ISD::SETGT: Opc = GTOpc; break;
7524 case ISD::SETGE: Swap = true;
7525 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7526 case ISD::SETULT: Swap = true;
7527 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7528 case ISD::SETUGE: Swap = true;
7529 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7530 }
7531 if (Swap)
7532 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007533
Nate Begeman30a0de92008-07-17 16:51:19 +00007534 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7535 // bits of the inputs before performing those operations.
7536 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007537 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007538 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7539 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007540 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007541 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7542 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007543 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7544 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007545 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007546
Dale Johannesenace16102009-02-03 19:33:06 +00007547 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007548
7549 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007550 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007551 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007552
Nate Begeman30a0de92008-07-17 16:51:19 +00007553 return Result;
7554}
Evan Cheng0488db92007-09-25 01:57:46 +00007555
Evan Cheng370e5342008-12-03 08:38:43 +00007556// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007557static bool isX86LogicalCmp(SDValue Op) {
7558 unsigned Opc = Op.getNode()->getOpcode();
7559 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7560 return true;
7561 if (Op.getResNo() == 1 &&
7562 (Opc == X86ISD::ADD ||
7563 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007564 Opc == X86ISD::ADC ||
7565 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007566 Opc == X86ISD::SMUL ||
7567 Opc == X86ISD::UMUL ||
7568 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007569 Opc == X86ISD::DEC ||
7570 Opc == X86ISD::OR ||
7571 Opc == X86ISD::XOR ||
7572 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007573 return true;
7574
Chris Lattner9637d5b2010-12-05 07:49:54 +00007575 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7576 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007577
Dan Gohman076aee32009-03-04 19:44:21 +00007578 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007579}
7580
Chris Lattnera2b56002010-12-05 01:23:24 +00007581static bool isZero(SDValue V) {
7582 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7583 return C && C->isNullValue();
7584}
7585
Chris Lattner96908b12010-12-05 02:00:51 +00007586static bool isAllOnes(SDValue V) {
7587 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7588 return C && C->isAllOnesValue();
7589}
7590
Dan Gohmand858e902010-04-17 15:26:15 +00007591SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007592 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007593 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007594 SDValue Op1 = Op.getOperand(1);
7595 SDValue Op2 = Op.getOperand(2);
7596 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007597 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007598
Dan Gohman1a492952009-10-20 16:22:37 +00007599 if (Cond.getOpcode() == ISD::SETCC) {
7600 SDValue NewCond = LowerSETCC(Cond, DAG);
7601 if (NewCond.getNode())
7602 Cond = NewCond;
7603 }
Evan Cheng734503b2006-09-11 02:19:56 +00007604
Chris Lattnera2b56002010-12-05 01:23:24 +00007605 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007606 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007607 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007608 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007609 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007610 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7611 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007612 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007613
Chris Lattnera2b56002010-12-05 01:23:24 +00007614 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007615
7616 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007617 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7618 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007619
7620 SDValue CmpOp0 = Cmp.getOperand(0);
7621 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7622 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007623
Chris Lattner96908b12010-12-05 02:00:51 +00007624 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007625 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7626 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007627
Chris Lattner96908b12010-12-05 02:00:51 +00007628 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7629 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007630
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007631 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007632 if (N2C == 0 || !N2C->isNullValue())
7633 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7634 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007635 }
7636 }
7637
Chris Lattnera2b56002010-12-05 01:23:24 +00007638 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007639 if (Cond.getOpcode() == ISD::AND &&
7640 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7641 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007642 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007643 Cond = Cond.getOperand(0);
7644 }
7645
Evan Cheng3f41d662007-10-08 22:16:29 +00007646 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7647 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007648 if (Cond.getOpcode() == X86ISD::SETCC ||
7649 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007650 CC = Cond.getOperand(0);
7651
Dan Gohman475871a2008-07-27 21:46:04 +00007652 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007653 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007654 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007655
Evan Cheng3f41d662007-10-08 22:16:29 +00007656 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007657 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007658 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007659 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007660
Chris Lattnerd1980a52009-03-12 06:52:53 +00007661 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7662 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007663 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007664 addTest = false;
7665 }
7666 }
7667
7668 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007669 // Look pass the truncate.
7670 if (Cond.getOpcode() == ISD::TRUNCATE)
7671 Cond = Cond.getOperand(0);
7672
7673 // We know the result of AND is compared against zero. Try to match
7674 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007675 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007676 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007677 if (NewSetCC.getNode()) {
7678 CC = NewSetCC.getOperand(0);
7679 Cond = NewSetCC.getOperand(1);
7680 addTest = false;
7681 }
7682 }
7683 }
7684
7685 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007686 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007687 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007688 }
7689
Benjamin Kramere915ff32010-12-22 23:09:28 +00007690 // a < b ? -1 : 0 -> RES = ~setcc_carry
7691 // a < b ? 0 : -1 -> RES = setcc_carry
7692 // a >= b ? -1 : 0 -> RES = setcc_carry
7693 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7694 if (Cond.getOpcode() == X86ISD::CMP) {
7695 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7696
7697 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7698 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7699 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7700 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7701 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7702 return DAG.getNOT(DL, Res, Res.getValueType());
7703 return Res;
7704 }
7705 }
7706
Evan Cheng0488db92007-09-25 01:57:46 +00007707 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7708 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007709 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007710 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007711 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007712}
7713
Evan Cheng370e5342008-12-03 08:38:43 +00007714// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7715// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7716// from the AND / OR.
7717static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7718 Opc = Op.getOpcode();
7719 if (Opc != ISD::OR && Opc != ISD::AND)
7720 return false;
7721 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7722 Op.getOperand(0).hasOneUse() &&
7723 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7724 Op.getOperand(1).hasOneUse());
7725}
7726
Evan Cheng961d6d42009-02-02 08:19:07 +00007727// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7728// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007729static bool isXor1OfSetCC(SDValue Op) {
7730 if (Op.getOpcode() != ISD::XOR)
7731 return false;
7732 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7733 if (N1C && N1C->getAPIntValue() == 1) {
7734 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7735 Op.getOperand(0).hasOneUse();
7736 }
7737 return false;
7738}
7739
Dan Gohmand858e902010-04-17 15:26:15 +00007740SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007741 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007742 SDValue Chain = Op.getOperand(0);
7743 SDValue Cond = Op.getOperand(1);
7744 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007745 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007746 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007747
Dan Gohman1a492952009-10-20 16:22:37 +00007748 if (Cond.getOpcode() == ISD::SETCC) {
7749 SDValue NewCond = LowerSETCC(Cond, DAG);
7750 if (NewCond.getNode())
7751 Cond = NewCond;
7752 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007753#if 0
7754 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007755 else if (Cond.getOpcode() == X86ISD::ADD ||
7756 Cond.getOpcode() == X86ISD::SUB ||
7757 Cond.getOpcode() == X86ISD::SMUL ||
7758 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007759 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007760#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007761
Evan Chengad9c0a32009-12-15 00:53:42 +00007762 // Look pass (and (setcc_carry (cmp ...)), 1).
7763 if (Cond.getOpcode() == ISD::AND &&
7764 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7765 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007766 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007767 Cond = Cond.getOperand(0);
7768 }
7769
Evan Cheng3f41d662007-10-08 22:16:29 +00007770 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7771 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007772 if (Cond.getOpcode() == X86ISD::SETCC ||
7773 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007774 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007775
Dan Gohman475871a2008-07-27 21:46:04 +00007776 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007777 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007778 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007779 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007780 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007781 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007782 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007783 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007784 default: break;
7785 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007786 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007787 // These can only come from an arithmetic instruction with overflow,
7788 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007789 Cond = Cond.getNode()->getOperand(1);
7790 addTest = false;
7791 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007792 }
Evan Cheng0488db92007-09-25 01:57:46 +00007793 }
Evan Cheng370e5342008-12-03 08:38:43 +00007794 } else {
7795 unsigned CondOpc;
7796 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7797 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007798 if (CondOpc == ISD::OR) {
7799 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7800 // two branches instead of an explicit OR instruction with a
7801 // separate test.
7802 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007803 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007804 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007805 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007806 Chain, Dest, CC, Cmp);
7807 CC = Cond.getOperand(1).getOperand(0);
7808 Cond = Cmp;
7809 addTest = false;
7810 }
7811 } else { // ISD::AND
7812 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7813 // two branches instead of an explicit AND instruction with a
7814 // separate test. However, we only do this if this block doesn't
7815 // have a fall-through edge, because this requires an explicit
7816 // jmp when the condition is false.
7817 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007818 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007819 Op.getNode()->hasOneUse()) {
7820 X86::CondCode CCode =
7821 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7822 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007823 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007824 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007825 // Look for an unconditional branch following this conditional branch.
7826 // We need this because we need to reverse the successors in order
7827 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007828 if (User->getOpcode() == ISD::BR) {
7829 SDValue FalseBB = User->getOperand(1);
7830 SDNode *NewBR =
7831 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007832 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007833 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007834 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007835
Dale Johannesene4d209d2009-02-03 20:21:25 +00007836 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007837 Chain, Dest, CC, Cmp);
7838 X86::CondCode CCode =
7839 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7840 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007841 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007842 Cond = Cmp;
7843 addTest = false;
7844 }
7845 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007846 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007847 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7848 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7849 // It should be transformed during dag combiner except when the condition
7850 // is set by a arithmetics with overflow node.
7851 X86::CondCode CCode =
7852 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7853 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007854 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007855 Cond = Cond.getOperand(0).getOperand(1);
7856 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007857 }
Evan Cheng0488db92007-09-25 01:57:46 +00007858 }
7859
7860 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007861 // Look pass the truncate.
7862 if (Cond.getOpcode() == ISD::TRUNCATE)
7863 Cond = Cond.getOperand(0);
7864
7865 // We know the result of AND is compared against zero. Try to match
7866 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007867 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007868 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7869 if (NewSetCC.getNode()) {
7870 CC = NewSetCC.getOperand(0);
7871 Cond = NewSetCC.getOperand(1);
7872 addTest = false;
7873 }
7874 }
7875 }
7876
7877 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007878 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007879 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007880 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007881 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007882 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007883}
7884
Anton Korobeynikove060b532007-04-17 19:34:00 +00007885
7886// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7887// Calls to _alloca is needed to probe the stack when allocating more than 4k
7888// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7889// that the guard pages used by the OS virtual memory manager are allocated in
7890// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007891SDValue
7892X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007893 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007894 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007895 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007896 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007897
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007898 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007899 SDValue Chain = Op.getOperand(0);
7900 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007901 // FIXME: Ensure alignment here
7902
Dan Gohman475871a2008-07-27 21:46:04 +00007903 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007904
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007906
Dale Johannesendd64c412009-02-04 00:33:20 +00007907 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007908 Flag = Chain.getValue(1);
7909
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007910 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007911
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007912 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007913 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007914
Dale Johannesendd64c412009-02-04 00:33:20 +00007915 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007916
Dan Gohman475871a2008-07-27 21:46:04 +00007917 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007918 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007919}
7920
Dan Gohmand858e902010-04-17 15:26:15 +00007921SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007922 MachineFunction &MF = DAG.getMachineFunction();
7923 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7924
Dan Gohman69de1932008-02-06 22:27:42 +00007925 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007926 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007927
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007928 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007929 // vastart just stores the address of the VarArgsFrameIndex slot into the
7930 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007931 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7932 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007933 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7934 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007935 }
7936
7937 // __va_list_tag:
7938 // gp_offset (0 - 6 * 8)
7939 // fp_offset (48 - 48 + 8 * 16)
7940 // overflow_arg_area (point to parameters coming in memory).
7941 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007942 SmallVector<SDValue, 8> MemOps;
7943 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007944 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007945 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007946 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7947 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007948 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007949 MemOps.push_back(Store);
7950
7951 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007952 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007953 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007954 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007955 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7956 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007957 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007958 MemOps.push_back(Store);
7959
7960 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007961 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007962 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007963 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7964 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007965 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7966 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007967 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007968 MemOps.push_back(Store);
7969
7970 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007971 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007972 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007973 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7974 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007975 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7976 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007977 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007978 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007979 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007980}
7981
Dan Gohmand858e902010-04-17 15:26:15 +00007982SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007983 assert(Subtarget->is64Bit() &&
7984 "LowerVAARG only handles 64-bit va_arg!");
7985 assert((Subtarget->isTargetLinux() ||
7986 Subtarget->isTargetDarwin()) &&
7987 "Unhandled target in LowerVAARG");
7988 assert(Op.getNode()->getNumOperands() == 4);
7989 SDValue Chain = Op.getOperand(0);
7990 SDValue SrcPtr = Op.getOperand(1);
7991 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7992 unsigned Align = Op.getConstantOperandVal(3);
7993 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007994
Dan Gohman320afb82010-10-12 18:00:49 +00007995 EVT ArgVT = Op.getNode()->getValueType(0);
7996 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7997 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7998 uint8_t ArgMode;
7999
8000 // Decide which area this value should be read from.
8001 // TODO: Implement the AMD64 ABI in its entirety. This simple
8002 // selection mechanism works only for the basic types.
8003 if (ArgVT == MVT::f80) {
8004 llvm_unreachable("va_arg for f80 not yet implemented");
8005 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8006 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8007 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8008 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8009 } else {
8010 llvm_unreachable("Unhandled argument type in LowerVAARG");
8011 }
8012
8013 if (ArgMode == 2) {
8014 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008015 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008016 !(DAG.getMachineFunction()
8017 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008018 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008019 }
8020
8021 // Insert VAARG_64 node into the DAG
8022 // VAARG_64 returns two values: Variable Argument Address, Chain
8023 SmallVector<SDValue, 11> InstOps;
8024 InstOps.push_back(Chain);
8025 InstOps.push_back(SrcPtr);
8026 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8027 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8028 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8029 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8030 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8031 VTs, &InstOps[0], InstOps.size(),
8032 MVT::i64,
8033 MachinePointerInfo(SV),
8034 /*Align=*/0,
8035 /*Volatile=*/false,
8036 /*ReadMem=*/true,
8037 /*WriteMem=*/true);
8038 Chain = VAARG.getValue(1);
8039
8040 // Load the next argument and return it
8041 return DAG.getLoad(ArgVT, dl,
8042 Chain,
8043 VAARG,
8044 MachinePointerInfo(),
8045 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008046}
8047
Dan Gohmand858e902010-04-17 15:26:15 +00008048SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008049 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008050 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008051 SDValue Chain = Op.getOperand(0);
8052 SDValue DstPtr = Op.getOperand(1);
8053 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008054 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8055 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008056 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008057
Chris Lattnere72f2022010-09-21 05:40:29 +00008058 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008059 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008060 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008061 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008062}
8063
Dan Gohman475871a2008-07-27 21:46:04 +00008064SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008065X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008066 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008067 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008068 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008069 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008070 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008071 case Intrinsic::x86_sse_comieq_ss:
8072 case Intrinsic::x86_sse_comilt_ss:
8073 case Intrinsic::x86_sse_comile_ss:
8074 case Intrinsic::x86_sse_comigt_ss:
8075 case Intrinsic::x86_sse_comige_ss:
8076 case Intrinsic::x86_sse_comineq_ss:
8077 case Intrinsic::x86_sse_ucomieq_ss:
8078 case Intrinsic::x86_sse_ucomilt_ss:
8079 case Intrinsic::x86_sse_ucomile_ss:
8080 case Intrinsic::x86_sse_ucomigt_ss:
8081 case Intrinsic::x86_sse_ucomige_ss:
8082 case Intrinsic::x86_sse_ucomineq_ss:
8083 case Intrinsic::x86_sse2_comieq_sd:
8084 case Intrinsic::x86_sse2_comilt_sd:
8085 case Intrinsic::x86_sse2_comile_sd:
8086 case Intrinsic::x86_sse2_comigt_sd:
8087 case Intrinsic::x86_sse2_comige_sd:
8088 case Intrinsic::x86_sse2_comineq_sd:
8089 case Intrinsic::x86_sse2_ucomieq_sd:
8090 case Intrinsic::x86_sse2_ucomilt_sd:
8091 case Intrinsic::x86_sse2_ucomile_sd:
8092 case Intrinsic::x86_sse2_ucomigt_sd:
8093 case Intrinsic::x86_sse2_ucomige_sd:
8094 case Intrinsic::x86_sse2_ucomineq_sd: {
8095 unsigned Opc = 0;
8096 ISD::CondCode CC = ISD::SETCC_INVALID;
8097 switch (IntNo) {
8098 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008099 case Intrinsic::x86_sse_comieq_ss:
8100 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008101 Opc = X86ISD::COMI;
8102 CC = ISD::SETEQ;
8103 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008104 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008105 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008106 Opc = X86ISD::COMI;
8107 CC = ISD::SETLT;
8108 break;
8109 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008110 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111 Opc = X86ISD::COMI;
8112 CC = ISD::SETLE;
8113 break;
8114 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008115 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008116 Opc = X86ISD::COMI;
8117 CC = ISD::SETGT;
8118 break;
8119 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008120 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008121 Opc = X86ISD::COMI;
8122 CC = ISD::SETGE;
8123 break;
8124 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008125 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008126 Opc = X86ISD::COMI;
8127 CC = ISD::SETNE;
8128 break;
8129 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008130 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008131 Opc = X86ISD::UCOMI;
8132 CC = ISD::SETEQ;
8133 break;
8134 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008135 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008136 Opc = X86ISD::UCOMI;
8137 CC = ISD::SETLT;
8138 break;
8139 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008140 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008141 Opc = X86ISD::UCOMI;
8142 CC = ISD::SETLE;
8143 break;
8144 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008145 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008146 Opc = X86ISD::UCOMI;
8147 CC = ISD::SETGT;
8148 break;
8149 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008150 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008151 Opc = X86ISD::UCOMI;
8152 CC = ISD::SETGE;
8153 break;
8154 case Intrinsic::x86_sse_ucomineq_ss:
8155 case Intrinsic::x86_sse2_ucomineq_sd:
8156 Opc = X86ISD::UCOMI;
8157 CC = ISD::SETNE;
8158 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008159 }
Evan Cheng734503b2006-09-11 02:19:56 +00008160
Dan Gohman475871a2008-07-27 21:46:04 +00008161 SDValue LHS = Op.getOperand(1);
8162 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008163 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008164 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008165 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8166 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8167 DAG.getConstant(X86CC, MVT::i8), Cond);
8168 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008169 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008170 // ptest and testp intrinsics. The intrinsic these come from are designed to
8171 // return an integer value, not just an instruction so lower it to the ptest
8172 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008173 case Intrinsic::x86_sse41_ptestz:
8174 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008175 case Intrinsic::x86_sse41_ptestnzc:
8176 case Intrinsic::x86_avx_ptestz_256:
8177 case Intrinsic::x86_avx_ptestc_256:
8178 case Intrinsic::x86_avx_ptestnzc_256:
8179 case Intrinsic::x86_avx_vtestz_ps:
8180 case Intrinsic::x86_avx_vtestc_ps:
8181 case Intrinsic::x86_avx_vtestnzc_ps:
8182 case Intrinsic::x86_avx_vtestz_pd:
8183 case Intrinsic::x86_avx_vtestc_pd:
8184 case Intrinsic::x86_avx_vtestnzc_pd:
8185 case Intrinsic::x86_avx_vtestz_ps_256:
8186 case Intrinsic::x86_avx_vtestc_ps_256:
8187 case Intrinsic::x86_avx_vtestnzc_ps_256:
8188 case Intrinsic::x86_avx_vtestz_pd_256:
8189 case Intrinsic::x86_avx_vtestc_pd_256:
8190 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8191 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008192 unsigned X86CC = 0;
8193 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008194 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008195 case Intrinsic::x86_avx_vtestz_ps:
8196 case Intrinsic::x86_avx_vtestz_pd:
8197 case Intrinsic::x86_avx_vtestz_ps_256:
8198 case Intrinsic::x86_avx_vtestz_pd_256:
8199 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008200 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008201 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008202 // ZF = 1
8203 X86CC = X86::COND_E;
8204 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008205 case Intrinsic::x86_avx_vtestc_ps:
8206 case Intrinsic::x86_avx_vtestc_pd:
8207 case Intrinsic::x86_avx_vtestc_ps_256:
8208 case Intrinsic::x86_avx_vtestc_pd_256:
8209 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008210 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008211 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008212 // CF = 1
8213 X86CC = X86::COND_B;
8214 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008215 case Intrinsic::x86_avx_vtestnzc_ps:
8216 case Intrinsic::x86_avx_vtestnzc_pd:
8217 case Intrinsic::x86_avx_vtestnzc_ps_256:
8218 case Intrinsic::x86_avx_vtestnzc_pd_256:
8219 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008220 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008221 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008222 // ZF and CF = 0
8223 X86CC = X86::COND_A;
8224 break;
8225 }
Eric Christopherfd179292009-08-27 18:07:15 +00008226
Eric Christopher71c67532009-07-29 00:28:05 +00008227 SDValue LHS = Op.getOperand(1);
8228 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008229 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8230 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008231 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8232 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8233 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008234 }
Evan Cheng5759f972008-05-04 09:15:50 +00008235
8236 // Fix vector shift instructions where the last operand is a non-immediate
8237 // i32 value.
8238 case Intrinsic::x86_sse2_pslli_w:
8239 case Intrinsic::x86_sse2_pslli_d:
8240 case Intrinsic::x86_sse2_pslli_q:
8241 case Intrinsic::x86_sse2_psrli_w:
8242 case Intrinsic::x86_sse2_psrli_d:
8243 case Intrinsic::x86_sse2_psrli_q:
8244 case Intrinsic::x86_sse2_psrai_w:
8245 case Intrinsic::x86_sse2_psrai_d:
8246 case Intrinsic::x86_mmx_pslli_w:
8247 case Intrinsic::x86_mmx_pslli_d:
8248 case Intrinsic::x86_mmx_pslli_q:
8249 case Intrinsic::x86_mmx_psrli_w:
8250 case Intrinsic::x86_mmx_psrli_d:
8251 case Intrinsic::x86_mmx_psrli_q:
8252 case Intrinsic::x86_mmx_psrai_w:
8253 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008254 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008255 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008256 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008257
8258 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008259 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008260 switch (IntNo) {
8261 case Intrinsic::x86_sse2_pslli_w:
8262 NewIntNo = Intrinsic::x86_sse2_psll_w;
8263 break;
8264 case Intrinsic::x86_sse2_pslli_d:
8265 NewIntNo = Intrinsic::x86_sse2_psll_d;
8266 break;
8267 case Intrinsic::x86_sse2_pslli_q:
8268 NewIntNo = Intrinsic::x86_sse2_psll_q;
8269 break;
8270 case Intrinsic::x86_sse2_psrli_w:
8271 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8272 break;
8273 case Intrinsic::x86_sse2_psrli_d:
8274 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8275 break;
8276 case Intrinsic::x86_sse2_psrli_q:
8277 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8278 break;
8279 case Intrinsic::x86_sse2_psrai_w:
8280 NewIntNo = Intrinsic::x86_sse2_psra_w;
8281 break;
8282 case Intrinsic::x86_sse2_psrai_d:
8283 NewIntNo = Intrinsic::x86_sse2_psra_d;
8284 break;
8285 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008287 switch (IntNo) {
8288 case Intrinsic::x86_mmx_pslli_w:
8289 NewIntNo = Intrinsic::x86_mmx_psll_w;
8290 break;
8291 case Intrinsic::x86_mmx_pslli_d:
8292 NewIntNo = Intrinsic::x86_mmx_psll_d;
8293 break;
8294 case Intrinsic::x86_mmx_pslli_q:
8295 NewIntNo = Intrinsic::x86_mmx_psll_q;
8296 break;
8297 case Intrinsic::x86_mmx_psrli_w:
8298 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8299 break;
8300 case Intrinsic::x86_mmx_psrli_d:
8301 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8302 break;
8303 case Intrinsic::x86_mmx_psrli_q:
8304 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8305 break;
8306 case Intrinsic::x86_mmx_psrai_w:
8307 NewIntNo = Intrinsic::x86_mmx_psra_w;
8308 break;
8309 case Intrinsic::x86_mmx_psrai_d:
8310 NewIntNo = Intrinsic::x86_mmx_psra_d;
8311 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008312 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008313 }
8314 break;
8315 }
8316 }
Mon P Wangefa42202009-09-03 19:56:25 +00008317
8318 // The vector shift intrinsics with scalars uses 32b shift amounts but
8319 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8320 // to be zero.
8321 SDValue ShOps[4];
8322 ShOps[0] = ShAmt;
8323 ShOps[1] = DAG.getConstant(0, MVT::i32);
8324 if (ShAmtVT == MVT::v4i32) {
8325 ShOps[2] = DAG.getUNDEF(MVT::i32);
8326 ShOps[3] = DAG.getUNDEF(MVT::i32);
8327 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8328 } else {
8329 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008330// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008331 }
8332
Owen Andersone50ed302009-08-10 22:56:29 +00008333 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008334 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008335 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008337 Op.getOperand(1), ShAmt);
8338 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008339 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008340}
Evan Cheng72261582005-12-20 06:22:03 +00008341
Dan Gohmand858e902010-04-17 15:26:15 +00008342SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8343 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008344 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8345 MFI->setReturnAddressIsTaken(true);
8346
Bill Wendling64e87322009-01-16 19:25:27 +00008347 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008348 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008349
8350 if (Depth > 0) {
8351 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8352 SDValue Offset =
8353 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008355 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008356 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008357 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008358 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008359 }
8360
8361 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008362 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008363 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008364 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008365}
8366
Dan Gohmand858e902010-04-17 15:26:15 +00008367SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008368 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8369 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008370
Owen Andersone50ed302009-08-10 22:56:29 +00008371 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008372 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008373 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8374 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008375 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008376 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008377 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8378 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008379 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008380 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008381}
8382
Dan Gohman475871a2008-07-27 21:46:04 +00008383SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008384 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008385 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008386}
8387
Dan Gohmand858e902010-04-17 15:26:15 +00008388SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008389 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008390 SDValue Chain = Op.getOperand(0);
8391 SDValue Offset = Op.getOperand(1);
8392 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008393 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008394
Dan Gohmand8816272010-08-11 18:14:00 +00008395 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8396 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8397 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008398 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008399
Dan Gohmand8816272010-08-11 18:14:00 +00008400 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8401 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008402 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008403 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8404 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008405 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008406 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008407
Dale Johannesene4d209d2009-02-03 20:21:25 +00008408 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008409 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008410 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008411}
8412
Dan Gohman475871a2008-07-27 21:46:04 +00008413SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008414 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008415 SDValue Root = Op.getOperand(0);
8416 SDValue Trmp = Op.getOperand(1); // trampoline
8417 SDValue FPtr = Op.getOperand(2); // nested function
8418 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008419 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008420
Dan Gohman69de1932008-02-06 22:27:42 +00008421 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008422
8423 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008424 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008425
8426 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008427 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8428 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008429
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008430 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8431 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008432
8433 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8434
8435 // Load the pointer to the nested function into R11.
8436 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008437 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008438 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008439 Addr, MachinePointerInfo(TrmpAddr),
8440 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008441
Owen Anderson825b72b2009-08-11 20:47:22 +00008442 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8443 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008444 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8445 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008446 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008447
8448 // Load the 'nest' parameter value into R10.
8449 // R10 is specified in X86CallingConv.td
8450 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008451 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8452 DAG.getConstant(10, MVT::i64));
8453 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008454 Addr, MachinePointerInfo(TrmpAddr, 10),
8455 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008456
Owen Anderson825b72b2009-08-11 20:47:22 +00008457 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8458 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008459 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8460 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008461 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008462
8463 // Jump to the nested function.
8464 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008465 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8466 DAG.getConstant(20, MVT::i64));
8467 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008468 Addr, MachinePointerInfo(TrmpAddr, 20),
8469 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008470
8471 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008472 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8473 DAG.getConstant(22, MVT::i64));
8474 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008475 MachinePointerInfo(TrmpAddr, 22),
8476 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008477
Dan Gohman475871a2008-07-27 21:46:04 +00008478 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008480 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008481 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008482 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008483 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008484 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008485 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008486
8487 switch (CC) {
8488 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008489 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008490 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008491 case CallingConv::X86_StdCall: {
8492 // Pass 'nest' parameter in ECX.
8493 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008494 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008495
8496 // Check that ECX wasn't needed by an 'inreg' parameter.
8497 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008498 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008499
Chris Lattner58d74912008-03-12 17:45:29 +00008500 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008501 unsigned InRegCount = 0;
8502 unsigned Idx = 1;
8503
8504 for (FunctionType::param_iterator I = FTy->param_begin(),
8505 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008506 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008507 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008508 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008509
8510 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008511 report_fatal_error("Nest register in use - reduce number of inreg"
8512 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008513 }
8514 }
8515 break;
8516 }
8517 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008518 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008519 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008520 // Pass 'nest' parameter in EAX.
8521 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008522 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008523 break;
8524 }
8525
Dan Gohman475871a2008-07-27 21:46:04 +00008526 SDValue OutChains[4];
8527 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008528
Owen Anderson825b72b2009-08-11 20:47:22 +00008529 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8530 DAG.getConstant(10, MVT::i32));
8531 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008532
Chris Lattnera62fe662010-02-05 19:20:30 +00008533 // This is storing the opcode for MOV32ri.
8534 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008535 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008536 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008538 Trmp, MachinePointerInfo(TrmpAddr),
8539 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008540
Owen Anderson825b72b2009-08-11 20:47:22 +00008541 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8542 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008543 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8544 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008545 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008546
Chris Lattnera62fe662010-02-05 19:20:30 +00008547 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008548 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8549 DAG.getConstant(5, MVT::i32));
8550 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008551 MachinePointerInfo(TrmpAddr, 5),
8552 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008553
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8555 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008556 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8557 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008558 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008559
Dan Gohman475871a2008-07-27 21:46:04 +00008560 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008561 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008562 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008563 }
8564}
8565
Dan Gohmand858e902010-04-17 15:26:15 +00008566SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8567 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008568 /*
8569 The rounding mode is in bits 11:10 of FPSR, and has the following
8570 settings:
8571 00 Round to nearest
8572 01 Round to -inf
8573 10 Round to +inf
8574 11 Round to 0
8575
8576 FLT_ROUNDS, on the other hand, expects the following:
8577 -1 Undefined
8578 0 Round to 0
8579 1 Round to nearest
8580 2 Round to +inf
8581 3 Round to -inf
8582
8583 To perform the conversion, we do:
8584 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8585 */
8586
8587 MachineFunction &MF = DAG.getMachineFunction();
8588 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008589 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008590 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008591 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008592 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008593
8594 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008595 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008596 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008597
Michael J. Spencerec38de22010-10-10 22:04:20 +00008598
Chris Lattner2156b792010-09-22 01:11:26 +00008599 MachineMemOperand *MMO =
8600 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8601 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008602
Chris Lattner2156b792010-09-22 01:11:26 +00008603 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8604 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8605 DAG.getVTList(MVT::Other),
8606 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008607
8608 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008609 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008610 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008611
8612 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008613 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008614 DAG.getNode(ISD::SRL, DL, MVT::i16,
8615 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008616 CWD, DAG.getConstant(0x800, MVT::i16)),
8617 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008618 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008619 DAG.getNode(ISD::SRL, DL, MVT::i16,
8620 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008621 CWD, DAG.getConstant(0x400, MVT::i16)),
8622 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008623
Dan Gohman475871a2008-07-27 21:46:04 +00008624 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008625 DAG.getNode(ISD::AND, DL, MVT::i16,
8626 DAG.getNode(ISD::ADD, DL, MVT::i16,
8627 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008628 DAG.getConstant(1, MVT::i16)),
8629 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008630
8631
Duncan Sands83ec4b62008-06-06 12:08:01 +00008632 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008633 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008634}
8635
Dan Gohmand858e902010-04-17 15:26:15 +00008636SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008637 EVT VT = Op.getValueType();
8638 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008639 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008640 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008641
8642 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008643 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008644 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008645 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008646 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008647 }
Evan Cheng18efe262007-12-14 02:13:44 +00008648
Evan Cheng152804e2007-12-14 08:30:15 +00008649 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008650 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008651 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008652
8653 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008654 SDValue Ops[] = {
8655 Op,
8656 DAG.getConstant(NumBits+NumBits-1, OpVT),
8657 DAG.getConstant(X86::COND_E, MVT::i8),
8658 Op.getValue(1)
8659 };
8660 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008661
8662 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008663 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008664
Owen Anderson825b72b2009-08-11 20:47:22 +00008665 if (VT == MVT::i8)
8666 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008667 return Op;
8668}
8669
Dan Gohmand858e902010-04-17 15:26:15 +00008670SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008671 EVT VT = Op.getValueType();
8672 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008673 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008674 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008675
8676 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008677 if (VT == MVT::i8) {
8678 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008679 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008680 }
Evan Cheng152804e2007-12-14 08:30:15 +00008681
8682 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008683 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008684 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008685
8686 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008687 SDValue Ops[] = {
8688 Op,
8689 DAG.getConstant(NumBits, OpVT),
8690 DAG.getConstant(X86::COND_E, MVT::i8),
8691 Op.getValue(1)
8692 };
8693 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008694
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 if (VT == MVT::i8)
8696 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008697 return Op;
8698}
8699
Dan Gohmand858e902010-04-17 15:26:15 +00008700SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008701 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008702 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008703 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008704
Mon P Wangaf9b9522008-12-18 21:42:19 +00008705 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8706 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8707 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8708 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8709 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8710 //
8711 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8712 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8713 // return AloBlo + AloBhi + AhiBlo;
8714
8715 SDValue A = Op.getOperand(0);
8716 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008717
Dale Johannesene4d209d2009-02-03 20:21:25 +00008718 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008719 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8720 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008721 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008722 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8723 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008724 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008725 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008726 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008727 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008728 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008729 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008730 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008731 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008732 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008733 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008734 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8735 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008736 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008737 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8738 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008739 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8740 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008741 return Res;
8742}
8743
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008744SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8745 EVT VT = Op.getValueType();
8746 DebugLoc dl = Op.getDebugLoc();
8747 SDValue R = Op.getOperand(0);
8748
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008749 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008750
Nate Begeman51409212010-07-28 00:21:48 +00008751 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8752
8753 if (VT == MVT::v4i32) {
8754 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8755 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8756 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8757
8758 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008759
Nate Begeman51409212010-07-28 00:21:48 +00008760 std::vector<Constant*> CV(4, CI);
8761 Constant *C = ConstantVector::get(CV);
8762 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8763 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008764 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008765 false, false, 16);
8766
8767 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008768 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008769 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8770 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8771 }
8772 if (VT == MVT::v16i8) {
8773 // a = a << 5;
8774 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8775 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8776 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8777
8778 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8779 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8780
8781 std::vector<Constant*> CVM1(16, CM1);
8782 std::vector<Constant*> CVM2(16, CM2);
8783 Constant *C = ConstantVector::get(CVM1);
8784 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8785 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008786 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008787 false, false, 16);
8788
8789 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8790 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8791 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8792 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8793 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008794 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008795 // a += a
8796 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008797
Nate Begeman51409212010-07-28 00:21:48 +00008798 C = ConstantVector::get(CVM2);
8799 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8800 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008801 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008802 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008803
Nate Begeman51409212010-07-28 00:21:48 +00008804 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8805 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8806 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8807 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8808 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008809 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008810 // a += a
8811 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008812
Nate Begeman51409212010-07-28 00:21:48 +00008813 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008814 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008815 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8816 return R;
8817 }
8818 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008819}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008820
Dan Gohmand858e902010-04-17 15:26:15 +00008821SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008822 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8823 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008824 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8825 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008826 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008827 SDValue LHS = N->getOperand(0);
8828 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008829 unsigned BaseOp = 0;
8830 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008831 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008832 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008833 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008834 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008835 // A subtract of one will be selected as a INC. Note that INC doesn't
8836 // set CF, so we can't do this for UADDO.
8837 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8838 if (C->getAPIntValue() == 1) {
8839 BaseOp = X86ISD::INC;
8840 Cond = X86::COND_O;
8841 break;
8842 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008843 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008844 Cond = X86::COND_O;
8845 break;
8846 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008847 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008848 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008849 break;
8850 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008851 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8852 // set CF, so we can't do this for USUBO.
8853 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8854 if (C->getAPIntValue() == 1) {
8855 BaseOp = X86ISD::DEC;
8856 Cond = X86::COND_O;
8857 break;
8858 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008859 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008860 Cond = X86::COND_O;
8861 break;
8862 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008863 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008864 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008865 break;
8866 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008867 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008868 Cond = X86::COND_O;
8869 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008870 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8871 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8872 MVT::i32);
8873 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008874
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008875 SDValue SetCC =
8876 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8877 DAG.getConstant(X86::COND_O, MVT::i32),
8878 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008879
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008880 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8881 return Sum;
8882 }
Bill Wendling74c37652008-12-09 22:08:41 +00008883 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008884
Bill Wendling61edeb52008-12-02 01:06:39 +00008885 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008886 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008887 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008888
Bill Wendling61edeb52008-12-02 01:06:39 +00008889 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008890 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8891 DAG.getConstant(Cond, MVT::i32),
8892 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008893
Bill Wendling61edeb52008-12-02 01:06:39 +00008894 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8895 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008896}
8897
Eric Christopher9a9d2752010-07-22 02:48:34 +00008898SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8899 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008900
Eric Christopherb6729dc2010-08-04 23:03:04 +00008901 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008902 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008903 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008904 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008905 SDValue Ops[] = {
8906 DAG.getRegister(X86::ESP, MVT::i32), // Base
8907 DAG.getTargetConstant(1, MVT::i8), // Scale
8908 DAG.getRegister(0, MVT::i32), // Index
8909 DAG.getTargetConstant(0, MVT::i32), // Disp
8910 DAG.getRegister(0, MVT::i32), // Segment.
8911 Zero,
8912 Chain
8913 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008914 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008915 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8916 array_lengthof(Ops));
8917 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008918 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008919
Eric Christopher9a9d2752010-07-22 02:48:34 +00008920 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008921 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008922 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008923
Chris Lattner132929a2010-08-14 17:26:09 +00008924 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8925 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8926 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8927 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008928
Chris Lattner132929a2010-08-14 17:26:09 +00008929 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8930 if (!Op1 && !Op2 && !Op3 && Op4)
8931 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008932
Chris Lattner132929a2010-08-14 17:26:09 +00008933 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8934 if (Op1 && !Op2 && !Op3 && !Op4)
8935 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008936
8937 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008938 // (MFENCE)>;
8939 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008940}
8941
Dan Gohmand858e902010-04-17 15:26:15 +00008942SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008943 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008944 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008945 unsigned Reg = 0;
8946 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008947 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008948 default:
8949 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008950 case MVT::i8: Reg = X86::AL; size = 1; break;
8951 case MVT::i16: Reg = X86::AX; size = 2; break;
8952 case MVT::i32: Reg = X86::EAX; size = 4; break;
8953 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008954 assert(Subtarget->is64Bit() && "Node not type legal!");
8955 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008956 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008957 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008958 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008959 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008960 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008961 Op.getOperand(1),
8962 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008963 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008964 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008965 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008966 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8967 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8968 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008969 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008970 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008971 return cpOut;
8972}
8973
Duncan Sands1607f052008-12-01 11:39:25 +00008974SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008975 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008976 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008977 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008978 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008979 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008980 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008981 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8982 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008983 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008984 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8985 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008986 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008987 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008988 rdx.getValue(1)
8989 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008990 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008991}
8992
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008993SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008994 SelectionDAG &DAG) const {
8995 EVT SrcVT = Op.getOperand(0).getValueType();
8996 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008997 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8998 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008999 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009000 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009001 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009002 // i64 <=> MMX conversions are Legal.
9003 if (SrcVT==MVT::i64 && DstVT.isVector())
9004 return Op;
9005 if (DstVT==MVT::i64 && SrcVT.isVector())
9006 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009007 // MMX <=> MMX conversions are Legal.
9008 if (SrcVT.isVector() && DstVT.isVector())
9009 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009010 // All other conversions need to be expanded.
9011 return SDValue();
9012}
Chris Lattner5b856542010-12-20 00:59:46 +00009013
Dan Gohmand858e902010-04-17 15:26:15 +00009014SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009015 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009016 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009017 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009018 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009019 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009020 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009021 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009022 Node->getOperand(0),
9023 Node->getOperand(1), negOp,
9024 cast<AtomicSDNode>(Node)->getSrcValue(),
9025 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009026}
9027
Chris Lattner5b856542010-12-20 00:59:46 +00009028static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9029 EVT VT = Op.getNode()->getValueType(0);
9030
9031 // Let legalize expand this if it isn't a legal type yet.
9032 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9033 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009034
Chris Lattner5b856542010-12-20 00:59:46 +00009035 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009036
Chris Lattner5b856542010-12-20 00:59:46 +00009037 unsigned Opc;
9038 bool ExtraOp = false;
9039 switch (Op.getOpcode()) {
9040 default: assert(0 && "Invalid code");
9041 case ISD::ADDC: Opc = X86ISD::ADD; break;
9042 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9043 case ISD::SUBC: Opc = X86ISD::SUB; break;
9044 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9045 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009046
Chris Lattner5b856542010-12-20 00:59:46 +00009047 if (!ExtraOp)
9048 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9049 Op.getOperand(1));
9050 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9051 Op.getOperand(1), Op.getOperand(2));
9052}
9053
Evan Cheng0db9fe62006-04-25 20:13:52 +00009054/// LowerOperation - Provide custom lowering hooks for some operations.
9055///
Dan Gohmand858e902010-04-17 15:26:15 +00009056SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009057 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009058 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009059 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009060 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9061 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009062 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009063 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009064 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9065 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9066 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009067 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009068 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009069 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9070 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9071 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009072 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009073 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009074 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009075 case ISD::SHL_PARTS:
9076 case ISD::SRA_PARTS:
9077 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9078 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009079 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009080 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009081 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009082 case ISD::FABS: return LowerFABS(Op, DAG);
9083 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009084 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009085 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009086 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009087 case ISD::SELECT: return LowerSELECT(Op, DAG);
9088 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009089 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009090 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009091 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009092 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009093 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009094 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9095 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009096 case ISD::FRAME_TO_ARGS_OFFSET:
9097 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009098 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009099 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009100 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009101 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009102 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9103 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009104 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009105 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009106 case ISD::SADDO:
9107 case ISD::UADDO:
9108 case ISD::SSUBO:
9109 case ISD::USUBO:
9110 case ISD::SMULO:
9111 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009112 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009113 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009114 case ISD::ADDC:
9115 case ISD::ADDE:
9116 case ISD::SUBC:
9117 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009118 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009119}
9120
Duncan Sands1607f052008-12-01 11:39:25 +00009121void X86TargetLowering::
9122ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009123 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009124 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009125 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009126 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009127
9128 SDValue Chain = Node->getOperand(0);
9129 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009130 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009131 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009133 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009134 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009135 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009136 SDValue Result =
9137 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9138 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009139 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009140 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009141 Results.push_back(Result.getValue(2));
9142}
9143
Duncan Sands126d9072008-07-04 11:47:58 +00009144/// ReplaceNodeResults - Replace a node with an illegal result type
9145/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009146void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9147 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009148 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009149 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009150 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009151 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009152 assert(false && "Do not know how to custom type legalize this operation!");
9153 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009154 case ISD::ADDC:
9155 case ISD::ADDE:
9156 case ISD::SUBC:
9157 case ISD::SUBE:
9158 // We don't want to expand or promote these.
9159 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009160 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009161 std::pair<SDValue,SDValue> Vals =
9162 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009163 SDValue FIST = Vals.first, StackSlot = Vals.second;
9164 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009165 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009166 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009167 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9168 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009169 }
9170 return;
9171 }
9172 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009173 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009174 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009175 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009176 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009177 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009178 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009179 eax.getValue(2));
9180 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9181 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009182 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009183 Results.push_back(edx.getValue(1));
9184 return;
9185 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009186 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009187 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009188 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009189 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9191 DAG.getConstant(0, MVT::i32));
9192 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9193 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009194 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9195 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009196 cpInL.getValue(1));
9197 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9199 DAG.getConstant(0, MVT::i32));
9200 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9201 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009202 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009203 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009204 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009205 swapInL.getValue(1));
9206 SDValue Ops[] = { swapInH.getValue(0),
9207 N->getOperand(1),
9208 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009209 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009210 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9211 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9212 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009213 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009214 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009215 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009216 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009217 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009218 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009219 Results.push_back(cpOutH.getValue(1));
9220 return;
9221 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009222 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009223 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9224 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009225 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009226 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9227 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009228 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009229 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9230 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009231 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009232 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9233 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009234 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009235 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9236 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009237 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009238 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9239 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009240 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009241 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9242 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009243 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009244}
9245
Evan Cheng72261582005-12-20 06:22:03 +00009246const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9247 switch (Opcode) {
9248 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009249 case X86ISD::BSF: return "X86ISD::BSF";
9250 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009251 case X86ISD::SHLD: return "X86ISD::SHLD";
9252 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009253 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009254 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009255 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009256 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009257 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009258 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009259 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9260 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9261 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009262 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009263 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009264 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009265 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009266 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009267 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009268 case X86ISD::COMI: return "X86ISD::COMI";
9269 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009270 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009271 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009272 case X86ISD::CMOV: return "X86ISD::CMOV";
9273 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009274 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009275 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9276 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009277 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009278 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009279 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009280 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009281 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009282 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9283 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009284 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009285 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009286 case X86ISD::PANDN: return "X86ISD::PANDN";
9287 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9288 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9289 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009290 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009291 case X86ISD::FMAX: return "X86ISD::FMAX";
9292 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009293 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9294 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009295 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009296 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009297 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009298 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009299 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009300 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9301 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009302 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9303 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9304 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9305 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9306 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9307 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009308 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9309 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009310 case X86ISD::VSHL: return "X86ISD::VSHL";
9311 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009312 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9313 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9314 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9315 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9316 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9317 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9318 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9319 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9320 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9321 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009322 case X86ISD::ADD: return "X86ISD::ADD";
9323 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009324 case X86ISD::ADC: return "X86ISD::ADC";
9325 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009326 case X86ISD::SMUL: return "X86ISD::SMUL";
9327 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009328 case X86ISD::INC: return "X86ISD::INC";
9329 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009330 case X86ISD::OR: return "X86ISD::OR";
9331 case X86ISD::XOR: return "X86ISD::XOR";
9332 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009333 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009334 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009335 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009336 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9337 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9338 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9339 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9340 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9341 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9342 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9343 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9344 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009345 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009346 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009347 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009348 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9349 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009350 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9351 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9352 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9353 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9354 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9355 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9356 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9357 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9358 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009359 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9360 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9361 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9362 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009363 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9364 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9365 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9366 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9367 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9368 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9369 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9370 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9371 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9372 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009373 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009374 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009375 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009376 }
9377}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009378
Chris Lattnerc9addb72007-03-30 23:15:24 +00009379// isLegalAddressingMode - Return true if the addressing mode represented
9380// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009381bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009382 const Type *Ty) const {
9383 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009384 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009385 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009386
Chris Lattnerc9addb72007-03-30 23:15:24 +00009387 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009388 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009389 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009390
Chris Lattnerc9addb72007-03-30 23:15:24 +00009391 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009392 unsigned GVFlags =
9393 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009394
Chris Lattnerdfed4132009-07-10 07:38:24 +00009395 // If a reference to this global requires an extra load, we can't fold it.
9396 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009397 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009398
Chris Lattnerdfed4132009-07-10 07:38:24 +00009399 // If BaseGV requires a register for the PIC base, we cannot also have a
9400 // BaseReg specified.
9401 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009402 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009403
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009404 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009405 if ((M != CodeModel::Small || R != Reloc::Static) &&
9406 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009407 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009409
Chris Lattnerc9addb72007-03-30 23:15:24 +00009410 switch (AM.Scale) {
9411 case 0:
9412 case 1:
9413 case 2:
9414 case 4:
9415 case 8:
9416 // These scales always work.
9417 break;
9418 case 3:
9419 case 5:
9420 case 9:
9421 // These scales are formed with basereg+scalereg. Only accept if there is
9422 // no basereg yet.
9423 if (AM.HasBaseReg)
9424 return false;
9425 break;
9426 default: // Other stuff never works.
9427 return false;
9428 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009429
Chris Lattnerc9addb72007-03-30 23:15:24 +00009430 return true;
9431}
9432
9433
Evan Cheng2bd122c2007-10-26 01:56:11 +00009434bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009435 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009436 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009437 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9438 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009439 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009440 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009441 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009442}
9443
Owen Andersone50ed302009-08-10 22:56:29 +00009444bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009445 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009446 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009447 unsigned NumBits1 = VT1.getSizeInBits();
9448 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009449 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009450 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009451 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009452}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009453
Dan Gohman97121ba2009-04-08 00:15:30 +00009454bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009455 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009456 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009457}
9458
Owen Andersone50ed302009-08-10 22:56:29 +00009459bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009460 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009461 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009462}
9463
Owen Andersone50ed302009-08-10 22:56:29 +00009464bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009465 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009466 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009467}
9468
Evan Cheng60c07e12006-07-05 22:17:51 +00009469/// isShuffleMaskLegal - Targets can use this to indicate that they only
9470/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9471/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9472/// are assumed to be legal.
9473bool
Eric Christopherfd179292009-08-27 18:07:15 +00009474X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009475 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009476 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009477 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009478 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009479
Nate Begemana09008b2009-10-19 02:17:23 +00009480 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009481 return (VT.getVectorNumElements() == 2 ||
9482 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9483 isMOVLMask(M, VT) ||
9484 isSHUFPMask(M, VT) ||
9485 isPSHUFDMask(M, VT) ||
9486 isPSHUFHWMask(M, VT) ||
9487 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009488 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009489 isUNPCKLMask(M, VT) ||
9490 isUNPCKHMask(M, VT) ||
9491 isUNPCKL_v_undef_Mask(M, VT) ||
9492 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009493}
9494
Dan Gohman7d8143f2008-04-09 20:09:42 +00009495bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009496X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009497 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009498 unsigned NumElts = VT.getVectorNumElements();
9499 // FIXME: This collection of masks seems suspect.
9500 if (NumElts == 2)
9501 return true;
9502 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9503 return (isMOVLMask(Mask, VT) ||
9504 isCommutedMOVLMask(Mask, VT, true) ||
9505 isSHUFPMask(Mask, VT) ||
9506 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009507 }
9508 return false;
9509}
9510
9511//===----------------------------------------------------------------------===//
9512// X86 Scheduler Hooks
9513//===----------------------------------------------------------------------===//
9514
Mon P Wang63307c32008-05-05 19:05:59 +00009515// private utility function
9516MachineBasicBlock *
9517X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9518 MachineBasicBlock *MBB,
9519 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009520 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009521 unsigned LoadOpc,
9522 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009523 unsigned notOpc,
9524 unsigned EAXreg,
9525 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009526 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009527 // For the atomic bitwise operator, we generate
9528 // thisMBB:
9529 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009530 // ld t1 = [bitinstr.addr]
9531 // op t2 = t1, [bitinstr.val]
9532 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009533 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9534 // bz newMBB
9535 // fallthrough -->nextMBB
9536 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9537 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009538 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009539 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009540
Mon P Wang63307c32008-05-05 19:05:59 +00009541 /// First build the CFG
9542 MachineFunction *F = MBB->getParent();
9543 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009544 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9545 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9546 F->insert(MBBIter, newMBB);
9547 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009548
Dan Gohman14152b42010-07-06 20:24:04 +00009549 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9550 nextMBB->splice(nextMBB->begin(), thisMBB,
9551 llvm::next(MachineBasicBlock::iterator(bInstr)),
9552 thisMBB->end());
9553 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009554
Mon P Wang63307c32008-05-05 19:05:59 +00009555 // Update thisMBB to fall through to newMBB
9556 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009557
Mon P Wang63307c32008-05-05 19:05:59 +00009558 // newMBB jumps to itself and fall through to nextMBB
9559 newMBB->addSuccessor(nextMBB);
9560 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009561
Mon P Wang63307c32008-05-05 19:05:59 +00009562 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009563 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009564 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009565 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009566 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009567 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009568 int numArgs = bInstr->getNumOperands() - 1;
9569 for (int i=0; i < numArgs; ++i)
9570 argOpers[i] = &bInstr->getOperand(i+1);
9571
9572 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009573 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009574 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009575
Dale Johannesen140be2d2008-08-19 18:47:28 +00009576 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009577 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009578 for (int i=0; i <= lastAddrIndx; ++i)
9579 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009580
Dale Johannesen140be2d2008-08-19 18:47:28 +00009581 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009582 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009583 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009584 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009585 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009586 tt = t1;
9587
Dale Johannesen140be2d2008-08-19 18:47:28 +00009588 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009589 assert((argOpers[valArgIndx]->isReg() ||
9590 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009591 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009592 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009593 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009594 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009595 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009596 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009597 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009598
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009599 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009600 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009601
Dale Johannesene4d209d2009-02-03 20:21:25 +00009602 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009603 for (int i=0; i <= lastAddrIndx; ++i)
9604 (*MIB).addOperand(*argOpers[i]);
9605 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009606 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009607 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9608 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009609
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009610 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009611 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009612
Mon P Wang63307c32008-05-05 19:05:59 +00009613 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009614 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009615
Dan Gohman14152b42010-07-06 20:24:04 +00009616 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009617 return nextMBB;
9618}
9619
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009620// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009621MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009622X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9623 MachineBasicBlock *MBB,
9624 unsigned regOpcL,
9625 unsigned regOpcH,
9626 unsigned immOpcL,
9627 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009628 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009629 // For the atomic bitwise operator, we generate
9630 // thisMBB (instructions are in pairs, except cmpxchg8b)
9631 // ld t1,t2 = [bitinstr.addr]
9632 // newMBB:
9633 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9634 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009635 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009636 // mov ECX, EBX <- t5, t6
9637 // mov EAX, EDX <- t1, t2
9638 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9639 // mov t3, t4 <- EAX, EDX
9640 // bz newMBB
9641 // result in out1, out2
9642 // fallthrough -->nextMBB
9643
9644 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9645 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009646 const unsigned NotOpc = X86::NOT32r;
9647 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9648 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9649 MachineFunction::iterator MBBIter = MBB;
9650 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009651
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009652 /// First build the CFG
9653 MachineFunction *F = MBB->getParent();
9654 MachineBasicBlock *thisMBB = MBB;
9655 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9656 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9657 F->insert(MBBIter, newMBB);
9658 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009659
Dan Gohman14152b42010-07-06 20:24:04 +00009660 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9661 nextMBB->splice(nextMBB->begin(), thisMBB,
9662 llvm::next(MachineBasicBlock::iterator(bInstr)),
9663 thisMBB->end());
9664 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009665
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009666 // Update thisMBB to fall through to newMBB
9667 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009668
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009669 // newMBB jumps to itself and fall through to nextMBB
9670 newMBB->addSuccessor(nextMBB);
9671 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009672
Dale Johannesene4d209d2009-02-03 20:21:25 +00009673 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009674 // Insert instructions into newMBB based on incoming instruction
9675 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009676 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009677 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009678 MachineOperand& dest1Oper = bInstr->getOperand(0);
9679 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009680 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9681 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009682 argOpers[i] = &bInstr->getOperand(i+2);
9683
Dan Gohman71ea4e52010-05-14 21:01:44 +00009684 // We use some of the operands multiple times, so conservatively just
9685 // clear any kill flags that might be present.
9686 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9687 argOpers[i]->setIsKill(false);
9688 }
9689
Evan Chengad5b52f2010-01-08 19:14:57 +00009690 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009691 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009692
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009693 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009694 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009695 for (int i=0; i <= lastAddrIndx; ++i)
9696 (*MIB).addOperand(*argOpers[i]);
9697 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009698 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009699 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009700 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009701 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009702 MachineOperand newOp3 = *(argOpers[3]);
9703 if (newOp3.isImm())
9704 newOp3.setImm(newOp3.getImm()+4);
9705 else
9706 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009707 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009708 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009709
9710 // t3/4 are defined later, at the bottom of the loop
9711 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9712 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009713 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009714 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009715 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009716 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9717
Evan Cheng306b4ca2010-01-08 23:41:50 +00009718 // The subsequent operations should be using the destination registers of
9719 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009720 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009721 t1 = F->getRegInfo().createVirtualRegister(RC);
9722 t2 = F->getRegInfo().createVirtualRegister(RC);
9723 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9724 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009725 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009726 t1 = dest1Oper.getReg();
9727 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009728 }
9729
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009730 int valArgIndx = lastAddrIndx + 1;
9731 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009732 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009733 "invalid operand");
9734 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9735 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009736 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009737 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009738 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009739 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009740 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009741 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009742 (*MIB).addOperand(*argOpers[valArgIndx]);
9743 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009744 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009745 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009746 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009747 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009748 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009749 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009750 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009751 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009752 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009753 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009754
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009755 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009756 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009757 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009758 MIB.addReg(t2);
9759
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009760 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009761 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009762 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009763 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009764
Dale Johannesene4d209d2009-02-03 20:21:25 +00009765 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009766 for (int i=0; i <= lastAddrIndx; ++i)
9767 (*MIB).addOperand(*argOpers[i]);
9768
9769 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009770 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9771 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009772
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009773 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009774 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009775 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009776 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009777
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009778 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009779 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009780
Dan Gohman14152b42010-07-06 20:24:04 +00009781 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009782 return nextMBB;
9783}
9784
9785// private utility function
9786MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009787X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9788 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009789 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009790 // For the atomic min/max operator, we generate
9791 // thisMBB:
9792 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009793 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009794 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009795 // cmp t1, t2
9796 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009797 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009798 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9799 // bz newMBB
9800 // fallthrough -->nextMBB
9801 //
9802 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9803 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009804 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009805 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009806
Mon P Wang63307c32008-05-05 19:05:59 +00009807 /// First build the CFG
9808 MachineFunction *F = MBB->getParent();
9809 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009810 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9811 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9812 F->insert(MBBIter, newMBB);
9813 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009814
Dan Gohman14152b42010-07-06 20:24:04 +00009815 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9816 nextMBB->splice(nextMBB->begin(), thisMBB,
9817 llvm::next(MachineBasicBlock::iterator(mInstr)),
9818 thisMBB->end());
9819 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009820
Mon P Wang63307c32008-05-05 19:05:59 +00009821 // Update thisMBB to fall through to newMBB
9822 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009823
Mon P Wang63307c32008-05-05 19:05:59 +00009824 // newMBB jumps to newMBB and fall through to nextMBB
9825 newMBB->addSuccessor(nextMBB);
9826 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009827
Dale Johannesene4d209d2009-02-03 20:21:25 +00009828 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009829 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009830 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009831 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009832 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009833 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009834 int numArgs = mInstr->getNumOperands() - 1;
9835 for (int i=0; i < numArgs; ++i)
9836 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009837
Mon P Wang63307c32008-05-05 19:05:59 +00009838 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009839 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009840 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009841
Mon P Wangab3e7472008-05-05 22:56:23 +00009842 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009843 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009844 for (int i=0; i <= lastAddrIndx; ++i)
9845 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009846
Mon P Wang63307c32008-05-05 19:05:59 +00009847 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009848 assert((argOpers[valArgIndx]->isReg() ||
9849 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009850 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009851
9852 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009853 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009854 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009855 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009856 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009857 (*MIB).addOperand(*argOpers[valArgIndx]);
9858
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009859 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009860 MIB.addReg(t1);
9861
Dale Johannesene4d209d2009-02-03 20:21:25 +00009862 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009863 MIB.addReg(t1);
9864 MIB.addReg(t2);
9865
9866 // Generate movc
9867 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009868 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009869 MIB.addReg(t2);
9870 MIB.addReg(t1);
9871
9872 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009873 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009874 for (int i=0; i <= lastAddrIndx; ++i)
9875 (*MIB).addOperand(*argOpers[i]);
9876 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009877 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009878 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9879 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009880
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009881 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009882 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009883
Mon P Wang63307c32008-05-05 19:05:59 +00009884 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009885 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009886
Dan Gohman14152b42010-07-06 20:24:04 +00009887 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009888 return nextMBB;
9889}
9890
Eric Christopherf83a5de2009-08-27 18:08:16 +00009891// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009892// or XMM0_V32I8 in AVX all of this code can be replaced with that
9893// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009894MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009895X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009896 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009897 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9898 "Target must have SSE4.2 or AVX features enabled");
9899
Eric Christopherb120ab42009-08-18 22:50:32 +00009900 DebugLoc dl = MI->getDebugLoc();
9901 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009902 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009903 if (!Subtarget->hasAVX()) {
9904 if (memArg)
9905 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9906 else
9907 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9908 } else {
9909 if (memArg)
9910 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9911 else
9912 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9913 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009914
Eric Christopher41c902f2010-11-30 08:20:21 +00009915 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009916 for (unsigned i = 0; i < numArgs; ++i) {
9917 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009918 if (!(Op.isReg() && Op.isImplicit()))
9919 MIB.addOperand(Op);
9920 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009921 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009922 .addReg(X86::XMM0);
9923
Dan Gohman14152b42010-07-06 20:24:04 +00009924 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009925 return BB;
9926}
9927
9928MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009929X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009930 DebugLoc dl = MI->getDebugLoc();
9931 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009932
Eric Christopher228232b2010-11-30 07:20:12 +00009933 // Address into RAX/EAX, other two args into ECX, EDX.
9934 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9935 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9936 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9937 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009938 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009939
Eric Christopher228232b2010-11-30 07:20:12 +00009940 unsigned ValOps = X86::AddrNumOperands;
9941 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9942 .addReg(MI->getOperand(ValOps).getReg());
9943 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9944 .addReg(MI->getOperand(ValOps+1).getReg());
9945
9946 // The instruction doesn't actually take any operands though.
9947 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009948
Eric Christopher228232b2010-11-30 07:20:12 +00009949 MI->eraseFromParent(); // The pseudo is gone now.
9950 return BB;
9951}
9952
9953MachineBasicBlock *
9954X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009955 DebugLoc dl = MI->getDebugLoc();
9956 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009957
Eric Christopher228232b2010-11-30 07:20:12 +00009958 // First arg in ECX, the second in EAX.
9959 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9960 .addReg(MI->getOperand(0).getReg());
9961 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9962 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009963
Eric Christopher228232b2010-11-30 07:20:12 +00009964 // The instruction doesn't actually take any operands though.
9965 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009966
Eric Christopher228232b2010-11-30 07:20:12 +00009967 MI->eraseFromParent(); // The pseudo is gone now.
9968 return BB;
9969}
9970
9971MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009972X86TargetLowering::EmitVAARG64WithCustomInserter(
9973 MachineInstr *MI,
9974 MachineBasicBlock *MBB) const {
9975 // Emit va_arg instruction on X86-64.
9976
9977 // Operands to this pseudo-instruction:
9978 // 0 ) Output : destination address (reg)
9979 // 1-5) Input : va_list address (addr, i64mem)
9980 // 6 ) ArgSize : Size (in bytes) of vararg type
9981 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9982 // 8 ) Align : Alignment of type
9983 // 9 ) EFLAGS (implicit-def)
9984
9985 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9986 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9987
9988 unsigned DestReg = MI->getOperand(0).getReg();
9989 MachineOperand &Base = MI->getOperand(1);
9990 MachineOperand &Scale = MI->getOperand(2);
9991 MachineOperand &Index = MI->getOperand(3);
9992 MachineOperand &Disp = MI->getOperand(4);
9993 MachineOperand &Segment = MI->getOperand(5);
9994 unsigned ArgSize = MI->getOperand(6).getImm();
9995 unsigned ArgMode = MI->getOperand(7).getImm();
9996 unsigned Align = MI->getOperand(8).getImm();
9997
9998 // Memory Reference
9999 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10000 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10001 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10002
10003 // Machine Information
10004 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10005 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10006 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10007 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10008 DebugLoc DL = MI->getDebugLoc();
10009
10010 // struct va_list {
10011 // i32 gp_offset
10012 // i32 fp_offset
10013 // i64 overflow_area (address)
10014 // i64 reg_save_area (address)
10015 // }
10016 // sizeof(va_list) = 24
10017 // alignment(va_list) = 8
10018
10019 unsigned TotalNumIntRegs = 6;
10020 unsigned TotalNumXMMRegs = 8;
10021 bool UseGPOffset = (ArgMode == 1);
10022 bool UseFPOffset = (ArgMode == 2);
10023 unsigned MaxOffset = TotalNumIntRegs * 8 +
10024 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10025
10026 /* Align ArgSize to a multiple of 8 */
10027 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10028 bool NeedsAlign = (Align > 8);
10029
10030 MachineBasicBlock *thisMBB = MBB;
10031 MachineBasicBlock *overflowMBB;
10032 MachineBasicBlock *offsetMBB;
10033 MachineBasicBlock *endMBB;
10034
10035 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10036 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10037 unsigned OffsetReg = 0;
10038
10039 if (!UseGPOffset && !UseFPOffset) {
10040 // If we only pull from the overflow region, we don't create a branch.
10041 // We don't need to alter control flow.
10042 OffsetDestReg = 0; // unused
10043 OverflowDestReg = DestReg;
10044
10045 offsetMBB = NULL;
10046 overflowMBB = thisMBB;
10047 endMBB = thisMBB;
10048 } else {
10049 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10050 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10051 // If not, pull from overflow_area. (branch to overflowMBB)
10052 //
10053 // thisMBB
10054 // | .
10055 // | .
10056 // offsetMBB overflowMBB
10057 // | .
10058 // | .
10059 // endMBB
10060
10061 // Registers for the PHI in endMBB
10062 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10063 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10064
10065 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10066 MachineFunction *MF = MBB->getParent();
10067 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10068 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10069 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10070
10071 MachineFunction::iterator MBBIter = MBB;
10072 ++MBBIter;
10073
10074 // Insert the new basic blocks
10075 MF->insert(MBBIter, offsetMBB);
10076 MF->insert(MBBIter, overflowMBB);
10077 MF->insert(MBBIter, endMBB);
10078
10079 // Transfer the remainder of MBB and its successor edges to endMBB.
10080 endMBB->splice(endMBB->begin(), thisMBB,
10081 llvm::next(MachineBasicBlock::iterator(MI)),
10082 thisMBB->end());
10083 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10084
10085 // Make offsetMBB and overflowMBB successors of thisMBB
10086 thisMBB->addSuccessor(offsetMBB);
10087 thisMBB->addSuccessor(overflowMBB);
10088
10089 // endMBB is a successor of both offsetMBB and overflowMBB
10090 offsetMBB->addSuccessor(endMBB);
10091 overflowMBB->addSuccessor(endMBB);
10092
10093 // Load the offset value into a register
10094 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10095 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10096 .addOperand(Base)
10097 .addOperand(Scale)
10098 .addOperand(Index)
10099 .addDisp(Disp, UseFPOffset ? 4 : 0)
10100 .addOperand(Segment)
10101 .setMemRefs(MMOBegin, MMOEnd);
10102
10103 // Check if there is enough room left to pull this argument.
10104 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10105 .addReg(OffsetReg)
10106 .addImm(MaxOffset + 8 - ArgSizeA8);
10107
10108 // Branch to "overflowMBB" if offset >= max
10109 // Fall through to "offsetMBB" otherwise
10110 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10111 .addMBB(overflowMBB);
10112 }
10113
10114 // In offsetMBB, emit code to use the reg_save_area.
10115 if (offsetMBB) {
10116 assert(OffsetReg != 0);
10117
10118 // Read the reg_save_area address.
10119 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10120 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10121 .addOperand(Base)
10122 .addOperand(Scale)
10123 .addOperand(Index)
10124 .addDisp(Disp, 16)
10125 .addOperand(Segment)
10126 .setMemRefs(MMOBegin, MMOEnd);
10127
10128 // Zero-extend the offset
10129 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10130 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10131 .addImm(0)
10132 .addReg(OffsetReg)
10133 .addImm(X86::sub_32bit);
10134
10135 // Add the offset to the reg_save_area to get the final address.
10136 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10137 .addReg(OffsetReg64)
10138 .addReg(RegSaveReg);
10139
10140 // Compute the offset for the next argument
10141 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10142 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10143 .addReg(OffsetReg)
10144 .addImm(UseFPOffset ? 16 : 8);
10145
10146 // Store it back into the va_list.
10147 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10148 .addOperand(Base)
10149 .addOperand(Scale)
10150 .addOperand(Index)
10151 .addDisp(Disp, UseFPOffset ? 4 : 0)
10152 .addOperand(Segment)
10153 .addReg(NextOffsetReg)
10154 .setMemRefs(MMOBegin, MMOEnd);
10155
10156 // Jump to endMBB
10157 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10158 .addMBB(endMBB);
10159 }
10160
10161 //
10162 // Emit code to use overflow area
10163 //
10164
10165 // Load the overflow_area address into a register.
10166 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10167 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10168 .addOperand(Base)
10169 .addOperand(Scale)
10170 .addOperand(Index)
10171 .addDisp(Disp, 8)
10172 .addOperand(Segment)
10173 .setMemRefs(MMOBegin, MMOEnd);
10174
10175 // If we need to align it, do so. Otherwise, just copy the address
10176 // to OverflowDestReg.
10177 if (NeedsAlign) {
10178 // Align the overflow address
10179 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10180 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10181
10182 // aligned_addr = (addr + (align-1)) & ~(align-1)
10183 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10184 .addReg(OverflowAddrReg)
10185 .addImm(Align-1);
10186
10187 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10188 .addReg(TmpReg)
10189 .addImm(~(uint64_t)(Align-1));
10190 } else {
10191 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10192 .addReg(OverflowAddrReg);
10193 }
10194
10195 // Compute the next overflow address after this argument.
10196 // (the overflow address should be kept 8-byte aligned)
10197 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10198 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10199 .addReg(OverflowDestReg)
10200 .addImm(ArgSizeA8);
10201
10202 // Store the new overflow address.
10203 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10204 .addOperand(Base)
10205 .addOperand(Scale)
10206 .addOperand(Index)
10207 .addDisp(Disp, 8)
10208 .addOperand(Segment)
10209 .addReg(NextAddrReg)
10210 .setMemRefs(MMOBegin, MMOEnd);
10211
10212 // If we branched, emit the PHI to the front of endMBB.
10213 if (offsetMBB) {
10214 BuildMI(*endMBB, endMBB->begin(), DL,
10215 TII->get(X86::PHI), DestReg)
10216 .addReg(OffsetDestReg).addMBB(offsetMBB)
10217 .addReg(OverflowDestReg).addMBB(overflowMBB);
10218 }
10219
10220 // Erase the pseudo instruction
10221 MI->eraseFromParent();
10222
10223 return endMBB;
10224}
10225
10226MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010227X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10228 MachineInstr *MI,
10229 MachineBasicBlock *MBB) const {
10230 // Emit code to save XMM registers to the stack. The ABI says that the
10231 // number of registers to save is given in %al, so it's theoretically
10232 // possible to do an indirect jump trick to avoid saving all of them,
10233 // however this code takes a simpler approach and just executes all
10234 // of the stores if %al is non-zero. It's less code, and it's probably
10235 // easier on the hardware branch predictor, and stores aren't all that
10236 // expensive anyway.
10237
10238 // Create the new basic blocks. One block contains all the XMM stores,
10239 // and one block is the final destination regardless of whether any
10240 // stores were performed.
10241 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10242 MachineFunction *F = MBB->getParent();
10243 MachineFunction::iterator MBBIter = MBB;
10244 ++MBBIter;
10245 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10246 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10247 F->insert(MBBIter, XMMSaveMBB);
10248 F->insert(MBBIter, EndMBB);
10249
Dan Gohman14152b42010-07-06 20:24:04 +000010250 // Transfer the remainder of MBB and its successor edges to EndMBB.
10251 EndMBB->splice(EndMBB->begin(), MBB,
10252 llvm::next(MachineBasicBlock::iterator(MI)),
10253 MBB->end());
10254 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10255
Dan Gohmand6708ea2009-08-15 01:38:56 +000010256 // The original block will now fall through to the XMM save block.
10257 MBB->addSuccessor(XMMSaveMBB);
10258 // The XMMSaveMBB will fall through to the end block.
10259 XMMSaveMBB->addSuccessor(EndMBB);
10260
10261 // Now add the instructions.
10262 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10263 DebugLoc DL = MI->getDebugLoc();
10264
10265 unsigned CountReg = MI->getOperand(0).getReg();
10266 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10267 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10268
10269 if (!Subtarget->isTargetWin64()) {
10270 // If %al is 0, branch around the XMM save block.
10271 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010272 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010273 MBB->addSuccessor(EndMBB);
10274 }
10275
10276 // In the XMM save block, save all the XMM argument registers.
10277 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10278 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010279 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010280 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010281 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010282 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010283 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010284 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10285 .addFrameIndex(RegSaveFrameIndex)
10286 .addImm(/*Scale=*/1)
10287 .addReg(/*IndexReg=*/0)
10288 .addImm(/*Disp=*/Offset)
10289 .addReg(/*Segment=*/0)
10290 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010291 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010292 }
10293
Dan Gohman14152b42010-07-06 20:24:04 +000010294 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010295
10296 return EndMBB;
10297}
Mon P Wang63307c32008-05-05 19:05:59 +000010298
Evan Cheng60c07e12006-07-05 22:17:51 +000010299MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010300X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010301 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010302 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10303 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010304
Chris Lattner52600972009-09-02 05:57:00 +000010305 // To "insert" a SELECT_CC instruction, we actually have to insert the
10306 // diamond control-flow pattern. The incoming instruction knows the
10307 // destination vreg to set, the condition code register to branch on, the
10308 // true/false values to select between, and a branch opcode to use.
10309 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10310 MachineFunction::iterator It = BB;
10311 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010312
Chris Lattner52600972009-09-02 05:57:00 +000010313 // thisMBB:
10314 // ...
10315 // TrueVal = ...
10316 // cmpTY ccX, r1, r2
10317 // bCC copy1MBB
10318 // fallthrough --> copy0MBB
10319 MachineBasicBlock *thisMBB = BB;
10320 MachineFunction *F = BB->getParent();
10321 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10322 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010323 F->insert(It, copy0MBB);
10324 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010325
Bill Wendling730c07e2010-06-25 20:48:10 +000010326 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10327 // live into the sink and copy blocks.
10328 const MachineFunction *MF = BB->getParent();
10329 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10330 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010331
Dan Gohman14152b42010-07-06 20:24:04 +000010332 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10333 const MachineOperand &MO = MI->getOperand(I);
10334 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010335 unsigned Reg = MO.getReg();
10336 if (Reg != X86::EFLAGS) continue;
10337 copy0MBB->addLiveIn(Reg);
10338 sinkMBB->addLiveIn(Reg);
10339 }
10340
Dan Gohman14152b42010-07-06 20:24:04 +000010341 // Transfer the remainder of BB and its successor edges to sinkMBB.
10342 sinkMBB->splice(sinkMBB->begin(), BB,
10343 llvm::next(MachineBasicBlock::iterator(MI)),
10344 BB->end());
10345 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10346
10347 // Add the true and fallthrough blocks as its successors.
10348 BB->addSuccessor(copy0MBB);
10349 BB->addSuccessor(sinkMBB);
10350
10351 // Create the conditional branch instruction.
10352 unsigned Opc =
10353 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10354 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10355
Chris Lattner52600972009-09-02 05:57:00 +000010356 // copy0MBB:
10357 // %FalseValue = ...
10358 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010359 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010360
Chris Lattner52600972009-09-02 05:57:00 +000010361 // sinkMBB:
10362 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10363 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010364 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10365 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010366 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10367 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10368
Dan Gohman14152b42010-07-06 20:24:04 +000010369 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010370 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010371}
10372
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010373MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010374X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010375 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010376 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10377 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010378
10379 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10380 // non-trivial part is impdef of ESP.
10381 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10382 // mingw-w64.
10383
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010384 const char *StackProbeSymbol =
10385 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10386
Dan Gohman14152b42010-07-06 20:24:04 +000010387 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010388 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010389 .addReg(X86::EAX, RegState::Implicit)
10390 .addReg(X86::ESP, RegState::Implicit)
10391 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010392 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10393 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010394
Dan Gohman14152b42010-07-06 20:24:04 +000010395 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010396 return BB;
10397}
Chris Lattner52600972009-09-02 05:57:00 +000010398
10399MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010400X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10401 MachineBasicBlock *BB) const {
10402 // This is pretty easy. We're taking the value that we received from
10403 // our load from the relocation, sticking it in either RDI (x86-64)
10404 // or EAX and doing an indirect call. The return value will then
10405 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010406 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010407 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010408 DebugLoc DL = MI->getDebugLoc();
10409 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010410
10411 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010412 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010413
Eric Christopher30ef0e52010-06-03 04:07:48 +000010414 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010415 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10416 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010417 .addReg(X86::RIP)
10418 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010419 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010420 MI->getOperand(3).getTargetFlags())
10421 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010422 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010423 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010424 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010425 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10426 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010427 .addReg(0)
10428 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010429 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010430 MI->getOperand(3).getTargetFlags())
10431 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010432 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010433 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010434 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010435 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10436 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010437 .addReg(TII->getGlobalBaseReg(F))
10438 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010439 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010440 MI->getOperand(3).getTargetFlags())
10441 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010442 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010443 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010444 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010445
Dan Gohman14152b42010-07-06 20:24:04 +000010446 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010447 return BB;
10448}
10449
10450MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010451X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010452 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010453 switch (MI->getOpcode()) {
10454 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010455 case X86::TAILJMPd64:
10456 case X86::TAILJMPr64:
10457 case X86::TAILJMPm64:
10458 assert(!"TAILJMP64 would not be touched here.");
10459 case X86::TCRETURNdi64:
10460 case X86::TCRETURNri64:
10461 case X86::TCRETURNmi64:
10462 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10463 // On AMD64, additional defs should be added before register allocation.
10464 if (!Subtarget->isTargetWin64()) {
10465 MI->addRegisterDefined(X86::RSI);
10466 MI->addRegisterDefined(X86::RDI);
10467 MI->addRegisterDefined(X86::XMM6);
10468 MI->addRegisterDefined(X86::XMM7);
10469 MI->addRegisterDefined(X86::XMM8);
10470 MI->addRegisterDefined(X86::XMM9);
10471 MI->addRegisterDefined(X86::XMM10);
10472 MI->addRegisterDefined(X86::XMM11);
10473 MI->addRegisterDefined(X86::XMM12);
10474 MI->addRegisterDefined(X86::XMM13);
10475 MI->addRegisterDefined(X86::XMM14);
10476 MI->addRegisterDefined(X86::XMM15);
10477 }
10478 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010479 case X86::WIN_ALLOCA:
10480 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010481 case X86::TLSCall_32:
10482 case X86::TLSCall_64:
10483 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010484 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010485 case X86::CMOV_FR32:
10486 case X86::CMOV_FR64:
10487 case X86::CMOV_V4F32:
10488 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010489 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010490 case X86::CMOV_GR16:
10491 case X86::CMOV_GR32:
10492 case X86::CMOV_RFP32:
10493 case X86::CMOV_RFP64:
10494 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010495 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010496
Dale Johannesen849f2142007-07-03 00:53:03 +000010497 case X86::FP32_TO_INT16_IN_MEM:
10498 case X86::FP32_TO_INT32_IN_MEM:
10499 case X86::FP32_TO_INT64_IN_MEM:
10500 case X86::FP64_TO_INT16_IN_MEM:
10501 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010502 case X86::FP64_TO_INT64_IN_MEM:
10503 case X86::FP80_TO_INT16_IN_MEM:
10504 case X86::FP80_TO_INT32_IN_MEM:
10505 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010506 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10507 DebugLoc DL = MI->getDebugLoc();
10508
Evan Cheng60c07e12006-07-05 22:17:51 +000010509 // Change the floating point control register to use "round towards zero"
10510 // mode when truncating to an integer value.
10511 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010512 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010513 addFrameReference(BuildMI(*BB, MI, DL,
10514 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010515
10516 // Load the old value of the high byte of the control word...
10517 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010518 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010519 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010520 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010521
10522 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010523 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010524 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010525
10526 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010527 addFrameReference(BuildMI(*BB, MI, DL,
10528 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010529
10530 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010531 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010532 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010533
10534 // Get the X86 opcode to use.
10535 unsigned Opc;
10536 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010537 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010538 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10539 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10540 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10541 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10542 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10543 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010544 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10545 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10546 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010547 }
10548
10549 X86AddressMode AM;
10550 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010551 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010552 AM.BaseType = X86AddressMode::RegBase;
10553 AM.Base.Reg = Op.getReg();
10554 } else {
10555 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010556 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010557 }
10558 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010559 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010560 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010561 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010562 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010563 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010564 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010565 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010566 AM.GV = Op.getGlobal();
10567 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010568 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010569 }
Dan Gohman14152b42010-07-06 20:24:04 +000010570 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010571 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010572
10573 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010574 addFrameReference(BuildMI(*BB, MI, DL,
10575 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010576
Dan Gohman14152b42010-07-06 20:24:04 +000010577 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010578 return BB;
10579 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010580 // String/text processing lowering.
10581 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010582 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010583 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10584 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010585 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010586 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10587 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010588 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010589 return EmitPCMP(MI, BB, 5, false /* in mem */);
10590 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010591 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010592 return EmitPCMP(MI, BB, 5, true /* in mem */);
10593
Eric Christopher228232b2010-11-30 07:20:12 +000010594 // Thread synchronization.
10595 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010596 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010597 case X86::MWAIT:
10598 return EmitMwait(MI, BB);
10599
Eric Christopherb120ab42009-08-18 22:50:32 +000010600 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010601 case X86::ATOMAND32:
10602 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010603 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010604 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010605 X86::NOT32r, X86::EAX,
10606 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010607 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010608 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10609 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010610 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010611 X86::NOT32r, X86::EAX,
10612 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010613 case X86::ATOMXOR32:
10614 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010615 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010616 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010617 X86::NOT32r, X86::EAX,
10618 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010619 case X86::ATOMNAND32:
10620 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010621 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010622 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010623 X86::NOT32r, X86::EAX,
10624 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010625 case X86::ATOMMIN32:
10626 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10627 case X86::ATOMMAX32:
10628 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10629 case X86::ATOMUMIN32:
10630 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10631 case X86::ATOMUMAX32:
10632 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010633
10634 case X86::ATOMAND16:
10635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10636 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010637 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010638 X86::NOT16r, X86::AX,
10639 X86::GR16RegisterClass);
10640 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010642 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010643 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010644 X86::NOT16r, X86::AX,
10645 X86::GR16RegisterClass);
10646 case X86::ATOMXOR16:
10647 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10648 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010649 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010650 X86::NOT16r, X86::AX,
10651 X86::GR16RegisterClass);
10652 case X86::ATOMNAND16:
10653 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10654 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010655 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010656 X86::NOT16r, X86::AX,
10657 X86::GR16RegisterClass, true);
10658 case X86::ATOMMIN16:
10659 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10660 case X86::ATOMMAX16:
10661 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10662 case X86::ATOMUMIN16:
10663 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10664 case X86::ATOMUMAX16:
10665 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10666
10667 case X86::ATOMAND8:
10668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10669 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010670 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010671 X86::NOT8r, X86::AL,
10672 X86::GR8RegisterClass);
10673 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010674 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010675 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010676 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010677 X86::NOT8r, X86::AL,
10678 X86::GR8RegisterClass);
10679 case X86::ATOMXOR8:
10680 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10681 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010682 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010683 X86::NOT8r, X86::AL,
10684 X86::GR8RegisterClass);
10685 case X86::ATOMNAND8:
10686 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10687 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010688 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010689 X86::NOT8r, X86::AL,
10690 X86::GR8RegisterClass, true);
10691 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010692 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010693 case X86::ATOMAND64:
10694 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010695 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010696 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010697 X86::NOT64r, X86::RAX,
10698 X86::GR64RegisterClass);
10699 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010700 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10701 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010702 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010703 X86::NOT64r, X86::RAX,
10704 X86::GR64RegisterClass);
10705 case X86::ATOMXOR64:
10706 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010707 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010708 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010709 X86::NOT64r, X86::RAX,
10710 X86::GR64RegisterClass);
10711 case X86::ATOMNAND64:
10712 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10713 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010714 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010715 X86::NOT64r, X86::RAX,
10716 X86::GR64RegisterClass, true);
10717 case X86::ATOMMIN64:
10718 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10719 case X86::ATOMMAX64:
10720 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10721 case X86::ATOMUMIN64:
10722 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10723 case X86::ATOMUMAX64:
10724 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010725
10726 // This group does 64-bit operations on a 32-bit host.
10727 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010728 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010729 X86::AND32rr, X86::AND32rr,
10730 X86::AND32ri, X86::AND32ri,
10731 false);
10732 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010733 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010734 X86::OR32rr, X86::OR32rr,
10735 X86::OR32ri, X86::OR32ri,
10736 false);
10737 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010738 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010739 X86::XOR32rr, X86::XOR32rr,
10740 X86::XOR32ri, X86::XOR32ri,
10741 false);
10742 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010743 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010744 X86::AND32rr, X86::AND32rr,
10745 X86::AND32ri, X86::AND32ri,
10746 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010747 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010748 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010749 X86::ADD32rr, X86::ADC32rr,
10750 X86::ADD32ri, X86::ADC32ri,
10751 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010752 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010753 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010754 X86::SUB32rr, X86::SBB32rr,
10755 X86::SUB32ri, X86::SBB32ri,
10756 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010757 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010758 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010759 X86::MOV32rr, X86::MOV32rr,
10760 X86::MOV32ri, X86::MOV32ri,
10761 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010762 case X86::VASTART_SAVE_XMM_REGS:
10763 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010764
10765 case X86::VAARG_64:
10766 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010767 }
10768}
10769
10770//===----------------------------------------------------------------------===//
10771// X86 Optimization Hooks
10772//===----------------------------------------------------------------------===//
10773
Dan Gohman475871a2008-07-27 21:46:04 +000010774void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010775 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010776 APInt &KnownZero,
10777 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010778 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010779 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010780 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010781 assert((Opc >= ISD::BUILTIN_OP_END ||
10782 Opc == ISD::INTRINSIC_WO_CHAIN ||
10783 Opc == ISD::INTRINSIC_W_CHAIN ||
10784 Opc == ISD::INTRINSIC_VOID) &&
10785 "Should use MaskedValueIsZero if you don't know whether Op"
10786 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010787
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010788 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010789 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010790 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010791 case X86ISD::ADD:
10792 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010793 case X86ISD::ADC:
10794 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010795 case X86ISD::SMUL:
10796 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010797 case X86ISD::INC:
10798 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010799 case X86ISD::OR:
10800 case X86ISD::XOR:
10801 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010802 // These nodes' second result is a boolean.
10803 if (Op.getResNo() == 0)
10804 break;
10805 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010806 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010807 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10808 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010809 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010810 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010811}
Chris Lattner259e97c2006-01-31 19:43:35 +000010812
Owen Andersonbc146b02010-09-21 20:42:50 +000010813unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10814 unsigned Depth) const {
10815 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10816 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10817 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010818
Owen Andersonbc146b02010-09-21 20:42:50 +000010819 // Fallback case.
10820 return 1;
10821}
10822
Evan Cheng206ee9d2006-07-07 08:33:52 +000010823/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010824/// node is a GlobalAddress + offset.
10825bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010826 const GlobalValue* &GA,
10827 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010828 if (N->getOpcode() == X86ISD::Wrapper) {
10829 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010830 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010831 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010832 return true;
10833 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010834 }
Evan Chengad4196b2008-05-12 19:56:52 +000010835 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010836}
10837
Evan Cheng206ee9d2006-07-07 08:33:52 +000010838/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10839/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10840/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010841/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010842static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010843 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010844 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010845 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010846
Eli Friedman7a5e5552009-06-07 06:52:44 +000010847 if (VT.getSizeInBits() != 128)
10848 return SDValue();
10849
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010850 // Don't create instructions with illegal types after legalize types has run.
10851 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10852 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10853 return SDValue();
10854
Nate Begemanfdea31a2010-03-24 20:49:50 +000010855 SmallVector<SDValue, 16> Elts;
10856 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010857 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010858
Nate Begemanfdea31a2010-03-24 20:49:50 +000010859 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010860}
Evan Chengd880b972008-05-09 21:53:03 +000010861
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010862/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10863/// generation and convert it from being a bunch of shuffles and extracts
10864/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010865static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10866 const TargetLowering &TLI) {
10867 SDValue InputVector = N->getOperand(0);
10868
10869 // Only operate on vectors of 4 elements, where the alternative shuffling
10870 // gets to be more expensive.
10871 if (InputVector.getValueType() != MVT::v4i32)
10872 return SDValue();
10873
10874 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10875 // single use which is a sign-extend or zero-extend, and all elements are
10876 // used.
10877 SmallVector<SDNode *, 4> Uses;
10878 unsigned ExtractedElements = 0;
10879 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10880 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10881 if (UI.getUse().getResNo() != InputVector.getResNo())
10882 return SDValue();
10883
10884 SDNode *Extract = *UI;
10885 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10886 return SDValue();
10887
10888 if (Extract->getValueType(0) != MVT::i32)
10889 return SDValue();
10890 if (!Extract->hasOneUse())
10891 return SDValue();
10892 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10893 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10894 return SDValue();
10895 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10896 return SDValue();
10897
10898 // Record which element was extracted.
10899 ExtractedElements |=
10900 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10901
10902 Uses.push_back(Extract);
10903 }
10904
10905 // If not all the elements were used, this may not be worthwhile.
10906 if (ExtractedElements != 15)
10907 return SDValue();
10908
10909 // Ok, we've now decided to do the transformation.
10910 DebugLoc dl = InputVector.getDebugLoc();
10911
10912 // Store the value to a temporary stack slot.
10913 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010914 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10915 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010916
10917 // Replace each use (extract) with a load of the appropriate element.
10918 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10919 UE = Uses.end(); UI != UE; ++UI) {
10920 SDNode *Extract = *UI;
10921
10922 // Compute the element's address.
10923 SDValue Idx = Extract->getOperand(1);
10924 unsigned EltSize =
10925 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10926 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10927 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10928
Eric Christopher90eb4022010-07-22 00:26:08 +000010929 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010930 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010931
10932 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010933 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010934 ScalarAddr, MachinePointerInfo(),
10935 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010936
10937 // Replace the exact with the load.
10938 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10939 }
10940
10941 // The replacement was made in place; don't return anything.
10942 return SDValue();
10943}
10944
Chris Lattner83e6c992006-10-04 06:57:07 +000010945/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010946static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010947 const X86Subtarget *Subtarget) {
10948 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010949 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010950 // Get the LHS/RHS of the select.
10951 SDValue LHS = N->getOperand(1);
10952 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010953
Dan Gohman670e5392009-09-21 18:03:22 +000010954 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010955 // instructions match the semantics of the common C idiom x<y?x:y but not
10956 // x<=y?x:y, because of how they handle negative zero (which can be
10957 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010958 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010959 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010960 Cond.getOpcode() == ISD::SETCC) {
10961 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010962
Chris Lattner47b4ce82009-03-11 05:48:52 +000010963 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010964 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010965 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10966 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010967 switch (CC) {
10968 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010969 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010970 // Converting this to a min would handle NaNs incorrectly, and swapping
10971 // the operands would cause it to handle comparisons between positive
10972 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010973 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010974 if (!UnsafeFPMath &&
10975 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10976 break;
10977 std::swap(LHS, RHS);
10978 }
Dan Gohman670e5392009-09-21 18:03:22 +000010979 Opcode = X86ISD::FMIN;
10980 break;
10981 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010982 // Converting this to a min would handle comparisons between positive
10983 // and negative zero incorrectly.
10984 if (!UnsafeFPMath &&
10985 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10986 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010987 Opcode = X86ISD::FMIN;
10988 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010989 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010990 // Converting this to a min would handle both negative zeros and NaNs
10991 // incorrectly, but we can swap the operands to fix both.
10992 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010993 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010994 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010995 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010996 Opcode = X86ISD::FMIN;
10997 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010998
Dan Gohman670e5392009-09-21 18:03:22 +000010999 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011000 // Converting this to a max would handle comparisons between positive
11001 // and negative zero incorrectly.
11002 if (!UnsafeFPMath &&
11003 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11004 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011005 Opcode = X86ISD::FMAX;
11006 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011007 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011008 // Converting this to a max would handle NaNs incorrectly, and swapping
11009 // the operands would cause it to handle comparisons between positive
11010 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011011 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011012 if (!UnsafeFPMath &&
11013 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11014 break;
11015 std::swap(LHS, RHS);
11016 }
Dan Gohman670e5392009-09-21 18:03:22 +000011017 Opcode = X86ISD::FMAX;
11018 break;
11019 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011020 // Converting this to a max would handle both negative zeros and NaNs
11021 // incorrectly, but we can swap the operands to fix both.
11022 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011023 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011024 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011025 case ISD::SETGE:
11026 Opcode = X86ISD::FMAX;
11027 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011028 }
Dan Gohman670e5392009-09-21 18:03:22 +000011029 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011030 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11031 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011032 switch (CC) {
11033 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011034 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011035 // Converting this to a min would handle comparisons between positive
11036 // and negative zero incorrectly, and swapping the operands would
11037 // cause it to handle NaNs incorrectly.
11038 if (!UnsafeFPMath &&
11039 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011040 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011041 break;
11042 std::swap(LHS, RHS);
11043 }
Dan Gohman670e5392009-09-21 18:03:22 +000011044 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011045 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011046 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011047 // Converting this to a min would handle NaNs incorrectly.
11048 if (!UnsafeFPMath &&
11049 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11050 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011051 Opcode = X86ISD::FMIN;
11052 break;
11053 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011054 // Converting this to a min would handle both negative zeros and NaNs
11055 // incorrectly, but we can swap the operands to fix both.
11056 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011057 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011058 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011059 case ISD::SETGE:
11060 Opcode = X86ISD::FMIN;
11061 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011062
Dan Gohman670e5392009-09-21 18:03:22 +000011063 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011064 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011065 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011066 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011067 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011068 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011069 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011070 // Converting this to a max would handle comparisons between positive
11071 // and negative zero incorrectly, and swapping the operands would
11072 // cause it to handle NaNs incorrectly.
11073 if (!UnsafeFPMath &&
11074 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011075 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011076 break;
11077 std::swap(LHS, RHS);
11078 }
Dan Gohman670e5392009-09-21 18:03:22 +000011079 Opcode = X86ISD::FMAX;
11080 break;
11081 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011082 // Converting this to a max would handle both negative zeros and NaNs
11083 // incorrectly, but we can swap the operands to fix both.
11084 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011085 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011086 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011087 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011088 Opcode = X86ISD::FMAX;
11089 break;
11090 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011091 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011092
Chris Lattner47b4ce82009-03-11 05:48:52 +000011093 if (Opcode)
11094 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011095 }
Eric Christopherfd179292009-08-27 18:07:15 +000011096
Chris Lattnerd1980a52009-03-12 06:52:53 +000011097 // If this is a select between two integer constants, try to do some
11098 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011099 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11100 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011101 // Don't do this for crazy integer types.
11102 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11103 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011104 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011105 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011106
Chris Lattnercee56e72009-03-13 05:53:31 +000011107 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011108 // Efficiently invertible.
11109 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11110 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11111 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11112 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011113 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011114 }
Eric Christopherfd179292009-08-27 18:07:15 +000011115
Chris Lattnerd1980a52009-03-12 06:52:53 +000011116 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011117 if (FalseC->getAPIntValue() == 0 &&
11118 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011119 if (NeedsCondInvert) // Invert the condition if needed.
11120 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11121 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011122
Chris Lattnerd1980a52009-03-12 06:52:53 +000011123 // Zero extend the condition if needed.
11124 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011125
Chris Lattnercee56e72009-03-13 05:53:31 +000011126 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011127 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011128 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011129 }
Eric Christopherfd179292009-08-27 18:07:15 +000011130
Chris Lattner97a29a52009-03-13 05:22:11 +000011131 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011132 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011133 if (NeedsCondInvert) // Invert the condition if needed.
11134 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11135 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011136
Chris Lattner97a29a52009-03-13 05:22:11 +000011137 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011138 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11139 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011140 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011141 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011142 }
Eric Christopherfd179292009-08-27 18:07:15 +000011143
Chris Lattnercee56e72009-03-13 05:53:31 +000011144 // Optimize cases that will turn into an LEA instruction. This requires
11145 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011146 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011147 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011148 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011149
Chris Lattnercee56e72009-03-13 05:53:31 +000011150 bool isFastMultiplier = false;
11151 if (Diff < 10) {
11152 switch ((unsigned char)Diff) {
11153 default: break;
11154 case 1: // result = add base, cond
11155 case 2: // result = lea base( , cond*2)
11156 case 3: // result = lea base(cond, cond*2)
11157 case 4: // result = lea base( , cond*4)
11158 case 5: // result = lea base(cond, cond*4)
11159 case 8: // result = lea base( , cond*8)
11160 case 9: // result = lea base(cond, cond*8)
11161 isFastMultiplier = true;
11162 break;
11163 }
11164 }
Eric Christopherfd179292009-08-27 18:07:15 +000011165
Chris Lattnercee56e72009-03-13 05:53:31 +000011166 if (isFastMultiplier) {
11167 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11168 if (NeedsCondInvert) // Invert the condition if needed.
11169 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11170 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011171
Chris Lattnercee56e72009-03-13 05:53:31 +000011172 // Zero extend the condition if needed.
11173 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11174 Cond);
11175 // Scale the condition by the difference.
11176 if (Diff != 1)
11177 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11178 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011179
Chris Lattnercee56e72009-03-13 05:53:31 +000011180 // Add the base if non-zero.
11181 if (FalseC->getAPIntValue() != 0)
11182 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11183 SDValue(FalseC, 0));
11184 return Cond;
11185 }
Eric Christopherfd179292009-08-27 18:07:15 +000011186 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011187 }
11188 }
Eric Christopherfd179292009-08-27 18:07:15 +000011189
Dan Gohman475871a2008-07-27 21:46:04 +000011190 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011191}
11192
Chris Lattnerd1980a52009-03-12 06:52:53 +000011193/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11194static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11195 TargetLowering::DAGCombinerInfo &DCI) {
11196 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011197
Chris Lattnerd1980a52009-03-12 06:52:53 +000011198 // If the flag operand isn't dead, don't touch this CMOV.
11199 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11200 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011201
Chris Lattnerd1980a52009-03-12 06:52:53 +000011202 // If this is a select between two integer constants, try to do some
11203 // optimizations. Note that the operands are ordered the opposite of SELECT
11204 // operands.
11205 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11206 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11207 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11208 // larger than FalseC (the false value).
11209 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011210
Chris Lattnerd1980a52009-03-12 06:52:53 +000011211 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11212 CC = X86::GetOppositeBranchCondition(CC);
11213 std::swap(TrueC, FalseC);
11214 }
Eric Christopherfd179292009-08-27 18:07:15 +000011215
Chris Lattnerd1980a52009-03-12 06:52:53 +000011216 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011217 // This is efficient for any integer data type (including i8/i16) and
11218 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011219 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11220 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011221 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11222 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011223
Chris Lattnerd1980a52009-03-12 06:52:53 +000011224 // Zero extend the condition if needed.
11225 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011226
Chris Lattnerd1980a52009-03-12 06:52:53 +000011227 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11228 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011229 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011230 if (N->getNumValues() == 2) // Dead flag value?
11231 return DCI.CombineTo(N, Cond, SDValue());
11232 return Cond;
11233 }
Eric Christopherfd179292009-08-27 18:07:15 +000011234
Chris Lattnercee56e72009-03-13 05:53:31 +000011235 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11236 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011237 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11238 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011239 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11240 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011241
Chris Lattner97a29a52009-03-13 05:22:11 +000011242 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011243 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11244 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011245 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11246 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011247
Chris Lattner97a29a52009-03-13 05:22:11 +000011248 if (N->getNumValues() == 2) // Dead flag value?
11249 return DCI.CombineTo(N, Cond, SDValue());
11250 return Cond;
11251 }
Eric Christopherfd179292009-08-27 18:07:15 +000011252
Chris Lattnercee56e72009-03-13 05:53:31 +000011253 // Optimize cases that will turn into an LEA instruction. This requires
11254 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011255 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011256 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011257 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011258
Chris Lattnercee56e72009-03-13 05:53:31 +000011259 bool isFastMultiplier = false;
11260 if (Diff < 10) {
11261 switch ((unsigned char)Diff) {
11262 default: break;
11263 case 1: // result = add base, cond
11264 case 2: // result = lea base( , cond*2)
11265 case 3: // result = lea base(cond, cond*2)
11266 case 4: // result = lea base( , cond*4)
11267 case 5: // result = lea base(cond, cond*4)
11268 case 8: // result = lea base( , cond*8)
11269 case 9: // result = lea base(cond, cond*8)
11270 isFastMultiplier = true;
11271 break;
11272 }
11273 }
Eric Christopherfd179292009-08-27 18:07:15 +000011274
Chris Lattnercee56e72009-03-13 05:53:31 +000011275 if (isFastMultiplier) {
11276 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11277 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011278 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11279 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011280 // Zero extend the condition if needed.
11281 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11282 Cond);
11283 // Scale the condition by the difference.
11284 if (Diff != 1)
11285 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11286 DAG.getConstant(Diff, Cond.getValueType()));
11287
11288 // Add the base if non-zero.
11289 if (FalseC->getAPIntValue() != 0)
11290 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11291 SDValue(FalseC, 0));
11292 if (N->getNumValues() == 2) // Dead flag value?
11293 return DCI.CombineTo(N, Cond, SDValue());
11294 return Cond;
11295 }
Eric Christopherfd179292009-08-27 18:07:15 +000011296 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011297 }
11298 }
11299 return SDValue();
11300}
11301
11302
Evan Cheng0b0cd912009-03-28 05:57:29 +000011303/// PerformMulCombine - Optimize a single multiply with constant into two
11304/// in order to implement it with two cheaper instructions, e.g.
11305/// LEA + SHL, LEA + LEA.
11306static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11307 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011308 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11309 return SDValue();
11310
Owen Andersone50ed302009-08-10 22:56:29 +000011311 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011312 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011313 return SDValue();
11314
11315 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11316 if (!C)
11317 return SDValue();
11318 uint64_t MulAmt = C->getZExtValue();
11319 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11320 return SDValue();
11321
11322 uint64_t MulAmt1 = 0;
11323 uint64_t MulAmt2 = 0;
11324 if ((MulAmt % 9) == 0) {
11325 MulAmt1 = 9;
11326 MulAmt2 = MulAmt / 9;
11327 } else if ((MulAmt % 5) == 0) {
11328 MulAmt1 = 5;
11329 MulAmt2 = MulAmt / 5;
11330 } else if ((MulAmt % 3) == 0) {
11331 MulAmt1 = 3;
11332 MulAmt2 = MulAmt / 3;
11333 }
11334 if (MulAmt2 &&
11335 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11336 DebugLoc DL = N->getDebugLoc();
11337
11338 if (isPowerOf2_64(MulAmt2) &&
11339 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11340 // If second multiplifer is pow2, issue it first. We want the multiply by
11341 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11342 // is an add.
11343 std::swap(MulAmt1, MulAmt2);
11344
11345 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011346 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011347 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011348 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011349 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011350 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011351 DAG.getConstant(MulAmt1, VT));
11352
Eric Christopherfd179292009-08-27 18:07:15 +000011353 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011354 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011355 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011356 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011357 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011358 DAG.getConstant(MulAmt2, VT));
11359
11360 // Do not add new nodes to DAG combiner worklist.
11361 DCI.CombineTo(N, NewMul, false);
11362 }
11363 return SDValue();
11364}
11365
Evan Chengad9c0a32009-12-15 00:53:42 +000011366static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11367 SDValue N0 = N->getOperand(0);
11368 SDValue N1 = N->getOperand(1);
11369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11370 EVT VT = N0.getValueType();
11371
11372 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11373 // since the result of setcc_c is all zero's or all ones.
11374 if (N1C && N0.getOpcode() == ISD::AND &&
11375 N0.getOperand(1).getOpcode() == ISD::Constant) {
11376 SDValue N00 = N0.getOperand(0);
11377 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11378 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11379 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11380 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11381 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11382 APInt ShAmt = N1C->getAPIntValue();
11383 Mask = Mask.shl(ShAmt);
11384 if (Mask != 0)
11385 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11386 N00, DAG.getConstant(Mask, VT));
11387 }
11388 }
11389
11390 return SDValue();
11391}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011392
Nate Begeman740ab032009-01-26 00:52:55 +000011393/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11394/// when possible.
11395static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11396 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011397 EVT VT = N->getValueType(0);
11398 if (!VT.isVector() && VT.isInteger() &&
11399 N->getOpcode() == ISD::SHL)
11400 return PerformSHLCombine(N, DAG);
11401
Nate Begeman740ab032009-01-26 00:52:55 +000011402 // On X86 with SSE2 support, we can transform this to a vector shift if
11403 // all elements are shifted by the same amount. We can't do this in legalize
11404 // because the a constant vector is typically transformed to a constant pool
11405 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011406 if (!Subtarget->hasSSE2())
11407 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011408
Owen Anderson825b72b2009-08-11 20:47:22 +000011409 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011410 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011411
Mon P Wang3becd092009-01-28 08:12:05 +000011412 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011413 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011414 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011415 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011416 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11417 unsigned NumElts = VT.getVectorNumElements();
11418 unsigned i = 0;
11419 for (; i != NumElts; ++i) {
11420 SDValue Arg = ShAmtOp.getOperand(i);
11421 if (Arg.getOpcode() == ISD::UNDEF) continue;
11422 BaseShAmt = Arg;
11423 break;
11424 }
11425 for (; i != NumElts; ++i) {
11426 SDValue Arg = ShAmtOp.getOperand(i);
11427 if (Arg.getOpcode() == ISD::UNDEF) continue;
11428 if (Arg != BaseShAmt) {
11429 return SDValue();
11430 }
11431 }
11432 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011433 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011434 SDValue InVec = ShAmtOp.getOperand(0);
11435 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11436 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11437 unsigned i = 0;
11438 for (; i != NumElts; ++i) {
11439 SDValue Arg = InVec.getOperand(i);
11440 if (Arg.getOpcode() == ISD::UNDEF) continue;
11441 BaseShAmt = Arg;
11442 break;
11443 }
11444 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11445 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011446 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011447 if (C->getZExtValue() == SplatIdx)
11448 BaseShAmt = InVec.getOperand(1);
11449 }
11450 }
11451 if (BaseShAmt.getNode() == 0)
11452 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11453 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011454 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011455 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011456
Mon P Wangefa42202009-09-03 19:56:25 +000011457 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011458 if (EltVT.bitsGT(MVT::i32))
11459 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11460 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011461 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011462
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011463 // The shift amount is identical so we can do a vector shift.
11464 SDValue ValOp = N->getOperand(0);
11465 switch (N->getOpcode()) {
11466 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011467 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011468 break;
11469 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011470 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011471 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011472 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011473 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011474 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011475 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011476 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011477 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011478 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011479 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011480 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011481 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011482 break;
11483 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011484 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011485 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011486 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011487 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011488 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011489 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011490 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011491 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011492 break;
11493 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011494 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011495 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011496 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011497 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011498 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011499 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011500 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011501 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011502 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011503 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011504 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011505 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011506 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011507 }
11508 return SDValue();
11509}
11510
Nate Begemanb65c1752010-12-17 22:55:37 +000011511
11512static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11513 TargetLowering::DAGCombinerInfo &DCI,
11514 const X86Subtarget *Subtarget) {
11515 if (DCI.isBeforeLegalizeOps())
11516 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011517
Nate Begemanb65c1752010-12-17 22:55:37 +000011518 // Want to form PANDN nodes, in the hopes of then easily combining them with
11519 // OR and AND nodes to form PBLEND/PSIGN.
11520 EVT VT = N->getValueType(0);
11521 if (VT != MVT::v2i64)
11522 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011523
Nate Begemanb65c1752010-12-17 22:55:37 +000011524 SDValue N0 = N->getOperand(0);
11525 SDValue N1 = N->getOperand(1);
11526 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011527
Nate Begemanb65c1752010-12-17 22:55:37 +000011528 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011529 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011530 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11531 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11532
11533 // Check RHS for vnot
11534 if (N1.getOpcode() == ISD::XOR &&
11535 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11536 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011537
Nate Begemanb65c1752010-12-17 22:55:37 +000011538 return SDValue();
11539}
11540
Evan Cheng760d1942010-01-04 21:22:48 +000011541static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011542 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011543 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011544 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011545 return SDValue();
11546
Evan Cheng760d1942010-01-04 21:22:48 +000011547 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011548 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011549 return SDValue();
11550
Evan Cheng760d1942010-01-04 21:22:48 +000011551 SDValue N0 = N->getOperand(0);
11552 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011553
Nate Begemanb65c1752010-12-17 22:55:37 +000011554 // look for psign/blend
11555 if (Subtarget->hasSSSE3()) {
11556 if (VT == MVT::v2i64) {
11557 // Canonicalize pandn to RHS
11558 if (N0.getOpcode() == X86ISD::PANDN)
11559 std::swap(N0, N1);
11560 // or (and (m, x), (pandn m, y))
11561 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11562 SDValue Mask = N1.getOperand(0);
11563 SDValue X = N1.getOperand(1);
11564 SDValue Y;
11565 if (N0.getOperand(0) == Mask)
11566 Y = N0.getOperand(1);
11567 if (N0.getOperand(1) == Mask)
11568 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011569
Nate Begemanb65c1752010-12-17 22:55:37 +000011570 // Check to see if the mask appeared in both the AND and PANDN and
11571 if (!Y.getNode())
11572 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011573
Nate Begemanb65c1752010-12-17 22:55:37 +000011574 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11575 if (Mask.getOpcode() != ISD::BITCAST ||
11576 X.getOpcode() != ISD::BITCAST ||
11577 Y.getOpcode() != ISD::BITCAST)
11578 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011579
Nate Begemanb65c1752010-12-17 22:55:37 +000011580 // Look through mask bitcast.
11581 Mask = Mask.getOperand(0);
11582 EVT MaskVT = Mask.getValueType();
11583
11584 // Validate that the Mask operand is a vector sra node. The sra node
11585 // will be an intrinsic.
11586 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11587 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011588
Nate Begemanb65c1752010-12-17 22:55:37 +000011589 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11590 // there is no psrai.b
11591 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11592 case Intrinsic::x86_sse2_psrai_w:
11593 case Intrinsic::x86_sse2_psrai_d:
11594 break;
11595 default: return SDValue();
11596 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011597
Nate Begemanb65c1752010-12-17 22:55:37 +000011598 // Check that the SRA is all signbits.
11599 SDValue SraC = Mask.getOperand(2);
11600 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11601 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11602 if ((SraAmt + 1) != EltBits)
11603 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011604
Nate Begemanb65c1752010-12-17 22:55:37 +000011605 DebugLoc DL = N->getDebugLoc();
11606
11607 // Now we know we at least have a plendvb with the mask val. See if
11608 // we can form a psignb/w/d.
11609 // psign = x.type == y.type == mask.type && y = sub(0, x);
11610 X = X.getOperand(0);
11611 Y = Y.getOperand(0);
11612 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11613 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11614 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11615 unsigned Opc = 0;
11616 switch (EltBits) {
11617 case 8: Opc = X86ISD::PSIGNB; break;
11618 case 16: Opc = X86ISD::PSIGNW; break;
11619 case 32: Opc = X86ISD::PSIGND; break;
11620 default: break;
11621 }
11622 if (Opc) {
11623 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11624 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11625 }
11626 }
11627 // PBLENDVB only available on SSE 4.1
11628 if (!Subtarget->hasSSE41())
11629 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011630
Nate Begemanb65c1752010-12-17 22:55:37 +000011631 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11632 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11633 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011634 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011635 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11636 }
11637 }
11638 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011639
Nate Begemanb65c1752010-12-17 22:55:37 +000011640 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011641 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11642 std::swap(N0, N1);
11643 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11644 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011645 if (!N0.hasOneUse() || !N1.hasOneUse())
11646 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011647
11648 SDValue ShAmt0 = N0.getOperand(1);
11649 if (ShAmt0.getValueType() != MVT::i8)
11650 return SDValue();
11651 SDValue ShAmt1 = N1.getOperand(1);
11652 if (ShAmt1.getValueType() != MVT::i8)
11653 return SDValue();
11654 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11655 ShAmt0 = ShAmt0.getOperand(0);
11656 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11657 ShAmt1 = ShAmt1.getOperand(0);
11658
11659 DebugLoc DL = N->getDebugLoc();
11660 unsigned Opc = X86ISD::SHLD;
11661 SDValue Op0 = N0.getOperand(0);
11662 SDValue Op1 = N1.getOperand(0);
11663 if (ShAmt0.getOpcode() == ISD::SUB) {
11664 Opc = X86ISD::SHRD;
11665 std::swap(Op0, Op1);
11666 std::swap(ShAmt0, ShAmt1);
11667 }
11668
Evan Cheng8b1190a2010-04-28 01:18:01 +000011669 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011670 if (ShAmt1.getOpcode() == ISD::SUB) {
11671 SDValue Sum = ShAmt1.getOperand(0);
11672 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011673 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11674 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11675 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11676 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011677 return DAG.getNode(Opc, DL, VT,
11678 Op0, Op1,
11679 DAG.getNode(ISD::TRUNCATE, DL,
11680 MVT::i8, ShAmt0));
11681 }
11682 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11683 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11684 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011685 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011686 return DAG.getNode(Opc, DL, VT,
11687 N0.getOperand(0), N1.getOperand(0),
11688 DAG.getNode(ISD::TRUNCATE, DL,
11689 MVT::i8, ShAmt0));
11690 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011691
Evan Cheng760d1942010-01-04 21:22:48 +000011692 return SDValue();
11693}
11694
Chris Lattner149a4e52008-02-22 02:09:43 +000011695/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011696static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011697 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011698 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11699 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011700 // A preferable solution to the general problem is to figure out the right
11701 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011702
11703 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011704 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011705 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011706 if (VT.getSizeInBits() != 64)
11707 return SDValue();
11708
Devang Patel578efa92009-06-05 21:57:13 +000011709 const Function *F = DAG.getMachineFunction().getFunction();
11710 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011711 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011712 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011713 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011714 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011715 isa<LoadSDNode>(St->getValue()) &&
11716 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11717 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011718 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011719 LoadSDNode *Ld = 0;
11720 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011721 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011722 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011723 // Must be a store of a load. We currently handle two cases: the load
11724 // is a direct child, and it's under an intervening TokenFactor. It is
11725 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011726 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011727 Ld = cast<LoadSDNode>(St->getChain());
11728 else if (St->getValue().hasOneUse() &&
11729 ChainVal->getOpcode() == ISD::TokenFactor) {
11730 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011731 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011732 TokenFactorIndex = i;
11733 Ld = cast<LoadSDNode>(St->getValue());
11734 } else
11735 Ops.push_back(ChainVal->getOperand(i));
11736 }
11737 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011738
Evan Cheng536e6672009-03-12 05:59:15 +000011739 if (!Ld || !ISD::isNormalLoad(Ld))
11740 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011741
Evan Cheng536e6672009-03-12 05:59:15 +000011742 // If this is not the MMX case, i.e. we are just turning i64 load/store
11743 // into f64 load/store, avoid the transformation if there are multiple
11744 // uses of the loaded value.
11745 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11746 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011747
Evan Cheng536e6672009-03-12 05:59:15 +000011748 DebugLoc LdDL = Ld->getDebugLoc();
11749 DebugLoc StDL = N->getDebugLoc();
11750 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11751 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11752 // pair instead.
11753 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011754 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011755 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11756 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011757 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011758 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011759 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011760 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011761 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011762 Ops.size());
11763 }
Evan Cheng536e6672009-03-12 05:59:15 +000011764 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011765 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011766 St->isVolatile(), St->isNonTemporal(),
11767 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011768 }
Evan Cheng536e6672009-03-12 05:59:15 +000011769
11770 // Otherwise, lower to two pairs of 32-bit loads / stores.
11771 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011772 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11773 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011774
Owen Anderson825b72b2009-08-11 20:47:22 +000011775 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011776 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011777 Ld->isVolatile(), Ld->isNonTemporal(),
11778 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011779 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011780 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011781 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011782 MinAlign(Ld->getAlignment(), 4));
11783
11784 SDValue NewChain = LoLd.getValue(1);
11785 if (TokenFactorIndex != -1) {
11786 Ops.push_back(LoLd);
11787 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011788 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011789 Ops.size());
11790 }
11791
11792 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011793 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11794 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011795
11796 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011797 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011798 St->isVolatile(), St->isNonTemporal(),
11799 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011800 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011801 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011802 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011803 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011804 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011805 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011806 }
Dan Gohman475871a2008-07-27 21:46:04 +000011807 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011808}
11809
Chris Lattner6cf73262008-01-25 06:14:17 +000011810/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11811/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011812static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011813 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11814 // F[X]OR(0.0, x) -> x
11815 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011816 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11817 if (C->getValueAPF().isPosZero())
11818 return N->getOperand(1);
11819 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11820 if (C->getValueAPF().isPosZero())
11821 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011822 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011823}
11824
11825/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011826static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011827 // FAND(0.0, x) -> 0.0
11828 // FAND(x, 0.0) -> 0.0
11829 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11830 if (C->getValueAPF().isPosZero())
11831 return N->getOperand(0);
11832 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11833 if (C->getValueAPF().isPosZero())
11834 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011835 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011836}
11837
Dan Gohmane5af2d32009-01-29 01:59:02 +000011838static SDValue PerformBTCombine(SDNode *N,
11839 SelectionDAG &DAG,
11840 TargetLowering::DAGCombinerInfo &DCI) {
11841 // BT ignores high bits in the bit index operand.
11842 SDValue Op1 = N->getOperand(1);
11843 if (Op1.hasOneUse()) {
11844 unsigned BitWidth = Op1.getValueSizeInBits();
11845 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11846 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011847 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11848 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011849 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011850 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11851 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11852 DCI.CommitTargetLoweringOpt(TLO);
11853 }
11854 return SDValue();
11855}
Chris Lattner83e6c992006-10-04 06:57:07 +000011856
Eli Friedman7a5e5552009-06-07 06:52:44 +000011857static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11858 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011859 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011860 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011861 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011862 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011863 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011864 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011865 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011866 }
11867 return SDValue();
11868}
11869
Evan Cheng2e489c42009-12-16 00:53:11 +000011870static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11871 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11872 // (and (i32 x86isd::setcc_carry), 1)
11873 // This eliminates the zext. This transformation is necessary because
11874 // ISD::SETCC is always legalized to i8.
11875 DebugLoc dl = N->getDebugLoc();
11876 SDValue N0 = N->getOperand(0);
11877 EVT VT = N->getValueType(0);
11878 if (N0.getOpcode() == ISD::AND &&
11879 N0.hasOneUse() &&
11880 N0.getOperand(0).hasOneUse()) {
11881 SDValue N00 = N0.getOperand(0);
11882 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11883 return SDValue();
11884 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11885 if (!C || C->getZExtValue() != 1)
11886 return SDValue();
11887 return DAG.getNode(ISD::AND, dl, VT,
11888 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11889 N00.getOperand(0), N00.getOperand(1)),
11890 DAG.getConstant(1, VT));
11891 }
11892
11893 return SDValue();
11894}
11895
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011896// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11897static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11898 unsigned X86CC = N->getConstantOperandVal(0);
11899 SDValue EFLAG = N->getOperand(1);
11900 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011901
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011902 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11903 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11904 // cases.
11905 if (X86CC == X86::COND_B)
11906 return DAG.getNode(ISD::AND, DL, MVT::i8,
11907 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11908 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11909 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011910
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011911 return SDValue();
11912}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011913
Chris Lattner23a01992010-12-20 01:37:09 +000011914// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11915static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11916 X86TargetLowering::DAGCombinerInfo &DCI) {
11917 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11918 // the result is either zero or one (depending on the input carry bit).
11919 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11920 if (X86::isZeroNode(N->getOperand(0)) &&
11921 X86::isZeroNode(N->getOperand(1)) &&
11922 // We don't have a good way to replace an EFLAGS use, so only do this when
11923 // dead right now.
11924 SDValue(N, 1).use_empty()) {
11925 DebugLoc DL = N->getDebugLoc();
11926 EVT VT = N->getValueType(0);
11927 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11928 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11929 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11930 DAG.getConstant(X86::COND_B,MVT::i8),
11931 N->getOperand(2)),
11932 DAG.getConstant(1, VT));
11933 return DCI.CombineTo(N, Res1, CarryOut);
11934 }
11935
11936 return SDValue();
11937}
11938
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011939// fold (add Y, (sete X, 0)) -> adc 0, Y
11940// (add Y, (setne X, 0)) -> sbb -1, Y
11941// (sub (sete X, 0), Y) -> sbb 0, Y
11942// (sub (setne X, 0), Y) -> adc -1, Y
11943static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11944 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011945
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011946 // Look through ZExts.
11947 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11948 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11949 return SDValue();
11950
11951 SDValue SetCC = Ext.getOperand(0);
11952 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11953 return SDValue();
11954
11955 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11956 if (CC != X86::COND_E && CC != X86::COND_NE)
11957 return SDValue();
11958
11959 SDValue Cmp = SetCC.getOperand(1);
11960 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011961 !X86::isZeroNode(Cmp.getOperand(1)) ||
11962 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011963 return SDValue();
11964
11965 SDValue CmpOp0 = Cmp.getOperand(0);
11966 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11967 DAG.getConstant(1, CmpOp0.getValueType()));
11968
11969 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11970 if (CC == X86::COND_NE)
11971 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11972 DL, OtherVal.getValueType(), OtherVal,
11973 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11974 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11975 DL, OtherVal.getValueType(), OtherVal,
11976 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11977}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011978
Dan Gohman475871a2008-07-27 21:46:04 +000011979SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011980 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011981 SelectionDAG &DAG = DCI.DAG;
11982 switch (N->getOpcode()) {
11983 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011984 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011985 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011986 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011987 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011988 case ISD::ADD:
11989 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011990 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011991 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011992 case ISD::SHL:
11993 case ISD::SRA:
11994 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011995 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011996 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011997 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011998 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011999 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12000 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012001 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012002 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012003 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012004 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012005 case X86ISD::SHUFPS: // Handle all target specific shuffles
12006 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012007 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012008 case X86ISD::PUNPCKHBW:
12009 case X86ISD::PUNPCKHWD:
12010 case X86ISD::PUNPCKHDQ:
12011 case X86ISD::PUNPCKHQDQ:
12012 case X86ISD::UNPCKHPS:
12013 case X86ISD::UNPCKHPD:
12014 case X86ISD::PUNPCKLBW:
12015 case X86ISD::PUNPCKLWD:
12016 case X86ISD::PUNPCKLDQ:
12017 case X86ISD::PUNPCKLQDQ:
12018 case X86ISD::UNPCKLPS:
12019 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012020 case X86ISD::VUNPCKLPS:
12021 case X86ISD::VUNPCKLPD:
12022 case X86ISD::VUNPCKLPSY:
12023 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012024 case X86ISD::MOVHLPS:
12025 case X86ISD::MOVLHPS:
12026 case X86ISD::PSHUFD:
12027 case X86ISD::PSHUFHW:
12028 case X86ISD::PSHUFLW:
12029 case X86ISD::MOVSS:
12030 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012031 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012032 }
12033
Dan Gohman475871a2008-07-27 21:46:04 +000012034 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012035}
12036
Evan Chenge5b51ac2010-04-17 06:13:15 +000012037/// isTypeDesirableForOp - Return true if the target has native support for
12038/// the specified value type and it is 'desirable' to use the type for the
12039/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12040/// instruction encodings are longer and some i16 instructions are slow.
12041bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12042 if (!isTypeLegal(VT))
12043 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012044 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012045 return true;
12046
12047 switch (Opc) {
12048 default:
12049 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012050 case ISD::LOAD:
12051 case ISD::SIGN_EXTEND:
12052 case ISD::ZERO_EXTEND:
12053 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012054 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012055 case ISD::SRL:
12056 case ISD::SUB:
12057 case ISD::ADD:
12058 case ISD::MUL:
12059 case ISD::AND:
12060 case ISD::OR:
12061 case ISD::XOR:
12062 return false;
12063 }
12064}
12065
12066/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012067/// beneficial for dag combiner to promote the specified node. If true, it
12068/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012069bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012070 EVT VT = Op.getValueType();
12071 if (VT != MVT::i16)
12072 return false;
12073
Evan Cheng4c26e932010-04-19 19:29:22 +000012074 bool Promote = false;
12075 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012076 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012077 default: break;
12078 case ISD::LOAD: {
12079 LoadSDNode *LD = cast<LoadSDNode>(Op);
12080 // If the non-extending load has a single use and it's not live out, then it
12081 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012082 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12083 Op.hasOneUse()*/) {
12084 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12085 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12086 // The only case where we'd want to promote LOAD (rather then it being
12087 // promoted as an operand is when it's only use is liveout.
12088 if (UI->getOpcode() != ISD::CopyToReg)
12089 return false;
12090 }
12091 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012092 Promote = true;
12093 break;
12094 }
12095 case ISD::SIGN_EXTEND:
12096 case ISD::ZERO_EXTEND:
12097 case ISD::ANY_EXTEND:
12098 Promote = true;
12099 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012100 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012101 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012102 SDValue N0 = Op.getOperand(0);
12103 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012104 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012105 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012106 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012107 break;
12108 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012109 case ISD::ADD:
12110 case ISD::MUL:
12111 case ISD::AND:
12112 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012113 case ISD::XOR:
12114 Commute = true;
12115 // fallthrough
12116 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012117 SDValue N0 = Op.getOperand(0);
12118 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012119 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012120 return false;
12121 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012122 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012123 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012124 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012125 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012126 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012127 }
12128 }
12129
12130 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012131 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012132}
12133
Evan Cheng60c07e12006-07-05 22:17:51 +000012134//===----------------------------------------------------------------------===//
12135// X86 Inline Assembly Support
12136//===----------------------------------------------------------------------===//
12137
Chris Lattnerb8105652009-07-20 17:51:36 +000012138bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12139 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012140
12141 std::string AsmStr = IA->getAsmString();
12142
12143 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012144 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012145 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012146
12147 switch (AsmPieces.size()) {
12148 default: return false;
12149 case 1:
12150 AsmStr = AsmPieces[0];
12151 AsmPieces.clear();
12152 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12153
Evan Cheng55d42002011-01-08 01:24:27 +000012154 // FIXME: this should verify that we are targetting a 486 or better. If not,
12155 // we will turn this bswap into something that will be lowered to logical ops
12156 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12157 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012158 // bswap $0
12159 if (AsmPieces.size() == 2 &&
12160 (AsmPieces[0] == "bswap" ||
12161 AsmPieces[0] == "bswapq" ||
12162 AsmPieces[0] == "bswapl") &&
12163 (AsmPieces[1] == "$0" ||
12164 AsmPieces[1] == "${0:q}")) {
12165 // No need to check constraints, nothing other than the equivalent of
12166 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012167 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12168 if (!Ty || Ty->getBitWidth() % 16 != 0)
12169 return false;
12170 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012171 }
12172 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012173 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012174 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012175 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012176 AsmPieces[1] == "$$8," &&
12177 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012178 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12179 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012180 const std::string &ConstraintsStr = IA->getConstraintString();
12181 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012182 std::sort(AsmPieces.begin(), AsmPieces.end());
12183 if (AsmPieces.size() == 4 &&
12184 AsmPieces[0] == "~{cc}" &&
12185 AsmPieces[1] == "~{dirflag}" &&
12186 AsmPieces[2] == "~{flags}" &&
12187 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012188 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12189 if (!Ty || Ty->getBitWidth() % 16 != 0)
12190 return false;
12191 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012192 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012193 }
12194 break;
12195 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012196 if (CI->getType()->isIntegerTy(32) &&
12197 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12198 SmallVector<StringRef, 4> Words;
12199 SplitString(AsmPieces[0], Words, " \t,");
12200 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12201 Words[2] == "${0:w}") {
12202 Words.clear();
12203 SplitString(AsmPieces[1], Words, " \t,");
12204 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12205 Words[2] == "$0") {
12206 Words.clear();
12207 SplitString(AsmPieces[2], Words, " \t,");
12208 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12209 Words[2] == "${0:w}") {
12210 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012211 const std::string &ConstraintsStr = IA->getConstraintString();
12212 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012213 std::sort(AsmPieces.begin(), AsmPieces.end());
12214 if (AsmPieces.size() == 4 &&
12215 AsmPieces[0] == "~{cc}" &&
12216 AsmPieces[1] == "~{dirflag}" &&
12217 AsmPieces[2] == "~{flags}" &&
12218 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012219 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12220 if (!Ty || Ty->getBitWidth() % 16 != 0)
12221 return false;
12222 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012223 }
12224 }
12225 }
12226 }
12227 }
Evan Cheng55d42002011-01-08 01:24:27 +000012228
12229 if (CI->getType()->isIntegerTy(64)) {
12230 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12231 if (Constraints.size() >= 2 &&
12232 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12233 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12234 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12235 SmallVector<StringRef, 4> Words;
12236 SplitString(AsmPieces[0], Words, " \t");
12237 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012238 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012239 SplitString(AsmPieces[1], Words, " \t");
12240 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12241 Words.clear();
12242 SplitString(AsmPieces[2], Words, " \t,");
12243 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12244 Words[2] == "%edx") {
12245 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12246 if (!Ty || Ty->getBitWidth() % 16 != 0)
12247 return false;
12248 return IntrinsicLowering::LowerToByteSwap(CI);
12249 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012250 }
12251 }
12252 }
12253 }
12254 break;
12255 }
12256 return false;
12257}
12258
12259
12260
Chris Lattnerf4dff842006-07-11 02:54:03 +000012261/// getConstraintType - Given a constraint letter, return the type of
12262/// constraint it is for this target.
12263X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012264X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12265 if (Constraint.size() == 1) {
12266 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012267 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012268 case 'q':
12269 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012270 case 'f':
12271 case 't':
12272 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012273 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012274 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012275 case 'Y':
12276 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012277 case 'a':
12278 case 'b':
12279 case 'c':
12280 case 'd':
12281 case 'S':
12282 case 'D':
12283 case 'A':
12284 return C_Register;
12285 case 'I':
12286 case 'J':
12287 case 'K':
12288 case 'L':
12289 case 'M':
12290 case 'N':
12291 case 'G':
12292 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012293 case 'e':
12294 case 'Z':
12295 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012296 default:
12297 break;
12298 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012299 }
Chris Lattner4234f572007-03-25 02:14:49 +000012300 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012301}
12302
John Thompson44ab89e2010-10-29 17:29:13 +000012303/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012304/// This object must already have been set up with the operand type
12305/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012306TargetLowering::ConstraintWeight
12307 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012308 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012309 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012310 Value *CallOperandVal = info.CallOperandVal;
12311 // If we don't have a value, we can't do a match,
12312 // but allow it at the lowest weight.
12313 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012314 return CW_Default;
12315 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012316 // Look at the constraint type.
12317 switch (*constraint) {
12318 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012319 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12320 case 'R':
12321 case 'q':
12322 case 'Q':
12323 case 'a':
12324 case 'b':
12325 case 'c':
12326 case 'd':
12327 case 'S':
12328 case 'D':
12329 case 'A':
12330 if (CallOperandVal->getType()->isIntegerTy())
12331 weight = CW_SpecificReg;
12332 break;
12333 case 'f':
12334 case 't':
12335 case 'u':
12336 if (type->isFloatingPointTy())
12337 weight = CW_SpecificReg;
12338 break;
12339 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012340 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012341 weight = CW_SpecificReg;
12342 break;
12343 case 'x':
12344 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012345 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012346 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012347 break;
12348 case 'I':
12349 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12350 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012351 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012352 }
12353 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012354 case 'J':
12355 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12356 if (C->getZExtValue() <= 63)
12357 weight = CW_Constant;
12358 }
12359 break;
12360 case 'K':
12361 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12362 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12363 weight = CW_Constant;
12364 }
12365 break;
12366 case 'L':
12367 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12368 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12369 weight = CW_Constant;
12370 }
12371 break;
12372 case 'M':
12373 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12374 if (C->getZExtValue() <= 3)
12375 weight = CW_Constant;
12376 }
12377 break;
12378 case 'N':
12379 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12380 if (C->getZExtValue() <= 0xff)
12381 weight = CW_Constant;
12382 }
12383 break;
12384 case 'G':
12385 case 'C':
12386 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12387 weight = CW_Constant;
12388 }
12389 break;
12390 case 'e':
12391 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12392 if ((C->getSExtValue() >= -0x80000000LL) &&
12393 (C->getSExtValue() <= 0x7fffffffLL))
12394 weight = CW_Constant;
12395 }
12396 break;
12397 case 'Z':
12398 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12399 if (C->getZExtValue() <= 0xffffffff)
12400 weight = CW_Constant;
12401 }
12402 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012403 }
12404 return weight;
12405}
12406
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012407/// LowerXConstraint - try to replace an X constraint, which matches anything,
12408/// with another that has more specific requirements based on the type of the
12409/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012410const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012411LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012412 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12413 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012414 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012415 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012416 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012417 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012418 return "x";
12419 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012420
Chris Lattner5e764232008-04-26 23:02:14 +000012421 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012422}
12423
Chris Lattner48884cd2007-08-25 00:47:38 +000012424/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12425/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012426void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012427 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012428 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012429 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012430 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012431
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012432 switch (Constraint) {
12433 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012434 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012436 if (C->getZExtValue() <= 31) {
12437 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012438 break;
12439 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012440 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012441 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012442 case 'J':
12443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012444 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012445 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12446 break;
12447 }
12448 }
12449 return;
12450 case 'K':
12451 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012452 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012453 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12454 break;
12455 }
12456 }
12457 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012458 case 'N':
12459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012460 if (C->getZExtValue() <= 255) {
12461 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012462 break;
12463 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012464 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012465 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012466 case 'e': {
12467 // 32-bit signed value
12468 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012469 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12470 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012471 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012472 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012473 break;
12474 }
12475 // FIXME gcc accepts some relocatable values here too, but only in certain
12476 // memory models; it's complicated.
12477 }
12478 return;
12479 }
12480 case 'Z': {
12481 // 32-bit unsigned value
12482 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012483 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12484 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012485 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12486 break;
12487 }
12488 }
12489 // FIXME gcc accepts some relocatable values here too, but only in certain
12490 // memory models; it's complicated.
12491 return;
12492 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012493 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012494 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012495 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012496 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012497 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012498 break;
12499 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012500
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012501 // In any sort of PIC mode addresses need to be computed at runtime by
12502 // adding in a register or some sort of table lookup. These can't
12503 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012504 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012505 return;
12506
Chris Lattnerdc43a882007-05-03 16:52:29 +000012507 // If we are in non-pic codegen mode, we allow the address of a global (with
12508 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012509 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012510 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012511
Chris Lattner49921962009-05-08 18:23:14 +000012512 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12513 while (1) {
12514 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12515 Offset += GA->getOffset();
12516 break;
12517 } else if (Op.getOpcode() == ISD::ADD) {
12518 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12519 Offset += C->getZExtValue();
12520 Op = Op.getOperand(0);
12521 continue;
12522 }
12523 } else if (Op.getOpcode() == ISD::SUB) {
12524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12525 Offset += -C->getZExtValue();
12526 Op = Op.getOperand(0);
12527 continue;
12528 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012529 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012530
Chris Lattner49921962009-05-08 18:23:14 +000012531 // Otherwise, this isn't something we can handle, reject it.
12532 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012533 }
Eric Christopherfd179292009-08-27 18:07:15 +000012534
Dan Gohman46510a72010-04-15 01:51:59 +000012535 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012536 // If we require an extra load to get this address, as in PIC mode, we
12537 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012538 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12539 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012540 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012541
Devang Patel0d881da2010-07-06 22:08:15 +000012542 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12543 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012544 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012545 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012546 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012547
Gabor Greifba36cb52008-08-28 21:40:38 +000012548 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012549 Ops.push_back(Result);
12550 return;
12551 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012552 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012553}
12554
Chris Lattner259e97c2006-01-31 19:43:35 +000012555std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012556getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012557 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012558 if (Constraint.size() == 1) {
12559 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012560 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012561 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012562 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12563 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012564 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012565 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12566 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12567 X86::R10D,X86::R11D,X86::R12D,
12568 X86::R13D,X86::R14D,X86::R15D,
12569 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012570 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012571 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12572 X86::SI, X86::DI, X86::R8W,X86::R9W,
12573 X86::R10W,X86::R11W,X86::R12W,
12574 X86::R13W,X86::R14W,X86::R15W,
12575 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012576 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012577 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12578 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12579 X86::R10B,X86::R11B,X86::R12B,
12580 X86::R13B,X86::R14B,X86::R15B,
12581 X86::BPL, X86::SPL, 0);
12582
Owen Anderson825b72b2009-08-11 20:47:22 +000012583 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012584 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12585 X86::RSI, X86::RDI, X86::R8, X86::R9,
12586 X86::R10, X86::R11, X86::R12,
12587 X86::R13, X86::R14, X86::R15,
12588 X86::RBP, X86::RSP, 0);
12589
12590 break;
12591 }
Eric Christopherfd179292009-08-27 18:07:15 +000012592 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012593 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012594 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012595 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012596 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012597 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012598 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012599 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012600 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012601 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12602 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012603 }
12604 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012605
Chris Lattner1efa40f2006-02-22 00:56:39 +000012606 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012607}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012608
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012609std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012610X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012611 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012612 // First, see if this is a constraint that directly corresponds to an LLVM
12613 // register class.
12614 if (Constraint.size() == 1) {
12615 // GCC Constraint Letters
12616 switch (Constraint[0]) {
12617 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012618 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012619 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012620 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012621 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012622 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012623 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012624 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012625 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012626 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012627 case 'R': // LEGACY_REGS
12628 if (VT == MVT::i8)
12629 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12630 if (VT == MVT::i16)
12631 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12632 if (VT == MVT::i32 || !Subtarget->is64Bit())
12633 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12634 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012635 case 'f': // FP Stack registers.
12636 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12637 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012638 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012639 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012640 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012641 return std::make_pair(0U, X86::RFP64RegisterClass);
12642 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012643 case 'y': // MMX_REGS if MMX allowed.
12644 if (!Subtarget->hasMMX()) break;
12645 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012646 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012647 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012648 // FALL THROUGH.
12649 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012650 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012651
Owen Anderson825b72b2009-08-11 20:47:22 +000012652 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012653 default: break;
12654 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012655 case MVT::f32:
12656 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012657 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012658 case MVT::f64:
12659 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012660 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012661 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012662 case MVT::v16i8:
12663 case MVT::v8i16:
12664 case MVT::v4i32:
12665 case MVT::v2i64:
12666 case MVT::v4f32:
12667 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012668 return std::make_pair(0U, X86::VR128RegisterClass);
12669 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012670 break;
12671 }
12672 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012673
Chris Lattnerf76d1802006-07-31 23:26:50 +000012674 // Use the default implementation in TargetLowering to convert the register
12675 // constraint into a member of a register class.
12676 std::pair<unsigned, const TargetRegisterClass*> Res;
12677 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012678
12679 // Not found as a standard register?
12680 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012681 // Map st(0) -> st(7) -> ST0
12682 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12683 tolower(Constraint[1]) == 's' &&
12684 tolower(Constraint[2]) == 't' &&
12685 Constraint[3] == '(' &&
12686 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12687 Constraint[5] == ')' &&
12688 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012689
Chris Lattner56d77c72009-09-13 22:41:48 +000012690 Res.first = X86::ST0+Constraint[4]-'0';
12691 Res.second = X86::RFP80RegisterClass;
12692 return Res;
12693 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012694
Chris Lattner56d77c72009-09-13 22:41:48 +000012695 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012696 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012697 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012698 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012699 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012700 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012701
12702 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012703 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012704 Res.first = X86::EFLAGS;
12705 Res.second = X86::CCRRegisterClass;
12706 return Res;
12707 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012708
Dale Johannesen330169f2008-11-13 21:52:36 +000012709 // 'A' means EAX + EDX.
12710 if (Constraint == "A") {
12711 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012712 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012713 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012714 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012715 return Res;
12716 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012717
Chris Lattnerf76d1802006-07-31 23:26:50 +000012718 // Otherwise, check to see if this is a register class of the wrong value
12719 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12720 // turn into {ax},{dx}.
12721 if (Res.second->hasType(VT))
12722 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012723
Chris Lattnerf76d1802006-07-31 23:26:50 +000012724 // All of the single-register GCC register classes map their values onto
12725 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12726 // really want an 8-bit or 32-bit register, map to the appropriate register
12727 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012728 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012729 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012730 unsigned DestReg = 0;
12731 switch (Res.first) {
12732 default: break;
12733 case X86::AX: DestReg = X86::AL; break;
12734 case X86::DX: DestReg = X86::DL; break;
12735 case X86::CX: DestReg = X86::CL; break;
12736 case X86::BX: DestReg = X86::BL; break;
12737 }
12738 if (DestReg) {
12739 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012740 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012741 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012742 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012743 unsigned DestReg = 0;
12744 switch (Res.first) {
12745 default: break;
12746 case X86::AX: DestReg = X86::EAX; break;
12747 case X86::DX: DestReg = X86::EDX; break;
12748 case X86::CX: DestReg = X86::ECX; break;
12749 case X86::BX: DestReg = X86::EBX; break;
12750 case X86::SI: DestReg = X86::ESI; break;
12751 case X86::DI: DestReg = X86::EDI; break;
12752 case X86::BP: DestReg = X86::EBP; break;
12753 case X86::SP: DestReg = X86::ESP; break;
12754 }
12755 if (DestReg) {
12756 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012757 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012758 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012759 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012760 unsigned DestReg = 0;
12761 switch (Res.first) {
12762 default: break;
12763 case X86::AX: DestReg = X86::RAX; break;
12764 case X86::DX: DestReg = X86::RDX; break;
12765 case X86::CX: DestReg = X86::RCX; break;
12766 case X86::BX: DestReg = X86::RBX; break;
12767 case X86::SI: DestReg = X86::RSI; break;
12768 case X86::DI: DestReg = X86::RDI; break;
12769 case X86::BP: DestReg = X86::RBP; break;
12770 case X86::SP: DestReg = X86::RSP; break;
12771 }
12772 if (DestReg) {
12773 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012774 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012775 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012776 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012777 } else if (Res.second == X86::FR32RegisterClass ||
12778 Res.second == X86::FR64RegisterClass ||
12779 Res.second == X86::VR128RegisterClass) {
12780 // Handle references to XMM physical registers that got mapped into the
12781 // wrong class. This can happen with constraints like {xmm0} where the
12782 // target independent register mapper will just pick the first match it can
12783 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012784 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012785 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012786 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012787 Res.second = X86::FR64RegisterClass;
12788 else if (X86::VR128RegisterClass->hasType(VT))
12789 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012790 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012791
Chris Lattnerf76d1802006-07-31 23:26:50 +000012792 return Res;
12793}