blob: 722202dc2d1f664e49738e4dcbe1d7884020d2a0 [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
David Greenea20244d2011-03-02 17:23:43 +00003176 if ((NumElems != 2 && NumElems != 4)
3177 || N->getValueType(0).getSizeInBits() > 128)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003178 return false;
3179
Evan Chengc5cdff22006-04-07 21:53:05 +00003180 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003181 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003182 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 for (unsigned i = 0; i < NumElems/2; ++i)
3185 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003186 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003187
3188 return true;
3189}
3190
Evan Cheng0038e592006-03-28 00:39:58 +00003191/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3192/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003193static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003194 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003196 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003197 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003198
David Greenea20244d2011-03-02 17:23:43 +00003199 // Handle vector lengths > 128 bits. Define a "section" as a set of
3200 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3201 // sections.
3202 unsigned NumSections = VT.getSizeInBits() / 128;
3203 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3204 unsigned NumSectionElts = NumElts / NumSections;
3205
3206 unsigned Start = 0;
3207 unsigned End = NumSectionElts;
3208 for (unsigned s = 0; s < NumSections; ++s) {
3209 for (unsigned i = Start, j = s * NumSectionElts;
3210 i != End;
3211 i += 2, ++j) {
3212 int BitI = Mask[i];
3213 int BitI1 = Mask[i+1];
3214 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003215 return false;
David Greenea20244d2011-03-02 17:23:43 +00003216 if (V2IsSplat) {
3217 if (!isUndefOrEqual(BitI1, NumElts))
3218 return false;
3219 } else {
3220 if (!isUndefOrEqual(BitI1, j + NumElts))
3221 return false;
3222 }
Evan Cheng39623da2006-04-20 08:58:49 +00003223 }
David Greenea20244d2011-03-02 17:23:43 +00003224 // Process the next 128 bits.
3225 Start += NumSectionElts;
3226 End += NumSectionElts;
Evan Cheng0038e592006-03-28 00:39:58 +00003227 }
David Greenea20244d2011-03-02 17:23:43 +00003228
Evan Cheng0038e592006-03-28 00:39:58 +00003229 return true;
3230}
3231
Nate Begeman9008ca62009-04-27 18:41:29 +00003232bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3233 SmallVector<int, 8> M;
3234 N->getMask(M);
3235 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003236}
3237
Evan Cheng4fcb9222006-03-28 02:43:26 +00003238/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3239/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003240static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003241 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003243 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003244 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003245
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3247 int BitI = Mask[i];
3248 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003249 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003250 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003251 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003252 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003253 return false;
3254 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003255 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003256 return false;
3257 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003258 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003259 return true;
3260}
3261
Nate Begeman9008ca62009-04-27 18:41:29 +00003262bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3263 SmallVector<int, 8> M;
3264 N->getMask(M);
3265 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003266}
3267
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003268/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3269/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3270/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003271static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003272 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003273 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003274 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003275
David Greenea20244d2011-03-02 17:23:43 +00003276 // Handle vector lengths > 128 bits. Define a "section" as a set of
3277 // 128 bits. AVX defines UNPCK* to operate independently on 128-bit
3278 // sections.
3279 unsigned NumSections = VT.getSizeInBits() / 128;
3280 if (NumSections == 0 ) NumSections = 1; // Handle MMX
3281 unsigned NumSectionElts = NumElems / NumSections;
3282
3283 for (unsigned s = 0; s < NumSections; ++s) {
3284 for (unsigned i = s * NumSectionElts, j = s * NumSectionElts;
3285 i != NumSectionElts * (s + 1);
3286 i += 2, ++j) {
3287 int BitI = Mask[i];
3288 int BitI1 = Mask[i+1];
3289
3290 if (!isUndefOrEqual(BitI, j))
3291 return false;
3292 if (!isUndefOrEqual(BitI1, j))
3293 return false;
3294 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003295 }
David Greenea20244d2011-03-02 17:23:43 +00003296
Rafael Espindola15684b22009-04-24 12:40:33 +00003297 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003298}
3299
Nate Begeman9008ca62009-04-27 18:41:29 +00003300bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3301 SmallVector<int, 8> M;
3302 N->getMask(M);
3303 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3304}
3305
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003306/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3307/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3308/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003309static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003311 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3312 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003313
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3315 int BitI = Mask[i];
3316 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003317 if (!isUndefOrEqual(BitI, j))
3318 return false;
3319 if (!isUndefOrEqual(BitI1, j))
3320 return false;
3321 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003322 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003323}
3324
Nate Begeman9008ca62009-04-27 18:41:29 +00003325bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3326 SmallVector<int, 8> M;
3327 N->getMask(M);
3328 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3329}
3330
Evan Cheng017dcc62006-04-21 01:05:10 +00003331/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3332/// specifies a shuffle of elements that is suitable for input to MOVSS,
3333/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003334static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003335 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003336 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003337
3338 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003339
Nate Begeman9008ca62009-04-27 18:41:29 +00003340 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003341 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 for (int i = 1; i < NumElts; ++i)
3344 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003345 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003346
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003347 return true;
3348}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003349
Nate Begeman9008ca62009-04-27 18:41:29 +00003350bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3351 SmallVector<int, 8> M;
3352 N->getMask(M);
3353 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003354}
3355
Evan Cheng017dcc62006-04-21 01:05:10 +00003356/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3357/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003358/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003359static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003360 bool V2IsSplat = false, bool V2IsUndef = false) {
3361 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003362 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003363 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003366 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003367
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 for (int i = 1; i < NumOps; ++i)
3369 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3370 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3371 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003372 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003373
Evan Cheng39623da2006-04-20 08:58:49 +00003374 return true;
3375}
3376
Nate Begeman9008ca62009-04-27 18:41:29 +00003377static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003378 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 SmallVector<int, 8> M;
3380 N->getMask(M);
3381 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003382}
3383
Evan Chengd9539472006-04-14 21:59:03 +00003384/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3385/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003386bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3387 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003388 return false;
3389
3390 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003391 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 int Elt = N->getMaskElt(i);
3393 if (Elt >= 0 && Elt != 1)
3394 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003395 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003396
3397 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003398 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 int Elt = N->getMaskElt(i);
3400 if (Elt >= 0 && Elt != 3)
3401 return false;
3402 if (Elt == 3)
3403 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003404 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003405 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003407 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003408}
3409
3410/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3411/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003412bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3413 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003414 return false;
3415
3416 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 for (unsigned i = 0; i < 2; ++i)
3418 if (N->getMaskElt(i) > 0)
3419 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003420
3421 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003422 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 int Elt = N->getMaskElt(i);
3424 if (Elt >= 0 && Elt != 2)
3425 return false;
3426 if (Elt == 2)
3427 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003428 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003430 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003431}
3432
Evan Cheng0b457f02008-09-25 20:50:48 +00003433/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3434/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003435bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3436 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003437
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 for (int i = 0; i < e; ++i)
3439 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003440 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 for (int i = 0; i < e; ++i)
3442 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003443 return false;
3444 return true;
3445}
3446
David Greenec38a03e2011-02-03 15:50:00 +00003447/// isVEXTRACTF128Index - Return true if the specified
3448/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3449/// suitable for input to VEXTRACTF128.
3450bool X86::isVEXTRACTF128Index(SDNode *N) {
3451 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3452 return false;
3453
3454 // The index should be aligned on a 128-bit boundary.
3455 uint64_t Index =
3456 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3457
3458 unsigned VL = N->getValueType(0).getVectorNumElements();
3459 unsigned VBits = N->getValueType(0).getSizeInBits();
3460 unsigned ElSize = VBits / VL;
3461 bool Result = (Index * ElSize) % 128 == 0;
3462
3463 return Result;
3464}
3465
David Greeneccacdc12011-02-04 16:08:29 +00003466/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3467/// operand specifies a subvector insert that is suitable for input to
3468/// VINSERTF128.
3469bool X86::isVINSERTF128Index(SDNode *N) {
3470 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3471 return false;
3472
3473 // The index should be aligned on a 128-bit boundary.
3474 uint64_t Index =
3475 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3476
3477 unsigned VL = N->getValueType(0).getVectorNumElements();
3478 unsigned VBits = N->getValueType(0).getSizeInBits();
3479 unsigned ElSize = VBits / VL;
3480 bool Result = (Index * ElSize) % 128 == 0;
3481
3482 return Result;
3483}
3484
Evan Cheng63d33002006-03-22 08:01:21 +00003485/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003486/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003487unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3489 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3490
Evan Chengb9df0ca2006-03-22 02:53:00 +00003491 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3492 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 for (int i = 0; i < NumOperands; ++i) {
3494 int Val = SVOp->getMaskElt(NumOperands-i-1);
3495 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003496 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003497 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003498 if (i != NumOperands - 1)
3499 Mask <<= Shift;
3500 }
Evan Cheng63d33002006-03-22 08:01:21 +00003501 return Mask;
3502}
3503
Evan Cheng506d3df2006-03-29 23:07:14 +00003504/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003505/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003506unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003507 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003508 unsigned Mask = 0;
3509 // 8 nodes, but we only care about the last 4.
3510 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003511 int Val = SVOp->getMaskElt(i);
3512 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003513 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003514 if (i != 4)
3515 Mask <<= 2;
3516 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003517 return Mask;
3518}
3519
3520/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003521/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003522unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003523 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003524 unsigned Mask = 0;
3525 // 8 nodes, but we only care about the first 4.
3526 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003527 int Val = SVOp->getMaskElt(i);
3528 if (Val >= 0)
3529 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003530 if (i != 0)
3531 Mask <<= 2;
3532 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003533 return Mask;
3534}
3535
Nate Begemana09008b2009-10-19 02:17:23 +00003536/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3537/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3538unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3539 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3540 EVT VVT = N->getValueType(0);
3541 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3542 int Val = 0;
3543
3544 unsigned i, e;
3545 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3546 Val = SVOp->getMaskElt(i);
3547 if (Val >= 0)
3548 break;
3549 }
3550 return (Val - i) * EltSize;
3551}
3552
David Greenec38a03e2011-02-03 15:50:00 +00003553/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3554/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3555/// instructions.
3556unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3557 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3558 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3559
3560 uint64_t Index =
3561 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3562
3563 EVT VecVT = N->getOperand(0).getValueType();
3564 EVT ElVT = VecVT.getVectorElementType();
3565
3566 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3567
3568 return Index / NumElemsPerChunk;
3569}
3570
David Greeneccacdc12011-02-04 16:08:29 +00003571/// getInsertVINSERTF128Immediate - Return the appropriate immediate
3572/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
3573/// instructions.
3574unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
3575 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3576 llvm_unreachable("Illegal insert subvector for VINSERTF128");
3577
3578 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00003579 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00003580
3581 EVT VecVT = N->getValueType(0);
3582 EVT ElVT = VecVT.getVectorElementType();
3583
3584 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3585
3586 return Index / NumElemsPerChunk;
3587}
3588
Evan Cheng37b73872009-07-30 08:33:02 +00003589/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3590/// constant +0.0.
3591bool X86::isZeroNode(SDValue Elt) {
3592 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003593 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003594 (isa<ConstantFPSDNode>(Elt) &&
3595 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3596}
3597
Nate Begeman9008ca62009-04-27 18:41:29 +00003598/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3599/// their permute mask.
3600static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3601 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003602 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003603 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003604 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003605
Nate Begeman5a5ca152009-04-29 05:20:52 +00003606 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 int idx = SVOp->getMaskElt(i);
3608 if (idx < 0)
3609 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003610 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003612 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003614 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3616 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003617}
3618
Evan Cheng779ccea2007-12-07 21:30:01 +00003619/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3620/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003621static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003622 unsigned NumElems = VT.getVectorNumElements();
3623 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003624 int idx = Mask[i];
3625 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003626 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003627 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003628 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003629 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003630 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003631 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003632}
3633
Evan Cheng533a0aa2006-04-19 20:35:22 +00003634/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3635/// match movhlps. The lower half elements should come from upper half of
3636/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003637/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003638static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3639 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003640 return false;
3641 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003642 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003643 return false;
3644 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003645 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003646 return false;
3647 return true;
3648}
3649
Evan Cheng5ced1d82006-04-06 23:23:56 +00003650/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003651/// is promoted to a vector. It also returns the LoadSDNode by reference if
3652/// required.
3653static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003654 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3655 return false;
3656 N = N->getOperand(0).getNode();
3657 if (!ISD::isNON_EXTLoad(N))
3658 return false;
3659 if (LD)
3660 *LD = cast<LoadSDNode>(N);
3661 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003662}
3663
Evan Cheng533a0aa2006-04-19 20:35:22 +00003664/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3665/// match movlp{s|d}. The lower half elements should come from lower half of
3666/// V1 (and in order), and the upper half elements should come from the upper
3667/// half of V2 (and in order). And since V1 will become the source of the
3668/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003669static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3670 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003671 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003672 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003673 // Is V2 is a vector load, don't do this transformation. We will try to use
3674 // load folding shufps op.
3675 if (ISD::isNON_EXTLoad(V2))
3676 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003677
Nate Begeman5a5ca152009-04-29 05:20:52 +00003678 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003679
Evan Cheng533a0aa2006-04-19 20:35:22 +00003680 if (NumElems != 2 && NumElems != 4)
3681 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003682 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003683 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003684 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003685 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003686 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003687 return false;
3688 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003689}
3690
Evan Cheng39623da2006-04-20 08:58:49 +00003691/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3692/// all the same.
3693static bool isSplatVector(SDNode *N) {
3694 if (N->getOpcode() != ISD::BUILD_VECTOR)
3695 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003696
Dan Gohman475871a2008-07-27 21:46:04 +00003697 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003698 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3699 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003700 return false;
3701 return true;
3702}
3703
Evan Cheng213d2cf2007-05-17 18:45:50 +00003704/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003705/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003706/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003707static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003708 SDValue V1 = N->getOperand(0);
3709 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003710 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3711 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003713 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003714 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003715 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3716 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003717 if (Opc != ISD::BUILD_VECTOR ||
3718 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003719 return false;
3720 } else if (Idx >= 0) {
3721 unsigned Opc = V1.getOpcode();
3722 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3723 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003724 if (Opc != ISD::BUILD_VECTOR ||
3725 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003726 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003727 }
3728 }
3729 return true;
3730}
3731
3732/// getZeroVector - Returns a vector of specified type with all zero elements.
3733///
Owen Andersone50ed302009-08-10 22:56:29 +00003734static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003735 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003736 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003737
Dale Johannesen0488fb62010-09-30 23:57:10 +00003738 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003739 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003740 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003741 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003742 if (HasSSE2) { // SSE2
3743 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3744 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3745 } else { // SSE1
3746 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3747 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3748 }
3749 } else if (VT.getSizeInBits() == 256) { // AVX
3750 // 256-bit logic and arithmetic instructions in AVX are
3751 // all floating-point, no support for integer ops. Default
3752 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003754 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3755 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003756 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003757 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003758}
3759
Chris Lattner8a594482007-11-25 00:24:49 +00003760/// getOnesVector - Returns a vector of specified type with all bits set.
3761///
Owen Andersone50ed302009-08-10 22:56:29 +00003762static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003763 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003764
Chris Lattner8a594482007-11-25 00:24:49 +00003765 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3766 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003768 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003769 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003770 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003771}
3772
3773
Evan Cheng39623da2006-04-20 08:58:49 +00003774/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3775/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003776static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003777 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003778 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003779
Evan Cheng39623da2006-04-20 08:58:49 +00003780 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003781 SmallVector<int, 8> MaskVec;
3782 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003783
Nate Begeman5a5ca152009-04-29 05:20:52 +00003784 for (unsigned i = 0; i != NumElems; ++i) {
3785 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 MaskVec[i] = NumElems;
3787 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003788 }
Evan Cheng39623da2006-04-20 08:58:49 +00003789 }
Evan Cheng39623da2006-04-20 08:58:49 +00003790 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3792 SVOp->getOperand(1), &MaskVec[0]);
3793 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003794}
3795
Evan Cheng017dcc62006-04-21 01:05:10 +00003796/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3797/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003798static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 SDValue V2) {
3800 unsigned NumElems = VT.getVectorNumElements();
3801 SmallVector<int, 8> Mask;
3802 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003803 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 Mask.push_back(i);
3805 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003806}
3807
Nate Begeman9008ca62009-04-27 18:41:29 +00003808/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003809static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003810 SDValue V2) {
3811 unsigned NumElems = VT.getVectorNumElements();
3812 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003813 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 Mask.push_back(i);
3815 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003816 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003818}
3819
Nate Begeman9008ca62009-04-27 18:41:29 +00003820/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003821static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 SDValue V2) {
3823 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003824 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003825 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003826 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 Mask.push_back(i + Half);
3828 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003829 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003831}
3832
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003833/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3834static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003836 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 DebugLoc dl = SV->getDebugLoc();
3838 SDValue V1 = SV->getOperand(0);
3839 int NumElems = VT.getVectorNumElements();
3840 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003841
Nate Begeman9008ca62009-04-27 18:41:29 +00003842 // unpack elements to the correct location
3843 while (NumElems > 4) {
3844 if (EltNo < NumElems/2) {
3845 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3846 } else {
3847 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3848 EltNo -= NumElems/2;
3849 }
3850 NumElems >>= 1;
3851 }
Eric Christopherfd179292009-08-27 18:07:15 +00003852
Nate Begeman9008ca62009-04-27 18:41:29 +00003853 // Perform the splat.
3854 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003855 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003857 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003858}
3859
Evan Chengba05f722006-04-21 23:03:30 +00003860/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003861/// vector of zero or undef vector. This produces a shuffle where the low
3862/// element of V2 is swizzled into the zero/undef vector, landing at element
3863/// 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 +00003864static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003865 bool isZero, bool HasSSE2,
3866 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003867 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003868 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3870 unsigned NumElems = VT.getVectorNumElements();
3871 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003872 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 // If this is the insertion idx, put the low elt of V2 here.
3874 MaskVec.push_back(i == Idx ? NumElems : i);
3875 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003876}
3877
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003878/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3879/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003880SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3881 unsigned Depth) {
3882 if (Depth == 6)
3883 return SDValue(); // Limit search depth.
3884
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003885 SDValue V = SDValue(N, 0);
3886 EVT VT = V.getValueType();
3887 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003888
3889 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3890 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3891 Index = SV->getMaskElt(Index);
3892
3893 if (Index < 0)
3894 return DAG.getUNDEF(VT.getVectorElementType());
3895
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003896 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003897 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003898 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003899 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003900
3901 // Recurse into target specific vector shuffles to find scalars.
3902 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003903 int NumElems = VT.getVectorNumElements();
3904 SmallVector<unsigned, 16> ShuffleMask;
3905 SDValue ImmN;
3906
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003907 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003908 case X86ISD::SHUFPS:
3909 case X86ISD::SHUFPD:
3910 ImmN = N->getOperand(N->getNumOperands()-1);
3911 DecodeSHUFPSMask(NumElems,
3912 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3913 ShuffleMask);
3914 break;
3915 case X86ISD::PUNPCKHBW:
3916 case X86ISD::PUNPCKHWD:
3917 case X86ISD::PUNPCKHDQ:
3918 case X86ISD::PUNPCKHQDQ:
3919 DecodePUNPCKHMask(NumElems, ShuffleMask);
3920 break;
3921 case X86ISD::UNPCKHPS:
3922 case X86ISD::UNPCKHPD:
3923 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3924 break;
3925 case X86ISD::PUNPCKLBW:
3926 case X86ISD::PUNPCKLWD:
3927 case X86ISD::PUNPCKLDQ:
3928 case X86ISD::PUNPCKLQDQ:
David Greenec4db4e52011-02-28 19:06:56 +00003929 DecodePUNPCKLMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003930 break;
3931 case X86ISD::UNPCKLPS:
3932 case X86ISD::UNPCKLPD:
David Greenec4db4e52011-02-28 19:06:56 +00003933 case X86ISD::VUNPCKLPS:
3934 case X86ISD::VUNPCKLPD:
3935 case X86ISD::VUNPCKLPSY:
3936 case X86ISD::VUNPCKLPDY:
3937 DecodeUNPCKLPMask(VT, ShuffleMask);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003938 break;
3939 case X86ISD::MOVHLPS:
3940 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3941 break;
3942 case X86ISD::MOVLHPS:
3943 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3944 break;
3945 case X86ISD::PSHUFD:
3946 ImmN = N->getOperand(N->getNumOperands()-1);
3947 DecodePSHUFMask(NumElems,
3948 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3949 ShuffleMask);
3950 break;
3951 case X86ISD::PSHUFHW:
3952 ImmN = N->getOperand(N->getNumOperands()-1);
3953 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3954 ShuffleMask);
3955 break;
3956 case X86ISD::PSHUFLW:
3957 ImmN = N->getOperand(N->getNumOperands()-1);
3958 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3959 ShuffleMask);
3960 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003961 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003962 case X86ISD::MOVSD: {
3963 // The index 0 always comes from the first element of the second source,
3964 // this is why MOVSS and MOVSD are used in the first place. The other
3965 // elements come from the other positions of the first source vector.
3966 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003967 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3968 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003969 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003970 default:
3971 assert("not implemented for target shuffle node");
3972 return SDValue();
3973 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003974
3975 Index = ShuffleMask[Index];
3976 if (Index < 0)
3977 return DAG.getUNDEF(VT.getVectorElementType());
3978
3979 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3980 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3981 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003982 }
3983
3984 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003985 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003986 V = V.getOperand(0);
3987 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003988 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003989
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003990 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003991 return SDValue();
3992 }
3993
3994 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3995 return (Index == 0) ? V.getOperand(0)
3996 : DAG.getUNDEF(VT.getVectorElementType());
3997
3998 if (V.getOpcode() == ISD::BUILD_VECTOR)
3999 return V.getOperand(Index);
4000
4001 return SDValue();
4002}
4003
4004/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4005/// shuffle operation which come from a consecutively from a zero. The
4006/// search can start in two diferent directions, from left or right.
4007static
4008unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
4009 bool ZerosFromLeft, SelectionDAG &DAG) {
4010 int i = 0;
4011
4012 while (i < NumElems) {
4013 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004014 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004015 if (!(Elt.getNode() &&
4016 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4017 break;
4018 ++i;
4019 }
4020
4021 return i;
4022}
4023
4024/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
4025/// MaskE correspond consecutively to elements from one of the vector operands,
4026/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4027static
4028bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
4029 int OpIdx, int NumElems, unsigned &OpNum) {
4030 bool SeenV1 = false;
4031 bool SeenV2 = false;
4032
4033 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
4034 int Idx = SVOp->getMaskElt(i);
4035 // Ignore undef indicies
4036 if (Idx < 0)
4037 continue;
4038
4039 if (Idx < NumElems)
4040 SeenV1 = true;
4041 else
4042 SeenV2 = true;
4043
4044 // Only accept consecutive elements from the same vector
4045 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4046 return false;
4047 }
4048
4049 OpNum = SeenV1 ? 0 : 1;
4050 return true;
4051}
4052
4053/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4054/// logical left shift of a vector.
4055static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4056 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4057 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4058 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4059 false /* check zeros from right */, DAG);
4060 unsigned OpSrc;
4061
4062 if (!NumZeros)
4063 return false;
4064
4065 // Considering the elements in the mask that are not consecutive zeros,
4066 // check if they consecutively come from only one of the source vectors.
4067 //
4068 // V1 = {X, A, B, C} 0
4069 // \ \ \ /
4070 // vector_shuffle V1, V2 <1, 2, 3, X>
4071 //
4072 if (!isShuffleMaskConsecutive(SVOp,
4073 0, // Mask Start Index
4074 NumElems-NumZeros-1, // Mask End Index
4075 NumZeros, // Where to start looking in the src vector
4076 NumElems, // Number of elements in vector
4077 OpSrc)) // Which source operand ?
4078 return false;
4079
4080 isLeft = false;
4081 ShAmt = NumZeros;
4082 ShVal = SVOp->getOperand(OpSrc);
4083 return true;
4084}
4085
4086/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4087/// logical left shift of a vector.
4088static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4089 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4090 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4091 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4092 true /* check zeros from left */, DAG);
4093 unsigned OpSrc;
4094
4095 if (!NumZeros)
4096 return false;
4097
4098 // Considering the elements in the mask that are not consecutive zeros,
4099 // check if they consecutively come from only one of the source vectors.
4100 //
4101 // 0 { A, B, X, X } = V2
4102 // / \ / /
4103 // vector_shuffle V1, V2 <X, X, 4, 5>
4104 //
4105 if (!isShuffleMaskConsecutive(SVOp,
4106 NumZeros, // Mask Start Index
4107 NumElems-1, // Mask End Index
4108 0, // Where to start looking in the src vector
4109 NumElems, // Number of elements in vector
4110 OpSrc)) // Which source operand ?
4111 return false;
4112
4113 isLeft = true;
4114 ShAmt = NumZeros;
4115 ShVal = SVOp->getOperand(OpSrc);
4116 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004117}
4118
4119/// isVectorShift - Returns true if the shuffle can be implemented as a
4120/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004121static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004122 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004123 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4124 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4125 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004126
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004127 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004128}
4129
Evan Chengc78d3b42006-04-24 18:01:45 +00004130/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4131///
Dan Gohman475871a2008-07-27 21:46:04 +00004132static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004133 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004134 SelectionDAG &DAG,
4135 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004136 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004137 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004138
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004139 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004140 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004141 bool First = true;
4142 for (unsigned i = 0; i < 16; ++i) {
4143 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4144 if (ThisIsNonZero && First) {
4145 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004146 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004147 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004149 First = false;
4150 }
4151
4152 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004153 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004154 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4155 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004156 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004158 }
4159 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4161 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4162 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004163 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004165 } else
4166 ThisElt = LastElt;
4167
Gabor Greifba36cb52008-08-28 21:40:38 +00004168 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004170 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004171 }
4172 }
4173
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004174 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004175}
4176
Bill Wendlinga348c562007-03-22 18:42:45 +00004177/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004178///
Dan Gohman475871a2008-07-27 21:46:04 +00004179static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004180 unsigned NumNonZero, unsigned NumZero,
4181 SelectionDAG &DAG,
4182 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004183 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004184 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004185
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004186 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004187 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004188 bool First = true;
4189 for (unsigned i = 0; i < 8; ++i) {
4190 bool isNonZero = (NonZeros & (1 << i)) != 0;
4191 if (isNonZero) {
4192 if (First) {
4193 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004194 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004195 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004197 First = false;
4198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004199 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004201 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004202 }
4203 }
4204
4205 return V;
4206}
4207
Evan Chengf26ffe92008-05-29 08:22:04 +00004208/// getVShift - Return a vector logical shift node.
4209///
Owen Andersone50ed302009-08-10 22:56:29 +00004210static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 unsigned NumBits, SelectionDAG &DAG,
4212 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004213 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00004214 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004215 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4216 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004217 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004218 DAG.getConstant(NumBits,
4219 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004220}
4221
Dan Gohman475871a2008-07-27 21:46:04 +00004222SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004223X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004224 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004225
Evan Chengc3630942009-12-09 21:00:30 +00004226 // Check if the scalar load can be widened into a vector load. And if
4227 // the address is "base + cst" see if the cst can be "absorbed" into
4228 // the shuffle mask.
4229 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4230 SDValue Ptr = LD->getBasePtr();
4231 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4232 return SDValue();
4233 EVT PVT = LD->getValueType(0);
4234 if (PVT != MVT::i32 && PVT != MVT::f32)
4235 return SDValue();
4236
4237 int FI = -1;
4238 int64_t Offset = 0;
4239 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4240 FI = FINode->getIndex();
4241 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004242 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004243 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4244 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4245 Offset = Ptr.getConstantOperandVal(1);
4246 Ptr = Ptr.getOperand(0);
4247 } else {
4248 return SDValue();
4249 }
4250
4251 SDValue Chain = LD->getChain();
4252 // Make sure the stack object alignment is at least 16.
4253 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4254 if (DAG.InferPtrAlignment(Ptr) < 16) {
4255 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004256 // Can't change the alignment. FIXME: It's possible to compute
4257 // the exact stack offset and reference FI + adjust offset instead.
4258 // If someone *really* cares about this. That's the way to implement it.
4259 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004260 } else {
4261 MFI->setObjectAlignment(FI, 16);
4262 }
4263 }
4264
4265 // (Offset % 16) must be multiple of 4. Then address is then
4266 // Ptr + (Offset & ~15).
4267 if (Offset < 0)
4268 return SDValue();
4269 if ((Offset % 16) & 3)
4270 return SDValue();
4271 int64_t StartOffset = Offset & ~15;
4272 if (StartOffset)
4273 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4274 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4275
4276 int EltNo = (Offset - StartOffset) >> 2;
4277 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4278 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004279 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4280 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004281 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004282 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004283 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4284 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004285 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004286 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004287 }
4288
4289 return SDValue();
4290}
4291
Michael J. Spencerec38de22010-10-10 22:04:20 +00004292/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4293/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004294/// load which has the same value as a build_vector whose operands are 'elts'.
4295///
4296/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004297///
Nate Begeman1449f292010-03-24 22:19:06 +00004298/// FIXME: we'd also like to handle the case where the last elements are zero
4299/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4300/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004301static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004302 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004303 EVT EltVT = VT.getVectorElementType();
4304 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004305
Nate Begemanfdea31a2010-03-24 20:49:50 +00004306 LoadSDNode *LDBase = NULL;
4307 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004308
Nate Begeman1449f292010-03-24 22:19:06 +00004309 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004310 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004311 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004312 for (unsigned i = 0; i < NumElems; ++i) {
4313 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004314
Nate Begemanfdea31a2010-03-24 20:49:50 +00004315 if (!Elt.getNode() ||
4316 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4317 return SDValue();
4318 if (!LDBase) {
4319 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4320 return SDValue();
4321 LDBase = cast<LoadSDNode>(Elt.getNode());
4322 LastLoadedElt = i;
4323 continue;
4324 }
4325 if (Elt.getOpcode() == ISD::UNDEF)
4326 continue;
4327
4328 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4329 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4330 return SDValue();
4331 LastLoadedElt = i;
4332 }
Nate Begeman1449f292010-03-24 22:19:06 +00004333
4334 // If we have found an entire vector of loads and undefs, then return a large
4335 // load of the entire vector width starting at the base pointer. If we found
4336 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004337 if (LastLoadedElt == NumElems - 1) {
4338 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004339 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004340 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004341 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004342 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004343 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004344 LDBase->isVolatile(), LDBase->isNonTemporal(),
4345 LDBase->getAlignment());
4346 } else if (NumElems == 4 && LastLoadedElt == 1) {
4347 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4348 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004349 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4350 Ops, 2, MVT::i32,
4351 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004352 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004353 }
4354 return SDValue();
4355}
4356
Evan Chengc3630942009-12-09 21:00:30 +00004357SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004358X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004359 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00004360
David Greenef125a292011-02-08 19:04:41 +00004361 EVT VT = Op.getValueType();
4362 EVT ExtVT = VT.getVectorElementType();
4363
4364 unsigned NumElems = Op.getNumOperands();
4365
4366 // For AVX-length vectors, build the individual 128-bit pieces and
4367 // use shuffles to put them in place.
Owen Anderson95771af2011-02-25 21:41:48 +00004368 if (VT.getSizeInBits() > 256 &&
4369 Subtarget->hasAVX() &&
David Greenef125a292011-02-08 19:04:41 +00004370 !ISD::isBuildVectorAllZeros(Op.getNode())) {
4371 SmallVector<SDValue, 8> V;
4372 V.resize(NumElems);
4373 for (unsigned i = 0; i < NumElems; ++i) {
4374 V[i] = Op.getOperand(i);
4375 }
Owen Anderson95771af2011-02-25 21:41:48 +00004376
David Greenef125a292011-02-08 19:04:41 +00004377 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
4378
4379 // Build the lower subvector.
4380 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
4381 // Build the upper subvector.
4382 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
4383 NumElems/2);
4384
4385 return ConcatVectors(Lower, Upper, DAG);
4386 }
4387
Chris Lattner6e80e442010-08-28 17:15:43 +00004388 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4389 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004390 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4391 // is present, so AllOnes is ignored.
4392 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4393 (Op.getValueType().getSizeInBits() != 256 &&
4394 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004395 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004396 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4397 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004398 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004399 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400
Gabor Greifba36cb52008-08-28 21:40:38 +00004401 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004402 return getOnesVector(Op.getValueType(), DAG, dl);
4403 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004404 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004405
Owen Andersone50ed302009-08-10 22:56:29 +00004406 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004407
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408 unsigned NumZero = 0;
4409 unsigned NumNonZero = 0;
4410 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004411 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004412 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004414 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004415 if (Elt.getOpcode() == ISD::UNDEF)
4416 continue;
4417 Values.insert(Elt);
4418 if (Elt.getOpcode() != ISD::Constant &&
4419 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004420 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004421 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004422 NumZero++;
4423 else {
4424 NonZeros |= (1 << i);
4425 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426 }
4427 }
4428
Chris Lattner97a2a562010-08-26 05:24:29 +00004429 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4430 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004431 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004432
Chris Lattner67f453a2008-03-09 05:42:06 +00004433 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004434 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004435 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004436 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Chris Lattner62098042008-03-09 01:05:04 +00004438 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4439 // the value are obviously zero, truncate the value to i32 and do the
4440 // insertion that way. Only do this if the value is non-constant or if the
4441 // value is a constant being inserted into element 0. It is cheaper to do
4442 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004443 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004444 (!IsAllConstants || Idx == 0)) {
4445 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004446 // Handle SSE only.
4447 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4448 EVT VecVT = MVT::v4i32;
4449 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004450
Chris Lattner62098042008-03-09 01:05:04 +00004451 // Truncate the value (which may itself be a constant) to i32, and
4452 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004453 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004454 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004455 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4456 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004457
Chris Lattner62098042008-03-09 01:05:04 +00004458 // Now we have our 32-bit value zero extended in the low element of
4459 // a vector. If Idx != 0, swizzle it into place.
4460 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004461 SmallVector<int, 4> Mask;
4462 Mask.push_back(Idx);
4463 for (unsigned i = 1; i != VecElts; ++i)
4464 Mask.push_back(i);
4465 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004466 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004468 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004469 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004470 }
4471 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004472
Chris Lattner19f79692008-03-08 22:59:52 +00004473 // If we have a constant or non-constant insertion into the low element of
4474 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4475 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004476 // depending on what the source datatype is.
4477 if (Idx == 0) {
4478 if (NumZero == 0) {
4479 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004480 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4481 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004482 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4483 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4484 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4485 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004486 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4487 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004488 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4489 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004490 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4491 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4492 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004493 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004494 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004495 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004496
4497 // Is it a vector logical left shift?
4498 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004499 X86::isZeroNode(Op.getOperand(0)) &&
4500 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004501 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004502 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004503 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004504 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004505 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004506 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004507
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004508 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004509 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004510
Chris Lattner19f79692008-03-08 22:59:52 +00004511 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4512 // is a non-constant being inserted into an element other than the low one,
4513 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4514 // movd/movss) to move this into the low element, then shuffle it into
4515 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004516 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004517 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004518
Evan Cheng0db9fe62006-04-25 20:13:52 +00004519 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004520 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4521 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004522 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004523 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004524 MaskVec.push_back(i == Idx ? 0 : 1);
4525 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004526 }
4527 }
4528
Chris Lattner67f453a2008-03-09 05:42:06 +00004529 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004530 if (Values.size() == 1) {
4531 if (EVTBits == 32) {
4532 // Instead of a shuffle like this:
4533 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4534 // Check if it's possible to issue this instead.
4535 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4536 unsigned Idx = CountTrailingZeros_32(NonZeros);
4537 SDValue Item = Op.getOperand(Idx);
4538 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4539 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4540 }
Dan Gohman475871a2008-07-27 21:46:04 +00004541 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004542 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004543
Dan Gohmana3941172007-07-24 22:55:08 +00004544 // A vector full of immediates; various special cases are already
4545 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004546 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004547 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004548
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004549 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004550 if (EVTBits == 64) {
4551 if (NumNonZero == 1) {
4552 // One half is zero or undef.
4553 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004554 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004555 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004556 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4557 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004558 }
Dan Gohman475871a2008-07-27 21:46:04 +00004559 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004560 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561
4562 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004563 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004564 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004565 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004566 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567 }
4568
Bill Wendling826f36f2007-03-28 00:57:11 +00004569 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004570 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004571 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004572 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004573 }
4574
4575 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004576 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004577 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004578 if (NumElems == 4 && NumZero > 0) {
4579 for (unsigned i = 0; i < 4; ++i) {
4580 bool isZero = !(NonZeros & (1 << i));
4581 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004582 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004583 else
Dale Johannesenace16102009-02-03 19:33:06 +00004584 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004585 }
4586
4587 for (unsigned i = 0; i < 2; ++i) {
4588 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4589 default: break;
4590 case 0:
4591 V[i] = V[i*2]; // Must be a zero vector.
4592 break;
4593 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004594 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595 break;
4596 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004597 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 break;
4599 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004600 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 break;
4602 }
4603 }
4604
Nate Begeman9008ca62009-04-27 18:41:29 +00004605 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004606 bool Reverse = (NonZeros & 0x3) == 2;
4607 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4610 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4612 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 }
4614
Nate Begemanfdea31a2010-03-24 20:49:50 +00004615 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4616 // Check for a build vector of consecutive loads.
4617 for (unsigned i = 0; i < NumElems; ++i)
4618 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004619
Nate Begemanfdea31a2010-03-24 20:49:50 +00004620 // Check for elements which are consecutive loads.
4621 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4622 if (LD.getNode())
4623 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004624
4625 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004626 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004627 SDValue Result;
4628 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4629 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4630 else
4631 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004632
Chris Lattner24faf612010-08-28 17:59:08 +00004633 for (unsigned i = 1; i < NumElems; ++i) {
4634 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4635 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004636 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004637 }
4638 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004639 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004640
Chris Lattner6e80e442010-08-28 17:15:43 +00004641 // Otherwise, expand into a number of unpckl*, start by extending each of
4642 // our (non-undef) elements to the full vector width with the element in the
4643 // bottom slot of the vector (which generates no code for SSE).
4644 for (unsigned i = 0; i < NumElems; ++i) {
4645 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4646 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4647 else
4648 V[i] = DAG.getUNDEF(VT);
4649 }
4650
4651 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4653 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4654 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004655 unsigned EltStride = NumElems >> 1;
4656 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004657 for (unsigned i = 0; i < EltStride; ++i) {
4658 // If V[i+EltStride] is undef and this is the first round of mixing,
4659 // then it is safe to just drop this shuffle: V[i] is already in the
4660 // right place, the one element (since it's the first round) being
4661 // inserted as undef can be dropped. This isn't safe for successive
4662 // rounds because they will permute elements within both vectors.
4663 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4664 EltStride == NumElems/2)
4665 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004666
Chris Lattner6e80e442010-08-28 17:15:43 +00004667 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004668 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004669 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004670 }
4671 return V[0];
4672 }
Dan Gohman475871a2008-07-27 21:46:04 +00004673 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004674}
4675
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004676SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004677X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004678 // We support concatenate two MMX registers and place them in a MMX
4679 // register. This is better than doing a stack convert.
4680 DebugLoc dl = Op.getDebugLoc();
4681 EVT ResVT = Op.getValueType();
4682 assert(Op.getNumOperands() == 2);
4683 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4684 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4685 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004686 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004687 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4688 InVec = Op.getOperand(1);
4689 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4690 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004691 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004692 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4693 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4694 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004695 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004696 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4697 Mask[0] = 0; Mask[1] = 2;
4698 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4699 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004700 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004701}
4702
Nate Begemanb9a47b82009-02-23 08:49:38 +00004703// v8i16 shuffles - Prefer shuffles in the following order:
4704// 1. [all] pshuflw, pshufhw, optional move
4705// 2. [ssse3] 1 x pshufb
4706// 3. [ssse3] 2 x pshufb + 1 x por
4707// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004708SDValue
4709X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4710 SelectionDAG &DAG) const {
4711 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004712 SDValue V1 = SVOp->getOperand(0);
4713 SDValue V2 = SVOp->getOperand(1);
4714 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004715 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004716
Nate Begemanb9a47b82009-02-23 08:49:38 +00004717 // Determine if more than 1 of the words in each of the low and high quadwords
4718 // of the result come from the same quadword of one of the two inputs. Undef
4719 // mask values count as coming from any quadword, for better codegen.
4720 SmallVector<unsigned, 4> LoQuad(4);
4721 SmallVector<unsigned, 4> HiQuad(4);
4722 BitVector InputQuads(4);
4723 for (unsigned i = 0; i < 8; ++i) {
4724 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004725 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004726 MaskVals.push_back(EltIdx);
4727 if (EltIdx < 0) {
4728 ++Quad[0];
4729 ++Quad[1];
4730 ++Quad[2];
4731 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004732 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004733 }
4734 ++Quad[EltIdx / 4];
4735 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004736 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004737
Nate Begemanb9a47b82009-02-23 08:49:38 +00004738 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004739 unsigned MaxQuad = 1;
4740 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004741 if (LoQuad[i] > MaxQuad) {
4742 BestLoQuad = i;
4743 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004744 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004745 }
4746
Nate Begemanb9a47b82009-02-23 08:49:38 +00004747 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004748 MaxQuad = 1;
4749 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004750 if (HiQuad[i] > MaxQuad) {
4751 BestHiQuad = i;
4752 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004753 }
4754 }
4755
Nate Begemanb9a47b82009-02-23 08:49:38 +00004756 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004757 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004758 // single pshufb instruction is necessary. If There are more than 2 input
4759 // quads, disable the next transformation since it does not help SSSE3.
4760 bool V1Used = InputQuads[0] || InputQuads[1];
4761 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004762 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004763 if (InputQuads.count() == 2 && V1Used && V2Used) {
4764 BestLoQuad = InputQuads.find_first();
4765 BestHiQuad = InputQuads.find_next(BestLoQuad);
4766 }
4767 if (InputQuads.count() > 2) {
4768 BestLoQuad = -1;
4769 BestHiQuad = -1;
4770 }
4771 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004772
Nate Begemanb9a47b82009-02-23 08:49:38 +00004773 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4774 // the shuffle mask. If a quad is scored as -1, that means that it contains
4775 // words from all 4 input quadwords.
4776 SDValue NewV;
4777 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004778 SmallVector<int, 8> MaskV;
4779 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4780 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004781 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004782 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4783 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4784 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004785
Nate Begemanb9a47b82009-02-23 08:49:38 +00004786 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4787 // source words for the shuffle, to aid later transformations.
4788 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004789 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004790 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004791 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004792 if (idx != (int)i)
4793 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004795 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004796 AllWordsInNewV = false;
4797 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004798 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004799
Nate Begemanb9a47b82009-02-23 08:49:38 +00004800 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4801 if (AllWordsInNewV) {
4802 for (int i = 0; i != 8; ++i) {
4803 int idx = MaskVals[i];
4804 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004805 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004806 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004807 if ((idx != i) && idx < 4)
4808 pshufhw = false;
4809 if ((idx != i) && idx > 3)
4810 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004811 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004812 V1 = NewV;
4813 V2Used = false;
4814 BestLoQuad = 0;
4815 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004816 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004817
Nate Begemanb9a47b82009-02-23 08:49:38 +00004818 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4819 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004820 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004821 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4822 unsigned TargetMask = 0;
4823 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004825 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4826 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4827 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004828 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004829 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 }
Eric Christopherfd179292009-08-27 18:07:15 +00004831
Nate Begemanb9a47b82009-02-23 08:49:38 +00004832 // If we have SSSE3, and all words of the result are from 1 input vector,
4833 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4834 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004835 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004837
Nate Begemanb9a47b82009-02-23 08:49:38 +00004838 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004839 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004840 // mask, and elements that come from V1 in the V2 mask, so that the two
4841 // results can be OR'd together.
4842 bool TwoInputs = V1Used && V2Used;
4843 for (unsigned i = 0; i != 8; ++i) {
4844 int EltIdx = MaskVals[i] * 2;
4845 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4847 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004848 continue;
4849 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004850 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4851 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004853 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004854 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004855 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004857 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004858 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004859
Nate Begemanb9a47b82009-02-23 08:49:38 +00004860 // Calculate the shuffle mask for the second input, shuffle it, and
4861 // OR it with the first shuffled input.
4862 pshufbMask.clear();
4863 for (unsigned i = 0; i != 8; ++i) {
4864 int EltIdx = MaskVals[i] * 2;
4865 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004866 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4867 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004868 continue;
4869 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4871 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004872 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004873 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004874 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004875 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 MVT::v16i8, &pshufbMask[0], 16));
4877 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004878 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004879 }
4880
4881 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4882 // and update MaskVals with new element order.
4883 BitVector InOrder(8);
4884 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004885 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004886 for (int i = 0; i != 4; ++i) {
4887 int idx = MaskVals[i];
4888 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004889 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004890 InOrder.set(i);
4891 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004892 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004893 InOrder.set(i);
4894 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004895 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004896 }
4897 }
4898 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004899 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004901 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004902
4903 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4904 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4905 NewV.getOperand(0),
4906 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4907 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004908 }
Eric Christopherfd179292009-08-27 18:07:15 +00004909
Nate Begemanb9a47b82009-02-23 08:49:38 +00004910 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4911 // and update MaskVals with the new element order.
4912 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004913 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004914 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004915 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004916 for (unsigned i = 4; i != 8; ++i) {
4917 int idx = MaskVals[i];
4918 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004919 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004920 InOrder.set(i);
4921 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004922 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004923 InOrder.set(i);
4924 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004926 }
4927 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004928 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004929 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004930
4931 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4932 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4933 NewV.getOperand(0),
4934 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4935 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004936 }
Eric Christopherfd179292009-08-27 18:07:15 +00004937
Nate Begemanb9a47b82009-02-23 08:49:38 +00004938 // In case BestHi & BestLo were both -1, which means each quadword has a word
4939 // from each of the four input quadwords, calculate the InOrder bitvector now
4940 // before falling through to the insert/extract cleanup.
4941 if (BestLoQuad == -1 && BestHiQuad == -1) {
4942 NewV = V1;
4943 for (int i = 0; i != 8; ++i)
4944 if (MaskVals[i] < 0 || MaskVals[i] == i)
4945 InOrder.set(i);
4946 }
Eric Christopherfd179292009-08-27 18:07:15 +00004947
Nate Begemanb9a47b82009-02-23 08:49:38 +00004948 // The other elements are put in the right place using pextrw and pinsrw.
4949 for (unsigned i = 0; i != 8; ++i) {
4950 if (InOrder[i])
4951 continue;
4952 int EltIdx = MaskVals[i];
4953 if (EltIdx < 0)
4954 continue;
4955 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004956 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004957 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004958 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004959 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004960 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004961 DAG.getIntPtrConstant(i));
4962 }
4963 return NewV;
4964}
4965
4966// v16i8 shuffles - Prefer shuffles in the following order:
4967// 1. [ssse3] 1 x pshufb
4968// 2. [ssse3] 2 x pshufb + 1 x por
4969// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4970static
Nate Begeman9008ca62009-04-27 18:41:29 +00004971SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004972 SelectionDAG &DAG,
4973 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 SDValue V1 = SVOp->getOperand(0);
4975 SDValue V2 = SVOp->getOperand(1);
4976 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004977 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004978 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004979
Nate Begemanb9a47b82009-02-23 08:49:38 +00004980 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004981 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004982 // present, fall back to case 3.
4983 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4984 bool V1Only = true;
4985 bool V2Only = true;
4986 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004988 if (EltIdx < 0)
4989 continue;
4990 if (EltIdx < 16)
4991 V2Only = false;
4992 else
4993 V1Only = false;
4994 }
Eric Christopherfd179292009-08-27 18:07:15 +00004995
Nate Begemanb9a47b82009-02-23 08:49:38 +00004996 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4997 if (TLI.getSubtarget()->hasSSSE3()) {
4998 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004999
Nate Begemanb9a47b82009-02-23 08:49:38 +00005000 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005001 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005002 //
5003 // Otherwise, we have elements from both input vectors, and must zero out
5004 // elements that come from V2 in the first mask, and V1 in the second mask
5005 // so that we can OR them together.
5006 bool TwoInputs = !(V1Only || V2Only);
5007 for (unsigned i = 0; i != 16; ++i) {
5008 int EltIdx = MaskVals[i];
5009 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005010 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005011 continue;
5012 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005013 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005014 }
5015 // If all the elements are from V2, assign it to V1 and return after
5016 // building the first pshufb.
5017 if (V2Only)
5018 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00005019 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005020 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005021 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005022 if (!TwoInputs)
5023 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005024
Nate Begemanb9a47b82009-02-23 08:49:38 +00005025 // Calculate the shuffle mask for the second input, shuffle it, and
5026 // OR it with the first shuffled input.
5027 pshufbMask.clear();
5028 for (unsigned i = 0; i != 16; ++i) {
5029 int EltIdx = MaskVals[i];
5030 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005032 continue;
5033 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005034 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005035 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005036 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005037 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005038 MVT::v16i8, &pshufbMask[0], 16));
5039 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005040 }
Eric Christopherfd179292009-08-27 18:07:15 +00005041
Nate Begemanb9a47b82009-02-23 08:49:38 +00005042 // No SSSE3 - Calculate in place words and then fix all out of place words
5043 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5044 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005045 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5046 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005047 SDValue NewV = V2Only ? V2 : V1;
5048 for (int i = 0; i != 8; ++i) {
5049 int Elt0 = MaskVals[i*2];
5050 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005051
Nate Begemanb9a47b82009-02-23 08:49:38 +00005052 // This word of the result is all undef, skip it.
5053 if (Elt0 < 0 && Elt1 < 0)
5054 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005055
Nate Begemanb9a47b82009-02-23 08:49:38 +00005056 // This word of the result is already in the correct place, skip it.
5057 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5058 continue;
5059 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5060 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005061
Nate Begemanb9a47b82009-02-23 08:49:38 +00005062 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5063 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5064 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005065
5066 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5067 // using a single extract together, load it and store it.
5068 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005069 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005070 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005071 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005072 DAG.getIntPtrConstant(i));
5073 continue;
5074 }
5075
Nate Begemanb9a47b82009-02-23 08:49:38 +00005076 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005077 // source byte is not also odd, shift the extracted word left 8 bits
5078 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005079 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005080 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005081 DAG.getIntPtrConstant(Elt1 / 2));
5082 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005083 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005084 DAG.getConstant(8,
5085 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005086 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005087 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5088 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005089 }
5090 // If Elt0 is defined, extract it from the appropriate source. If the
5091 // source byte is not also even, shift the extracted word right 8 bits. If
5092 // Elt1 was also defined, OR the extracted values together before
5093 // inserting them in the result.
5094 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005095 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005096 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5097 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00005099 DAG.getConstant(8,
5100 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005101 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005102 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5103 DAG.getConstant(0x00FF, MVT::i16));
5104 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00005105 : InsElt0;
5106 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005107 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005108 DAG.getIntPtrConstant(i));
5109 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005110 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005111}
5112
Evan Cheng7a831ce2007-12-15 03:00:47 +00005113/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005114/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00005115/// done when every pair / quad of shuffle mask elements point to elements in
5116/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005117/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00005118static
Nate Begeman9008ca62009-04-27 18:41:29 +00005119SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005120 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00005121 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00005122 SDValue V1 = SVOp->getOperand(0);
5123 SDValue V2 = SVOp->getOperand(1);
5124 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00005125 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00005126 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00005127 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005128 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005129 case MVT::v4f32: NewVT = MVT::v2f64; break;
5130 case MVT::v4i32: NewVT = MVT::v2i64; break;
5131 case MVT::v8i16: NewVT = MVT::v4i32; break;
5132 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00005133 }
5134
Nate Begeman9008ca62009-04-27 18:41:29 +00005135 int Scale = NumElems / NewWidth;
5136 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00005137 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005138 int StartIdx = -1;
5139 for (int j = 0; j < Scale; ++j) {
5140 int EltIdx = SVOp->getMaskElt(i+j);
5141 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005142 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00005143 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00005144 StartIdx = EltIdx - (EltIdx % Scale);
5145 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00005146 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005147 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005148 if (StartIdx == -1)
5149 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00005150 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005151 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005152 }
5153
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005154 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
5155 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00005156 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005157}
5158
Evan Chengd880b972008-05-09 21:53:03 +00005159/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005160///
Owen Andersone50ed302009-08-10 22:56:29 +00005161static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00005162 SDValue SrcOp, SelectionDAG &DAG,
5163 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005164 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005165 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00005166 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00005167 LD = dyn_cast<LoadSDNode>(SrcOp);
5168 if (!LD) {
5169 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5170 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00005171 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00005172 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00005173 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005174 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00005175 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005176 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005178 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005179 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5180 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5181 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00005182 SrcOp.getOperand(0)
5183 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005184 }
5185 }
5186 }
5187
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005188 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005189 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005190 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00005191 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00005192}
5193
Evan Chengace3c172008-07-22 21:13:36 +00005194/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
5195/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00005196static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00005197LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
5198 SDValue V1 = SVOp->getOperand(0);
5199 SDValue V2 = SVOp->getOperand(1);
5200 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005201 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00005202
Evan Chengace3c172008-07-22 21:13:36 +00005203 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00005204 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005205 SmallVector<int, 8> Mask1(4U, -1);
5206 SmallVector<int, 8> PermMask;
5207 SVOp->getMask(PermMask);
5208
Evan Chengace3c172008-07-22 21:13:36 +00005209 unsigned NumHi = 0;
5210 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00005211 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005212 int Idx = PermMask[i];
5213 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005214 Locs[i] = std::make_pair(-1, -1);
5215 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005216 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
5217 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005218 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00005219 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005220 NumLo++;
5221 } else {
5222 Locs[i] = std::make_pair(1, NumHi);
5223 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005224 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005225 NumHi++;
5226 }
5227 }
5228 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005229
Evan Chengace3c172008-07-22 21:13:36 +00005230 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005231 // If no more than two elements come from either vector. This can be
5232 // implemented with two shuffles. First shuffle gather the elements.
5233 // The second shuffle, which takes the first shuffle as both of its
5234 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00005235 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005236
Nate Begeman9008ca62009-04-27 18:41:29 +00005237 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00005238
Evan Chengace3c172008-07-22 21:13:36 +00005239 for (unsigned i = 0; i != 4; ++i) {
5240 if (Locs[i].first == -1)
5241 continue;
5242 else {
5243 unsigned Idx = (i < 2) ? 0 : 4;
5244 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005245 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005246 }
5247 }
5248
Nate Begeman9008ca62009-04-27 18:41:29 +00005249 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005250 } else if (NumLo == 3 || NumHi == 3) {
5251 // Otherwise, we must have three elements from one vector, call it X, and
5252 // one element from the other, call it Y. First, use a shufps to build an
5253 // intermediate vector with the one element from Y and the element from X
5254 // that will be in the same half in the final destination (the indexes don't
5255 // matter). Then, use a shufps to build the final vector, taking the half
5256 // containing the element from Y from the intermediate, and the other half
5257 // from X.
5258 if (NumHi == 3) {
5259 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005260 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005261 std::swap(V1, V2);
5262 }
5263
5264 // Find the element from V2.
5265 unsigned HiIndex;
5266 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005267 int Val = PermMask[HiIndex];
5268 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005269 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005270 if (Val >= 4)
5271 break;
5272 }
5273
Nate Begeman9008ca62009-04-27 18:41:29 +00005274 Mask1[0] = PermMask[HiIndex];
5275 Mask1[1] = -1;
5276 Mask1[2] = PermMask[HiIndex^1];
5277 Mask1[3] = -1;
5278 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005279
5280 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005281 Mask1[0] = PermMask[0];
5282 Mask1[1] = PermMask[1];
5283 Mask1[2] = HiIndex & 1 ? 6 : 4;
5284 Mask1[3] = HiIndex & 1 ? 4 : 6;
5285 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005286 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005287 Mask1[0] = HiIndex & 1 ? 2 : 0;
5288 Mask1[1] = HiIndex & 1 ? 0 : 2;
5289 Mask1[2] = PermMask[2];
5290 Mask1[3] = PermMask[3];
5291 if (Mask1[2] >= 0)
5292 Mask1[2] += 4;
5293 if (Mask1[3] >= 0)
5294 Mask1[3] += 4;
5295 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005296 }
Evan Chengace3c172008-07-22 21:13:36 +00005297 }
5298
5299 // Break it into (shuffle shuffle_hi, shuffle_lo).
5300 Locs.clear();
David Greenec4db4e52011-02-28 19:06:56 +00005301 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00005302 SmallVector<int,8> LoMask(4U, -1);
5303 SmallVector<int,8> HiMask(4U, -1);
5304
5305 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005306 unsigned MaskIdx = 0;
5307 unsigned LoIdx = 0;
5308 unsigned HiIdx = 2;
5309 for (unsigned i = 0; i != 4; ++i) {
5310 if (i == 2) {
5311 MaskPtr = &HiMask;
5312 MaskIdx = 1;
5313 LoIdx = 0;
5314 HiIdx = 2;
5315 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005316 int Idx = PermMask[i];
5317 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005318 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005320 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005321 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005322 LoIdx++;
5323 } else {
5324 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005326 HiIdx++;
5327 }
5328 }
5329
Nate Begeman9008ca62009-04-27 18:41:29 +00005330 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5331 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5332 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005333 for (unsigned i = 0; i != 4; ++i) {
5334 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005335 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005336 } else {
5337 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005338 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005339 }
5340 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005341 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005342}
5343
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005344static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005345 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005346 V = V.getOperand(0);
5347 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5348 V = V.getOperand(0);
5349 if (MayFoldLoad(V))
5350 return true;
5351 return false;
5352}
5353
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005354// FIXME: the version above should always be used. Since there's
5355// a bug where several vector shuffles can't be folded because the
5356// DAG is not updated during lowering and a node claims to have two
5357// uses while it only has one, use this version, and let isel match
5358// another instruction if the load really happens to have more than
5359// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005360// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005361static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005362 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005363 V = V.getOperand(0);
5364 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5365 V = V.getOperand(0);
5366 if (ISD::isNormalLoad(V.getNode()))
5367 return true;
5368 return false;
5369}
5370
5371/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5372/// a vector extract, and if both can be later optimized into a single load.
5373/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5374/// here because otherwise a target specific shuffle node is going to be
5375/// emitted for this shuffle, and the optimization not done.
5376/// FIXME: This is probably not the best approach, but fix the problem
5377/// until the right path is decided.
5378static
5379bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5380 const TargetLowering &TLI) {
5381 EVT VT = V.getValueType();
5382 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5383
5384 // Be sure that the vector shuffle is present in a pattern like this:
5385 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5386 if (!V.hasOneUse())
5387 return false;
5388
5389 SDNode *N = *V.getNode()->use_begin();
5390 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5391 return false;
5392
5393 SDValue EltNo = N->getOperand(1);
5394 if (!isa<ConstantSDNode>(EltNo))
5395 return false;
5396
5397 // If the bit convert changed the number of elements, it is unsafe
5398 // to examine the mask.
5399 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005400 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005401 EVT SrcVT = V.getOperand(0).getValueType();
5402 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5403 return false;
5404 V = V.getOperand(0);
5405 HasShuffleIntoBitcast = true;
5406 }
5407
5408 // Select the input vector, guarding against out of range extract vector.
5409 unsigned NumElems = VT.getVectorNumElements();
5410 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5411 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5412 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5413
5414 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005415 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005416 V = V.getOperand(0);
5417
5418 if (ISD::isNormalLoad(V.getNode())) {
5419 // Is the original load suitable?
5420 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5421
5422 // FIXME: avoid the multi-use bug that is preventing lots of
5423 // of foldings to be detected, this is still wrong of course, but
5424 // give the temporary desired behavior, and if it happens that
5425 // the load has real more uses, during isel it will not fold, and
5426 // will generate poor code.
5427 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5428 return false;
5429
5430 if (!HasShuffleIntoBitcast)
5431 return true;
5432
5433 // If there's a bitcast before the shuffle, check if the load type and
5434 // alignment is valid.
5435 unsigned Align = LN0->getAlignment();
5436 unsigned NewAlign =
5437 TLI.getTargetData()->getABITypeAlignment(
5438 VT.getTypeForEVT(*DAG.getContext()));
5439
5440 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5441 return false;
5442 }
5443
5444 return true;
5445}
5446
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005447static
Evan Cheng835580f2010-10-07 20:50:20 +00005448SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5449 EVT VT = Op.getValueType();
5450
5451 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005452 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5453 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005454 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5455 V1, DAG));
5456}
5457
5458static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005459SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5460 bool HasSSE2) {
5461 SDValue V1 = Op.getOperand(0);
5462 SDValue V2 = Op.getOperand(1);
5463 EVT VT = Op.getValueType();
5464
5465 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5466
5467 if (HasSSE2 && VT == MVT::v2f64)
5468 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5469
5470 // v4f32 or v4i32
5471 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5472}
5473
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005474static
5475SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5476 SDValue V1 = Op.getOperand(0);
5477 SDValue V2 = Op.getOperand(1);
5478 EVT VT = Op.getValueType();
5479
5480 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5481 "unsupported shuffle type");
5482
5483 if (V2.getOpcode() == ISD::UNDEF)
5484 V2 = V1;
5485
5486 // v4i32 or v4f32
5487 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5488}
5489
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005490static
5491SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5492 SDValue V1 = Op.getOperand(0);
5493 SDValue V2 = Op.getOperand(1);
5494 EVT VT = Op.getValueType();
5495 unsigned NumElems = VT.getVectorNumElements();
5496
5497 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5498 // operand of these instructions is only memory, so check if there's a
5499 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5500 // same masks.
5501 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005502
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005503 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005504 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005505 CanFoldLoad = true;
5506
5507 // When V1 is a load, it can be folded later into a store in isel, example:
5508 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5509 // turns into:
5510 // (MOVLPSmr addr:$src1, VR128:$src2)
5511 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005512 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005513 CanFoldLoad = true;
5514
Eric Christopher893a8822011-02-20 05:04:42 +00005515 // Both of them can't be memory operations though.
5516 if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
5517 CanFoldLoad = false;
Owen Anderson95771af2011-02-25 21:41:48 +00005518
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005519 if (CanFoldLoad) {
5520 if (HasSSE2 && NumElems == 2)
5521 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5522
5523 if (NumElems == 4)
5524 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5525 }
5526
5527 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5528 // movl and movlp will both match v2i64, but v2i64 is never matched by
5529 // movl earlier because we make it strict to avoid messing with the movlp load
5530 // folding logic (see the code above getMOVLP call). Match it here then,
5531 // this is horrible, but will stay like this until we move all shuffle
5532 // matching to x86 specific nodes. Note that for the 1st condition all
5533 // types are matched with movsd.
5534 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5535 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5536 else if (HasSSE2)
5537 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5538
5539
5540 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5541
5542 // Invert the operand order and use SHUFPS to match it.
5543 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5544 X86::getShuffleSHUFImmediate(SVOp), DAG);
5545}
5546
David Greenec4db4e52011-02-28 19:06:56 +00005547static inline unsigned getUNPCKLOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005548 switch(VT.getSimpleVT().SimpleTy) {
5549 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5550 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
David Greenec4db4e52011-02-28 19:06:56 +00005551 case MVT::v4f32:
5552 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPS : X86ISD::UNPCKLPS;
5553 case MVT::v2f64:
5554 return Subtarget->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5555 case MVT::v8f32: return X86ISD::VUNPCKLPSY;
5556 case MVT::v4f64: return X86ISD::VUNPCKLPDY;
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005557 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5558 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5559 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005560 llvm_unreachable("Unknown type for unpckl");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005561 }
5562 return 0;
5563}
5564
5565static inline unsigned getUNPCKHOpcode(EVT VT) {
5566 switch(VT.getSimpleVT().SimpleTy) {
5567 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5568 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5569 case MVT::v4f32: return X86ISD::UNPCKHPS;
5570 case MVT::v2f64: return X86ISD::UNPCKHPD;
5571 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5572 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5573 default:
Eric Christopherdd6e40a2011-02-19 03:19:09 +00005574 llvm_unreachable("Unknown type for unpckh");
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005575 }
5576 return 0;
5577}
5578
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005579static
5580SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005581 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005582 const X86Subtarget *Subtarget) {
5583 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5584 EVT VT = Op.getValueType();
5585 DebugLoc dl = Op.getDebugLoc();
5586 SDValue V1 = Op.getOperand(0);
5587 SDValue V2 = Op.getOperand(1);
5588
5589 if (isZeroShuffle(SVOp))
5590 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5591
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005592 // Handle splat operations
5593 if (SVOp->isSplat()) {
5594 // Special case, this is the only place now where it's
5595 // allowed to return a vector_shuffle operation without
5596 // using a target specific node, because *hopefully* it
5597 // will be optimized away by the dag combiner.
5598 if (VT.getVectorNumElements() <= 4 &&
5599 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5600 return Op;
5601
5602 // Handle splats by matching through known masks
5603 if (VT.getVectorNumElements() <= 4)
5604 return SDValue();
5605
Evan Cheng835580f2010-10-07 20:50:20 +00005606 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005607 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005608 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005609
5610 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5611 // do it!
5612 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5613 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5614 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005615 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005616 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5617 // FIXME: Figure out a cleaner way to do this.
5618 // Try to make use of movq to zero out the top part.
5619 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5620 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5621 if (NewOp.getNode()) {
5622 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5623 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5624 DAG, Subtarget, dl);
5625 }
5626 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5627 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5628 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5629 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5630 DAG, Subtarget, dl);
5631 }
5632 }
5633 return SDValue();
5634}
5635
Dan Gohman475871a2008-07-27 21:46:04 +00005636SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005637X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005638 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue V1 = Op.getOperand(0);
5640 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005641 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005642 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005643 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005644 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005645 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5646 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005647 bool V1IsSplat = false;
5648 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005649 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005650 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005651 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005652 MachineFunction &MF = DAG.getMachineFunction();
5653 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005654
Dale Johannesen0488fb62010-09-30 23:57:10 +00005655 // Shuffle operations on MMX not supported.
5656 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005657 return Op;
5658
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005659 // Vector shuffle lowering takes 3 steps:
5660 //
5661 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5662 // narrowing and commutation of operands should be handled.
5663 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5664 // shuffle nodes.
5665 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5666 // so the shuffle can be broken into other shuffles and the legalizer can
5667 // try the lowering again.
5668 //
5669 // The general ideia is that no vector_shuffle operation should be left to
5670 // be matched during isel, all of them must be converted to a target specific
5671 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005672
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005673 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5674 // narrowing and commutation of operands should be handled. The actual code
5675 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005676 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005677 if (NewOp.getNode())
5678 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005679
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005680 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5681 // unpckh_undef). Only use pshufd if speed is more important than size.
5682 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5683 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005684 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005685 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5686 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5687 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005688
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005689 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005690 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005691 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005692
Dale Johannesen0488fb62010-09-30 23:57:10 +00005693 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005694 return getMOVHighToLow(Op, dl, DAG);
5695
5696 // Use to match splats
5697 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5698 (VT == MVT::v2f64 || VT == MVT::v2i64))
5699 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5700
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005701 if (X86::isPSHUFDMask(SVOp)) {
5702 // The actual implementation will match the mask in the if above and then
5703 // during isel it can match several different instructions, not only pshufd
5704 // as its name says, sad but true, emulate the behavior for now...
5705 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5706 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5707
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005708 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5709
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005710 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005711 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5712
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005713 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005714 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5715 TargetMask, DAG);
5716
5717 if (VT == MVT::v4f32)
5718 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5719 TargetMask, DAG);
5720 }
Eric Christopherfd179292009-08-27 18:07:15 +00005721
Evan Chengf26ffe92008-05-29 08:22:04 +00005722 // Check if this can be converted into a logical shift.
5723 bool isLeft = false;
5724 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005725 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005726 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005727 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005728 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005729 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005730 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005731 EVT EltVT = VT.getVectorElementType();
5732 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005733 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005734 }
Eric Christopherfd179292009-08-27 18:07:15 +00005735
Nate Begeman9008ca62009-04-27 18:41:29 +00005736 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005737 if (V1IsUndef)
5738 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005739 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005740 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005741 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005742 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005743 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5744
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005745 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005746 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5747 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005748 }
Eric Christopherfd179292009-08-27 18:07:15 +00005749
Nate Begeman9008ca62009-04-27 18:41:29 +00005750 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005751 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5752 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005753
Dale Johannesen0488fb62010-09-30 23:57:10 +00005754 if (X86::isMOVHLPSMask(SVOp))
5755 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005756
Dale Johannesen0488fb62010-09-30 23:57:10 +00005757 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5758 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005759
Dale Johannesen0488fb62010-09-30 23:57:10 +00005760 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5761 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005762
Dale Johannesen0488fb62010-09-30 23:57:10 +00005763 if (X86::isMOVLPMask(SVOp))
5764 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005765
Nate Begeman9008ca62009-04-27 18:41:29 +00005766 if (ShouldXformToMOVHLPS(SVOp) ||
5767 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5768 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005769
Evan Chengf26ffe92008-05-29 08:22:04 +00005770 if (isShift) {
5771 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005772 EVT EltVT = VT.getVectorElementType();
5773 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005774 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005775 }
Eric Christopherfd179292009-08-27 18:07:15 +00005776
Evan Cheng9eca5e82006-10-25 21:49:50 +00005777 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005778 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5779 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005780 V1IsSplat = isSplatVector(V1.getNode());
5781 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005782
Chris Lattner8a594482007-11-25 00:24:49 +00005783 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005784 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005785 Op = CommuteVectorShuffle(SVOp, DAG);
5786 SVOp = cast<ShuffleVectorSDNode>(Op);
5787 V1 = SVOp->getOperand(0);
5788 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005789 std::swap(V1IsSplat, V2IsSplat);
5790 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005791 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005792 }
5793
Nate Begeman9008ca62009-04-27 18:41:29 +00005794 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5795 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005796 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005797 return V1;
5798 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5799 // the instruction selector will not match, so get a canonical MOVL with
5800 // swapped operands to undo the commute.
5801 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005802 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005803
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005804 if (X86::isUNPCKLMask(SVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005805 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5806 dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005807
5808 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005809 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005810
Evan Cheng9bbbb982006-10-25 20:48:19 +00005811 if (V2IsSplat) {
5812 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005813 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005814 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005815 SDValue NewMask = NormalizeMask(SVOp, DAG);
5816 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5817 if (NSVOp != SVOp) {
5818 if (X86::isUNPCKLMask(NSVOp, true)) {
5819 return NewMask;
5820 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5821 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005822 }
5823 }
5824 }
5825
Evan Cheng9eca5e82006-10-25 21:49:50 +00005826 if (Commuted) {
5827 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005828 // FIXME: this seems wrong.
5829 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5830 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005831
5832 if (X86::isUNPCKLMask(NewSVOp))
David Greenec4db4e52011-02-28 19:06:56 +00005833 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5834 dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005835
5836 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005837 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005838 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005839
Nate Begeman9008ca62009-04-27 18:41:29 +00005840 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005841 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005842 return CommuteVectorShuffle(SVOp, DAG);
5843
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005844 // The checks below are all present in isShuffleMaskLegal, but they are
5845 // inlined here right now to enable us to directly emit target specific
5846 // nodes, and remove one by one until they don't return Op anymore.
5847 SmallVector<int, 16> M;
5848 SVOp->getMask(M);
5849
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005850 if (isPALIGNRMask(M, VT, HasSSSE3))
5851 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5852 X86::getShufflePALIGNRImmediate(SVOp),
5853 DAG);
5854
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005855 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5856 SVOp->getSplatIndex() == 0 && V2IsUndef) {
David Greenec4db4e52011-02-28 19:06:56 +00005857 if (VT == MVT::v2f64) {
5858 X86ISD::NodeType Opcode =
5859 getSubtarget()->hasAVX() ? X86ISD::VUNPCKLPD : X86ISD::UNPCKLPD;
5860 return getTargetShuffleNode(Opcode, dl, VT, V1, V1, DAG);
5861 }
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005862 if (VT == MVT::v2i64)
5863 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5864 }
5865
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005866 if (isPSHUFHWMask(M, VT))
5867 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5868 X86::getShufflePSHUFHWImmediate(SVOp),
5869 DAG);
5870
5871 if (isPSHUFLWMask(M, VT))
5872 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5873 X86::getShufflePSHUFLWImmediate(SVOp),
5874 DAG);
5875
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005876 if (isSHUFPMask(M, VT)) {
5877 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5878 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5879 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5880 TargetMask, DAG);
5881 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5882 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5883 TargetMask, DAG);
5884 }
5885
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005886 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5887 if (VT != MVT::v2i64 && VT != MVT::v2f64)
David Greenec4db4e52011-02-28 19:06:56 +00005888 return getTargetShuffleNode(getUNPCKLOpcode(VT, getSubtarget()),
5889 dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005890 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5891 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5892 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5893
Evan Cheng14b32e12007-12-11 01:46:18 +00005894 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005896 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005897 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005898 return NewOp;
5899 }
5900
Owen Anderson825b72b2009-08-11 20:47:22 +00005901 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005902 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 if (NewOp.getNode())
5904 return NewOp;
5905 }
Eric Christopherfd179292009-08-27 18:07:15 +00005906
Dale Johannesen0488fb62010-09-30 23:57:10 +00005907 // Handle all 4 wide cases with a number of shuffles.
5908 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005909 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910
Dan Gohman475871a2008-07-27 21:46:04 +00005911 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005912}
5913
Dan Gohman475871a2008-07-27 21:46:04 +00005914SDValue
5915X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005916 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005917 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005918 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005919 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005921 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005923 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005924 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005925 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005926 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5927 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5928 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5930 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005931 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005933 Op.getOperand(0)),
5934 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005935 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005936 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005937 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005938 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005939 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005940 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005941 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5942 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005943 // result has a single use which is a store or a bitcast to i32. And in
5944 // the case of a store, it's not worth it if the index is a constant 0,
5945 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005946 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005947 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005948 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005949 if ((User->getOpcode() != ISD::STORE ||
5950 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5951 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005952 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005954 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005956 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005957 Op.getOperand(0)),
5958 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005959 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005960 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005961 // ExtractPS works with constant index.
5962 if (isa<ConstantSDNode>(Op.getOperand(1)))
5963 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005964 }
Dan Gohman475871a2008-07-27 21:46:04 +00005965 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005966}
5967
5968
Dan Gohman475871a2008-07-27 21:46:04 +00005969SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005970X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5971 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005972 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005973 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005974
David Greene74a579d2011-02-10 16:57:36 +00005975 SDValue Vec = Op.getOperand(0);
5976 EVT VecVT = Vec.getValueType();
5977
5978 // If this is a 256-bit vector result, first extract the 128-bit
5979 // vector and then extract from the 128-bit vector.
5980 if (VecVT.getSizeInBits() > 128) {
5981 DebugLoc dl = Op.getNode()->getDebugLoc();
5982 unsigned NumElems = VecVT.getVectorNumElements();
5983 SDValue Idx = Op.getOperand(1);
5984
5985 if (!isa<ConstantSDNode>(Idx))
5986 return SDValue();
5987
5988 unsigned ExtractNumElems = NumElems / (VecVT.getSizeInBits() / 128);
5989 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5990
5991 // Get the 128-bit vector.
5992 bool Upper = IdxVal >= ExtractNumElems;
5993 Vec = Extract128BitVector(Vec, Idx, DAG, dl);
5994
5995 // Extract from it.
5996 SDValue ScaledIdx = Idx;
5997 if (Upper)
5998 ScaledIdx = DAG.getNode(ISD::SUB, dl, Idx.getValueType(), Idx,
5999 DAG.getConstant(ExtractNumElems,
6000 Idx.getValueType()));
6001 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
6002 ScaledIdx);
6003 }
6004
6005 assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
6006
Evan Cheng62a3f152008-03-24 21:52:23 +00006007 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006008 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006009 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006010 return Res;
6011 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006012
Owen Andersone50ed302009-08-10 22:56:29 +00006013 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006014 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006015 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006016 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006017 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006018 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006019 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006020 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6021 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006022 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00006024 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006025 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00006026 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00006027 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006028 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00006029 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00006030 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006031 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006032 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006033 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006034 if (Idx == 0)
6035 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00006036
Evan Cheng0db9fe62006-04-25 20:13:52 +00006037 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00006038 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006039 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006040 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006041 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006042 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006043 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00006044 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006045 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6046 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6047 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006048 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006049 if (Idx == 0)
6050 return Op;
6051
6052 // UNPCKHPD the element to the lowest double word, then movsd.
6053 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6054 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00006055 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00006056 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00006057 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00006058 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00006059 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00006060 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006061 }
6062
Dan Gohman475871a2008-07-27 21:46:04 +00006063 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006064}
6065
Dan Gohman475871a2008-07-27 21:46:04 +00006066SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006067X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6068 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006069 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006070 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006071 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006072
Dan Gohman475871a2008-07-27 21:46:04 +00006073 SDValue N0 = Op.getOperand(0);
6074 SDValue N1 = Op.getOperand(1);
6075 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00006076
Dan Gohman8a55ce42009-09-23 21:02:20 +00006077 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00006078 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006079 unsigned Opc;
6080 if (VT == MVT::v8i16)
6081 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00006082 else if (VT == MVT::v16i8)
6083 Opc = X86ISD::PINSRB;
6084 else
6085 Opc = X86ISD::PINSRB;
6086
Nate Begeman14d12ca2008-02-11 04:19:36 +00006087 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
6088 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006089 if (N1.getValueType() != MVT::i32)
6090 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6091 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006092 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00006093 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006094 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00006095 // Bits [7:6] of the constant are the source select. This will always be
6096 // zero here. The DAG Combiner may combine an extract_elt index into these
6097 // bits. For example (insert (extract, 3), 2) could be matched by putting
6098 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00006099 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00006100 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00006101 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00006102 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006103 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00006104 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00006105 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00006106 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00006107 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00006108 // PINSR* works with constant index.
6109 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006110 }
Dan Gohman475871a2008-07-27 21:46:04 +00006111 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006112}
6113
Dan Gohman475871a2008-07-27 21:46:04 +00006114SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006115X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006116 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006117 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006118
David Greene6b381262011-02-09 15:32:06 +00006119 DebugLoc dl = Op.getDebugLoc();
6120 SDValue N0 = Op.getOperand(0);
6121 SDValue N1 = Op.getOperand(1);
6122 SDValue N2 = Op.getOperand(2);
6123
6124 // If this is a 256-bit vector result, first insert into a 128-bit
6125 // vector and then insert into the 256-bit vector.
6126 if (VT.getSizeInBits() > 128) {
6127 if (!isa<ConstantSDNode>(N2))
6128 return SDValue();
6129
6130 // Get the 128-bit vector.
6131 unsigned NumElems = VT.getVectorNumElements();
6132 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
6133 bool Upper = IdxVal >= NumElems / 2;
6134
6135 SDValue SubN0 = Extract128BitVector(N0, N2, DAG, dl);
6136
6137 // Insert into it.
6138 SDValue ScaledN2 = N2;
6139 if (Upper)
6140 ScaledN2 = DAG.getNode(ISD::SUB, dl, N2.getValueType(), N2,
Owen Anderson95771af2011-02-25 21:41:48 +00006141 DAG.getConstant(NumElems /
David Greene6b381262011-02-09 15:32:06 +00006142 (VT.getSizeInBits() / 128),
6143 N2.getValueType()));
6144 Op = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, SubN0.getValueType(), SubN0,
6145 N1, ScaledN2);
6146
6147 // Insert the 128-bit vector
6148 // FIXME: Why UNDEF?
6149 return Insert128BitVector(N0, Op, N2, DAG, dl);
6150 }
6151
Nate Begeman14d12ca2008-02-11 04:19:36 +00006152 if (Subtarget->hasSSE41())
6153 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
6154
Dan Gohman8a55ce42009-09-23 21:02:20 +00006155 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00006156 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00006157
Dan Gohman8a55ce42009-09-23 21:02:20 +00006158 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00006159 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
6160 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00006161 if (N1.getValueType() != MVT::i32)
6162 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
6163 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006164 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00006165 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006166 }
Dan Gohman475871a2008-07-27 21:46:04 +00006167 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006168}
6169
Dan Gohman475871a2008-07-27 21:46:04 +00006170SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006171X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
David Greene2fcdfb42011-02-10 23:11:29 +00006172 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006173 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00006174 EVT OpVT = Op.getValueType();
6175
6176 // If this is a 256-bit vector result, first insert into a 128-bit
6177 // vector and then insert into the 256-bit vector.
6178 if (OpVT.getSizeInBits() > 128) {
6179 // Insert into a 128-bit vector.
6180 EVT VT128 = EVT::getVectorVT(*Context,
6181 OpVT.getVectorElementType(),
6182 OpVT.getVectorNumElements() / 2);
6183
6184 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
6185
6186 // Insert the 128-bit vector.
6187 return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
6188 DAG.getConstant(0, MVT::i32),
6189 DAG, dl);
6190 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006191
Chris Lattnerf172ecd2010-07-04 23:07:25 +00006192 if (Op.getValueType() == MVT::v1i64 &&
6193 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00006194 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00006195
Owen Anderson825b72b2009-08-11 20:47:22 +00006196 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00006197 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
6198 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006199 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00006200 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006201}
6202
David Greene91585092011-01-26 15:38:49 +00006203// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
6204// a simple subregister reference or explicit instructions to grab
6205// upper bits of a vector.
6206SDValue
6207X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6208 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00006209 DebugLoc dl = Op.getNode()->getDebugLoc();
6210 SDValue Vec = Op.getNode()->getOperand(0);
6211 SDValue Idx = Op.getNode()->getOperand(1);
6212
6213 if (Op.getNode()->getValueType(0).getSizeInBits() == 128
6214 && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
6215 return Extract128BitVector(Vec, Idx, DAG, dl);
6216 }
David Greene91585092011-01-26 15:38:49 +00006217 }
6218 return SDValue();
6219}
6220
David Greenecfe33c42011-01-26 19:13:22 +00006221// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
6222// simple superregister reference or explicit instructions to insert
6223// the upper bits of a vector.
6224SDValue
6225X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
6226 if (Subtarget->hasAVX()) {
6227 DebugLoc dl = Op.getNode()->getDebugLoc();
6228 SDValue Vec = Op.getNode()->getOperand(0);
6229 SDValue SubVec = Op.getNode()->getOperand(1);
6230 SDValue Idx = Op.getNode()->getOperand(2);
6231
6232 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
6233 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
David Greenea5f26012011-02-07 19:36:54 +00006234 return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00006235 }
6236 }
6237 return SDValue();
6238}
6239
Bill Wendling056292f2008-09-16 21:48:12 +00006240// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
6241// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
6242// one of the above mentioned nodes. It has to be wrapped because otherwise
6243// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
6244// be used to form addressing mode. These wrapped nodes will be selected
6245// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00006246SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006247X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006248 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006249
Chris Lattner41621a22009-06-26 19:22:52 +00006250 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6251 // global base reg.
6252 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006253 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006254 CodeModel::Model M = getTargetMachine().getCodeModel();
6255
Chris Lattner4f066492009-07-11 20:29:19 +00006256 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006257 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006258 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006259 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006260 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006261 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006262 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006263
Evan Cheng1606e8e2009-03-13 07:51:59 +00006264 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00006265 CP->getAlignment(),
6266 CP->getOffset(), OpFlag);
6267 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00006268 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006269 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00006270 if (OpFlag) {
6271 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006272 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006273 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006274 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006275 }
6276
6277 return Result;
6278}
6279
Dan Gohmand858e902010-04-17 15:26:15 +00006280SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006281 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00006282
Chris Lattner18c59872009-06-27 04:16:01 +00006283 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6284 // global base reg.
6285 unsigned char OpFlag = 0;
6286 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006287 CodeModel::Model M = getTargetMachine().getCodeModel();
6288
Chris Lattner4f066492009-07-11 20:29:19 +00006289 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006290 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006291 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006292 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006293 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006294 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006295 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006296
Chris Lattner18c59872009-06-27 04:16:01 +00006297 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
6298 OpFlag);
6299 DebugLoc DL = JT->getDebugLoc();
6300 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006301
Chris Lattner18c59872009-06-27 04:16:01 +00006302 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00006303 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00006304 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6305 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006306 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006307 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006308
Chris Lattner18c59872009-06-27 04:16:01 +00006309 return Result;
6310}
6311
6312SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006313X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00006314 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00006315
Chris Lattner18c59872009-06-27 04:16:01 +00006316 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6317 // global base reg.
6318 unsigned char OpFlag = 0;
6319 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006320 CodeModel::Model M = getTargetMachine().getCodeModel();
6321
Chris Lattner4f066492009-07-11 20:29:19 +00006322 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006323 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00006324 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00006325 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006326 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00006327 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00006328 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00006329
Chris Lattner18c59872009-06-27 04:16:01 +00006330 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00006331
Chris Lattner18c59872009-06-27 04:16:01 +00006332 DebugLoc DL = Op.getDebugLoc();
6333 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00006334
6335
Chris Lattner18c59872009-06-27 04:16:01 +00006336 // With PIC, the address is actually $g + Offset.
6337 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00006338 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00006339 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6340 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006341 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00006342 Result);
6343 }
Eric Christopherfd179292009-08-27 18:07:15 +00006344
Chris Lattner18c59872009-06-27 04:16:01 +00006345 return Result;
6346}
6347
Dan Gohman475871a2008-07-27 21:46:04 +00006348SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006349X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006350 // Create the TargetBlockAddressAddress node.
6351 unsigned char OpFlags =
6352 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006353 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006354 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006355 DebugLoc dl = Op.getDebugLoc();
6356 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6357 /*isTarget=*/true, OpFlags);
6358
Dan Gohmanf705adb2009-10-30 01:28:02 +00006359 if (Subtarget->isPICStyleRIPRel() &&
6360 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006361 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6362 else
6363 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006364
Dan Gohman29cbade2009-11-20 23:18:13 +00006365 // With PIC, the address is actually $g + Offset.
6366 if (isGlobalRelativeToPICBase(OpFlags)) {
6367 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6368 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6369 Result);
6370 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006371
6372 return Result;
6373}
6374
6375SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006376X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006377 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006378 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006379 // Create the TargetGlobalAddress node, folding in the constant
6380 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006381 unsigned char OpFlags =
6382 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006383 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006384 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006385 if (OpFlags == X86II::MO_NO_FLAG &&
6386 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006387 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006388 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006389 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006390 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006391 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006392 }
Eric Christopherfd179292009-08-27 18:07:15 +00006393
Chris Lattner4f066492009-07-11 20:29:19 +00006394 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006395 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006396 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6397 else
6398 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006399
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006400 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006401 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006402 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6403 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006404 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006406
Chris Lattner36c25012009-07-10 07:34:39 +00006407 // For globals that require a load from a stub to get the address, emit the
6408 // load.
6409 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006410 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006411 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006412
Dan Gohman6520e202008-10-18 02:06:02 +00006413 // If there was a non-zero offset that we didn't fold, create an explicit
6414 // addition for it.
6415 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006416 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006417 DAG.getConstant(Offset, getPointerTy()));
6418
Evan Cheng0db9fe62006-04-25 20:13:52 +00006419 return Result;
6420}
6421
Evan Chengda43bcf2008-09-24 00:05:32 +00006422SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006423X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006424 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006425 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006426 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006427}
6428
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006429static SDValue
6430GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006431 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006432 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006433 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006434 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006435 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006436 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006437 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006438 GA->getOffset(),
6439 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006440 if (InFlag) {
6441 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006442 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006443 } else {
6444 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006445 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006446 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006447
6448 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006449 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006450
Rafael Espindola15f1b662009-04-24 12:59:40 +00006451 SDValue Flag = Chain.getValue(1);
6452 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006453}
6454
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006455// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006456static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006457LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006458 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006459 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006460 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6461 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006462 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006463 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006464 InFlag = Chain.getValue(1);
6465
Chris Lattnerb903bed2009-06-26 21:20:29 +00006466 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006467}
6468
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006469// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006470static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006471LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006472 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006473 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6474 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006475}
6476
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006477// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6478// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006479static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006480 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006481 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006482 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006483
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006484 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6485 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6486 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006487
Michael J. Spencerec38de22010-10-10 22:04:20 +00006488 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006489 DAG.getIntPtrConstant(0),
6490 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006491
Chris Lattnerb903bed2009-06-26 21:20:29 +00006492 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006493 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6494 // initialexec.
6495 unsigned WrapperKind = X86ISD::Wrapper;
6496 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006497 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006498 } else if (is64Bit) {
6499 assert(model == TLSModel::InitialExec);
6500 OperandFlags = X86II::MO_GOTTPOFF;
6501 WrapperKind = X86ISD::WrapperRIP;
6502 } else {
6503 assert(model == TLSModel::InitialExec);
6504 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006505 }
Eric Christopherfd179292009-08-27 18:07:15 +00006506
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006507 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6508 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006509 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006510 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006511 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006512 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006513
Rafael Espindola9a580232009-02-27 13:37:18 +00006514 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006515 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006516 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006517
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006518 // The address of the thread local variable is the add of the thread
6519 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006520 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006521}
6522
Dan Gohman475871a2008-07-27 21:46:04 +00006523SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006524X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006525
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006526 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006527 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006528
Eric Christopher30ef0e52010-06-03 04:07:48 +00006529 if (Subtarget->isTargetELF()) {
6530 // TODO: implement the "local dynamic" model
6531 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006532
Eric Christopher30ef0e52010-06-03 04:07:48 +00006533 // If GV is an alias then use the aliasee for determining
6534 // thread-localness.
6535 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6536 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006537
6538 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006539 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006540
Eric Christopher30ef0e52010-06-03 04:07:48 +00006541 switch (model) {
6542 case TLSModel::GeneralDynamic:
6543 case TLSModel::LocalDynamic: // not implemented
6544 if (Subtarget->is64Bit())
6545 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6546 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006547
Eric Christopher30ef0e52010-06-03 04:07:48 +00006548 case TLSModel::InitialExec:
6549 case TLSModel::LocalExec:
6550 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6551 Subtarget->is64Bit());
6552 }
6553 } else if (Subtarget->isTargetDarwin()) {
6554 // Darwin only has one model of TLS. Lower to that.
6555 unsigned char OpFlag = 0;
6556 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6557 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006558
Eric Christopher30ef0e52010-06-03 04:07:48 +00006559 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6560 // global base reg.
6561 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6562 !Subtarget->is64Bit();
6563 if (PIC32)
6564 OpFlag = X86II::MO_TLVP_PIC_BASE;
6565 else
6566 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006567 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006568 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006569 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006570 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006571 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006572
Eric Christopher30ef0e52010-06-03 04:07:48 +00006573 // With PIC32, the address is actually $g + Offset.
6574 if (PIC32)
6575 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6576 DAG.getNode(X86ISD::GlobalBaseReg,
6577 DebugLoc(), getPointerTy()),
6578 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006579
Eric Christopher30ef0e52010-06-03 04:07:48 +00006580 // Lowering the machine isd will make sure everything is in the right
6581 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006582 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006583 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006584 SDValue Args[] = { Chain, Offset };
6585 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006586
Eric Christopher30ef0e52010-06-03 04:07:48 +00006587 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6588 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6589 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006590
Eric Christopher30ef0e52010-06-03 04:07:48 +00006591 // And our return value (tls address) is in the standard call return value
6592 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006593 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6594 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006595 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006596
Eric Christopher30ef0e52010-06-03 04:07:48 +00006597 assert(false &&
6598 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006599
Torok Edwinc23197a2009-07-14 16:55:14 +00006600 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006601 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006602}
6603
Evan Cheng0db9fe62006-04-25 20:13:52 +00006604
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006605/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006606/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006607SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006608 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006609 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006610 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006611 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006612 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006613 SDValue ShOpLo = Op.getOperand(0);
6614 SDValue ShOpHi = Op.getOperand(1);
6615 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006616 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006617 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006618 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006619
Dan Gohman475871a2008-07-27 21:46:04 +00006620 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006621 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006622 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6623 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006624 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006625 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6626 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006627 }
Evan Chenge3413162006-01-09 18:33:28 +00006628
Owen Anderson825b72b2009-08-11 20:47:22 +00006629 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6630 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006631 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006632 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006633
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006635 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006636 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6637 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006638
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006639 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006640 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6641 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006642 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006643 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6644 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006645 }
6646
Dan Gohman475871a2008-07-27 21:46:04 +00006647 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006648 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006649}
Evan Chenga3195e82006-01-12 22:54:21 +00006650
Dan Gohmand858e902010-04-17 15:26:15 +00006651SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6652 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006653 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006654
Dale Johannesen0488fb62010-09-30 23:57:10 +00006655 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006656 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006657
Owen Anderson825b72b2009-08-11 20:47:22 +00006658 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006659 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006660
Eli Friedman36df4992009-05-27 00:47:34 +00006661 // These are really Legal; return the operand so the caller accepts it as
6662 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006663 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006664 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006665 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006666 Subtarget->is64Bit()) {
6667 return Op;
6668 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006669
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006670 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006671 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006673 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006674 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006675 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006676 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006677 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006678 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006679 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6680}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006681
Owen Andersone50ed302009-08-10 22:56:29 +00006682SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006683 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006684 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006685 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006686 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006687 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006688 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006689 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006690 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006691 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006692 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006693
Chris Lattner492a43e2010-09-22 01:28:21 +00006694 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006695
Chris Lattner492a43e2010-09-22 01:28:21 +00006696 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6697 MachineMemOperand *MMO =
6698 DAG.getMachineFunction()
6699 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6700 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006701
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006702 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006703 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6704 X86ISD::FILD, DL,
6705 Tys, Ops, array_lengthof(Ops),
6706 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006707
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006708 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006710 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006711
6712 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6713 // shouldn't be necessary except that RFP cannot be live across
6714 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006715 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006716 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6717 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006718 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006719 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006720 SDValue Ops[] = {
6721 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6722 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006723 MachineMemOperand *MMO =
6724 DAG.getMachineFunction()
6725 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006726 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006727
Chris Lattner492a43e2010-09-22 01:28:21 +00006728 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6729 Ops, array_lengthof(Ops),
6730 Op.getValueType(), MMO);
6731 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006732 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006733 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006734 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006735
Evan Cheng0db9fe62006-04-25 20:13:52 +00006736 return Result;
6737}
6738
Bill Wendling8b8a6362009-01-17 03:56:04 +00006739// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006740SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6741 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006742 // This algorithm is not obvious. Here it is in C code, more or less:
6743 /*
6744 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6745 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6746 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006747
Bill Wendling8b8a6362009-01-17 03:56:04 +00006748 // Copy ints to xmm registers.
6749 __m128i xh = _mm_cvtsi32_si128( hi );
6750 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006751
Bill Wendling8b8a6362009-01-17 03:56:04 +00006752 // Combine into low half of a single xmm register.
6753 __m128i x = _mm_unpacklo_epi32( xh, xl );
6754 __m128d d;
6755 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006756
Bill Wendling8b8a6362009-01-17 03:56:04 +00006757 // Merge in appropriate exponents to give the integer bits the right
6758 // magnitude.
6759 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006760
Bill Wendling8b8a6362009-01-17 03:56:04 +00006761 // Subtract away the biases to deal with the IEEE-754 double precision
6762 // implicit 1.
6763 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006764
Bill Wendling8b8a6362009-01-17 03:56:04 +00006765 // All conversions up to here are exact. The correctly rounded result is
6766 // calculated using the current rounding mode using the following
6767 // horizontal add.
6768 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6769 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6770 // store doesn't really need to be here (except
6771 // maybe to zero the other double)
6772 return sd;
6773 }
6774 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006775
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006776 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006777 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006778
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006779 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006780 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006781 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6782 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6783 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6784 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006785 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006786 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006787
Bill Wendling8b8a6362009-01-17 03:56:04 +00006788 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006789 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006790 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006791 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006792 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006793 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006794 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006795
Owen Anderson825b72b2009-08-11 20:47:22 +00006796 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6797 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006798 Op.getOperand(0),
6799 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006800 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6801 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006802 Op.getOperand(0),
6803 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006804 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6805 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006806 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006807 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006808 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006809 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006810 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006811 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006812 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006813 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006814
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006815 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006816 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6818 DAG.getUNDEF(MVT::v2f64), ShufMask);
6819 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6820 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006821 DAG.getIntPtrConstant(0));
6822}
6823
Bill Wendling8b8a6362009-01-17 03:56:04 +00006824// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006825SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6826 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006827 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006828 // FP constant to bias correct the final result.
6829 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006830 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006831
6832 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006833 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6834 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006835 Op.getOperand(0),
6836 DAG.getIntPtrConstant(0)));
6837
Owen Anderson825b72b2009-08-11 20:47:22 +00006838 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006839 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006840 DAG.getIntPtrConstant(0));
6841
6842 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006843 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006844 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006845 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006846 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006847 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006848 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 MVT::v2f64, Bias)));
6850 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006851 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006852 DAG.getIntPtrConstant(0));
6853
6854 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006856
6857 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006858 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006859
Owen Anderson825b72b2009-08-11 20:47:22 +00006860 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006861 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006862 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006863 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006864 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006865 }
6866
6867 // Handle final rounding.
6868 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006869}
6870
Dan Gohmand858e902010-04-17 15:26:15 +00006871SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6872 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006873 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006874 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006875
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006876 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006877 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6878 // the optimization here.
6879 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006880 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006881
Owen Andersone50ed302009-08-10 22:56:29 +00006882 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006883 EVT DstVT = Op.getValueType();
6884 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006885 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006886 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006887 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006888
6889 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006890 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006891 if (SrcVT == MVT::i32) {
6892 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6893 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6894 getPointerTy(), StackSlot, WordOff);
6895 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006896 StackSlot, MachinePointerInfo(),
6897 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006898 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006899 OffsetSlot, MachinePointerInfo(),
6900 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006901 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6902 return Fild;
6903 }
6904
6905 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6906 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006907 StackSlot, MachinePointerInfo(),
6908 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006909 // For i64 source, we need to add the appropriate power of 2 if the input
6910 // was negative. This is the same as the optimization in
6911 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6912 // we must be careful to do the computation in x87 extended precision, not
6913 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006914 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6915 MachineMemOperand *MMO =
6916 DAG.getMachineFunction()
6917 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6918 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006919
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006920 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6921 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006922 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6923 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006924
6925 APInt FF(32, 0x5F800000ULL);
6926
6927 // Check whether the sign bit is set.
6928 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6929 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6930 ISD::SETLT);
6931
6932 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6933 SDValue FudgePtr = DAG.getConstantPool(
6934 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6935 getPointerTy());
6936
6937 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6938 SDValue Zero = DAG.getIntPtrConstant(0);
6939 SDValue Four = DAG.getIntPtrConstant(4);
6940 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6941 Zero, Four);
6942 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6943
6944 // Load the value out, extending it from f32 to f80.
6945 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00006946 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006947 FudgePtr, MachinePointerInfo::getConstantPool(),
6948 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006949 // Extend everything to 80 bits to force it to be done on x87.
6950 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6951 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006952}
6953
Dan Gohman475871a2008-07-27 21:46:04 +00006954std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006955FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006956 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006957
Owen Andersone50ed302009-08-10 22:56:29 +00006958 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006959
6960 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6962 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006963 }
6964
Owen Anderson825b72b2009-08-11 20:47:22 +00006965 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6966 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006967 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006969 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006971 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006972 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006973 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006974 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006975 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006976 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006977
Evan Cheng87c89352007-10-15 20:11:21 +00006978 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6979 // stack slot.
6980 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006981 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006982 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006983 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006984
Michael J. Spencerec38de22010-10-10 22:04:20 +00006985
6986
Evan Cheng0db9fe62006-04-25 20:13:52 +00006987 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006989 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6991 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6992 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006993 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006994
Dan Gohman475871a2008-07-27 21:46:04 +00006995 SDValue Chain = DAG.getEntryNode();
6996 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006997 EVT TheVT = Op.getOperand(0).getValueType();
6998 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006999 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00007000 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007001 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007002 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007003 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00007004 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00007005 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00007006 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00007007
Chris Lattner492a43e2010-09-22 01:28:21 +00007008 MachineMemOperand *MMO =
7009 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7010 MachineMemOperand::MOLoad, MemSize, MemSize);
7011 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
7012 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007013 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00007014 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007015 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7016 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007017
Chris Lattner07290932010-09-22 01:05:16 +00007018 MachineMemOperand *MMO =
7019 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7020 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007021
Evan Cheng0db9fe62006-04-25 20:13:52 +00007022 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00007023 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00007024 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
7025 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00007026
Chris Lattner27a6c732007-11-24 07:07:01 +00007027 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007028}
7029
Dan Gohmand858e902010-04-17 15:26:15 +00007030SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7031 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00007032 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007033 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007034
Eli Friedman948e95a2009-05-23 09:59:16 +00007035 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00007036 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00007037 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7038 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00007039
Chris Lattner27a6c732007-11-24 07:07:01 +00007040 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007041 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007042 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00007043}
7044
Dan Gohmand858e902010-04-17 15:26:15 +00007045SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7046 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00007047 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7048 SDValue FIST = Vals.first, StackSlot = Vals.second;
7049 assert(FIST.getNode() && "Unexpected failure");
7050
7051 // Load the result.
7052 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00007053 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007054}
7055
Dan Gohmand858e902010-04-17 15:26:15 +00007056SDValue X86TargetLowering::LowerFABS(SDValue Op,
7057 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007058 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007059 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007060 EVT VT = Op.getValueType();
7061 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007062 if (VT.isVector())
7063 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007064 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007066 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00007067 CV.push_back(C);
7068 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007069 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007070 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00007071 CV.push_back(C);
7072 CV.push_back(C);
7073 CV.push_back(C);
7074 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007075 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007076 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007077 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007078 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007079 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007080 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007081 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007082}
7083
Dan Gohmand858e902010-04-17 15:26:15 +00007084SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007085 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007086 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007087 EVT VT = Op.getValueType();
7088 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00007089 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007090 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007091 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007093 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00007094 CV.push_back(C);
7095 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007096 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007097 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00007098 CV.push_back(C);
7099 CV.push_back(C);
7100 CV.push_back(C);
7101 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007102 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007103 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007104 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007105 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007106 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007107 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007108 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007109 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007110 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007111 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007112 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007113 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00007114 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007115 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00007116 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007117}
7118
Dan Gohmand858e902010-04-17 15:26:15 +00007119SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00007120 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00007121 SDValue Op0 = Op.getOperand(0);
7122 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007123 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007124 EVT VT = Op.getValueType();
7125 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00007126
7127 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007128 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007129 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007130 SrcVT = VT;
7131 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007132 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007133 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00007134 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007135 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00007136 }
7137
7138 // At this point the operands and the result should have the same
7139 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00007140
Evan Cheng68c47cb2007-01-05 07:55:56 +00007141 // First get the sign bit of second operand.
7142 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007144 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
7145 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007146 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007147 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
7148 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7149 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7150 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007151 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007152 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007153 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007154 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007155 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007156 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007157 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007158
7159 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00007160 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 // Op0 is MVT::f32, Op1 is MVT::f64.
7162 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
7163 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
7164 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007165 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00007166 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00007167 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00007168 }
7169
Evan Cheng73d6cf12007-01-05 21:37:56 +00007170 // Clear first operand sign bit.
7171 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00007172 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007173 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
7174 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007175 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00007176 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
7177 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7178 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
7179 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00007180 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00007181 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007182 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007183 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00007184 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00007185 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00007186 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00007187
7188 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00007189 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007190}
7191
Dan Gohman076aee32009-03-04 19:44:21 +00007192/// Emit nodes that will be selected as "test Op0,Op0", or something
7193/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007194SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007195 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007196 DebugLoc dl = Op.getDebugLoc();
7197
Dan Gohman31125812009-03-07 01:58:32 +00007198 // CF and OF aren't always set the way we want. Determine which
7199 // of these we need.
7200 bool NeedCF = false;
7201 bool NeedOF = false;
7202 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007203 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00007204 case X86::COND_A: case X86::COND_AE:
7205 case X86::COND_B: case X86::COND_BE:
7206 NeedCF = true;
7207 break;
7208 case X86::COND_G: case X86::COND_GE:
7209 case X86::COND_L: case X86::COND_LE:
7210 case X86::COND_O: case X86::COND_NO:
7211 NeedOF = true;
7212 break;
Dan Gohman31125812009-03-07 01:58:32 +00007213 }
7214
Dan Gohman076aee32009-03-04 19:44:21 +00007215 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00007216 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
7217 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007218 if (Op.getResNo() != 0 || NeedOF || NeedCF)
7219 // Emit a CMP with 0, which is the TEST pattern.
7220 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7221 DAG.getConstant(0, Op.getValueType()));
7222
7223 unsigned Opcode = 0;
7224 unsigned NumOperands = 0;
7225 switch (Op.getNode()->getOpcode()) {
7226 case ISD::ADD:
7227 // Due to an isel shortcoming, be conservative if this add is likely to be
7228 // selected as part of a load-modify-store instruction. When the root node
7229 // in a match is a store, isel doesn't know how to remap non-chain non-flag
7230 // uses of other nodes in the match, such as the ADD in this case. This
7231 // leads to the ADD being left around and reselected, with the result being
7232 // two adds in the output. Alas, even if none our users are stores, that
7233 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
7234 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
7235 // climbing the DAG back to the root, and it doesn't seem to be worth the
7236 // effort.
7237 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00007238 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007239 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
7240 goto default_case;
7241
7242 if (ConstantSDNode *C =
7243 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
7244 // An add of one will be selected as an INC.
7245 if (C->getAPIntValue() == 1) {
7246 Opcode = X86ISD::INC;
7247 NumOperands = 1;
7248 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00007249 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007250
7251 // An add of negative one (subtract of one) will be selected as a DEC.
7252 if (C->getAPIntValue().isAllOnesValue()) {
7253 Opcode = X86ISD::DEC;
7254 NumOperands = 1;
7255 break;
7256 }
Dan Gohman076aee32009-03-04 19:44:21 +00007257 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007258
7259 // Otherwise use a regular EFLAGS-setting add.
7260 Opcode = X86ISD::ADD;
7261 NumOperands = 2;
7262 break;
7263 case ISD::AND: {
7264 // If the primary and result isn't used, don't bother using X86ISD::AND,
7265 // because a TEST instruction will be better.
7266 bool NonFlagUse = false;
7267 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7268 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
7269 SDNode *User = *UI;
7270 unsigned UOpNo = UI.getOperandNo();
7271 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
7272 // Look pass truncate.
7273 UOpNo = User->use_begin().getOperandNo();
7274 User = *User->use_begin();
7275 }
7276
7277 if (User->getOpcode() != ISD::BRCOND &&
7278 User->getOpcode() != ISD::SETCC &&
7279 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
7280 NonFlagUse = true;
7281 break;
7282 }
Dan Gohman076aee32009-03-04 19:44:21 +00007283 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007284
7285 if (!NonFlagUse)
7286 break;
7287 }
7288 // FALL THROUGH
7289 case ISD::SUB:
7290 case ISD::OR:
7291 case ISD::XOR:
7292 // Due to the ISEL shortcoming noted above, be conservative if this op is
7293 // likely to be selected as part of a load-modify-store instruction.
7294 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
7295 UE = Op.getNode()->use_end(); UI != UE; ++UI)
7296 if (UI->getOpcode() == ISD::STORE)
7297 goto default_case;
7298
7299 // Otherwise use a regular EFLAGS-setting instruction.
7300 switch (Op.getNode()->getOpcode()) {
7301 default: llvm_unreachable("unexpected operator!");
7302 case ISD::SUB: Opcode = X86ISD::SUB; break;
7303 case ISD::OR: Opcode = X86ISD::OR; break;
7304 case ISD::XOR: Opcode = X86ISD::XOR; break;
7305 case ISD::AND: Opcode = X86ISD::AND; break;
7306 }
7307
7308 NumOperands = 2;
7309 break;
7310 case X86ISD::ADD:
7311 case X86ISD::SUB:
7312 case X86ISD::INC:
7313 case X86ISD::DEC:
7314 case X86ISD::OR:
7315 case X86ISD::XOR:
7316 case X86ISD::AND:
7317 return SDValue(Op.getNode(), 1);
7318 default:
7319 default_case:
7320 break;
Dan Gohman076aee32009-03-04 19:44:21 +00007321 }
7322
Bill Wendlingc25ccf82010-06-28 21:08:32 +00007323 if (Opcode == 0)
7324 // Emit a CMP with 0, which is the TEST pattern.
7325 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
7326 DAG.getConstant(0, Op.getValueType()));
7327
7328 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
7329 SmallVector<SDValue, 4> Ops;
7330 for (unsigned i = 0; i != NumOperands; ++i)
7331 Ops.push_back(Op.getOperand(i));
7332
7333 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
7334 DAG.ReplaceAllUsesWith(Op, New);
7335 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00007336}
7337
7338/// Emit nodes that will be selected as "cmp Op0,Op1", or something
7339/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00007340SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00007341 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00007342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
7343 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00007344 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00007345
7346 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007347 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00007348}
7349
Evan Chengd40d03e2010-01-06 19:38:29 +00007350/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7351/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007352SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7353 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007354 SDValue Op0 = And.getOperand(0);
7355 SDValue Op1 = And.getOperand(1);
7356 if (Op0.getOpcode() == ISD::TRUNCATE)
7357 Op0 = Op0.getOperand(0);
7358 if (Op1.getOpcode() == ISD::TRUNCATE)
7359 Op1 = Op1.getOperand(0);
7360
Evan Chengd40d03e2010-01-06 19:38:29 +00007361 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007362 if (Op1.getOpcode() == ISD::SHL)
7363 std::swap(Op0, Op1);
7364 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007365 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7366 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007367 // If we looked past a truncate, check that it's only truncating away
7368 // known zeros.
7369 unsigned BitWidth = Op0.getValueSizeInBits();
7370 unsigned AndBitWidth = And.getValueSizeInBits();
7371 if (BitWidth > AndBitWidth) {
7372 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7373 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7374 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7375 return SDValue();
7376 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007377 LHS = Op1;
7378 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007379 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007380 } else if (Op1.getOpcode() == ISD::Constant) {
7381 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7382 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007383 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7384 LHS = AndLHS.getOperand(0);
7385 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007386 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007387 }
Evan Cheng0488db92007-09-25 01:57:46 +00007388
Evan Chengd40d03e2010-01-06 19:38:29 +00007389 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007390 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007391 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007392 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007393 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007394 // Also promote i16 to i32 for performance / code size reason.
7395 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007396 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007397 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007398
Evan Chengd40d03e2010-01-06 19:38:29 +00007399 // If the operand types disagree, extend the shift amount to match. Since
7400 // BT ignores high bits (like shifts) we can use anyextend.
7401 if (LHS.getValueType() != RHS.getValueType())
7402 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007403
Evan Chengd40d03e2010-01-06 19:38:29 +00007404 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7405 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7406 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7407 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007408 }
7409
Evan Cheng54de3ea2010-01-05 06:52:31 +00007410 return SDValue();
7411}
7412
Dan Gohmand858e902010-04-17 15:26:15 +00007413SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007414 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7415 SDValue Op0 = Op.getOperand(0);
7416 SDValue Op1 = Op.getOperand(1);
7417 DebugLoc dl = Op.getDebugLoc();
7418 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7419
7420 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007421 // Lower (X & (1 << N)) == 0 to BT(X, N).
7422 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7423 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007424 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007425 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007426 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007427 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7428 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7429 if (NewSetCC.getNode())
7430 return NewSetCC;
7431 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007432
Chris Lattner481eebc2010-12-19 21:23:48 +00007433 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7434 // these.
7435 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007436 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7437 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7438 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007439
Chris Lattner481eebc2010-12-19 21:23:48 +00007440 // If the input is a setcc, then reuse the input setcc or use a new one with
7441 // the inverted condition.
7442 if (Op0.getOpcode() == X86ISD::SETCC) {
7443 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7444 bool Invert = (CC == ISD::SETNE) ^
7445 cast<ConstantSDNode>(Op1)->isNullValue();
7446 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007447
Evan Cheng2c755ba2010-02-27 07:36:59 +00007448 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007449 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7450 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7451 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007452 }
7453
Evan Chenge5b51ac2010-04-17 06:13:15 +00007454 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007455 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007456 if (X86CC == X86::COND_INVALID)
7457 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007458
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007459 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007460 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007461 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007462}
7463
Dan Gohmand858e902010-04-17 15:26:15 +00007464SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007465 SDValue Cond;
7466 SDValue Op0 = Op.getOperand(0);
7467 SDValue Op1 = Op.getOperand(1);
7468 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007469 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007470 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7471 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007472 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007473
7474 if (isFP) {
7475 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007476 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007477 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7478 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007479 bool Swap = false;
7480
7481 switch (SetCCOpcode) {
7482 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007483 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007484 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007485 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007486 case ISD::SETGT: Swap = true; // Fallthrough
7487 case ISD::SETLT:
7488 case ISD::SETOLT: SSECC = 1; break;
7489 case ISD::SETOGE:
7490 case ISD::SETGE: Swap = true; // Fallthrough
7491 case ISD::SETLE:
7492 case ISD::SETOLE: SSECC = 2; break;
7493 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007494 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007495 case ISD::SETNE: SSECC = 4; break;
7496 case ISD::SETULE: Swap = true;
7497 case ISD::SETUGE: SSECC = 5; break;
7498 case ISD::SETULT: Swap = true;
7499 case ISD::SETUGT: SSECC = 6; break;
7500 case ISD::SETO: SSECC = 7; break;
7501 }
7502 if (Swap)
7503 std::swap(Op0, Op1);
7504
Nate Begemanfb8ead02008-07-25 19:05:58 +00007505 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007506 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007507 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007508 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007509 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7510 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007511 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007512 }
7513 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007514 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007515 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7516 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007517 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007518 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007519 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007520 }
7521 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007522 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007524
Nate Begeman30a0de92008-07-17 16:51:19 +00007525 // We are handling one of the integer comparisons here. Since SSE only has
7526 // GT and EQ comparisons for integer, swapping operands and multiple
7527 // operations may be required for some comparisons.
7528 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7529 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007530
Owen Anderson825b72b2009-08-11 20:47:22 +00007531 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007532 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007533 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007534 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007535 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7536 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007538
Nate Begeman30a0de92008-07-17 16:51:19 +00007539 switch (SetCCOpcode) {
7540 default: break;
7541 case ISD::SETNE: Invert = true;
7542 case ISD::SETEQ: Opc = EQOpc; break;
7543 case ISD::SETLT: Swap = true;
7544 case ISD::SETGT: Opc = GTOpc; break;
7545 case ISD::SETGE: Swap = true;
7546 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7547 case ISD::SETULT: Swap = true;
7548 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7549 case ISD::SETUGE: Swap = true;
7550 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7551 }
7552 if (Swap)
7553 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007554
Nate Begeman30a0de92008-07-17 16:51:19 +00007555 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7556 // bits of the inputs before performing those operations.
7557 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007558 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007559 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7560 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007561 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007562 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7563 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007564 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7565 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007567
Dale Johannesenace16102009-02-03 19:33:06 +00007568 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007569
7570 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007571 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007572 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007573
Nate Begeman30a0de92008-07-17 16:51:19 +00007574 return Result;
7575}
Evan Cheng0488db92007-09-25 01:57:46 +00007576
Evan Cheng370e5342008-12-03 08:38:43 +00007577// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007578static bool isX86LogicalCmp(SDValue Op) {
7579 unsigned Opc = Op.getNode()->getOpcode();
7580 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7581 return true;
7582 if (Op.getResNo() == 1 &&
7583 (Opc == X86ISD::ADD ||
7584 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007585 Opc == X86ISD::ADC ||
7586 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007587 Opc == X86ISD::SMUL ||
7588 Opc == X86ISD::UMUL ||
7589 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007590 Opc == X86ISD::DEC ||
7591 Opc == X86ISD::OR ||
7592 Opc == X86ISD::XOR ||
7593 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007594 return true;
7595
Chris Lattner9637d5b2010-12-05 07:49:54 +00007596 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7597 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007598
Dan Gohman076aee32009-03-04 19:44:21 +00007599 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007600}
7601
Chris Lattnera2b56002010-12-05 01:23:24 +00007602static bool isZero(SDValue V) {
7603 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7604 return C && C->isNullValue();
7605}
7606
Chris Lattner96908b12010-12-05 02:00:51 +00007607static bool isAllOnes(SDValue V) {
7608 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7609 return C && C->isAllOnesValue();
7610}
7611
Dan Gohmand858e902010-04-17 15:26:15 +00007612SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007613 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007614 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007615 SDValue Op1 = Op.getOperand(1);
7616 SDValue Op2 = Op.getOperand(2);
7617 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007618 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007619
Dan Gohman1a492952009-10-20 16:22:37 +00007620 if (Cond.getOpcode() == ISD::SETCC) {
7621 SDValue NewCond = LowerSETCC(Cond, DAG);
7622 if (NewCond.getNode())
7623 Cond = NewCond;
7624 }
Evan Cheng734503b2006-09-11 02:19:56 +00007625
Chris Lattnera2b56002010-12-05 01:23:24 +00007626 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007627 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007628 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007629 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007630 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007631 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7632 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007633 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007634
Chris Lattnera2b56002010-12-05 01:23:24 +00007635 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007636
7637 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007638 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7639 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007640
7641 SDValue CmpOp0 = Cmp.getOperand(0);
7642 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7643 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007644
Chris Lattner96908b12010-12-05 02:00:51 +00007645 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007646 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7647 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007648
Chris Lattner96908b12010-12-05 02:00:51 +00007649 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7650 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007651
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007652 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007653 if (N2C == 0 || !N2C->isNullValue())
7654 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7655 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007656 }
7657 }
7658
Chris Lattnera2b56002010-12-05 01:23:24 +00007659 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007660 if (Cond.getOpcode() == ISD::AND &&
7661 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7662 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007663 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007664 Cond = Cond.getOperand(0);
7665 }
7666
Evan Cheng3f41d662007-10-08 22:16:29 +00007667 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7668 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007669 if (Cond.getOpcode() == X86ISD::SETCC ||
7670 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007671 CC = Cond.getOperand(0);
7672
Dan Gohman475871a2008-07-27 21:46:04 +00007673 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007674 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007675 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007676
Evan Cheng3f41d662007-10-08 22:16:29 +00007677 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007678 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007679 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007680 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007681
Chris Lattnerd1980a52009-03-12 06:52:53 +00007682 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7683 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007684 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007685 addTest = false;
7686 }
7687 }
7688
7689 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007690 // Look pass the truncate.
7691 if (Cond.getOpcode() == ISD::TRUNCATE)
7692 Cond = Cond.getOperand(0);
7693
7694 // We know the result of AND is compared against zero. Try to match
7695 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007696 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007697 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007698 if (NewSetCC.getNode()) {
7699 CC = NewSetCC.getOperand(0);
7700 Cond = NewSetCC.getOperand(1);
7701 addTest = false;
7702 }
7703 }
7704 }
7705
7706 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007707 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007708 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007709 }
7710
Benjamin Kramere915ff32010-12-22 23:09:28 +00007711 // a < b ? -1 : 0 -> RES = ~setcc_carry
7712 // a < b ? 0 : -1 -> RES = setcc_carry
7713 // a >= b ? -1 : 0 -> RES = setcc_carry
7714 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7715 if (Cond.getOpcode() == X86ISD::CMP) {
7716 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7717
7718 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7719 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7720 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7721 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7722 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7723 return DAG.getNOT(DL, Res, Res.getValueType());
7724 return Res;
7725 }
7726 }
7727
Evan Cheng0488db92007-09-25 01:57:46 +00007728 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7729 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007730 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007731 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007732 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007733}
7734
Evan Cheng370e5342008-12-03 08:38:43 +00007735// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7736// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7737// from the AND / OR.
7738static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7739 Opc = Op.getOpcode();
7740 if (Opc != ISD::OR && Opc != ISD::AND)
7741 return false;
7742 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7743 Op.getOperand(0).hasOneUse() &&
7744 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7745 Op.getOperand(1).hasOneUse());
7746}
7747
Evan Cheng961d6d42009-02-02 08:19:07 +00007748// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7749// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007750static bool isXor1OfSetCC(SDValue Op) {
7751 if (Op.getOpcode() != ISD::XOR)
7752 return false;
7753 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7754 if (N1C && N1C->getAPIntValue() == 1) {
7755 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7756 Op.getOperand(0).hasOneUse();
7757 }
7758 return false;
7759}
7760
Dan Gohmand858e902010-04-17 15:26:15 +00007761SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007762 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007763 SDValue Chain = Op.getOperand(0);
7764 SDValue Cond = Op.getOperand(1);
7765 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007766 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007767 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007768
Dan Gohman1a492952009-10-20 16:22:37 +00007769 if (Cond.getOpcode() == ISD::SETCC) {
7770 SDValue NewCond = LowerSETCC(Cond, DAG);
7771 if (NewCond.getNode())
7772 Cond = NewCond;
7773 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007774#if 0
7775 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007776 else if (Cond.getOpcode() == X86ISD::ADD ||
7777 Cond.getOpcode() == X86ISD::SUB ||
7778 Cond.getOpcode() == X86ISD::SMUL ||
7779 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007780 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007781#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007782
Evan Chengad9c0a32009-12-15 00:53:42 +00007783 // Look pass (and (setcc_carry (cmp ...)), 1).
7784 if (Cond.getOpcode() == ISD::AND &&
7785 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7786 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007787 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007788 Cond = Cond.getOperand(0);
7789 }
7790
Evan Cheng3f41d662007-10-08 22:16:29 +00007791 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7792 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007793 if (Cond.getOpcode() == X86ISD::SETCC ||
7794 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007795 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007796
Dan Gohman475871a2008-07-27 21:46:04 +00007797 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007798 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007799 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007800 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007801 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007802 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007803 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007804 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007805 default: break;
7806 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007807 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007808 // These can only come from an arithmetic instruction with overflow,
7809 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007810 Cond = Cond.getNode()->getOperand(1);
7811 addTest = false;
7812 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007813 }
Evan Cheng0488db92007-09-25 01:57:46 +00007814 }
Evan Cheng370e5342008-12-03 08:38:43 +00007815 } else {
7816 unsigned CondOpc;
7817 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7818 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007819 if (CondOpc == ISD::OR) {
7820 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7821 // two branches instead of an explicit OR instruction with a
7822 // separate test.
7823 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007824 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007825 CC = Cond.getOperand(0).getOperand(0);
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 CC = Cond.getOperand(1).getOperand(0);
7829 Cond = Cmp;
7830 addTest = false;
7831 }
7832 } else { // ISD::AND
7833 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7834 // two branches instead of an explicit AND instruction with a
7835 // separate test. However, we only do this if this block doesn't
7836 // have a fall-through edge, because this requires an explicit
7837 // jmp when the condition is false.
7838 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007839 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007840 Op.getNode()->hasOneUse()) {
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);
Dan Gohman027657d2010-06-18 15:30:29 +00007845 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007846 // Look for an unconditional branch following this conditional branch.
7847 // We need this because we need to reverse the successors in order
7848 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007849 if (User->getOpcode() == ISD::BR) {
7850 SDValue FalseBB = User->getOperand(1);
7851 SDNode *NewBR =
7852 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007853 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007854 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007855 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007856
Dale Johannesene4d209d2009-02-03 20:21:25 +00007857 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007858 Chain, Dest, CC, Cmp);
7859 X86::CondCode CCode =
7860 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7861 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007862 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007863 Cond = Cmp;
7864 addTest = false;
7865 }
7866 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007867 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007868 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7869 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7870 // It should be transformed during dag combiner except when the condition
7871 // is set by a arithmetics with overflow node.
7872 X86::CondCode CCode =
7873 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7874 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007875 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007876 Cond = Cond.getOperand(0).getOperand(1);
7877 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007878 }
Evan Cheng0488db92007-09-25 01:57:46 +00007879 }
7880
7881 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007882 // Look pass the truncate.
7883 if (Cond.getOpcode() == ISD::TRUNCATE)
7884 Cond = Cond.getOperand(0);
7885
7886 // We know the result of AND is compared against zero. Try to match
7887 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007888 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007889 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7890 if (NewSetCC.getNode()) {
7891 CC = NewSetCC.getOperand(0);
7892 Cond = NewSetCC.getOperand(1);
7893 addTest = false;
7894 }
7895 }
7896 }
7897
7898 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007899 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007900 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007901 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007902 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007903 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007904}
7905
Anton Korobeynikove060b532007-04-17 19:34:00 +00007906
7907// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7908// Calls to _alloca is needed to probe the stack when allocating more than 4k
7909// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7910// that the guard pages used by the OS virtual memory manager are allocated in
7911// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007912SDValue
7913X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007914 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007915 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007916 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007917 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007918
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007919 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007920 SDValue Chain = Op.getOperand(0);
7921 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007922 // FIXME: Ensure alignment here
7923
Dan Gohman475871a2008-07-27 21:46:04 +00007924 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007925
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007927
Dale Johannesendd64c412009-02-04 00:33:20 +00007928 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007929 Flag = Chain.getValue(1);
7930
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007931 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007932
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007933 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007934 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007935
Dale Johannesendd64c412009-02-04 00:33:20 +00007936 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007937
Dan Gohman475871a2008-07-27 21:46:04 +00007938 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007939 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007940}
7941
Dan Gohmand858e902010-04-17 15:26:15 +00007942SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007943 MachineFunction &MF = DAG.getMachineFunction();
7944 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7945
Dan Gohman69de1932008-02-06 22:27:42 +00007946 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007947 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007948
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007949 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007950 // vastart just stores the address of the VarArgsFrameIndex slot into the
7951 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007952 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7953 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007954 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7955 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007956 }
7957
7958 // __va_list_tag:
7959 // gp_offset (0 - 6 * 8)
7960 // fp_offset (48 - 48 + 8 * 16)
7961 // overflow_arg_area (point to parameters coming in memory).
7962 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007963 SmallVector<SDValue, 8> MemOps;
7964 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007965 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007966 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007967 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7968 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007969 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007970 MemOps.push_back(Store);
7971
7972 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007973 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007974 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007975 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007976 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7977 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007978 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007979 MemOps.push_back(Store);
7980
7981 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007982 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007983 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007984 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7985 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007986 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7987 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007988 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007989 MemOps.push_back(Store);
7990
7991 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007992 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007993 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007994 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7995 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007996 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7997 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007998 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007999 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00008000 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00008001}
8002
Dan Gohmand858e902010-04-17 15:26:15 +00008003SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00008004 assert(Subtarget->is64Bit() &&
8005 "LowerVAARG only handles 64-bit va_arg!");
8006 assert((Subtarget->isTargetLinux() ||
8007 Subtarget->isTargetDarwin()) &&
8008 "Unhandled target in LowerVAARG");
8009 assert(Op.getNode()->getNumOperands() == 4);
8010 SDValue Chain = Op.getOperand(0);
8011 SDValue SrcPtr = Op.getOperand(1);
8012 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
8013 unsigned Align = Op.getConstantOperandVal(3);
8014 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00008015
Dan Gohman320afb82010-10-12 18:00:49 +00008016 EVT ArgVT = Op.getNode()->getValueType(0);
8017 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
8018 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
8019 uint8_t ArgMode;
8020
8021 // Decide which area this value should be read from.
8022 // TODO: Implement the AMD64 ABI in its entirety. This simple
8023 // selection mechanism works only for the basic types.
8024 if (ArgVT == MVT::f80) {
8025 llvm_unreachable("va_arg for f80 not yet implemented");
8026 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
8027 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
8028 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
8029 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
8030 } else {
8031 llvm_unreachable("Unhandled argument type in LowerVAARG");
8032 }
8033
8034 if (ArgMode == 2) {
8035 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00008036 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00008037 !(DAG.getMachineFunction()
8038 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00008039 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00008040 }
8041
8042 // Insert VAARG_64 node into the DAG
8043 // VAARG_64 returns two values: Variable Argument Address, Chain
8044 SmallVector<SDValue, 11> InstOps;
8045 InstOps.push_back(Chain);
8046 InstOps.push_back(SrcPtr);
8047 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
8048 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
8049 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
8050 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
8051 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
8052 VTs, &InstOps[0], InstOps.size(),
8053 MVT::i64,
8054 MachinePointerInfo(SV),
8055 /*Align=*/0,
8056 /*Volatile=*/false,
8057 /*ReadMem=*/true,
8058 /*WriteMem=*/true);
8059 Chain = VAARG.getValue(1);
8060
8061 // Load the next argument and return it
8062 return DAG.getLoad(ArgVT, dl,
8063 Chain,
8064 VAARG,
8065 MachinePointerInfo(),
8066 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00008067}
8068
Dan Gohmand858e902010-04-17 15:26:15 +00008069SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00008070 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00008071 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00008072 SDValue Chain = Op.getOperand(0);
8073 SDValue DstPtr = Op.getOperand(1);
8074 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00008075 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
8076 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00008077 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00008078
Chris Lattnere72f2022010-09-21 05:40:29 +00008079 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00008080 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008081 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00008082 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00008083}
8084
Dan Gohman475871a2008-07-27 21:46:04 +00008085SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008086X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008087 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008088 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008089 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00008090 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00008091 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00008092 case Intrinsic::x86_sse_comieq_ss:
8093 case Intrinsic::x86_sse_comilt_ss:
8094 case Intrinsic::x86_sse_comile_ss:
8095 case Intrinsic::x86_sse_comigt_ss:
8096 case Intrinsic::x86_sse_comige_ss:
8097 case Intrinsic::x86_sse_comineq_ss:
8098 case Intrinsic::x86_sse_ucomieq_ss:
8099 case Intrinsic::x86_sse_ucomilt_ss:
8100 case Intrinsic::x86_sse_ucomile_ss:
8101 case Intrinsic::x86_sse_ucomigt_ss:
8102 case Intrinsic::x86_sse_ucomige_ss:
8103 case Intrinsic::x86_sse_ucomineq_ss:
8104 case Intrinsic::x86_sse2_comieq_sd:
8105 case Intrinsic::x86_sse2_comilt_sd:
8106 case Intrinsic::x86_sse2_comile_sd:
8107 case Intrinsic::x86_sse2_comigt_sd:
8108 case Intrinsic::x86_sse2_comige_sd:
8109 case Intrinsic::x86_sse2_comineq_sd:
8110 case Intrinsic::x86_sse2_ucomieq_sd:
8111 case Intrinsic::x86_sse2_ucomilt_sd:
8112 case Intrinsic::x86_sse2_ucomile_sd:
8113 case Intrinsic::x86_sse2_ucomigt_sd:
8114 case Intrinsic::x86_sse2_ucomige_sd:
8115 case Intrinsic::x86_sse2_ucomineq_sd: {
8116 unsigned Opc = 0;
8117 ISD::CondCode CC = ISD::SETCC_INVALID;
8118 switch (IntNo) {
8119 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008120 case Intrinsic::x86_sse_comieq_ss:
8121 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008122 Opc = X86ISD::COMI;
8123 CC = ISD::SETEQ;
8124 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008125 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008126 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008127 Opc = X86ISD::COMI;
8128 CC = ISD::SETLT;
8129 break;
8130 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008131 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008132 Opc = X86ISD::COMI;
8133 CC = ISD::SETLE;
8134 break;
8135 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008136 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008137 Opc = X86ISD::COMI;
8138 CC = ISD::SETGT;
8139 break;
8140 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008141 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008142 Opc = X86ISD::COMI;
8143 CC = ISD::SETGE;
8144 break;
8145 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008146 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008147 Opc = X86ISD::COMI;
8148 CC = ISD::SETNE;
8149 break;
8150 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008151 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008152 Opc = X86ISD::UCOMI;
8153 CC = ISD::SETEQ;
8154 break;
8155 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008156 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008157 Opc = X86ISD::UCOMI;
8158 CC = ISD::SETLT;
8159 break;
8160 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008161 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008162 Opc = X86ISD::UCOMI;
8163 CC = ISD::SETLE;
8164 break;
8165 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008166 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008167 Opc = X86ISD::UCOMI;
8168 CC = ISD::SETGT;
8169 break;
8170 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00008171 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00008172 Opc = X86ISD::UCOMI;
8173 CC = ISD::SETGE;
8174 break;
8175 case Intrinsic::x86_sse_ucomineq_ss:
8176 case Intrinsic::x86_sse2_ucomineq_sd:
8177 Opc = X86ISD::UCOMI;
8178 CC = ISD::SETNE;
8179 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00008180 }
Evan Cheng734503b2006-09-11 02:19:56 +00008181
Dan Gohman475871a2008-07-27 21:46:04 +00008182 SDValue LHS = Op.getOperand(1);
8183 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00008184 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008185 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008186 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
8187 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8188 DAG.getConstant(X86CC, MVT::i8), Cond);
8189 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00008190 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008191 // ptest and testp intrinsics. The intrinsic these come from are designed to
8192 // return an integer value, not just an instruction so lower it to the ptest
8193 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00008194 case Intrinsic::x86_sse41_ptestz:
8195 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008196 case Intrinsic::x86_sse41_ptestnzc:
8197 case Intrinsic::x86_avx_ptestz_256:
8198 case Intrinsic::x86_avx_ptestc_256:
8199 case Intrinsic::x86_avx_ptestnzc_256:
8200 case Intrinsic::x86_avx_vtestz_ps:
8201 case Intrinsic::x86_avx_vtestc_ps:
8202 case Intrinsic::x86_avx_vtestnzc_ps:
8203 case Intrinsic::x86_avx_vtestz_pd:
8204 case Intrinsic::x86_avx_vtestc_pd:
8205 case Intrinsic::x86_avx_vtestnzc_pd:
8206 case Intrinsic::x86_avx_vtestz_ps_256:
8207 case Intrinsic::x86_avx_vtestc_ps_256:
8208 case Intrinsic::x86_avx_vtestnzc_ps_256:
8209 case Intrinsic::x86_avx_vtestz_pd_256:
8210 case Intrinsic::x86_avx_vtestc_pd_256:
8211 case Intrinsic::x86_avx_vtestnzc_pd_256: {
8212 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00008213 unsigned X86CC = 0;
8214 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00008215 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008216 case Intrinsic::x86_avx_vtestz_ps:
8217 case Intrinsic::x86_avx_vtestz_pd:
8218 case Intrinsic::x86_avx_vtestz_ps_256:
8219 case Intrinsic::x86_avx_vtestz_pd_256:
8220 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008221 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008222 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008223 // ZF = 1
8224 X86CC = X86::COND_E;
8225 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008226 case Intrinsic::x86_avx_vtestc_ps:
8227 case Intrinsic::x86_avx_vtestc_pd:
8228 case Intrinsic::x86_avx_vtestc_ps_256:
8229 case Intrinsic::x86_avx_vtestc_pd_256:
8230 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00008231 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008232 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008233 // CF = 1
8234 X86CC = X86::COND_B;
8235 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008236 case Intrinsic::x86_avx_vtestnzc_ps:
8237 case Intrinsic::x86_avx_vtestnzc_pd:
8238 case Intrinsic::x86_avx_vtestnzc_ps_256:
8239 case Intrinsic::x86_avx_vtestnzc_pd_256:
8240 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00008241 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008242 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00008243 // ZF and CF = 0
8244 X86CC = X86::COND_A;
8245 break;
8246 }
Eric Christopherfd179292009-08-27 18:07:15 +00008247
Eric Christopher71c67532009-07-29 00:28:05 +00008248 SDValue LHS = Op.getOperand(1);
8249 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00008250 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
8251 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00008252 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
8253 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
8254 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00008255 }
Evan Cheng5759f972008-05-04 09:15:50 +00008256
8257 // Fix vector shift instructions where the last operand is a non-immediate
8258 // i32 value.
8259 case Intrinsic::x86_sse2_pslli_w:
8260 case Intrinsic::x86_sse2_pslli_d:
8261 case Intrinsic::x86_sse2_pslli_q:
8262 case Intrinsic::x86_sse2_psrli_w:
8263 case Intrinsic::x86_sse2_psrli_d:
8264 case Intrinsic::x86_sse2_psrli_q:
8265 case Intrinsic::x86_sse2_psrai_w:
8266 case Intrinsic::x86_sse2_psrai_d:
8267 case Intrinsic::x86_mmx_pslli_w:
8268 case Intrinsic::x86_mmx_pslli_d:
8269 case Intrinsic::x86_mmx_pslli_q:
8270 case Intrinsic::x86_mmx_psrli_w:
8271 case Intrinsic::x86_mmx_psrli_d:
8272 case Intrinsic::x86_mmx_psrli_q:
8273 case Intrinsic::x86_mmx_psrai_w:
8274 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00008275 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00008276 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00008277 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00008278
8279 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008281 switch (IntNo) {
8282 case Intrinsic::x86_sse2_pslli_w:
8283 NewIntNo = Intrinsic::x86_sse2_psll_w;
8284 break;
8285 case Intrinsic::x86_sse2_pslli_d:
8286 NewIntNo = Intrinsic::x86_sse2_psll_d;
8287 break;
8288 case Intrinsic::x86_sse2_pslli_q:
8289 NewIntNo = Intrinsic::x86_sse2_psll_q;
8290 break;
8291 case Intrinsic::x86_sse2_psrli_w:
8292 NewIntNo = Intrinsic::x86_sse2_psrl_w;
8293 break;
8294 case Intrinsic::x86_sse2_psrli_d:
8295 NewIntNo = Intrinsic::x86_sse2_psrl_d;
8296 break;
8297 case Intrinsic::x86_sse2_psrli_q:
8298 NewIntNo = Intrinsic::x86_sse2_psrl_q;
8299 break;
8300 case Intrinsic::x86_sse2_psrai_w:
8301 NewIntNo = Intrinsic::x86_sse2_psra_w;
8302 break;
8303 case Intrinsic::x86_sse2_psrai_d:
8304 NewIntNo = Intrinsic::x86_sse2_psra_d;
8305 break;
8306 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00008307 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00008308 switch (IntNo) {
8309 case Intrinsic::x86_mmx_pslli_w:
8310 NewIntNo = Intrinsic::x86_mmx_psll_w;
8311 break;
8312 case Intrinsic::x86_mmx_pslli_d:
8313 NewIntNo = Intrinsic::x86_mmx_psll_d;
8314 break;
8315 case Intrinsic::x86_mmx_pslli_q:
8316 NewIntNo = Intrinsic::x86_mmx_psll_q;
8317 break;
8318 case Intrinsic::x86_mmx_psrli_w:
8319 NewIntNo = Intrinsic::x86_mmx_psrl_w;
8320 break;
8321 case Intrinsic::x86_mmx_psrli_d:
8322 NewIntNo = Intrinsic::x86_mmx_psrl_d;
8323 break;
8324 case Intrinsic::x86_mmx_psrli_q:
8325 NewIntNo = Intrinsic::x86_mmx_psrl_q;
8326 break;
8327 case Intrinsic::x86_mmx_psrai_w:
8328 NewIntNo = Intrinsic::x86_mmx_psra_w;
8329 break;
8330 case Intrinsic::x86_mmx_psrai_d:
8331 NewIntNo = Intrinsic::x86_mmx_psra_d;
8332 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008333 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00008334 }
8335 break;
8336 }
8337 }
Mon P Wangefa42202009-09-03 19:56:25 +00008338
8339 // The vector shift intrinsics with scalars uses 32b shift amounts but
8340 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
8341 // to be zero.
8342 SDValue ShOps[4];
8343 ShOps[0] = ShAmt;
8344 ShOps[1] = DAG.getConstant(0, MVT::i32);
8345 if (ShAmtVT == MVT::v4i32) {
8346 ShOps[2] = DAG.getUNDEF(MVT::i32);
8347 ShOps[3] = DAG.getUNDEF(MVT::i32);
8348 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
8349 } else {
8350 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008351// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008352 }
8353
Owen Andersone50ed302009-08-10 22:56:29 +00008354 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008355 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008356 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008357 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008358 Op.getOperand(1), ShAmt);
8359 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008360 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008361}
Evan Cheng72261582005-12-20 06:22:03 +00008362
Dan Gohmand858e902010-04-17 15:26:15 +00008363SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8364 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008365 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8366 MFI->setReturnAddressIsTaken(true);
8367
Bill Wendling64e87322009-01-16 19:25:27 +00008368 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008369 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008370
8371 if (Depth > 0) {
8372 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8373 SDValue Offset =
8374 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008375 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008376 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008377 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008378 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008379 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008380 }
8381
8382 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008383 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008384 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008385 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008386}
8387
Dan Gohmand858e902010-04-17 15:26:15 +00008388SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008389 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8390 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008391
Owen Andersone50ed302009-08-10 22:56:29 +00008392 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008393 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008394 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8395 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008396 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008397 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008398 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8399 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008400 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008401 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008402}
8403
Dan Gohman475871a2008-07-27 21:46:04 +00008404SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008405 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008406 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008407}
8408
Dan Gohmand858e902010-04-17 15:26:15 +00008409SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008410 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008411 SDValue Chain = Op.getOperand(0);
8412 SDValue Offset = Op.getOperand(1);
8413 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008414 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008415
Dan Gohmand8816272010-08-11 18:14:00 +00008416 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8417 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8418 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008419 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008420
Dan Gohmand8816272010-08-11 18:14:00 +00008421 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8422 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008423 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008424 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8425 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008426 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008427 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008428
Dale Johannesene4d209d2009-02-03 20:21:25 +00008429 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008430 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008431 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008432}
8433
Dan Gohman475871a2008-07-27 21:46:04 +00008434SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008435 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008436 SDValue Root = Op.getOperand(0);
8437 SDValue Trmp = Op.getOperand(1); // trampoline
8438 SDValue FPtr = Op.getOperand(2); // nested function
8439 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008440 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008441
Dan Gohman69de1932008-02-06 22:27:42 +00008442 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008443
8444 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008445 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008446
8447 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008448 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8449 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008450
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008451 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8452 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008453
8454 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8455
8456 // Load the pointer to the nested function into R11.
8457 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008458 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008459 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008460 Addr, MachinePointerInfo(TrmpAddr),
8461 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008462
Owen Anderson825b72b2009-08-11 20:47:22 +00008463 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8464 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008465 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8466 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008467 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008468
8469 // Load the 'nest' parameter value into R10.
8470 // R10 is specified in X86CallingConv.td
8471 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008472 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8473 DAG.getConstant(10, MVT::i64));
8474 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008475 Addr, MachinePointerInfo(TrmpAddr, 10),
8476 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008477
Owen Anderson825b72b2009-08-11 20:47:22 +00008478 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8479 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008480 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8481 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008482 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008483
8484 // Jump to the nested function.
8485 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008486 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8487 DAG.getConstant(20, MVT::i64));
8488 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008489 Addr, MachinePointerInfo(TrmpAddr, 20),
8490 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008491
8492 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008493 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8494 DAG.getConstant(22, MVT::i64));
8495 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008496 MachinePointerInfo(TrmpAddr, 22),
8497 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008498
Dan Gohman475871a2008-07-27 21:46:04 +00008499 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008500 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008501 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008502 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008503 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008504 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008505 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008506 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008507
8508 switch (CC) {
8509 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008510 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008511 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008512 case CallingConv::X86_StdCall: {
8513 // Pass 'nest' parameter in ECX.
8514 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008515 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008516
8517 // Check that ECX wasn't needed by an 'inreg' parameter.
8518 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008519 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008520
Chris Lattner58d74912008-03-12 17:45:29 +00008521 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008522 unsigned InRegCount = 0;
8523 unsigned Idx = 1;
8524
8525 for (FunctionType::param_iterator I = FTy->param_begin(),
8526 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008527 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008528 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008529 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008530
8531 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008532 report_fatal_error("Nest register in use - reduce number of inreg"
8533 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008534 }
8535 }
8536 break;
8537 }
8538 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008539 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008540 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008541 // Pass 'nest' parameter in EAX.
8542 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008543 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008544 break;
8545 }
8546
Dan Gohman475871a2008-07-27 21:46:04 +00008547 SDValue OutChains[4];
8548 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008549
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8551 DAG.getConstant(10, MVT::i32));
8552 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008553
Chris Lattnera62fe662010-02-05 19:20:30 +00008554 // This is storing the opcode for MOV32ri.
8555 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008556 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008557 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008558 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008559 Trmp, MachinePointerInfo(TrmpAddr),
8560 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008561
Owen Anderson825b72b2009-08-11 20:47:22 +00008562 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8563 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008564 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8565 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008566 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008567
Chris Lattnera62fe662010-02-05 19:20:30 +00008568 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8570 DAG.getConstant(5, MVT::i32));
8571 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008572 MachinePointerInfo(TrmpAddr, 5),
8573 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008574
Owen Anderson825b72b2009-08-11 20:47:22 +00008575 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8576 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008577 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8578 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008579 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008580
Dan Gohman475871a2008-07-27 21:46:04 +00008581 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008582 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008583 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008584 }
8585}
8586
Dan Gohmand858e902010-04-17 15:26:15 +00008587SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8588 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008589 /*
8590 The rounding mode is in bits 11:10 of FPSR, and has the following
8591 settings:
8592 00 Round to nearest
8593 01 Round to -inf
8594 10 Round to +inf
8595 11 Round to 0
8596
8597 FLT_ROUNDS, on the other hand, expects the following:
8598 -1 Undefined
8599 0 Round to 0
8600 1 Round to nearest
8601 2 Round to +inf
8602 3 Round to -inf
8603
8604 To perform the conversion, we do:
8605 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8606 */
8607
8608 MachineFunction &MF = DAG.getMachineFunction();
8609 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008610 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008611 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008612 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008613 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008614
8615 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008616 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008617 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008618
Michael J. Spencerec38de22010-10-10 22:04:20 +00008619
Chris Lattner2156b792010-09-22 01:11:26 +00008620 MachineMemOperand *MMO =
8621 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8622 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008623
Chris Lattner2156b792010-09-22 01:11:26 +00008624 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8625 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8626 DAG.getVTList(MVT::Other),
8627 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008628
8629 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008630 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008631 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008632
8633 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008634 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008635 DAG.getNode(ISD::SRL, DL, MVT::i16,
8636 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008637 CWD, DAG.getConstant(0x800, MVT::i16)),
8638 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008639 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008640 DAG.getNode(ISD::SRL, DL, MVT::i16,
8641 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008642 CWD, DAG.getConstant(0x400, MVT::i16)),
8643 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008644
Dan Gohman475871a2008-07-27 21:46:04 +00008645 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008646 DAG.getNode(ISD::AND, DL, MVT::i16,
8647 DAG.getNode(ISD::ADD, DL, MVT::i16,
8648 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008649 DAG.getConstant(1, MVT::i16)),
8650 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008651
8652
Duncan Sands83ec4b62008-06-06 12:08:01 +00008653 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008654 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008655}
8656
Dan Gohmand858e902010-04-17 15:26:15 +00008657SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008658 EVT VT = Op.getValueType();
8659 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008660 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008661 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008662
8663 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008664 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008665 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008666 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008667 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008668 }
Evan Cheng18efe262007-12-14 02:13:44 +00008669
Evan Cheng152804e2007-12-14 08:30:15 +00008670 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008671 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008672 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008673
8674 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008675 SDValue Ops[] = {
8676 Op,
8677 DAG.getConstant(NumBits+NumBits-1, OpVT),
8678 DAG.getConstant(X86::COND_E, MVT::i8),
8679 Op.getValue(1)
8680 };
8681 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008682
8683 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008684 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008685
Owen Anderson825b72b2009-08-11 20:47:22 +00008686 if (VT == MVT::i8)
8687 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008688 return Op;
8689}
8690
Dan Gohmand858e902010-04-17 15:26:15 +00008691SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008692 EVT VT = Op.getValueType();
8693 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008694 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008695 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008696
8697 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008698 if (VT == MVT::i8) {
8699 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008700 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008701 }
Evan Cheng152804e2007-12-14 08:30:15 +00008702
8703 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008704 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008705 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008706
8707 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008708 SDValue Ops[] = {
8709 Op,
8710 DAG.getConstant(NumBits, OpVT),
8711 DAG.getConstant(X86::COND_E, MVT::i8),
8712 Op.getValue(1)
8713 };
8714 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008715
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 if (VT == MVT::i8)
8717 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008718 return Op;
8719}
8720
Dan Gohmand858e902010-04-17 15:26:15 +00008721SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008722 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008723 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008724 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008725
Mon P Wangaf9b9522008-12-18 21:42:19 +00008726 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8727 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8728 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8729 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8730 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8731 //
8732 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8733 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8734 // return AloBlo + AloBhi + AhiBlo;
8735
8736 SDValue A = Op.getOperand(0);
8737 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008738
Dale Johannesene4d209d2009-02-03 20:21:25 +00008739 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008740 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8741 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008742 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008743 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8744 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008745 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008746 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008747 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008748 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008749 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008750 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008751 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008752 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008753 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008754 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008755 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8756 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008757 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008758 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8759 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008760 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8761 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008762 return Res;
8763}
8764
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008765SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8766 EVT VT = Op.getValueType();
8767 DebugLoc dl = Op.getDebugLoc();
8768 SDValue R = Op.getOperand(0);
8769
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008770 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008771
Nate Begeman51409212010-07-28 00:21:48 +00008772 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8773
8774 if (VT == MVT::v4i32) {
8775 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8776 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8777 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8778
8779 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008780
Nate Begeman51409212010-07-28 00:21:48 +00008781 std::vector<Constant*> CV(4, CI);
8782 Constant *C = ConstantVector::get(CV);
8783 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8784 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008785 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008786 false, false, 16);
8787
8788 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008789 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008790 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8791 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8792 }
8793 if (VT == MVT::v16i8) {
8794 // a = a << 5;
8795 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8796 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8797 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8798
8799 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8800 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8801
8802 std::vector<Constant*> CVM1(16, CM1);
8803 std::vector<Constant*> CVM2(16, CM2);
8804 Constant *C = ConstantVector::get(CVM1);
8805 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8806 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008807 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008808 false, false, 16);
8809
8810 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8811 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8812 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8813 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8814 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008815 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008816 // a += a
8817 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008818
Nate Begeman51409212010-07-28 00:21:48 +00008819 C = ConstantVector::get(CVM2);
8820 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8821 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008822 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008823 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008824
Nate Begeman51409212010-07-28 00:21:48 +00008825 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8826 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8827 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8828 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8829 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008830 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008831 // a += a
8832 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008833
Nate Begeman51409212010-07-28 00:21:48 +00008834 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008835 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008836 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8837 return R;
8838 }
8839 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008840}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008841
Dan Gohmand858e902010-04-17 15:26:15 +00008842SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008843 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8844 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008845 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8846 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008847 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008848 SDValue LHS = N->getOperand(0);
8849 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008850 unsigned BaseOp = 0;
8851 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008852 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008853 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008854 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008855 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008856 // A subtract of one will be selected as a INC. Note that INC doesn't
8857 // set CF, so we can't do this for UADDO.
8858 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8859 if (C->getAPIntValue() == 1) {
8860 BaseOp = X86ISD::INC;
8861 Cond = X86::COND_O;
8862 break;
8863 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008864 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008865 Cond = X86::COND_O;
8866 break;
8867 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008868 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008869 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008870 break;
8871 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008872 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8873 // set CF, so we can't do this for USUBO.
8874 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8875 if (C->getAPIntValue() == 1) {
8876 BaseOp = X86ISD::DEC;
8877 Cond = X86::COND_O;
8878 break;
8879 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008880 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008881 Cond = X86::COND_O;
8882 break;
8883 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008884 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008885 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008886 break;
8887 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008888 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008889 Cond = X86::COND_O;
8890 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008891 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8892 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8893 MVT::i32);
8894 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008895
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008896 SDValue SetCC =
8897 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8898 DAG.getConstant(X86::COND_O, MVT::i32),
8899 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008900
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008901 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8902 return Sum;
8903 }
Bill Wendling74c37652008-12-09 22:08:41 +00008904 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008905
Bill Wendling61edeb52008-12-02 01:06:39 +00008906 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008907 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008908 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008909
Bill Wendling61edeb52008-12-02 01:06:39 +00008910 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008911 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8912 DAG.getConstant(Cond, MVT::i32),
8913 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008914
Bill Wendling61edeb52008-12-02 01:06:39 +00008915 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8916 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008917}
8918
Eric Christopher9a9d2752010-07-22 02:48:34 +00008919SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8920 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008921
Eric Christopherb6729dc2010-08-04 23:03:04 +00008922 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008923 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008924 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008925 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008926 SDValue Ops[] = {
8927 DAG.getRegister(X86::ESP, MVT::i32), // Base
8928 DAG.getTargetConstant(1, MVT::i8), // Scale
8929 DAG.getRegister(0, MVT::i32), // Index
8930 DAG.getTargetConstant(0, MVT::i32), // Disp
8931 DAG.getRegister(0, MVT::i32), // Segment.
8932 Zero,
8933 Chain
8934 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008935 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008936 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8937 array_lengthof(Ops));
8938 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008939 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008940
Eric Christopher9a9d2752010-07-22 02:48:34 +00008941 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008942 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008943 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008944
Chris Lattner132929a2010-08-14 17:26:09 +00008945 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8946 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8947 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8948 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008949
Chris Lattner132929a2010-08-14 17:26:09 +00008950 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8951 if (!Op1 && !Op2 && !Op3 && Op4)
8952 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008953
Chris Lattner132929a2010-08-14 17:26:09 +00008954 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8955 if (Op1 && !Op2 && !Op3 && !Op4)
8956 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008957
8958 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008959 // (MFENCE)>;
8960 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008961}
8962
Dan Gohmand858e902010-04-17 15:26:15 +00008963SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008964 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008965 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008966 unsigned Reg = 0;
8967 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008968 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008969 default:
8970 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 case MVT::i8: Reg = X86::AL; size = 1; break;
8972 case MVT::i16: Reg = X86::AX; size = 2; break;
8973 case MVT::i32: Reg = X86::EAX; size = 4; break;
8974 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008975 assert(Subtarget->is64Bit() && "Node not type legal!");
8976 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008977 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008978 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008979 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008980 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008981 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008982 Op.getOperand(1),
8983 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008984 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008985 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008986 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008987 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8988 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8989 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008990 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008991 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008992 return cpOut;
8993}
8994
Duncan Sands1607f052008-12-01 11:39:25 +00008995SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008996 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008997 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008998 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008999 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009000 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009001 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009002 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
9003 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00009004 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00009005 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
9006 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00009007 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00009008 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00009009 rdx.getValue(1)
9010 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00009011 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009012}
9013
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009014SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00009015 SelectionDAG &DAG) const {
9016 EVT SrcVT = Op.getOperand(0).getValueType();
9017 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00009018 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
9019 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00009020 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00009021 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009022 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00009023 // i64 <=> MMX conversions are Legal.
9024 if (SrcVT==MVT::i64 && DstVT.isVector())
9025 return Op;
9026 if (DstVT==MVT::i64 && SrcVT.isVector())
9027 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00009028 // MMX <=> MMX conversions are Legal.
9029 if (SrcVT.isVector() && DstVT.isVector())
9030 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00009031 // All other conversions need to be expanded.
9032 return SDValue();
9033}
Chris Lattner5b856542010-12-20 00:59:46 +00009034
Dan Gohmand858e902010-04-17 15:26:15 +00009035SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009036 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00009037 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00009038 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009039 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00009040 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00009041 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009042 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00009043 Node->getOperand(0),
9044 Node->getOperand(1), negOp,
9045 cast<AtomicSDNode>(Node)->getSrcValue(),
9046 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00009047}
9048
Chris Lattner5b856542010-12-20 00:59:46 +00009049static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
9050 EVT VT = Op.getNode()->getValueType(0);
9051
9052 // Let legalize expand this if it isn't a legal type yet.
9053 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9054 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009055
Chris Lattner5b856542010-12-20 00:59:46 +00009056 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009057
Chris Lattner5b856542010-12-20 00:59:46 +00009058 unsigned Opc;
9059 bool ExtraOp = false;
9060 switch (Op.getOpcode()) {
9061 default: assert(0 && "Invalid code");
9062 case ISD::ADDC: Opc = X86ISD::ADD; break;
9063 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
9064 case ISD::SUBC: Opc = X86ISD::SUB; break;
9065 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
9066 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009067
Chris Lattner5b856542010-12-20 00:59:46 +00009068 if (!ExtraOp)
9069 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9070 Op.getOperand(1));
9071 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
9072 Op.getOperand(1), Op.getOperand(2));
9073}
9074
Evan Cheng0db9fe62006-04-25 20:13:52 +00009075/// LowerOperation - Provide custom lowering hooks for some operations.
9076///
Dan Gohmand858e902010-04-17 15:26:15 +00009077SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00009078 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009079 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00009080 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009081 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
9082 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009083 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00009084 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009085 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
9086 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
9087 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00009088 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00009089 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009090 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
9091 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
9092 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009093 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00009094 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00009095 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009096 case ISD::SHL_PARTS:
9097 case ISD::SRA_PARTS:
9098 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
9099 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00009100 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009101 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00009102 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009103 case ISD::FABS: return LowerFABS(Op, DAG);
9104 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009105 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009106 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00009107 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00009108 case ISD::SELECT: return LowerSELECT(Op, DAG);
9109 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009110 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009111 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00009112 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00009113 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009114 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00009115 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
9116 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009117 case ISD::FRAME_TO_ARGS_OFFSET:
9118 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009119 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009120 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00009121 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00009122 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00009123 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
9124 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00009125 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00009126 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00009127 case ISD::SADDO:
9128 case ISD::UADDO:
9129 case ISD::SSUBO:
9130 case ISD::USUBO:
9131 case ISD::SMULO:
9132 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00009133 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009134 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00009135 case ISD::ADDC:
9136 case ISD::ADDE:
9137 case ISD::SUBC:
9138 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009139 }
Chris Lattner27a6c732007-11-24 07:07:01 +00009140}
9141
Duncan Sands1607f052008-12-01 11:39:25 +00009142void X86TargetLowering::
9143ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009144 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009145 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009146 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00009147 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00009148
9149 SDValue Chain = Node->getOperand(0);
9150 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009151 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009152 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00009153 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009154 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00009155 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00009156 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00009157 SDValue Result =
9158 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
9159 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00009160 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009162 Results.push_back(Result.getValue(2));
9163}
9164
Duncan Sands126d9072008-07-04 11:47:58 +00009165/// ReplaceNodeResults - Replace a node with an illegal result type
9166/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00009167void X86TargetLowering::ReplaceNodeResults(SDNode *N,
9168 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00009169 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009170 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00009171 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00009172 default:
Duncan Sands1607f052008-12-01 11:39:25 +00009173 assert(false && "Do not know how to custom type legalize this operation!");
9174 return;
Chris Lattner5b856542010-12-20 00:59:46 +00009175 case ISD::ADDC:
9176 case ISD::ADDE:
9177 case ISD::SUBC:
9178 case ISD::SUBE:
9179 // We don't want to expand or promote these.
9180 return;
Duncan Sands1607f052008-12-01 11:39:25 +00009181 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00009182 std::pair<SDValue,SDValue> Vals =
9183 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00009184 SDValue FIST = Vals.first, StackSlot = Vals.second;
9185 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00009186 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00009187 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00009188 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
9189 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00009190 }
9191 return;
9192 }
9193 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009194 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00009195 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009196 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009197 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00009198 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009199 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00009200 eax.getValue(2));
9201 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
9202 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00009203 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009204 Results.push_back(edx.getValue(1));
9205 return;
9206 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009207 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00009208 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009209 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00009210 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009211 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9212 DAG.getConstant(0, MVT::i32));
9213 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
9214 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009215 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
9216 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009217 cpInL.getValue(1));
9218 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00009219 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9220 DAG.getConstant(0, MVT::i32));
9221 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
9222 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00009223 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00009224 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009225 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00009226 swapInL.getValue(1));
9227 SDValue Ops[] = { swapInH.getValue(0),
9228 N->getOperand(1),
9229 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009230 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00009231 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
9232 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
9233 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00009234 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009235 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00009236 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00009237 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00009238 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00009239 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00009240 Results.push_back(cpOutH.getValue(1));
9241 return;
9242 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009243 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00009244 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
9245 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009246 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00009247 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
9248 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009249 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00009250 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
9251 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009252 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00009253 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
9254 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009255 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00009256 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
9257 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009258 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00009259 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
9260 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00009261 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00009262 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
9263 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00009264 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00009265}
9266
Evan Cheng72261582005-12-20 06:22:03 +00009267const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
9268 switch (Opcode) {
9269 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00009270 case X86ISD::BSF: return "X86ISD::BSF";
9271 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00009272 case X86ISD::SHLD: return "X86ISD::SHLD";
9273 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00009274 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009275 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00009276 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00009277 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00009278 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00009279 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00009280 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
9281 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
9282 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00009283 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00009284 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00009285 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00009286 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00009287 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00009288 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00009289 case X86ISD::COMI: return "X86ISD::COMI";
9290 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00009291 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00009292 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00009293 case X86ISD::CMOV: return "X86ISD::CMOV";
9294 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00009295 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00009296 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
9297 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00009298 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00009299 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00009300 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009301 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00009302 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00009303 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
9304 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00009305 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00009306 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00009307 case X86ISD::PANDN: return "X86ISD::PANDN";
9308 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
9309 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
9310 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00009311 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00009312 case X86ISD::FMAX: return "X86ISD::FMAX";
9313 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00009314 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
9315 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00009316 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00009317 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00009318 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00009319 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00009320 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00009321 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
9322 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009323 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
9324 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
9325 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
9326 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
9327 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
9328 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00009329 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
9330 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00009331 case X86ISD::VSHL: return "X86ISD::VSHL";
9332 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00009333 case X86ISD::CMPPD: return "X86ISD::CMPPD";
9334 case X86ISD::CMPPS: return "X86ISD::CMPPS";
9335 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
9336 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
9337 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
9338 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
9339 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
9340 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
9341 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
9342 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00009343 case X86ISD::ADD: return "X86ISD::ADD";
9344 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00009345 case X86ISD::ADC: return "X86ISD::ADC";
9346 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00009347 case X86ISD::SMUL: return "X86ISD::SMUL";
9348 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00009349 case X86ISD::INC: return "X86ISD::INC";
9350 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009351 case X86ISD::OR: return "X86ISD::OR";
9352 case X86ISD::XOR: return "X86ISD::XOR";
9353 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009354 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009355 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009356 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009357 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9358 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9359 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9360 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9361 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9362 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9363 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9364 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9365 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009366 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009367 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009368 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009369 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9370 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009371 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9372 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9373 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9374 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9375 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9376 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9377 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9378 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9379 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
David Greenefbf05d32011-02-22 23:31:46 +00009380 case X86ISD::VUNPCKLPS: return "X86ISD::VUNPCKLPS";
9381 case X86ISD::VUNPCKLPD: return "X86ISD::VUNPCKLPD";
9382 case X86ISD::VUNPCKLPSY: return "X86ISD::VUNPCKLPSY";
9383 case X86ISD::VUNPCKLPDY: return "X86ISD::VUNPCKLPDY";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009384 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9385 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9386 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9387 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9388 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9389 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9390 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9391 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9392 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9393 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009394 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009395 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009396 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009397 }
9398}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009399
Chris Lattnerc9addb72007-03-30 23:15:24 +00009400// isLegalAddressingMode - Return true if the addressing mode represented
9401// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009402bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009403 const Type *Ty) const {
9404 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009405 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009406 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009407
Chris Lattnerc9addb72007-03-30 23:15:24 +00009408 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009409 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009410 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009411
Chris Lattnerc9addb72007-03-30 23:15:24 +00009412 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009413 unsigned GVFlags =
9414 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009415
Chris Lattnerdfed4132009-07-10 07:38:24 +00009416 // If a reference to this global requires an extra load, we can't fold it.
9417 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009418 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009419
Chris Lattnerdfed4132009-07-10 07:38:24 +00009420 // If BaseGV requires a register for the PIC base, we cannot also have a
9421 // BaseReg specified.
9422 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009423 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009424
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009425 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009426 if ((M != CodeModel::Small || R != Reloc::Static) &&
9427 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009428 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009430
Chris Lattnerc9addb72007-03-30 23:15:24 +00009431 switch (AM.Scale) {
9432 case 0:
9433 case 1:
9434 case 2:
9435 case 4:
9436 case 8:
9437 // These scales always work.
9438 break;
9439 case 3:
9440 case 5:
9441 case 9:
9442 // These scales are formed with basereg+scalereg. Only accept if there is
9443 // no basereg yet.
9444 if (AM.HasBaseReg)
9445 return false;
9446 break;
9447 default: // Other stuff never works.
9448 return false;
9449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009450
Chris Lattnerc9addb72007-03-30 23:15:24 +00009451 return true;
9452}
9453
9454
Evan Cheng2bd122c2007-10-26 01:56:11 +00009455bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009456 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009457 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009458 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9459 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009460 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009461 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009462 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009463}
9464
Owen Andersone50ed302009-08-10 22:56:29 +00009465bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009466 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009467 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009468 unsigned NumBits1 = VT1.getSizeInBits();
9469 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009470 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009471 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009472 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009473}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009474
Dan Gohman97121ba2009-04-08 00:15:30 +00009475bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009476 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009477 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009478}
9479
Owen Andersone50ed302009-08-10 22:56:29 +00009480bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009481 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009482 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009483}
9484
Owen Andersone50ed302009-08-10 22:56:29 +00009485bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009486 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009487 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009488}
9489
Evan Cheng60c07e12006-07-05 22:17:51 +00009490/// isShuffleMaskLegal - Targets can use this to indicate that they only
9491/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9492/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9493/// are assumed to be legal.
9494bool
Eric Christopherfd179292009-08-27 18:07:15 +00009495X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009496 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009497 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009498 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009499 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009500
Nate Begemana09008b2009-10-19 02:17:23 +00009501 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009502 return (VT.getVectorNumElements() == 2 ||
9503 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9504 isMOVLMask(M, VT) ||
9505 isSHUFPMask(M, VT) ||
9506 isPSHUFDMask(M, VT) ||
9507 isPSHUFHWMask(M, VT) ||
9508 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009509 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009510 isUNPCKLMask(M, VT) ||
9511 isUNPCKHMask(M, VT) ||
9512 isUNPCKL_v_undef_Mask(M, VT) ||
9513 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009514}
9515
Dan Gohman7d8143f2008-04-09 20:09:42 +00009516bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009517X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009518 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009519 unsigned NumElts = VT.getVectorNumElements();
9520 // FIXME: This collection of masks seems suspect.
9521 if (NumElts == 2)
9522 return true;
9523 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9524 return (isMOVLMask(Mask, VT) ||
9525 isCommutedMOVLMask(Mask, VT, true) ||
9526 isSHUFPMask(Mask, VT) ||
9527 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009528 }
9529 return false;
9530}
9531
9532//===----------------------------------------------------------------------===//
9533// X86 Scheduler Hooks
9534//===----------------------------------------------------------------------===//
9535
Mon P Wang63307c32008-05-05 19:05:59 +00009536// private utility function
9537MachineBasicBlock *
9538X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9539 MachineBasicBlock *MBB,
9540 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009541 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009542 unsigned LoadOpc,
9543 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009544 unsigned notOpc,
9545 unsigned EAXreg,
9546 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009547 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009548 // For the atomic bitwise operator, we generate
9549 // thisMBB:
9550 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009551 // ld t1 = [bitinstr.addr]
9552 // op t2 = t1, [bitinstr.val]
9553 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009554 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9555 // bz newMBB
9556 // fallthrough -->nextMBB
9557 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9558 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009559 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009560 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009561
Mon P Wang63307c32008-05-05 19:05:59 +00009562 /// First build the CFG
9563 MachineFunction *F = MBB->getParent();
9564 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009565 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9566 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9567 F->insert(MBBIter, newMBB);
9568 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009569
Dan Gohman14152b42010-07-06 20:24:04 +00009570 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9571 nextMBB->splice(nextMBB->begin(), thisMBB,
9572 llvm::next(MachineBasicBlock::iterator(bInstr)),
9573 thisMBB->end());
9574 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009575
Mon P Wang63307c32008-05-05 19:05:59 +00009576 // Update thisMBB to fall through to newMBB
9577 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009578
Mon P Wang63307c32008-05-05 19:05:59 +00009579 // newMBB jumps to itself and fall through to nextMBB
9580 newMBB->addSuccessor(nextMBB);
9581 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009582
Mon P Wang63307c32008-05-05 19:05:59 +00009583 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009584 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009585 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009586 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009587 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009588 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009589 int numArgs = bInstr->getNumOperands() - 1;
9590 for (int i=0; i < numArgs; ++i)
9591 argOpers[i] = &bInstr->getOperand(i+1);
9592
9593 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009594 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009595 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009596
Dale Johannesen140be2d2008-08-19 18:47:28 +00009597 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009598 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009599 for (int i=0; i <= lastAddrIndx; ++i)
9600 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009601
Dale Johannesen140be2d2008-08-19 18:47:28 +00009602 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009603 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009604 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009606 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009607 tt = t1;
9608
Dale Johannesen140be2d2008-08-19 18:47:28 +00009609 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009610 assert((argOpers[valArgIndx]->isReg() ||
9611 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009612 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009613 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009614 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009615 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009616 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009617 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009618 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009619
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009620 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009621 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009622
Dale Johannesene4d209d2009-02-03 20:21:25 +00009623 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009624 for (int i=0; i <= lastAddrIndx; ++i)
9625 (*MIB).addOperand(*argOpers[i]);
9626 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009627 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009628 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9629 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009630
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009631 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009632 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009633
Mon P Wang63307c32008-05-05 19:05:59 +00009634 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009635 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009636
Dan Gohman14152b42010-07-06 20:24:04 +00009637 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009638 return nextMBB;
9639}
9640
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009641// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009642MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009643X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9644 MachineBasicBlock *MBB,
9645 unsigned regOpcL,
9646 unsigned regOpcH,
9647 unsigned immOpcL,
9648 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009649 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009650 // For the atomic bitwise operator, we generate
9651 // thisMBB (instructions are in pairs, except cmpxchg8b)
9652 // ld t1,t2 = [bitinstr.addr]
9653 // newMBB:
9654 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9655 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009656 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009657 // mov ECX, EBX <- t5, t6
9658 // mov EAX, EDX <- t1, t2
9659 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9660 // mov t3, t4 <- EAX, EDX
9661 // bz newMBB
9662 // result in out1, out2
9663 // fallthrough -->nextMBB
9664
9665 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9666 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009667 const unsigned NotOpc = X86::NOT32r;
9668 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9669 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9670 MachineFunction::iterator MBBIter = MBB;
9671 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009672
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009673 /// First build the CFG
9674 MachineFunction *F = MBB->getParent();
9675 MachineBasicBlock *thisMBB = MBB;
9676 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9677 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9678 F->insert(MBBIter, newMBB);
9679 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009680
Dan Gohman14152b42010-07-06 20:24:04 +00009681 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9682 nextMBB->splice(nextMBB->begin(), thisMBB,
9683 llvm::next(MachineBasicBlock::iterator(bInstr)),
9684 thisMBB->end());
9685 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009686
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009687 // Update thisMBB to fall through to newMBB
9688 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009689
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009690 // newMBB jumps to itself and fall through to nextMBB
9691 newMBB->addSuccessor(nextMBB);
9692 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009693
Dale Johannesene4d209d2009-02-03 20:21:25 +00009694 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009695 // Insert instructions into newMBB based on incoming instruction
9696 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009697 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009698 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009699 MachineOperand& dest1Oper = bInstr->getOperand(0);
9700 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009701 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9702 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009703 argOpers[i] = &bInstr->getOperand(i+2);
9704
Dan Gohman71ea4e52010-05-14 21:01:44 +00009705 // We use some of the operands multiple times, so conservatively just
9706 // clear any kill flags that might be present.
9707 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9708 argOpers[i]->setIsKill(false);
9709 }
9710
Evan Chengad5b52f2010-01-08 19:14:57 +00009711 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009712 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009713
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009714 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009715 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009716 for (int i=0; i <= lastAddrIndx; ++i)
9717 (*MIB).addOperand(*argOpers[i]);
9718 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009719 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009720 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009721 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009722 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009723 MachineOperand newOp3 = *(argOpers[3]);
9724 if (newOp3.isImm())
9725 newOp3.setImm(newOp3.getImm()+4);
9726 else
9727 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009728 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009729 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009730
9731 // t3/4 are defined later, at the bottom of the loop
9732 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9733 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009734 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009735 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009736 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009737 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9738
Evan Cheng306b4ca2010-01-08 23:41:50 +00009739 // The subsequent operations should be using the destination registers of
9740 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009741 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009742 t1 = F->getRegInfo().createVirtualRegister(RC);
9743 t2 = F->getRegInfo().createVirtualRegister(RC);
9744 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9745 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009746 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009747 t1 = dest1Oper.getReg();
9748 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009749 }
9750
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009751 int valArgIndx = lastAddrIndx + 1;
9752 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009753 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009754 "invalid operand");
9755 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9756 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009757 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009758 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009759 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009760 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009761 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009762 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009763 (*MIB).addOperand(*argOpers[valArgIndx]);
9764 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009765 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009766 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009767 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009768 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009769 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009770 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009771 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009772 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009773 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009774 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009775
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009776 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009777 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009778 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009779 MIB.addReg(t2);
9780
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009781 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009782 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009783 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009784 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009785
Dale Johannesene4d209d2009-02-03 20:21:25 +00009786 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009787 for (int i=0; i <= lastAddrIndx; ++i)
9788 (*MIB).addOperand(*argOpers[i]);
9789
9790 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009791 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9792 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009793
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009794 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009795 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009796 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009797 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009798
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009799 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009800 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009801
Dan Gohman14152b42010-07-06 20:24:04 +00009802 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009803 return nextMBB;
9804}
9805
9806// private utility function
9807MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009808X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9809 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009810 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009811 // For the atomic min/max operator, we generate
9812 // thisMBB:
9813 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009814 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009815 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009816 // cmp t1, t2
9817 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009818 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009819 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9820 // bz newMBB
9821 // fallthrough -->nextMBB
9822 //
9823 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9824 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009825 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009826 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009827
Mon P Wang63307c32008-05-05 19:05:59 +00009828 /// First build the CFG
9829 MachineFunction *F = MBB->getParent();
9830 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009831 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9832 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9833 F->insert(MBBIter, newMBB);
9834 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009835
Dan Gohman14152b42010-07-06 20:24:04 +00009836 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9837 nextMBB->splice(nextMBB->begin(), thisMBB,
9838 llvm::next(MachineBasicBlock::iterator(mInstr)),
9839 thisMBB->end());
9840 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009841
Mon P Wang63307c32008-05-05 19:05:59 +00009842 // Update thisMBB to fall through to newMBB
9843 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009844
Mon P Wang63307c32008-05-05 19:05:59 +00009845 // newMBB jumps to newMBB and fall through to nextMBB
9846 newMBB->addSuccessor(nextMBB);
9847 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009848
Dale Johannesene4d209d2009-02-03 20:21:25 +00009849 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009850 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009851 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009852 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009853 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009854 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009855 int numArgs = mInstr->getNumOperands() - 1;
9856 for (int i=0; i < numArgs; ++i)
9857 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009858
Mon P Wang63307c32008-05-05 19:05:59 +00009859 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009860 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009861 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009862
Mon P Wangab3e7472008-05-05 22:56:23 +00009863 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009864 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009865 for (int i=0; i <= lastAddrIndx; ++i)
9866 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009867
Mon P Wang63307c32008-05-05 19:05:59 +00009868 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009869 assert((argOpers[valArgIndx]->isReg() ||
9870 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009871 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009872
9873 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009874 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009875 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009876 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009877 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009878 (*MIB).addOperand(*argOpers[valArgIndx]);
9879
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009880 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009881 MIB.addReg(t1);
9882
Dale Johannesene4d209d2009-02-03 20:21:25 +00009883 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009884 MIB.addReg(t1);
9885 MIB.addReg(t2);
9886
9887 // Generate movc
9888 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009889 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009890 MIB.addReg(t2);
9891 MIB.addReg(t1);
9892
9893 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009894 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009895 for (int i=0; i <= lastAddrIndx; ++i)
9896 (*MIB).addOperand(*argOpers[i]);
9897 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009898 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009899 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9900 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009901
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009902 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009903 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009904
Mon P Wang63307c32008-05-05 19:05:59 +00009905 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009906 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009907
Dan Gohman14152b42010-07-06 20:24:04 +00009908 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009909 return nextMBB;
9910}
9911
Eric Christopherf83a5de2009-08-27 18:08:16 +00009912// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009913// or XMM0_V32I8 in AVX all of this code can be replaced with that
9914// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009915MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009916X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009917 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009918 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9919 "Target must have SSE4.2 or AVX features enabled");
9920
Eric Christopherb120ab42009-08-18 22:50:32 +00009921 DebugLoc dl = MI->getDebugLoc();
9922 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009923 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009924 if (!Subtarget->hasAVX()) {
9925 if (memArg)
9926 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9927 else
9928 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9929 } else {
9930 if (memArg)
9931 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9932 else
9933 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9934 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009935
Eric Christopher41c902f2010-11-30 08:20:21 +00009936 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009937 for (unsigned i = 0; i < numArgs; ++i) {
9938 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009939 if (!(Op.isReg() && Op.isImplicit()))
9940 MIB.addOperand(Op);
9941 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009942 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009943 .addReg(X86::XMM0);
9944
Dan Gohman14152b42010-07-06 20:24:04 +00009945 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009946 return BB;
9947}
9948
9949MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009950X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009951 DebugLoc dl = MI->getDebugLoc();
9952 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009953
Eric Christopher228232b2010-11-30 07:20:12 +00009954 // Address into RAX/EAX, other two args into ECX, EDX.
9955 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9956 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9957 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9958 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009959 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009960
Eric Christopher228232b2010-11-30 07:20:12 +00009961 unsigned ValOps = X86::AddrNumOperands;
9962 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9963 .addReg(MI->getOperand(ValOps).getReg());
9964 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9965 .addReg(MI->getOperand(ValOps+1).getReg());
9966
9967 // The instruction doesn't actually take any operands though.
9968 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009969
Eric Christopher228232b2010-11-30 07:20:12 +00009970 MI->eraseFromParent(); // The pseudo is gone now.
9971 return BB;
9972}
9973
9974MachineBasicBlock *
9975X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009976 DebugLoc dl = MI->getDebugLoc();
9977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009978
Eric Christopher228232b2010-11-30 07:20:12 +00009979 // First arg in ECX, the second in EAX.
9980 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9981 .addReg(MI->getOperand(0).getReg());
9982 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9983 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009984
Eric Christopher228232b2010-11-30 07:20:12 +00009985 // The instruction doesn't actually take any operands though.
9986 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009987
Eric Christopher228232b2010-11-30 07:20:12 +00009988 MI->eraseFromParent(); // The pseudo is gone now.
9989 return BB;
9990}
9991
9992MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009993X86TargetLowering::EmitVAARG64WithCustomInserter(
9994 MachineInstr *MI,
9995 MachineBasicBlock *MBB) const {
9996 // Emit va_arg instruction on X86-64.
9997
9998 // Operands to this pseudo-instruction:
9999 // 0 ) Output : destination address (reg)
10000 // 1-5) Input : va_list address (addr, i64mem)
10001 // 6 ) ArgSize : Size (in bytes) of vararg type
10002 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
10003 // 8 ) Align : Alignment of type
10004 // 9 ) EFLAGS (implicit-def)
10005
10006 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
10007 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
10008
10009 unsigned DestReg = MI->getOperand(0).getReg();
10010 MachineOperand &Base = MI->getOperand(1);
10011 MachineOperand &Scale = MI->getOperand(2);
10012 MachineOperand &Index = MI->getOperand(3);
10013 MachineOperand &Disp = MI->getOperand(4);
10014 MachineOperand &Segment = MI->getOperand(5);
10015 unsigned ArgSize = MI->getOperand(6).getImm();
10016 unsigned ArgMode = MI->getOperand(7).getImm();
10017 unsigned Align = MI->getOperand(8).getImm();
10018
10019 // Memory Reference
10020 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
10021 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
10022 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
10023
10024 // Machine Information
10025 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10026 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
10027 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
10028 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
10029 DebugLoc DL = MI->getDebugLoc();
10030
10031 // struct va_list {
10032 // i32 gp_offset
10033 // i32 fp_offset
10034 // i64 overflow_area (address)
10035 // i64 reg_save_area (address)
10036 // }
10037 // sizeof(va_list) = 24
10038 // alignment(va_list) = 8
10039
10040 unsigned TotalNumIntRegs = 6;
10041 unsigned TotalNumXMMRegs = 8;
10042 bool UseGPOffset = (ArgMode == 1);
10043 bool UseFPOffset = (ArgMode == 2);
10044 unsigned MaxOffset = TotalNumIntRegs * 8 +
10045 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
10046
10047 /* Align ArgSize to a multiple of 8 */
10048 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
10049 bool NeedsAlign = (Align > 8);
10050
10051 MachineBasicBlock *thisMBB = MBB;
10052 MachineBasicBlock *overflowMBB;
10053 MachineBasicBlock *offsetMBB;
10054 MachineBasicBlock *endMBB;
10055
10056 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
10057 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
10058 unsigned OffsetReg = 0;
10059
10060 if (!UseGPOffset && !UseFPOffset) {
10061 // If we only pull from the overflow region, we don't create a branch.
10062 // We don't need to alter control flow.
10063 OffsetDestReg = 0; // unused
10064 OverflowDestReg = DestReg;
10065
10066 offsetMBB = NULL;
10067 overflowMBB = thisMBB;
10068 endMBB = thisMBB;
10069 } else {
10070 // First emit code to check if gp_offset (or fp_offset) is below the bound.
10071 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
10072 // If not, pull from overflow_area. (branch to overflowMBB)
10073 //
10074 // thisMBB
10075 // | .
10076 // | .
10077 // offsetMBB overflowMBB
10078 // | .
10079 // | .
10080 // endMBB
10081
10082 // Registers for the PHI in endMBB
10083 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
10084 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
10085
10086 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10087 MachineFunction *MF = MBB->getParent();
10088 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10089 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10090 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
10091
10092 MachineFunction::iterator MBBIter = MBB;
10093 ++MBBIter;
10094
10095 // Insert the new basic blocks
10096 MF->insert(MBBIter, offsetMBB);
10097 MF->insert(MBBIter, overflowMBB);
10098 MF->insert(MBBIter, endMBB);
10099
10100 // Transfer the remainder of MBB and its successor edges to endMBB.
10101 endMBB->splice(endMBB->begin(), thisMBB,
10102 llvm::next(MachineBasicBlock::iterator(MI)),
10103 thisMBB->end());
10104 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10105
10106 // Make offsetMBB and overflowMBB successors of thisMBB
10107 thisMBB->addSuccessor(offsetMBB);
10108 thisMBB->addSuccessor(overflowMBB);
10109
10110 // endMBB is a successor of both offsetMBB and overflowMBB
10111 offsetMBB->addSuccessor(endMBB);
10112 overflowMBB->addSuccessor(endMBB);
10113
10114 // Load the offset value into a register
10115 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10116 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
10117 .addOperand(Base)
10118 .addOperand(Scale)
10119 .addOperand(Index)
10120 .addDisp(Disp, UseFPOffset ? 4 : 0)
10121 .addOperand(Segment)
10122 .setMemRefs(MMOBegin, MMOEnd);
10123
10124 // Check if there is enough room left to pull this argument.
10125 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
10126 .addReg(OffsetReg)
10127 .addImm(MaxOffset + 8 - ArgSizeA8);
10128
10129 // Branch to "overflowMBB" if offset >= max
10130 // Fall through to "offsetMBB" otherwise
10131 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
10132 .addMBB(overflowMBB);
10133 }
10134
10135 // In offsetMBB, emit code to use the reg_save_area.
10136 if (offsetMBB) {
10137 assert(OffsetReg != 0);
10138
10139 // Read the reg_save_area address.
10140 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
10141 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
10142 .addOperand(Base)
10143 .addOperand(Scale)
10144 .addOperand(Index)
10145 .addDisp(Disp, 16)
10146 .addOperand(Segment)
10147 .setMemRefs(MMOBegin, MMOEnd);
10148
10149 // Zero-extend the offset
10150 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
10151 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
10152 .addImm(0)
10153 .addReg(OffsetReg)
10154 .addImm(X86::sub_32bit);
10155
10156 // Add the offset to the reg_save_area to get the final address.
10157 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
10158 .addReg(OffsetReg64)
10159 .addReg(RegSaveReg);
10160
10161 // Compute the offset for the next argument
10162 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
10163 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
10164 .addReg(OffsetReg)
10165 .addImm(UseFPOffset ? 16 : 8);
10166
10167 // Store it back into the va_list.
10168 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
10169 .addOperand(Base)
10170 .addOperand(Scale)
10171 .addOperand(Index)
10172 .addDisp(Disp, UseFPOffset ? 4 : 0)
10173 .addOperand(Segment)
10174 .addReg(NextOffsetReg)
10175 .setMemRefs(MMOBegin, MMOEnd);
10176
10177 // Jump to endMBB
10178 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
10179 .addMBB(endMBB);
10180 }
10181
10182 //
10183 // Emit code to use overflow area
10184 //
10185
10186 // Load the overflow_area address into a register.
10187 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
10188 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
10189 .addOperand(Base)
10190 .addOperand(Scale)
10191 .addOperand(Index)
10192 .addDisp(Disp, 8)
10193 .addOperand(Segment)
10194 .setMemRefs(MMOBegin, MMOEnd);
10195
10196 // If we need to align it, do so. Otherwise, just copy the address
10197 // to OverflowDestReg.
10198 if (NeedsAlign) {
10199 // Align the overflow address
10200 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
10201 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
10202
10203 // aligned_addr = (addr + (align-1)) & ~(align-1)
10204 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
10205 .addReg(OverflowAddrReg)
10206 .addImm(Align-1);
10207
10208 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
10209 .addReg(TmpReg)
10210 .addImm(~(uint64_t)(Align-1));
10211 } else {
10212 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
10213 .addReg(OverflowAddrReg);
10214 }
10215
10216 // Compute the next overflow address after this argument.
10217 // (the overflow address should be kept 8-byte aligned)
10218 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
10219 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
10220 .addReg(OverflowDestReg)
10221 .addImm(ArgSizeA8);
10222
10223 // Store the new overflow address.
10224 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
10225 .addOperand(Base)
10226 .addOperand(Scale)
10227 .addOperand(Index)
10228 .addDisp(Disp, 8)
10229 .addOperand(Segment)
10230 .addReg(NextAddrReg)
10231 .setMemRefs(MMOBegin, MMOEnd);
10232
10233 // If we branched, emit the PHI to the front of endMBB.
10234 if (offsetMBB) {
10235 BuildMI(*endMBB, endMBB->begin(), DL,
10236 TII->get(X86::PHI), DestReg)
10237 .addReg(OffsetDestReg).addMBB(offsetMBB)
10238 .addReg(OverflowDestReg).addMBB(overflowMBB);
10239 }
10240
10241 // Erase the pseudo instruction
10242 MI->eraseFromParent();
10243
10244 return endMBB;
10245}
10246
10247MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000010248X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
10249 MachineInstr *MI,
10250 MachineBasicBlock *MBB) const {
10251 // Emit code to save XMM registers to the stack. The ABI says that the
10252 // number of registers to save is given in %al, so it's theoretically
10253 // possible to do an indirect jump trick to avoid saving all of them,
10254 // however this code takes a simpler approach and just executes all
10255 // of the stores if %al is non-zero. It's less code, and it's probably
10256 // easier on the hardware branch predictor, and stores aren't all that
10257 // expensive anyway.
10258
10259 // Create the new basic blocks. One block contains all the XMM stores,
10260 // and one block is the final destination regardless of whether any
10261 // stores were performed.
10262 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10263 MachineFunction *F = MBB->getParent();
10264 MachineFunction::iterator MBBIter = MBB;
10265 ++MBBIter;
10266 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
10267 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
10268 F->insert(MBBIter, XMMSaveMBB);
10269 F->insert(MBBIter, EndMBB);
10270
Dan Gohman14152b42010-07-06 20:24:04 +000010271 // Transfer the remainder of MBB and its successor edges to EndMBB.
10272 EndMBB->splice(EndMBB->begin(), MBB,
10273 llvm::next(MachineBasicBlock::iterator(MI)),
10274 MBB->end());
10275 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
10276
Dan Gohmand6708ea2009-08-15 01:38:56 +000010277 // The original block will now fall through to the XMM save block.
10278 MBB->addSuccessor(XMMSaveMBB);
10279 // The XMMSaveMBB will fall through to the end block.
10280 XMMSaveMBB->addSuccessor(EndMBB);
10281
10282 // Now add the instructions.
10283 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10284 DebugLoc DL = MI->getDebugLoc();
10285
10286 unsigned CountReg = MI->getOperand(0).getReg();
10287 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
10288 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
10289
10290 if (!Subtarget->isTargetWin64()) {
10291 // If %al is 0, branch around the XMM save block.
10292 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000010293 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010294 MBB->addSuccessor(EndMBB);
10295 }
10296
10297 // In the XMM save block, save all the XMM argument registers.
10298 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
10299 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000010300 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000010301 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000010302 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000010303 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000010304 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010305 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
10306 .addFrameIndex(RegSaveFrameIndex)
10307 .addImm(/*Scale=*/1)
10308 .addReg(/*IndexReg=*/0)
10309 .addImm(/*Disp=*/Offset)
10310 .addReg(/*Segment=*/0)
10311 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000010312 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010313 }
10314
Dan Gohman14152b42010-07-06 20:24:04 +000010315 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000010316
10317 return EndMBB;
10318}
Mon P Wang63307c32008-05-05 19:05:59 +000010319
Evan Cheng60c07e12006-07-05 22:17:51 +000010320MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000010321X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010322 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000010323 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10324 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000010325
Chris Lattner52600972009-09-02 05:57:00 +000010326 // To "insert" a SELECT_CC instruction, we actually have to insert the
10327 // diamond control-flow pattern. The incoming instruction knows the
10328 // destination vreg to set, the condition code register to branch on, the
10329 // true/false values to select between, and a branch opcode to use.
10330 const BasicBlock *LLVM_BB = BB->getBasicBlock();
10331 MachineFunction::iterator It = BB;
10332 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000010333
Chris Lattner52600972009-09-02 05:57:00 +000010334 // thisMBB:
10335 // ...
10336 // TrueVal = ...
10337 // cmpTY ccX, r1, r2
10338 // bCC copy1MBB
10339 // fallthrough --> copy0MBB
10340 MachineBasicBlock *thisMBB = BB;
10341 MachineFunction *F = BB->getParent();
10342 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
10343 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000010344 F->insert(It, copy0MBB);
10345 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000010346
Bill Wendling730c07e2010-06-25 20:48:10 +000010347 // If the EFLAGS register isn't dead in the terminator, then claim that it's
10348 // live into the sink and copy blocks.
10349 const MachineFunction *MF = BB->getParent();
10350 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
10351 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +000010352
Dan Gohman14152b42010-07-06 20:24:04 +000010353 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10354 const MachineOperand &MO = MI->getOperand(I);
10355 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010356 unsigned Reg = MO.getReg();
10357 if (Reg != X86::EFLAGS) continue;
10358 copy0MBB->addLiveIn(Reg);
10359 sinkMBB->addLiveIn(Reg);
10360 }
10361
Dan Gohman14152b42010-07-06 20:24:04 +000010362 // Transfer the remainder of BB and its successor edges to sinkMBB.
10363 sinkMBB->splice(sinkMBB->begin(), BB,
10364 llvm::next(MachineBasicBlock::iterator(MI)),
10365 BB->end());
10366 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10367
10368 // Add the true and fallthrough blocks as its successors.
10369 BB->addSuccessor(copy0MBB);
10370 BB->addSuccessor(sinkMBB);
10371
10372 // Create the conditional branch instruction.
10373 unsigned Opc =
10374 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10375 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10376
Chris Lattner52600972009-09-02 05:57:00 +000010377 // copy0MBB:
10378 // %FalseValue = ...
10379 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010380 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010381
Chris Lattner52600972009-09-02 05:57:00 +000010382 // sinkMBB:
10383 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10384 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010385 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10386 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010387 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10388 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10389
Dan Gohman14152b42010-07-06 20:24:04 +000010390 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010391 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010392}
10393
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010394MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010395X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010396 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010397 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10398 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010399
10400 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10401 // non-trivial part is impdef of ESP.
10402 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10403 // mingw-w64.
10404
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010405 const char *StackProbeSymbol =
10406 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10407
Dan Gohman14152b42010-07-06 20:24:04 +000010408 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010409 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010410 .addReg(X86::EAX, RegState::Implicit)
10411 .addReg(X86::ESP, RegState::Implicit)
10412 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010413 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10414 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010415
Dan Gohman14152b42010-07-06 20:24:04 +000010416 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010417 return BB;
10418}
Chris Lattner52600972009-09-02 05:57:00 +000010419
10420MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010421X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10422 MachineBasicBlock *BB) const {
10423 // This is pretty easy. We're taking the value that we received from
10424 // our load from the relocation, sticking it in either RDI (x86-64)
10425 // or EAX and doing an indirect call. The return value will then
10426 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010427 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010428 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010429 DebugLoc DL = MI->getDebugLoc();
10430 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010431
10432 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010433 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010434
Eric Christopher30ef0e52010-06-03 04:07:48 +000010435 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010436 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10437 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010438 .addReg(X86::RIP)
10439 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010440 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010441 MI->getOperand(3).getTargetFlags())
10442 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010443 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010444 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010445 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010446 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10447 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010448 .addReg(0)
10449 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010450 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010451 MI->getOperand(3).getTargetFlags())
10452 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010453 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010454 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010455 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010456 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10457 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010458 .addReg(TII->getGlobalBaseReg(F))
10459 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010460 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010461 MI->getOperand(3).getTargetFlags())
10462 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010463 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010464 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010465 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010466
Dan Gohman14152b42010-07-06 20:24:04 +000010467 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010468 return BB;
10469}
10470
10471MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010472X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010473 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010474 switch (MI->getOpcode()) {
10475 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010476 case X86::TAILJMPd64:
10477 case X86::TAILJMPr64:
10478 case X86::TAILJMPm64:
10479 assert(!"TAILJMP64 would not be touched here.");
10480 case X86::TCRETURNdi64:
10481 case X86::TCRETURNri64:
10482 case X86::TCRETURNmi64:
10483 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10484 // On AMD64, additional defs should be added before register allocation.
10485 if (!Subtarget->isTargetWin64()) {
10486 MI->addRegisterDefined(X86::RSI);
10487 MI->addRegisterDefined(X86::RDI);
10488 MI->addRegisterDefined(X86::XMM6);
10489 MI->addRegisterDefined(X86::XMM7);
10490 MI->addRegisterDefined(X86::XMM8);
10491 MI->addRegisterDefined(X86::XMM9);
10492 MI->addRegisterDefined(X86::XMM10);
10493 MI->addRegisterDefined(X86::XMM11);
10494 MI->addRegisterDefined(X86::XMM12);
10495 MI->addRegisterDefined(X86::XMM13);
10496 MI->addRegisterDefined(X86::XMM14);
10497 MI->addRegisterDefined(X86::XMM15);
10498 }
10499 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010500 case X86::WIN_ALLOCA:
10501 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010502 case X86::TLSCall_32:
10503 case X86::TLSCall_64:
10504 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010505 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010506 case X86::CMOV_FR32:
10507 case X86::CMOV_FR64:
10508 case X86::CMOV_V4F32:
10509 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010510 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010511 case X86::CMOV_GR16:
10512 case X86::CMOV_GR32:
10513 case X86::CMOV_RFP32:
10514 case X86::CMOV_RFP64:
10515 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010516 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010517
Dale Johannesen849f2142007-07-03 00:53:03 +000010518 case X86::FP32_TO_INT16_IN_MEM:
10519 case X86::FP32_TO_INT32_IN_MEM:
10520 case X86::FP32_TO_INT64_IN_MEM:
10521 case X86::FP64_TO_INT16_IN_MEM:
10522 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010523 case X86::FP64_TO_INT64_IN_MEM:
10524 case X86::FP80_TO_INT16_IN_MEM:
10525 case X86::FP80_TO_INT32_IN_MEM:
10526 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010527 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10528 DebugLoc DL = MI->getDebugLoc();
10529
Evan Cheng60c07e12006-07-05 22:17:51 +000010530 // Change the floating point control register to use "round towards zero"
10531 // mode when truncating to an integer value.
10532 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010533 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010534 addFrameReference(BuildMI(*BB, MI, DL,
10535 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010536
10537 // Load the old value of the high byte of the control word...
10538 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010539 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010540 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010541 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010542
10543 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010544 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010545 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010546
10547 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010548 addFrameReference(BuildMI(*BB, MI, DL,
10549 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010550
10551 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010552 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010553 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010554
10555 // Get the X86 opcode to use.
10556 unsigned Opc;
10557 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010558 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010559 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10560 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10561 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10562 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10563 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10564 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010565 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10566 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10567 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010568 }
10569
10570 X86AddressMode AM;
10571 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010572 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010573 AM.BaseType = X86AddressMode::RegBase;
10574 AM.Base.Reg = Op.getReg();
10575 } else {
10576 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010577 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010578 }
10579 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010580 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010581 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010582 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010583 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010584 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010585 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010586 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010587 AM.GV = Op.getGlobal();
10588 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010589 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010590 }
Dan Gohman14152b42010-07-06 20:24:04 +000010591 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010592 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010593
10594 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010595 addFrameReference(BuildMI(*BB, MI, DL,
10596 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010597
Dan Gohman14152b42010-07-06 20:24:04 +000010598 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010599 return BB;
10600 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010601 // String/text processing lowering.
10602 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010603 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010604 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10605 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010606 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010607 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10608 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010609 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010610 return EmitPCMP(MI, BB, 5, false /* in mem */);
10611 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010612 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010613 return EmitPCMP(MI, BB, 5, true /* in mem */);
10614
Eric Christopher228232b2010-11-30 07:20:12 +000010615 // Thread synchronization.
10616 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010617 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010618 case X86::MWAIT:
10619 return EmitMwait(MI, BB);
10620
Eric Christopherb120ab42009-08-18 22:50:32 +000010621 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010622 case X86::ATOMAND32:
10623 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010624 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010625 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010626 X86::NOT32r, X86::EAX,
10627 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010628 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10630 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010631 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010632 X86::NOT32r, X86::EAX,
10633 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010634 case X86::ATOMXOR32:
10635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010636 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010637 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010638 X86::NOT32r, X86::EAX,
10639 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010640 case X86::ATOMNAND32:
10641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010642 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010643 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010644 X86::NOT32r, X86::EAX,
10645 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010646 case X86::ATOMMIN32:
10647 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10648 case X86::ATOMMAX32:
10649 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10650 case X86::ATOMUMIN32:
10651 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10652 case X86::ATOMUMAX32:
10653 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010654
10655 case X86::ATOMAND16:
10656 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10657 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010658 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010659 X86::NOT16r, X86::AX,
10660 X86::GR16RegisterClass);
10661 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010662 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010663 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010664 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010665 X86::NOT16r, X86::AX,
10666 X86::GR16RegisterClass);
10667 case X86::ATOMXOR16:
10668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10669 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010670 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010671 X86::NOT16r, X86::AX,
10672 X86::GR16RegisterClass);
10673 case X86::ATOMNAND16:
10674 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10675 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010676 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010677 X86::NOT16r, X86::AX,
10678 X86::GR16RegisterClass, true);
10679 case X86::ATOMMIN16:
10680 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10681 case X86::ATOMMAX16:
10682 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10683 case X86::ATOMUMIN16:
10684 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10685 case X86::ATOMUMAX16:
10686 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10687
10688 case X86::ATOMAND8:
10689 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10690 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010691 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010692 X86::NOT8r, X86::AL,
10693 X86::GR8RegisterClass);
10694 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010695 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010696 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010697 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010698 X86::NOT8r, X86::AL,
10699 X86::GR8RegisterClass);
10700 case X86::ATOMXOR8:
10701 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10702 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010703 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010704 X86::NOT8r, X86::AL,
10705 X86::GR8RegisterClass);
10706 case X86::ATOMNAND8:
10707 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10708 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010709 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010710 X86::NOT8r, X86::AL,
10711 X86::GR8RegisterClass, true);
10712 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010713 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010714 case X86::ATOMAND64:
10715 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010716 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010717 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010718 X86::NOT64r, X86::RAX,
10719 X86::GR64RegisterClass);
10720 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010721 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10722 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010723 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010724 X86::NOT64r, X86::RAX,
10725 X86::GR64RegisterClass);
10726 case X86::ATOMXOR64:
10727 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010728 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010729 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010730 X86::NOT64r, X86::RAX,
10731 X86::GR64RegisterClass);
10732 case X86::ATOMNAND64:
10733 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10734 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010735 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010736 X86::NOT64r, X86::RAX,
10737 X86::GR64RegisterClass, true);
10738 case X86::ATOMMIN64:
10739 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10740 case X86::ATOMMAX64:
10741 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10742 case X86::ATOMUMIN64:
10743 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10744 case X86::ATOMUMAX64:
10745 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010746
10747 // This group does 64-bit operations on a 32-bit host.
10748 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010749 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010750 X86::AND32rr, X86::AND32rr,
10751 X86::AND32ri, X86::AND32ri,
10752 false);
10753 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010754 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010755 X86::OR32rr, X86::OR32rr,
10756 X86::OR32ri, X86::OR32ri,
10757 false);
10758 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010759 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010760 X86::XOR32rr, X86::XOR32rr,
10761 X86::XOR32ri, X86::XOR32ri,
10762 false);
10763 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010764 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010765 X86::AND32rr, X86::AND32rr,
10766 X86::AND32ri, X86::AND32ri,
10767 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010768 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010769 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010770 X86::ADD32rr, X86::ADC32rr,
10771 X86::ADD32ri, X86::ADC32ri,
10772 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010773 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010774 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010775 X86::SUB32rr, X86::SBB32rr,
10776 X86::SUB32ri, X86::SBB32ri,
10777 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010778 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010779 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010780 X86::MOV32rr, X86::MOV32rr,
10781 X86::MOV32ri, X86::MOV32ri,
10782 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010783 case X86::VASTART_SAVE_XMM_REGS:
10784 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010785
10786 case X86::VAARG_64:
10787 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010788 }
10789}
10790
10791//===----------------------------------------------------------------------===//
10792// X86 Optimization Hooks
10793//===----------------------------------------------------------------------===//
10794
Dan Gohman475871a2008-07-27 21:46:04 +000010795void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010796 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010797 APInt &KnownZero,
10798 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010799 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010800 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010801 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010802 assert((Opc >= ISD::BUILTIN_OP_END ||
10803 Opc == ISD::INTRINSIC_WO_CHAIN ||
10804 Opc == ISD::INTRINSIC_W_CHAIN ||
10805 Opc == ISD::INTRINSIC_VOID) &&
10806 "Should use MaskedValueIsZero if you don't know whether Op"
10807 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010808
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010809 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010810 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010811 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010812 case X86ISD::ADD:
10813 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010814 case X86ISD::ADC:
10815 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010816 case X86ISD::SMUL:
10817 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010818 case X86ISD::INC:
10819 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010820 case X86ISD::OR:
10821 case X86ISD::XOR:
10822 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010823 // These nodes' second result is a boolean.
10824 if (Op.getResNo() == 0)
10825 break;
10826 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010827 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010828 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10829 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010830 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010831 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010832}
Chris Lattner259e97c2006-01-31 19:43:35 +000010833
Owen Andersonbc146b02010-09-21 20:42:50 +000010834unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10835 unsigned Depth) const {
10836 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10837 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10838 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010839
Owen Andersonbc146b02010-09-21 20:42:50 +000010840 // Fallback case.
10841 return 1;
10842}
10843
Evan Cheng206ee9d2006-07-07 08:33:52 +000010844/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010845/// node is a GlobalAddress + offset.
10846bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010847 const GlobalValue* &GA,
10848 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010849 if (N->getOpcode() == X86ISD::Wrapper) {
10850 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010851 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010852 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010853 return true;
10854 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010855 }
Evan Chengad4196b2008-05-12 19:56:52 +000010856 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010857}
10858
Evan Cheng206ee9d2006-07-07 08:33:52 +000010859/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10860/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10861/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010862/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010863static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010864 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010865 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010866 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010867
Eli Friedman7a5e5552009-06-07 06:52:44 +000010868 if (VT.getSizeInBits() != 128)
10869 return SDValue();
10870
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010871 // Don't create instructions with illegal types after legalize types has run.
10872 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10873 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10874 return SDValue();
10875
Nate Begemanfdea31a2010-03-24 20:49:50 +000010876 SmallVector<SDValue, 16> Elts;
10877 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010878 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010879
Nate Begemanfdea31a2010-03-24 20:49:50 +000010880 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010881}
Evan Chengd880b972008-05-09 21:53:03 +000010882
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010883/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10884/// generation and convert it from being a bunch of shuffles and extracts
10885/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010886static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10887 const TargetLowering &TLI) {
10888 SDValue InputVector = N->getOperand(0);
10889
10890 // Only operate on vectors of 4 elements, where the alternative shuffling
10891 // gets to be more expensive.
10892 if (InputVector.getValueType() != MVT::v4i32)
10893 return SDValue();
10894
10895 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10896 // single use which is a sign-extend or zero-extend, and all elements are
10897 // used.
10898 SmallVector<SDNode *, 4> Uses;
10899 unsigned ExtractedElements = 0;
10900 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10901 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10902 if (UI.getUse().getResNo() != InputVector.getResNo())
10903 return SDValue();
10904
10905 SDNode *Extract = *UI;
10906 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10907 return SDValue();
10908
10909 if (Extract->getValueType(0) != MVT::i32)
10910 return SDValue();
10911 if (!Extract->hasOneUse())
10912 return SDValue();
10913 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10914 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10915 return SDValue();
10916 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10917 return SDValue();
10918
10919 // Record which element was extracted.
10920 ExtractedElements |=
10921 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10922
10923 Uses.push_back(Extract);
10924 }
10925
10926 // If not all the elements were used, this may not be worthwhile.
10927 if (ExtractedElements != 15)
10928 return SDValue();
10929
10930 // Ok, we've now decided to do the transformation.
10931 DebugLoc dl = InputVector.getDebugLoc();
10932
10933 // Store the value to a temporary stack slot.
10934 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010935 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10936 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010937
10938 // Replace each use (extract) with a load of the appropriate element.
10939 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10940 UE = Uses.end(); UI != UE; ++UI) {
10941 SDNode *Extract = *UI;
10942
10943 // Compute the element's address.
10944 SDValue Idx = Extract->getOperand(1);
10945 unsigned EltSize =
10946 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10947 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10948 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10949
Eric Christopher90eb4022010-07-22 00:26:08 +000010950 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010951 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010952
10953 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010954 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010955 ScalarAddr, MachinePointerInfo(),
10956 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010957
10958 // Replace the exact with the load.
10959 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10960 }
10961
10962 // The replacement was made in place; don't return anything.
10963 return SDValue();
10964}
10965
Chris Lattner83e6c992006-10-04 06:57:07 +000010966/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010967static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010968 const X86Subtarget *Subtarget) {
10969 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010970 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010971 // Get the LHS/RHS of the select.
10972 SDValue LHS = N->getOperand(1);
10973 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010974
Dan Gohman670e5392009-09-21 18:03:22 +000010975 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010976 // instructions match the semantics of the common C idiom x<y?x:y but not
10977 // x<=y?x:y, because of how they handle negative zero (which can be
10978 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010979 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010980 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010981 Cond.getOpcode() == ISD::SETCC) {
10982 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010983
Chris Lattner47b4ce82009-03-11 05:48:52 +000010984 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010985 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010986 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10987 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010988 switch (CC) {
10989 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010990 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010991 // Converting this to a min would handle NaNs incorrectly, and swapping
10992 // the operands would cause it to handle comparisons between positive
10993 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010994 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010995 if (!UnsafeFPMath &&
10996 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10997 break;
10998 std::swap(LHS, RHS);
10999 }
Dan Gohman670e5392009-09-21 18:03:22 +000011000 Opcode = X86ISD::FMIN;
11001 break;
11002 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011003 // Converting this to a min would handle comparisons between positive
11004 // and negative zero incorrectly.
11005 if (!UnsafeFPMath &&
11006 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
11007 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011008 Opcode = X86ISD::FMIN;
11009 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011010 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011011 // Converting this to a min would handle both negative zeros and NaNs
11012 // incorrectly, but we can swap the operands to fix both.
11013 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011014 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011015 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011016 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011017 Opcode = X86ISD::FMIN;
11018 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011019
Dan Gohman670e5392009-09-21 18:03:22 +000011020 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011021 // Converting this to a max would handle comparisons between positive
11022 // and negative zero incorrectly.
11023 if (!UnsafeFPMath &&
11024 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
11025 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011026 Opcode = X86ISD::FMAX;
11027 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000011028 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011029 // Converting this to a max would handle NaNs incorrectly, and swapping
11030 // the operands would cause it to handle comparisons between positive
11031 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011032 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000011033 if (!UnsafeFPMath &&
11034 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
11035 break;
11036 std::swap(LHS, RHS);
11037 }
Dan Gohman670e5392009-09-21 18:03:22 +000011038 Opcode = X86ISD::FMAX;
11039 break;
11040 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011041 // Converting this to a max would handle both negative zeros and NaNs
11042 // incorrectly, but we can swap the operands to fix both.
11043 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011044 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011045 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011046 case ISD::SETGE:
11047 Opcode = X86ISD::FMAX;
11048 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000011049 }
Dan Gohman670e5392009-09-21 18:03:22 +000011050 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000011051 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
11052 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000011053 switch (CC) {
11054 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000011055 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011056 // Converting this to a min would handle comparisons between positive
11057 // and negative zero incorrectly, and swapping the operands would
11058 // cause it to handle NaNs incorrectly.
11059 if (!UnsafeFPMath &&
11060 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000011061 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011062 break;
11063 std::swap(LHS, RHS);
11064 }
Dan Gohman670e5392009-09-21 18:03:22 +000011065 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000011066 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011067 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000011068 // Converting this to a min would handle NaNs incorrectly.
11069 if (!UnsafeFPMath &&
11070 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
11071 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011072 Opcode = X86ISD::FMIN;
11073 break;
11074 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000011075 // Converting this to a min would handle both negative zeros and NaNs
11076 // incorrectly, but we can swap the operands to fix both.
11077 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011078 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011079 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011080 case ISD::SETGE:
11081 Opcode = X86ISD::FMIN;
11082 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011083
Dan Gohman670e5392009-09-21 18:03:22 +000011084 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000011085 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000011086 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011087 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011088 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000011089 break;
Dan Gohman670e5392009-09-21 18:03:22 +000011090 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000011091 // Converting this to a max would handle comparisons between positive
11092 // and negative zero incorrectly, and swapping the operands would
11093 // cause it to handle NaNs incorrectly.
11094 if (!UnsafeFPMath &&
11095 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000011096 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000011097 break;
11098 std::swap(LHS, RHS);
11099 }
Dan Gohman670e5392009-09-21 18:03:22 +000011100 Opcode = X86ISD::FMAX;
11101 break;
11102 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000011103 // Converting this to a max would handle both negative zeros and NaNs
11104 // incorrectly, but we can swap the operands to fix both.
11105 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000011106 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011107 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000011108 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000011109 Opcode = X86ISD::FMAX;
11110 break;
11111 }
Chris Lattner83e6c992006-10-04 06:57:07 +000011112 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000011113
Chris Lattner47b4ce82009-03-11 05:48:52 +000011114 if (Opcode)
11115 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000011116 }
Eric Christopherfd179292009-08-27 18:07:15 +000011117
Chris Lattnerd1980a52009-03-12 06:52:53 +000011118 // If this is a select between two integer constants, try to do some
11119 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000011120 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
11121 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000011122 // Don't do this for crazy integer types.
11123 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
11124 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000011125 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011126 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000011127
Chris Lattnercee56e72009-03-13 05:53:31 +000011128 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000011129 // Efficiently invertible.
11130 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
11131 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
11132 isa<ConstantSDNode>(Cond.getOperand(1))))) {
11133 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000011134 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011135 }
Eric Christopherfd179292009-08-27 18:07:15 +000011136
Chris Lattnerd1980a52009-03-12 06:52:53 +000011137 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011138 if (FalseC->getAPIntValue() == 0 &&
11139 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000011140 if (NeedsCondInvert) // Invert the condition if needed.
11141 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11142 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011143
Chris Lattnerd1980a52009-03-12 06:52:53 +000011144 // Zero extend the condition if needed.
11145 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011146
Chris Lattnercee56e72009-03-13 05:53:31 +000011147 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000011148 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011149 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011150 }
Eric Christopherfd179292009-08-27 18:07:15 +000011151
Chris Lattner97a29a52009-03-13 05:22:11 +000011152 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000011153 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000011154 if (NeedsCondInvert) // Invert the condition if needed.
11155 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11156 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011157
Chris Lattner97a29a52009-03-13 05:22:11 +000011158 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011159 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11160 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011161 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000011162 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000011163 }
Eric Christopherfd179292009-08-27 18:07:15 +000011164
Chris Lattnercee56e72009-03-13 05:53:31 +000011165 // Optimize cases that will turn into an LEA instruction. This requires
11166 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011167 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011168 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011169 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011170
Chris Lattnercee56e72009-03-13 05:53:31 +000011171 bool isFastMultiplier = false;
11172 if (Diff < 10) {
11173 switch ((unsigned char)Diff) {
11174 default: break;
11175 case 1: // result = add base, cond
11176 case 2: // result = lea base( , cond*2)
11177 case 3: // result = lea base(cond, cond*2)
11178 case 4: // result = lea base( , cond*4)
11179 case 5: // result = lea base(cond, cond*4)
11180 case 8: // result = lea base( , cond*8)
11181 case 9: // result = lea base(cond, cond*8)
11182 isFastMultiplier = true;
11183 break;
11184 }
11185 }
Eric Christopherfd179292009-08-27 18:07:15 +000011186
Chris Lattnercee56e72009-03-13 05:53:31 +000011187 if (isFastMultiplier) {
11188 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11189 if (NeedsCondInvert) // Invert the condition if needed.
11190 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
11191 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011192
Chris Lattnercee56e72009-03-13 05:53:31 +000011193 // Zero extend the condition if needed.
11194 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11195 Cond);
11196 // Scale the condition by the difference.
11197 if (Diff != 1)
11198 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11199 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000011200
Chris Lattnercee56e72009-03-13 05:53:31 +000011201 // Add the base if non-zero.
11202 if (FalseC->getAPIntValue() != 0)
11203 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11204 SDValue(FalseC, 0));
11205 return Cond;
11206 }
Eric Christopherfd179292009-08-27 18:07:15 +000011207 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011208 }
11209 }
Eric Christopherfd179292009-08-27 18:07:15 +000011210
Dan Gohman475871a2008-07-27 21:46:04 +000011211 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000011212}
11213
Chris Lattnerd1980a52009-03-12 06:52:53 +000011214/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
11215static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
11216 TargetLowering::DAGCombinerInfo &DCI) {
11217 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000011218
Chris Lattnerd1980a52009-03-12 06:52:53 +000011219 // If the flag operand isn't dead, don't touch this CMOV.
11220 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
11221 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000011222
Chris Lattnerd1980a52009-03-12 06:52:53 +000011223 // If this is a select between two integer constants, try to do some
11224 // optimizations. Note that the operands are ordered the opposite of SELECT
11225 // operands.
11226 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
11227 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11228 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
11229 // larger than FalseC (the false value).
11230 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000011231
Chris Lattnerd1980a52009-03-12 06:52:53 +000011232 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
11233 CC = X86::GetOppositeBranchCondition(CC);
11234 std::swap(TrueC, FalseC);
11235 }
Eric Christopherfd179292009-08-27 18:07:15 +000011236
Chris Lattnerd1980a52009-03-12 06:52:53 +000011237 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000011238 // This is efficient for any integer data type (including i8/i16) and
11239 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000011240 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
11241 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011242 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11243 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011244
Chris Lattnerd1980a52009-03-12 06:52:53 +000011245 // Zero extend the condition if needed.
11246 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011247
Chris Lattnerd1980a52009-03-12 06:52:53 +000011248 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
11249 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000011250 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000011251 if (N->getNumValues() == 2) // Dead flag value?
11252 return DCI.CombineTo(N, Cond, SDValue());
11253 return Cond;
11254 }
Eric Christopherfd179292009-08-27 18:07:15 +000011255
Chris Lattnercee56e72009-03-13 05:53:31 +000011256 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
11257 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000011258 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
11259 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011260 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11261 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000011262
Chris Lattner97a29a52009-03-13 05:22:11 +000011263 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000011264 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
11265 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000011266 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11267 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000011268
Chris Lattner97a29a52009-03-13 05:22:11 +000011269 if (N->getNumValues() == 2) // Dead flag value?
11270 return DCI.CombineTo(N, Cond, SDValue());
11271 return Cond;
11272 }
Eric Christopherfd179292009-08-27 18:07:15 +000011273
Chris Lattnercee56e72009-03-13 05:53:31 +000011274 // Optimize cases that will turn into an LEA instruction. This requires
11275 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000011276 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000011277 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000011278 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000011279
Chris Lattnercee56e72009-03-13 05:53:31 +000011280 bool isFastMultiplier = false;
11281 if (Diff < 10) {
11282 switch ((unsigned char)Diff) {
11283 default: break;
11284 case 1: // result = add base, cond
11285 case 2: // result = lea base( , cond*2)
11286 case 3: // result = lea base(cond, cond*2)
11287 case 4: // result = lea base( , cond*4)
11288 case 5: // result = lea base(cond, cond*4)
11289 case 8: // result = lea base( , cond*8)
11290 case 9: // result = lea base(cond, cond*8)
11291 isFastMultiplier = true;
11292 break;
11293 }
11294 }
Eric Christopherfd179292009-08-27 18:07:15 +000011295
Chris Lattnercee56e72009-03-13 05:53:31 +000011296 if (isFastMultiplier) {
11297 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
11298 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000011299 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11300 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000011301 // Zero extend the condition if needed.
11302 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
11303 Cond);
11304 // Scale the condition by the difference.
11305 if (Diff != 1)
11306 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
11307 DAG.getConstant(Diff, Cond.getValueType()));
11308
11309 // Add the base if non-zero.
11310 if (FalseC->getAPIntValue() != 0)
11311 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
11312 SDValue(FalseC, 0));
11313 if (N->getNumValues() == 2) // Dead flag value?
11314 return DCI.CombineTo(N, Cond, SDValue());
11315 return Cond;
11316 }
Eric Christopherfd179292009-08-27 18:07:15 +000011317 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000011318 }
11319 }
11320 return SDValue();
11321}
11322
11323
Evan Cheng0b0cd912009-03-28 05:57:29 +000011324/// PerformMulCombine - Optimize a single multiply with constant into two
11325/// in order to implement it with two cheaper instructions, e.g.
11326/// LEA + SHL, LEA + LEA.
11327static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
11328 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000011329 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11330 return SDValue();
11331
Owen Andersone50ed302009-08-10 22:56:29 +000011332 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011333 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000011334 return SDValue();
11335
11336 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
11337 if (!C)
11338 return SDValue();
11339 uint64_t MulAmt = C->getZExtValue();
11340 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
11341 return SDValue();
11342
11343 uint64_t MulAmt1 = 0;
11344 uint64_t MulAmt2 = 0;
11345 if ((MulAmt % 9) == 0) {
11346 MulAmt1 = 9;
11347 MulAmt2 = MulAmt / 9;
11348 } else if ((MulAmt % 5) == 0) {
11349 MulAmt1 = 5;
11350 MulAmt2 = MulAmt / 5;
11351 } else if ((MulAmt % 3) == 0) {
11352 MulAmt1 = 3;
11353 MulAmt2 = MulAmt / 3;
11354 }
11355 if (MulAmt2 &&
11356 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11357 DebugLoc DL = N->getDebugLoc();
11358
11359 if (isPowerOf2_64(MulAmt2) &&
11360 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11361 // If second multiplifer is pow2, issue it first. We want the multiply by
11362 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11363 // is an add.
11364 std::swap(MulAmt1, MulAmt2);
11365
11366 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011367 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011368 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011369 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011370 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011371 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011372 DAG.getConstant(MulAmt1, VT));
11373
Eric Christopherfd179292009-08-27 18:07:15 +000011374 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011375 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011376 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011377 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011378 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011379 DAG.getConstant(MulAmt2, VT));
11380
11381 // Do not add new nodes to DAG combiner worklist.
11382 DCI.CombineTo(N, NewMul, false);
11383 }
11384 return SDValue();
11385}
11386
Evan Chengad9c0a32009-12-15 00:53:42 +000011387static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11388 SDValue N0 = N->getOperand(0);
11389 SDValue N1 = N->getOperand(1);
11390 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11391 EVT VT = N0.getValueType();
11392
11393 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11394 // since the result of setcc_c is all zero's or all ones.
11395 if (N1C && N0.getOpcode() == ISD::AND &&
11396 N0.getOperand(1).getOpcode() == ISD::Constant) {
11397 SDValue N00 = N0.getOperand(0);
11398 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11399 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11400 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11401 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11402 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11403 APInt ShAmt = N1C->getAPIntValue();
11404 Mask = Mask.shl(ShAmt);
11405 if (Mask != 0)
11406 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11407 N00, DAG.getConstant(Mask, VT));
11408 }
11409 }
11410
11411 return SDValue();
11412}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011413
Nate Begeman740ab032009-01-26 00:52:55 +000011414/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11415/// when possible.
11416static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11417 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011418 EVT VT = N->getValueType(0);
11419 if (!VT.isVector() && VT.isInteger() &&
11420 N->getOpcode() == ISD::SHL)
11421 return PerformSHLCombine(N, DAG);
11422
Nate Begeman740ab032009-01-26 00:52:55 +000011423 // On X86 with SSE2 support, we can transform this to a vector shift if
11424 // all elements are shifted by the same amount. We can't do this in legalize
11425 // because the a constant vector is typically transformed to a constant pool
11426 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011427 if (!Subtarget->hasSSE2())
11428 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011429
Owen Anderson825b72b2009-08-11 20:47:22 +000011430 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011431 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011432
Mon P Wang3becd092009-01-28 08:12:05 +000011433 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011434 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011435 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011436 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011437 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11438 unsigned NumElts = VT.getVectorNumElements();
11439 unsigned i = 0;
11440 for (; i != NumElts; ++i) {
11441 SDValue Arg = ShAmtOp.getOperand(i);
11442 if (Arg.getOpcode() == ISD::UNDEF) continue;
11443 BaseShAmt = Arg;
11444 break;
11445 }
11446 for (; i != NumElts; ++i) {
11447 SDValue Arg = ShAmtOp.getOperand(i);
11448 if (Arg.getOpcode() == ISD::UNDEF) continue;
11449 if (Arg != BaseShAmt) {
11450 return SDValue();
11451 }
11452 }
11453 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011454 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011455 SDValue InVec = ShAmtOp.getOperand(0);
11456 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11457 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11458 unsigned i = 0;
11459 for (; i != NumElts; ++i) {
11460 SDValue Arg = InVec.getOperand(i);
11461 if (Arg.getOpcode() == ISD::UNDEF) continue;
11462 BaseShAmt = Arg;
11463 break;
11464 }
11465 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011467 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011468 if (C->getZExtValue() == SplatIdx)
11469 BaseShAmt = InVec.getOperand(1);
11470 }
11471 }
11472 if (BaseShAmt.getNode() == 0)
11473 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11474 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011475 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011476 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011477
Mon P Wangefa42202009-09-03 19:56:25 +000011478 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011479 if (EltVT.bitsGT(MVT::i32))
11480 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11481 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011482 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011483
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011484 // The shift amount is identical so we can do a vector shift.
11485 SDValue ValOp = N->getOperand(0);
11486 switch (N->getOpcode()) {
11487 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011488 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011489 break;
11490 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011491 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011492 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011493 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011494 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011495 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011496 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011497 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011498 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011499 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011500 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011501 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011502 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011503 break;
11504 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011505 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011506 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011507 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011508 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011509 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011510 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011511 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011512 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011513 break;
11514 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011515 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011516 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011517 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011518 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011519 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011520 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011521 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011522 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011523 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011524 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011525 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011526 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011527 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011528 }
11529 return SDValue();
11530}
11531
Nate Begemanb65c1752010-12-17 22:55:37 +000011532
11533static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11534 TargetLowering::DAGCombinerInfo &DCI,
11535 const X86Subtarget *Subtarget) {
11536 if (DCI.isBeforeLegalizeOps())
11537 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011538
Nate Begemanb65c1752010-12-17 22:55:37 +000011539 // Want to form PANDN nodes, in the hopes of then easily combining them with
11540 // OR and AND nodes to form PBLEND/PSIGN.
11541 EVT VT = N->getValueType(0);
11542 if (VT != MVT::v2i64)
11543 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011544
Nate Begemanb65c1752010-12-17 22:55:37 +000011545 SDValue N0 = N->getOperand(0);
11546 SDValue N1 = N->getOperand(1);
11547 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011548
Nate Begemanb65c1752010-12-17 22:55:37 +000011549 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011550 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011551 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11552 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11553
11554 // Check RHS for vnot
11555 if (N1.getOpcode() == ISD::XOR &&
11556 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11557 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011558
Nate Begemanb65c1752010-12-17 22:55:37 +000011559 return SDValue();
11560}
11561
Evan Cheng760d1942010-01-04 21:22:48 +000011562static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011563 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011564 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011565 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011566 return SDValue();
11567
Evan Cheng760d1942010-01-04 21:22:48 +000011568 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011569 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011570 return SDValue();
11571
Evan Cheng760d1942010-01-04 21:22:48 +000011572 SDValue N0 = N->getOperand(0);
11573 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011574
Nate Begemanb65c1752010-12-17 22:55:37 +000011575 // look for psign/blend
11576 if (Subtarget->hasSSSE3()) {
11577 if (VT == MVT::v2i64) {
11578 // Canonicalize pandn to RHS
11579 if (N0.getOpcode() == X86ISD::PANDN)
11580 std::swap(N0, N1);
11581 // or (and (m, x), (pandn m, y))
11582 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11583 SDValue Mask = N1.getOperand(0);
11584 SDValue X = N1.getOperand(1);
11585 SDValue Y;
11586 if (N0.getOperand(0) == Mask)
11587 Y = N0.getOperand(1);
11588 if (N0.getOperand(1) == Mask)
11589 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011590
Nate Begemanb65c1752010-12-17 22:55:37 +000011591 // Check to see if the mask appeared in both the AND and PANDN and
11592 if (!Y.getNode())
11593 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011594
Nate Begemanb65c1752010-12-17 22:55:37 +000011595 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11596 if (Mask.getOpcode() != ISD::BITCAST ||
11597 X.getOpcode() != ISD::BITCAST ||
11598 Y.getOpcode() != ISD::BITCAST)
11599 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011600
Nate Begemanb65c1752010-12-17 22:55:37 +000011601 // Look through mask bitcast.
11602 Mask = Mask.getOperand(0);
11603 EVT MaskVT = Mask.getValueType();
11604
11605 // Validate that the Mask operand is a vector sra node. The sra node
11606 // will be an intrinsic.
11607 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11608 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011609
Nate Begemanb65c1752010-12-17 22:55:37 +000011610 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11611 // there is no psrai.b
11612 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11613 case Intrinsic::x86_sse2_psrai_w:
11614 case Intrinsic::x86_sse2_psrai_d:
11615 break;
11616 default: return SDValue();
11617 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011618
Nate Begemanb65c1752010-12-17 22:55:37 +000011619 // Check that the SRA is all signbits.
11620 SDValue SraC = Mask.getOperand(2);
11621 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11622 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11623 if ((SraAmt + 1) != EltBits)
11624 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011625
Nate Begemanb65c1752010-12-17 22:55:37 +000011626 DebugLoc DL = N->getDebugLoc();
11627
11628 // Now we know we at least have a plendvb with the mask val. See if
11629 // we can form a psignb/w/d.
11630 // psign = x.type == y.type == mask.type && y = sub(0, x);
11631 X = X.getOperand(0);
11632 Y = Y.getOperand(0);
11633 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11634 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11635 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11636 unsigned Opc = 0;
11637 switch (EltBits) {
11638 case 8: Opc = X86ISD::PSIGNB; break;
11639 case 16: Opc = X86ISD::PSIGNW; break;
11640 case 32: Opc = X86ISD::PSIGND; break;
11641 default: break;
11642 }
11643 if (Opc) {
11644 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11645 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11646 }
11647 }
11648 // PBLENDVB only available on SSE 4.1
11649 if (!Subtarget->hasSSE41())
11650 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011651
Nate Begemanb65c1752010-12-17 22:55:37 +000011652 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11653 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11654 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011655 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011656 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11657 }
11658 }
11659 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011660
Nate Begemanb65c1752010-12-17 22:55:37 +000011661 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011662 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11663 std::swap(N0, N1);
11664 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11665 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011666 if (!N0.hasOneUse() || !N1.hasOneUse())
11667 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011668
11669 SDValue ShAmt0 = N0.getOperand(1);
11670 if (ShAmt0.getValueType() != MVT::i8)
11671 return SDValue();
11672 SDValue ShAmt1 = N1.getOperand(1);
11673 if (ShAmt1.getValueType() != MVT::i8)
11674 return SDValue();
11675 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11676 ShAmt0 = ShAmt0.getOperand(0);
11677 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11678 ShAmt1 = ShAmt1.getOperand(0);
11679
11680 DebugLoc DL = N->getDebugLoc();
11681 unsigned Opc = X86ISD::SHLD;
11682 SDValue Op0 = N0.getOperand(0);
11683 SDValue Op1 = N1.getOperand(0);
11684 if (ShAmt0.getOpcode() == ISD::SUB) {
11685 Opc = X86ISD::SHRD;
11686 std::swap(Op0, Op1);
11687 std::swap(ShAmt0, ShAmt1);
11688 }
11689
Evan Cheng8b1190a2010-04-28 01:18:01 +000011690 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011691 if (ShAmt1.getOpcode() == ISD::SUB) {
11692 SDValue Sum = ShAmt1.getOperand(0);
11693 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011694 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11695 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11696 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11697 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011698 return DAG.getNode(Opc, DL, VT,
11699 Op0, Op1,
11700 DAG.getNode(ISD::TRUNCATE, DL,
11701 MVT::i8, ShAmt0));
11702 }
11703 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11704 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11705 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011706 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011707 return DAG.getNode(Opc, DL, VT,
11708 N0.getOperand(0), N1.getOperand(0),
11709 DAG.getNode(ISD::TRUNCATE, DL,
11710 MVT::i8, ShAmt0));
11711 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011712
Evan Cheng760d1942010-01-04 21:22:48 +000011713 return SDValue();
11714}
11715
Chris Lattner149a4e52008-02-22 02:09:43 +000011716/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011717static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011718 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011719 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11720 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011721 // A preferable solution to the general problem is to figure out the right
11722 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011723
11724 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011725 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011726 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011727 if (VT.getSizeInBits() != 64)
11728 return SDValue();
11729
Devang Patel578efa92009-06-05 21:57:13 +000011730 const Function *F = DAG.getMachineFunction().getFunction();
11731 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011732 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011733 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011734 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011735 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011736 isa<LoadSDNode>(St->getValue()) &&
11737 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11738 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011739 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011740 LoadSDNode *Ld = 0;
11741 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011742 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011743 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011744 // Must be a store of a load. We currently handle two cases: the load
11745 // is a direct child, and it's under an intervening TokenFactor. It is
11746 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011747 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011748 Ld = cast<LoadSDNode>(St->getChain());
11749 else if (St->getValue().hasOneUse() &&
11750 ChainVal->getOpcode() == ISD::TokenFactor) {
11751 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011752 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011753 TokenFactorIndex = i;
11754 Ld = cast<LoadSDNode>(St->getValue());
11755 } else
11756 Ops.push_back(ChainVal->getOperand(i));
11757 }
11758 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011759
Evan Cheng536e6672009-03-12 05:59:15 +000011760 if (!Ld || !ISD::isNormalLoad(Ld))
11761 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011762
Evan Cheng536e6672009-03-12 05:59:15 +000011763 // If this is not the MMX case, i.e. we are just turning i64 load/store
11764 // into f64 load/store, avoid the transformation if there are multiple
11765 // uses of the loaded value.
11766 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11767 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011768
Evan Cheng536e6672009-03-12 05:59:15 +000011769 DebugLoc LdDL = Ld->getDebugLoc();
11770 DebugLoc StDL = N->getDebugLoc();
11771 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11772 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11773 // pair instead.
11774 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011775 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011776 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11777 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011778 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011779 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011780 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011781 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011782 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011783 Ops.size());
11784 }
Evan Cheng536e6672009-03-12 05:59:15 +000011785 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011786 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011787 St->isVolatile(), St->isNonTemporal(),
11788 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011789 }
Evan Cheng536e6672009-03-12 05:59:15 +000011790
11791 // Otherwise, lower to two pairs of 32-bit loads / stores.
11792 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011793 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11794 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011795
Owen Anderson825b72b2009-08-11 20:47:22 +000011796 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011797 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011798 Ld->isVolatile(), Ld->isNonTemporal(),
11799 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011800 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011801 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011802 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011803 MinAlign(Ld->getAlignment(), 4));
11804
11805 SDValue NewChain = LoLd.getValue(1);
11806 if (TokenFactorIndex != -1) {
11807 Ops.push_back(LoLd);
11808 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011809 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011810 Ops.size());
11811 }
11812
11813 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011814 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11815 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011816
11817 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011818 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011819 St->isVolatile(), St->isNonTemporal(),
11820 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011821 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011822 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011823 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011824 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011825 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011826 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011827 }
Dan Gohman475871a2008-07-27 21:46:04 +000011828 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011829}
11830
Chris Lattner6cf73262008-01-25 06:14:17 +000011831/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11832/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011833static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011834 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11835 // F[X]OR(0.0, x) -> x
11836 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011837 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11838 if (C->getValueAPF().isPosZero())
11839 return N->getOperand(1);
11840 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11841 if (C->getValueAPF().isPosZero())
11842 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011843 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011844}
11845
11846/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011847static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011848 // FAND(0.0, x) -> 0.0
11849 // FAND(x, 0.0) -> 0.0
11850 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11851 if (C->getValueAPF().isPosZero())
11852 return N->getOperand(0);
11853 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11854 if (C->getValueAPF().isPosZero())
11855 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011856 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011857}
11858
Dan Gohmane5af2d32009-01-29 01:59:02 +000011859static SDValue PerformBTCombine(SDNode *N,
11860 SelectionDAG &DAG,
11861 TargetLowering::DAGCombinerInfo &DCI) {
11862 // BT ignores high bits in the bit index operand.
11863 SDValue Op1 = N->getOperand(1);
11864 if (Op1.hasOneUse()) {
11865 unsigned BitWidth = Op1.getValueSizeInBits();
11866 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11867 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011868 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11869 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011870 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011871 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11872 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11873 DCI.CommitTargetLoweringOpt(TLO);
11874 }
11875 return SDValue();
11876}
Chris Lattner83e6c992006-10-04 06:57:07 +000011877
Eli Friedman7a5e5552009-06-07 06:52:44 +000011878static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11879 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011880 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011881 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011882 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011883 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011884 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011885 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011886 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011887 }
11888 return SDValue();
11889}
11890
Evan Cheng2e489c42009-12-16 00:53:11 +000011891static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11892 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11893 // (and (i32 x86isd::setcc_carry), 1)
11894 // This eliminates the zext. This transformation is necessary because
11895 // ISD::SETCC is always legalized to i8.
11896 DebugLoc dl = N->getDebugLoc();
11897 SDValue N0 = N->getOperand(0);
11898 EVT VT = N->getValueType(0);
11899 if (N0.getOpcode() == ISD::AND &&
11900 N0.hasOneUse() &&
11901 N0.getOperand(0).hasOneUse()) {
11902 SDValue N00 = N0.getOperand(0);
11903 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11904 return SDValue();
11905 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11906 if (!C || C->getZExtValue() != 1)
11907 return SDValue();
11908 return DAG.getNode(ISD::AND, dl, VT,
11909 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11910 N00.getOperand(0), N00.getOperand(1)),
11911 DAG.getConstant(1, VT));
11912 }
11913
11914 return SDValue();
11915}
11916
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011917// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11918static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11919 unsigned X86CC = N->getConstantOperandVal(0);
11920 SDValue EFLAG = N->getOperand(1);
11921 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011922
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011923 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11924 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11925 // cases.
11926 if (X86CC == X86::COND_B)
11927 return DAG.getNode(ISD::AND, DL, MVT::i8,
11928 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11929 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11930 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011931
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011932 return SDValue();
11933}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011934
Chris Lattner23a01992010-12-20 01:37:09 +000011935// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11936static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11937 X86TargetLowering::DAGCombinerInfo &DCI) {
11938 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11939 // the result is either zero or one (depending on the input carry bit).
11940 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11941 if (X86::isZeroNode(N->getOperand(0)) &&
11942 X86::isZeroNode(N->getOperand(1)) &&
11943 // We don't have a good way to replace an EFLAGS use, so only do this when
11944 // dead right now.
11945 SDValue(N, 1).use_empty()) {
11946 DebugLoc DL = N->getDebugLoc();
11947 EVT VT = N->getValueType(0);
11948 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11949 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11950 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11951 DAG.getConstant(X86::COND_B,MVT::i8),
11952 N->getOperand(2)),
11953 DAG.getConstant(1, VT));
11954 return DCI.CombineTo(N, Res1, CarryOut);
11955 }
11956
11957 return SDValue();
11958}
11959
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011960// fold (add Y, (sete X, 0)) -> adc 0, Y
11961// (add Y, (setne X, 0)) -> sbb -1, Y
11962// (sub (sete X, 0), Y) -> sbb 0, Y
11963// (sub (setne X, 0), Y) -> adc -1, Y
11964static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11965 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011966
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011967 // Look through ZExts.
11968 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11969 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11970 return SDValue();
11971
11972 SDValue SetCC = Ext.getOperand(0);
11973 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11974 return SDValue();
11975
11976 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11977 if (CC != X86::COND_E && CC != X86::COND_NE)
11978 return SDValue();
11979
11980 SDValue Cmp = SetCC.getOperand(1);
11981 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011982 !X86::isZeroNode(Cmp.getOperand(1)) ||
11983 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011984 return SDValue();
11985
11986 SDValue CmpOp0 = Cmp.getOperand(0);
11987 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11988 DAG.getConstant(1, CmpOp0.getValueType()));
11989
11990 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11991 if (CC == X86::COND_NE)
11992 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11993 DL, OtherVal.getValueType(), OtherVal,
11994 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11995 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11996 DL, OtherVal.getValueType(), OtherVal,
11997 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11998}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011999
Dan Gohman475871a2008-07-27 21:46:04 +000012000SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000012001 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000012002 SelectionDAG &DAG = DCI.DAG;
12003 switch (N->getOpcode()) {
12004 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000012005 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012006 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000012007 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000012008 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000012009 case ISD::ADD:
12010 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000012011 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000012012 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000012013 case ISD::SHL:
12014 case ISD::SRA:
12015 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000012016 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000012017 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000012018 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000012019 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000012020 case X86ISD::FOR: return PerformFORCombine(N, DAG);
12021 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000012022 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000012023 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000012024 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000012025 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012026 case X86ISD::SHUFPS: // Handle all target specific shuffles
12027 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000012028 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012029 case X86ISD::PUNPCKHBW:
12030 case X86ISD::PUNPCKHWD:
12031 case X86ISD::PUNPCKHDQ:
12032 case X86ISD::PUNPCKHQDQ:
12033 case X86ISD::UNPCKHPS:
12034 case X86ISD::UNPCKHPD:
12035 case X86ISD::PUNPCKLBW:
12036 case X86ISD::PUNPCKLWD:
12037 case X86ISD::PUNPCKLDQ:
12038 case X86ISD::PUNPCKLQDQ:
12039 case X86ISD::UNPCKLPS:
12040 case X86ISD::UNPCKLPD:
David Greenefbf05d32011-02-22 23:31:46 +000012041 case X86ISD::VUNPCKLPS:
12042 case X86ISD::VUNPCKLPD:
12043 case X86ISD::VUNPCKLPSY:
12044 case X86ISD::VUNPCKLPDY:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000012045 case X86ISD::MOVHLPS:
12046 case X86ISD::MOVLHPS:
12047 case X86ISD::PSHUFD:
12048 case X86ISD::PSHUFHW:
12049 case X86ISD::PSHUFLW:
12050 case X86ISD::MOVSS:
12051 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000012052 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000012053 }
12054
Dan Gohman475871a2008-07-27 21:46:04 +000012055 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000012056}
12057
Evan Chenge5b51ac2010-04-17 06:13:15 +000012058/// isTypeDesirableForOp - Return true if the target has native support for
12059/// the specified value type and it is 'desirable' to use the type for the
12060/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
12061/// instruction encodings are longer and some i16 instructions are slow.
12062bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
12063 if (!isTypeLegal(VT))
12064 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012065 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000012066 return true;
12067
12068 switch (Opc) {
12069 default:
12070 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000012071 case ISD::LOAD:
12072 case ISD::SIGN_EXTEND:
12073 case ISD::ZERO_EXTEND:
12074 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012075 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000012076 case ISD::SRL:
12077 case ISD::SUB:
12078 case ISD::ADD:
12079 case ISD::MUL:
12080 case ISD::AND:
12081 case ISD::OR:
12082 case ISD::XOR:
12083 return false;
12084 }
12085}
12086
12087/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000012088/// beneficial for dag combiner to promote the specified node. If true, it
12089/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000012090bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012091 EVT VT = Op.getValueType();
12092 if (VT != MVT::i16)
12093 return false;
12094
Evan Cheng4c26e932010-04-19 19:29:22 +000012095 bool Promote = false;
12096 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012097 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000012098 default: break;
12099 case ISD::LOAD: {
12100 LoadSDNode *LD = cast<LoadSDNode>(Op);
12101 // If the non-extending load has a single use and it's not live out, then it
12102 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012103 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
12104 Op.hasOneUse()*/) {
12105 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
12106 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
12107 // The only case where we'd want to promote LOAD (rather then it being
12108 // promoted as an operand is when it's only use is liveout.
12109 if (UI->getOpcode() != ISD::CopyToReg)
12110 return false;
12111 }
12112 }
Evan Cheng4c26e932010-04-19 19:29:22 +000012113 Promote = true;
12114 break;
12115 }
12116 case ISD::SIGN_EXTEND:
12117 case ISD::ZERO_EXTEND:
12118 case ISD::ANY_EXTEND:
12119 Promote = true;
12120 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012121 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000012122 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000012123 SDValue N0 = Op.getOperand(0);
12124 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000012125 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000012126 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012127 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000012128 break;
12129 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000012130 case ISD::ADD:
12131 case ISD::MUL:
12132 case ISD::AND:
12133 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000012134 case ISD::XOR:
12135 Commute = true;
12136 // fallthrough
12137 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000012138 SDValue N0 = Op.getOperand(0);
12139 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000012140 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012141 return false;
12142 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000012143 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012144 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000012145 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000012146 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000012147 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012148 }
12149 }
12150
12151 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000012152 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000012153}
12154
Evan Cheng60c07e12006-07-05 22:17:51 +000012155//===----------------------------------------------------------------------===//
12156// X86 Inline Assembly Support
12157//===----------------------------------------------------------------------===//
12158
Chris Lattnerb8105652009-07-20 17:51:36 +000012159bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
12160 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000012161
12162 std::string AsmStr = IA->getAsmString();
12163
12164 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000012165 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000012166 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000012167
12168 switch (AsmPieces.size()) {
12169 default: return false;
12170 case 1:
12171 AsmStr = AsmPieces[0];
12172 AsmPieces.clear();
12173 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
12174
Evan Cheng55d42002011-01-08 01:24:27 +000012175 // FIXME: this should verify that we are targetting a 486 or better. If not,
12176 // we will turn this bswap into something that will be lowered to logical ops
12177 // instead of emitting the bswap asm. For now, we don't support 486 or lower
12178 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000012179 // bswap $0
12180 if (AsmPieces.size() == 2 &&
12181 (AsmPieces[0] == "bswap" ||
12182 AsmPieces[0] == "bswapq" ||
12183 AsmPieces[0] == "bswapl") &&
12184 (AsmPieces[1] == "$0" ||
12185 AsmPieces[1] == "${0:q}")) {
12186 // No need to check constraints, nothing other than the equivalent of
12187 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000012188 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12189 if (!Ty || Ty->getBitWidth() % 16 != 0)
12190 return false;
12191 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000012192 }
12193 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012194 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012195 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012196 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000012197 AsmPieces[1] == "$$8," &&
12198 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000012199 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12200 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012201 const std::string &ConstraintsStr = IA->getConstraintString();
12202 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +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);
Dan Gohman0ef701e2010-03-04 19:58:08 +000012213 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012214 }
12215 break;
12216 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000012217 if (CI->getType()->isIntegerTy(32) &&
12218 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
12219 SmallVector<StringRef, 4> Words;
12220 SplitString(AsmPieces[0], Words, " \t,");
12221 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12222 Words[2] == "${0:w}") {
12223 Words.clear();
12224 SplitString(AsmPieces[1], Words, " \t,");
12225 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
12226 Words[2] == "$0") {
12227 Words.clear();
12228 SplitString(AsmPieces[2], Words, " \t,");
12229 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
12230 Words[2] == "${0:w}") {
12231 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012232 const std::string &ConstraintsStr = IA->getConstraintString();
12233 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000012234 std::sort(AsmPieces.begin(), AsmPieces.end());
12235 if (AsmPieces.size() == 4 &&
12236 AsmPieces[0] == "~{cc}" &&
12237 AsmPieces[1] == "~{dirflag}" &&
12238 AsmPieces[2] == "~{flags}" &&
12239 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000012240 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12241 if (!Ty || Ty->getBitWidth() % 16 != 0)
12242 return false;
12243 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000012244 }
12245 }
12246 }
12247 }
12248 }
Evan Cheng55d42002011-01-08 01:24:27 +000012249
12250 if (CI->getType()->isIntegerTy(64)) {
12251 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
12252 if (Constraints.size() >= 2 &&
12253 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
12254 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
12255 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
12256 SmallVector<StringRef, 4> Words;
12257 SplitString(AsmPieces[0], Words, " \t");
12258 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000012259 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000012260 SplitString(AsmPieces[1], Words, " \t");
12261 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
12262 Words.clear();
12263 SplitString(AsmPieces[2], Words, " \t,");
12264 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
12265 Words[2] == "%edx") {
12266 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
12267 if (!Ty || Ty->getBitWidth() % 16 != 0)
12268 return false;
12269 return IntrinsicLowering::LowerToByteSwap(CI);
12270 }
Chris Lattnerb8105652009-07-20 17:51:36 +000012271 }
12272 }
12273 }
12274 }
12275 break;
12276 }
12277 return false;
12278}
12279
12280
12281
Chris Lattnerf4dff842006-07-11 02:54:03 +000012282/// getConstraintType - Given a constraint letter, return the type of
12283/// constraint it is for this target.
12284X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000012285X86TargetLowering::getConstraintType(const std::string &Constraint) const {
12286 if (Constraint.size() == 1) {
12287 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000012288 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000012289 case 'q':
12290 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000012291 case 'f':
12292 case 't':
12293 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000012294 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000012295 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000012296 case 'Y':
12297 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000012298 case 'a':
12299 case 'b':
12300 case 'c':
12301 case 'd':
12302 case 'S':
12303 case 'D':
12304 case 'A':
12305 return C_Register;
12306 case 'I':
12307 case 'J':
12308 case 'K':
12309 case 'L':
12310 case 'M':
12311 case 'N':
12312 case 'G':
12313 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000012314 case 'e':
12315 case 'Z':
12316 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000012317 default:
12318 break;
12319 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000012320 }
Chris Lattner4234f572007-03-25 02:14:49 +000012321 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000012322}
12323
John Thompson44ab89e2010-10-29 17:29:13 +000012324/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000012325/// This object must already have been set up with the operand type
12326/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000012327TargetLowering::ConstraintWeight
12328 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000012329 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000012330 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012331 Value *CallOperandVal = info.CallOperandVal;
12332 // If we don't have a value, we can't do a match,
12333 // but allow it at the lowest weight.
12334 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000012335 return CW_Default;
12336 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000012337 // Look at the constraint type.
12338 switch (*constraint) {
12339 default:
John Thompson44ab89e2010-10-29 17:29:13 +000012340 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12341 case 'R':
12342 case 'q':
12343 case 'Q':
12344 case 'a':
12345 case 'b':
12346 case 'c':
12347 case 'd':
12348 case 'S':
12349 case 'D':
12350 case 'A':
12351 if (CallOperandVal->getType()->isIntegerTy())
12352 weight = CW_SpecificReg;
12353 break;
12354 case 'f':
12355 case 't':
12356 case 'u':
12357 if (type->isFloatingPointTy())
12358 weight = CW_SpecificReg;
12359 break;
12360 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012361 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012362 weight = CW_SpecificReg;
12363 break;
12364 case 'x':
12365 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012366 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012367 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012368 break;
12369 case 'I':
12370 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12371 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012372 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012373 }
12374 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012375 case 'J':
12376 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12377 if (C->getZExtValue() <= 63)
12378 weight = CW_Constant;
12379 }
12380 break;
12381 case 'K':
12382 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12383 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12384 weight = CW_Constant;
12385 }
12386 break;
12387 case 'L':
12388 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12389 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12390 weight = CW_Constant;
12391 }
12392 break;
12393 case 'M':
12394 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12395 if (C->getZExtValue() <= 3)
12396 weight = CW_Constant;
12397 }
12398 break;
12399 case 'N':
12400 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12401 if (C->getZExtValue() <= 0xff)
12402 weight = CW_Constant;
12403 }
12404 break;
12405 case 'G':
12406 case 'C':
12407 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12408 weight = CW_Constant;
12409 }
12410 break;
12411 case 'e':
12412 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12413 if ((C->getSExtValue() >= -0x80000000LL) &&
12414 (C->getSExtValue() <= 0x7fffffffLL))
12415 weight = CW_Constant;
12416 }
12417 break;
12418 case 'Z':
12419 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12420 if (C->getZExtValue() <= 0xffffffff)
12421 weight = CW_Constant;
12422 }
12423 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012424 }
12425 return weight;
12426}
12427
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012428/// LowerXConstraint - try to replace an X constraint, which matches anything,
12429/// with another that has more specific requirements based on the type of the
12430/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012431const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012432LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012433 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12434 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012435 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012436 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012437 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012438 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012439 return "x";
12440 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012441
Chris Lattner5e764232008-04-26 23:02:14 +000012442 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012443}
12444
Chris Lattner48884cd2007-08-25 00:47:38 +000012445/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12446/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012447void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012448 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012449 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012450 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012451 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012452
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012453 switch (Constraint) {
12454 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012455 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012457 if (C->getZExtValue() <= 31) {
12458 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012459 break;
12460 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012461 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012462 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012463 case 'J':
12464 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012465 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012466 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12467 break;
12468 }
12469 }
12470 return;
12471 case 'K':
12472 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012473 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012474 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12475 break;
12476 }
12477 }
12478 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012479 case 'N':
12480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012481 if (C->getZExtValue() <= 255) {
12482 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012483 break;
12484 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012485 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012486 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012487 case 'e': {
12488 // 32-bit signed value
12489 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012490 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12491 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012492 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012493 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012494 break;
12495 }
12496 // FIXME gcc accepts some relocatable values here too, but only in certain
12497 // memory models; it's complicated.
12498 }
12499 return;
12500 }
12501 case 'Z': {
12502 // 32-bit unsigned value
12503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012504 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12505 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012506 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12507 break;
12508 }
12509 }
12510 // FIXME gcc accepts some relocatable values here too, but only in certain
12511 // memory models; it's complicated.
12512 return;
12513 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012514 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012515 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012516 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012517 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012518 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012519 break;
12520 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012521
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012522 // In any sort of PIC mode addresses need to be computed at runtime by
12523 // adding in a register or some sort of table lookup. These can't
12524 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012525 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012526 return;
12527
Chris Lattnerdc43a882007-05-03 16:52:29 +000012528 // If we are in non-pic codegen mode, we allow the address of a global (with
12529 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012530 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012531 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012532
Chris Lattner49921962009-05-08 18:23:14 +000012533 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12534 while (1) {
12535 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12536 Offset += GA->getOffset();
12537 break;
12538 } else if (Op.getOpcode() == ISD::ADD) {
12539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12540 Offset += C->getZExtValue();
12541 Op = Op.getOperand(0);
12542 continue;
12543 }
12544 } else if (Op.getOpcode() == ISD::SUB) {
12545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12546 Offset += -C->getZExtValue();
12547 Op = Op.getOperand(0);
12548 continue;
12549 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012550 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012551
Chris Lattner49921962009-05-08 18:23:14 +000012552 // Otherwise, this isn't something we can handle, reject it.
12553 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012554 }
Eric Christopherfd179292009-08-27 18:07:15 +000012555
Dan Gohman46510a72010-04-15 01:51:59 +000012556 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012557 // If we require an extra load to get this address, as in PIC mode, we
12558 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012559 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12560 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012561 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012562
Devang Patel0d881da2010-07-06 22:08:15 +000012563 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12564 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012565 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012566 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012567 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012568
Gabor Greifba36cb52008-08-28 21:40:38 +000012569 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012570 Ops.push_back(Result);
12571 return;
12572 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012573 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012574}
12575
Chris Lattner259e97c2006-01-31 19:43:35 +000012576std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012577getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012578 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012579 if (Constraint.size() == 1) {
12580 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012581 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012582 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012583 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12584 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012585 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012586 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12587 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12588 X86::R10D,X86::R11D,X86::R12D,
12589 X86::R13D,X86::R14D,X86::R15D,
12590 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012591 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012592 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12593 X86::SI, X86::DI, X86::R8W,X86::R9W,
12594 X86::R10W,X86::R11W,X86::R12W,
12595 X86::R13W,X86::R14W,X86::R15W,
12596 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012597 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012598 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12599 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12600 X86::R10B,X86::R11B,X86::R12B,
12601 X86::R13B,X86::R14B,X86::R15B,
12602 X86::BPL, X86::SPL, 0);
12603
Owen Anderson825b72b2009-08-11 20:47:22 +000012604 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012605 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12606 X86::RSI, X86::RDI, X86::R8, X86::R9,
12607 X86::R10, X86::R11, X86::R12,
12608 X86::R13, X86::R14, X86::R15,
12609 X86::RBP, X86::RSP, 0);
12610
12611 break;
12612 }
Eric Christopherfd179292009-08-27 18:07:15 +000012613 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012614 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012615 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012616 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012617 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012618 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012619 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012620 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012621 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012622 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12623 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012624 }
12625 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012626
Chris Lattner1efa40f2006-02-22 00:56:39 +000012627 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012628}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012629
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012630std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012631X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012632 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012633 // First, see if this is a constraint that directly corresponds to an LLVM
12634 // register class.
12635 if (Constraint.size() == 1) {
12636 // GCC Constraint Letters
12637 switch (Constraint[0]) {
12638 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012639 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012640 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012641 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012642 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012643 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012644 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012645 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012646 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012647 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012648 case 'R': // LEGACY_REGS
12649 if (VT == MVT::i8)
12650 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12651 if (VT == MVT::i16)
12652 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12653 if (VT == MVT::i32 || !Subtarget->is64Bit())
12654 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12655 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012656 case 'f': // FP Stack registers.
12657 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12658 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012659 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012660 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012661 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012662 return std::make_pair(0U, X86::RFP64RegisterClass);
12663 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012664 case 'y': // MMX_REGS if MMX allowed.
12665 if (!Subtarget->hasMMX()) break;
12666 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012667 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012668 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012669 // FALL THROUGH.
12670 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012671 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012672
Owen Anderson825b72b2009-08-11 20:47:22 +000012673 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012674 default: break;
12675 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012676 case MVT::f32:
12677 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012678 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012679 case MVT::f64:
12680 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012681 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012682 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012683 case MVT::v16i8:
12684 case MVT::v8i16:
12685 case MVT::v4i32:
12686 case MVT::v2i64:
12687 case MVT::v4f32:
12688 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012689 return std::make_pair(0U, X86::VR128RegisterClass);
12690 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012691 break;
12692 }
12693 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012694
Chris Lattnerf76d1802006-07-31 23:26:50 +000012695 // Use the default implementation in TargetLowering to convert the register
12696 // constraint into a member of a register class.
12697 std::pair<unsigned, const TargetRegisterClass*> Res;
12698 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012699
12700 // Not found as a standard register?
12701 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012702 // Map st(0) -> st(7) -> ST0
12703 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12704 tolower(Constraint[1]) == 's' &&
12705 tolower(Constraint[2]) == 't' &&
12706 Constraint[3] == '(' &&
12707 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12708 Constraint[5] == ')' &&
12709 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012710
Chris Lattner56d77c72009-09-13 22:41:48 +000012711 Res.first = X86::ST0+Constraint[4]-'0';
12712 Res.second = X86::RFP80RegisterClass;
12713 return Res;
12714 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012715
Chris Lattner56d77c72009-09-13 22:41:48 +000012716 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012717 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012718 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012719 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012720 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012721 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012722
12723 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012724 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012725 Res.first = X86::EFLAGS;
12726 Res.second = X86::CCRRegisterClass;
12727 return Res;
12728 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012729
Dale Johannesen330169f2008-11-13 21:52:36 +000012730 // 'A' means EAX + EDX.
12731 if (Constraint == "A") {
12732 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012733 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012734 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012735 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012736 return Res;
12737 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012738
Chris Lattnerf76d1802006-07-31 23:26:50 +000012739 // Otherwise, check to see if this is a register class of the wrong value
12740 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12741 // turn into {ax},{dx}.
12742 if (Res.second->hasType(VT))
12743 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012744
Chris Lattnerf76d1802006-07-31 23:26:50 +000012745 // All of the single-register GCC register classes map their values onto
12746 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12747 // really want an 8-bit or 32-bit register, map to the appropriate register
12748 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012749 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012750 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012751 unsigned DestReg = 0;
12752 switch (Res.first) {
12753 default: break;
12754 case X86::AX: DestReg = X86::AL; break;
12755 case X86::DX: DestReg = X86::DL; break;
12756 case X86::CX: DestReg = X86::CL; break;
12757 case X86::BX: DestReg = X86::BL; break;
12758 }
12759 if (DestReg) {
12760 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012761 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012762 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012763 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012764 unsigned DestReg = 0;
12765 switch (Res.first) {
12766 default: break;
12767 case X86::AX: DestReg = X86::EAX; break;
12768 case X86::DX: DestReg = X86::EDX; break;
12769 case X86::CX: DestReg = X86::ECX; break;
12770 case X86::BX: DestReg = X86::EBX; break;
12771 case X86::SI: DestReg = X86::ESI; break;
12772 case X86::DI: DestReg = X86::EDI; break;
12773 case X86::BP: DestReg = X86::EBP; break;
12774 case X86::SP: DestReg = X86::ESP; break;
12775 }
12776 if (DestReg) {
12777 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012778 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012779 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012780 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012781 unsigned DestReg = 0;
12782 switch (Res.first) {
12783 default: break;
12784 case X86::AX: DestReg = X86::RAX; break;
12785 case X86::DX: DestReg = X86::RDX; break;
12786 case X86::CX: DestReg = X86::RCX; break;
12787 case X86::BX: DestReg = X86::RBX; break;
12788 case X86::SI: DestReg = X86::RSI; break;
12789 case X86::DI: DestReg = X86::RDI; break;
12790 case X86::BP: DestReg = X86::RBP; break;
12791 case X86::SP: DestReg = X86::RSP; break;
12792 }
12793 if (DestReg) {
12794 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012795 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012796 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012797 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012798 } else if (Res.second == X86::FR32RegisterClass ||
12799 Res.second == X86::FR64RegisterClass ||
12800 Res.second == X86::VR128RegisterClass) {
12801 // Handle references to XMM physical registers that got mapped into the
12802 // wrong class. This can happen with constraints like {xmm0} where the
12803 // target independent register mapper will just pick the first match it can
12804 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012805 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012806 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012807 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012808 Res.second = X86::FR64RegisterClass;
12809 else if (X86::VR128RegisterClass->hasType(VT))
12810 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012811 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012812
Chris Lattnerf76d1802006-07-31 23:26:50 +000012813 return Res;
12814}