blob: 09ec69d9803605c79b3e582696f6d5e65e162e10 [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
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001274// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001275unsigned
1276X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1277 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001278 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001279
1280 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001281 switch (RC->getID()) {
1282 default:
1283 return 0;
1284 case X86::GR32RegClassID:
1285 return 4 - FPDiff;
1286 case X86::GR64RegClassID:
1287 return 8 - FPDiff;
1288 case X86::VR128RegClassID:
1289 return Subtarget->is64Bit() ? 10 : 4;
1290 case X86::VR64RegClassID:
1291 return 4;
1292 }
1293}
1294
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001295bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1296 unsigned &Offset) const {
1297 if (!Subtarget->isTargetLinux())
1298 return false;
1299
1300 if (Subtarget->is64Bit()) {
1301 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1302 Offset = 0x28;
1303 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1304 AddressSpace = 256;
1305 else
1306 AddressSpace = 257;
1307 } else {
1308 // %gs:0x14 on i386
1309 Offset = 0x14;
1310 AddressSpace = 256;
1311 }
1312 return true;
1313}
1314
1315
Chris Lattner2b02a442007-02-25 08:29:00 +00001316//===----------------------------------------------------------------------===//
1317// Return Value Calling Convention Implementation
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner59ed56b2007-02-28 04:55:35 +00001320#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001321
Michael J. Spencerec38de22010-10-10 22:04:20 +00001322bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001323X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001324 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001325 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001326 SmallVector<CCValAssign, 16> RVLocs;
1327 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001328 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001329 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001330}
1331
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332SDValue
1333X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001334 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001335 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001336 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001337 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001338 MachineFunction &MF = DAG.getMachineFunction();
1339 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Chris Lattner9774c912007-02-27 05:28:59 +00001341 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1343 RVLocs, *DAG.getContext());
1344 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001345
Evan Chengdcea1632010-02-04 02:40:39 +00001346 // Add the regs to the liveout set for the function.
1347 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1348 for (unsigned i = 0; i != RVLocs.size(); ++i)
1349 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1350 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001353
Dan Gohman475871a2008-07-27 21:46:04 +00001354 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001355 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1356 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001357 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1358 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001359
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001360 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001361 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1362 CCValAssign &VA = RVLocs[i];
1363 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001364 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001365 EVT ValVT = ValToCopy.getValueType();
1366
Dale Johannesenc4510512010-09-24 19:05:48 +00001367 // If this is x86-64, and we disabled SSE, we can't return FP values,
1368 // or SSE or MMX vectors.
1369 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1370 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001371 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001372 report_fatal_error("SSE register return with SSE disabled");
1373 }
1374 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1375 // llvm-gcc has never done it right and no one has noticed, so this
1376 // should be OK for now.
1377 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001378 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001379 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001380
Chris Lattner447ff682008-03-11 03:23:40 +00001381 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1382 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001383 if (VA.getLocReg() == X86::ST0 ||
1384 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001385 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1386 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001387 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001388 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001389 RetOps.push_back(ValToCopy);
1390 // Don't emit a copytoreg.
1391 continue;
1392 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001393
Evan Cheng242b38b2009-02-23 09:03:22 +00001394 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1395 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001396 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001397 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001398 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001399 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001400 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1401 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001402 // If we don't have SSE2 available, convert to v4f32 so the generated
1403 // register is legal.
1404 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001405 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001406 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001408 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001409
Dale Johannesendd64c412009-02-04 00:33:20 +00001410 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001411 Flag = Chain.getValue(1);
1412 }
Dan Gohman61a92132008-04-21 23:59:07 +00001413
1414 // The x86-64 ABI for returning structs by value requires that we copy
1415 // the sret argument into %rax for the return. We saved the argument into
1416 // a virtual register in the entry block, so now we copy the value out
1417 // and into %rax.
1418 if (Subtarget->is64Bit() &&
1419 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1420 MachineFunction &MF = DAG.getMachineFunction();
1421 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1422 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001423 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001424 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001425 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001426
Dale Johannesendd64c412009-02-04 00:33:20 +00001427 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001428 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001429
1430 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001431 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001433
Chris Lattner447ff682008-03-11 03:23:40 +00001434 RetOps[0] = Chain; // Update chain.
1435
1436 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001437 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001438 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001439
1440 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001442}
1443
Evan Cheng3d2125c2010-11-30 23:55:39 +00001444bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1445 if (N->getNumValues() != 1)
1446 return false;
1447 if (!N->hasNUsesOfValue(1, 0))
1448 return false;
1449
1450 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001451 if (Copy->getOpcode() != ISD::CopyToReg &&
1452 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001453 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001454
1455 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001456 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001457 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001458 if (UI->getOpcode() != X86ISD::RET_FLAG)
1459 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001460 HasRet = true;
1461 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001462
Evan Cheng1bf891a2010-12-01 22:59:46 +00001463 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001464}
1465
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466/// LowerCallResult - Lower the result values of a call into the
1467/// appropriate copies out of appropriate physical registers.
1468///
1469SDValue
1470X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001471 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472 const SmallVectorImpl<ISD::InputArg> &Ins,
1473 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001474 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001475
Chris Lattnere32bbf62007-02-28 07:09:55 +00001476 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001477 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001478 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001480 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001482
Chris Lattner3085e152007-02-25 08:59:22 +00001483 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001484 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001485 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001486 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001487
Torok Edwin3f142c32009-02-01 18:15:56 +00001488 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001489 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001490 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001491 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001492 }
1493
Evan Cheng79fb3b42009-02-20 20:43:02 +00001494 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001495
1496 // If this is a call to a function that returns an fp value on the floating
1497 // point stack, we must guarantee the the value is popped from the stack, so
1498 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1499 // if the return value is not used. We use the FpGET_ST0 instructions
1500 // instead.
1501 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1502 // If we prefer to use the value in xmm registers, copy it out as f80 and
1503 // use a truncate to move it from fp stack reg to xmm reg.
1504 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1505 bool isST0 = VA.getLocReg() == X86::ST0;
1506 unsigned Opc = 0;
1507 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1508 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1509 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1510 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001511 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001512 Ops, 2), 1);
1513 Val = Chain.getValue(0);
1514
1515 // Round the f80 to the right size, which also moves it to the appropriate
1516 // xmm register.
1517 if (CopyVT != VA.getValVT())
1518 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1519 // This truncation won't change the value.
1520 DAG.getIntPtrConstant(1));
1521 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001522 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1523 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1524 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001526 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1528 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001529 } else {
1530 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001532 Val = Chain.getValue(0);
1533 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001534 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001535 } else {
1536 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1537 CopyVT, InFlag).getValue(1);
1538 Val = Chain.getValue(0);
1539 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001540 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001542 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001543
Dan Gohman98ca4f22009-08-05 01:29:28 +00001544 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001545}
1546
1547
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001548//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001549// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001550//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001551// StdCall calling convention seems to be standard for many Windows' API
1552// routines and around. It differs from C calling convention just a little:
1553// callee should clean up the stack, not caller. Symbols should be also
1554// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001555// For info on fast calling convention see Fast Calling Convention (tail call)
1556// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001557
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001559/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1561 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001562 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001563
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001565}
1566
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001567/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001568/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001569static bool
1570ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1571 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001572 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001573
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001575}
1576
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001577/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1578/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001579/// the specific parameter attribute. The copy will be passed as a byval
1580/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001581static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001582CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001583 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1584 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001585 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001586
Dale Johannesendd64c412009-02-04 00:33:20 +00001587 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001588 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001589 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001590}
1591
Chris Lattner29689432010-03-11 00:22:57 +00001592/// IsTailCallConvention - Return true if the calling convention is one that
1593/// supports tail call optimization.
1594static bool IsTailCallConvention(CallingConv::ID CC) {
1595 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1596}
1597
Evan Cheng0c439eb2010-01-27 00:07:07 +00001598/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1599/// a tailcall target by changing its ABI.
1600static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001601 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001602}
1603
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604SDValue
1605X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001606 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001607 const SmallVectorImpl<ISD::InputArg> &Ins,
1608 DebugLoc dl, SelectionDAG &DAG,
1609 const CCValAssign &VA,
1610 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001611 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001612 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001613 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001614 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001615 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001616 EVT ValVT;
1617
1618 // If value is passed by pointer we have address passed instead of the value
1619 // itself.
1620 if (VA.getLocInfo() == CCValAssign::Indirect)
1621 ValVT = VA.getLocVT();
1622 else
1623 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001624
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001625 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001626 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001627 // In case of tail call optimization mark all arguments mutable. Since they
1628 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001629 if (Flags.isByVal()) {
1630 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001631 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001632 return DAG.getFrameIndex(FI, getPointerTy());
1633 } else {
1634 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001635 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001636 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1637 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001638 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001639 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001640 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001641}
1642
Dan Gohman475871a2008-07-27 21:46:04 +00001643SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001645 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646 bool isVarArg,
1647 const SmallVectorImpl<ISD::InputArg> &Ins,
1648 DebugLoc dl,
1649 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001650 SmallVectorImpl<SDValue> &InVals)
1651 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001652 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001653 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001654
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 const Function* Fn = MF.getFunction();
1656 if (Fn->hasExternalLinkage() &&
1657 Subtarget->isTargetCygMing() &&
1658 Fn->getName() == "main")
1659 FuncInfo->setForceFramePointer(true);
1660
Evan Cheng1bc78042006-04-26 01:20:17 +00001661 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001664
Chris Lattner29689432010-03-11 00:22:57 +00001665 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1666 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001667
Chris Lattner638402b2007-02-28 07:00:42 +00001668 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001669 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001670 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1671 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001672
1673 // Allocate shadow area for Win64
1674 if (IsWin64) {
1675 CCInfo.AllocateStack(32, 8);
1676 }
1677
Duncan Sands45907662010-10-31 13:21:44 +00001678 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001679
Chris Lattnerf39f7712007-02-28 05:46:49 +00001680 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001681 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001682 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1683 CCValAssign &VA = ArgLocs[i];
1684 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1685 // places.
1686 assert(VA.getValNo() != LastVal &&
1687 "Don't support value assigned to multiple locs yet");
1688 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Chris Lattnerf39f7712007-02-28 05:46:49 +00001690 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001691 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001692 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001694 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001700 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001701 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1702 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001703 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001704 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001705 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001706 RC = X86::VR64RegisterClass;
1707 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001708 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001709
Devang Patel68e6bee2011-02-21 23:21:26 +00001710 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712
Chris Lattnerf39f7712007-02-28 05:46:49 +00001713 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1714 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1715 // right size.
1716 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001717 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001718 DAG.getValueType(VA.getValVT()));
1719 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001720 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001721 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001722 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001723 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001725 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001726 // Handle MMX values passed in XMM regs.
1727 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001728 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1729 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001730 } else
1731 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001732 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001733 } else {
1734 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001736 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001737
1738 // If value is passed via pointer - do a load.
1739 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001740 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1741 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001742
Dan Gohman98ca4f22009-08-05 01:29:28 +00001743 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001744 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001745
Dan Gohman61a92132008-04-21 23:59:07 +00001746 // The x86-64 ABI for returning structs by value requires that we copy
1747 // the sret argument into %rax for the return. Save the argument into
1748 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001749 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001750 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1751 unsigned Reg = FuncInfo->getSRetReturnReg();
1752 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001754 FuncInfo->setSRetReturnReg(Reg);
1755 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001758 }
1759
Chris Lattnerf39f7712007-02-28 05:46:49 +00001760 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001761 // Align stack specially for tail calls.
1762 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001763 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001764
Evan Cheng1bc78042006-04-26 01:20:17 +00001765 // If the function takes variable number of arguments, make a frame index for
1766 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001767 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001768 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1769 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001770 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001771 }
1772 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001773 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1774
1775 // FIXME: We should really autogenerate these arrays
1776 static const unsigned GPR64ArgRegsWin64[] = {
1777 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001778 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001779 static const unsigned GPR64ArgRegs64Bit[] = {
1780 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1781 };
1782 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1784 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1785 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001786 const unsigned *GPR64ArgRegs;
1787 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001788
1789 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001790 // The XMM registers which might contain var arg parameters are shadowed
1791 // in their paired GPR. So we only need to save the GPR to their home
1792 // slots.
1793 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001794 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001795 } else {
1796 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1797 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001798
1799 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001800 }
1801 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1802 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001803
Devang Patel578efa92009-06-05 21:57:13 +00001804 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001805 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001806 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001807 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001808 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001809 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001810 // Kernel mode asks for SSE to be disabled, so don't push them
1811 // on the stack.
1812 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001813
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001814 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001815 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001816 // Get to the caller-allocated home save location. Add 8 to account
1817 // for the return address.
1818 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001819 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001820 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001821 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1822 } else {
1823 // For X86-64, if there are vararg parameters that are passed via
1824 // registers, then we must store them to their spots on the stack so they
1825 // may be loaded by deferencing the result of va_next.
1826 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1827 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1828 FuncInfo->setRegSaveFrameIndex(
1829 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001830 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001831 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001832
Gordon Henriksen86737662008-01-05 16:56:59 +00001833 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001834 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001835 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1836 getPointerTy());
1837 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001838 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001839 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1840 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001841 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001842 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001843 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001845 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001846 MachinePointerInfo::getFixedStack(
1847 FuncInfo->getRegSaveFrameIndex(), Offset),
1848 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001850 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001852
Dan Gohmanface41a2009-08-16 21:24:25 +00001853 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1854 // Now store the XMM (fp + vector) parameter registers.
1855 SmallVector<SDValue, 11> SaveXMMOps;
1856 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001857
Devang Patel68e6bee2011-02-21 23:21:26 +00001858 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001859 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1860 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001861
Dan Gohman1e93df62010-04-17 14:41:14 +00001862 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1863 FuncInfo->getRegSaveFrameIndex()));
1864 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1865 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001866
Dan Gohmanface41a2009-08-16 21:24:25 +00001867 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001868 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patel68e6bee2011-02-21 23:21:26 +00001869 X86::VR128RegisterClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00001870 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1871 SaveXMMOps.push_back(Val);
1872 }
1873 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1874 MVT::Other,
1875 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001876 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001877
1878 if (!MemOps.empty())
1879 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1880 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001882 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001883
Gordon Henriksen86737662008-01-05 16:56:59 +00001884 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001885 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001886 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001887 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001888 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001889 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001890 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001891 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001892 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001893
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001895 // RegSaveFrameIndex is X86-64 only.
1896 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001897 if (CallConv == CallingConv::X86_FastCall ||
1898 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001899 // fastcc functions can't have varargs.
1900 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001901 }
Evan Cheng25caf632006-05-23 21:06:34 +00001902
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001904}
1905
Dan Gohman475871a2008-07-27 21:46:04 +00001906SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001907X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1908 SDValue StackPtr, SDValue Arg,
1909 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001910 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001911 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001912 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001914 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001915 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001916 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001917
1918 return DAG.getStore(Chain, dl, Arg, PtrOff,
1919 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001920 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001921}
1922
Bill Wendling64e87322009-01-16 19:25:27 +00001923/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001924/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001925SDValue
1926X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001927 SDValue &OutRetAddr, SDValue Chain,
1928 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001929 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001930 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001931 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001932 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001933
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001934 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001935 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1936 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001937 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938}
1939
1940/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1941/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001942static SDValue
1943EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001945 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001946 // Store the return address to the appropriate stack slot.
1947 if (!FPDiff) return Chain;
1948 // Calculate the new stack slot for the return address.
1949 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001950 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001951 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001953 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001954 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001955 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001956 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001957 return Chain;
1958}
1959
Dan Gohman98ca4f22009-08-05 01:29:28 +00001960SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001961X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001962 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001963 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001965 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001966 const SmallVectorImpl<ISD::InputArg> &Ins,
1967 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001968 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001969 MachineFunction &MF = DAG.getMachineFunction();
1970 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00001971 bool IsWin64 = Subtarget->isTargetWin64();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001973 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974
Evan Cheng5f941932010-02-05 02:21:12 +00001975 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001976 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001977 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1978 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001979 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001980
1981 // Sibcalls are automatically detected tailcalls which do not require
1982 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001983 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001984 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001985
1986 if (isTailCall)
1987 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001988 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001989
Chris Lattner29689432010-03-11 00:22:57 +00001990 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1991 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001992
Chris Lattner638402b2007-02-28 07:00:42 +00001993 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001994 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1996 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001997
1998 // Allocate shadow area for Win64
1999 if (IsWin64) {
2000 CCInfo.AllocateStack(32, 8);
2001 }
2002
Duncan Sands45907662010-10-31 13:21:44 +00002003 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Chris Lattner423c5f42007-02-28 05:31:48 +00002005 // Get a count of how many bytes are to be pushed on the stack.
2006 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002007 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002008 // This is a sibcall. The memory operands are available in caller's
2009 // own caller's stack.
2010 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00002011 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002012 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002013
Gordon Henriksen86737662008-01-05 16:56:59 +00002014 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002015 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002016 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002017 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002018 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2019 FPDiff = NumBytesCallerPushed - NumBytes;
2020
2021 // Set the delta of movement of the returnaddr stackslot.
2022 // But only set if delta is greater than previous delta.
2023 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2024 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2025 }
2026
Evan Chengf22f9b32010-02-06 03:28:46 +00002027 if (!IsSibcall)
2028 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002029
Dan Gohman475871a2008-07-27 21:46:04 +00002030 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002031 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002032 if (isTailCall && FPDiff)
2033 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2034 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002035
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2037 SmallVector<SDValue, 8> MemOpChains;
2038 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002039
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002040 // Walk the register/memloc assignments, inserting copies/loads. In the case
2041 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002042 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2043 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002044 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002045 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002046 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002047 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002048
Chris Lattner423c5f42007-02-28 05:31:48 +00002049 // Promote the value if needed.
2050 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002051 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002052 case CCValAssign::Full: break;
2053 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002054 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002055 break;
2056 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002057 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002058 break;
2059 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002060 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2061 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002062 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2064 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002065 } else
2066 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2067 break;
2068 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002069 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002070 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002071 case CCValAssign::Indirect: {
2072 // Store the argument.
2073 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002074 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002075 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002076 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002077 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002078 Arg = SpillSlot;
2079 break;
2080 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002081 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002082
Chris Lattner423c5f42007-02-28 05:31:48 +00002083 if (VA.isRegLoc()) {
2084 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002085 if (isVarArg && IsWin64) {
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002086 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2087 // shadow reg if callee is a varargs function.
2088 unsigned ShadowReg = 0;
2089 switch (VA.getLocReg()) {
2090 case X86::XMM0: ShadowReg = X86::RCX; break;
2091 case X86::XMM1: ShadowReg = X86::RDX; break;
2092 case X86::XMM2: ShadowReg = X86::R8; break;
2093 case X86::XMM3: ShadowReg = X86::R9; break;
2094 }
2095 if (ShadowReg)
2096 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
2097 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002098 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002099 assert(VA.isMemLoc());
2100 if (StackPtr.getNode() == 0)
2101 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2102 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2103 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002104 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002106
Evan Cheng32fe1032006-05-25 00:59:30 +00002107 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002108 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002109 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002110
Evan Cheng347d5f72006-04-28 21:29:37 +00002111 // Build a sequence of copy-to-reg nodes chained together with token chain
2112 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002114 // Tail call byval lowering might overwrite argument registers so in case of
2115 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002117 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002118 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002119 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002120 InFlag = Chain.getValue(1);
2121 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002122
Chris Lattner88e1fd52009-07-09 04:24:46 +00002123 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002124 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2125 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002126 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002127 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2128 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00002129 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002130 InFlag);
2131 InFlag = Chain.getValue(1);
2132 } else {
2133 // If we are tail calling and generating PIC/GOT style code load the
2134 // address of the callee into ECX. The value in ecx is used as target of
2135 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2136 // for tail calls on PIC/GOT architectures. Normally we would just put the
2137 // address of GOT into ebx and then call target@PLT. But for tail calls
2138 // ebx would be restored (since ebx is callee saved) before jumping to the
2139 // target@PLT.
2140
2141 // Note: The actual moving to ECX is done further down.
2142 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2143 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2144 !G->getGlobal()->hasProtectedVisibility())
2145 Callee = LowerGlobalAddress(Callee, DAG);
2146 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002147 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002148 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002149 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002150
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002151 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002152 // From AMD64 ABI document:
2153 // For calls that may call functions that use varargs or stdargs
2154 // (prototype-less calls or calls to functions containing ellipsis (...) in
2155 // the declaration) %al is used as hidden argument to specify the number
2156 // of SSE registers used. The contents of %al do not need to match exactly
2157 // the number of registers, but must be an ubound on the number of SSE
2158 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002159
Gordon Henriksen86737662008-01-05 16:56:59 +00002160 // Count the number of XMM registers allocated.
2161 static const unsigned XMMArgRegs[] = {
2162 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2163 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2164 };
2165 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002166 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002167 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002168
Dale Johannesendd64c412009-02-04 00:33:20 +00002169 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002170 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002171 InFlag = Chain.getValue(1);
2172 }
2173
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002174
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002175 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 if (isTailCall) {
2177 // Force all the incoming stack arguments to be loaded from the stack
2178 // before any new outgoing arguments are stored to the stack, because the
2179 // outgoing stack slots may alias the incoming argument stack slots, and
2180 // the alias isn't otherwise explicit. This is slightly more conservative
2181 // than necessary, because it means that each store effectively depends
2182 // on every argument instead of just those arguments it would clobber.
2183 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2184
Dan Gohman475871a2008-07-27 21:46:04 +00002185 SmallVector<SDValue, 8> MemOpChains2;
2186 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002188 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002189 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002190 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002191 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2192 CCValAssign &VA = ArgLocs[i];
2193 if (VA.isRegLoc())
2194 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002195 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002196 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002198 // Create frame index.
2199 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002200 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002201 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002202 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002203
Duncan Sands276dcbd2008-03-21 09:14:45 +00002204 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002205 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002207 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002208 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002209 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002210 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002211
Dan Gohman98ca4f22009-08-05 01:29:28 +00002212 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2213 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002214 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002216 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002217 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002218 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002219 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002220 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002221 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 }
2223 }
2224
2225 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002227 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002228
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002229 // Copy arguments to their registers.
2230 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002231 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002232 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002233 InFlag = Chain.getValue(1);
2234 }
Dan Gohman475871a2008-07-27 21:46:04 +00002235 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002236
Gordon Henriksen86737662008-01-05 16:56:59 +00002237 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002239 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002240 }
2241
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002242 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2243 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2244 // In the 64-bit large code model, we have to make all calls
2245 // through a register, since the call instruction's 32-bit
2246 // pc-relative offset may not be large enough to hold the whole
2247 // address.
2248 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002249 // If the callee is a GlobalAddress node (quite common, every direct call
2250 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2251 // it.
2252
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002253 // We should use extra load for direct calls to dllimported functions in
2254 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002255 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002256 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002257 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002258
Chris Lattner48a7d022009-07-09 05:02:21 +00002259 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2260 // external symbols most go through the PLT in PIC mode. If the symbol
2261 // has hidden or protected visibility, or if it is static or local, then
2262 // we don't need to use the PLT - we can directly call it.
2263 if (Subtarget->isTargetELF() &&
2264 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002265 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002266 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002267 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002268 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2269 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002270 // PC-relative references to external symbols should go through $stub,
2271 // unless we're building with the leopard linker or later, which
2272 // automatically synthesizes these stubs.
2273 OpFlags = X86II::MO_DARWIN_STUB;
2274 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002275
Devang Patel0d881da2010-07-06 22:08:15 +00002276 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002277 G->getOffset(), OpFlags);
2278 }
Bill Wendling056292f2008-09-16 21:48:12 +00002279 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002280 unsigned char OpFlags = 0;
2281
Evan Cheng1bf891a2010-12-01 22:59:46 +00002282 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2283 // external symbols should go through the PLT.
2284 if (Subtarget->isTargetELF() &&
2285 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2286 OpFlags = X86II::MO_PLT;
2287 } else if (Subtarget->isPICStyleStubAny() &&
2288 Subtarget->getDarwinVers() < 9) {
2289 // PC-relative references to external symbols should go through $stub,
2290 // unless we're building with the leopard linker or later, which
2291 // automatically synthesizes these stubs.
2292 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002293 }
Eric Christopherfd179292009-08-27 18:07:15 +00002294
Chris Lattner48a7d022009-07-09 05:02:21 +00002295 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2296 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002297 }
2298
Chris Lattnerd96d0722007-02-25 06:40:16 +00002299 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002300 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002301 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002302
Evan Chengf22f9b32010-02-06 03:28:46 +00002303 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002304 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2305 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002306 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002308
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002309 Ops.push_back(Chain);
2310 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002311
Dan Gohman98ca4f22009-08-05 01:29:28 +00002312 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002314
Gordon Henriksen86737662008-01-05 16:56:59 +00002315 // Add argument registers to the end of the list so that they are known live
2316 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002317 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2318 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2319 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002320
Evan Cheng586ccac2008-03-18 23:36:35 +00002321 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002323 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2324
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002325 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002326 if (Is64Bit && isVarArg && !IsWin64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002328
Gabor Greifba36cb52008-08-28 21:40:38 +00002329 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002330 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002331
Dan Gohman98ca4f22009-08-05 01:29:28 +00002332 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002333 // We used to do:
2334 //// If this is the first return lowered for this function, add the regs
2335 //// to the liveout set for the function.
2336 // This isn't right, although it's probably harmless on x86; liveouts
2337 // should be computed from returns not tail calls. Consider a void
2338 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002339 return DAG.getNode(X86ISD::TC_RETURN, dl,
2340 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002341 }
2342
Dale Johannesenace16102009-02-03 19:33:06 +00002343 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002344 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002345
Chris Lattner2d297092006-05-23 18:50:38 +00002346 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002347 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002348 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002349 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002350 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002351 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002352 // pops the hidden struct pointer, so we have to push it back.
2353 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002354 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002356 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002357
Gordon Henriksenae636f82008-01-03 16:47:34 +00002358 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002359 if (!IsSibcall) {
2360 Chain = DAG.getCALLSEQ_END(Chain,
2361 DAG.getIntPtrConstant(NumBytes, true),
2362 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2363 true),
2364 InFlag);
2365 InFlag = Chain.getValue(1);
2366 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002367
Chris Lattner3085e152007-02-25 08:59:22 +00002368 // Handle result values, copying them out of physregs into vregs that we
2369 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002370 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2371 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002372}
2373
Evan Cheng25ab6902006-09-08 06:48:29 +00002374
2375//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002376// Fast Calling Convention (tail call) implementation
2377//===----------------------------------------------------------------------===//
2378
2379// Like std call, callee cleans arguments, convention except that ECX is
2380// reserved for storing the tail called function address. Only 2 registers are
2381// free for argument passing (inreg). Tail call optimization is performed
2382// provided:
2383// * tailcallopt is enabled
2384// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002385// On X86_64 architecture with GOT-style position independent code only local
2386// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002387// To keep the stack aligned according to platform abi the function
2388// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2389// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002390// If a tail called function callee has more arguments than the caller the
2391// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002392// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002393// original REtADDR, but before the saved framepointer or the spilled registers
2394// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2395// stack layout:
2396// arg1
2397// arg2
2398// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002399// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002400// move area ]
2401// (possible EBP)
2402// ESI
2403// EDI
2404// local1 ..
2405
2406/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2407/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002408unsigned
2409X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2410 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002411 MachineFunction &MF = DAG.getMachineFunction();
2412 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002413 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002414 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002415 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002416 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002417 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002418 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2419 // Number smaller than 12 so just add the difference.
2420 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2421 } else {
2422 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002423 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002424 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002425 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002426 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002427}
2428
Evan Cheng5f941932010-02-05 02:21:12 +00002429/// MatchingStackOffset - Return true if the given stack call argument is
2430/// already available in the same position (relatively) of the caller's
2431/// incoming argument stack.
2432static
2433bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2434 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2435 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002436 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2437 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002438 if (Arg.getOpcode() == ISD::CopyFromReg) {
2439 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002440 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002441 return false;
2442 MachineInstr *Def = MRI->getVRegDef(VR);
2443 if (!Def)
2444 return false;
2445 if (!Flags.isByVal()) {
2446 if (!TII->isLoadFromStackSlot(Def, FI))
2447 return false;
2448 } else {
2449 unsigned Opcode = Def->getOpcode();
2450 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2451 Def->getOperand(1).isFI()) {
2452 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002453 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002454 } else
2455 return false;
2456 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002457 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2458 if (Flags.isByVal())
2459 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002460 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002461 // define @foo(%struct.X* %A) {
2462 // tail call @bar(%struct.X* byval %A)
2463 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002464 return false;
2465 SDValue Ptr = Ld->getBasePtr();
2466 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2467 if (!FINode)
2468 return false;
2469 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002470 } else
2471 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002472
Evan Cheng4cae1332010-03-05 08:38:04 +00002473 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002474 if (!MFI->isFixedObjectIndex(FI))
2475 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002476 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002477}
2478
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2480/// for tail call optimization. Targets which want to do tail call
2481/// optimization should implement this function.
2482bool
2483X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002484 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002485 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002486 bool isCalleeStructRet,
2487 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002488 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002489 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002490 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002492 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002493 CalleeCC != CallingConv::C)
2494 return false;
2495
Evan Cheng7096ae42010-01-29 06:45:59 +00002496 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002497 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002498 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002499 CallingConv::ID CallerCC = CallerF->getCallingConv();
2500 bool CCMatch = CallerCC == CalleeCC;
2501
Dan Gohman1797ed52010-02-08 20:27:50 +00002502 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002503 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002504 return true;
2505 return false;
2506 }
2507
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002508 // Look for obvious safe cases to perform tail call optimization that do not
2509 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002510
Evan Cheng2c12cb42010-03-26 16:26:03 +00002511 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2512 // emit a special epilogue.
2513 if (RegInfo->needsStackRealignment(MF))
2514 return false;
2515
Eric Christopher90eb4022010-07-22 00:26:08 +00002516 // Do not sibcall optimize vararg calls unless the call site is not passing
2517 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002518 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002519 return false;
2520
Evan Chenga375d472010-03-15 18:54:48 +00002521 // Also avoid sibcall optimization if either caller or callee uses struct
2522 // return semantics.
2523 if (isCalleeStructRet || isCallerStructRet)
2524 return false;
2525
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002526 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2527 // Therefore if it's not used by the call it is not safe to optimize this into
2528 // a sibcall.
2529 bool Unused = false;
2530 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2531 if (!Ins[i].Used) {
2532 Unused = true;
2533 break;
2534 }
2535 }
2536 if (Unused) {
2537 SmallVector<CCValAssign, 16> RVLocs;
2538 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2539 RVLocs, *DAG.getContext());
2540 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002541 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002542 CCValAssign &VA = RVLocs[i];
2543 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2544 return false;
2545 }
2546 }
2547
Evan Cheng13617962010-04-30 01:12:32 +00002548 // If the calling conventions do not match, then we'd better make sure the
2549 // results are returned in the same way as what the caller expects.
2550 if (!CCMatch) {
2551 SmallVector<CCValAssign, 16> RVLocs1;
2552 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2553 RVLocs1, *DAG.getContext());
2554 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2555
2556 SmallVector<CCValAssign, 16> RVLocs2;
2557 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2558 RVLocs2, *DAG.getContext());
2559 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2560
2561 if (RVLocs1.size() != RVLocs2.size())
2562 return false;
2563 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2564 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2565 return false;
2566 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2567 return false;
2568 if (RVLocs1[i].isRegLoc()) {
2569 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2570 return false;
2571 } else {
2572 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2573 return false;
2574 }
2575 }
2576 }
2577
Evan Chenga6bff982010-01-30 01:22:00 +00002578 // If the callee takes no arguments then go on to check the results of the
2579 // call.
2580 if (!Outs.empty()) {
2581 // Check if stack adjustment is needed. For now, do not do this if any
2582 // argument is passed on the stack.
2583 SmallVector<CCValAssign, 16> ArgLocs;
2584 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2585 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002586
2587 // Allocate shadow area for Win64
2588 if (Subtarget->isTargetWin64()) {
2589 CCInfo.AllocateStack(32, 8);
2590 }
2591
Duncan Sands45907662010-10-31 13:21:44 +00002592 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002593 if (CCInfo.getNextStackOffset()) {
2594 MachineFunction &MF = DAG.getMachineFunction();
2595 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2596 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002597
2598 // Check if the arguments are already laid out in the right way as
2599 // the caller's fixed stack objects.
2600 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002601 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2602 const X86InstrInfo *TII =
2603 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002604 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2605 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002606 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002607 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002608 if (VA.getLocInfo() == CCValAssign::Indirect)
2609 return false;
2610 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002611 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2612 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002613 return false;
2614 }
2615 }
2616 }
Evan Cheng9c044672010-05-29 01:35:22 +00002617
2618 // If the tailcall address may be in a register, then make sure it's
2619 // possible to register allocate for it. In 32-bit, the call address can
2620 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002621 // callee-saved registers are restored. These happen to be the same
2622 // registers used to pass 'inreg' arguments so watch out for those.
2623 if (!Subtarget->is64Bit() &&
2624 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002625 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002626 unsigned NumInRegs = 0;
2627 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2628 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002629 if (!VA.isRegLoc())
2630 continue;
2631 unsigned Reg = VA.getLocReg();
2632 switch (Reg) {
2633 default: break;
2634 case X86::EAX: case X86::EDX: case X86::ECX:
2635 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002636 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002637 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002638 }
2639 }
2640 }
Evan Chenga6bff982010-01-30 01:22:00 +00002641 }
Evan Chengb1712452010-01-27 06:25:16 +00002642
Dale Johannesend155d7e2010-10-25 22:17:05 +00002643 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002644 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002645 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2646 return false;
2647
Evan Cheng86809cc2010-02-03 03:28:02 +00002648 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002649}
2650
Dan Gohman3df24e62008-09-03 23:12:08 +00002651FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002652X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2653 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002654}
2655
2656
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002657//===----------------------------------------------------------------------===//
2658// Other Lowering Hooks
2659//===----------------------------------------------------------------------===//
2660
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002661static bool MayFoldLoad(SDValue Op) {
2662 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2663}
2664
2665static bool MayFoldIntoStore(SDValue Op) {
2666 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2667}
2668
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002669static bool isTargetShuffle(unsigned Opcode) {
2670 switch(Opcode) {
2671 default: return false;
2672 case X86ISD::PSHUFD:
2673 case X86ISD::PSHUFHW:
2674 case X86ISD::PSHUFLW:
2675 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002676 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002677 case X86ISD::SHUFPS:
2678 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002679 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002680 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002681 case X86ISD::MOVLPS:
2682 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002683 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002684 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002685 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002686 case X86ISD::MOVSS:
2687 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002688 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002689 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002690 case X86ISD::VUNPCKLPS:
2691 case X86ISD::VUNPCKLPD:
2692 case X86ISD::VUNPCKLPSY:
2693 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002694 case X86ISD::PUNPCKLWD:
2695 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002696 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002697 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002698 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002699 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002700 case X86ISD::PUNPCKHWD:
2701 case X86ISD::PUNPCKHBW:
2702 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002703 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002704 return true;
2705 }
2706 return false;
2707}
2708
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002709static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002710 SDValue V1, SelectionDAG &DAG) {
2711 switch(Opc) {
2712 default: llvm_unreachable("Unknown x86 shuffle node");
2713 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002714 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002715 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002716 return DAG.getNode(Opc, dl, VT, V1);
2717 }
2718
2719 return SDValue();
2720}
2721
2722static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002723 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002724 switch(Opc) {
2725 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002726 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002727 case X86ISD::PSHUFHW:
2728 case X86ISD::PSHUFLW:
2729 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2730 }
2731
2732 return SDValue();
2733}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002734
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002735static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2736 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2737 switch(Opc) {
2738 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002739 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002740 case X86ISD::SHUFPD:
2741 case X86ISD::SHUFPS:
2742 return DAG.getNode(Opc, dl, VT, V1, V2,
2743 DAG.getConstant(TargetMask, MVT::i8));
2744 }
2745 return SDValue();
2746}
2747
2748static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2749 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2750 switch(Opc) {
2751 default: llvm_unreachable("Unknown x86 shuffle node");
2752 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002753 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002754 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002755 case X86ISD::MOVLPS:
2756 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002757 case X86ISD::MOVSS:
2758 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002759 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002760 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +00002761 case X86ISD::VUNPCKLPS:
2762 case X86ISD::VUNPCKLPD:
2763 case X86ISD::VUNPCKLPSY:
2764 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002765 case X86ISD::PUNPCKLWD:
2766 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002767 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002768 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002769 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002770 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002771 case X86ISD::PUNPCKHWD:
2772 case X86ISD::PUNPCKHBW:
2773 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002774 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002775 return DAG.getNode(Opc, dl, VT, V1, V2);
2776 }
2777 return SDValue();
2778}
2779
Dan Gohmand858e902010-04-17 15:26:15 +00002780SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002781 MachineFunction &MF = DAG.getMachineFunction();
2782 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2783 int ReturnAddrIndex = FuncInfo->getRAIndex();
2784
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002785 if (ReturnAddrIndex == 0) {
2786 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002787 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002788 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002789 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002790 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002791 }
2792
Evan Cheng25ab6902006-09-08 06:48:29 +00002793 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002794}
2795
2796
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002797bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2798 bool hasSymbolicDisplacement) {
2799 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002800 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002801 return false;
2802
2803 // If we don't have a symbolic displacement - we don't have any extra
2804 // restrictions.
2805 if (!hasSymbolicDisplacement)
2806 return true;
2807
2808 // FIXME: Some tweaks might be needed for medium code model.
2809 if (M != CodeModel::Small && M != CodeModel::Kernel)
2810 return false;
2811
2812 // For small code model we assume that latest object is 16MB before end of 31
2813 // bits boundary. We may also accept pretty large negative constants knowing
2814 // that all objects are in the positive half of address space.
2815 if (M == CodeModel::Small && Offset < 16*1024*1024)
2816 return true;
2817
2818 // For kernel code model we know that all object resist in the negative half
2819 // of 32bits address space. We may not accept negative offsets, since they may
2820 // be just off and we may accept pretty large positive ones.
2821 if (M == CodeModel::Kernel && Offset > 0)
2822 return true;
2823
2824 return false;
2825}
2826
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002827/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2828/// specific condition code, returning the condition code and the LHS/RHS of the
2829/// comparison to make.
2830static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2831 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002832 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002833 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2834 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2835 // X > -1 -> X == 0, jump !sign.
2836 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002837 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002838 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2839 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002840 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002841 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002842 // X < 1 -> X <= 0
2843 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002844 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002845 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002846 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002847
Evan Chengd9558e02006-01-06 00:43:03 +00002848 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002849 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002850 case ISD::SETEQ: return X86::COND_E;
2851 case ISD::SETGT: return X86::COND_G;
2852 case ISD::SETGE: return X86::COND_GE;
2853 case ISD::SETLT: return X86::COND_L;
2854 case ISD::SETLE: return X86::COND_LE;
2855 case ISD::SETNE: return X86::COND_NE;
2856 case ISD::SETULT: return X86::COND_B;
2857 case ISD::SETUGT: return X86::COND_A;
2858 case ISD::SETULE: return X86::COND_BE;
2859 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002860 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002862
Chris Lattner4c78e022008-12-23 23:42:27 +00002863 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002864
Chris Lattner4c78e022008-12-23 23:42:27 +00002865 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002866 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2867 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002868 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2869 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002870 }
2871
Chris Lattner4c78e022008-12-23 23:42:27 +00002872 switch (SetCCOpcode) {
2873 default: break;
2874 case ISD::SETOLT:
2875 case ISD::SETOLE:
2876 case ISD::SETUGT:
2877 case ISD::SETUGE:
2878 std::swap(LHS, RHS);
2879 break;
2880 }
2881
2882 // On a floating point condition, the flags are set as follows:
2883 // ZF PF CF op
2884 // 0 | 0 | 0 | X > Y
2885 // 0 | 0 | 1 | X < Y
2886 // 1 | 0 | 0 | X == Y
2887 // 1 | 1 | 1 | unordered
2888 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002889 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002890 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002891 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002892 case ISD::SETOLT: // flipped
2893 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002894 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002895 case ISD::SETOLE: // flipped
2896 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002897 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002898 case ISD::SETUGT: // flipped
2899 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002900 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002901 case ISD::SETUGE: // flipped
2902 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002903 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002904 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002905 case ISD::SETNE: return X86::COND_NE;
2906 case ISD::SETUO: return X86::COND_P;
2907 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002908 case ISD::SETOEQ:
2909 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002910 }
Evan Chengd9558e02006-01-06 00:43:03 +00002911}
2912
Evan Cheng4a460802006-01-11 00:33:36 +00002913/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2914/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002915/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002916static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002917 switch (X86CC) {
2918 default:
2919 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002920 case X86::COND_B:
2921 case X86::COND_BE:
2922 case X86::COND_E:
2923 case X86::COND_P:
2924 case X86::COND_A:
2925 case X86::COND_AE:
2926 case X86::COND_NE:
2927 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002928 return true;
2929 }
2930}
2931
Evan Chengeb2f9692009-10-27 19:56:55 +00002932/// isFPImmLegal - Returns true if the target can instruction select the
2933/// specified FP immediate natively. If false, the legalizer will
2934/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002935bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002936 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2937 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2938 return true;
2939 }
2940 return false;
2941}
2942
Nate Begeman9008ca62009-04-27 18:41:29 +00002943/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2944/// the specified range (L, H].
2945static bool isUndefOrInRange(int Val, int Low, int Hi) {
2946 return (Val < 0) || (Val >= Low && Val < Hi);
2947}
2948
2949/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2950/// specified value.
2951static bool isUndefOrEqual(int Val, int CmpVal) {
2952 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002953 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002955}
2956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2958/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2959/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002960static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002961 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002962 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 return (Mask[0] < 2 && Mask[1] < 2);
2965 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002966}
2967
Nate Begeman9008ca62009-04-27 18:41:29 +00002968bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002969 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 N->getMask(M);
2971 return ::isPSHUFDMask(M, N->getValueType(0));
2972}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2975/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002976static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002978 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002979
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 // Lower quadword copied in order or undef.
2981 for (int i = 0; i != 4; ++i)
2982 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002983 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002984
Evan Cheng506d3df2006-03-29 23:07:14 +00002985 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002986 for (int i = 4; i != 8; ++i)
2987 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002988 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002989
Evan Cheng506d3df2006-03-29 23:07:14 +00002990 return true;
2991}
2992
Nate Begeman9008ca62009-04-27 18:41:29 +00002993bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002994 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 N->getMask(M);
2996 return ::isPSHUFHWMask(M, N->getValueType(0));
2997}
Evan Cheng506d3df2006-03-29 23:07:14 +00002998
Nate Begeman9008ca62009-04-27 18:41:29 +00002999/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3000/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00003001static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003002 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00003003 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003004
Rafael Espindola15684b22009-04-24 12:40:33 +00003005 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003006 for (int i = 4; i != 8; ++i)
3007 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00003008 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003009
Rafael Espindola15684b22009-04-24 12:40:33 +00003010 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 for (int i = 0; i != 4; ++i)
3012 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00003013 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003014
Rafael Espindola15684b22009-04-24 12:40:33 +00003015 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003016}
3017
Nate Begeman9008ca62009-04-27 18:41:29 +00003018bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00003019 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00003020 N->getMask(M);
3021 return ::isPSHUFLWMask(M, N->getValueType(0));
3022}
3023
Nate Begemana09008b2009-10-19 02:17:23 +00003024/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3025/// is suitable for input to PALIGNR.
3026static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
3027 bool hasSSSE3) {
3028 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003029
Nate Begemana09008b2009-10-19 02:17:23 +00003030 // Do not handle v2i64 / v2f64 shuffles with palignr.
3031 if (e < 4 || !hasSSSE3)
3032 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003033
Nate Begemana09008b2009-10-19 02:17:23 +00003034 for (i = 0; i != e; ++i)
3035 if (Mask[i] >= 0)
3036 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003037
Nate Begemana09008b2009-10-19 02:17:23 +00003038 // All undef, not a palignr.
3039 if (i == e)
3040 return false;
3041
3042 // Determine if it's ok to perform a palignr with only the LHS, since we
3043 // don't have access to the actual shuffle elements to see if RHS is undef.
3044 bool Unary = Mask[i] < (int)e;
3045 bool NeedsUnary = false;
3046
3047 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003048
Nate Begemana09008b2009-10-19 02:17:23 +00003049 // Check the rest of the elements to see if they are consecutive.
3050 for (++i; i != e; ++i) {
3051 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00003052 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00003053 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003054
Nate Begemana09008b2009-10-19 02:17:23 +00003055 Unary = Unary && (m < (int)e);
3056 NeedsUnary = NeedsUnary || (m < s);
3057
3058 if (NeedsUnary && !Unary)
3059 return false;
3060 if (Unary && m != ((s+i) & (e-1)))
3061 return false;
3062 if (!Unary && m != (s+i))
3063 return false;
3064 }
3065 return true;
3066}
3067
3068bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
3069 SmallVector<int, 8> M;
3070 N->getMask(M);
3071 return ::isPALIGNRMask(M, N->getValueType(0), true);
3072}
3073
Evan Cheng14aed5e2006-03-24 01:18:28 +00003074/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3075/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00003076static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 int NumElems = VT.getVectorNumElements();
3078 if (NumElems != 2 && NumElems != 4)
3079 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003080
Nate Begeman9008ca62009-04-27 18:41:29 +00003081 int Half = NumElems / 2;
3082 for (int i = 0; i < Half; ++i)
3083 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003084 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003085 for (int i = Half; i < NumElems; ++i)
3086 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003087 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003088
Evan Cheng14aed5e2006-03-24 01:18:28 +00003089 return true;
3090}
3091
Nate Begeman9008ca62009-04-27 18:41:29 +00003092bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3093 SmallVector<int, 8> M;
3094 N->getMask(M);
3095 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003096}
3097
Evan Cheng213d2cf2007-05-17 18:45:50 +00003098/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00003099/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3100/// half elements to come from vector 1 (which would equal the dest.) and
3101/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00003102static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003104
3105 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003106 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003107
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 int Half = NumElems / 2;
3109 for (int i = 0; i < Half; ++i)
3110 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00003111 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 for (int i = Half; i < NumElems; ++i)
3113 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00003114 return false;
3115 return true;
3116}
3117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3119 SmallVector<int, 8> M;
3120 N->getMask(M);
3121 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003122}
3123
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003124/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3125/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00003126bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
3127 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003128 return false;
3129
Evan Cheng2064a2b2006-03-28 06:50:32 +00003130 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00003131 return isUndefOrEqual(N->getMaskElt(0), 6) &&
3132 isUndefOrEqual(N->getMaskElt(1), 7) &&
3133 isUndefOrEqual(N->getMaskElt(2), 2) &&
3134 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003135}
3136
Nate Begeman0b10b912009-11-07 23:17:15 +00003137/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3138/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3139/// <2, 3, 2, 3>
3140bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
3141 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00003142
Nate Begeman0b10b912009-11-07 23:17:15 +00003143 if (NumElems != 4)
3144 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003145
Nate Begeman0b10b912009-11-07 23:17:15 +00003146 return isUndefOrEqual(N->getMaskElt(0), 2) &&
3147 isUndefOrEqual(N->getMaskElt(1), 3) &&
3148 isUndefOrEqual(N->getMaskElt(2), 2) &&
3149 isUndefOrEqual(N->getMaskElt(3), 3);
3150}
3151
Evan Cheng5ced1d82006-04-06 23:23:56 +00003152/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3153/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003154bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3155 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003156
Evan Cheng5ced1d82006-04-06 23:23:56 +00003157 if (NumElems != 2 && NumElems != 4)
3158 return false;
3159
Evan Chengc5cdff22006-04-07 21:53:05 +00003160 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003162 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003163
Evan Chengc5cdff22006-04-07 21:53:05 +00003164 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003166 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003167
3168 return true;
3169}
3170
Nate Begeman0b10b912009-11-07 23:17:15 +00003171/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3172/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3173bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003175
Evan Cheng5ced1d82006-04-06 23:23:56 +00003176 if (NumElems != 2 && NumElems != 4)
3177 return false;
3178
Evan Chengc5cdff22006-04-07 21:53:05 +00003179 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003181 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003182
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 for (unsigned i = 0; i < NumElems/2; ++i)
3184 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003185 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003186
3187 return true;
3188}
3189
Evan Cheng0038e592006-03-28 00:39:58 +00003190/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3191/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003192static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003193 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003195 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003196 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003197
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3199 int BitI = Mask[i];
3200 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003201 if (!isUndefOrEqual(BitI, j))
3202 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003203 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003204 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003205 return false;
3206 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003207 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003208 return false;
3209 }
Evan Cheng0038e592006-03-28 00:39:58 +00003210 }
Evan Cheng0038e592006-03-28 00:39:58 +00003211 return true;
3212}
3213
Nate Begeman9008ca62009-04-27 18:41:29 +00003214bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3215 SmallVector<int, 8> M;
3216 N->getMask(M);
3217 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003218}
3219
Evan Cheng4fcb9222006-03-28 02:43:26 +00003220/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3221/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003222static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003223 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003225 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003226 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003227
Nate Begeman9008ca62009-04-27 18:41:29 +00003228 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3229 int BitI = Mask[i];
3230 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003231 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003232 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003233 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003234 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003235 return false;
3236 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003237 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003238 return false;
3239 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003240 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003241 return true;
3242}
3243
Nate Begeman9008ca62009-04-27 18:41:29 +00003244bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3245 SmallVector<int, 8> M;
3246 N->getMask(M);
3247 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003248}
3249
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003250/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3251/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3252/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003253static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003255 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003256 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003257
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3259 int BitI = Mask[i];
3260 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003261 if (!isUndefOrEqual(BitI, j))
3262 return false;
3263 if (!isUndefOrEqual(BitI1, j))
3264 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003265 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003266 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003267}
3268
Nate Begeman9008ca62009-04-27 18:41:29 +00003269bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3270 SmallVector<int, 8> M;
3271 N->getMask(M);
3272 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3273}
3274
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003275/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3276/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3277/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003278static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003279 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003280 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3281 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003282
Nate Begeman9008ca62009-04-27 18:41:29 +00003283 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3284 int BitI = Mask[i];
3285 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003286 if (!isUndefOrEqual(BitI, j))
3287 return false;
3288 if (!isUndefOrEqual(BitI1, j))
3289 return false;
3290 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003291 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003292}
3293
Nate Begeman9008ca62009-04-27 18:41:29 +00003294bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3295 SmallVector<int, 8> M;
3296 N->getMask(M);
3297 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3298}
3299
Evan Cheng017dcc62006-04-21 01:05:10 +00003300/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3301/// specifies a shuffle of elements that is suitable for input to MOVSS,
3302/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003303static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003304 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003305 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003306
3307 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003308
Nate Begeman9008ca62009-04-27 18:41:29 +00003309 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003310 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003311
Nate Begeman9008ca62009-04-27 18:41:29 +00003312 for (int i = 1; i < NumElts; ++i)
3313 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003314 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003315
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003316 return true;
3317}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003318
Nate Begeman9008ca62009-04-27 18:41:29 +00003319bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3320 SmallVector<int, 8> M;
3321 N->getMask(M);
3322 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003323}
3324
Evan Cheng017dcc62006-04-21 01:05:10 +00003325/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3326/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003327/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003328static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003329 bool V2IsSplat = false, bool V2IsUndef = false) {
3330 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003331 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003332 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003333
Nate Begeman9008ca62009-04-27 18:41:29 +00003334 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003335 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003336
Nate Begeman9008ca62009-04-27 18:41:29 +00003337 for (int i = 1; i < NumOps; ++i)
3338 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3339 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3340 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003341 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Evan Cheng39623da2006-04-20 08:58:49 +00003343 return true;
3344}
3345
Nate Begeman9008ca62009-04-27 18:41:29 +00003346static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003347 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003348 SmallVector<int, 8> M;
3349 N->getMask(M);
3350 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003351}
3352
Evan Chengd9539472006-04-14 21:59:03 +00003353/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3354/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003355bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3356 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003357 return false;
3358
3359 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003360 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 int Elt = N->getMaskElt(i);
3362 if (Elt >= 0 && Elt != 1)
3363 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003364 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003365
3366 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003367 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 int Elt = N->getMaskElt(i);
3369 if (Elt >= 0 && Elt != 3)
3370 return false;
3371 if (Elt == 3)
3372 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003373 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003374 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003376 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003377}
3378
3379/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3380/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003381bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3382 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003383 return false;
3384
3385 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 for (unsigned i = 0; i < 2; ++i)
3387 if (N->getMaskElt(i) > 0)
3388 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003389
3390 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003391 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 int Elt = N->getMaskElt(i);
3393 if (Elt >= 0 && Elt != 2)
3394 return false;
3395 if (Elt == 2)
3396 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003397 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003399 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003400}
3401
Evan Cheng0b457f02008-09-25 20:50:48 +00003402/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3403/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003404bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3405 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003406
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 for (int i = 0; i < e; ++i)
3408 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003409 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 for (int i = 0; i < e; ++i)
3411 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003412 return false;
3413 return true;
3414}
3415
David Greenec38a03e2011-02-03 15:50:00 +00003416/// isVEXTRACTF128Index - Return true if the specified
3417/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3418/// suitable for input to VEXTRACTF128.
3419bool X86::isVEXTRACTF128Index(SDNode *N) {
3420 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3421 return false;
3422
3423 // The index should be aligned on a 128-bit boundary.
3424 uint64_t Index =
3425 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3426
3427 unsigned VL = N->getValueType(0).getVectorNumElements();
3428 unsigned VBits = N->getValueType(0).getSizeInBits();
3429 unsigned ElSize = VBits / VL;
3430 bool Result = (Index * ElSize) % 128 == 0;
3431
3432 return Result;
3433}
3434
David Greeneccacdc12011-02-04 16:08:29 +00003435/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3436/// operand specifies a subvector insert that is suitable for input to
3437/// VINSERTF128.
3438bool X86::isVINSERTF128Index(SDNode *N) {
3439 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3440 return false;
3441
3442 // The index should be aligned on a 128-bit boundary.
3443 uint64_t Index =
3444 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3445
3446 unsigned VL = N->getValueType(0).getVectorNumElements();
3447 unsigned VBits = N->getValueType(0).getSizeInBits();
3448 unsigned ElSize = VBits / VL;
3449 bool Result = (Index * ElSize) % 128 == 0;
3450
3451 return Result;
3452}
3453
Evan Cheng63d33002006-03-22 08:01:21 +00003454/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003455/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003456unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003457 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3458 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3459
Evan Chengb9df0ca2006-03-22 02:53:00 +00003460 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3461 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 for (int i = 0; i < NumOperands; ++i) {
3463 int Val = SVOp->getMaskElt(NumOperands-i-1);
3464 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003465 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003466 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003467 if (i != NumOperands - 1)
3468 Mask <<= Shift;
3469 }
Evan Cheng63d33002006-03-22 08:01:21 +00003470 return Mask;
3471}
3472
Evan Cheng506d3df2006-03-29 23:07:14 +00003473/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003474/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003475unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003477 unsigned Mask = 0;
3478 // 8 nodes, but we only care about the last 4.
3479 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 int Val = SVOp->getMaskElt(i);
3481 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003482 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003483 if (i != 4)
3484 Mask <<= 2;
3485 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003486 return Mask;
3487}
3488
3489/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003490/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003491unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003493 unsigned Mask = 0;
3494 // 8 nodes, but we only care about the first 4.
3495 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 int Val = SVOp->getMaskElt(i);
3497 if (Val >= 0)
3498 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003499 if (i != 0)
3500 Mask <<= 2;
3501 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003502 return Mask;
3503}
3504
Nate Begemana09008b2009-10-19 02:17:23 +00003505/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3506/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3507unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3508 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3509 EVT VVT = N->getValueType(0);
3510 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3511 int Val = 0;
3512
3513 unsigned i, e;
3514 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3515 Val = SVOp->getMaskElt(i);
3516 if (Val >= 0)
3517 break;
3518 }
3519 return (Val - i) * EltSize;
3520}
3521
David Greenec38a03e2011-02-03 15:50:00 +00003522/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3523/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3524/// instructions.
3525unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3526 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3527 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3528
3529 uint64_t Index =
3530 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3531
3532 EVT VecVT = N->getOperand(0).getValueType();
3533 EVT ElVT = VecVT.getVectorElementType();
3534
3535 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3536
3537 return Index / NumElemsPerChunk;
3538}
3539
David Greeneccacdc12011-02-04 16:08:29 +00003540/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3541/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3542/// instructions.
3543unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3544 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3545 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3546
3547 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003548 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003549
3550 EVT VecVT = N->getValueType(0);
3551 EVT ElVT = VecVT.getVectorElementType();
3552
3553 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3554
3555 return Index / NumElemsPerChunk;
3556}
3557
Evan Cheng37b73872009-07-30 08:33:02 +00003558/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3559/// constant +0.0.
3560bool X86::isZeroNode(SDValue Elt) {
3561 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003562 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003563 (isa<ConstantFPSDNode>(Elt) &&
3564 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3565}
3566
Nate Begeman9008ca62009-04-27 18:41:29 +00003567/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3568/// their permute mask.
3569static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3570 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003571 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003572 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003573 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003574
Nate Begeman5a5ca152009-04-29 05:20:52 +00003575 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 int idx = SVOp->getMaskElt(i);
3577 if (idx < 0)
3578 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003579 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003581 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003582 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003583 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003584 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3585 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003586}
3587
Evan Cheng779ccea2007-12-07 21:30:01 +00003588/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3589/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003590static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003591 unsigned NumElems = VT.getVectorNumElements();
3592 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003593 int idx = Mask[i];
3594 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003595 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003596 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003597 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003598 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003599 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003600 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003601}
3602
Evan Cheng533a0aa2006-04-19 20:35:22 +00003603/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3604/// match movhlps. The lower half elements should come from upper half of
3605/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003606/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003607static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3608 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003609 return false;
3610 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003612 return false;
3613 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003615 return false;
3616 return true;
3617}
3618
Evan Cheng5ced1d82006-04-06 23:23:56 +00003619/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003620/// is promoted to a vector. It also returns the LoadSDNode by reference if
3621/// required.
3622static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003623 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3624 return false;
3625 N = N->getOperand(0).getNode();
3626 if (!ISD::isNON_EXTLoad(N))
3627 return false;
3628 if (LD)
3629 *LD = cast<LoadSDNode>(N);
3630 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003631}
3632
Evan Cheng533a0aa2006-04-19 20:35:22 +00003633/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3634/// match movlp{s|d}. The lower half elements should come from lower half of
3635/// V1 (and in order), and the upper half elements should come from the upper
3636/// half of V2 (and in order). And since V1 will become the source of the
3637/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003638static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3639 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003640 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003641 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003642 // Is V2 is a vector load, don't do this transformation. We will try to use
3643 // load folding shufps op.
3644 if (ISD::isNON_EXTLoad(V2))
3645 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003646
Nate Begeman5a5ca152009-04-29 05:20:52 +00003647 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003648
Evan Cheng533a0aa2006-04-19 20:35:22 +00003649 if (NumElems != 2 && NumElems != 4)
3650 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003651 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003652 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003653 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003654 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003656 return false;
3657 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003658}
3659
Evan Cheng39623da2006-04-20 08:58:49 +00003660/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3661/// all the same.
3662static bool isSplatVector(SDNode *N) {
3663 if (N->getOpcode() != ISD::BUILD_VECTOR)
3664 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003665
Dan Gohman475871a2008-07-27 21:46:04 +00003666 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003667 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3668 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003669 return false;
3670 return true;
3671}
3672
Evan Cheng213d2cf2007-05-17 18:45:50 +00003673/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003674/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003675/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003676static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003677 SDValue V1 = N->getOperand(0);
3678 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003679 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3680 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003682 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003683 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003684 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3685 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003686 if (Opc != ISD::BUILD_VECTOR ||
3687 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003688 return false;
3689 } else if (Idx >= 0) {
3690 unsigned Opc = V1.getOpcode();
3691 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3692 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003693 if (Opc != ISD::BUILD_VECTOR ||
3694 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003695 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003696 }
3697 }
3698 return true;
3699}
3700
3701/// getZeroVector - Returns a vector of specified type with all zero elements.
3702///
Owen Andersone50ed302009-08-10 22:56:29 +00003703static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003704 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003705 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003706
Dale Johannesen0488fb62010-09-30 23:57:10 +00003707 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003708 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003709 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003710 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003711 if (HasSSE2) { // SSE2
3712 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3713 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3714 } else { // SSE1
3715 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3716 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3717 }
3718 } else if (VT.getSizeInBits() == 256) { // AVX
3719 // 256-bit logic and arithmetic instructions in AVX are
3720 // all floating-point, no support for integer ops. Default
3721 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003722 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003723 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3724 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003725 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003726 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003727}
3728
Chris Lattner8a594482007-11-25 00:24:49 +00003729/// getOnesVector - Returns a vector of specified type with all bits set.
3730///
Owen Andersone50ed302009-08-10 22:56:29 +00003731static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003732 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003733
Chris Lattner8a594482007-11-25 00:24:49 +00003734 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3735 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003736 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003737 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003738 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003739 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003740}
3741
3742
Evan Cheng39623da2006-04-20 08:58:49 +00003743/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3744/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003745static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003746 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003747 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003748
Evan Cheng39623da2006-04-20 08:58:49 +00003749 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003750 SmallVector<int, 8> MaskVec;
3751 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003752
Nate Begeman5a5ca152009-04-29 05:20:52 +00003753 for (unsigned i = 0; i != NumElems; ++i) {
3754 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003755 MaskVec[i] = NumElems;
3756 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003757 }
Evan Cheng39623da2006-04-20 08:58:49 +00003758 }
Evan Cheng39623da2006-04-20 08:58:49 +00003759 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003760 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3761 SVOp->getOperand(1), &MaskVec[0]);
3762 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003763}
3764
Evan Cheng017dcc62006-04-21 01:05:10 +00003765/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3766/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003767static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003768 SDValue V2) {
3769 unsigned NumElems = VT.getVectorNumElements();
3770 SmallVector<int, 8> Mask;
3771 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003772 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 Mask.push_back(i);
3774 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003775}
3776
Nate Begeman9008ca62009-04-27 18:41:29 +00003777/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003778static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003779 SDValue V2) {
3780 unsigned NumElems = VT.getVectorNumElements();
3781 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003782 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003783 Mask.push_back(i);
3784 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003785 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003787}
3788
Nate Begeman9008ca62009-04-27 18:41:29 +00003789/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003790static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 SDValue V2) {
3792 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003793 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003794 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003795 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 Mask.push_back(i + Half);
3797 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003798 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003800}
3801
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003802/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3803static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003805 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 DebugLoc dl = SV->getDebugLoc();
3807 SDValue V1 = SV->getOperand(0);
3808 int NumElems = VT.getVectorNumElements();
3809 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003810
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 // unpack elements to the correct location
3812 while (NumElems > 4) {
3813 if (EltNo < NumElems/2) {
3814 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3815 } else {
3816 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3817 EltNo -= NumElems/2;
3818 }
3819 NumElems >>= 1;
3820 }
Eric Christopherfd179292009-08-27 18:07:15 +00003821
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 // Perform the splat.
3823 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003824 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003825 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003826 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003827}
3828
Evan Chengba05f722006-04-21 23:03:30 +00003829/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003830/// vector of zero or undef vector. This produces a shuffle where the low
3831/// element of V2 is swizzled into the zero/undef vector, landing at element
3832/// 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 +00003833static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003834 bool isZero, bool HasSSE2,
3835 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003836 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003837 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3839 unsigned NumElems = VT.getVectorNumElements();
3840 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003841 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003842 // If this is the insertion idx, put the low elt of V2 here.
3843 MaskVec.push_back(i == Idx ? NumElems : i);
3844 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003845}
3846
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003847/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3848/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003849SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3850 unsigned Depth) {
3851 if (Depth == 6)
3852 return SDValue(); // Limit search depth.
3853
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003854 SDValue V = SDValue(N, 0);
3855 EVT VT = V.getValueType();
3856 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003857
3858 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3859 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3860 Index = SV->getMaskElt(Index);
3861
3862 if (Index < 0)
3863 return DAG.getUNDEF(VT.getVectorElementType());
3864
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003865 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003866 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003867 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003868 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003869
3870 // Recurse into target specific vector shuffles to find scalars.
3871 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003872 int NumElems = VT.getVectorNumElements();
3873 SmallVector<unsigned, 16> ShuffleMask;
3874 SDValue ImmN;
3875
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003876 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003877 case X86ISD::SHUFPS:
3878 case X86ISD::SHUFPD:
3879 ImmN = N->getOperand(N->getNumOperands()-1);
3880 DecodeSHUFPSMask(NumElems,
3881 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3882 ShuffleMask);
3883 break;
3884 case X86ISD::PUNPCKHBW:
3885 case X86ISD::PUNPCKHWD:
3886 case X86ISD::PUNPCKHDQ:
3887 case X86ISD::PUNPCKHQDQ:
3888 DecodePUNPCKHMask(NumElems, ShuffleMask);
3889 break;
3890 case X86ISD::UNPCKHPS:
3891 case X86ISD::UNPCKHPD:
3892 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3893 break;
3894 case X86ISD::PUNPCKLBW:
3895 case X86ISD::PUNPCKLWD:
3896 case X86ISD::PUNPCKLDQ:
3897 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003898 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003899 break;
3900 case X86ISD::UNPCKLPS:
3901 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003902 case X86ISD::VUNPCKLPS:
3903 case X86ISD::VUNPCKLPD:
3904 case X86ISD::VUNPCKLPSY:
3905 case X86ISD::VUNPCKLPDY:
3906 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003907 break;
3908 case X86ISD::MOVHLPS:
3909 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3910 break;
3911 case X86ISD::MOVLHPS:
3912 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3913 break;
3914 case X86ISD::PSHUFD:
3915 ImmN = N->getOperand(N->getNumOperands()-1);
3916 DecodePSHUFMask(NumElems,
3917 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3918 ShuffleMask);
3919 break;
3920 case X86ISD::PSHUFHW:
3921 ImmN = N->getOperand(N->getNumOperands()-1);
3922 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3923 ShuffleMask);
3924 break;
3925 case X86ISD::PSHUFLW:
3926 ImmN = N->getOperand(N->getNumOperands()-1);
3927 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3928 ShuffleMask);
3929 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003930 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003931 case X86ISD::MOVSD: {
3932 // The index 0 always comes from the first element of the second source,
3933 // this is why MOVSS and MOVSD are used in the first place. The other
3934 // elements come from the other positions of the first source vector.
3935 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003936 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3937 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003938 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003939 default:
3940 assert("not implemented for target shuffle node");
3941 return SDValue();
3942 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003943
3944 Index = ShuffleMask[Index];
3945 if (Index < 0)
3946 return DAG.getUNDEF(VT.getVectorElementType());
3947
3948 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3949 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3950 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003951 }
3952
3953 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003954 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003955 V = V.getOperand(0);
3956 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003957 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003958
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003959 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003960 return SDValue();
3961 }
3962
3963 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3964 return (Index == 0) ? V.getOperand(0)
3965 : DAG.getUNDEF(VT.getVectorElementType());
3966
3967 if (V.getOpcode() == ISD::BUILD_VECTOR)
3968 return V.getOperand(Index);
3969
3970 return SDValue();
3971}
3972
3973/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3974/// shuffle operation which come from a consecutively from a zero. The
3975/// search can start in two diferent directions, from left or right.
3976static
3977unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3978 bool ZerosFromLeft, SelectionDAG &DAG) {
3979 int i = 0;
3980
3981 while (i < NumElems) {
3982 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003983 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003984 if (!(Elt.getNode() &&
3985 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3986 break;
3987 ++i;
3988 }
3989
3990 return i;
3991}
3992
3993/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3994/// MaskE correspond consecutively to elements from one of the vector operands,
3995/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3996static
3997bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3998 int OpIdx, int NumElems, unsigned &OpNum) {
3999 bool SeenV1 = false;
4000 bool SeenV2 = false;
4001
4002 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4003 int Idx = SVOp->getMaskElt(i);
4004 // Ignore undef indicies
4005 if (Idx < 0)
4006 continue;
4007
4008 if (Idx < NumElems)
4009 SeenV1 = true;
4010 else
4011 SeenV2 = true;
4012
4013 // Only accept consecutive elements from the same vector
4014 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4015 return false;
4016 }
4017
4018 OpNum = SeenV1 ? 0 : 1;
4019 return true;
4020}
4021
4022/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4023/// logical left shift of a vector.
4024static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4025 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4026 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4027 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4028 false /* check zeros from right */, DAG);
4029 unsigned OpSrc;
4030
4031 if (!NumZeros)
4032 return false;
4033
4034 // Considering the elements in the mask that are not consecutive zeros,
4035 // check if they consecutively come from only one of the source vectors.
4036 //
4037 // V1 = {X, A, B, C} 0
4038 // \ \ \ /
4039 // vector_shuffle V1, V2 <1, 2, 3, X>
4040 //
4041 if (!isShuffleMaskConsecutive(SVOp,
4042 0, // Mask Start Index
4043 NumElems-NumZeros-1, // Mask End Index
4044 NumZeros, // Where to start looking in the src vector
4045 NumElems, // Number of elements in vector
4046 OpSrc)) // Which source operand ?
4047 return false;
4048
4049 isLeft = false;
4050 ShAmt = NumZeros;
4051 ShVal = SVOp->getOperand(OpSrc);
4052 return true;
4053}
4054
4055/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4056/// logical left shift of a vector.
4057static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4058 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4059 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4060 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4061 true /* check zeros from left */, DAG);
4062 unsigned OpSrc;
4063
4064 if (!NumZeros)
4065 return false;
4066
4067 // Considering the elements in the mask that are not consecutive zeros,
4068 // check if they consecutively come from only one of the source vectors.
4069 //
4070 // 0 { A, B, X, X } = V2
4071 // / \ / /
4072 // vector_shuffle V1, V2 <X, X, 4, 5>
4073 //
4074 if (!isShuffleMaskConsecutive(SVOp,
4075 NumZeros, // Mask Start Index
4076 NumElems-1, // Mask End Index
4077 0, // Where to start looking in the src vector
4078 NumElems, // Number of elements in vector
4079 OpSrc)) // Which source operand ?
4080 return false;
4081
4082 isLeft = true;
4083 ShAmt = NumZeros;
4084 ShVal = SVOp->getOperand(OpSrc);
4085 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004086}
4087
4088/// isVectorShift - Returns true if the shuffle can be implemented as a
4089/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004090static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004091 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004092 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4093 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4094 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004095
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004096 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004097}
4098
Evan Chengc78d3b42006-04-24 18:01:45 +00004099/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4100///
Dan Gohman475871a2008-07-27 21:46:04 +00004101static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004102 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004103 SelectionDAG &DAG,
4104 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004105 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004106 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004107
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004108 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004110 bool First = true;
4111 for (unsigned i = 0; i < 16; ++i) {
4112 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4113 if (ThisIsNonZero && First) {
4114 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004115 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004116 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004117 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004118 First = false;
4119 }
4120
4121 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004122 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004123 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4124 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004125 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004126 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004127 }
4128 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4130 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4131 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004132 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004133 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004134 } else
4135 ThisElt = LastElt;
4136
Gabor Greifba36cb52008-08-28 21:40:38 +00004137 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004139 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004140 }
4141 }
4142
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004143 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004144}
4145
Bill Wendlinga348c562007-03-22 18:42:45 +00004146/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004147///
Dan Gohman475871a2008-07-27 21:46:04 +00004148static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004149 unsigned NumNonZero, unsigned NumZero,
4150 SelectionDAG &DAG,
4151 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004152 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004153 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004154
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004155 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004157 bool First = true;
4158 for (unsigned i = 0; i < 8; ++i) {
4159 bool isNonZero = (NonZeros & (1 << i)) != 0;
4160 if (isNonZero) {
4161 if (First) {
4162 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004164 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004166 First = false;
4167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004168 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004170 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004171 }
4172 }
4173
4174 return V;
4175}
4176
Evan Chengf26ffe92008-05-29 08:22:04 +00004177/// getVShift - Return a vector logical shift node.
4178///
Owen Andersone50ed302009-08-10 22:56:29 +00004179static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004180 unsigned NumBits, SelectionDAG &DAG,
4181 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004182 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004183 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004184 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4185 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004186 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004187 DAG.getConstant(NumBits,
4188 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004189}
4190
Dan Gohman475871a2008-07-27 21:46:04 +00004191SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004192X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004193 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004194
Evan Chengc3630942009-12-09 21:00:30 +00004195 // Check if the scalar load can be widened into a vector load. And if
4196 // the address is "base + cst" see if the cst can be "absorbed" into
4197 // the shuffle mask.
4198 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4199 SDValue Ptr = LD->getBasePtr();
4200 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4201 return SDValue();
4202 EVT PVT = LD->getValueType(0);
4203 if (PVT != MVT::i32 && PVT != MVT::f32)
4204 return SDValue();
4205
4206 int FI = -1;
4207 int64_t Offset = 0;
4208 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4209 FI = FINode->getIndex();
4210 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004211 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004212 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4213 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4214 Offset = Ptr.getConstantOperandVal(1);
4215 Ptr = Ptr.getOperand(0);
4216 } else {
4217 return SDValue();
4218 }
4219
4220 SDValue Chain = LD->getChain();
4221 // Make sure the stack object alignment is at least 16.
4222 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4223 if (DAG.InferPtrAlignment(Ptr) < 16) {
4224 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004225 // Can't change the alignment. FIXME: It's possible to compute
4226 // the exact stack offset and reference FI + adjust offset instead.
4227 // If someone *really* cares about this. That's the way to implement it.
4228 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004229 } else {
4230 MFI->setObjectAlignment(FI, 16);
4231 }
4232 }
4233
4234 // (Offset % 16) must be multiple of 4. Then address is then
4235 // Ptr + (Offset & ~15).
4236 if (Offset < 0)
4237 return SDValue();
4238 if ((Offset % 16) & 3)
4239 return SDValue();
4240 int64_t StartOffset = Offset & ~15;
4241 if (StartOffset)
4242 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4243 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4244
4245 int EltNo = (Offset - StartOffset) >> 2;
4246 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4247 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004248 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4249 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004250 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004251 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004252 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4253 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004254 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004255 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004256 }
4257
4258 return SDValue();
4259}
4260
Michael J. Spencerec38de22010-10-10 22:04:20 +00004261/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4262/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004263/// load which has the same value as a build_vector whose operands are 'elts'.
4264///
4265/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004266///
Nate Begeman1449f292010-03-24 22:19:06 +00004267/// FIXME: we'd also like to handle the case where the last elements are zero
4268/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4269/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004270static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004271 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004272 EVT EltVT = VT.getVectorElementType();
4273 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004274
Nate Begemanfdea31a2010-03-24 20:49:50 +00004275 LoadSDNode *LDBase = NULL;
4276 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004277
Nate Begeman1449f292010-03-24 22:19:06 +00004278 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004279 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004280 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004281 for (unsigned i = 0; i < NumElems; ++i) {
4282 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004283
Nate Begemanfdea31a2010-03-24 20:49:50 +00004284 if (!Elt.getNode() ||
4285 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4286 return SDValue();
4287 if (!LDBase) {
4288 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4289 return SDValue();
4290 LDBase = cast<LoadSDNode>(Elt.getNode());
4291 LastLoadedElt = i;
4292 continue;
4293 }
4294 if (Elt.getOpcode() == ISD::UNDEF)
4295 continue;
4296
4297 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4298 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4299 return SDValue();
4300 LastLoadedElt = i;
4301 }
Nate Begeman1449f292010-03-24 22:19:06 +00004302
4303 // If we have found an entire vector of loads and undefs, then return a large
4304 // load of the entire vector width starting at the base pointer. If we found
4305 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004306 if (LastLoadedElt == NumElems - 1) {
4307 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004308 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004309 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004310 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004311 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004312 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004313 LDBase->isVolatile(), LDBase->isNonTemporal(),
4314 LDBase->getAlignment());
4315 } else if (NumElems == 4 && LastLoadedElt == 1) {
4316 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4317 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004318 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4319 Ops, 2, MVT::i32,
4320 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004321 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004322 }
4323 return SDValue();
4324}
4325
Evan Chengc3630942009-12-09 21:00:30 +00004326SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004327X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004328 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004329
David Greenef125a292011-02-08 19:04:41 +00004330 EVT VT = Op.getValueType();
4331 EVT ExtVT = VT.getVectorElementType();
4332
4333 unsigned NumElems = Op.getNumOperands();
4334
4335 // For AVX-length vectors, build the individual 128-bit pieces and
4336 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004337 if (VT.getSizeInBits() > 256 &&
4338 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004339 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4340 SmallVector<SDValue, 8> V;
4341 V.resize(NumElems);
4342 for (unsigned i = 0; i < NumElems; ++i) {
4343 V[i] = Op.getOperand(i);
4344 }
Owen Anderson95771af2011-02-25 21:41:48 +00004345
David Greenef125a292011-02-08 19:04:41 +00004346 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4347
4348 // Build the lower subvector.
4349 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4350 // Build the upper subvector.
4351 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4352 NumElems/2);
4353
4354 return ConcatVectors(Lower, Upper, DAG);
4355 }
4356
Chris Lattner6e80e442010-08-28 17:15:43 +00004357 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4358 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004359 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4360 // is present, so AllOnes is ignored.
4361 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4362 (Op.getValueType().getSizeInBits() != 256 &&
4363 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004364 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004365 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4366 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004367 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004368 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004369
Gabor Greifba36cb52008-08-28 21:40:38 +00004370 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004371 return getOnesVector(Op.getValueType(), DAG, dl);
4372 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004373 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004374
Owen Andersone50ed302009-08-10 22:56:29 +00004375 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004376
Evan Cheng0db9fe62006-04-25 20:13:52 +00004377 unsigned NumZero = 0;
4378 unsigned NumNonZero = 0;
4379 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004380 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004381 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004382 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004384 if (Elt.getOpcode() == ISD::UNDEF)
4385 continue;
4386 Values.insert(Elt);
4387 if (Elt.getOpcode() != ISD::Constant &&
4388 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004389 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004390 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004391 NumZero++;
4392 else {
4393 NonZeros |= (1 << i);
4394 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004395 }
4396 }
4397
Chris Lattner97a2a562010-08-26 05:24:29 +00004398 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4399 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004400 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401
Chris Lattner67f453a2008-03-09 05:42:06 +00004402 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004403 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004405 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004406
Chris Lattner62098042008-03-09 01:05:04 +00004407 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4408 // the value are obviously zero, truncate the value to i32 and do the
4409 // insertion that way. Only do this if the value is non-constant or if the
4410 // value is a constant being inserted into element 0. It is cheaper to do
4411 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004412 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004413 (!IsAllConstants || Idx == 0)) {
4414 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004415 // Handle SSE only.
4416 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4417 EVT VecVT = MVT::v4i32;
4418 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004419
Chris Lattner62098042008-03-09 01:05:04 +00004420 // Truncate the value (which may itself be a constant) to i32, and
4421 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004423 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004424 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4425 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004426
Chris Lattner62098042008-03-09 01:05:04 +00004427 // Now we have our 32-bit value zero extended in the low element of
4428 // a vector. If Idx != 0, swizzle it into place.
4429 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 SmallVector<int, 4> Mask;
4431 Mask.push_back(Idx);
4432 for (unsigned i = 1; i != VecElts; ++i)
4433 Mask.push_back(i);
4434 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004435 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004437 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004438 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004439 }
4440 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004441
Chris Lattner19f79692008-03-08 22:59:52 +00004442 // If we have a constant or non-constant insertion into the low element of
4443 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4444 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004445 // depending on what the source datatype is.
4446 if (Idx == 0) {
4447 if (NumZero == 0) {
4448 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004449 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4450 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004451 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4452 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4453 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4454 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004455 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4456 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004457 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4458 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004459 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4460 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4461 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004462 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004463 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004464 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004465
4466 // Is it a vector logical left shift?
4467 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004468 X86::isZeroNode(Op.getOperand(0)) &&
4469 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004470 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004471 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004472 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004473 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004474 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004476
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004477 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004478 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479
Chris Lattner19f79692008-03-08 22:59:52 +00004480 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4481 // is a non-constant being inserted into an element other than the low one,
4482 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4483 // movd/movss) to move this into the low element, then shuffle it into
4484 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004486 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004487
Evan Cheng0db9fe62006-04-25 20:13:52 +00004488 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004489 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4490 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004492 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 MaskVec.push_back(i == Idx ? 0 : 1);
4494 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004495 }
4496 }
4497
Chris Lattner67f453a2008-03-09 05:42:06 +00004498 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004499 if (Values.size() == 1) {
4500 if (EVTBits == 32) {
4501 // Instead of a shuffle like this:
4502 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4503 // Check if it's possible to issue this instead.
4504 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4505 unsigned Idx = CountTrailingZeros_32(NonZeros);
4506 SDValue Item = Op.getOperand(Idx);
4507 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4508 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4509 }
Dan Gohman475871a2008-07-27 21:46:04 +00004510 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004512
Dan Gohmana3941172007-07-24 22:55:08 +00004513 // A vector full of immediates; various special cases are already
4514 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004515 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004516 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004517
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004518 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004519 if (EVTBits == 64) {
4520 if (NumNonZero == 1) {
4521 // One half is zero or undef.
4522 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004523 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004524 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004525 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4526 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004527 }
Dan Gohman475871a2008-07-27 21:46:04 +00004528 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004529 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004530
4531 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004532 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004533 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004534 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004535 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004536 }
4537
Bill Wendling826f36f2007-03-28 00:57:11 +00004538 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004539 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004540 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004541 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004542 }
4543
4544 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004545 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004546 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547 if (NumElems == 4 && NumZero > 0) {
4548 for (unsigned i = 0; i < 4; ++i) {
4549 bool isZero = !(NonZeros & (1 << i));
4550 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004551 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 else
Dale Johannesenace16102009-02-03 19:33:06 +00004553 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 }
4555
4556 for (unsigned i = 0; i < 2; ++i) {
4557 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4558 default: break;
4559 case 0:
4560 V[i] = V[i*2]; // Must be a zero vector.
4561 break;
4562 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004563 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004564 break;
4565 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004566 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567 break;
4568 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004570 break;
4571 }
4572 }
4573
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575 bool Reverse = (NonZeros & 0x3) == 2;
4576 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004577 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004578 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4579 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004580 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4581 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582 }
4583
Nate Begemanfdea31a2010-03-24 20:49:50 +00004584 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4585 // Check for a build vector of consecutive loads.
4586 for (unsigned i = 0; i < NumElems; ++i)
4587 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004588
Nate Begemanfdea31a2010-03-24 20:49:50 +00004589 // Check for elements which are consecutive loads.
4590 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4591 if (LD.getNode())
4592 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004593
4594 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004595 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004596 SDValue Result;
4597 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4598 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4599 else
4600 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004601
Chris Lattner24faf612010-08-28 17:59:08 +00004602 for (unsigned i = 1; i < NumElems; ++i) {
4603 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4604 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004605 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004606 }
4607 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004609
Chris Lattner6e80e442010-08-28 17:15:43 +00004610 // Otherwise, expand into a number of unpckl*, start by extending each of
4611 // our (non-undef) elements to the full vector width with the element in the
4612 // bottom slot of the vector (which generates no code for SSE).
4613 for (unsigned i = 0; i < NumElems; ++i) {
4614 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4615 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4616 else
4617 V[i] = DAG.getUNDEF(VT);
4618 }
4619
4620 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004621 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4622 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4623 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004624 unsigned EltStride = NumElems >> 1;
4625 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004626 for (unsigned i = 0; i < EltStride; ++i) {
4627 // If V[i+EltStride] is undef and this is the first round of mixing,
4628 // then it is safe to just drop this shuffle: V[i] is already in the
4629 // right place, the one element (since it's the first round) being
4630 // inserted as undef can be dropped. This isn't safe for successive
4631 // rounds because they will permute elements within both vectors.
4632 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4633 EltStride == NumElems/2)
4634 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004635
Chris Lattner6e80e442010-08-28 17:15:43 +00004636 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004637 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004638 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639 }
4640 return V[0];
4641 }
Dan Gohman475871a2008-07-27 21:46:04 +00004642 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004643}
4644
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004645SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004646X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004647 // We support concatenate two MMX registers and place them in a MMX
4648 // register. This is better than doing a stack convert.
4649 DebugLoc dl = Op.getDebugLoc();
4650 EVT ResVT = Op.getValueType();
4651 assert(Op.getNumOperands() == 2);
4652 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4653 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4654 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004655 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004656 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4657 InVec = Op.getOperand(1);
4658 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4659 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004660 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004661 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4662 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4663 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004664 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004665 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4666 Mask[0] = 0; Mask[1] = 2;
4667 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4668 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004669 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004670}
4671
Nate Begemanb9a47b82009-02-23 08:49:38 +00004672// v8i16 shuffles - Prefer shuffles in the following order:
4673// 1. [all] pshuflw, pshufhw, optional move
4674// 2. [ssse3] 1 x pshufb
4675// 3. [ssse3] 2 x pshufb + 1 x por
4676// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004677SDValue
4678X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4679 SelectionDAG &DAG) const {
4680 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 SDValue V1 = SVOp->getOperand(0);
4682 SDValue V2 = SVOp->getOperand(1);
4683 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004684 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004685
Nate Begemanb9a47b82009-02-23 08:49:38 +00004686 // Determine if more than 1 of the words in each of the low and high quadwords
4687 // of the result come from the same quadword of one of the two inputs. Undef
4688 // mask values count as coming from any quadword, for better codegen.
4689 SmallVector<unsigned, 4> LoQuad(4);
4690 SmallVector<unsigned, 4> HiQuad(4);
4691 BitVector InputQuads(4);
4692 for (unsigned i = 0; i < 8; ++i) {
4693 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004695 MaskVals.push_back(EltIdx);
4696 if (EltIdx < 0) {
4697 ++Quad[0];
4698 ++Quad[1];
4699 ++Quad[2];
4700 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004701 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004702 }
4703 ++Quad[EltIdx / 4];
4704 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004705 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004706
Nate Begemanb9a47b82009-02-23 08:49:38 +00004707 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004708 unsigned MaxQuad = 1;
4709 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004710 if (LoQuad[i] > MaxQuad) {
4711 BestLoQuad = i;
4712 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004713 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004714 }
4715
Nate Begemanb9a47b82009-02-23 08:49:38 +00004716 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004717 MaxQuad = 1;
4718 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004719 if (HiQuad[i] > MaxQuad) {
4720 BestHiQuad = i;
4721 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004722 }
4723 }
4724
Nate Begemanb9a47b82009-02-23 08:49:38 +00004725 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004726 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004727 // single pshufb instruction is necessary. If There are more than 2 input
4728 // quads, disable the next transformation since it does not help SSSE3.
4729 bool V1Used = InputQuads[0] || InputQuads[1];
4730 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004731 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004732 if (InputQuads.count() == 2 && V1Used && V2Used) {
4733 BestLoQuad = InputQuads.find_first();
4734 BestHiQuad = InputQuads.find_next(BestLoQuad);
4735 }
4736 if (InputQuads.count() > 2) {
4737 BestLoQuad = -1;
4738 BestHiQuad = -1;
4739 }
4740 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004741
Nate Begemanb9a47b82009-02-23 08:49:38 +00004742 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4743 // the shuffle mask. If a quad is scored as -1, that means that it contains
4744 // words from all 4 input quadwords.
4745 SDValue NewV;
4746 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004747 SmallVector<int, 8> MaskV;
4748 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4749 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004750 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004751 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4752 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4753 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004754
Nate Begemanb9a47b82009-02-23 08:49:38 +00004755 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4756 // source words for the shuffle, to aid later transformations.
4757 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004758 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004759 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004760 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004761 if (idx != (int)i)
4762 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004763 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004764 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004765 AllWordsInNewV = false;
4766 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004767 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004768
Nate Begemanb9a47b82009-02-23 08:49:38 +00004769 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4770 if (AllWordsInNewV) {
4771 for (int i = 0; i != 8; ++i) {
4772 int idx = MaskVals[i];
4773 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004774 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004775 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004776 if ((idx != i) && idx < 4)
4777 pshufhw = false;
4778 if ((idx != i) && idx > 3)
4779 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004780 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004781 V1 = NewV;
4782 V2Used = false;
4783 BestLoQuad = 0;
4784 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004785 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004786
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4788 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004789 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004790 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4791 unsigned TargetMask = 0;
4792 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004793 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004794 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4795 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4796 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004797 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004798 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004799 }
Eric Christopherfd179292009-08-27 18:07:15 +00004800
Nate Begemanb9a47b82009-02-23 08:49:38 +00004801 // If we have SSSE3, and all words of the result are from 1 input vector,
4802 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4803 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004804 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004805 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004806
Nate Begemanb9a47b82009-02-23 08:49:38 +00004807 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004808 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004809 // mask, and elements that come from V1 in the V2 mask, so that the two
4810 // results can be OR'd together.
4811 bool TwoInputs = V1Used && V2Used;
4812 for (unsigned i = 0; i != 8; ++i) {
4813 int EltIdx = MaskVals[i] * 2;
4814 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4816 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004817 continue;
4818 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004819 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4820 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004821 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004822 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004823 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004824 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004825 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004826 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004827 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004828
Nate Begemanb9a47b82009-02-23 08:49:38 +00004829 // Calculate the shuffle mask for the second input, shuffle it, and
4830 // OR it with the first shuffled input.
4831 pshufbMask.clear();
4832 for (unsigned i = 0; i != 8; ++i) {
4833 int EltIdx = MaskVals[i] * 2;
4834 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4836 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004837 continue;
4838 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004839 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4840 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004841 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004842 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004843 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004844 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 MVT::v16i8, &pshufbMask[0], 16));
4846 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004847 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 }
4849
4850 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4851 // and update MaskVals with new element order.
4852 BitVector InOrder(8);
4853 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004854 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 for (int i = 0; i != 4; ++i) {
4856 int idx = MaskVals[i];
4857 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004858 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004859 InOrder.set(i);
4860 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004862 InOrder.set(i);
4863 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004865 }
4866 }
4867 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004868 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004870 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004871
4872 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4873 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4874 NewV.getOperand(0),
4875 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4876 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004877 }
Eric Christopherfd179292009-08-27 18:07:15 +00004878
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4880 // and update MaskVals with the new element order.
4881 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004882 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004883 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004884 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004885 for (unsigned i = 4; i != 8; ++i) {
4886 int idx = MaskVals[i];
4887 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004888 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004889 InOrder.set(i);
4890 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004891 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004892 InOrder.set(i);
4893 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004894 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004895 }
4896 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004898 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004899
4900 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4901 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4902 NewV.getOperand(0),
4903 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4904 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004905 }
Eric Christopherfd179292009-08-27 18:07:15 +00004906
Nate Begemanb9a47b82009-02-23 08:49:38 +00004907 // In case BestHi & BestLo were both -1, which means each quadword has a word
4908 // from each of the four input quadwords, calculate the InOrder bitvector now
4909 // before falling through to the insert/extract cleanup.
4910 if (BestLoQuad == -1 && BestHiQuad == -1) {
4911 NewV = V1;
4912 for (int i = 0; i != 8; ++i)
4913 if (MaskVals[i] < 0 || MaskVals[i] == i)
4914 InOrder.set(i);
4915 }
Eric Christopherfd179292009-08-27 18:07:15 +00004916
Nate Begemanb9a47b82009-02-23 08:49:38 +00004917 // The other elements are put in the right place using pextrw and pinsrw.
4918 for (unsigned i = 0; i != 8; ++i) {
4919 if (InOrder[i])
4920 continue;
4921 int EltIdx = MaskVals[i];
4922 if (EltIdx < 0)
4923 continue;
4924 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004925 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004928 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004929 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004930 DAG.getIntPtrConstant(i));
4931 }
4932 return NewV;
4933}
4934
4935// v16i8 shuffles - Prefer shuffles in the following order:
4936// 1. [ssse3] 1 x pshufb
4937// 2. [ssse3] 2 x pshufb + 1 x por
4938// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4939static
Nate Begeman9008ca62009-04-27 18:41:29 +00004940SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004941 SelectionDAG &DAG,
4942 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004943 SDValue V1 = SVOp->getOperand(0);
4944 SDValue V2 = SVOp->getOperand(1);
4945 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004946 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004947 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004948
Nate Begemanb9a47b82009-02-23 08:49:38 +00004949 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004950 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004951 // present, fall back to case 3.
4952 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4953 bool V1Only = true;
4954 bool V2Only = true;
4955 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004956 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004957 if (EltIdx < 0)
4958 continue;
4959 if (EltIdx < 16)
4960 V2Only = false;
4961 else
4962 V1Only = false;
4963 }
Eric Christopherfd179292009-08-27 18:07:15 +00004964
Nate Begemanb9a47b82009-02-23 08:49:38 +00004965 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4966 if (TLI.getSubtarget()->hasSSSE3()) {
4967 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004968
Nate Begemanb9a47b82009-02-23 08:49:38 +00004969 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004970 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004971 //
4972 // Otherwise, we have elements from both input vectors, and must zero out
4973 // elements that come from V2 in the first mask, and V1 in the second mask
4974 // so that we can OR them together.
4975 bool TwoInputs = !(V1Only || V2Only);
4976 for (unsigned i = 0; i != 16; ++i) {
4977 int EltIdx = MaskVals[i];
4978 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004979 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004980 continue;
4981 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004982 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004983 }
4984 // If all the elements are from V2, assign it to V1 and return after
4985 // building the first pshufb.
4986 if (V2Only)
4987 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004988 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004989 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004991 if (!TwoInputs)
4992 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004993
Nate Begemanb9a47b82009-02-23 08:49:38 +00004994 // Calculate the shuffle mask for the second input, shuffle it, and
4995 // OR it with the first shuffled input.
4996 pshufbMask.clear();
4997 for (unsigned i = 0; i != 16; ++i) {
4998 int EltIdx = MaskVals[i];
4999 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005001 continue;
5002 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005003 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005004 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005005 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005006 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005007 MVT::v16i8, &pshufbMask[0], 16));
5008 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005009 }
Eric Christopherfd179292009-08-27 18:07:15 +00005010
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 // No SSSE3 - Calculate in place words and then fix all out of place words
5012 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5013 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005014 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5015 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005016 SDValue NewV = V2Only ? V2 : V1;
5017 for (int i = 0; i != 8; ++i) {
5018 int Elt0 = MaskVals[i*2];
5019 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005020
Nate Begemanb9a47b82009-02-23 08:49:38 +00005021 // This word of the result is all undef, skip it.
5022 if (Elt0 < 0 && Elt1 < 0)
5023 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005024
Nate Begemanb9a47b82009-02-23 08:49:38 +00005025 // This word of the result is already in the correct place, skip it.
5026 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5027 continue;
5028 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5029 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005030
Nate Begemanb9a47b82009-02-23 08:49:38 +00005031 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5032 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5033 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005034
5035 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5036 // using a single extract together, load it and store it.
5037 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005038 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005039 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005040 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005041 DAG.getIntPtrConstant(i));
5042 continue;
5043 }
5044
Nate Begemanb9a47b82009-02-23 08:49:38 +00005045 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005046 // source byte is not also odd, shift the extracted word left 8 bits
5047 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005048 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005049 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005050 DAG.getIntPtrConstant(Elt1 / 2));
5051 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005053 DAG.getConstant(8,
5054 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005055 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005056 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5057 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005058 }
5059 // If Elt0 is defined, extract it from the appropriate source. If the
5060 // source byte is not also even, shift the extracted word right 8 bits. If
5061 // Elt1 was also defined, OR the extracted values together before
5062 // inserting them in the result.
5063 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005064 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005065 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5066 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005068 DAG.getConstant(8,
5069 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005070 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005071 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5072 DAG.getConstant(0x00FF, MVT::i16));
5073 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005074 : InsElt0;
5075 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005076 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005077 DAG.getIntPtrConstant(i));
5078 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005079 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005080}
5081
Evan Cheng7a831ce2007-12-15 03:00:47 +00005082/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005083/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005084/// done when every pair / quad of shuffle mask elements point to elements in
5085/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005086/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005087static
Nate Begeman9008ca62009-04-27 18:41:29 +00005088SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005089 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005090 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005091 SDValue V1 = SVOp->getOperand(0);
5092 SDValue V2 = SVOp->getOperand(1);
5093 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005094 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005095 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005097 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 case MVT::v4f32: NewVT = MVT::v2f64; break;
5099 case MVT::v4i32: NewVT = MVT::v2i64; break;
5100 case MVT::v8i16: NewVT = MVT::v4i32; break;
5101 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005102 }
5103
Nate Begeman9008ca62009-04-27 18:41:29 +00005104 int Scale = NumElems / NewWidth;
5105 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005106 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005107 int StartIdx = -1;
5108 for (int j = 0; j < Scale; ++j) {
5109 int EltIdx = SVOp->getMaskElt(i+j);
5110 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005111 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005112 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005113 StartIdx = EltIdx - (EltIdx % Scale);
5114 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005115 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005116 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005117 if (StartIdx == -1)
5118 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005119 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005120 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005121 }
5122
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005123 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5124 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005125 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005126}
5127
Evan Chengd880b972008-05-09 21:53:03 +00005128/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005129///
Owen Andersone50ed302009-08-10 22:56:29 +00005130static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005131 SDValue SrcOp, SelectionDAG &DAG,
5132 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005134 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005135 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005136 LD = dyn_cast<LoadSDNode>(SrcOp);
5137 if (!LD) {
5138 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5139 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005140 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005141 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005142 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005143 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005144 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005145 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005146 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005147 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005148 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5149 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5150 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005151 SrcOp.getOperand(0)
5152 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005153 }
5154 }
5155 }
5156
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,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005159 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005160 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005161}
5162
Evan Chengace3c172008-07-22 21:13:36 +00005163/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5164/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005165static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005166LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5167 SDValue V1 = SVOp->getOperand(0);
5168 SDValue V2 = SVOp->getOperand(1);
5169 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005170 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005171
Evan Chengace3c172008-07-22 21:13:36 +00005172 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005173 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005174 SmallVector<int, 8> Mask1(4U, -1);
5175 SmallVector<int, 8> PermMask;
5176 SVOp->getMask(PermMask);
5177
Evan Chengace3c172008-07-22 21:13:36 +00005178 unsigned NumHi = 0;
5179 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005180 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005181 int Idx = PermMask[i];
5182 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005183 Locs[i] = std::make_pair(-1, -1);
5184 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005185 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5186 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005187 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005188 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005189 NumLo++;
5190 } else {
5191 Locs[i] = std::make_pair(1, NumHi);
5192 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005193 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005194 NumHi++;
5195 }
5196 }
5197 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005198
Evan Chengace3c172008-07-22 21:13:36 +00005199 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005200 // If no more than two elements come from either vector. This can be
5201 // implemented with two shuffles. First shuffle gather the elements.
5202 // The second shuffle, which takes the first shuffle as both of its
5203 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005204 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005205
Nate Begeman9008ca62009-04-27 18:41:29 +00005206 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005207
Evan Chengace3c172008-07-22 21:13:36 +00005208 for (unsigned i = 0; i != 4; ++i) {
5209 if (Locs[i].first == -1)
5210 continue;
5211 else {
5212 unsigned Idx = (i < 2) ? 0 : 4;
5213 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005215 }
5216 }
5217
Nate Begeman9008ca62009-04-27 18:41:29 +00005218 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005219 } else if (NumLo == 3 || NumHi == 3) {
5220 // Otherwise, we must have three elements from one vector, call it X, and
5221 // one element from the other, call it Y. First, use a shufps to build an
5222 // intermediate vector with the one element from Y and the element from X
5223 // that will be in the same half in the final destination (the indexes don't
5224 // matter). Then, use a shufps to build the final vector, taking the half
5225 // containing the element from Y from the intermediate, and the other half
5226 // from X.
5227 if (NumHi == 3) {
5228 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005229 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005230 std::swap(V1, V2);
5231 }
5232
5233 // Find the element from V2.
5234 unsigned HiIndex;
5235 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005236 int Val = PermMask[HiIndex];
5237 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005238 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005239 if (Val >= 4)
5240 break;
5241 }
5242
Nate Begeman9008ca62009-04-27 18:41:29 +00005243 Mask1[0] = PermMask[HiIndex];
5244 Mask1[1] = -1;
5245 Mask1[2] = PermMask[HiIndex^1];
5246 Mask1[3] = -1;
5247 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005248
5249 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005250 Mask1[0] = PermMask[0];
5251 Mask1[1] = PermMask[1];
5252 Mask1[2] = HiIndex & 1 ? 6 : 4;
5253 Mask1[3] = HiIndex & 1 ? 4 : 6;
5254 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005255 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005256 Mask1[0] = HiIndex & 1 ? 2 : 0;
5257 Mask1[1] = HiIndex & 1 ? 0 : 2;
5258 Mask1[2] = PermMask[2];
5259 Mask1[3] = PermMask[3];
5260 if (Mask1[2] >= 0)
5261 Mask1[2] += 4;
5262 if (Mask1[3] >= 0)
5263 Mask1[3] += 4;
5264 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005265 }
Evan Chengace3c172008-07-22 21:13:36 +00005266 }
5267
5268 // Break it into (shuffle shuffle_hi, shuffle_lo).
5269 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005270 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005271 SmallVector<int,8> LoMask(4U, -1);
5272 SmallVector<int,8> HiMask(4U, -1);
5273
5274 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005275 unsigned MaskIdx = 0;
5276 unsigned LoIdx = 0;
5277 unsigned HiIdx = 2;
5278 for (unsigned i = 0; i != 4; ++i) {
5279 if (i == 2) {
5280 MaskPtr = &HiMask;
5281 MaskIdx = 1;
5282 LoIdx = 0;
5283 HiIdx = 2;
5284 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005285 int Idx = PermMask[i];
5286 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005287 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005288 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005289 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005290 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005291 LoIdx++;
5292 } else {
5293 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005294 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005295 HiIdx++;
5296 }
5297 }
5298
Nate Begeman9008ca62009-04-27 18:41:29 +00005299 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5300 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5301 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005302 for (unsigned i = 0; i != 4; ++i) {
5303 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005305 } else {
5306 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005307 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005308 }
5309 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005310 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005311}
5312
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005313static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005314 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005315 V = V.getOperand(0);
5316 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5317 V = V.getOperand(0);
5318 if (MayFoldLoad(V))
5319 return true;
5320 return false;
5321}
5322
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005323// FIXME: the version above should always be used. Since there's
5324// a bug where several vector shuffles can't be folded because the
5325// DAG is not updated during lowering and a node claims to have two
5326// uses while it only has one, use this version, and let isel match
5327// another instruction if the load really happens to have more than
5328// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005329// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005330static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005331 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005332 V = V.getOperand(0);
5333 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5334 V = V.getOperand(0);
5335 if (ISD::isNormalLoad(V.getNode()))
5336 return true;
5337 return false;
5338}
5339
5340/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5341/// a vector extract, and if both can be later optimized into a single load.
5342/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5343/// here because otherwise a target specific shuffle node is going to be
5344/// emitted for this shuffle, and the optimization not done.
5345/// FIXME: This is probably not the best approach, but fix the problem
5346/// until the right path is decided.
5347static
5348bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5349 const TargetLowering &TLI) {
5350 EVT VT = V.getValueType();
5351 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5352
5353 // Be sure that the vector shuffle is present in a pattern like this:
5354 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5355 if (!V.hasOneUse())
5356 return false;
5357
5358 SDNode *N = *V.getNode()->use_begin();
5359 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5360 return false;
5361
5362 SDValue EltNo = N->getOperand(1);
5363 if (!isa<ConstantSDNode>(EltNo))
5364 return false;
5365
5366 // If the bit convert changed the number of elements, it is unsafe
5367 // to examine the mask.
5368 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005369 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005370 EVT SrcVT = V.getOperand(0).getValueType();
5371 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5372 return false;
5373 V = V.getOperand(0);
5374 HasShuffleIntoBitcast = true;
5375 }
5376
5377 // Select the input vector, guarding against out of range extract vector.
5378 unsigned NumElems = VT.getVectorNumElements();
5379 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5380 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5381 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5382
5383 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005384 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005385 V = V.getOperand(0);
5386
5387 if (ISD::isNormalLoad(V.getNode())) {
5388 // Is the original load suitable?
5389 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5390
5391 // FIXME: avoid the multi-use bug that is preventing lots of
5392 // of foldings to be detected, this is still wrong of course, but
5393 // give the temporary desired behavior, and if it happens that
5394 // the load has real more uses, during isel it will not fold, and
5395 // will generate poor code.
5396 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5397 return false;
5398
5399 if (!HasShuffleIntoBitcast)
5400 return true;
5401
5402 // If there's a bitcast before the shuffle, check if the load type and
5403 // alignment is valid.
5404 unsigned Align = LN0->getAlignment();
5405 unsigned NewAlign =
5406 TLI.getTargetData()->getABITypeAlignment(
5407 VT.getTypeForEVT(*DAG.getContext()));
5408
5409 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5410 return false;
5411 }
5412
5413 return true;
5414}
5415
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005416static
Evan Cheng835580f2010-10-07 20:50:20 +00005417SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5418 EVT VT = Op.getValueType();
5419
5420 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005421 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5422 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005423 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5424 V1, DAG));
5425}
5426
5427static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005428SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5429 bool HasSSE2) {
5430 SDValue V1 = Op.getOperand(0);
5431 SDValue V2 = Op.getOperand(1);
5432 EVT VT = Op.getValueType();
5433
5434 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5435
5436 if (HasSSE2 && VT == MVT::v2f64)
5437 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5438
5439 // v4f32 or v4i32
5440 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5441}
5442
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005443static
5444SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5445 SDValue V1 = Op.getOperand(0);
5446 SDValue V2 = Op.getOperand(1);
5447 EVT VT = Op.getValueType();
5448
5449 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5450 "unsupported shuffle type");
5451
5452 if (V2.getOpcode() == ISD::UNDEF)
5453 V2 = V1;
5454
5455 // v4i32 or v4f32
5456 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5457}
5458
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005459static
5460SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5461 SDValue V1 = Op.getOperand(0);
5462 SDValue V2 = Op.getOperand(1);
5463 EVT VT = Op.getValueType();
5464 unsigned NumElems = VT.getVectorNumElements();
5465
5466 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5467 // operand of these instructions is only memory, so check if there's a
5468 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5469 // same masks.
5470 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005471
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005472 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005473 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005474 CanFoldLoad = true;
5475
5476 // When V1 is a load, it can be folded later into a store in isel, example:
5477 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5478 // turns into:
5479 // (MOVLPSmr addr:$src1, VR128:$src2)
5480 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005481 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005482 CanFoldLoad = true;
5483
Eric Christopher893a8822011-02-20 05:04:42 +00005484 // Both of them can't be memory operations though.
5485 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5486 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005487
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005488 if (CanFoldLoad) {
5489 if (HasSSE2 && NumElems == 2)
5490 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5491
5492 if (NumElems == 4)
5493 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5494 }
5495
5496 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5497 // movl and movlp will both match v2i64, but v2i64 is never matched by
5498 // movl earlier because we make it strict to avoid messing with the movlp load
5499 // folding logic (see the code above getMOVLP call). Match it here then,
5500 // this is horrible, but will stay like this until we move all shuffle
5501 // matching to x86 specific nodes. Note that for the 1st condition all
5502 // types are matched with movsd.
5503 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5504 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5505 else if (HasSSE2)
5506 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5507
5508
5509 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5510
5511 // Invert the operand order and use SHUFPS to match it.
5512 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5513 X86::getShuffleSHUFImmediate(SVOp), DAG);
5514}
5515
David Greenec4db4e52011-02-28 19:06:56 +00005516static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005517 switch(VT.getSimpleVT().SimpleTy) {
5518 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5519 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005520 case MVT::v4f32:
5521 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5522 case MVT::v2f64:
5523 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5524 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5525 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005526 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5527 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5528 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005529 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005530 }
5531 return 0;
5532}
5533
5534static inline unsigned getUNPCKHOpcode(EVT VT) {
5535 switch(VT.getSimpleVT().SimpleTy) {
5536 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5537 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5538 case MVT::v4f32: return X86ISD::UNPCKHPS;
5539 case MVT::v2f64: return X86ISD::UNPCKHPD;
5540 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5541 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5542 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005543 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005544 }
5545 return 0;
5546}
5547
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005548static
5549SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005550 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005551 const X86Subtarget *Subtarget) {
5552 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5553 EVT VT = Op.getValueType();
5554 DebugLoc dl = Op.getDebugLoc();
5555 SDValue V1 = Op.getOperand(0);
5556 SDValue V2 = Op.getOperand(1);
5557
5558 if (isZeroShuffle(SVOp))
5559 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5560
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005561 // Handle splat operations
5562 if (SVOp->isSplat()) {
5563 // Special case, this is the only place now where it's
5564 // allowed to return a vector_shuffle operation without
5565 // using a target specific node, because *hopefully* it
5566 // will be optimized away by the dag combiner.
5567 if (VT.getVectorNumElements() <= 4 &&
5568 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5569 return Op;
5570
5571 // Handle splats by matching through known masks
5572 if (VT.getVectorNumElements() <= 4)
5573 return SDValue();
5574
Evan Cheng835580f2010-10-07 20:50:20 +00005575 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005576 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005577 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005578
5579 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5580 // do it!
5581 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5582 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5583 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005584 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005585 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5586 // FIXME: Figure out a cleaner way to do this.
5587 // Try to make use of movq to zero out the top part.
5588 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5589 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5590 if (NewOp.getNode()) {
5591 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5592 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5593 DAG, Subtarget, dl);
5594 }
5595 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5596 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5597 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5598 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5599 DAG, Subtarget, dl);
5600 }
5601 }
5602 return SDValue();
5603}
5604
Dan Gohman475871a2008-07-27 21:46:04 +00005605SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005606X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005607 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005608 SDValue V1 = Op.getOperand(0);
5609 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005610 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005611 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005612 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005613 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005614 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5615 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005616 bool V1IsSplat = false;
5617 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005618 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005619 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005620 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005621 MachineFunction &MF = DAG.getMachineFunction();
5622 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005623
Dale Johannesen0488fb62010-09-30 23:57:10 +00005624 // Shuffle operations on MMX not supported.
5625 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005626 return Op;
5627
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005628 // Vector shuffle lowering takes 3 steps:
5629 //
5630 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5631 // narrowing and commutation of operands should be handled.
5632 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5633 // shuffle nodes.
5634 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5635 // so the shuffle can be broken into other shuffles and the legalizer can
5636 // try the lowering again.
5637 //
5638 // The general ideia is that no vector_shuffle operation should be left to
5639 // be matched during isel, all of them must be converted to a target specific
5640 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005641
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005642 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5643 // narrowing and commutation of operands should be handled. The actual code
5644 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005645 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005646 if (NewOp.getNode())
5647 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005648
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005649 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5650 // unpckh_undef). Only use pshufd if speed is more important than size.
5651 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5652 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005653 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005654 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5655 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5656 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005657
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005658 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005659 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005660 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005661
Dale Johannesen0488fb62010-09-30 23:57:10 +00005662 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005663 return getMOVHighToLow(Op, dl, DAG);
5664
5665 // Use to match splats
5666 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5667 (VT == MVT::v2f64 || VT == MVT::v2i64))
5668 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5669
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005670 if (X86::isPSHUFDMask(SVOp)) {
5671 // The actual implementation will match the mask in the if above and then
5672 // during isel it can match several different instructions, not only pshufd
5673 // as its name says, sad but true, emulate the behavior for now...
5674 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5675 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5676
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005677 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5678
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005679 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005680 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5681
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005682 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005683 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5684 TargetMask, DAG);
5685
5686 if (VT == MVT::v4f32)
5687 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5688 TargetMask, DAG);
5689 }
Eric Christopherfd179292009-08-27 18:07:15 +00005690
Evan Chengf26ffe92008-05-29 08:22:04 +00005691 // Check if this can be converted into a logical shift.
5692 bool isLeft = false;
5693 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005694 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005695 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005696 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005697 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005698 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005699 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005700 EVT EltVT = VT.getVectorElementType();
5701 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005702 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005703 }
Eric Christopherfd179292009-08-27 18:07:15 +00005704
Nate Begeman9008ca62009-04-27 18:41:29 +00005705 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005706 if (V1IsUndef)
5707 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005708 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005709 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005710 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005711 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005712 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5713
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005714 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005715 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5716 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005717 }
Eric Christopherfd179292009-08-27 18:07:15 +00005718
Nate Begeman9008ca62009-04-27 18:41:29 +00005719 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005720 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5721 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005722
Dale Johannesen0488fb62010-09-30 23:57:10 +00005723 if (X86::isMOVHLPSMask(SVOp))
5724 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005725
Dale Johannesen0488fb62010-09-30 23:57:10 +00005726 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5727 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005728
Dale Johannesen0488fb62010-09-30 23:57:10 +00005729 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5730 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005731
Dale Johannesen0488fb62010-09-30 23:57:10 +00005732 if (X86::isMOVLPMask(SVOp))
5733 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005734
Nate Begeman9008ca62009-04-27 18:41:29 +00005735 if (ShouldXformToMOVHLPS(SVOp) ||
5736 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5737 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005738
Evan Chengf26ffe92008-05-29 08:22:04 +00005739 if (isShift) {
5740 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005741 EVT EltVT = VT.getVectorElementType();
5742 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005743 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005744 }
Eric Christopherfd179292009-08-27 18:07:15 +00005745
Evan Cheng9eca5e82006-10-25 21:49:50 +00005746 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005747 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5748 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005749 V1IsSplat = isSplatVector(V1.getNode());
5750 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005751
Chris Lattner8a594482007-11-25 00:24:49 +00005752 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005753 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005754 Op = CommuteVectorShuffle(SVOp, DAG);
5755 SVOp = cast<ShuffleVectorSDNode>(Op);
5756 V1 = SVOp->getOperand(0);
5757 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005758 std::swap(V1IsSplat, V2IsSplat);
5759 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005760 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005761 }
5762
Nate Begeman9008ca62009-04-27 18:41:29 +00005763 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5764 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005765 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005766 return V1;
5767 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5768 // the instruction selector will not match, so get a canonical MOVL with
5769 // swapped operands to undo the commute.
5770 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005771 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005772
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005773 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005774 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5775 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005776
5777 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005778 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005779
Evan Cheng9bbbb982006-10-25 20:48:19 +00005780 if (V2IsSplat) {
5781 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005782 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005783 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005784 SDValue NewMask = NormalizeMask(SVOp, DAG);
5785 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5786 if (NSVOp != SVOp) {
5787 if (X86::isUNPCKLMask(NSVOp, true)) {
5788 return NewMask;
5789 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5790 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005791 }
5792 }
5793 }
5794
Evan Cheng9eca5e82006-10-25 21:49:50 +00005795 if (Commuted) {
5796 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005797 // FIXME: this seems wrong.
5798 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5799 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005800
5801 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005802 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5803 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005804
5805 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005806 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005807 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005808
Nate Begeman9008ca62009-04-27 18:41:29 +00005809 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005810 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005811 return CommuteVectorShuffle(SVOp, DAG);
5812
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005813 // The checks below are all present in isShuffleMaskLegal, but they are
5814 // inlined here right now to enable us to directly emit target specific
5815 // nodes, and remove one by one until they don't return Op anymore.
5816 SmallVector<int, 16> M;
5817 SVOp->getMask(M);
5818
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005819 if (isPALIGNRMask(M, VT, HasSSSE3))
5820 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5821 X86::getShufflePALIGNRImmediate(SVOp),
5822 DAG);
5823
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005824 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5825 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005826 if (VT == MVT::v2f64) {
5827 X86ISD::NodeType Opcode =
5828 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5829 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5830 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005831 if (VT == MVT::v2i64)
5832 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5833 }
5834
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005835 if (isPSHUFHWMask(M, VT))
5836 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5837 X86::getShufflePSHUFHWImmediate(SVOp),
5838 DAG);
5839
5840 if (isPSHUFLWMask(M, VT))
5841 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5842 X86::getShufflePSHUFLWImmediate(SVOp),
5843 DAG);
5844
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005845 if (isSHUFPMask(M, VT)) {
5846 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5847 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5848 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5849 TargetMask, DAG);
5850 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5851 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5852 TargetMask, DAG);
5853 }
5854
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005855 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5856 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005857 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5858 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005859 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5860 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5861 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5862
Evan Cheng14b32e12007-12-11 01:46:18 +00005863 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005865 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005866 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005867 return NewOp;
5868 }
5869
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005871 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005872 if (NewOp.getNode())
5873 return NewOp;
5874 }
Eric Christopherfd179292009-08-27 18:07:15 +00005875
Dale Johannesen0488fb62010-09-30 23:57:10 +00005876 // Handle all 4 wide cases with a number of shuffles.
5877 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005878 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005879
Dan Gohman475871a2008-07-27 21:46:04 +00005880 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005881}
5882
Dan Gohman475871a2008-07-27 21:46:04 +00005883SDValue
5884X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005885 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005886 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005887 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005888 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005890 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005892 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005893 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005894 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005895 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5896 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5897 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5899 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005900 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005901 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005902 Op.getOperand(0)),
5903 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005905 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005906 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005907 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005908 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005909 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005910 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5911 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005912 // result has a single use which is a store or a bitcast to i32. And in
5913 // the case of a store, it's not worth it if the index is a constant 0,
5914 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005915 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005916 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005917 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005918 if ((User->getOpcode() != ISD::STORE ||
5919 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5920 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005921 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005923 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005925 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005926 Op.getOperand(0)),
5927 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005928 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005930 // ExtractPS works with constant index.
5931 if (isa<ConstantSDNode>(Op.getOperand(1)))
5932 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005933 }
Dan Gohman475871a2008-07-27 21:46:04 +00005934 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005935}
5936
5937
Dan Gohman475871a2008-07-27 21:46:04 +00005938SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005939X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5940 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005941 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005942 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005943
David Greene74a579d2011-02-10 16:57:36 +00005944 SDValue Vec = Op.getOperand(0);
5945 EVT VecVT = Vec.getValueType();
5946
5947 // If this is a 256-bit vector result, first extract the 128-bit
5948 // vector and then extract from the 128-bit vector.
5949 if (VecVT.getSizeInBits() > 128) {
5950 DebugLoc dl = Op.getNode()->getDebugLoc();
5951 unsigned NumElems = VecVT.getVectorNumElements();
5952 SDValue Idx = Op.getOperand(1);
5953
5954 if (!isa<ConstantSDNode>(Idx))
5955 return SDValue();
5956
5957 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5958 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5959
5960 // Get the 128-bit vector.
5961 bool Upper = IdxVal >= ExtractNumElems;
5962 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5963
5964 // Extract from it.
5965 SDValue ScaledIdx = Idx;
5966 if (Upper)
5967 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5968 DAG.getConstant(ExtractNumElems,
5969 Idx.getValueType()));
5970 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
5971 ScaledIdx);
5972 }
5973
5974 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
5975
Evan Cheng62a3f152008-03-24 21:52:23 +00005976 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005977 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005978 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005979 return Res;
5980 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005981
Owen Andersone50ed302009-08-10 22:56:29 +00005982 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005983 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005984 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005985 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005986 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005987 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005988 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5990 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005991 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005993 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005994 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005995 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005996 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005997 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005998 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005999 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006000 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006001 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006002 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006003 if (Idx == 0)
6004 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006005
Evan Cheng0db9fe62006-04-25 20:13:52 +00006006 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006007 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006008 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006009 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006010 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006011 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006012 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006013 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006014 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6015 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6016 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006017 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006018 if (Idx == 0)
6019 return Op;
6020
6021 // UNPCKHPD the element to the lowest double word, then movsd.
6022 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6023 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006024 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006025 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006026 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006027 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006028 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006029 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006030 }
6031
Dan Gohman475871a2008-07-27 21:46:04 +00006032 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006033}
6034
Dan Gohman475871a2008-07-27 21:46:04 +00006035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006036X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6037 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006038 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006039 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006040 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006041
Dan Gohman475871a2008-07-27 21:46:04 +00006042 SDValue N0 = Op.getOperand(0);
6043 SDValue N1 = Op.getOperand(1);
6044 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006045
Dan Gohman8a55ce42009-09-23 21:02:20 +00006046 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006047 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006048 unsigned Opc;
6049 if (VT == MVT::v8i16)
6050 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006051 else if (VT == MVT::v16i8)
6052 Opc = X86ISD::PINSRB;
6053 else
6054 Opc = X86ISD::PINSRB;
6055
Nate Begeman14d12ca2008-02-11 04:19:36 +00006056 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6057 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006058 if (N1.getValueType() != MVT::i32)
6059 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6060 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006061 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006062 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006063 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006064 // Bits [7:6] of the constant are the source select. This will always be
6065 // zero here. The DAG Combiner may combine an extract_elt index into these
6066 // bits. For example (insert (extract, 3), 2) could be matched by putting
6067 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006068 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006069 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006070 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006071 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006072 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006073 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006074 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006075 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006076 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006077 // PINSR* works with constant index.
6078 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006079 }
Dan Gohman475871a2008-07-27 21:46:04 +00006080 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006081}
6082
Dan Gohman475871a2008-07-27 21:46:04 +00006083SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006084X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006085 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006086 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006087
David Greene6b381262011-02-09 15:32:06 +00006088 DebugLoc dl = Op.getDebugLoc();
6089 SDValue N0 = Op.getOperand(0);
6090 SDValue N1 = Op.getOperand(1);
6091 SDValue N2 = Op.getOperand(2);
6092
6093 // If this is a 256-bit vector result, first insert into a 128-bit
6094 // vector and then insert into the 256-bit vector.
6095 if (VT.getSizeInBits() > 128) {
6096 if (!isa<ConstantSDNode>(N2))
6097 return SDValue();
6098
6099 // Get the 128-bit vector.
6100 unsigned NumElems = VT.getVectorNumElements();
6101 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6102 bool Upper = IdxVal >= NumElems / 2;
6103
6104 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6105
6106 // Insert into it.
6107 SDValue ScaledN2 = N2;
6108 if (Upper)
6109 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006110 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006111 (VT.getSizeInBits() / 128),
6112 N2.getValueType()));
6113 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6114 N1, ScaledN2);
6115
6116 // Insert the 128-bit vector
6117 // FIXME: Why UNDEF?
6118 return Insert128BitVector(N0, Op, N2, DAG, dl);
6119 }
6120
Nate Begeman14d12ca2008-02-11 04:19:36 +00006121 if (Subtarget->hasSSE41())
6122 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6123
Dan Gohman8a55ce42009-09-23 21:02:20 +00006124 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006125 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006126
Dan Gohman8a55ce42009-09-23 21:02:20 +00006127 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006128 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6129 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006130 if (N1.getValueType() != MVT::i32)
6131 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6132 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006133 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006134 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006135 }
Dan Gohman475871a2008-07-27 21:46:04 +00006136 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006137}
6138
Dan Gohman475871a2008-07-27 21:46:04 +00006139SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006140X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006141 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006142 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006143 EVT OpVT = Op.getValueType();
6144
6145 // If this is a 256-bit vector result, first insert into a 128-bit
6146 // vector and then insert into the 256-bit vector.
6147 if (OpVT.getSizeInBits() > 128) {
6148 // Insert into a 128-bit vector.
6149 EVT VT128 = EVT::getVectorVT(*Context,
6150 OpVT.getVectorElementType(),
6151 OpVT.getVectorNumElements() / 2);
6152
6153 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6154
6155 // Insert the 128-bit vector.
6156 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6157 DAG.getConstant(0, MVT::i32),
6158 DAG, dl);
6159 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006160
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006161 if (Op.getValueType() == MVT::v1i64 &&
6162 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006163 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006164
Owen Anderson825b72b2009-08-11 20:47:22 +00006165 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006166 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6167 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006168 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006169 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006170}
6171
David Greene91585092011-01-26 15:38:49 +00006172// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6173// a simple subregister reference or explicit instructions to grab
6174// upper bits of a vector.
6175SDValue
6176X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6177 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006178 DebugLoc dl = Op.getNode()->getDebugLoc();
6179 SDValue Vec = Op.getNode()->getOperand(0);
6180 SDValue Idx = Op.getNode()->getOperand(1);
6181
6182 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6183 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6184 return Extract128BitVector(Vec, Idx, DAG, dl);
6185 }
David Greene91585092011-01-26 15:38:49 +00006186 }
6187 return SDValue();
6188}
6189
David Greenecfe33c42011-01-26 19:13:22 +00006190// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6191// simple superregister reference or explicit instructions to insert
6192// the upper bits of a vector.
6193SDValue
6194X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6195 if (Subtarget->hasAVX()) {
6196 DebugLoc dl = Op.getNode()->getDebugLoc();
6197 SDValue Vec = Op.getNode()->getOperand(0);
6198 SDValue SubVec = Op.getNode()->getOperand(1);
6199 SDValue Idx = Op.getNode()->getOperand(2);
6200
6201 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6202 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006203 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006204 }
6205 }
6206 return SDValue();
6207}
6208
Bill Wendling056292f2008-09-16 21:48:12 +00006209// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6210// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6211// one of the above mentioned nodes. It has to be wrapped because otherwise
6212// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6213// be used to form addressing mode. These wrapped nodes will be selected
6214// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006215SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006216X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006217 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006218
Chris Lattner41621a22009-06-26 19:22:52 +00006219 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6220 // global base reg.
6221 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006222 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006223 CodeModel::Model M = getTargetMachine().getCodeModel();
6224
Chris Lattner4f066492009-07-11 20:29:19 +00006225 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006226 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006227 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006228 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006229 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006230 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006231 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006232
Evan Cheng1606e8e2009-03-13 07:51:59 +00006233 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006234 CP->getAlignment(),
6235 CP->getOffset(), OpFlag);
6236 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006237 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006238 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006239 if (OpFlag) {
6240 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006241 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006242 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006243 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006244 }
6245
6246 return Result;
6247}
6248
Dan Gohmand858e902010-04-17 15:26:15 +00006249SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006250 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006251
Chris Lattner18c59872009-06-27 04:16:01 +00006252 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6253 // global base reg.
6254 unsigned char OpFlag = 0;
6255 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006256 CodeModel::Model M = getTargetMachine().getCodeModel();
6257
Chris Lattner4f066492009-07-11 20:29:19 +00006258 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006259 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006260 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006261 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006262 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006263 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006264 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006265
Chris Lattner18c59872009-06-27 04:16:01 +00006266 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6267 OpFlag);
6268 DebugLoc DL = JT->getDebugLoc();
6269 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006270
Chris Lattner18c59872009-06-27 04:16:01 +00006271 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006272 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006273 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6274 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006275 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006276 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006277
Chris Lattner18c59872009-06-27 04:16:01 +00006278 return Result;
6279}
6280
6281SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006282X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006283 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006284
Chris Lattner18c59872009-06-27 04:16:01 +00006285 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6286 // global base reg.
6287 unsigned char OpFlag = 0;
6288 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006289 CodeModel::Model M = getTargetMachine().getCodeModel();
6290
Chris Lattner4f066492009-07-11 20:29:19 +00006291 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006292 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006293 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006294 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006295 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006296 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006297 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006298
Chris Lattner18c59872009-06-27 04:16:01 +00006299 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006300
Chris Lattner18c59872009-06-27 04:16:01 +00006301 DebugLoc DL = Op.getDebugLoc();
6302 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006303
6304
Chris Lattner18c59872009-06-27 04:16:01 +00006305 // With PIC, the address is actually $g + Offset.
6306 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006307 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006308 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6309 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006310 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006311 Result);
6312 }
Eric Christopherfd179292009-08-27 18:07:15 +00006313
Chris Lattner18c59872009-06-27 04:16:01 +00006314 return Result;
6315}
6316
Dan Gohman475871a2008-07-27 21:46:04 +00006317SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006318X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006319 // Create the TargetBlockAddressAddress node.
6320 unsigned char OpFlags =
6321 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006322 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006323 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006324 DebugLoc dl = Op.getDebugLoc();
6325 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6326 /*isTarget=*/true, OpFlags);
6327
Dan Gohmanf705adb2009-10-30 01:28:02 +00006328 if (Subtarget->isPICStyleRIPRel() &&
6329 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006330 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6331 else
6332 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006333
Dan Gohman29cbade2009-11-20 23:18:13 +00006334 // With PIC, the address is actually $g + Offset.
6335 if (isGlobalRelativeToPICBase(OpFlags)) {
6336 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6337 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6338 Result);
6339 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006340
6341 return Result;
6342}
6343
6344SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006345X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006346 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006347 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006348 // Create the TargetGlobalAddress node, folding in the constant
6349 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006350 unsigned char OpFlags =
6351 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006352 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006353 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006354 if (OpFlags == X86II::MO_NO_FLAG &&
6355 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006356 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006357 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006358 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006359 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006360 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006361 }
Eric Christopherfd179292009-08-27 18:07:15 +00006362
Chris Lattner4f066492009-07-11 20:29:19 +00006363 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006364 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006365 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6366 else
6367 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006368
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006369 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006370 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006371 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6372 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006373 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006375
Chris Lattner36c25012009-07-10 07:34:39 +00006376 // For globals that require a load from a stub to get the address, emit the
6377 // load.
6378 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006379 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006380 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006381
Dan Gohman6520e202008-10-18 02:06:02 +00006382 // If there was a non-zero offset that we didn't fold, create an explicit
6383 // addition for it.
6384 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006385 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006386 DAG.getConstant(Offset, getPointerTy()));
6387
Evan Cheng0db9fe62006-04-25 20:13:52 +00006388 return Result;
6389}
6390
Evan Chengda43bcf2008-09-24 00:05:32 +00006391SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006392X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006393 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006394 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006395 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006396}
6397
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006398static SDValue
6399GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006400 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006401 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006402 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006403 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006404 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006405 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006406 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006407 GA->getOffset(),
6408 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006409 if (InFlag) {
6410 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006411 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006412 } else {
6413 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006414 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006415 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006416
6417 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006418 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006419
Rafael Espindola15f1b662009-04-24 12:59:40 +00006420 SDValue Flag = Chain.getValue(1);
6421 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006422}
6423
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006424// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006425static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006426LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006427 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006428 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006429 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6430 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006431 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006432 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006433 InFlag = Chain.getValue(1);
6434
Chris Lattnerb903bed2009-06-26 21:20:29 +00006435 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006436}
6437
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006438// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006439static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006440LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006441 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006442 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6443 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006444}
6445
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006446// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6447// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006448static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006449 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006450 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006451 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006452
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006453 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6454 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6455 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006456
Michael J. Spencerec38de22010-10-10 22:04:20 +00006457 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006458 DAG.getIntPtrConstant(0),
6459 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006460
Chris Lattnerb903bed2009-06-26 21:20:29 +00006461 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006462 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6463 // initialexec.
6464 unsigned WrapperKind = X86ISD::Wrapper;
6465 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006466 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006467 } else if (is64Bit) {
6468 assert(model == TLSModel::InitialExec);
6469 OperandFlags = X86II::MO_GOTTPOFF;
6470 WrapperKind = X86ISD::WrapperRIP;
6471 } else {
6472 assert(model == TLSModel::InitialExec);
6473 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006474 }
Eric Christopherfd179292009-08-27 18:07:15 +00006475
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006476 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6477 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006478 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006479 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006480 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006481 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006482
Rafael Espindola9a580232009-02-27 13:37:18 +00006483 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006484 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006485 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006486
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006487 // The address of the thread local variable is the add of the thread
6488 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006489 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006490}
6491
Dan Gohman475871a2008-07-27 21:46:04 +00006492SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006493X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006494
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006495 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006496 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006497
Eric Christopher30ef0e52010-06-03 04:07:48 +00006498 if (Subtarget->isTargetELF()) {
6499 // TODO: implement the "local dynamic" model
6500 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006501
Eric Christopher30ef0e52010-06-03 04:07:48 +00006502 // If GV is an alias then use the aliasee for determining
6503 // thread-localness.
6504 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6505 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006506
6507 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006508 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006509
Eric Christopher30ef0e52010-06-03 04:07:48 +00006510 switch (model) {
6511 case TLSModel::GeneralDynamic:
6512 case TLSModel::LocalDynamic: // not implemented
6513 if (Subtarget->is64Bit())
6514 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6515 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006516
Eric Christopher30ef0e52010-06-03 04:07:48 +00006517 case TLSModel::InitialExec:
6518 case TLSModel::LocalExec:
6519 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6520 Subtarget->is64Bit());
6521 }
6522 } else if (Subtarget->isTargetDarwin()) {
6523 // Darwin only has one model of TLS. Lower to that.
6524 unsigned char OpFlag = 0;
6525 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6526 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006527
Eric Christopher30ef0e52010-06-03 04:07:48 +00006528 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6529 // global base reg.
6530 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6531 !Subtarget->is64Bit();
6532 if (PIC32)
6533 OpFlag = X86II::MO_TLVP_PIC_BASE;
6534 else
6535 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006536 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006537 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006538 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006539 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006540 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006541
Eric Christopher30ef0e52010-06-03 04:07:48 +00006542 // With PIC32, the address is actually $g + Offset.
6543 if (PIC32)
6544 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6545 DAG.getNode(X86ISD::GlobalBaseReg,
6546 DebugLoc(), getPointerTy()),
6547 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006548
Eric Christopher30ef0e52010-06-03 04:07:48 +00006549 // Lowering the machine isd will make sure everything is in the right
6550 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006551 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006552 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006553 SDValue Args[] = { Chain, Offset };
6554 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006555
Eric Christopher30ef0e52010-06-03 04:07:48 +00006556 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6557 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6558 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006559
Eric Christopher30ef0e52010-06-03 04:07:48 +00006560 // And our return value (tls address) is in the standard call return value
6561 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006562 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6563 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006564 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006565
Eric Christopher30ef0e52010-06-03 04:07:48 +00006566 assert(false &&
6567 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006568
Torok Edwinc23197a2009-07-14 16:55:14 +00006569 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006570 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006571}
6572
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006574/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006575/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006576SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006577 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006578 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006579 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006580 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006581 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006582 SDValue ShOpLo = Op.getOperand(0);
6583 SDValue ShOpHi = Op.getOperand(1);
6584 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006585 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006586 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006587 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006588
Dan Gohman475871a2008-07-27 21:46:04 +00006589 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006590 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006591 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6592 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006593 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006594 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6595 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006596 }
Evan Chenge3413162006-01-09 18:33:28 +00006597
Owen Anderson825b72b2009-08-11 20:47:22 +00006598 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6599 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006600 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006601 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006602
Dan Gohman475871a2008-07-27 21:46:04 +00006603 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006604 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6606 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006607
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006608 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006609 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6610 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006611 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006612 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6613 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006614 }
6615
Dan Gohman475871a2008-07-27 21:46:04 +00006616 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006617 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618}
Evan Chenga3195e82006-01-12 22:54:21 +00006619
Dan Gohmand858e902010-04-17 15:26:15 +00006620SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6621 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006622 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006623
Dale Johannesen0488fb62010-09-30 23:57:10 +00006624 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006625 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006626
Owen Anderson825b72b2009-08-11 20:47:22 +00006627 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006628 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006629
Eli Friedman36df4992009-05-27 00:47:34 +00006630 // These are really Legal; return the operand so the caller accepts it as
6631 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006632 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006633 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006634 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006635 Subtarget->is64Bit()) {
6636 return Op;
6637 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006638
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006639 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006640 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006641 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006642 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006643 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006644 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006645 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006646 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006647 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006648 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6649}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006650
Owen Andersone50ed302009-08-10 22:56:29 +00006651SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006652 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006653 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006654 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006655 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006656 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006657 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006658 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006659 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006660 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006662
Chris Lattner492a43e2010-09-22 01:28:21 +00006663 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006664
Chris Lattner492a43e2010-09-22 01:28:21 +00006665 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6666 MachineMemOperand *MMO =
6667 DAG.getMachineFunction()
6668 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6669 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006670
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006671 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006672 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6673 X86ISD::FILD, DL,
6674 Tys, Ops, array_lengthof(Ops),
6675 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006676
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006677 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006678 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006679 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680
6681 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6682 // shouldn't be necessary except that RFP cannot be live across
6683 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006684 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006685 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6686 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006687 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006688 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006689 SDValue Ops[] = {
6690 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6691 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006692 MachineMemOperand *MMO =
6693 DAG.getMachineFunction()
6694 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006695 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006696
Chris Lattner492a43e2010-09-22 01:28:21 +00006697 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6698 Ops, array_lengthof(Ops),
6699 Op.getValueType(), MMO);
6700 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006701 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006702 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006703 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006704
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705 return Result;
6706}
6707
Bill Wendling8b8a6362009-01-17 03:56:04 +00006708// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006709SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6710 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006711 // This algorithm is not obvious. Here it is in C code, more or less:
6712 /*
6713 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6714 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6715 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006716
Bill Wendling8b8a6362009-01-17 03:56:04 +00006717 // Copy ints to xmm registers.
6718 __m128i xh = _mm_cvtsi32_si128( hi );
6719 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006720
Bill Wendling8b8a6362009-01-17 03:56:04 +00006721 // Combine into low half of a single xmm register.
6722 __m128i x = _mm_unpacklo_epi32( xh, xl );
6723 __m128d d;
6724 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006725
Bill Wendling8b8a6362009-01-17 03:56:04 +00006726 // Merge in appropriate exponents to give the integer bits the right
6727 // magnitude.
6728 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006729
Bill Wendling8b8a6362009-01-17 03:56:04 +00006730 // Subtract away the biases to deal with the IEEE-754 double precision
6731 // implicit 1.
6732 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006733
Bill Wendling8b8a6362009-01-17 03:56:04 +00006734 // All conversions up to here are exact. The correctly rounded result is
6735 // calculated using the current rounding mode using the following
6736 // horizontal add.
6737 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6738 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6739 // store doesn't really need to be here (except
6740 // maybe to zero the other double)
6741 return sd;
6742 }
6743 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006744
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006745 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006746 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006747
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006748 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006749 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006750 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6751 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6752 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6753 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006754 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006755 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006756
Bill Wendling8b8a6362009-01-17 03:56:04 +00006757 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006758 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006759 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006760 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006761 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006762 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006763 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006764
Owen Anderson825b72b2009-08-11 20:47:22 +00006765 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6766 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006767 Op.getOperand(0),
6768 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006769 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6770 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006771 Op.getOperand(0),
6772 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006773 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6774 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006775 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006776 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006777 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006778 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006779 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006780 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006781 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006783
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006784 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006785 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006786 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6787 DAG.getUNDEF(MVT::v2f64), ShufMask);
6788 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6789 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006790 DAG.getIntPtrConstant(0));
6791}
6792
Bill Wendling8b8a6362009-01-17 03:56:04 +00006793// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006794SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6795 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006796 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006797 // FP constant to bias correct the final result.
6798 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006799 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006800
6801 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006802 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6803 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006804 Op.getOperand(0),
6805 DAG.getIntPtrConstant(0)));
6806
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006808 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006809 DAG.getIntPtrConstant(0));
6810
6811 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006812 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006813 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006814 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006815 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006816 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006817 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006818 MVT::v2f64, Bias)));
6819 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006820 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006821 DAG.getIntPtrConstant(0));
6822
6823 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006824 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006825
6826 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006827 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006828
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006830 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006831 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006832 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006833 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006834 }
6835
6836 // Handle final rounding.
6837 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006838}
6839
Dan Gohmand858e902010-04-17 15:26:15 +00006840SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6841 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006842 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006843 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006844
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006845 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006846 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6847 // the optimization here.
6848 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006849 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006850
Owen Andersone50ed302009-08-10 22:56:29 +00006851 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006852 EVT DstVT = Op.getValueType();
6853 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006854 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006855 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006856 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006857
6858 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006860 if (SrcVT == MVT::i32) {
6861 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6862 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6863 getPointerTy(), StackSlot, WordOff);
6864 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006865 StackSlot, MachinePointerInfo(),
6866 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006867 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006868 OffsetSlot, MachinePointerInfo(),
6869 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006870 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6871 return Fild;
6872 }
6873
6874 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6875 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006876 StackSlot, MachinePointerInfo(),
6877 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006878 // For i64 source, we need to add the appropriate power of 2 if the input
6879 // was negative. This is the same as the optimization in
6880 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6881 // we must be careful to do the computation in x87 extended precision, not
6882 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006883 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6884 MachineMemOperand *MMO =
6885 DAG.getMachineFunction()
6886 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6887 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006888
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006889 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6890 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006891 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6892 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006893
6894 APInt FF(32, 0x5F800000ULL);
6895
6896 // Check whether the sign bit is set.
6897 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6898 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6899 ISD::SETLT);
6900
6901 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6902 SDValue FudgePtr = DAG.getConstantPool(
6903 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6904 getPointerTy());
6905
6906 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6907 SDValue Zero = DAG.getIntPtrConstant(0);
6908 SDValue Four = DAG.getIntPtrConstant(4);
6909 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6910 Zero, Four);
6911 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6912
6913 // Load the value out, extending it from f32 to f80.
6914 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006915 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006916 FudgePtr, MachinePointerInfo::getConstantPool(),
6917 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006918 // Extend everything to 80 bits to force it to be done on x87.
6919 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6920 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006921}
6922
Dan Gohman475871a2008-07-27 21:46:04 +00006923std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006924FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006925 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006926
Owen Andersone50ed302009-08-10 22:56:29 +00006927 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006928
6929 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6931 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006932 }
6933
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6935 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006936 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006937
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006938 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006939 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006940 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006941 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006942 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006944 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006945 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006946
Evan Cheng87c89352007-10-15 20:11:21 +00006947 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6948 // stack slot.
6949 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006950 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006951 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006952 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006953
Michael J. Spencerec38de22010-10-10 22:04:20 +00006954
6955
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006958 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006959 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6960 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6961 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006962 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006963
Dan Gohman475871a2008-07-27 21:46:04 +00006964 SDValue Chain = DAG.getEntryNode();
6965 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006966 EVT TheVT = Op.getOperand(0).getValueType();
6967 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006968 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006969 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006970 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006971 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006972 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006973 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006974 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006975 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006976
Chris Lattner492a43e2010-09-22 01:28:21 +00006977 MachineMemOperand *MMO =
6978 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6979 MachineMemOperand::MOLoad, MemSize, MemSize);
6980 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6981 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006982 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006983 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006984 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6985 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006986
Chris Lattner07290932010-09-22 01:05:16 +00006987 MachineMemOperand *MMO =
6988 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6989 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006990
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006993 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6994 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006995
Chris Lattner27a6c732007-11-24 07:07:01 +00006996 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006997}
6998
Dan Gohmand858e902010-04-17 15:26:15 +00006999SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7000 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007001 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007002 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007003
Eli Friedman948e95a2009-05-23 09:59:16 +00007004 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007005 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007006 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7007 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007008
Chris Lattner27a6c732007-11-24 07:07:01 +00007009 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007010 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007011 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007012}
7013
Dan Gohmand858e902010-04-17 15:26:15 +00007014SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7015 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007016 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7017 SDValue FIST = Vals.first, StackSlot = Vals.second;
7018 assert(FIST.getNode() && "Unexpected failure");
7019
7020 // Load the result.
7021 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007022 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007023}
7024
Dan Gohmand858e902010-04-17 15:26:15 +00007025SDValue X86TargetLowering::LowerFABS(SDValue Op,
7026 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007027 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007028 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007029 EVT VT = Op.getValueType();
7030 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007031 if (VT.isVector())
7032 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007033 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007034 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007035 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007036 CV.push_back(C);
7037 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007038 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007039 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007040 CV.push_back(C);
7041 CV.push_back(C);
7042 CV.push_back(C);
7043 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007044 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007045 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007046 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007047 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007048 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007049 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007050 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007051}
7052
Dan Gohmand858e902010-04-17 15:26:15 +00007053SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007054 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007055 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007056 EVT VT = Op.getValueType();
7057 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007058 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007059 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007060 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007062 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007063 CV.push_back(C);
7064 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007066 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007067 CV.push_back(C);
7068 CV.push_back(C);
7069 CV.push_back(C);
7070 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007071 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007072 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007073 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007074 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007075 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007076 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007077 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007078 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007080 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007081 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007083 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007084 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007085 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007086}
7087
Dan Gohmand858e902010-04-17 15:26:15 +00007088SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007089 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007090 SDValue Op0 = Op.getOperand(0);
7091 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007092 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007093 EVT VT = Op.getValueType();
7094 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007095
7096 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007097 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007098 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007099 SrcVT = VT;
7100 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007101 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007102 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007103 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007104 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007105 }
7106
7107 // At this point the operands and the result should have the same
7108 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007109
Evan Cheng68c47cb2007-01-05 07:55:56 +00007110 // First get the sign bit of second operand.
7111 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007113 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7114 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007115 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007116 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7117 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7118 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7119 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007120 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007121 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007122 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007123 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007124 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007125 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007126 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007127
7128 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007129 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 // Op0 is MVT::f32, Op1 is MVT::f64.
7131 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7132 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7133 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007134 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007135 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007136 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007137 }
7138
Evan Cheng73d6cf12007-01-05 21:37:56 +00007139 // Clear first operand sign bit.
7140 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007141 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007142 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7143 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007144 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007145 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7146 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7147 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7148 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007149 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007150 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007151 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007152 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007153 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007154 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007155 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007156
7157 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007158 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007159}
7160
Dan Gohman076aee32009-03-04 19:44:21 +00007161/// Emit nodes that will be selected as "test Op0,Op0", or something
7162/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007163SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007164 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007165 DebugLoc dl = Op.getDebugLoc();
7166
Dan Gohman31125812009-03-07 01:58:32 +00007167 // CF and OF aren't always set the way we want. Determine which
7168 // of these we need.
7169 bool NeedCF = false;
7170 bool NeedOF = false;
7171 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007172 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007173 case X86::COND_A: case X86::COND_AE:
7174 case X86::COND_B: case X86::COND_BE:
7175 NeedCF = true;
7176 break;
7177 case X86::COND_G: case X86::COND_GE:
7178 case X86::COND_L: case X86::COND_LE:
7179 case X86::COND_O: case X86::COND_NO:
7180 NeedOF = true;
7181 break;
Dan Gohman31125812009-03-07 01:58:32 +00007182 }
7183
Dan Gohman076aee32009-03-04 19:44:21 +00007184 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007185 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7186 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007187 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7188 // Emit a CMP with 0, which is the TEST pattern.
7189 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7190 DAG.getConstant(0, Op.getValueType()));
7191
7192 unsigned Opcode = 0;
7193 unsigned NumOperands = 0;
7194 switch (Op.getNode()->getOpcode()) {
7195 case ISD::ADD:
7196 // Due to an isel shortcoming, be conservative if this add is likely to be
7197 // selected as part of a load-modify-store instruction. When the root node
7198 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7199 // uses of other nodes in the match, such as the ADD in this case. This
7200 // leads to the ADD being left around and reselected, with the result being
7201 // two adds in the output. Alas, even if none our users are stores, that
7202 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7203 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7204 // climbing the DAG back to the root, and it doesn't seem to be worth the
7205 // effort.
7206 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007207 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007208 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7209 goto default_case;
7210
7211 if (ConstantSDNode *C =
7212 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7213 // An add of one will be selected as an INC.
7214 if (C->getAPIntValue() == 1) {
7215 Opcode = X86ISD::INC;
7216 NumOperands = 1;
7217 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007218 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007219
7220 // An add of negative one (subtract of one) will be selected as a DEC.
7221 if (C->getAPIntValue().isAllOnesValue()) {
7222 Opcode = X86ISD::DEC;
7223 NumOperands = 1;
7224 break;
7225 }
Dan Gohman076aee32009-03-04 19:44:21 +00007226 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007227
7228 // Otherwise use a regular EFLAGS-setting add.
7229 Opcode = X86ISD::ADD;
7230 NumOperands = 2;
7231 break;
7232 case ISD::AND: {
7233 // If the primary and result isn't used, don't bother using X86ISD::AND,
7234 // because a TEST instruction will be better.
7235 bool NonFlagUse = false;
7236 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7237 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7238 SDNode *User = *UI;
7239 unsigned UOpNo = UI.getOperandNo();
7240 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7241 // Look pass truncate.
7242 UOpNo = User->use_begin().getOperandNo();
7243 User = *User->use_begin();
7244 }
7245
7246 if (User->getOpcode() != ISD::BRCOND &&
7247 User->getOpcode() != ISD::SETCC &&
7248 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7249 NonFlagUse = true;
7250 break;
7251 }
Dan Gohman076aee32009-03-04 19:44:21 +00007252 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007253
7254 if (!NonFlagUse)
7255 break;
7256 }
7257 // FALL THROUGH
7258 case ISD::SUB:
7259 case ISD::OR:
7260 case ISD::XOR:
7261 // Due to the ISEL shortcoming noted above, be conservative if this op is
7262 // likely to be selected as part of a load-modify-store instruction.
7263 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7264 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7265 if (UI->getOpcode() == ISD::STORE)
7266 goto default_case;
7267
7268 // Otherwise use a regular EFLAGS-setting instruction.
7269 switch (Op.getNode()->getOpcode()) {
7270 default: llvm_unreachable("unexpected operator!");
7271 case ISD::SUB: Opcode = X86ISD::SUB; break;
7272 case ISD::OR: Opcode = X86ISD::OR; break;
7273 case ISD::XOR: Opcode = X86ISD::XOR; break;
7274 case ISD::AND: Opcode = X86ISD::AND; break;
7275 }
7276
7277 NumOperands = 2;
7278 break;
7279 case X86ISD::ADD:
7280 case X86ISD::SUB:
7281 case X86ISD::INC:
7282 case X86ISD::DEC:
7283 case X86ISD::OR:
7284 case X86ISD::XOR:
7285 case X86ISD::AND:
7286 return SDValue(Op.getNode(), 1);
7287 default:
7288 default_case:
7289 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007290 }
7291
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007292 if (Opcode == 0)
7293 // Emit a CMP with 0, which is the TEST pattern.
7294 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7295 DAG.getConstant(0, Op.getValueType()));
7296
7297 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7298 SmallVector<SDValue, 4> Ops;
7299 for (unsigned i = 0; i != NumOperands; ++i)
7300 Ops.push_back(Op.getOperand(i));
7301
7302 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7303 DAG.ReplaceAllUsesWith(Op, New);
7304 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007305}
7306
7307/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7308/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007309SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007310 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007311 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7312 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007313 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007314
7315 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007317}
7318
Evan Chengd40d03e2010-01-06 19:38:29 +00007319/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7320/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007321SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7322 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007323 SDValue Op0 = And.getOperand(0);
7324 SDValue Op1 = And.getOperand(1);
7325 if (Op0.getOpcode() == ISD::TRUNCATE)
7326 Op0 = Op0.getOperand(0);
7327 if (Op1.getOpcode() == ISD::TRUNCATE)
7328 Op1 = Op1.getOperand(0);
7329
Evan Chengd40d03e2010-01-06 19:38:29 +00007330 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007331 if (Op1.getOpcode() == ISD::SHL)
7332 std::swap(Op0, Op1);
7333 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007334 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7335 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007336 // If we looked past a truncate, check that it's only truncating away
7337 // known zeros.
7338 unsigned BitWidth = Op0.getValueSizeInBits();
7339 unsigned AndBitWidth = And.getValueSizeInBits();
7340 if (BitWidth > AndBitWidth) {
7341 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7342 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7343 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7344 return SDValue();
7345 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007346 LHS = Op1;
7347 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007348 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007349 } else if (Op1.getOpcode() == ISD::Constant) {
7350 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7351 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007352 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7353 LHS = AndLHS.getOperand(0);
7354 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007355 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007356 }
Evan Cheng0488db92007-09-25 01:57:46 +00007357
Evan Chengd40d03e2010-01-06 19:38:29 +00007358 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007359 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007360 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007361 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007362 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007363 // Also promote i16 to i32 for performance / code size reason.
7364 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007365 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007366 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007367
Evan Chengd40d03e2010-01-06 19:38:29 +00007368 // If the operand types disagree, extend the shift amount to match. Since
7369 // BT ignores high bits (like shifts) we can use anyextend.
7370 if (LHS.getValueType() != RHS.getValueType())
7371 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007372
Evan Chengd40d03e2010-01-06 19:38:29 +00007373 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7374 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7375 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7376 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007377 }
7378
Evan Cheng54de3ea2010-01-05 06:52:31 +00007379 return SDValue();
7380}
7381
Dan Gohmand858e902010-04-17 15:26:15 +00007382SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007383 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7384 SDValue Op0 = Op.getOperand(0);
7385 SDValue Op1 = Op.getOperand(1);
7386 DebugLoc dl = Op.getDebugLoc();
7387 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7388
7389 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007390 // Lower (X & (1 << N)) == 0 to BT(X, N).
7391 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7392 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007393 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007394 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007395 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007396 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7397 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7398 if (NewSetCC.getNode())
7399 return NewSetCC;
7400 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007401
Chris Lattner481eebc2010-12-19 21:23:48 +00007402 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7403 // these.
7404 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007405 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7406 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7407 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007408
Chris Lattner481eebc2010-12-19 21:23:48 +00007409 // If the input is a setcc, then reuse the input setcc or use a new one with
7410 // the inverted condition.
7411 if (Op0.getOpcode() == X86ISD::SETCC) {
7412 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7413 bool Invert = (CC == ISD::SETNE) ^
7414 cast<ConstantSDNode>(Op1)->isNullValue();
7415 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007416
Evan Cheng2c755ba2010-02-27 07:36:59 +00007417 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007418 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7419 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7420 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007421 }
7422
Evan Chenge5b51ac2010-04-17 06:13:15 +00007423 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007424 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007425 if (X86CC == X86::COND_INVALID)
7426 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007427
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007428 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007429 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007430 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007431}
7432
Dan Gohmand858e902010-04-17 15:26:15 +00007433SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007434 SDValue Cond;
7435 SDValue Op0 = Op.getOperand(0);
7436 SDValue Op1 = Op.getOperand(1);
7437 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007438 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007439 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7440 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007441 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007442
7443 if (isFP) {
7444 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007445 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007446 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7447 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007448 bool Swap = false;
7449
7450 switch (SetCCOpcode) {
7451 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007452 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007453 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007454 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007455 case ISD::SETGT: Swap = true; // Fallthrough
7456 case ISD::SETLT:
7457 case ISD::SETOLT: SSECC = 1; break;
7458 case ISD::SETOGE:
7459 case ISD::SETGE: Swap = true; // Fallthrough
7460 case ISD::SETLE:
7461 case ISD::SETOLE: SSECC = 2; break;
7462 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007463 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007464 case ISD::SETNE: SSECC = 4; break;
7465 case ISD::SETULE: Swap = true;
7466 case ISD::SETUGE: SSECC = 5; break;
7467 case ISD::SETULT: Swap = true;
7468 case ISD::SETUGT: SSECC = 6; break;
7469 case ISD::SETO: SSECC = 7; break;
7470 }
7471 if (Swap)
7472 std::swap(Op0, Op1);
7473
Nate Begemanfb8ead02008-07-25 19:05:58 +00007474 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007475 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007476 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007477 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007478 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7479 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007480 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007481 }
7482 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007483 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007484 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7485 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007486 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007487 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007488 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007489 }
7490 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007492 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007493
Nate Begeman30a0de92008-07-17 16:51:19 +00007494 // We are handling one of the integer comparisons here. Since SSE only has
7495 // GT and EQ comparisons for integer, swapping operands and multiple
7496 // operations may be required for some comparisons.
7497 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7498 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007499
Owen Anderson825b72b2009-08-11 20:47:22 +00007500 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007501 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007502 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007503 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007504 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7505 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007506 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007507
Nate Begeman30a0de92008-07-17 16:51:19 +00007508 switch (SetCCOpcode) {
7509 default: break;
7510 case ISD::SETNE: Invert = true;
7511 case ISD::SETEQ: Opc = EQOpc; break;
7512 case ISD::SETLT: Swap = true;
7513 case ISD::SETGT: Opc = GTOpc; break;
7514 case ISD::SETGE: Swap = true;
7515 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7516 case ISD::SETULT: Swap = true;
7517 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7518 case ISD::SETUGE: Swap = true;
7519 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7520 }
7521 if (Swap)
7522 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007523
Nate Begeman30a0de92008-07-17 16:51:19 +00007524 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7525 // bits of the inputs before performing those operations.
7526 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007527 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007528 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7529 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007530 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007531 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7532 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007533 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7534 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007536
Dale Johannesenace16102009-02-03 19:33:06 +00007537 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007538
7539 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007540 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007541 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007542
Nate Begeman30a0de92008-07-17 16:51:19 +00007543 return Result;
7544}
Evan Cheng0488db92007-09-25 01:57:46 +00007545
Evan Cheng370e5342008-12-03 08:38:43 +00007546// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007547static bool isX86LogicalCmp(SDValue Op) {
7548 unsigned Opc = Op.getNode()->getOpcode();
7549 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7550 return true;
7551 if (Op.getResNo() == 1 &&
7552 (Opc == X86ISD::ADD ||
7553 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007554 Opc == X86ISD::ADC ||
7555 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007556 Opc == X86ISD::SMUL ||
7557 Opc == X86ISD::UMUL ||
7558 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007559 Opc == X86ISD::DEC ||
7560 Opc == X86ISD::OR ||
7561 Opc == X86ISD::XOR ||
7562 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007563 return true;
7564
Chris Lattner9637d5b2010-12-05 07:49:54 +00007565 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7566 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007567
Dan Gohman076aee32009-03-04 19:44:21 +00007568 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007569}
7570
Chris Lattnera2b56002010-12-05 01:23:24 +00007571static bool isZero(SDValue V) {
7572 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7573 return C && C->isNullValue();
7574}
7575
Chris Lattner96908b12010-12-05 02:00:51 +00007576static bool isAllOnes(SDValue V) {
7577 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7578 return C && C->isAllOnesValue();
7579}
7580
Dan Gohmand858e902010-04-17 15:26:15 +00007581SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007582 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007583 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007584 SDValue Op1 = Op.getOperand(1);
7585 SDValue Op2 = Op.getOperand(2);
7586 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007587 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007588
Dan Gohman1a492952009-10-20 16:22:37 +00007589 if (Cond.getOpcode() == ISD::SETCC) {
7590 SDValue NewCond = LowerSETCC(Cond, DAG);
7591 if (NewCond.getNode())
7592 Cond = NewCond;
7593 }
Evan Cheng734503b2006-09-11 02:19:56 +00007594
Chris Lattnera2b56002010-12-05 01:23:24 +00007595 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007596 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007597 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007598 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007599 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007600 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7601 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007602 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007603
Chris Lattnera2b56002010-12-05 01:23:24 +00007604 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007605
7606 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007607 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7608 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007609
7610 SDValue CmpOp0 = Cmp.getOperand(0);
7611 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7612 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007613
Chris Lattner96908b12010-12-05 02:00:51 +00007614 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007615 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7616 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007617
Chris Lattner96908b12010-12-05 02:00:51 +00007618 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7619 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007620
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007621 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007622 if (N2C == 0 || !N2C->isNullValue())
7623 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7624 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007625 }
7626 }
7627
Chris Lattnera2b56002010-12-05 01:23:24 +00007628 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007629 if (Cond.getOpcode() == ISD::AND &&
7630 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7631 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007632 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007633 Cond = Cond.getOperand(0);
7634 }
7635
Evan Cheng3f41d662007-10-08 22:16:29 +00007636 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7637 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007638 if (Cond.getOpcode() == X86ISD::SETCC ||
7639 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007640 CC = Cond.getOperand(0);
7641
Dan Gohman475871a2008-07-27 21:46:04 +00007642 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007643 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007644 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007645
Evan Cheng3f41d662007-10-08 22:16:29 +00007646 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007647 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007648 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007649 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007650
Chris Lattnerd1980a52009-03-12 06:52:53 +00007651 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7652 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007653 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007654 addTest = false;
7655 }
7656 }
7657
7658 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007659 // Look pass the truncate.
7660 if (Cond.getOpcode() == ISD::TRUNCATE)
7661 Cond = Cond.getOperand(0);
7662
7663 // We know the result of AND is compared against zero. Try to match
7664 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007665 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007666 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007667 if (NewSetCC.getNode()) {
7668 CC = NewSetCC.getOperand(0);
7669 Cond = NewSetCC.getOperand(1);
7670 addTest = false;
7671 }
7672 }
7673 }
7674
7675 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007676 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007677 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007678 }
7679
Benjamin Kramere915ff32010-12-22 23:09:28 +00007680 // a < b ? -1 : 0 -> RES = ~setcc_carry
7681 // a < b ? 0 : -1 -> RES = setcc_carry
7682 // a >= b ? -1 : 0 -> RES = setcc_carry
7683 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7684 if (Cond.getOpcode() == X86ISD::CMP) {
7685 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7686
7687 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7688 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7689 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7690 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7691 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7692 return DAG.getNOT(DL, Res, Res.getValueType());
7693 return Res;
7694 }
7695 }
7696
Evan Cheng0488db92007-09-25 01:57:46 +00007697 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7698 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007699 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007700 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007701 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007702}
7703
Evan Cheng370e5342008-12-03 08:38:43 +00007704// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7705// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7706// from the AND / OR.
7707static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7708 Opc = Op.getOpcode();
7709 if (Opc != ISD::OR && Opc != ISD::AND)
7710 return false;
7711 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7712 Op.getOperand(0).hasOneUse() &&
7713 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7714 Op.getOperand(1).hasOneUse());
7715}
7716
Evan Cheng961d6d42009-02-02 08:19:07 +00007717// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7718// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007719static bool isXor1OfSetCC(SDValue Op) {
7720 if (Op.getOpcode() != ISD::XOR)
7721 return false;
7722 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7723 if (N1C && N1C->getAPIntValue() == 1) {
7724 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7725 Op.getOperand(0).hasOneUse();
7726 }
7727 return false;
7728}
7729
Dan Gohmand858e902010-04-17 15:26:15 +00007730SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007731 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007732 SDValue Chain = Op.getOperand(0);
7733 SDValue Cond = Op.getOperand(1);
7734 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007735 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007736 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007737
Dan Gohman1a492952009-10-20 16:22:37 +00007738 if (Cond.getOpcode() == ISD::SETCC) {
7739 SDValue NewCond = LowerSETCC(Cond, DAG);
7740 if (NewCond.getNode())
7741 Cond = NewCond;
7742 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007743#if 0
7744 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007745 else if (Cond.getOpcode() == X86ISD::ADD ||
7746 Cond.getOpcode() == X86ISD::SUB ||
7747 Cond.getOpcode() == X86ISD::SMUL ||
7748 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007749 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007750#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007751
Evan Chengad9c0a32009-12-15 00:53:42 +00007752 // Look pass (and (setcc_carry (cmp ...)), 1).
7753 if (Cond.getOpcode() == ISD::AND &&
7754 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7755 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007756 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007757 Cond = Cond.getOperand(0);
7758 }
7759
Evan Cheng3f41d662007-10-08 22:16:29 +00007760 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7761 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007762 if (Cond.getOpcode() == X86ISD::SETCC ||
7763 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007764 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007765
Dan Gohman475871a2008-07-27 21:46:04 +00007766 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007767 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007768 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007769 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007770 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007771 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007772 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007773 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007774 default: break;
7775 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007776 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007777 // These can only come from an arithmetic instruction with overflow,
7778 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007779 Cond = Cond.getNode()->getOperand(1);
7780 addTest = false;
7781 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007782 }
Evan Cheng0488db92007-09-25 01:57:46 +00007783 }
Evan Cheng370e5342008-12-03 08:38:43 +00007784 } else {
7785 unsigned CondOpc;
7786 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7787 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007788 if (CondOpc == ISD::OR) {
7789 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7790 // two branches instead of an explicit OR instruction with a
7791 // separate test.
7792 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007793 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007794 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007795 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007796 Chain, Dest, CC, Cmp);
7797 CC = Cond.getOperand(1).getOperand(0);
7798 Cond = Cmp;
7799 addTest = false;
7800 }
7801 } else { // ISD::AND
7802 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7803 // two branches instead of an explicit AND instruction with a
7804 // separate test. However, we only do this if this block doesn't
7805 // have a fall-through edge, because this requires an explicit
7806 // jmp when the condition is false.
7807 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007808 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007809 Op.getNode()->hasOneUse()) {
7810 X86::CondCode CCode =
7811 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7812 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007813 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007814 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007815 // Look for an unconditional branch following this conditional branch.
7816 // We need this because we need to reverse the successors in order
7817 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007818 if (User->getOpcode() == ISD::BR) {
7819 SDValue FalseBB = User->getOperand(1);
7820 SDNode *NewBR =
7821 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007822 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007823 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007824 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007825
Dale Johannesene4d209d2009-02-03 20:21:25 +00007826 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007827 Chain, Dest, CC, Cmp);
7828 X86::CondCode CCode =
7829 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7830 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007831 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007832 Cond = Cmp;
7833 addTest = false;
7834 }
7835 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007836 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007837 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7838 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7839 // It should be transformed during dag combiner except when the condition
7840 // is set by a arithmetics with overflow node.
7841 X86::CondCode CCode =
7842 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7843 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007845 Cond = Cond.getOperand(0).getOperand(1);
7846 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007847 }
Evan Cheng0488db92007-09-25 01:57:46 +00007848 }
7849
7850 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007851 // Look pass the truncate.
7852 if (Cond.getOpcode() == ISD::TRUNCATE)
7853 Cond = Cond.getOperand(0);
7854
7855 // We know the result of AND is compared against zero. Try to match
7856 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007857 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007858 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7859 if (NewSetCC.getNode()) {
7860 CC = NewSetCC.getOperand(0);
7861 Cond = NewSetCC.getOperand(1);
7862 addTest = false;
7863 }
7864 }
7865 }
7866
7867 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007868 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007869 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007870 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007871 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007872 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007873}
7874
Anton Korobeynikove060b532007-04-17 19:34:00 +00007875
7876// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7877// Calls to _alloca is needed to probe the stack when allocating more than 4k
7878// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7879// that the guard pages used by the OS virtual memory manager are allocated in
7880// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007881SDValue
7882X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007883 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007884 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007885 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007886 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007887
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007888 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007889 SDValue Chain = Op.getOperand(0);
7890 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007891 // FIXME: Ensure alignment here
7892
Dan Gohman475871a2008-07-27 21:46:04 +00007893 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007894
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007896
Dale Johannesendd64c412009-02-04 00:33:20 +00007897 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007898 Flag = Chain.getValue(1);
7899
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007900 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007901
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007902 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007903 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007904
Dale Johannesendd64c412009-02-04 00:33:20 +00007905 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007906
Dan Gohman475871a2008-07-27 21:46:04 +00007907 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007908 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007909}
7910
Dan Gohmand858e902010-04-17 15:26:15 +00007911SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007912 MachineFunction &MF = DAG.getMachineFunction();
7913 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7914
Dan Gohman69de1932008-02-06 22:27:42 +00007915 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007916 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007917
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007918 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007919 // vastart just stores the address of the VarArgsFrameIndex slot into the
7920 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007921 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7922 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007923 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7924 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007925 }
7926
7927 // __va_list_tag:
7928 // gp_offset (0 - 6 * 8)
7929 // fp_offset (48 - 48 + 8 * 16)
7930 // overflow_arg_area (point to parameters coming in memory).
7931 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007932 SmallVector<SDValue, 8> MemOps;
7933 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007934 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007935 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007936 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7937 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007938 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007939 MemOps.push_back(Store);
7940
7941 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007942 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007943 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007944 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007945 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7946 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007947 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007948 MemOps.push_back(Store);
7949
7950 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007951 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007952 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007953 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7954 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007955 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7956 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007957 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007958 MemOps.push_back(Store);
7959
7960 // Store ptr to reg_save_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(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007963 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7964 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007965 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7966 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007967 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007968 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007969 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007970}
7971
Dan Gohmand858e902010-04-17 15:26:15 +00007972SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007973 assert(Subtarget->is64Bit() &&
7974 "LowerVAARG only handles 64-bit va_arg!");
7975 assert((Subtarget->isTargetLinux() ||
7976 Subtarget->isTargetDarwin()) &&
7977 "Unhandled target in LowerVAARG");
7978 assert(Op.getNode()->getNumOperands() == 4);
7979 SDValue Chain = Op.getOperand(0);
7980 SDValue SrcPtr = Op.getOperand(1);
7981 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7982 unsigned Align = Op.getConstantOperandVal(3);
7983 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007984
Dan Gohman320afb82010-10-12 18:00:49 +00007985 EVT ArgVT = Op.getNode()->getValueType(0);
7986 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7987 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7988 uint8_t ArgMode;
7989
7990 // Decide which area this value should be read from.
7991 // TODO: Implement the AMD64 ABI in its entirety. This simple
7992 // selection mechanism works only for the basic types.
7993 if (ArgVT == MVT::f80) {
7994 llvm_unreachable("va_arg for f80 not yet implemented");
7995 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7996 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7997 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7998 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7999 } else {
8000 llvm_unreachable("Unhandled argument type in LowerVAARG");
8001 }
8002
8003 if (ArgMode == 2) {
8004 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008005 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008006 !(DAG.getMachineFunction()
8007 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008008 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008009 }
8010
8011 // Insert VAARG_64 node into the DAG
8012 // VAARG_64 returns two values: Variable Argument Address, Chain
8013 SmallVector<SDValue, 11> InstOps;
8014 InstOps.push_back(Chain);
8015 InstOps.push_back(SrcPtr);
8016 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8017 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8018 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8019 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8020 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8021 VTs, &InstOps[0], InstOps.size(),
8022 MVT::i64,
8023 MachinePointerInfo(SV),
8024 /*Align=*/0,
8025 /*Volatile=*/false,
8026 /*ReadMem=*/true,
8027 /*WriteMem=*/true);
8028 Chain = VAARG.getValue(1);
8029
8030 // Load the next argument and return it
8031 return DAG.getLoad(ArgVT, dl,
8032 Chain,
8033 VAARG,
8034 MachinePointerInfo(),
8035 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008036}
8037
Dan Gohmand858e902010-04-17 15:26:15 +00008038SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008039 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008040 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008041 SDValue Chain = Op.getOperand(0);
8042 SDValue DstPtr = Op.getOperand(1);
8043 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008044 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8045 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008046 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008047
Chris Lattnere72f2022010-09-21 05:40:29 +00008048 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008049 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008050 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008051 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008052}
8053
Dan Gohman475871a2008-07-27 21:46:04 +00008054SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008055X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008056 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008057 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008058 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008059 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008060 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008061 case Intrinsic::x86_sse_comieq_ss:
8062 case Intrinsic::x86_sse_comilt_ss:
8063 case Intrinsic::x86_sse_comile_ss:
8064 case Intrinsic::x86_sse_comigt_ss:
8065 case Intrinsic::x86_sse_comige_ss:
8066 case Intrinsic::x86_sse_comineq_ss:
8067 case Intrinsic::x86_sse_ucomieq_ss:
8068 case Intrinsic::x86_sse_ucomilt_ss:
8069 case Intrinsic::x86_sse_ucomile_ss:
8070 case Intrinsic::x86_sse_ucomigt_ss:
8071 case Intrinsic::x86_sse_ucomige_ss:
8072 case Intrinsic::x86_sse_ucomineq_ss:
8073 case Intrinsic::x86_sse2_comieq_sd:
8074 case Intrinsic::x86_sse2_comilt_sd:
8075 case Intrinsic::x86_sse2_comile_sd:
8076 case Intrinsic::x86_sse2_comigt_sd:
8077 case Intrinsic::x86_sse2_comige_sd:
8078 case Intrinsic::x86_sse2_comineq_sd:
8079 case Intrinsic::x86_sse2_ucomieq_sd:
8080 case Intrinsic::x86_sse2_ucomilt_sd:
8081 case Intrinsic::x86_sse2_ucomile_sd:
8082 case Intrinsic::x86_sse2_ucomigt_sd:
8083 case Intrinsic::x86_sse2_ucomige_sd:
8084 case Intrinsic::x86_sse2_ucomineq_sd: {
8085 unsigned Opc = 0;
8086 ISD::CondCode CC = ISD::SETCC_INVALID;
8087 switch (IntNo) {
8088 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008089 case Intrinsic::x86_sse_comieq_ss:
8090 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008091 Opc = X86ISD::COMI;
8092 CC = ISD::SETEQ;
8093 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008094 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008095 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008096 Opc = X86ISD::COMI;
8097 CC = ISD::SETLT;
8098 break;
8099 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008100 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008101 Opc = X86ISD::COMI;
8102 CC = ISD::SETLE;
8103 break;
8104 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008105 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008106 Opc = X86ISD::COMI;
8107 CC = ISD::SETGT;
8108 break;
8109 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008110 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111 Opc = X86ISD::COMI;
8112 CC = ISD::SETGE;
8113 break;
8114 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008115 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008116 Opc = X86ISD::COMI;
8117 CC = ISD::SETNE;
8118 break;
8119 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008120 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008121 Opc = X86ISD::UCOMI;
8122 CC = ISD::SETEQ;
8123 break;
8124 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008125 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008126 Opc = X86ISD::UCOMI;
8127 CC = ISD::SETLT;
8128 break;
8129 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008130 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008131 Opc = X86ISD::UCOMI;
8132 CC = ISD::SETLE;
8133 break;
8134 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008135 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008136 Opc = X86ISD::UCOMI;
8137 CC = ISD::SETGT;
8138 break;
8139 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008140 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008141 Opc = X86ISD::UCOMI;
8142 CC = ISD::SETGE;
8143 break;
8144 case Intrinsic::x86_sse_ucomineq_ss:
8145 case Intrinsic::x86_sse2_ucomineq_sd:
8146 Opc = X86ISD::UCOMI;
8147 CC = ISD::SETNE;
8148 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008149 }
Evan Cheng734503b2006-09-11 02:19:56 +00008150
Dan Gohman475871a2008-07-27 21:46:04 +00008151 SDValue LHS = Op.getOperand(1);
8152 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008153 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008154 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008155 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8156 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8157 DAG.getConstant(X86CC, MVT::i8), Cond);
8158 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008159 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008160 // ptest and testp intrinsics. The intrinsic these come from are designed to
8161 // return an integer value, not just an instruction so lower it to the ptest
8162 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008163 case Intrinsic::x86_sse41_ptestz:
8164 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008165 case Intrinsic::x86_sse41_ptestnzc:
8166 case Intrinsic::x86_avx_ptestz_256:
8167 case Intrinsic::x86_avx_ptestc_256:
8168 case Intrinsic::x86_avx_ptestnzc_256:
8169 case Intrinsic::x86_avx_vtestz_ps:
8170 case Intrinsic::x86_avx_vtestc_ps:
8171 case Intrinsic::x86_avx_vtestnzc_ps:
8172 case Intrinsic::x86_avx_vtestz_pd:
8173 case Intrinsic::x86_avx_vtestc_pd:
8174 case Intrinsic::x86_avx_vtestnzc_pd:
8175 case Intrinsic::x86_avx_vtestz_ps_256:
8176 case Intrinsic::x86_avx_vtestc_ps_256:
8177 case Intrinsic::x86_avx_vtestnzc_ps_256:
8178 case Intrinsic::x86_avx_vtestz_pd_256:
8179 case Intrinsic::x86_avx_vtestc_pd_256:
8180 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8181 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008182 unsigned X86CC = 0;
8183 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008184 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008185 case Intrinsic::x86_avx_vtestz_ps:
8186 case Intrinsic::x86_avx_vtestz_pd:
8187 case Intrinsic::x86_avx_vtestz_ps_256:
8188 case Intrinsic::x86_avx_vtestz_pd_256:
8189 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008190 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008191 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008192 // ZF = 1
8193 X86CC = X86::COND_E;
8194 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008195 case Intrinsic::x86_avx_vtestc_ps:
8196 case Intrinsic::x86_avx_vtestc_pd:
8197 case Intrinsic::x86_avx_vtestc_ps_256:
8198 case Intrinsic::x86_avx_vtestc_pd_256:
8199 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008200 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008201 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008202 // CF = 1
8203 X86CC = X86::COND_B;
8204 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008205 case Intrinsic::x86_avx_vtestnzc_ps:
8206 case Intrinsic::x86_avx_vtestnzc_pd:
8207 case Intrinsic::x86_avx_vtestnzc_ps_256:
8208 case Intrinsic::x86_avx_vtestnzc_pd_256:
8209 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008210 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008211 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008212 // ZF and CF = 0
8213 X86CC = X86::COND_A;
8214 break;
8215 }
Eric Christopherfd179292009-08-27 18:07:15 +00008216
Eric Christopher71c67532009-07-29 00:28:05 +00008217 SDValue LHS = Op.getOperand(1);
8218 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008219 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8220 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008221 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8222 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8223 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008224 }
Evan Cheng5759f972008-05-04 09:15:50 +00008225
8226 // Fix vector shift instructions where the last operand is a non-immediate
8227 // i32 value.
8228 case Intrinsic::x86_sse2_pslli_w:
8229 case Intrinsic::x86_sse2_pslli_d:
8230 case Intrinsic::x86_sse2_pslli_q:
8231 case Intrinsic::x86_sse2_psrli_w:
8232 case Intrinsic::x86_sse2_psrli_d:
8233 case Intrinsic::x86_sse2_psrli_q:
8234 case Intrinsic::x86_sse2_psrai_w:
8235 case Intrinsic::x86_sse2_psrai_d:
8236 case Intrinsic::x86_mmx_pslli_w:
8237 case Intrinsic::x86_mmx_pslli_d:
8238 case Intrinsic::x86_mmx_pslli_q:
8239 case Intrinsic::x86_mmx_psrli_w:
8240 case Intrinsic::x86_mmx_psrli_d:
8241 case Intrinsic::x86_mmx_psrli_q:
8242 case Intrinsic::x86_mmx_psrai_w:
8243 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008244 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008245 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008246 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008247
8248 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008250 switch (IntNo) {
8251 case Intrinsic::x86_sse2_pslli_w:
8252 NewIntNo = Intrinsic::x86_sse2_psll_w;
8253 break;
8254 case Intrinsic::x86_sse2_pslli_d:
8255 NewIntNo = Intrinsic::x86_sse2_psll_d;
8256 break;
8257 case Intrinsic::x86_sse2_pslli_q:
8258 NewIntNo = Intrinsic::x86_sse2_psll_q;
8259 break;
8260 case Intrinsic::x86_sse2_psrli_w:
8261 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8262 break;
8263 case Intrinsic::x86_sse2_psrli_d:
8264 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8265 break;
8266 case Intrinsic::x86_sse2_psrli_q:
8267 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8268 break;
8269 case Intrinsic::x86_sse2_psrai_w:
8270 NewIntNo = Intrinsic::x86_sse2_psra_w;
8271 break;
8272 case Intrinsic::x86_sse2_psrai_d:
8273 NewIntNo = Intrinsic::x86_sse2_psra_d;
8274 break;
8275 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008277 switch (IntNo) {
8278 case Intrinsic::x86_mmx_pslli_w:
8279 NewIntNo = Intrinsic::x86_mmx_psll_w;
8280 break;
8281 case Intrinsic::x86_mmx_pslli_d:
8282 NewIntNo = Intrinsic::x86_mmx_psll_d;
8283 break;
8284 case Intrinsic::x86_mmx_pslli_q:
8285 NewIntNo = Intrinsic::x86_mmx_psll_q;
8286 break;
8287 case Intrinsic::x86_mmx_psrli_w:
8288 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8289 break;
8290 case Intrinsic::x86_mmx_psrli_d:
8291 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8292 break;
8293 case Intrinsic::x86_mmx_psrli_q:
8294 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8295 break;
8296 case Intrinsic::x86_mmx_psrai_w:
8297 NewIntNo = Intrinsic::x86_mmx_psra_w;
8298 break;
8299 case Intrinsic::x86_mmx_psrai_d:
8300 NewIntNo = Intrinsic::x86_mmx_psra_d;
8301 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008302 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008303 }
8304 break;
8305 }
8306 }
Mon P Wangefa42202009-09-03 19:56:25 +00008307
8308 // The vector shift intrinsics with scalars uses 32b shift amounts but
8309 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8310 // to be zero.
8311 SDValue ShOps[4];
8312 ShOps[0] = ShAmt;
8313 ShOps[1] = DAG.getConstant(0, MVT::i32);
8314 if (ShAmtVT == MVT::v4i32) {
8315 ShOps[2] = DAG.getUNDEF(MVT::i32);
8316 ShOps[3] = DAG.getUNDEF(MVT::i32);
8317 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8318 } else {
8319 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008320// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008321 }
8322
Owen Andersone50ed302009-08-10 22:56:29 +00008323 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008324 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008325 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008326 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008327 Op.getOperand(1), ShAmt);
8328 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008329 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008330}
Evan Cheng72261582005-12-20 06:22:03 +00008331
Dan Gohmand858e902010-04-17 15:26:15 +00008332SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8333 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008334 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8335 MFI->setReturnAddressIsTaken(true);
8336
Bill Wendling64e87322009-01-16 19:25:27 +00008337 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008338 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008339
8340 if (Depth > 0) {
8341 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8342 SDValue Offset =
8343 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008344 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008345 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008346 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008347 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008348 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008349 }
8350
8351 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008352 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008353 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008354 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008355}
8356
Dan Gohmand858e902010-04-17 15:26:15 +00008357SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008358 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8359 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008360
Owen Andersone50ed302009-08-10 22:56:29 +00008361 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008362 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008363 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8364 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008365 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008366 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008367 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8368 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008369 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008370 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008371}
8372
Dan Gohman475871a2008-07-27 21:46:04 +00008373SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008374 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008375 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008376}
8377
Dan Gohmand858e902010-04-17 15:26:15 +00008378SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008379 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008380 SDValue Chain = Op.getOperand(0);
8381 SDValue Offset = Op.getOperand(1);
8382 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008383 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008384
Dan Gohmand8816272010-08-11 18:14:00 +00008385 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8386 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8387 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008388 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008389
Dan Gohmand8816272010-08-11 18:14:00 +00008390 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8391 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008392 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008393 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8394 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008395 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008396 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008397
Dale Johannesene4d209d2009-02-03 20:21:25 +00008398 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008399 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008400 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008401}
8402
Dan Gohman475871a2008-07-27 21:46:04 +00008403SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008404 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008405 SDValue Root = Op.getOperand(0);
8406 SDValue Trmp = Op.getOperand(1); // trampoline
8407 SDValue FPtr = Op.getOperand(2); // nested function
8408 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008409 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008410
Dan Gohman69de1932008-02-06 22:27:42 +00008411 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008412
8413 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008414 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008415
8416 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008417 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8418 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008419
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008420 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8421 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008422
8423 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8424
8425 // Load the pointer to the nested function into R11.
8426 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008427 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008429 Addr, MachinePointerInfo(TrmpAddr),
8430 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008431
Owen Anderson825b72b2009-08-11 20:47:22 +00008432 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8433 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008434 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8435 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008436 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008437
8438 // Load the 'nest' parameter value into R10.
8439 // R10 is specified in X86CallingConv.td
8440 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008441 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8442 DAG.getConstant(10, MVT::i64));
8443 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008444 Addr, MachinePointerInfo(TrmpAddr, 10),
8445 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008446
Owen Anderson825b72b2009-08-11 20:47:22 +00008447 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8448 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008449 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8450 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008451 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008452
8453 // Jump to the nested function.
8454 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008455 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8456 DAG.getConstant(20, MVT::i64));
8457 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008458 Addr, MachinePointerInfo(TrmpAddr, 20),
8459 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008460
8461 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008462 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8463 DAG.getConstant(22, MVT::i64));
8464 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008465 MachinePointerInfo(TrmpAddr, 22),
8466 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008467
Dan Gohman475871a2008-07-27 21:46:04 +00008468 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008469 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008470 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008471 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008472 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008473 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008474 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008475 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008476
8477 switch (CC) {
8478 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008479 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008480 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008481 case CallingConv::X86_StdCall: {
8482 // Pass 'nest' parameter in ECX.
8483 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008484 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008485
8486 // Check that ECX wasn't needed by an 'inreg' parameter.
8487 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008488 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008489
Chris Lattner58d74912008-03-12 17:45:29 +00008490 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008491 unsigned InRegCount = 0;
8492 unsigned Idx = 1;
8493
8494 for (FunctionType::param_iterator I = FTy->param_begin(),
8495 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008496 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008497 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008498 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008499
8500 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008501 report_fatal_error("Nest register in use - reduce number of inreg"
8502 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008503 }
8504 }
8505 break;
8506 }
8507 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008508 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008509 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008510 // Pass 'nest' parameter in EAX.
8511 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008512 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008513 break;
8514 }
8515
Dan Gohman475871a2008-07-27 21:46:04 +00008516 SDValue OutChains[4];
8517 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008518
Owen Anderson825b72b2009-08-11 20:47:22 +00008519 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8520 DAG.getConstant(10, MVT::i32));
8521 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008522
Chris Lattnera62fe662010-02-05 19:20:30 +00008523 // This is storing the opcode for MOV32ri.
8524 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008525 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008526 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008527 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008528 Trmp, MachinePointerInfo(TrmpAddr),
8529 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008530
Owen Anderson825b72b2009-08-11 20:47:22 +00008531 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8532 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008533 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8534 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008535 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008536
Chris Lattnera62fe662010-02-05 19:20:30 +00008537 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008538 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8539 DAG.getConstant(5, MVT::i32));
8540 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008541 MachinePointerInfo(TrmpAddr, 5),
8542 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008543
Owen Anderson825b72b2009-08-11 20:47:22 +00008544 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8545 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008546 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8547 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008548 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008549
Dan Gohman475871a2008-07-27 21:46:04 +00008550 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008551 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008552 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008553 }
8554}
8555
Dan Gohmand858e902010-04-17 15:26:15 +00008556SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8557 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008558 /*
8559 The rounding mode is in bits 11:10 of FPSR, and has the following
8560 settings:
8561 00 Round to nearest
8562 01 Round to -inf
8563 10 Round to +inf
8564 11 Round to 0
8565
8566 FLT_ROUNDS, on the other hand, expects the following:
8567 -1 Undefined
8568 0 Round to 0
8569 1 Round to nearest
8570 2 Round to +inf
8571 3 Round to -inf
8572
8573 To perform the conversion, we do:
8574 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8575 */
8576
8577 MachineFunction &MF = DAG.getMachineFunction();
8578 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008579 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008580 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008581 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008582 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008583
8584 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008585 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008586 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008587
Michael J. Spencerec38de22010-10-10 22:04:20 +00008588
Chris Lattner2156b792010-09-22 01:11:26 +00008589 MachineMemOperand *MMO =
8590 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8591 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008592
Chris Lattner2156b792010-09-22 01:11:26 +00008593 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8594 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8595 DAG.getVTList(MVT::Other),
8596 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008597
8598 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008599 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008600 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008601
8602 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008603 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008604 DAG.getNode(ISD::SRL, DL, MVT::i16,
8605 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008606 CWD, DAG.getConstant(0x800, MVT::i16)),
8607 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008608 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008609 DAG.getNode(ISD::SRL, DL, MVT::i16,
8610 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008611 CWD, DAG.getConstant(0x400, MVT::i16)),
8612 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008613
Dan Gohman475871a2008-07-27 21:46:04 +00008614 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008615 DAG.getNode(ISD::AND, DL, MVT::i16,
8616 DAG.getNode(ISD::ADD, DL, MVT::i16,
8617 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 DAG.getConstant(1, MVT::i16)),
8619 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008620
8621
Duncan Sands83ec4b62008-06-06 12:08:01 +00008622 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008623 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008624}
8625
Dan Gohmand858e902010-04-17 15:26:15 +00008626SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008627 EVT VT = Op.getValueType();
8628 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008629 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008630 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008631
8632 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008633 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008634 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008635 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008636 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008637 }
Evan Cheng18efe262007-12-14 02:13:44 +00008638
Evan Cheng152804e2007-12-14 08:30:15 +00008639 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008640 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008641 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008642
8643 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008644 SDValue Ops[] = {
8645 Op,
8646 DAG.getConstant(NumBits+NumBits-1, OpVT),
8647 DAG.getConstant(X86::COND_E, MVT::i8),
8648 Op.getValue(1)
8649 };
8650 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008651
8652 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008653 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008654
Owen Anderson825b72b2009-08-11 20:47:22 +00008655 if (VT == MVT::i8)
8656 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008657 return Op;
8658}
8659
Dan Gohmand858e902010-04-17 15:26:15 +00008660SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008661 EVT VT = Op.getValueType();
8662 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008663 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008664 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008665
8666 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 if (VT == MVT::i8) {
8668 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008669 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008670 }
Evan Cheng152804e2007-12-14 08:30:15 +00008671
8672 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008673 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008674 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008675
8676 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008677 SDValue Ops[] = {
8678 Op,
8679 DAG.getConstant(NumBits, OpVT),
8680 DAG.getConstant(X86::COND_E, MVT::i8),
8681 Op.getValue(1)
8682 };
8683 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008684
Owen Anderson825b72b2009-08-11 20:47:22 +00008685 if (VT == MVT::i8)
8686 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008687 return Op;
8688}
8689
Dan Gohmand858e902010-04-17 15:26:15 +00008690SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008691 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008693 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008694
Mon P Wangaf9b9522008-12-18 21:42:19 +00008695 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8696 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8697 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8698 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8699 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8700 //
8701 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8702 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8703 // return AloBlo + AloBhi + AhiBlo;
8704
8705 SDValue A = Op.getOperand(0);
8706 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008707
Dale Johannesene4d209d2009-02-03 20:21:25 +00008708 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008709 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8710 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008711 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008712 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8713 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008714 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008715 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008716 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008717 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008718 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008719 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008720 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008721 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008722 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008723 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008724 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8725 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008726 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008727 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8728 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008729 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8730 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008731 return Res;
8732}
8733
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008734SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8735 EVT VT = Op.getValueType();
8736 DebugLoc dl = Op.getDebugLoc();
8737 SDValue R = Op.getOperand(0);
8738
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008739 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008740
Nate Begeman51409212010-07-28 00:21:48 +00008741 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8742
8743 if (VT == MVT::v4i32) {
8744 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8745 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8746 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8747
8748 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008749
Nate Begeman51409212010-07-28 00:21:48 +00008750 std::vector<Constant*> CV(4, CI);
8751 Constant *C = ConstantVector::get(CV);
8752 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8753 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008754 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008755 false, false, 16);
8756
8757 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008758 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008759 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8760 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8761 }
8762 if (VT == MVT::v16i8) {
8763 // a = a << 5;
8764 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8765 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8766 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8767
8768 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8769 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8770
8771 std::vector<Constant*> CVM1(16, CM1);
8772 std::vector<Constant*> CVM2(16, CM2);
8773 Constant *C = ConstantVector::get(CVM1);
8774 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8775 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008776 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008777 false, false, 16);
8778
8779 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8780 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8781 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8782 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8783 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008784 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008785 // a += a
8786 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008787
Nate Begeman51409212010-07-28 00:21:48 +00008788 C = ConstantVector::get(CVM2);
8789 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8790 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008791 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008792 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008793
Nate Begeman51409212010-07-28 00:21:48 +00008794 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8795 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8796 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8797 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8798 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008799 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008800 // a += a
8801 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008802
Nate Begeman51409212010-07-28 00:21:48 +00008803 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008804 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008805 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8806 return R;
8807 }
8808 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008809}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008810
Dan Gohmand858e902010-04-17 15:26:15 +00008811SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008812 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8813 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008814 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8815 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008816 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008817 SDValue LHS = N->getOperand(0);
8818 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008819 unsigned BaseOp = 0;
8820 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008821 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008822 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008823 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008824 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008825 // A subtract of one will be selected as a INC. Note that INC doesn't
8826 // set CF, so we can't do this for UADDO.
8827 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8828 if (C->getAPIntValue() == 1) {
8829 BaseOp = X86ISD::INC;
8830 Cond = X86::COND_O;
8831 break;
8832 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008833 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008834 Cond = X86::COND_O;
8835 break;
8836 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008837 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008838 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008839 break;
8840 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008841 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8842 // set CF, so we can't do this for USUBO.
8843 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8844 if (C->getAPIntValue() == 1) {
8845 BaseOp = X86ISD::DEC;
8846 Cond = X86::COND_O;
8847 break;
8848 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008849 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008850 Cond = X86::COND_O;
8851 break;
8852 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008853 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008854 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008855 break;
8856 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008857 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008858 Cond = X86::COND_O;
8859 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008860 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8861 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8862 MVT::i32);
8863 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008864
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008865 SDValue SetCC =
8866 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8867 DAG.getConstant(X86::COND_O, MVT::i32),
8868 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008869
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008870 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8871 return Sum;
8872 }
Bill Wendling74c37652008-12-09 22:08:41 +00008873 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008874
Bill Wendling61edeb52008-12-02 01:06:39 +00008875 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008876 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008877 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008878
Bill Wendling61edeb52008-12-02 01:06:39 +00008879 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008880 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8881 DAG.getConstant(Cond, MVT::i32),
8882 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008883
Bill Wendling61edeb52008-12-02 01:06:39 +00008884 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8885 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008886}
8887
Eric Christopher9a9d2752010-07-22 02:48:34 +00008888SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8889 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008890
Eric Christopherb6729dc2010-08-04 23:03:04 +00008891 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008892 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008893 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008894 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008895 SDValue Ops[] = {
8896 DAG.getRegister(X86::ESP, MVT::i32), // Base
8897 DAG.getTargetConstant(1, MVT::i8), // Scale
8898 DAG.getRegister(0, MVT::i32), // Index
8899 DAG.getTargetConstant(0, MVT::i32), // Disp
8900 DAG.getRegister(0, MVT::i32), // Segment.
8901 Zero,
8902 Chain
8903 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008904 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008905 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8906 array_lengthof(Ops));
8907 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008908 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008909
Eric Christopher9a9d2752010-07-22 02:48:34 +00008910 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008911 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008912 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008913
Chris Lattner132929a2010-08-14 17:26:09 +00008914 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8915 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8916 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8917 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008918
Chris Lattner132929a2010-08-14 17:26:09 +00008919 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8920 if (!Op1 && !Op2 && !Op3 && Op4)
8921 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008922
Chris Lattner132929a2010-08-14 17:26:09 +00008923 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8924 if (Op1 && !Op2 && !Op3 && !Op4)
8925 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008926
8927 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008928 // (MFENCE)>;
8929 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008930}
8931
Dan Gohmand858e902010-04-17 15:26:15 +00008932SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008933 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008934 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008935 unsigned Reg = 0;
8936 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008937 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008938 default:
8939 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008940 case MVT::i8: Reg = X86::AL; size = 1; break;
8941 case MVT::i16: Reg = X86::AX; size = 2; break;
8942 case MVT::i32: Reg = X86::EAX; size = 4; break;
8943 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008944 assert(Subtarget->is64Bit() && "Node not type legal!");
8945 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008946 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008947 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008948 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008949 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008950 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008951 Op.getOperand(1),
8952 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008953 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008954 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008955 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008956 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8957 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8958 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008959 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008960 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008961 return cpOut;
8962}
8963
Duncan Sands1607f052008-12-01 11:39:25 +00008964SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008965 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008966 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008967 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008968 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008969 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008970 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8972 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008973 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008974 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8975 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008976 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008977 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008978 rdx.getValue(1)
8979 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008980 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008981}
8982
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008983SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008984 SelectionDAG &DAG) const {
8985 EVT SrcVT = Op.getOperand(0).getValueType();
8986 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008987 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8988 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008989 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008990 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008991 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008992 // i64 <=> MMX conversions are Legal.
8993 if (SrcVT==MVT::i64 && DstVT.isVector())
8994 return Op;
8995 if (DstVT==MVT::i64 && SrcVT.isVector())
8996 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008997 // MMX <=> MMX conversions are Legal.
8998 if (SrcVT.isVector() && DstVT.isVector())
8999 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009000 // All other conversions need to be expanded.
9001 return SDValue();
9002}
Chris Lattner5b856542010-12-20 00:59:46 +00009003
Dan Gohmand858e902010-04-17 15:26:15 +00009004SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009005 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009006 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009007 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009008 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009009 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009010 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009011 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009012 Node->getOperand(0),
9013 Node->getOperand(1), negOp,
9014 cast<AtomicSDNode>(Node)->getSrcValue(),
9015 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009016}
9017
Chris Lattner5b856542010-12-20 00:59:46 +00009018static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9019 EVT VT = Op.getNode()->getValueType(0);
9020
9021 // Let legalize expand this if it isn't a legal type yet.
9022 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9023 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009024
Chris Lattner5b856542010-12-20 00:59:46 +00009025 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009026
Chris Lattner5b856542010-12-20 00:59:46 +00009027 unsigned Opc;
9028 bool ExtraOp = false;
9029 switch (Op.getOpcode()) {
9030 default: assert(0 && "Invalid code");
9031 case ISD::ADDC: Opc = X86ISD::ADD; break;
9032 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9033 case ISD::SUBC: Opc = X86ISD::SUB; break;
9034 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9035 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009036
Chris Lattner5b856542010-12-20 00:59:46 +00009037 if (!ExtraOp)
9038 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9039 Op.getOperand(1));
9040 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9041 Op.getOperand(1), Op.getOperand(2));
9042}
9043
Evan Cheng0db9fe62006-04-25 20:13:52 +00009044/// LowerOperation - Provide custom lowering hooks for some operations.
9045///
Dan Gohmand858e902010-04-17 15:26:15 +00009046SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009047 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009048 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009049 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009050 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9051 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009052 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009053 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009054 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9055 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9056 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009057 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009058 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009059 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9060 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9061 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009062 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009063 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009064 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009065 case ISD::SHL_PARTS:
9066 case ISD::SRA_PARTS:
9067 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9068 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009069 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009070 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009071 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009072 case ISD::FABS: return LowerFABS(Op, DAG);
9073 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009074 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009075 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009076 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009077 case ISD::SELECT: return LowerSELECT(Op, DAG);
9078 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009079 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009080 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009081 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009082 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009083 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009084 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9085 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009086 case ISD::FRAME_TO_ARGS_OFFSET:
9087 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009088 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009089 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009090 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009091 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009092 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9093 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009094 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009095 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009096 case ISD::SADDO:
9097 case ISD::UADDO:
9098 case ISD::SSUBO:
9099 case ISD::USUBO:
9100 case ISD::SMULO:
9101 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009102 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009103 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009104 case ISD::ADDC:
9105 case ISD::ADDE:
9106 case ISD::SUBC:
9107 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009108 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009109}
9110
Duncan Sands1607f052008-12-01 11:39:25 +00009111void X86TargetLowering::
9112ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009113 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009114 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009115 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009116 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009117
9118 SDValue Chain = Node->getOperand(0);
9119 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009120 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009121 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009122 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009123 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009124 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009125 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009126 SDValue Result =
9127 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9128 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009129 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009130 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009131 Results.push_back(Result.getValue(2));
9132}
9133
Duncan Sands126d9072008-07-04 11:47:58 +00009134/// ReplaceNodeResults - Replace a node with an illegal result type
9135/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009136void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9137 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009138 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009139 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009140 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009141 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009142 assert(false && "Do not know how to custom type legalize this operation!");
9143 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009144 case ISD::ADDC:
9145 case ISD::ADDE:
9146 case ISD::SUBC:
9147 case ISD::SUBE:
9148 // We don't want to expand or promote these.
9149 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009150 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009151 std::pair<SDValue,SDValue> Vals =
9152 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009153 SDValue FIST = Vals.first, StackSlot = Vals.second;
9154 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009155 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009156 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009157 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9158 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009159 }
9160 return;
9161 }
9162 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009163 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009164 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009165 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009166 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009167 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009168 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009169 eax.getValue(2));
9170 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9171 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009173 Results.push_back(edx.getValue(1));
9174 return;
9175 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009176 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009177 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009178 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009179 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009180 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9181 DAG.getConstant(0, MVT::i32));
9182 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9183 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009184 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9185 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009186 cpInL.getValue(1));
9187 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009188 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9189 DAG.getConstant(0, MVT::i32));
9190 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9191 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009192 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009193 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009194 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009195 swapInL.getValue(1));
9196 SDValue Ops[] = { swapInH.getValue(0),
9197 N->getOperand(1),
9198 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009199 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009200 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9201 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9202 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009203 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009204 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009205 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009206 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009207 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009208 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009209 Results.push_back(cpOutH.getValue(1));
9210 return;
9211 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009212 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009213 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9214 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009215 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009216 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9217 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009218 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009219 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9220 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009221 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009222 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9223 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009224 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009225 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9226 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009227 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009228 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9229 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009230 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009231 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9232 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009233 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009234}
9235
Evan Cheng72261582005-12-20 06:22:03 +00009236const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9237 switch (Opcode) {
9238 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009239 case X86ISD::BSF: return "X86ISD::BSF";
9240 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009241 case X86ISD::SHLD: return "X86ISD::SHLD";
9242 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009243 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009244 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009245 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009246 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009247 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009248 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009249 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9250 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9251 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009252 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009253 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009254 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009255 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009256 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009257 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009258 case X86ISD::COMI: return "X86ISD::COMI";
9259 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009260 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009261 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009262 case X86ISD::CMOV: return "X86ISD::CMOV";
9263 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009264 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009265 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9266 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009267 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009268 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009269 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009270 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009271 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009272 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9273 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009274 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009275 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009276 case X86ISD::PANDN: return "X86ISD::PANDN";
9277 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9278 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9279 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009280 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009281 case X86ISD::FMAX: return "X86ISD::FMAX";
9282 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009283 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9284 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009285 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009286 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009287 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009288 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009289 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009290 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9291 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009292 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9293 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9294 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9295 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9296 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9297 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009298 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9299 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009300 case X86ISD::VSHL: return "X86ISD::VSHL";
9301 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009302 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9303 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9304 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9305 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9306 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9307 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9308 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9309 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9310 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9311 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009312 case X86ISD::ADD: return "X86ISD::ADD";
9313 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009314 case X86ISD::ADC: return "X86ISD::ADC";
9315 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009316 case X86ISD::SMUL: return "X86ISD::SMUL";
9317 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009318 case X86ISD::INC: return "X86ISD::INC";
9319 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009320 case X86ISD::OR: return "X86ISD::OR";
9321 case X86ISD::XOR: return "X86ISD::XOR";
9322 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009323 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009324 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009325 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009326 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9327 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9328 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9329 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9330 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9331 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9332 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9333 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9334 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009335 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009336 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009337 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009338 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9339 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009340 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9341 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9342 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9343 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9344 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9345 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9346 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9347 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9348 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009349 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9350 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9351 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9352 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009353 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9354 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9355 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9356 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9357 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9358 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9359 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9360 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9361 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9362 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009363 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009364 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009365 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009366 }
9367}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009368
Chris Lattnerc9addb72007-03-30 23:15:24 +00009369// isLegalAddressingMode - Return true if the addressing mode represented
9370// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009371bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009372 const Type *Ty) const {
9373 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009374 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009375 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009376
Chris Lattnerc9addb72007-03-30 23:15:24 +00009377 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009378 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009379 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009380
Chris Lattnerc9addb72007-03-30 23:15:24 +00009381 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009382 unsigned GVFlags =
9383 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009384
Chris Lattnerdfed4132009-07-10 07:38:24 +00009385 // If a reference to this global requires an extra load, we can't fold it.
9386 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009387 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009388
Chris Lattnerdfed4132009-07-10 07:38:24 +00009389 // If BaseGV requires a register for the PIC base, we cannot also have a
9390 // BaseReg specified.
9391 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009392 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009393
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009394 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009395 if ((M != CodeModel::Small || R != Reloc::Static) &&
9396 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009397 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009398 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009399
Chris Lattnerc9addb72007-03-30 23:15:24 +00009400 switch (AM.Scale) {
9401 case 0:
9402 case 1:
9403 case 2:
9404 case 4:
9405 case 8:
9406 // These scales always work.
9407 break;
9408 case 3:
9409 case 5:
9410 case 9:
9411 // These scales are formed with basereg+scalereg. Only accept if there is
9412 // no basereg yet.
9413 if (AM.HasBaseReg)
9414 return false;
9415 break;
9416 default: // Other stuff never works.
9417 return false;
9418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009419
Chris Lattnerc9addb72007-03-30 23:15:24 +00009420 return true;
9421}
9422
9423
Evan Cheng2bd122c2007-10-26 01:56:11 +00009424bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009425 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009426 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009427 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9428 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009429 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009430 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009431 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009432}
9433
Owen Andersone50ed302009-08-10 22:56:29 +00009434bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009435 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009436 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009437 unsigned NumBits1 = VT1.getSizeInBits();
9438 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009439 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009440 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009441 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009442}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009443
Dan Gohman97121ba2009-04-08 00:15:30 +00009444bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009445 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009446 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009447}
9448
Owen Andersone50ed302009-08-10 22:56:29 +00009449bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009450 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009451 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009452}
9453
Owen Andersone50ed302009-08-10 22:56:29 +00009454bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009455 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009457}
9458
Evan Cheng60c07e12006-07-05 22:17:51 +00009459/// isShuffleMaskLegal - Targets can use this to indicate that they only
9460/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9461/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9462/// are assumed to be legal.
9463bool
Eric Christopherfd179292009-08-27 18:07:15 +00009464X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009465 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009466 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009467 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009468 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009469
Nate Begemana09008b2009-10-19 02:17:23 +00009470 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009471 return (VT.getVectorNumElements() == 2 ||
9472 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9473 isMOVLMask(M, VT) ||
9474 isSHUFPMask(M, VT) ||
9475 isPSHUFDMask(M, VT) ||
9476 isPSHUFHWMask(M, VT) ||
9477 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009478 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009479 isUNPCKLMask(M, VT) ||
9480 isUNPCKHMask(M, VT) ||
9481 isUNPCKL_v_undef_Mask(M, VT) ||
9482 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009483}
9484
Dan Gohman7d8143f2008-04-09 20:09:42 +00009485bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009486X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009487 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009488 unsigned NumElts = VT.getVectorNumElements();
9489 // FIXME: This collection of masks seems suspect.
9490 if (NumElts == 2)
9491 return true;
9492 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9493 return (isMOVLMask(Mask, VT) ||
9494 isCommutedMOVLMask(Mask, VT, true) ||
9495 isSHUFPMask(Mask, VT) ||
9496 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009497 }
9498 return false;
9499}
9500
9501//===----------------------------------------------------------------------===//
9502// X86 Scheduler Hooks
9503//===----------------------------------------------------------------------===//
9504
Mon P Wang63307c32008-05-05 19:05:59 +00009505// private utility function
9506MachineBasicBlock *
9507X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9508 MachineBasicBlock *MBB,
9509 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009510 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009511 unsigned LoadOpc,
9512 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009513 unsigned notOpc,
9514 unsigned EAXreg,
9515 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009516 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009517 // For the atomic bitwise operator, we generate
9518 // thisMBB:
9519 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009520 // ld t1 = [bitinstr.addr]
9521 // op t2 = t1, [bitinstr.val]
9522 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009523 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9524 // bz newMBB
9525 // fallthrough -->nextMBB
9526 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9527 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009528 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009529 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009530
Mon P Wang63307c32008-05-05 19:05:59 +00009531 /// First build the CFG
9532 MachineFunction *F = MBB->getParent();
9533 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009534 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9535 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9536 F->insert(MBBIter, newMBB);
9537 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009538
Dan Gohman14152b42010-07-06 20:24:04 +00009539 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9540 nextMBB->splice(nextMBB->begin(), thisMBB,
9541 llvm::next(MachineBasicBlock::iterator(bInstr)),
9542 thisMBB->end());
9543 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009544
Mon P Wang63307c32008-05-05 19:05:59 +00009545 // Update thisMBB to fall through to newMBB
9546 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009547
Mon P Wang63307c32008-05-05 19:05:59 +00009548 // newMBB jumps to itself and fall through to nextMBB
9549 newMBB->addSuccessor(nextMBB);
9550 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009551
Mon P Wang63307c32008-05-05 19:05:59 +00009552 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009553 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009554 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009555 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009556 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009557 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009558 int numArgs = bInstr->getNumOperands() - 1;
9559 for (int i=0; i < numArgs; ++i)
9560 argOpers[i] = &bInstr->getOperand(i+1);
9561
9562 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009563 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009564 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009565
Dale Johannesen140be2d2008-08-19 18:47:28 +00009566 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009567 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009568 for (int i=0; i <= lastAddrIndx; ++i)
9569 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009570
Dale Johannesen140be2d2008-08-19 18:47:28 +00009571 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009572 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009573 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009575 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009576 tt = t1;
9577
Dale Johannesen140be2d2008-08-19 18:47:28 +00009578 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009579 assert((argOpers[valArgIndx]->isReg() ||
9580 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009581 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009582 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009583 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009584 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009585 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009586 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009587 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009588
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009589 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009590 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009591
Dale Johannesene4d209d2009-02-03 20:21:25 +00009592 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009593 for (int i=0; i <= lastAddrIndx; ++i)
9594 (*MIB).addOperand(*argOpers[i]);
9595 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009596 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009597 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9598 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009599
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009600 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009601 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009602
Mon P Wang63307c32008-05-05 19:05:59 +00009603 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009604 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009605
Dan Gohman14152b42010-07-06 20:24:04 +00009606 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009607 return nextMBB;
9608}
9609
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009610// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009611MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009612X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9613 MachineBasicBlock *MBB,
9614 unsigned regOpcL,
9615 unsigned regOpcH,
9616 unsigned immOpcL,
9617 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009618 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009619 // For the atomic bitwise operator, we generate
9620 // thisMBB (instructions are in pairs, except cmpxchg8b)
9621 // ld t1,t2 = [bitinstr.addr]
9622 // newMBB:
9623 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9624 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009625 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009626 // mov ECX, EBX <- t5, t6
9627 // mov EAX, EDX <- t1, t2
9628 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9629 // mov t3, t4 <- EAX, EDX
9630 // bz newMBB
9631 // result in out1, out2
9632 // fallthrough -->nextMBB
9633
9634 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9635 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009636 const unsigned NotOpc = X86::NOT32r;
9637 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9638 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9639 MachineFunction::iterator MBBIter = MBB;
9640 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009641
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009642 /// First build the CFG
9643 MachineFunction *F = MBB->getParent();
9644 MachineBasicBlock *thisMBB = MBB;
9645 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9646 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9647 F->insert(MBBIter, newMBB);
9648 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009649
Dan Gohman14152b42010-07-06 20:24:04 +00009650 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9651 nextMBB->splice(nextMBB->begin(), thisMBB,
9652 llvm::next(MachineBasicBlock::iterator(bInstr)),
9653 thisMBB->end());
9654 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009655
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009656 // Update thisMBB to fall through to newMBB
9657 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009658
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009659 // newMBB jumps to itself and fall through to nextMBB
9660 newMBB->addSuccessor(nextMBB);
9661 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009662
Dale Johannesene4d209d2009-02-03 20:21:25 +00009663 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009664 // Insert instructions into newMBB based on incoming instruction
9665 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009666 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009667 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009668 MachineOperand& dest1Oper = bInstr->getOperand(0);
9669 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009670 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9671 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009672 argOpers[i] = &bInstr->getOperand(i+2);
9673
Dan Gohman71ea4e52010-05-14 21:01:44 +00009674 // We use some of the operands multiple times, so conservatively just
9675 // clear any kill flags that might be present.
9676 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9677 argOpers[i]->setIsKill(false);
9678 }
9679
Evan Chengad5b52f2010-01-08 19:14:57 +00009680 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009681 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009682
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009683 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009684 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009685 for (int i=0; i <= lastAddrIndx; ++i)
9686 (*MIB).addOperand(*argOpers[i]);
9687 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009688 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009689 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009690 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009691 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009692 MachineOperand newOp3 = *(argOpers[3]);
9693 if (newOp3.isImm())
9694 newOp3.setImm(newOp3.getImm()+4);
9695 else
9696 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009697 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009698 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009699
9700 // t3/4 are defined later, at the bottom of the loop
9701 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9702 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009703 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009704 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009705 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009706 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9707
Evan Cheng306b4ca2010-01-08 23:41:50 +00009708 // The subsequent operations should be using the destination registers of
9709 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009710 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009711 t1 = F->getRegInfo().createVirtualRegister(RC);
9712 t2 = F->getRegInfo().createVirtualRegister(RC);
9713 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9714 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009715 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009716 t1 = dest1Oper.getReg();
9717 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009718 }
9719
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009720 int valArgIndx = lastAddrIndx + 1;
9721 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009722 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009723 "invalid operand");
9724 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9725 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009726 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009727 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009728 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009729 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009730 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009731 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009732 (*MIB).addOperand(*argOpers[valArgIndx]);
9733 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009734 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009735 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009736 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009737 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009738 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009739 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009740 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009741 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009742 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009743 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009744
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009745 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009746 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009747 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009748 MIB.addReg(t2);
9749
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009750 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009751 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009752 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009753 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009754
Dale Johannesene4d209d2009-02-03 20:21:25 +00009755 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009756 for (int i=0; i <= lastAddrIndx; ++i)
9757 (*MIB).addOperand(*argOpers[i]);
9758
9759 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009760 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9761 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009762
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009763 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009764 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009765 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009766 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009767
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009768 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009769 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009770
Dan Gohman14152b42010-07-06 20:24:04 +00009771 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009772 return nextMBB;
9773}
9774
9775// private utility function
9776MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009777X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9778 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009779 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009780 // For the atomic min/max operator, we generate
9781 // thisMBB:
9782 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009783 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009784 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009785 // cmp t1, t2
9786 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009787 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009788 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9789 // bz newMBB
9790 // fallthrough -->nextMBB
9791 //
9792 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9793 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009794 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009795 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009796
Mon P Wang63307c32008-05-05 19:05:59 +00009797 /// First build the CFG
9798 MachineFunction *F = MBB->getParent();
9799 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009800 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9801 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9802 F->insert(MBBIter, newMBB);
9803 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009804
Dan Gohman14152b42010-07-06 20:24:04 +00009805 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9806 nextMBB->splice(nextMBB->begin(), thisMBB,
9807 llvm::next(MachineBasicBlock::iterator(mInstr)),
9808 thisMBB->end());
9809 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009810
Mon P Wang63307c32008-05-05 19:05:59 +00009811 // Update thisMBB to fall through to newMBB
9812 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009813
Mon P Wang63307c32008-05-05 19:05:59 +00009814 // newMBB jumps to newMBB and fall through to nextMBB
9815 newMBB->addSuccessor(nextMBB);
9816 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009817
Dale Johannesene4d209d2009-02-03 20:21:25 +00009818 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009819 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009820 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009821 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009822 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009823 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009824 int numArgs = mInstr->getNumOperands() - 1;
9825 for (int i=0; i < numArgs; ++i)
9826 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009827
Mon P Wang63307c32008-05-05 19:05:59 +00009828 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009829 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009830 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009831
Mon P Wangab3e7472008-05-05 22:56:23 +00009832 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009833 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009834 for (int i=0; i <= lastAddrIndx; ++i)
9835 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009836
Mon P Wang63307c32008-05-05 19:05:59 +00009837 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009838 assert((argOpers[valArgIndx]->isReg() ||
9839 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009840 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009841
9842 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009843 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009844 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009845 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009846 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009847 (*MIB).addOperand(*argOpers[valArgIndx]);
9848
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009849 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009850 MIB.addReg(t1);
9851
Dale Johannesene4d209d2009-02-03 20:21:25 +00009852 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009853 MIB.addReg(t1);
9854 MIB.addReg(t2);
9855
9856 // Generate movc
9857 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009858 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009859 MIB.addReg(t2);
9860 MIB.addReg(t1);
9861
9862 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009863 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009864 for (int i=0; i <= lastAddrIndx; ++i)
9865 (*MIB).addOperand(*argOpers[i]);
9866 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009867 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009868 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9869 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009870
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009871 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009872 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009873
Mon P Wang63307c32008-05-05 19:05:59 +00009874 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009875 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009876
Dan Gohman14152b42010-07-06 20:24:04 +00009877 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009878 return nextMBB;
9879}
9880
Eric Christopherf83a5de2009-08-27 18:08:16 +00009881// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009882// or XMM0_V32I8 in AVX all of this code can be replaced with that
9883// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009884MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009885X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009886 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009887 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9888 "Target must have SSE4.2 or AVX features enabled");
9889
Eric Christopherb120ab42009-08-18 22:50:32 +00009890 DebugLoc dl = MI->getDebugLoc();
9891 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009892 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009893 if (!Subtarget->hasAVX()) {
9894 if (memArg)
9895 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9896 else
9897 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9898 } else {
9899 if (memArg)
9900 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9901 else
9902 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9903 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009904
Eric Christopher41c902f2010-11-30 08:20:21 +00009905 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009906 for (unsigned i = 0; i < numArgs; ++i) {
9907 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009908 if (!(Op.isReg() && Op.isImplicit()))
9909 MIB.addOperand(Op);
9910 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009911 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009912 .addReg(X86::XMM0);
9913
Dan Gohman14152b42010-07-06 20:24:04 +00009914 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009915 return BB;
9916}
9917
9918MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009919X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009920 DebugLoc dl = MI->getDebugLoc();
9921 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009922
Eric Christopher228232b2010-11-30 07:20:12 +00009923 // Address into RAX/EAX, other two args into ECX, EDX.
9924 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9925 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9926 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9927 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009928 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009929
Eric Christopher228232b2010-11-30 07:20:12 +00009930 unsigned ValOps = X86::AddrNumOperands;
9931 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9932 .addReg(MI->getOperand(ValOps).getReg());
9933 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9934 .addReg(MI->getOperand(ValOps+1).getReg());
9935
9936 // The instruction doesn't actually take any operands though.
9937 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009938
Eric Christopher228232b2010-11-30 07:20:12 +00009939 MI->eraseFromParent(); // The pseudo is gone now.
9940 return BB;
9941}
9942
9943MachineBasicBlock *
9944X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009945 DebugLoc dl = MI->getDebugLoc();
9946 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009947
Eric Christopher228232b2010-11-30 07:20:12 +00009948 // First arg in ECX, the second in EAX.
9949 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9950 .addReg(MI->getOperand(0).getReg());
9951 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9952 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009953
Eric Christopher228232b2010-11-30 07:20:12 +00009954 // The instruction doesn't actually take any operands though.
9955 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009956
Eric Christopher228232b2010-11-30 07:20:12 +00009957 MI->eraseFromParent(); // The pseudo is gone now.
9958 return BB;
9959}
9960
9961MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009962X86TargetLowering::EmitVAARG64WithCustomInserter(
9963 MachineInstr *MI,
9964 MachineBasicBlock *MBB) const {
9965 // Emit va_arg instruction on X86-64.
9966
9967 // Operands to this pseudo-instruction:
9968 // 0 ) Output : destination address (reg)
9969 // 1-5) Input : va_list address (addr, i64mem)
9970 // 6 ) ArgSize : Size (in bytes) of vararg type
9971 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9972 // 8 ) Align : Alignment of type
9973 // 9 ) EFLAGS (implicit-def)
9974
9975 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9976 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9977
9978 unsigned DestReg = MI->getOperand(0).getReg();
9979 MachineOperand &Base = MI->getOperand(1);
9980 MachineOperand &Scale = MI->getOperand(2);
9981 MachineOperand &Index = MI->getOperand(3);
9982 MachineOperand &Disp = MI->getOperand(4);
9983 MachineOperand &Segment = MI->getOperand(5);
9984 unsigned ArgSize = MI->getOperand(6).getImm();
9985 unsigned ArgMode = MI->getOperand(7).getImm();
9986 unsigned Align = MI->getOperand(8).getImm();
9987
9988 // Memory Reference
9989 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9990 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9991 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9992
9993 // Machine Information
9994 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9995 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9996 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9997 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9998 DebugLoc DL = MI->getDebugLoc();
9999
10000 // struct va_list {
10001 // i32 gp_offset
10002 // i32 fp_offset
10003 // i64 overflow_area (address)
10004 // i64 reg_save_area (address)
10005 // }
10006 // sizeof(va_list) = 24
10007 // alignment(va_list) = 8
10008
10009 unsigned TotalNumIntRegs = 6;
10010 unsigned TotalNumXMMRegs = 8;
10011 bool UseGPOffset = (ArgMode == 1);
10012 bool UseFPOffset = (ArgMode == 2);
10013 unsigned MaxOffset = TotalNumIntRegs * 8 +
10014 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10015
10016 /* Align ArgSize to a multiple of 8 */
10017 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10018 bool NeedsAlign = (Align > 8);
10019
10020 MachineBasicBlock *thisMBB = MBB;
10021 MachineBasicBlock *overflowMBB;
10022 MachineBasicBlock *offsetMBB;
10023 MachineBasicBlock *endMBB;
10024
10025 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10026 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10027 unsigned OffsetReg = 0;
10028
10029 if (!UseGPOffset && !UseFPOffset) {
10030 // If we only pull from the overflow region, we don't create a branch.
10031 // We don't need to alter control flow.
10032 OffsetDestReg = 0; // unused
10033 OverflowDestReg = DestReg;
10034
10035 offsetMBB = NULL;
10036 overflowMBB = thisMBB;
10037 endMBB = thisMBB;
10038 } else {
10039 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10040 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10041 // If not, pull from overflow_area. (branch to overflowMBB)
10042 //
10043 // thisMBB
10044 // | .
10045 // | .
10046 // offsetMBB overflowMBB
10047 // | .
10048 // | .
10049 // endMBB
10050
10051 // Registers for the PHI in endMBB
10052 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10053 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10054
10055 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10056 MachineFunction *MF = MBB->getParent();
10057 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10058 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10059 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10060
10061 MachineFunction::iterator MBBIter = MBB;
10062 ++MBBIter;
10063
10064 // Insert the new basic blocks
10065 MF->insert(MBBIter, offsetMBB);
10066 MF->insert(MBBIter, overflowMBB);
10067 MF->insert(MBBIter, endMBB);
10068
10069 // Transfer the remainder of MBB and its successor edges to endMBB.
10070 endMBB->splice(endMBB->begin(), thisMBB,
10071 llvm::next(MachineBasicBlock::iterator(MI)),
10072 thisMBB->end());
10073 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10074
10075 // Make offsetMBB and overflowMBB successors of thisMBB
10076 thisMBB->addSuccessor(offsetMBB);
10077 thisMBB->addSuccessor(overflowMBB);
10078
10079 // endMBB is a successor of both offsetMBB and overflowMBB
10080 offsetMBB->addSuccessor(endMBB);
10081 overflowMBB->addSuccessor(endMBB);
10082
10083 // Load the offset value into a register
10084 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10085 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10086 .addOperand(Base)
10087 .addOperand(Scale)
10088 .addOperand(Index)
10089 .addDisp(Disp, UseFPOffset ? 4 : 0)
10090 .addOperand(Segment)
10091 .setMemRefs(MMOBegin, MMOEnd);
10092
10093 // Check if there is enough room left to pull this argument.
10094 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10095 .addReg(OffsetReg)
10096 .addImm(MaxOffset + 8 - ArgSizeA8);
10097
10098 // Branch to "overflowMBB" if offset >= max
10099 // Fall through to "offsetMBB" otherwise
10100 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10101 .addMBB(overflowMBB);
10102 }
10103
10104 // In offsetMBB, emit code to use the reg_save_area.
10105 if (offsetMBB) {
10106 assert(OffsetReg != 0);
10107
10108 // Read the reg_save_area address.
10109 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10110 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10111 .addOperand(Base)
10112 .addOperand(Scale)
10113 .addOperand(Index)
10114 .addDisp(Disp, 16)
10115 .addOperand(Segment)
10116 .setMemRefs(MMOBegin, MMOEnd);
10117
10118 // Zero-extend the offset
10119 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10120 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10121 .addImm(0)
10122 .addReg(OffsetReg)
10123 .addImm(X86::sub_32bit);
10124
10125 // Add the offset to the reg_save_area to get the final address.
10126 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10127 .addReg(OffsetReg64)
10128 .addReg(RegSaveReg);
10129
10130 // Compute the offset for the next argument
10131 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10132 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10133 .addReg(OffsetReg)
10134 .addImm(UseFPOffset ? 16 : 8);
10135
10136 // Store it back into the va_list.
10137 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10138 .addOperand(Base)
10139 .addOperand(Scale)
10140 .addOperand(Index)
10141 .addDisp(Disp, UseFPOffset ? 4 : 0)
10142 .addOperand(Segment)
10143 .addReg(NextOffsetReg)
10144 .setMemRefs(MMOBegin, MMOEnd);
10145
10146 // Jump to endMBB
10147 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10148 .addMBB(endMBB);
10149 }
10150
10151 //
10152 // Emit code to use overflow area
10153 //
10154
10155 // Load the overflow_area address into a register.
10156 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10157 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10158 .addOperand(Base)
10159 .addOperand(Scale)
10160 .addOperand(Index)
10161 .addDisp(Disp, 8)
10162 .addOperand(Segment)
10163 .setMemRefs(MMOBegin, MMOEnd);
10164
10165 // If we need to align it, do so. Otherwise, just copy the address
10166 // to OverflowDestReg.
10167 if (NeedsAlign) {
10168 // Align the overflow address
10169 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10170 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10171
10172 // aligned_addr = (addr + (align-1)) & ~(align-1)
10173 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10174 .addReg(OverflowAddrReg)
10175 .addImm(Align-1);
10176
10177 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10178 .addReg(TmpReg)
10179 .addImm(~(uint64_t)(Align-1));
10180 } else {
10181 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10182 .addReg(OverflowAddrReg);
10183 }
10184
10185 // Compute the next overflow address after this argument.
10186 // (the overflow address should be kept 8-byte aligned)
10187 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10188 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10189 .addReg(OverflowDestReg)
10190 .addImm(ArgSizeA8);
10191
10192 // Store the new overflow address.
10193 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10194 .addOperand(Base)
10195 .addOperand(Scale)
10196 .addOperand(Index)
10197 .addDisp(Disp, 8)
10198 .addOperand(Segment)
10199 .addReg(NextAddrReg)
10200 .setMemRefs(MMOBegin, MMOEnd);
10201
10202 // If we branched, emit the PHI to the front of endMBB.
10203 if (offsetMBB) {
10204 BuildMI(*endMBB, endMBB->begin(), DL,
10205 TII->get(X86::PHI), DestReg)
10206 .addReg(OffsetDestReg).addMBB(offsetMBB)
10207 .addReg(OverflowDestReg).addMBB(overflowMBB);
10208 }
10209
10210 // Erase the pseudo instruction
10211 MI->eraseFromParent();
10212
10213 return endMBB;
10214}
10215
10216MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010217X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10218 MachineInstr *MI,
10219 MachineBasicBlock *MBB) const {
10220 // Emit code to save XMM registers to the stack. The ABI says that the
10221 // number of registers to save is given in %al, so it's theoretically
10222 // possible to do an indirect jump trick to avoid saving all of them,
10223 // however this code takes a simpler approach and just executes all
10224 // of the stores if %al is non-zero. It's less code, and it's probably
10225 // easier on the hardware branch predictor, and stores aren't all that
10226 // expensive anyway.
10227
10228 // Create the new basic blocks. One block contains all the XMM stores,
10229 // and one block is the final destination regardless of whether any
10230 // stores were performed.
10231 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10232 MachineFunction *F = MBB->getParent();
10233 MachineFunction::iterator MBBIter = MBB;
10234 ++MBBIter;
10235 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10236 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10237 F->insert(MBBIter, XMMSaveMBB);
10238 F->insert(MBBIter, EndMBB);
10239
Dan Gohman14152b42010-07-06 20:24:04 +000010240 // Transfer the remainder of MBB and its successor edges to EndMBB.
10241 EndMBB->splice(EndMBB->begin(), MBB,
10242 llvm::next(MachineBasicBlock::iterator(MI)),
10243 MBB->end());
10244 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10245
Dan Gohmand6708ea2009-08-15 01:38:56 +000010246 // The original block will now fall through to the XMM save block.
10247 MBB->addSuccessor(XMMSaveMBB);
10248 // The XMMSaveMBB will fall through to the end block.
10249 XMMSaveMBB->addSuccessor(EndMBB);
10250
10251 // Now add the instructions.
10252 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10253 DebugLoc DL = MI->getDebugLoc();
10254
10255 unsigned CountReg = MI->getOperand(0).getReg();
10256 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10257 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10258
10259 if (!Subtarget->isTargetWin64()) {
10260 // If %al is 0, branch around the XMM save block.
10261 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010262 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010263 MBB->addSuccessor(EndMBB);
10264 }
10265
10266 // In the XMM save block, save all the XMM argument registers.
10267 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10268 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010269 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010270 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010271 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010272 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010273 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010274 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10275 .addFrameIndex(RegSaveFrameIndex)
10276 .addImm(/*Scale=*/1)
10277 .addReg(/*IndexReg=*/0)
10278 .addImm(/*Disp=*/Offset)
10279 .addReg(/*Segment=*/0)
10280 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010281 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010282 }
10283
Dan Gohman14152b42010-07-06 20:24:04 +000010284 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010285
10286 return EndMBB;
10287}
Mon P Wang63307c32008-05-05 19:05:59 +000010288
Evan Cheng60c07e12006-07-05 22:17:51 +000010289MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010290X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010291 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010292 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10293 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010294
Chris Lattner52600972009-09-02 05:57:00 +000010295 // To "insert" a SELECT_CC instruction, we actually have to insert the
10296 // diamond control-flow pattern. The incoming instruction knows the
10297 // destination vreg to set, the condition code register to branch on, the
10298 // true/false values to select between, and a branch opcode to use.
10299 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10300 MachineFunction::iterator It = BB;
10301 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010302
Chris Lattner52600972009-09-02 05:57:00 +000010303 // thisMBB:
10304 // ...
10305 // TrueVal = ...
10306 // cmpTY ccX, r1, r2
10307 // bCC copy1MBB
10308 // fallthrough --> copy0MBB
10309 MachineBasicBlock *thisMBB = BB;
10310 MachineFunction *F = BB->getParent();
10311 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10312 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010313 F->insert(It, copy0MBB);
10314 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010315
Bill Wendling730c07e2010-06-25 20:48:10 +000010316 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10317 // live into the sink and copy blocks.
10318 const MachineFunction *MF = BB->getParent();
10319 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10320 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010321
Dan Gohman14152b42010-07-06 20:24:04 +000010322 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10323 const MachineOperand &MO = MI->getOperand(I);
10324 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010325 unsigned Reg = MO.getReg();
10326 if (Reg != X86::EFLAGS) continue;
10327 copy0MBB->addLiveIn(Reg);
10328 sinkMBB->addLiveIn(Reg);
10329 }
10330
Dan Gohman14152b42010-07-06 20:24:04 +000010331 // Transfer the remainder of BB and its successor edges to sinkMBB.
10332 sinkMBB->splice(sinkMBB->begin(), BB,
10333 llvm::next(MachineBasicBlock::iterator(MI)),
10334 BB->end());
10335 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10336
10337 // Add the true and fallthrough blocks as its successors.
10338 BB->addSuccessor(copy0MBB);
10339 BB->addSuccessor(sinkMBB);
10340
10341 // Create the conditional branch instruction.
10342 unsigned Opc =
10343 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10344 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10345
Chris Lattner52600972009-09-02 05:57:00 +000010346 // copy0MBB:
10347 // %FalseValue = ...
10348 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010349 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010350
Chris Lattner52600972009-09-02 05:57:00 +000010351 // sinkMBB:
10352 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10353 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010354 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10355 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010356 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10357 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10358
Dan Gohman14152b42010-07-06 20:24:04 +000010359 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010360 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010361}
10362
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010363MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010364X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010365 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010366 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10367 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010368
10369 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10370 // non-trivial part is impdef of ESP.
10371 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10372 // mingw-w64.
10373
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010374 const char *StackProbeSymbol =
10375 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10376
Dan Gohman14152b42010-07-06 20:24:04 +000010377 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010378 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010379 .addReg(X86::EAX, RegState::Implicit)
10380 .addReg(X86::ESP, RegState::Implicit)
10381 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010382 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10383 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010384
Dan Gohman14152b42010-07-06 20:24:04 +000010385 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010386 return BB;
10387}
Chris Lattner52600972009-09-02 05:57:00 +000010388
10389MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010390X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10391 MachineBasicBlock *BB) const {
10392 // This is pretty easy. We're taking the value that we received from
10393 // our load from the relocation, sticking it in either RDI (x86-64)
10394 // or EAX and doing an indirect call. The return value will then
10395 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010396 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010397 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010398 DebugLoc DL = MI->getDebugLoc();
10399 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010400
10401 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010402 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010403
Eric Christopher30ef0e52010-06-03 04:07:48 +000010404 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010405 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10406 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010407 .addReg(X86::RIP)
10408 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010409 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010410 MI->getOperand(3).getTargetFlags())
10411 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010412 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010413 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010414 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010415 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10416 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010417 .addReg(0)
10418 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010419 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010420 MI->getOperand(3).getTargetFlags())
10421 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010422 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010423 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010424 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010425 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10426 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010427 .addReg(TII->getGlobalBaseReg(F))
10428 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010429 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +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 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010435
Dan Gohman14152b42010-07-06 20:24:04 +000010436 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010437 return BB;
10438}
10439
10440MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010441X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010442 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010443 switch (MI->getOpcode()) {
10444 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010445 case X86::TAILJMPd64:
10446 case X86::TAILJMPr64:
10447 case X86::TAILJMPm64:
10448 assert(!"TAILJMP64 would not be touched here.");
10449 case X86::TCRETURNdi64:
10450 case X86::TCRETURNri64:
10451 case X86::TCRETURNmi64:
10452 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10453 // On AMD64, additional defs should be added before register allocation.
10454 if (!Subtarget->isTargetWin64()) {
10455 MI->addRegisterDefined(X86::RSI);
10456 MI->addRegisterDefined(X86::RDI);
10457 MI->addRegisterDefined(X86::XMM6);
10458 MI->addRegisterDefined(X86::XMM7);
10459 MI->addRegisterDefined(X86::XMM8);
10460 MI->addRegisterDefined(X86::XMM9);
10461 MI->addRegisterDefined(X86::XMM10);
10462 MI->addRegisterDefined(X86::XMM11);
10463 MI->addRegisterDefined(X86::XMM12);
10464 MI->addRegisterDefined(X86::XMM13);
10465 MI->addRegisterDefined(X86::XMM14);
10466 MI->addRegisterDefined(X86::XMM15);
10467 }
10468 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010469 case X86::WIN_ALLOCA:
10470 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010471 case X86::TLSCall_32:
10472 case X86::TLSCall_64:
10473 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010474 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010475 case X86::CMOV_FR32:
10476 case X86::CMOV_FR64:
10477 case X86::CMOV_V4F32:
10478 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010479 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010480 case X86::CMOV_GR16:
10481 case X86::CMOV_GR32:
10482 case X86::CMOV_RFP32:
10483 case X86::CMOV_RFP64:
10484 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010485 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010486
Dale Johannesen849f2142007-07-03 00:53:03 +000010487 case X86::FP32_TO_INT16_IN_MEM:
10488 case X86::FP32_TO_INT32_IN_MEM:
10489 case X86::FP32_TO_INT64_IN_MEM:
10490 case X86::FP64_TO_INT16_IN_MEM:
10491 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010492 case X86::FP64_TO_INT64_IN_MEM:
10493 case X86::FP80_TO_INT16_IN_MEM:
10494 case X86::FP80_TO_INT32_IN_MEM:
10495 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010496 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10497 DebugLoc DL = MI->getDebugLoc();
10498
Evan Cheng60c07e12006-07-05 22:17:51 +000010499 // Change the floating point control register to use "round towards zero"
10500 // mode when truncating to an integer value.
10501 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010502 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010503 addFrameReference(BuildMI(*BB, MI, DL,
10504 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010505
10506 // Load the old value of the high byte of the control word...
10507 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010508 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010509 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010510 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010511
10512 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010513 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010514 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010515
10516 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010517 addFrameReference(BuildMI(*BB, MI, DL,
10518 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010519
10520 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010521 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010522 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010523
10524 // Get the X86 opcode to use.
10525 unsigned Opc;
10526 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010527 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010528 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10529 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10530 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10531 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10532 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10533 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010534 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10535 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10536 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010537 }
10538
10539 X86AddressMode AM;
10540 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010541 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010542 AM.BaseType = X86AddressMode::RegBase;
10543 AM.Base.Reg = Op.getReg();
10544 } else {
10545 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010546 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010547 }
10548 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010549 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010550 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010551 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010552 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010553 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010554 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010555 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010556 AM.GV = Op.getGlobal();
10557 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010558 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010559 }
Dan Gohman14152b42010-07-06 20:24:04 +000010560 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010561 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010562
10563 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010564 addFrameReference(BuildMI(*BB, MI, DL,
10565 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010566
Dan Gohman14152b42010-07-06 20:24:04 +000010567 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010568 return BB;
10569 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010570 // String/text processing lowering.
10571 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010572 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010573 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10574 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010575 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010576 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10577 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010578 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010579 return EmitPCMP(MI, BB, 5, false /* in mem */);
10580 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010581 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010582 return EmitPCMP(MI, BB, 5, true /* in mem */);
10583
Eric Christopher228232b2010-11-30 07:20:12 +000010584 // Thread synchronization.
10585 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010586 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010587 case X86::MWAIT:
10588 return EmitMwait(MI, BB);
10589
Eric Christopherb120ab42009-08-18 22:50:32 +000010590 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010591 case X86::ATOMAND32:
10592 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010593 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010594 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010595 X86::NOT32r, X86::EAX,
10596 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010597 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010598 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10599 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010600 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010601 X86::NOT32r, X86::EAX,
10602 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010603 case X86::ATOMXOR32:
10604 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010605 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010606 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010607 X86::NOT32r, X86::EAX,
10608 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010609 case X86::ATOMNAND32:
10610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010611 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010612 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010613 X86::NOT32r, X86::EAX,
10614 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010615 case X86::ATOMMIN32:
10616 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10617 case X86::ATOMMAX32:
10618 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10619 case X86::ATOMUMIN32:
10620 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10621 case X86::ATOMUMAX32:
10622 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010623
10624 case X86::ATOMAND16:
10625 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10626 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010627 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010628 X86::NOT16r, X86::AX,
10629 X86::GR16RegisterClass);
10630 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010631 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010632 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010633 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010634 X86::NOT16r, X86::AX,
10635 X86::GR16RegisterClass);
10636 case X86::ATOMXOR16:
10637 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10638 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010639 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010640 X86::NOT16r, X86::AX,
10641 X86::GR16RegisterClass);
10642 case X86::ATOMNAND16:
10643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10644 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010645 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010646 X86::NOT16r, X86::AX,
10647 X86::GR16RegisterClass, true);
10648 case X86::ATOMMIN16:
10649 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10650 case X86::ATOMMAX16:
10651 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10652 case X86::ATOMUMIN16:
10653 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10654 case X86::ATOMUMAX16:
10655 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10656
10657 case X86::ATOMAND8:
10658 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10659 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010660 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010661 X86::NOT8r, X86::AL,
10662 X86::GR8RegisterClass);
10663 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010664 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010665 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010666 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010667 X86::NOT8r, X86::AL,
10668 X86::GR8RegisterClass);
10669 case X86::ATOMXOR8:
10670 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10671 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010672 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010673 X86::NOT8r, X86::AL,
10674 X86::GR8RegisterClass);
10675 case X86::ATOMNAND8:
10676 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10677 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010678 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010679 X86::NOT8r, X86::AL,
10680 X86::GR8RegisterClass, true);
10681 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010682 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010683 case X86::ATOMAND64:
10684 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010685 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010686 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010687 X86::NOT64r, X86::RAX,
10688 X86::GR64RegisterClass);
10689 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010690 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10691 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010692 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010693 X86::NOT64r, X86::RAX,
10694 X86::GR64RegisterClass);
10695 case X86::ATOMXOR64:
10696 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010697 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010698 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010699 X86::NOT64r, X86::RAX,
10700 X86::GR64RegisterClass);
10701 case X86::ATOMNAND64:
10702 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10703 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010704 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010705 X86::NOT64r, X86::RAX,
10706 X86::GR64RegisterClass, true);
10707 case X86::ATOMMIN64:
10708 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10709 case X86::ATOMMAX64:
10710 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10711 case X86::ATOMUMIN64:
10712 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10713 case X86::ATOMUMAX64:
10714 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010715
10716 // This group does 64-bit operations on a 32-bit host.
10717 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010718 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010719 X86::AND32rr, X86::AND32rr,
10720 X86::AND32ri, X86::AND32ri,
10721 false);
10722 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010723 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010724 X86::OR32rr, X86::OR32rr,
10725 X86::OR32ri, X86::OR32ri,
10726 false);
10727 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010728 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010729 X86::XOR32rr, X86::XOR32rr,
10730 X86::XOR32ri, X86::XOR32ri,
10731 false);
10732 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010733 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010734 X86::AND32rr, X86::AND32rr,
10735 X86::AND32ri, X86::AND32ri,
10736 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010737 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010738 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010739 X86::ADD32rr, X86::ADC32rr,
10740 X86::ADD32ri, X86::ADC32ri,
10741 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010742 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010743 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010744 X86::SUB32rr, X86::SBB32rr,
10745 X86::SUB32ri, X86::SBB32ri,
10746 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010747 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010748 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010749 X86::MOV32rr, X86::MOV32rr,
10750 X86::MOV32ri, X86::MOV32ri,
10751 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010752 case X86::VASTART_SAVE_XMM_REGS:
10753 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010754
10755 case X86::VAARG_64:
10756 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010757 }
10758}
10759
10760//===----------------------------------------------------------------------===//
10761// X86 Optimization Hooks
10762//===----------------------------------------------------------------------===//
10763
Dan Gohman475871a2008-07-27 21:46:04 +000010764void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010765 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010766 APInt &KnownZero,
10767 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010768 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010769 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010770 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010771 assert((Opc >= ISD::BUILTIN_OP_END ||
10772 Opc == ISD::INTRINSIC_WO_CHAIN ||
10773 Opc == ISD::INTRINSIC_W_CHAIN ||
10774 Opc == ISD::INTRINSIC_VOID) &&
10775 "Should use MaskedValueIsZero if you don't know whether Op"
10776 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010777
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010778 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010779 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010780 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010781 case X86ISD::ADD:
10782 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010783 case X86ISD::ADC:
10784 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010785 case X86ISD::SMUL:
10786 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010787 case X86ISD::INC:
10788 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010789 case X86ISD::OR:
10790 case X86ISD::XOR:
10791 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010792 // These nodes' second result is a boolean.
10793 if (Op.getResNo() == 0)
10794 break;
10795 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010796 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010797 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10798 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010799 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010800 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010801}
Chris Lattner259e97c2006-01-31 19:43:35 +000010802
Owen Andersonbc146b02010-09-21 20:42:50 +000010803unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10804 unsigned Depth) const {
10805 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10806 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10807 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010808
Owen Andersonbc146b02010-09-21 20:42:50 +000010809 // Fallback case.
10810 return 1;
10811}
10812
Evan Cheng206ee9d2006-07-07 08:33:52 +000010813/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010814/// node is a GlobalAddress + offset.
10815bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010816 const GlobalValue* &GA,
10817 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010818 if (N->getOpcode() == X86ISD::Wrapper) {
10819 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010820 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010821 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010822 return true;
10823 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010824 }
Evan Chengad4196b2008-05-12 19:56:52 +000010825 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010826}
10827
Evan Cheng206ee9d2006-07-07 08:33:52 +000010828/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10829/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10830/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010831/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010832static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010833 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010834 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010835 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010836
Eli Friedman7a5e5552009-06-07 06:52:44 +000010837 if (VT.getSizeInBits() != 128)
10838 return SDValue();
10839
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010840 // Don't create instructions with illegal types after legalize types has run.
10841 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10842 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10843 return SDValue();
10844
Nate Begemanfdea31a2010-03-24 20:49:50 +000010845 SmallVector<SDValue, 16> Elts;
10846 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010847 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010848
Nate Begemanfdea31a2010-03-24 20:49:50 +000010849 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010850}
Evan Chengd880b972008-05-09 21:53:03 +000010851
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010852/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10853/// generation and convert it from being a bunch of shuffles and extracts
10854/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010855static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10856 const TargetLowering &TLI) {
10857 SDValue InputVector = N->getOperand(0);
10858
10859 // Only operate on vectors of 4 elements, where the alternative shuffling
10860 // gets to be more expensive.
10861 if (InputVector.getValueType() != MVT::v4i32)
10862 return SDValue();
10863
10864 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10865 // single use which is a sign-extend or zero-extend, and all elements are
10866 // used.
10867 SmallVector<SDNode *, 4> Uses;
10868 unsigned ExtractedElements = 0;
10869 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10870 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10871 if (UI.getUse().getResNo() != InputVector.getResNo())
10872 return SDValue();
10873
10874 SDNode *Extract = *UI;
10875 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10876 return SDValue();
10877
10878 if (Extract->getValueType(0) != MVT::i32)
10879 return SDValue();
10880 if (!Extract->hasOneUse())
10881 return SDValue();
10882 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10883 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10884 return SDValue();
10885 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10886 return SDValue();
10887
10888 // Record which element was extracted.
10889 ExtractedElements |=
10890 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10891
10892 Uses.push_back(Extract);
10893 }
10894
10895 // If not all the elements were used, this may not be worthwhile.
10896 if (ExtractedElements != 15)
10897 return SDValue();
10898
10899 // Ok, we've now decided to do the transformation.
10900 DebugLoc dl = InputVector.getDebugLoc();
10901
10902 // Store the value to a temporary stack slot.
10903 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010904 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10905 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010906
10907 // Replace each use (extract) with a load of the appropriate element.
10908 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10909 UE = Uses.end(); UI != UE; ++UI) {
10910 SDNode *Extract = *UI;
10911
10912 // Compute the element's address.
10913 SDValue Idx = Extract->getOperand(1);
10914 unsigned EltSize =
10915 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10916 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10917 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10918
Eric Christopher90eb4022010-07-22 00:26:08 +000010919 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010920 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010921
10922 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010923 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010924 ScalarAddr, MachinePointerInfo(),
10925 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010926
10927 // Replace the exact with the load.
10928 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10929 }
10930
10931 // The replacement was made in place; don't return anything.
10932 return SDValue();
10933}
10934
Chris Lattner83e6c992006-10-04 06:57:07 +000010935/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010936static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010937 const X86Subtarget *Subtarget) {
10938 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010939 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010940 // Get the LHS/RHS of the select.
10941 SDValue LHS = N->getOperand(1);
10942 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010943
Dan Gohman670e5392009-09-21 18:03:22 +000010944 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010945 // instructions match the semantics of the common C idiom x<y?x:y but not
10946 // x<=y?x:y, because of how they handle negative zero (which can be
10947 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010948 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010949 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010950 Cond.getOpcode() == ISD::SETCC) {
10951 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010952
Chris Lattner47b4ce82009-03-11 05:48:52 +000010953 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010954 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010955 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10956 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010957 switch (CC) {
10958 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010959 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010960 // Converting this to a min would handle NaNs incorrectly, and swapping
10961 // the operands would cause it to handle comparisons between positive
10962 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010963 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010964 if (!UnsafeFPMath &&
10965 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10966 break;
10967 std::swap(LHS, RHS);
10968 }
Dan Gohman670e5392009-09-21 18:03:22 +000010969 Opcode = X86ISD::FMIN;
10970 break;
10971 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010972 // Converting this to a min would handle comparisons between positive
10973 // and negative zero incorrectly.
10974 if (!UnsafeFPMath &&
10975 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10976 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010977 Opcode = X86ISD::FMIN;
10978 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010979 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010980 // Converting this to a min would handle both negative zeros and NaNs
10981 // incorrectly, but we can swap the operands to fix both.
10982 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010983 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010984 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010985 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010986 Opcode = X86ISD::FMIN;
10987 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010988
Dan Gohman670e5392009-09-21 18:03:22 +000010989 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010990 // Converting this to a max would handle comparisons between positive
10991 // and negative zero incorrectly.
10992 if (!UnsafeFPMath &&
10993 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10994 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010995 Opcode = X86ISD::FMAX;
10996 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010997 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010998 // Converting this to a max would handle NaNs incorrectly, and swapping
10999 // the operands would cause it to handle comparisons between positive
11000 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011001 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011002 if (!UnsafeFPMath &&
11003 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11004 break;
11005 std::swap(LHS, RHS);
11006 }
Dan Gohman670e5392009-09-21 18:03:22 +000011007 Opcode = X86ISD::FMAX;
11008 break;
11009 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011010 // Converting this to a max would handle both negative zeros and NaNs
11011 // incorrectly, but we can swap the operands to fix both.
11012 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011013 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011014 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011015 case ISD::SETGE:
11016 Opcode = X86ISD::FMAX;
11017 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011018 }
Dan Gohman670e5392009-09-21 18:03:22 +000011019 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011020 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11021 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011022 switch (CC) {
11023 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011024 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011025 // Converting this to a min would handle comparisons between positive
11026 // and negative zero incorrectly, and swapping the operands would
11027 // cause it to handle NaNs incorrectly.
11028 if (!UnsafeFPMath &&
11029 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011030 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011031 break;
11032 std::swap(LHS, RHS);
11033 }
Dan Gohman670e5392009-09-21 18:03:22 +000011034 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011035 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011036 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011037 // Converting this to a min would handle NaNs incorrectly.
11038 if (!UnsafeFPMath &&
11039 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11040 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011041 Opcode = X86ISD::FMIN;
11042 break;
11043 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011044 // Converting this to a min would handle both negative zeros and NaNs
11045 // incorrectly, but we can swap the operands to fix both.
11046 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011047 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011048 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011049 case ISD::SETGE:
11050 Opcode = X86ISD::FMIN;
11051 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011052
Dan Gohman670e5392009-09-21 18:03:22 +000011053 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011054 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011055 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011056 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011057 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011058 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011059 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011060 // Converting this to a max would handle comparisons between positive
11061 // and negative zero incorrectly, and swapping the operands would
11062 // cause it to handle NaNs incorrectly.
11063 if (!UnsafeFPMath &&
11064 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011065 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011066 break;
11067 std::swap(LHS, RHS);
11068 }
Dan Gohman670e5392009-09-21 18:03:22 +000011069 Opcode = X86ISD::FMAX;
11070 break;
11071 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011072 // Converting this to a max would handle both negative zeros and NaNs
11073 // incorrectly, but we can swap the operands to fix both.
11074 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011075 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011076 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011077 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011078 Opcode = X86ISD::FMAX;
11079 break;
11080 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011081 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011082
Chris Lattner47b4ce82009-03-11 05:48:52 +000011083 if (Opcode)
11084 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011085 }
Eric Christopherfd179292009-08-27 18:07:15 +000011086
Chris Lattnerd1980a52009-03-12 06:52:53 +000011087 // If this is a select between two integer constants, try to do some
11088 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011089 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11090 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011091 // Don't do this for crazy integer types.
11092 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11093 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011094 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011095 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011096
Chris Lattnercee56e72009-03-13 05:53:31 +000011097 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011098 // Efficiently invertible.
11099 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11100 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11101 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11102 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011103 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011104 }
Eric Christopherfd179292009-08-27 18:07:15 +000011105
Chris Lattnerd1980a52009-03-12 06:52:53 +000011106 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011107 if (FalseC->getAPIntValue() == 0 &&
11108 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011109 if (NeedsCondInvert) // Invert the condition if needed.
11110 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11111 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011112
Chris Lattnerd1980a52009-03-12 06:52:53 +000011113 // Zero extend the condition if needed.
11114 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011115
Chris Lattnercee56e72009-03-13 05:53:31 +000011116 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011117 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011118 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011119 }
Eric Christopherfd179292009-08-27 18:07:15 +000011120
Chris Lattner97a29a52009-03-13 05:22:11 +000011121 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011122 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011123 if (NeedsCondInvert) // Invert the condition if needed.
11124 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11125 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011126
Chris Lattner97a29a52009-03-13 05:22:11 +000011127 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011128 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11129 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011130 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011131 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011132 }
Eric Christopherfd179292009-08-27 18:07:15 +000011133
Chris Lattnercee56e72009-03-13 05:53:31 +000011134 // Optimize cases that will turn into an LEA instruction. This requires
11135 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011136 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011137 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011138 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011139
Chris Lattnercee56e72009-03-13 05:53:31 +000011140 bool isFastMultiplier = false;
11141 if (Diff < 10) {
11142 switch ((unsigned char)Diff) {
11143 default: break;
11144 case 1: // result = add base, cond
11145 case 2: // result = lea base( , cond*2)
11146 case 3: // result = lea base(cond, cond*2)
11147 case 4: // result = lea base( , cond*4)
11148 case 5: // result = lea base(cond, cond*4)
11149 case 8: // result = lea base( , cond*8)
11150 case 9: // result = lea base(cond, cond*8)
11151 isFastMultiplier = true;
11152 break;
11153 }
11154 }
Eric Christopherfd179292009-08-27 18:07:15 +000011155
Chris Lattnercee56e72009-03-13 05:53:31 +000011156 if (isFastMultiplier) {
11157 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11158 if (NeedsCondInvert) // Invert the condition if needed.
11159 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11160 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011161
Chris Lattnercee56e72009-03-13 05:53:31 +000011162 // Zero extend the condition if needed.
11163 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11164 Cond);
11165 // Scale the condition by the difference.
11166 if (Diff != 1)
11167 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11168 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011169
Chris Lattnercee56e72009-03-13 05:53:31 +000011170 // Add the base if non-zero.
11171 if (FalseC->getAPIntValue() != 0)
11172 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11173 SDValue(FalseC, 0));
11174 return Cond;
11175 }
Eric Christopherfd179292009-08-27 18:07:15 +000011176 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011177 }
11178 }
Eric Christopherfd179292009-08-27 18:07:15 +000011179
Dan Gohman475871a2008-07-27 21:46:04 +000011180 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011181}
11182
Chris Lattnerd1980a52009-03-12 06:52:53 +000011183/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11184static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11185 TargetLowering::DAGCombinerInfo &DCI) {
11186 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011187
Chris Lattnerd1980a52009-03-12 06:52:53 +000011188 // If the flag operand isn't dead, don't touch this CMOV.
11189 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11190 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011191
Chris Lattnerd1980a52009-03-12 06:52:53 +000011192 // If this is a select between two integer constants, try to do some
11193 // optimizations. Note that the operands are ordered the opposite of SELECT
11194 // operands.
11195 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11196 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11197 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11198 // larger than FalseC (the false value).
11199 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011200
Chris Lattnerd1980a52009-03-12 06:52:53 +000011201 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11202 CC = X86::GetOppositeBranchCondition(CC);
11203 std::swap(TrueC, FalseC);
11204 }
Eric Christopherfd179292009-08-27 18:07:15 +000011205
Chris Lattnerd1980a52009-03-12 06:52:53 +000011206 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011207 // This is efficient for any integer data type (including i8/i16) and
11208 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011209 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11210 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011211 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11212 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011213
Chris Lattnerd1980a52009-03-12 06:52:53 +000011214 // Zero extend the condition if needed.
11215 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011216
Chris Lattnerd1980a52009-03-12 06:52:53 +000011217 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11218 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011219 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011220 if (N->getNumValues() == 2) // Dead flag value?
11221 return DCI.CombineTo(N, Cond, SDValue());
11222 return Cond;
11223 }
Eric Christopherfd179292009-08-27 18:07:15 +000011224
Chris Lattnercee56e72009-03-13 05:53:31 +000011225 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11226 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011227 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11228 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011229 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11230 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011231
Chris Lattner97a29a52009-03-13 05:22:11 +000011232 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011233 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11234 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011235 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11236 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011237
Chris Lattner97a29a52009-03-13 05:22:11 +000011238 if (N->getNumValues() == 2) // Dead flag value?
11239 return DCI.CombineTo(N, Cond, SDValue());
11240 return Cond;
11241 }
Eric Christopherfd179292009-08-27 18:07:15 +000011242
Chris Lattnercee56e72009-03-13 05:53:31 +000011243 // Optimize cases that will turn into an LEA instruction. This requires
11244 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011245 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011246 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011247 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011248
Chris Lattnercee56e72009-03-13 05:53:31 +000011249 bool isFastMultiplier = false;
11250 if (Diff < 10) {
11251 switch ((unsigned char)Diff) {
11252 default: break;
11253 case 1: // result = add base, cond
11254 case 2: // result = lea base( , cond*2)
11255 case 3: // result = lea base(cond, cond*2)
11256 case 4: // result = lea base( , cond*4)
11257 case 5: // result = lea base(cond, cond*4)
11258 case 8: // result = lea base( , cond*8)
11259 case 9: // result = lea base(cond, cond*8)
11260 isFastMultiplier = true;
11261 break;
11262 }
11263 }
Eric Christopherfd179292009-08-27 18:07:15 +000011264
Chris Lattnercee56e72009-03-13 05:53:31 +000011265 if (isFastMultiplier) {
11266 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11267 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011268 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11269 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011270 // Zero extend the condition if needed.
11271 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11272 Cond);
11273 // Scale the condition by the difference.
11274 if (Diff != 1)
11275 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11276 DAG.getConstant(Diff, Cond.getValueType()));
11277
11278 // Add the base if non-zero.
11279 if (FalseC->getAPIntValue() != 0)
11280 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11281 SDValue(FalseC, 0));
11282 if (N->getNumValues() == 2) // Dead flag value?
11283 return DCI.CombineTo(N, Cond, SDValue());
11284 return Cond;
11285 }
Eric Christopherfd179292009-08-27 18:07:15 +000011286 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011287 }
11288 }
11289 return SDValue();
11290}
11291
11292
Evan Cheng0b0cd912009-03-28 05:57:29 +000011293/// PerformMulCombine - Optimize a single multiply with constant into two
11294/// in order to implement it with two cheaper instructions, e.g.
11295/// LEA + SHL, LEA + LEA.
11296static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11297 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011298 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11299 return SDValue();
11300
Owen Andersone50ed302009-08-10 22:56:29 +000011301 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011302 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011303 return SDValue();
11304
11305 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11306 if (!C)
11307 return SDValue();
11308 uint64_t MulAmt = C->getZExtValue();
11309 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11310 return SDValue();
11311
11312 uint64_t MulAmt1 = 0;
11313 uint64_t MulAmt2 = 0;
11314 if ((MulAmt % 9) == 0) {
11315 MulAmt1 = 9;
11316 MulAmt2 = MulAmt / 9;
11317 } else if ((MulAmt % 5) == 0) {
11318 MulAmt1 = 5;
11319 MulAmt2 = MulAmt / 5;
11320 } else if ((MulAmt % 3) == 0) {
11321 MulAmt1 = 3;
11322 MulAmt2 = MulAmt / 3;
11323 }
11324 if (MulAmt2 &&
11325 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11326 DebugLoc DL = N->getDebugLoc();
11327
11328 if (isPowerOf2_64(MulAmt2) &&
11329 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11330 // If second multiplifer is pow2, issue it first. We want the multiply by
11331 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11332 // is an add.
11333 std::swap(MulAmt1, MulAmt2);
11334
11335 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011336 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011337 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011338 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011339 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011340 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011341 DAG.getConstant(MulAmt1, VT));
11342
Eric Christopherfd179292009-08-27 18:07:15 +000011343 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011344 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011345 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011346 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011347 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011348 DAG.getConstant(MulAmt2, VT));
11349
11350 // Do not add new nodes to DAG combiner worklist.
11351 DCI.CombineTo(N, NewMul, false);
11352 }
11353 return SDValue();
11354}
11355
Evan Chengad9c0a32009-12-15 00:53:42 +000011356static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11357 SDValue N0 = N->getOperand(0);
11358 SDValue N1 = N->getOperand(1);
11359 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11360 EVT VT = N0.getValueType();
11361
11362 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11363 // since the result of setcc_c is all zero's or all ones.
11364 if (N1C && N0.getOpcode() == ISD::AND &&
11365 N0.getOperand(1).getOpcode() == ISD::Constant) {
11366 SDValue N00 = N0.getOperand(0);
11367 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11368 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11369 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11370 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11371 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11372 APInt ShAmt = N1C->getAPIntValue();
11373 Mask = Mask.shl(ShAmt);
11374 if (Mask != 0)
11375 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11376 N00, DAG.getConstant(Mask, VT));
11377 }
11378 }
11379
11380 return SDValue();
11381}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011382
Nate Begeman740ab032009-01-26 00:52:55 +000011383/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11384/// when possible.
11385static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11386 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011387 EVT VT = N->getValueType(0);
11388 if (!VT.isVector() && VT.isInteger() &&
11389 N->getOpcode() == ISD::SHL)
11390 return PerformSHLCombine(N, DAG);
11391
Nate Begeman740ab032009-01-26 00:52:55 +000011392 // On X86 with SSE2 support, we can transform this to a vector shift if
11393 // all elements are shifted by the same amount. We can't do this in legalize
11394 // because the a constant vector is typically transformed to a constant pool
11395 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011396 if (!Subtarget->hasSSE2())
11397 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011398
Owen Anderson825b72b2009-08-11 20:47:22 +000011399 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011400 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011401
Mon P Wang3becd092009-01-28 08:12:05 +000011402 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011403 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011404 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011405 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011406 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11407 unsigned NumElts = VT.getVectorNumElements();
11408 unsigned i = 0;
11409 for (; i != NumElts; ++i) {
11410 SDValue Arg = ShAmtOp.getOperand(i);
11411 if (Arg.getOpcode() == ISD::UNDEF) continue;
11412 BaseShAmt = Arg;
11413 break;
11414 }
11415 for (; i != NumElts; ++i) {
11416 SDValue Arg = ShAmtOp.getOperand(i);
11417 if (Arg.getOpcode() == ISD::UNDEF) continue;
11418 if (Arg != BaseShAmt) {
11419 return SDValue();
11420 }
11421 }
11422 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011423 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011424 SDValue InVec = ShAmtOp.getOperand(0);
11425 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11426 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11427 unsigned i = 0;
11428 for (; i != NumElts; ++i) {
11429 SDValue Arg = InVec.getOperand(i);
11430 if (Arg.getOpcode() == ISD::UNDEF) continue;
11431 BaseShAmt = Arg;
11432 break;
11433 }
11434 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011436 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011437 if (C->getZExtValue() == SplatIdx)
11438 BaseShAmt = InVec.getOperand(1);
11439 }
11440 }
11441 if (BaseShAmt.getNode() == 0)
11442 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11443 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011444 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011445 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011446
Mon P Wangefa42202009-09-03 19:56:25 +000011447 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011448 if (EltVT.bitsGT(MVT::i32))
11449 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11450 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011451 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011452
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011453 // The shift amount is identical so we can do a vector shift.
11454 SDValue ValOp = N->getOperand(0);
11455 switch (N->getOpcode()) {
11456 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011457 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011458 break;
11459 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011460 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011461 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011462 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011463 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011464 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011465 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011466 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011467 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011468 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011469 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011470 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011471 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011472 break;
11473 case ISD::SRA:
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_psrai_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_psrai_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::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011484 if (VT == MVT::v2i64)
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_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011487 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011488 if (VT == MVT::v4i32)
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_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011491 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011492 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011493 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011494 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011495 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011496 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011497 }
11498 return SDValue();
11499}
11500
Nate Begemanb65c1752010-12-17 22:55:37 +000011501
11502static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11503 TargetLowering::DAGCombinerInfo &DCI,
11504 const X86Subtarget *Subtarget) {
11505 if (DCI.isBeforeLegalizeOps())
11506 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011507
Nate Begemanb65c1752010-12-17 22:55:37 +000011508 // Want to form PANDN nodes, in the hopes of then easily combining them with
11509 // OR and AND nodes to form PBLEND/PSIGN.
11510 EVT VT = N->getValueType(0);
11511 if (VT != MVT::v2i64)
11512 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011513
Nate Begemanb65c1752010-12-17 22:55:37 +000011514 SDValue N0 = N->getOperand(0);
11515 SDValue N1 = N->getOperand(1);
11516 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011517
Nate Begemanb65c1752010-12-17 22:55:37 +000011518 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011519 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011520 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11521 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11522
11523 // Check RHS for vnot
11524 if (N1.getOpcode() == ISD::XOR &&
11525 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11526 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011527
Nate Begemanb65c1752010-12-17 22:55:37 +000011528 return SDValue();
11529}
11530
Evan Cheng760d1942010-01-04 21:22:48 +000011531static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011532 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011533 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011534 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011535 return SDValue();
11536
Evan Cheng760d1942010-01-04 21:22:48 +000011537 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011538 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011539 return SDValue();
11540
Evan Cheng760d1942010-01-04 21:22:48 +000011541 SDValue N0 = N->getOperand(0);
11542 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011543
Nate Begemanb65c1752010-12-17 22:55:37 +000011544 // look for psign/blend
11545 if (Subtarget->hasSSSE3()) {
11546 if (VT == MVT::v2i64) {
11547 // Canonicalize pandn to RHS
11548 if (N0.getOpcode() == X86ISD::PANDN)
11549 std::swap(N0, N1);
11550 // or (and (m, x), (pandn m, y))
11551 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11552 SDValue Mask = N1.getOperand(0);
11553 SDValue X = N1.getOperand(1);
11554 SDValue Y;
11555 if (N0.getOperand(0) == Mask)
11556 Y = N0.getOperand(1);
11557 if (N0.getOperand(1) == Mask)
11558 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011559
Nate Begemanb65c1752010-12-17 22:55:37 +000011560 // Check to see if the mask appeared in both the AND and PANDN and
11561 if (!Y.getNode())
11562 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011563
Nate Begemanb65c1752010-12-17 22:55:37 +000011564 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11565 if (Mask.getOpcode() != ISD::BITCAST ||
11566 X.getOpcode() != ISD::BITCAST ||
11567 Y.getOpcode() != ISD::BITCAST)
11568 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011569
Nate Begemanb65c1752010-12-17 22:55:37 +000011570 // Look through mask bitcast.
11571 Mask = Mask.getOperand(0);
11572 EVT MaskVT = Mask.getValueType();
11573
11574 // Validate that the Mask operand is a vector sra node. The sra node
11575 // will be an intrinsic.
11576 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11577 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011578
Nate Begemanb65c1752010-12-17 22:55:37 +000011579 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11580 // there is no psrai.b
11581 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11582 case Intrinsic::x86_sse2_psrai_w:
11583 case Intrinsic::x86_sse2_psrai_d:
11584 break;
11585 default: return SDValue();
11586 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011587
Nate Begemanb65c1752010-12-17 22:55:37 +000011588 // Check that the SRA is all signbits.
11589 SDValue SraC = Mask.getOperand(2);
11590 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11591 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11592 if ((SraAmt + 1) != EltBits)
11593 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011594
Nate Begemanb65c1752010-12-17 22:55:37 +000011595 DebugLoc DL = N->getDebugLoc();
11596
11597 // Now we know we at least have a plendvb with the mask val. See if
11598 // we can form a psignb/w/d.
11599 // psign = x.type == y.type == mask.type && y = sub(0, x);
11600 X = X.getOperand(0);
11601 Y = Y.getOperand(0);
11602 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11603 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11604 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11605 unsigned Opc = 0;
11606 switch (EltBits) {
11607 case 8: Opc = X86ISD::PSIGNB; break;
11608 case 16: Opc = X86ISD::PSIGNW; break;
11609 case 32: Opc = X86ISD::PSIGND; break;
11610 default: break;
11611 }
11612 if (Opc) {
11613 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11614 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11615 }
11616 }
11617 // PBLENDVB only available on SSE 4.1
11618 if (!Subtarget->hasSSE41())
11619 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011620
Nate Begemanb65c1752010-12-17 22:55:37 +000011621 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11622 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11623 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011624 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011625 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11626 }
11627 }
11628 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011629
Nate Begemanb65c1752010-12-17 22:55:37 +000011630 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011631 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11632 std::swap(N0, N1);
11633 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11634 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011635 if (!N0.hasOneUse() || !N1.hasOneUse())
11636 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011637
11638 SDValue ShAmt0 = N0.getOperand(1);
11639 if (ShAmt0.getValueType() != MVT::i8)
11640 return SDValue();
11641 SDValue ShAmt1 = N1.getOperand(1);
11642 if (ShAmt1.getValueType() != MVT::i8)
11643 return SDValue();
11644 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11645 ShAmt0 = ShAmt0.getOperand(0);
11646 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11647 ShAmt1 = ShAmt1.getOperand(0);
11648
11649 DebugLoc DL = N->getDebugLoc();
11650 unsigned Opc = X86ISD::SHLD;
11651 SDValue Op0 = N0.getOperand(0);
11652 SDValue Op1 = N1.getOperand(0);
11653 if (ShAmt0.getOpcode() == ISD::SUB) {
11654 Opc = X86ISD::SHRD;
11655 std::swap(Op0, Op1);
11656 std::swap(ShAmt0, ShAmt1);
11657 }
11658
Evan Cheng8b1190a2010-04-28 01:18:01 +000011659 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011660 if (ShAmt1.getOpcode() == ISD::SUB) {
11661 SDValue Sum = ShAmt1.getOperand(0);
11662 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011663 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11664 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11665 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11666 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011667 return DAG.getNode(Opc, DL, VT,
11668 Op0, Op1,
11669 DAG.getNode(ISD::TRUNCATE, DL,
11670 MVT::i8, ShAmt0));
11671 }
11672 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11673 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11674 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011675 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011676 return DAG.getNode(Opc, DL, VT,
11677 N0.getOperand(0), N1.getOperand(0),
11678 DAG.getNode(ISD::TRUNCATE, DL,
11679 MVT::i8, ShAmt0));
11680 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011681
Evan Cheng760d1942010-01-04 21:22:48 +000011682 return SDValue();
11683}
11684
Chris Lattner149a4e52008-02-22 02:09:43 +000011685/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011686static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011687 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011688 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11689 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011690 // A preferable solution to the general problem is to figure out the right
11691 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011692
11693 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011694 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011695 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011696 if (VT.getSizeInBits() != 64)
11697 return SDValue();
11698
Devang Patel578efa92009-06-05 21:57:13 +000011699 const Function *F = DAG.getMachineFunction().getFunction();
11700 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011701 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011702 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011703 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011704 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011705 isa<LoadSDNode>(St->getValue()) &&
11706 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11707 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011708 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011709 LoadSDNode *Ld = 0;
11710 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011711 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011712 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011713 // Must be a store of a load. We currently handle two cases: the load
11714 // is a direct child, and it's under an intervening TokenFactor. It is
11715 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011716 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011717 Ld = cast<LoadSDNode>(St->getChain());
11718 else if (St->getValue().hasOneUse() &&
11719 ChainVal->getOpcode() == ISD::TokenFactor) {
11720 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011721 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011722 TokenFactorIndex = i;
11723 Ld = cast<LoadSDNode>(St->getValue());
11724 } else
11725 Ops.push_back(ChainVal->getOperand(i));
11726 }
11727 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011728
Evan Cheng536e6672009-03-12 05:59:15 +000011729 if (!Ld || !ISD::isNormalLoad(Ld))
11730 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011731
Evan Cheng536e6672009-03-12 05:59:15 +000011732 // If this is not the MMX case, i.e. we are just turning i64 load/store
11733 // into f64 load/store, avoid the transformation if there are multiple
11734 // uses of the loaded value.
11735 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11736 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011737
Evan Cheng536e6672009-03-12 05:59:15 +000011738 DebugLoc LdDL = Ld->getDebugLoc();
11739 DebugLoc StDL = N->getDebugLoc();
11740 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11741 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11742 // pair instead.
11743 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011744 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011745 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11746 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011747 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011748 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011749 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011750 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011751 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011752 Ops.size());
11753 }
Evan Cheng536e6672009-03-12 05:59:15 +000011754 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011755 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011756 St->isVolatile(), St->isNonTemporal(),
11757 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011758 }
Evan Cheng536e6672009-03-12 05:59:15 +000011759
11760 // Otherwise, lower to two pairs of 32-bit loads / stores.
11761 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011762 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11763 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011764
Owen Anderson825b72b2009-08-11 20:47:22 +000011765 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011766 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011767 Ld->isVolatile(), Ld->isNonTemporal(),
11768 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011769 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011770 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011771 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011772 MinAlign(Ld->getAlignment(), 4));
11773
11774 SDValue NewChain = LoLd.getValue(1);
11775 if (TokenFactorIndex != -1) {
11776 Ops.push_back(LoLd);
11777 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011778 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011779 Ops.size());
11780 }
11781
11782 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011783 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11784 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011785
11786 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011787 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011788 St->isVolatile(), St->isNonTemporal(),
11789 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011790 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011791 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011792 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011793 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011794 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011795 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011796 }
Dan Gohman475871a2008-07-27 21:46:04 +000011797 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011798}
11799
Chris Lattner6cf73262008-01-25 06:14:17 +000011800/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11801/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011802static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011803 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11804 // F[X]OR(0.0, x) -> x
11805 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011806 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11807 if (C->getValueAPF().isPosZero())
11808 return N->getOperand(1);
11809 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11810 if (C->getValueAPF().isPosZero())
11811 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011812 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011813}
11814
11815/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011816static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011817 // FAND(0.0, x) -> 0.0
11818 // FAND(x, 0.0) -> 0.0
11819 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11820 if (C->getValueAPF().isPosZero())
11821 return N->getOperand(0);
11822 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11823 if (C->getValueAPF().isPosZero())
11824 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011825 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011826}
11827
Dan Gohmane5af2d32009-01-29 01:59:02 +000011828static SDValue PerformBTCombine(SDNode *N,
11829 SelectionDAG &DAG,
11830 TargetLowering::DAGCombinerInfo &DCI) {
11831 // BT ignores high bits in the bit index operand.
11832 SDValue Op1 = N->getOperand(1);
11833 if (Op1.hasOneUse()) {
11834 unsigned BitWidth = Op1.getValueSizeInBits();
11835 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11836 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011837 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11838 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011839 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011840 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11841 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11842 DCI.CommitTargetLoweringOpt(TLO);
11843 }
11844 return SDValue();
11845}
Chris Lattner83e6c992006-10-04 06:57:07 +000011846
Eli Friedman7a5e5552009-06-07 06:52:44 +000011847static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11848 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011849 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011850 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011851 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011852 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011853 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011854 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011855 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011856 }
11857 return SDValue();
11858}
11859
Evan Cheng2e489c42009-12-16 00:53:11 +000011860static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11861 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11862 // (and (i32 x86isd::setcc_carry), 1)
11863 // This eliminates the zext. This transformation is necessary because
11864 // ISD::SETCC is always legalized to i8.
11865 DebugLoc dl = N->getDebugLoc();
11866 SDValue N0 = N->getOperand(0);
11867 EVT VT = N->getValueType(0);
11868 if (N0.getOpcode() == ISD::AND &&
11869 N0.hasOneUse() &&
11870 N0.getOperand(0).hasOneUse()) {
11871 SDValue N00 = N0.getOperand(0);
11872 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11873 return SDValue();
11874 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11875 if (!C || C->getZExtValue() != 1)
11876 return SDValue();
11877 return DAG.getNode(ISD::AND, dl, VT,
11878 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11879 N00.getOperand(0), N00.getOperand(1)),
11880 DAG.getConstant(1, VT));
11881 }
11882
11883 return SDValue();
11884}
11885
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011886// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11887static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11888 unsigned X86CC = N->getConstantOperandVal(0);
11889 SDValue EFLAG = N->getOperand(1);
11890 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011891
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011892 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11893 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11894 // cases.
11895 if (X86CC == X86::COND_B)
11896 return DAG.getNode(ISD::AND, DL, MVT::i8,
11897 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11898 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11899 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011900
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011901 return SDValue();
11902}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011903
Chris Lattner23a01992010-12-20 01:37:09 +000011904// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11905static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11906 X86TargetLowering::DAGCombinerInfo &DCI) {
11907 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11908 // the result is either zero or one (depending on the input carry bit).
11909 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11910 if (X86::isZeroNode(N->getOperand(0)) &&
11911 X86::isZeroNode(N->getOperand(1)) &&
11912 // We don't have a good way to replace an EFLAGS use, so only do this when
11913 // dead right now.
11914 SDValue(N, 1).use_empty()) {
11915 DebugLoc DL = N->getDebugLoc();
11916 EVT VT = N->getValueType(0);
11917 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11918 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11919 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11920 DAG.getConstant(X86::COND_B,MVT::i8),
11921 N->getOperand(2)),
11922 DAG.getConstant(1, VT));
11923 return DCI.CombineTo(N, Res1, CarryOut);
11924 }
11925
11926 return SDValue();
11927}
11928
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011929// fold (add Y, (sete X, 0)) -> adc 0, Y
11930// (add Y, (setne X, 0)) -> sbb -1, Y
11931// (sub (sete X, 0), Y) -> sbb 0, Y
11932// (sub (setne X, 0), Y) -> adc -1, Y
11933static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11934 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011935
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011936 // Look through ZExts.
11937 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11938 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11939 return SDValue();
11940
11941 SDValue SetCC = Ext.getOperand(0);
11942 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11943 return SDValue();
11944
11945 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11946 if (CC != X86::COND_E && CC != X86::COND_NE)
11947 return SDValue();
11948
11949 SDValue Cmp = SetCC.getOperand(1);
11950 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011951 !X86::isZeroNode(Cmp.getOperand(1)) ||
11952 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011953 return SDValue();
11954
11955 SDValue CmpOp0 = Cmp.getOperand(0);
11956 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11957 DAG.getConstant(1, CmpOp0.getValueType()));
11958
11959 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11960 if (CC == X86::COND_NE)
11961 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11962 DL, OtherVal.getValueType(), OtherVal,
11963 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11964 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11965 DL, OtherVal.getValueType(), OtherVal,
11966 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11967}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011968
Dan Gohman475871a2008-07-27 21:46:04 +000011969SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011970 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011971 SelectionDAG &DAG = DCI.DAG;
11972 switch (N->getOpcode()) {
11973 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011974 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011975 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011976 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011977 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011978 case ISD::ADD:
11979 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011980 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011981 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011982 case ISD::SHL:
11983 case ISD::SRA:
11984 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011985 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011986 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011987 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011988 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011989 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11990 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011991 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011992 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011993 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011994 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011995 case X86ISD::SHUFPS: // Handle all target specific shuffles
11996 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011997 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011998 case X86ISD::PUNPCKHBW:
11999 case X86ISD::PUNPCKHWD:
12000 case X86ISD::PUNPCKHDQ:
12001 case X86ISD::PUNPCKHQDQ:
12002 case X86ISD::UNPCKHPS:
12003 case X86ISD::UNPCKHPD:
12004 case X86ISD::PUNPCKLBW:
12005 case X86ISD::PUNPCKLWD:
12006 case X86ISD::PUNPCKLDQ:
12007 case X86ISD::PUNPCKLQDQ:
12008 case X86ISD::UNPCKLPS:
12009 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012010 case X86ISD::VUNPCKLPS:
12011 case X86ISD::VUNPCKLPD:
12012 case X86ISD::VUNPCKLPSY:
12013 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012014 case X86ISD::MOVHLPS:
12015 case X86ISD::MOVLHPS:
12016 case X86ISD::PSHUFD:
12017 case X86ISD::PSHUFHW:
12018 case X86ISD::PSHUFLW:
12019 case X86ISD::MOVSS:
12020 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012021 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012022 }
12023
Dan Gohman475871a2008-07-27 21:46:04 +000012024 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012025}
12026
Evan Chenge5b51ac2010-04-17 06:13:15 +000012027/// isTypeDesirableForOp - Return true if the target has native support for
12028/// the specified value type and it is 'desirable' to use the type for the
12029/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12030/// instruction encodings are longer and some i16 instructions are slow.
12031bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12032 if (!isTypeLegal(VT))
12033 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012034 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012035 return true;
12036
12037 switch (Opc) {
12038 default:
12039 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012040 case ISD::LOAD:
12041 case ISD::SIGN_EXTEND:
12042 case ISD::ZERO_EXTEND:
12043 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012044 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012045 case ISD::SRL:
12046 case ISD::SUB:
12047 case ISD::ADD:
12048 case ISD::MUL:
12049 case ISD::AND:
12050 case ISD::OR:
12051 case ISD::XOR:
12052 return false;
12053 }
12054}
12055
12056/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012057/// beneficial for dag combiner to promote the specified node. If true, it
12058/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012059bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012060 EVT VT = Op.getValueType();
12061 if (VT != MVT::i16)
12062 return false;
12063
Evan Cheng4c26e932010-04-19 19:29:22 +000012064 bool Promote = false;
12065 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012066 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012067 default: break;
12068 case ISD::LOAD: {
12069 LoadSDNode *LD = cast<LoadSDNode>(Op);
12070 // If the non-extending load has a single use and it's not live out, then it
12071 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012072 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12073 Op.hasOneUse()*/) {
12074 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12075 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12076 // The only case where we'd want to promote LOAD (rather then it being
12077 // promoted as an operand is when it's only use is liveout.
12078 if (UI->getOpcode() != ISD::CopyToReg)
12079 return false;
12080 }
12081 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012082 Promote = true;
12083 break;
12084 }
12085 case ISD::SIGN_EXTEND:
12086 case ISD::ZERO_EXTEND:
12087 case ISD::ANY_EXTEND:
12088 Promote = true;
12089 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012090 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012091 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012092 SDValue N0 = Op.getOperand(0);
12093 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012094 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012095 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012096 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012097 break;
12098 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012099 case ISD::ADD:
12100 case ISD::MUL:
12101 case ISD::AND:
12102 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012103 case ISD::XOR:
12104 Commute = true;
12105 // fallthrough
12106 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012107 SDValue N0 = Op.getOperand(0);
12108 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012109 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012110 return false;
12111 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012112 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012113 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012114 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012115 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012116 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012117 }
12118 }
12119
12120 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012121 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012122}
12123
Evan Cheng60c07e12006-07-05 22:17:51 +000012124//===----------------------------------------------------------------------===//
12125// X86 Inline Assembly Support
12126//===----------------------------------------------------------------------===//
12127
Chris Lattnerb8105652009-07-20 17:51:36 +000012128bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12129 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012130
12131 std::string AsmStr = IA->getAsmString();
12132
12133 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012134 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012135 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012136
12137 switch (AsmPieces.size()) {
12138 default: return false;
12139 case 1:
12140 AsmStr = AsmPieces[0];
12141 AsmPieces.clear();
12142 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12143
Evan Cheng55d42002011-01-08 01:24:27 +000012144 // FIXME: this should verify that we are targetting a 486 or better. If not,
12145 // we will turn this bswap into something that will be lowered to logical ops
12146 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12147 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012148 // bswap $0
12149 if (AsmPieces.size() == 2 &&
12150 (AsmPieces[0] == "bswap" ||
12151 AsmPieces[0] == "bswapq" ||
12152 AsmPieces[0] == "bswapl") &&
12153 (AsmPieces[1] == "$0" ||
12154 AsmPieces[1] == "${0:q}")) {
12155 // No need to check constraints, nothing other than the equivalent of
12156 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012157 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12158 if (!Ty || Ty->getBitWidth() % 16 != 0)
12159 return false;
12160 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012161 }
12162 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012163 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012164 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012165 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012166 AsmPieces[1] == "$$8," &&
12167 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012168 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12169 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012170 const std::string &ConstraintsStr = IA->getConstraintString();
12171 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000012172 std::sort(AsmPieces.begin(), AsmPieces.end());
12173 if (AsmPieces.size() == 4 &&
12174 AsmPieces[0] == "~{cc}" &&
12175 AsmPieces[1] == "~{dirflag}" &&
12176 AsmPieces[2] == "~{flags}" &&
12177 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012178 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12179 if (!Ty || Ty->getBitWidth() % 16 != 0)
12180 return false;
12181 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012182 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012183 }
12184 break;
12185 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012186 if (CI->getType()->isIntegerTy(32) &&
12187 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12188 SmallVector<StringRef, 4> Words;
12189 SplitString(AsmPieces[0], Words, " \t,");
12190 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12191 Words[2] == "${0:w}") {
12192 Words.clear();
12193 SplitString(AsmPieces[1], Words, " \t,");
12194 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12195 Words[2] == "$0") {
12196 Words.clear();
12197 SplitString(AsmPieces[2], Words, " \t,");
12198 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12199 Words[2] == "${0:w}") {
12200 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012201 const std::string &ConstraintsStr = IA->getConstraintString();
12202 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012203 std::sort(AsmPieces.begin(), AsmPieces.end());
12204 if (AsmPieces.size() == 4 &&
12205 AsmPieces[0] == "~{cc}" &&
12206 AsmPieces[1] == "~{dirflag}" &&
12207 AsmPieces[2] == "~{flags}" &&
12208 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012209 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12210 if (!Ty || Ty->getBitWidth() % 16 != 0)
12211 return false;
12212 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012213 }
12214 }
12215 }
12216 }
12217 }
Evan Cheng55d42002011-01-08 01:24:27 +000012218
12219 if (CI->getType()->isIntegerTy(64)) {
12220 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12221 if (Constraints.size() >= 2 &&
12222 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12223 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12224 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12225 SmallVector<StringRef, 4> Words;
12226 SplitString(AsmPieces[0], Words, " \t");
12227 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012228 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012229 SplitString(AsmPieces[1], Words, " \t");
12230 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12231 Words.clear();
12232 SplitString(AsmPieces[2], Words, " \t,");
12233 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12234 Words[2] == "%edx") {
12235 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12236 if (!Ty || Ty->getBitWidth() % 16 != 0)
12237 return false;
12238 return IntrinsicLowering::LowerToByteSwap(CI);
12239 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012240 }
12241 }
12242 }
12243 }
12244 break;
12245 }
12246 return false;
12247}
12248
12249
12250
Chris Lattnerf4dff842006-07-11 02:54:03 +000012251/// getConstraintType - Given a constraint letter, return the type of
12252/// constraint it is for this target.
12253X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012254X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12255 if (Constraint.size() == 1) {
12256 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012257 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012258 case 'q':
12259 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012260 case 'f':
12261 case 't':
12262 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012263 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012264 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012265 case 'Y':
12266 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012267 case 'a':
12268 case 'b':
12269 case 'c':
12270 case 'd':
12271 case 'S':
12272 case 'D':
12273 case 'A':
12274 return C_Register;
12275 case 'I':
12276 case 'J':
12277 case 'K':
12278 case 'L':
12279 case 'M':
12280 case 'N':
12281 case 'G':
12282 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012283 case 'e':
12284 case 'Z':
12285 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012286 default:
12287 break;
12288 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012289 }
Chris Lattner4234f572007-03-25 02:14:49 +000012290 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012291}
12292
John Thompson44ab89e2010-10-29 17:29:13 +000012293/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012294/// This object must already have been set up with the operand type
12295/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012296TargetLowering::ConstraintWeight
12297 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012298 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012299 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012300 Value *CallOperandVal = info.CallOperandVal;
12301 // If we don't have a value, we can't do a match,
12302 // but allow it at the lowest weight.
12303 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012304 return CW_Default;
12305 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012306 // Look at the constraint type.
12307 switch (*constraint) {
12308 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012309 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12310 case 'R':
12311 case 'q':
12312 case 'Q':
12313 case 'a':
12314 case 'b':
12315 case 'c':
12316 case 'd':
12317 case 'S':
12318 case 'D':
12319 case 'A':
12320 if (CallOperandVal->getType()->isIntegerTy())
12321 weight = CW_SpecificReg;
12322 break;
12323 case 'f':
12324 case 't':
12325 case 'u':
12326 if (type->isFloatingPointTy())
12327 weight = CW_SpecificReg;
12328 break;
12329 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012330 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012331 weight = CW_SpecificReg;
12332 break;
12333 case 'x':
12334 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012335 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012336 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012337 break;
12338 case 'I':
12339 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12340 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012341 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012342 }
12343 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012344 case 'J':
12345 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12346 if (C->getZExtValue() <= 63)
12347 weight = CW_Constant;
12348 }
12349 break;
12350 case 'K':
12351 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12352 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12353 weight = CW_Constant;
12354 }
12355 break;
12356 case 'L':
12357 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12358 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12359 weight = CW_Constant;
12360 }
12361 break;
12362 case 'M':
12363 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12364 if (C->getZExtValue() <= 3)
12365 weight = CW_Constant;
12366 }
12367 break;
12368 case 'N':
12369 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12370 if (C->getZExtValue() <= 0xff)
12371 weight = CW_Constant;
12372 }
12373 break;
12374 case 'G':
12375 case 'C':
12376 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12377 weight = CW_Constant;
12378 }
12379 break;
12380 case 'e':
12381 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12382 if ((C->getSExtValue() >= -0x80000000LL) &&
12383 (C->getSExtValue() <= 0x7fffffffLL))
12384 weight = CW_Constant;
12385 }
12386 break;
12387 case 'Z':
12388 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12389 if (C->getZExtValue() <= 0xffffffff)
12390 weight = CW_Constant;
12391 }
12392 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012393 }
12394 return weight;
12395}
12396
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012397/// LowerXConstraint - try to replace an X constraint, which matches anything,
12398/// with another that has more specific requirements based on the type of the
12399/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012400const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012401LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012402 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12403 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012404 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012405 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012406 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012407 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012408 return "x";
12409 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012410
Chris Lattner5e764232008-04-26 23:02:14 +000012411 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012412}
12413
Chris Lattner48884cd2007-08-25 00:47:38 +000012414/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12415/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012416void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012417 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012418 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012419 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012420 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012421
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012422 switch (Constraint) {
12423 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012424 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012425 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012426 if (C->getZExtValue() <= 31) {
12427 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012428 break;
12429 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012430 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012431 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012432 case 'J':
12433 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012434 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012435 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12436 break;
12437 }
12438 }
12439 return;
12440 case 'K':
12441 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012442 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012443 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12444 break;
12445 }
12446 }
12447 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012448 case 'N':
12449 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012450 if (C->getZExtValue() <= 255) {
12451 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012452 break;
12453 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012454 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012455 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012456 case 'e': {
12457 // 32-bit signed value
12458 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012459 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12460 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012461 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012462 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012463 break;
12464 }
12465 // FIXME gcc accepts some relocatable values here too, but only in certain
12466 // memory models; it's complicated.
12467 }
12468 return;
12469 }
12470 case 'Z': {
12471 // 32-bit unsigned value
12472 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012473 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12474 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012475 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12476 break;
12477 }
12478 }
12479 // FIXME gcc accepts some relocatable values here too, but only in certain
12480 // memory models; it's complicated.
12481 return;
12482 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012483 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012484 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012485 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012486 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012487 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012488 break;
12489 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012490
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012491 // In any sort of PIC mode addresses need to be computed at runtime by
12492 // adding in a register or some sort of table lookup. These can't
12493 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012494 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012495 return;
12496
Chris Lattnerdc43a882007-05-03 16:52:29 +000012497 // If we are in non-pic codegen mode, we allow the address of a global (with
12498 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012499 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012500 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012501
Chris Lattner49921962009-05-08 18:23:14 +000012502 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12503 while (1) {
12504 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12505 Offset += GA->getOffset();
12506 break;
12507 } else if (Op.getOpcode() == ISD::ADD) {
12508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12509 Offset += C->getZExtValue();
12510 Op = Op.getOperand(0);
12511 continue;
12512 }
12513 } else if (Op.getOpcode() == ISD::SUB) {
12514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12515 Offset += -C->getZExtValue();
12516 Op = Op.getOperand(0);
12517 continue;
12518 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012519 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012520
Chris Lattner49921962009-05-08 18:23:14 +000012521 // Otherwise, this isn't something we can handle, reject it.
12522 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012523 }
Eric Christopherfd179292009-08-27 18:07:15 +000012524
Dan Gohman46510a72010-04-15 01:51:59 +000012525 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012526 // If we require an extra load to get this address, as in PIC mode, we
12527 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012528 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12529 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012530 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012531
Devang Patel0d881da2010-07-06 22:08:15 +000012532 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12533 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012534 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012535 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012536 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012537
Gabor Greifba36cb52008-08-28 21:40:38 +000012538 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012539 Ops.push_back(Result);
12540 return;
12541 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012542 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012543}
12544
Chris Lattner259e97c2006-01-31 19:43:35 +000012545std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012546getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012547 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012548 if (Constraint.size() == 1) {
12549 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012550 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012551 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012552 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12553 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012554 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012555 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12556 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12557 X86::R10D,X86::R11D,X86::R12D,
12558 X86::R13D,X86::R14D,X86::R15D,
12559 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012560 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012561 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12562 X86::SI, X86::DI, X86::R8W,X86::R9W,
12563 X86::R10W,X86::R11W,X86::R12W,
12564 X86::R13W,X86::R14W,X86::R15W,
12565 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012566 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012567 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12568 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12569 X86::R10B,X86::R11B,X86::R12B,
12570 X86::R13B,X86::R14B,X86::R15B,
12571 X86::BPL, X86::SPL, 0);
12572
Owen Anderson825b72b2009-08-11 20:47:22 +000012573 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012574 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12575 X86::RSI, X86::RDI, X86::R8, X86::R9,
12576 X86::R10, X86::R11, X86::R12,
12577 X86::R13, X86::R14, X86::R15,
12578 X86::RBP, X86::RSP, 0);
12579
12580 break;
12581 }
Eric Christopherfd179292009-08-27 18:07:15 +000012582 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012583 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012584 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012585 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012586 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012587 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012588 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012589 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012590 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012591 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12592 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012593 }
12594 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012595
Chris Lattner1efa40f2006-02-22 00:56:39 +000012596 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012597}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012598
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012599std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012600X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012601 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012602 // First, see if this is a constraint that directly corresponds to an LLVM
12603 // register class.
12604 if (Constraint.size() == 1) {
12605 // GCC Constraint Letters
12606 switch (Constraint[0]) {
12607 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012608 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012609 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012610 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012611 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012612 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012613 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012614 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012615 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012616 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012617 case 'R': // LEGACY_REGS
12618 if (VT == MVT::i8)
12619 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12620 if (VT == MVT::i16)
12621 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12622 if (VT == MVT::i32 || !Subtarget->is64Bit())
12623 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12624 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012625 case 'f': // FP Stack registers.
12626 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12627 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012628 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012629 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012630 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012631 return std::make_pair(0U, X86::RFP64RegisterClass);
12632 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012633 case 'y': // MMX_REGS if MMX allowed.
12634 if (!Subtarget->hasMMX()) break;
12635 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012636 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012637 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012638 // FALL THROUGH.
12639 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012640 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012641
Owen Anderson825b72b2009-08-11 20:47:22 +000012642 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012643 default: break;
12644 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012645 case MVT::f32:
12646 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012647 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012648 case MVT::f64:
12649 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012650 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012651 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012652 case MVT::v16i8:
12653 case MVT::v8i16:
12654 case MVT::v4i32:
12655 case MVT::v2i64:
12656 case MVT::v4f32:
12657 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012658 return std::make_pair(0U, X86::VR128RegisterClass);
12659 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012660 break;
12661 }
12662 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012663
Chris Lattnerf76d1802006-07-31 23:26:50 +000012664 // Use the default implementation in TargetLowering to convert the register
12665 // constraint into a member of a register class.
12666 std::pair<unsigned, const TargetRegisterClass*> Res;
12667 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012668
12669 // Not found as a standard register?
12670 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012671 // Map st(0) -> st(7) -> ST0
12672 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12673 tolower(Constraint[1]) == 's' &&
12674 tolower(Constraint[2]) == 't' &&
12675 Constraint[3] == '(' &&
12676 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12677 Constraint[5] == ')' &&
12678 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012679
Chris Lattner56d77c72009-09-13 22:41:48 +000012680 Res.first = X86::ST0+Constraint[4]-'0';
12681 Res.second = X86::RFP80RegisterClass;
12682 return Res;
12683 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012684
Chris Lattner56d77c72009-09-13 22:41:48 +000012685 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012686 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012687 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012688 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012689 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012690 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012691
12692 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012693 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012694 Res.first = X86::EFLAGS;
12695 Res.second = X86::CCRRegisterClass;
12696 return Res;
12697 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012698
Dale Johannesen330169f2008-11-13 21:52:36 +000012699 // 'A' means EAX + EDX.
12700 if (Constraint == "A") {
12701 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012702 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012703 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012704 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012705 return Res;
12706 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012707
Chris Lattnerf76d1802006-07-31 23:26:50 +000012708 // Otherwise, check to see if this is a register class of the wrong value
12709 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12710 // turn into {ax},{dx}.
12711 if (Res.second->hasType(VT))
12712 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012713
Chris Lattnerf76d1802006-07-31 23:26:50 +000012714 // All of the single-register GCC register classes map their values onto
12715 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12716 // really want an 8-bit or 32-bit register, map to the appropriate register
12717 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012718 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012719 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012720 unsigned DestReg = 0;
12721 switch (Res.first) {
12722 default: break;
12723 case X86::AX: DestReg = X86::AL; break;
12724 case X86::DX: DestReg = X86::DL; break;
12725 case X86::CX: DestReg = X86::CL; break;
12726 case X86::BX: DestReg = X86::BL; break;
12727 }
12728 if (DestReg) {
12729 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012730 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012731 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012732 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012733 unsigned DestReg = 0;
12734 switch (Res.first) {
12735 default: break;
12736 case X86::AX: DestReg = X86::EAX; break;
12737 case X86::DX: DestReg = X86::EDX; break;
12738 case X86::CX: DestReg = X86::ECX; break;
12739 case X86::BX: DestReg = X86::EBX; break;
12740 case X86::SI: DestReg = X86::ESI; break;
12741 case X86::DI: DestReg = X86::EDI; break;
12742 case X86::BP: DestReg = X86::EBP; break;
12743 case X86::SP: DestReg = X86::ESP; break;
12744 }
12745 if (DestReg) {
12746 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012747 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012748 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012749 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012750 unsigned DestReg = 0;
12751 switch (Res.first) {
12752 default: break;
12753 case X86::AX: DestReg = X86::RAX; break;
12754 case X86::DX: DestReg = X86::RDX; break;
12755 case X86::CX: DestReg = X86::RCX; break;
12756 case X86::BX: DestReg = X86::RBX; break;
12757 case X86::SI: DestReg = X86::RSI; break;
12758 case X86::DI: DestReg = X86::RDI; break;
12759 case X86::BP: DestReg = X86::RBP; break;
12760 case X86::SP: DestReg = X86::RSP; break;
12761 }
12762 if (DestReg) {
12763 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012764 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012765 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012766 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012767 } else if (Res.second == X86::FR32RegisterClass ||
12768 Res.second == X86::FR64RegisterClass ||
12769 Res.second == X86::VR128RegisterClass) {
12770 // Handle references to XMM physical registers that got mapped into the
12771 // wrong class. This can happen with constraints like {xmm0} where the
12772 // target independent register mapper will just pick the first match it can
12773 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012774 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012775 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012776 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012777 Res.second = X86::FR64RegisterClass;
12778 else if (X86::VR128RegisterClass->hasType(VT))
12779 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012780 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012781
Chris Lattnerf76d1802006-07-31 23:26:50 +000012782 return Res;
12783}